From e9752451da890c33b8a93115716c3b7b9866a0cf Mon Sep 17 00:00:00 2001 From: nkotaa Date: Tue, 19 Mar 2024 20:35:03 +0800 Subject: [PATCH 1/3] Refactor command classes In our command classes, execute() returns a string for the Ui to print. This is unintuitive and conveys no information about the success or failure of the execution action. Let's have execute() return its result status instead. This would make it easier to write tests and assertions, aligning with our goals for a more robust application. --- src/main/java/seedu/binbash/command/AddCommand.java | 5 +++-- src/main/java/seedu/binbash/command/ByeCommand.java | 5 +++-- src/main/java/seedu/binbash/command/Command.java | 7 ++++++- .../java/seedu/binbash/command/DeleteCommand.java | 11 ++++++----- src/main/java/seedu/binbash/command/ListCommand.java | 5 +++-- .../java/seedu/binbash/command/SearchCommand.java | 5 +++-- 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main/java/seedu/binbash/command/AddCommand.java b/src/main/java/seedu/binbash/command/AddCommand.java index 71e950e758..ad495f566b 100644 --- a/src/main/java/seedu/binbash/command/AddCommand.java +++ b/src/main/java/seedu/binbash/command/AddCommand.java @@ -42,8 +42,9 @@ public AddCommand(ItemList itemList, String itemName, String itemDescription, in } @Override - public String execute() { - return itemList.addItem(itemName, itemDescription, itemQuantity, itemExpirationDate, + public boolean execute() { + executionUiOutput = itemList.addItem(itemName, itemDescription, itemQuantity, itemExpirationDate, itemSalePrice, itemCostPrice); + return true; } } diff --git a/src/main/java/seedu/binbash/command/ByeCommand.java b/src/main/java/seedu/binbash/command/ByeCommand.java index 4bc5384a5f..adb2350c0d 100644 --- a/src/main/java/seedu/binbash/command/ByeCommand.java +++ b/src/main/java/seedu/binbash/command/ByeCommand.java @@ -9,7 +9,8 @@ public ByeCommand(ItemList itemList) { } @Override - public String execute() { - return ""; + public boolean execute() { + executionUiOutput = ""; + return true; } } diff --git a/src/main/java/seedu/binbash/command/Command.java b/src/main/java/seedu/binbash/command/Command.java index 1d53ee5340..6238dfa511 100644 --- a/src/main/java/seedu/binbash/command/Command.java +++ b/src/main/java/seedu/binbash/command/Command.java @@ -6,11 +6,16 @@ public abstract class Command { protected ItemList itemList; protected Logger commandLogger; + protected String executionUiOutput; protected Command(ItemList itemList) { this.itemList = itemList; commandLogger = Logger.getLogger("CommandLogger"); } - public abstract String execute(); + public String getExecutionUiOutput() { + return executionUiOutput; + } + + public abstract boolean execute(); } diff --git a/src/main/java/seedu/binbash/command/DeleteCommand.java b/src/main/java/seedu/binbash/command/DeleteCommand.java index eb64683fb0..98c62973ae 100644 --- a/src/main/java/seedu/binbash/command/DeleteCommand.java +++ b/src/main/java/seedu/binbash/command/DeleteCommand.java @@ -30,15 +30,16 @@ public DeleteCommand(ItemList itemList, String keyword) { )); } - public String execute() { + public boolean execute() { if (isIndex) { // Ensure index out of bounds error is caught by Parser. assert index > 0 && index <= itemList.getItemCount(); commandLogger.log(Level.INFO, "Delete identifier is detected as an index"); - return itemList.deleteItem(index); + executionUiOutput = itemList.deleteItem(index); + } else { + commandLogger.log(Level.INFO, "Delete identifier is detected as an item name"); + executionUiOutput = itemList.deleteItem(keyword); } - - commandLogger.log(Level.INFO, "Delete identifier is detected as an item name"); - return itemList.deleteItem(keyword); + return true; } } diff --git a/src/main/java/seedu/binbash/command/ListCommand.java b/src/main/java/seedu/binbash/command/ListCommand.java index 654f0b00a0..a2fd8bf8ee 100644 --- a/src/main/java/seedu/binbash/command/ListCommand.java +++ b/src/main/java/seedu/binbash/command/ListCommand.java @@ -9,7 +9,8 @@ public ListCommand(ItemList itemList) { commandLogger.fine("Creating List Command..."); } - public String execute() { - return itemList.printList(itemList.getItemList()); + public boolean execute() { + executionUiOutput = itemList.printList(itemList.getItemList()); + return true; } } diff --git a/src/main/java/seedu/binbash/command/SearchCommand.java b/src/main/java/seedu/binbash/command/SearchCommand.java index 9ba619ac0a..8c73a413e3 100644 --- a/src/main/java/seedu/binbash/command/SearchCommand.java +++ b/src/main/java/seedu/binbash/command/SearchCommand.java @@ -18,7 +18,8 @@ public SearchCommand(ItemList itemList, String keyword) { )); } - public String execute() { - return itemList.searchItem(keyword); + public boolean execute() { + executionUiOutput = itemList.searchItem(keyword); + return true; } } From b1cea2e46dd9679a2174fdb5ab8cd5151bdbfbd5 Mon Sep 17 00:00:00 2001 From: nkotaa Date: Tue, 19 Mar 2024 20:51:05 +0800 Subject: [PATCH 2/3] Refactor main --- src/main/java/seedu/binbash/BinBash.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/binbash/BinBash.java b/src/main/java/seedu/binbash/BinBash.java index e3b3e2ec5c..685a717f94 100644 --- a/src/main/java/seedu/binbash/BinBash.java +++ b/src/main/java/seedu/binbash/BinBash.java @@ -32,8 +32,8 @@ private void run() { continue; } - String executionResult = userCommand.execute(); - userInterface.talk(executionResult); + userCommand.execute(); + userInterface.talk(userCommand.getExecutionUiOutput()); storage.saveToStorage(itemList.getItemList()); } catch (BinBashException e) { From 806be2a16798005051c27a25aab9fd3e3a5fe425 Mon Sep 17 00:00:00 2001 From: nkotaa Date: Tue, 19 Mar 2024 21:40:25 +0800 Subject: [PATCH 3/3] Update test format --- src/test/java/seedu/binbash/command/ListCommandTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/seedu/binbash/command/ListCommandTest.java b/src/test/java/seedu/binbash/command/ListCommandTest.java index b93f172c38..3fac8afe58 100644 --- a/src/test/java/seedu/binbash/command/ListCommandTest.java +++ b/src/test/java/seedu/binbash/command/ListCommandTest.java @@ -21,7 +21,8 @@ void execute_listCommandWithTwoItemsInItemList_correctPrintFormatForBothItems() ListCommand listCommand = new ListCommand(itemList); - String actualOutput = listCommand.execute(); + listCommand.execute(); + String actualOutput = listCommand.getExecutionUiOutput(); String expectedOutput = "1. testItem1" + System.lineSeparator() + "\tdescription: Test item 1" + System.lineSeparator() +