Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2113T-T09-3#175 from NgYaoDong/NgYaoDon…
Browse files Browse the repository at this point in the history
…g-PED_Fix

Add fix for bug when deleting product, but transaction still lingering.
  • Loading branch information
cheeseong2001 authored Apr 7, 2024
2 parents e48449d + 67ddb93 commit 44cd7d9
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/diagrams/ArchitectureSequenceDiagram.puml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ command --[COMMANDS_COLOR]> ui
deactivate command
ui --[UI_COLOR]> user : "Product has been deleted."

stockpal -[STOCKPAL_COLOR]> storage : save(productList)
stockpal -[STOCKPAL_COLOR]> storage : save(productList), save(transactionList)
activate storage STORAGE_COLOR

storage -[STORAGE_COLOR]> storage : Save to file
Expand Down
9 changes: 4 additions & 5 deletions docs/diagrams/CommandClassDiagram.puml
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,18 @@ transaction -left-|> command
exit -up-|> command
help -up-|> command
help -down[hidden]-> new
find -right[hidden]-> list
find -up[hidden]-> delete
delete -right[hidden]-> edit
find -up[hidden]-> edit

delete -up-|> action
new -up-|> action
edit -up-|> action
find -up-|> action
list -up-|> action

inflow -up-|> transaction
outflow -up-|> transaction
delete -up-|> transaction
delete -up[hidden]-|> inflow
history -up-|> transaction
outflow -up-|> transaction
history -up[hidden]-> outflow

@enduml
Binary file modified docs/images/ArchitectureSequenceDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/CommandClassDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/team/ngyaodong.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Given below are my contributions to the project.

* **Enhancements to existing features**:
* Refactored code to reduce coupling between classes (Pull Request [#50](https://github.com/AY2324S2-CS2113T-T09-3/tp/pull/50))
* Added code to ensure deletion of transactions when products are deleted (Pull Request [#175](https://github.com/AY2324S2-CS2113T-T09-3/tp/pull/175))

* **Documentation**:
* User Guide:
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/seedu/stockpal/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import seedu.stockpal.common.FormatUtils;
import seedu.stockpal.common.Messages;
import seedu.stockpal.data.ProductList;
import seedu.stockpal.data.TransactionList;
import seedu.stockpal.data.product.Pid;
import seedu.stockpal.exceptions.PidNotFoundException;
import seedu.stockpal.ui.Ui;

import java.util.logging.Level;
import java.util.logging.Logger;

//@@author cheeseong2001
/**
* DeleteCommand class is responsible for setting up and executing the delete feature to delete a particular product
* of a specific PID from the product list of StockPal.
*/
public class DeleteCommand extends ListActionCommand {
public class DeleteCommand extends TransactionActionCommand {

public static final String COMMAND_KEYWORD = "delete";
public static final String COMMAND_DESCRIPTION = "Deletes an existing product from the inventory.";
Expand All @@ -40,8 +42,9 @@ public DeleteCommand(Integer pid) {
* @throws PidNotFoundException If product with PID that user specified cannot be found in productList
*/
@Override
public void execute(ProductList productList) throws PidNotFoundException {
public void execute(ProductList productList, TransactionList transactionList) throws PidNotFoundException {
productList.deleteProduct(pid);
transactionList.deleteTransactions(pid);
Ui.printDeleteSuccessMessage();
LOGGER.log(Level.INFO, Messages.MESSAGE_DELETE_SUCCESS);
}
Expand All @@ -52,5 +55,4 @@ public static String commandDetails() {
, COMMAND_USAGE, COMMAND_FLAGS, COMMAND_FLAG_DESCRIPTIONS);
return formattedDetails;
}
//@@author cheeseong2001
}
10 changes: 10 additions & 0 deletions src/main/java/seedu/stockpal/data/TransactionList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.stockpal.data;

import seedu.stockpal.common.Messages;
import seedu.stockpal.data.product.Pid;
import seedu.stockpal.ui.Ui;

import java.util.List;
Expand Down Expand Up @@ -71,4 +72,13 @@ public Transaction get(int i) {
return transactions.get(i);
}

//@@author NgYaoDong
/**
* Delete Transactions related to the product getting deleted.
*
* @param pid PID of the product being deleted.
*/
public void deleteTransactions(Pid pid) {
transactions.removeIf(transaction -> transaction.getPid() == pid.getPid());
}
}
8 changes: 6 additions & 2 deletions src/test/java/seedu/stockpal/commands/DeleteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import seedu.stockpal.data.ProductList;
import seedu.stockpal.data.TransactionList;
import seedu.stockpal.data.product.Product;
import seedu.stockpal.exceptions.PidNotFoundException;
import seedu.stockpal.exceptions.StockPalException;
Expand All @@ -11,9 +12,11 @@
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

//@@author cheeseong2001
public class DeleteCommandTest {
public ProductList testProductList;
public ProductList expectedProductList;
public TransactionList transactionList;

@BeforeEach
public void setUp() {
Expand All @@ -22,6 +25,7 @@ public void setUp() {
Product testProduct3 = new Product("Test3", 3, null, null, 3);
expectedProductList = new ProductList();
testProductList = new ProductList();
transactionList = new TransactionList();

testProductList.addProduct(testProduct1);
testProductList.addProduct(testProduct2);
Expand All @@ -37,7 +41,7 @@ public void execute_productPidExists_success() {
DeleteCommand testDeleteCommand = new DeleteCommand(pidToDelete);
expectedProductList.addProduct(expectedProduct2);
expectedProductList.addProduct(expectedProduct3);
testDeleteCommand.execute(testProductList);
testDeleteCommand.execute(testProductList, transactionList);
} catch (StockPalException spe) {
fail();
}
Expand All @@ -62,6 +66,6 @@ public void execute_productPidDoesNotExist_pidNotFoundExceptionThrown() {
expectedProductList.addProduct(expectedProduct1);
expectedProductList.addProduct(expectedProduct2);
expectedProductList.addProduct(expectedProduct3);
assertThrows(PidNotFoundException.class, () -> testDeleteCommand.execute(expectedProductList));
assertThrows(PidNotFoundException.class, () -> testDeleteCommand.execute(expectedProductList, transactionList));
}
}

0 comments on commit 44cd7d9

Please sign in to comment.