From 58750f929de79baf60fa83d0e997ab1ef52c11e8 Mon Sep 17 00:00:00 2001 From: PureUsagi Date: Mon, 11 Mar 2024 17:14:58 +0800 Subject: [PATCH 1/6] Add Command abstract class. --- src/main/java/seedu/binbash/command/Command.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/seedu/binbash/command/Command.java diff --git a/src/main/java/seedu/binbash/command/Command.java b/src/main/java/seedu/binbash/command/Command.java new file mode 100644 index 0000000000..b9f14c0e8f --- /dev/null +++ b/src/main/java/seedu/binbash/command/Command.java @@ -0,0 +1,16 @@ +package seedu.binbash.command; + +import java.util.regex.Pattern; +import seedu.binbash.ItemList; + +public abstract class Command { + protected ItemList itemList; + public static final String COMMAND_STRING = "command"; + public static final Pattern COMMAND_FORMAT = + Pattern.compile("(?\\S+)(?.*)"); + protected Command(ItemList itemList) { + this.itemList = itemList; + } + + abstract public String execute(); +} From 0939422c7153df5e1d59158877abed3903f4568e Mon Sep 17 00:00:00 2001 From: PureUsagi Date: Mon, 11 Mar 2024 17:31:43 +0800 Subject: [PATCH 2/6] Add deleteItem method to ItemList class. --- src/main/java/seedu/binbash/ItemList.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/seedu/binbash/ItemList.java b/src/main/java/seedu/binbash/ItemList.java index 30ff109fd5..c0e44e9056 100644 --- a/src/main/java/seedu/binbash/ItemList.java +++ b/src/main/java/seedu/binbash/ItemList.java @@ -21,6 +21,15 @@ public void addItem(Item item) { itemList.add(item); } + public String deleteItem(int index) { + Item tempItem = itemList.get(index - 1); + itemList.remove(index - 1); + + String output = "Got it! I've removed the following item:" + + String.format("\t%s", tempItem); + return output; + } + @Override public String toString() { return itemList.toString(); From 32eababf78e2580de50fdbdfb30771ae91781df9 Mon Sep 17 00:00:00 2001 From: PureUsagi Date: Mon, 11 Mar 2024 17:31:58 +0800 Subject: [PATCH 3/6] Add Delete Command Class. --- .../seedu/binbash/command/DeleteCommand.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/main/java/seedu/binbash/command/DeleteCommand.java diff --git a/src/main/java/seedu/binbash/command/DeleteCommand.java b/src/main/java/seedu/binbash/command/DeleteCommand.java new file mode 100644 index 0000000000..1d38ee8620 --- /dev/null +++ b/src/main/java/seedu/binbash/command/DeleteCommand.java @@ -0,0 +1,21 @@ +package seedu.binbash.command; + +import java.util.regex.Pattern; +import seedu.binbash.ItemList; + +public class DeleteCommand extends Command { + public final static String COMMAND_WORD = "delete"; + public final static Pattern COMMAND_FORMAT = + Pattern.compile("delete\\s(?\\d+)"); + protected int index; + + public DeleteCommand(ItemList itemList, int index) { + super(itemList); + + this.index = index; + } + + public String execute() { + return itemList.deleteItem(index); + } +} From eb7789fbaa720ea9f64926a3387828f7082df78d Mon Sep 17 00:00:00 2001 From: PureUsagi Date: Mon, 11 Mar 2024 17:53:17 +0800 Subject: [PATCH 4/6] Reformat code to be compliant with checkstyle. --- src/main/java/seedu/binbash/command/Command.java | 5 +++-- src/main/java/seedu/binbash/command/DeleteCommand.java | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/binbash/command/Command.java b/src/main/java/seedu/binbash/command/Command.java index b9f14c0e8f..863bd18a14 100644 --- a/src/main/java/seedu/binbash/command/Command.java +++ b/src/main/java/seedu/binbash/command/Command.java @@ -4,13 +4,14 @@ import seedu.binbash.ItemList; public abstract class Command { - protected ItemList itemList; public static final String COMMAND_STRING = "command"; public static final Pattern COMMAND_FORMAT = Pattern.compile("(?\\S+)(?.*)"); + protected ItemList itemList; + protected Command(ItemList itemList) { this.itemList = itemList; } - abstract public String execute(); + public abstract String execute(); } diff --git a/src/main/java/seedu/binbash/command/DeleteCommand.java b/src/main/java/seedu/binbash/command/DeleteCommand.java index 1d38ee8620..1fcfb1f314 100644 --- a/src/main/java/seedu/binbash/command/DeleteCommand.java +++ b/src/main/java/seedu/binbash/command/DeleteCommand.java @@ -4,8 +4,8 @@ import seedu.binbash.ItemList; public class DeleteCommand extends Command { - public final static String COMMAND_WORD = "delete"; - public final static Pattern COMMAND_FORMAT = + public static final String COMMAND_WORD = "delete"; + public static final Pattern COMMAND_FORMAT = Pattern.compile("delete\\s(?\\d+)"); protected int index; From 15ab9d7e2ff62cb34f21b1d159e3bb131a1cd79a Mon Sep 17 00:00:00 2001 From: PureUsagi Date: Tue, 12 Mar 2024 19:04:06 +0800 Subject: [PATCH 5/6] Changed Constant COMMAND_WORD to COMMAND_STRING. --- src/main/java/seedu/binbash/command/DeleteCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/binbash/command/DeleteCommand.java b/src/main/java/seedu/binbash/command/DeleteCommand.java index 1fcfb1f314..351456c11e 100644 --- a/src/main/java/seedu/binbash/command/DeleteCommand.java +++ b/src/main/java/seedu/binbash/command/DeleteCommand.java @@ -4,7 +4,7 @@ import seedu.binbash.ItemList; public class DeleteCommand extends Command { - public static final String COMMAND_WORD = "delete"; + public static final String COMMAND_STRING = "delete"; public static final Pattern COMMAND_FORMAT = Pattern.compile("delete\\s(?\\d+)"); protected int index; From 292f6bedad0d178ff02b53d45c873b5e5776d9fc Mon Sep 17 00:00:00 2001 From: PureUsagi Date: Tue, 12 Mar 2024 19:07:40 +0800 Subject: [PATCH 6/6] Simplify Delete method in ItemList. --- src/main/java/seedu/binbash/ItemList.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/seedu/binbash/ItemList.java b/src/main/java/seedu/binbash/ItemList.java index c0e44e9056..4e1cf42ffc 100644 --- a/src/main/java/seedu/binbash/ItemList.java +++ b/src/main/java/seedu/binbash/ItemList.java @@ -22,8 +22,7 @@ public void addItem(Item item) { } public String deleteItem(int index) { - Item tempItem = itemList.get(index - 1); - itemList.remove(index - 1); + Item tempItem = itemList.remove(index - 1); String output = "Got it! I've removed the following item:" + String.format("\t%s", tempItem);