Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor command #79

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/seedu/binbash/BinBash.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/seedu/binbash/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/main/java/seedu/binbash/command/ByeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public ByeCommand(ItemList itemList) {
}

@Override
public String execute() {
return "";
public boolean execute() {
executionUiOutput = "";
return true;
}
}
7 changes: 6 additions & 1 deletion src/main/java/seedu/binbash/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
11 changes: 6 additions & 5 deletions src/main/java/seedu/binbash/command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/main/java/seedu/binbash/command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/main/java/seedu/binbash/command/SearchCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion src/test/java/seedu/binbash/command/ListCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() +
Expand Down
Loading