From 4b13e649ab87d1ba25be87c2662c069a3670a29d Mon Sep 17 00:00:00 2001 From: nur-haziq Date: Sat, 16 Mar 2024 14:35:11 +0800 Subject: [PATCH 1/3] Add SearchCommand.java Add skeleton code for SearchCommand class --- .../seedu/binbash/command/SearchCommand.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/seedu/binbash/command/SearchCommand.java diff --git a/src/main/java/seedu/binbash/command/SearchCommand.java b/src/main/java/seedu/binbash/command/SearchCommand.java new file mode 100644 index 0000000000..7caf72fac5 --- /dev/null +++ b/src/main/java/seedu/binbash/command/SearchCommand.java @@ -0,0 +1,23 @@ +package seedu.binbash.command; + +import seedu.binbash.ItemList; + +import java.util.regex.Pattern; + +public class SearchCommand extends Command { + + public static final String COMMAND_STRING = "search"; + public static final Pattern COMMAND_FORMAT = + Pattern.compile("search\\s+(?.+?)\\s*$"); + protected String keyword; + + public SearchCommand(ItemList itemList, String keyword) { + super(itemList); + + this.keyword = keyword; + } + + public String execute() { + return itemList.searchItem(keyword); + } +} From 3569004599026a89cdb84af6941c8f5bac2caef1 Mon Sep 17 00:00:00 2001 From: nur-haziq Date: Sat, 16 Mar 2024 14:35:49 +0800 Subject: [PATCH 2/3] Add search functionality --- src/main/java/seedu/binbash/ItemList.java | 25 +++++++++++++++--- src/main/java/seedu/binbash/Parser.java | 26 ++++++++++++------- .../seedu/binbash/command/ListCommand.java | 2 +- src/test/java/seedu/binbash/ItemListTest.java | 2 +- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/main/java/seedu/binbash/ItemList.java b/src/main/java/seedu/binbash/ItemList.java index ecb12b094b..c023380ab4 100644 --- a/src/main/java/seedu/binbash/ItemList.java +++ b/src/main/java/seedu/binbash/ItemList.java @@ -1,7 +1,9 @@ package seedu.binbash; +import java.lang.reflect.Array; import java.util.List; import java.util.ArrayList; +import java.util.stream.Collectors; public class ItemList { private final List itemList; @@ -40,17 +42,32 @@ public String deleteItem(int index) { return output; } + public String searchItem(String keyword) { + ArrayList filteredList = (ArrayList) itemList.stream() + .filter(item -> item.getItemName().contains(keyword)) + .collect(Collectors.toList()); + + String output = ""; + + if (filteredList.isEmpty()) { + output += String.format("There are no tasks with the keyword '%s'!", keyword); + } else { + output = String.format("Here's a list of items that contain the keyword '%s': ", keyword) + + System.lineSeparator() + + printList(filteredList); + } + + return output; + } + /** - * DO LET ME KNOW IF THE METHOD NAME IS WEIRD. IM RETURNING A STRING REPRESENTATION INSTEAD - * OF CALLING SOUT TO STAY CONSISTENT WITH THE OTHER COMMANDS BEHAVIOUR. SO IT DOESN'T ACTUALLY - * PRINT THE LIST. IF THERES A BETTER NAME LMK THANKS * * Returns a string representation of all the items in the list. Each item's string * representation is obtained by calling its `toString` method. * * @return A concatenated string of all item representations in the list, each on a new line. */ - public String printList() { + public String printList(List itemList) { String output = ""; for (Item item: itemList) { diff --git a/src/main/java/seedu/binbash/Parser.java b/src/main/java/seedu/binbash/Parser.java index 4d0153852f..1cc4a8bd07 100644 --- a/src/main/java/seedu/binbash/Parser.java +++ b/src/main/java/seedu/binbash/Parser.java @@ -2,11 +2,7 @@ import java.util.regex.Matcher; -import seedu.binbash.command.AddCommand; -import seedu.binbash.command.ByeCommand; -import seedu.binbash.command.Command; -import seedu.binbash.command.DeleteCommand; -import seedu.binbash.command.ListCommand; +import seedu.binbash.command.*; public class Parser { private final ItemList itemList; @@ -27,13 +23,15 @@ public Command parseCommand(String userInput) { return parseDeleteCommand(userInput); case "list": return parseListCommand(userInput); + case "search": + return parseSearchCommand(userInput); default: return new ByeCommand(itemList); } } - private Command parseDeleteCommand(String arguments) { - Matcher matcher = DeleteCommand.COMMAND_FORMAT.matcher(arguments); + private Command parseDeleteCommand(String userInput) { + Matcher matcher = DeleteCommand.COMMAND_FORMAT.matcher(userInput); if (matcher.matches()) { int index = Integer.parseInt(matcher.group("index")); return new DeleteCommand(itemList, index); @@ -42,8 +40,8 @@ private Command parseDeleteCommand(String arguments) { } } - private Command parseAddCommand(String arguments) { - Matcher matcher = AddCommand.COMMAND_FORMAT.matcher(arguments); + private Command parseAddCommand(String userInput) { + Matcher matcher = AddCommand.COMMAND_FORMAT.matcher(userInput); if (matcher.matches()) { String itemName = matcher.group("itemName"); String itemDescription = matcher.group("itemDescription"); @@ -53,6 +51,16 @@ private Command parseAddCommand(String arguments) { } } + private Command parseSearchCommand(String userInput) { + Matcher matcher = SearchCommand.COMMAND_FORMAT.matcher(userInput); + if (matcher.matches()) { + String keyword = matcher.group("keyword"); + return new SearchCommand(itemList, keyword); + } else { + return null; + } + } + private Command parseListCommand(String arguments) { return new ListCommand(itemList); } diff --git a/src/main/java/seedu/binbash/command/ListCommand.java b/src/main/java/seedu/binbash/command/ListCommand.java index 1eac5c4804..1995e95f99 100644 --- a/src/main/java/seedu/binbash/command/ListCommand.java +++ b/src/main/java/seedu/binbash/command/ListCommand.java @@ -15,6 +15,6 @@ public ListCommand(ItemList itemList) { } public String execute() { - return itemList.printList(); + return itemList.printList(itemList.getItemList()); } } diff --git a/src/test/java/seedu/binbash/ItemListTest.java b/src/test/java/seedu/binbash/ItemListTest.java index 6d595b8d89..33698c2a23 100644 --- a/src/test/java/seedu/binbash/ItemListTest.java +++ b/src/test/java/seedu/binbash/ItemListTest.java @@ -42,7 +42,7 @@ void printList_twoItemsInItemList_correctPrintFormatForBothItems() { itemList.addItem("testItem", "1"); itemList.addItem("testItem", "2"); - String actualOutput = itemList.printList(); + String actualOutput = itemList.printList(itemList.getItemList()); String expectedOutput = "testItem: 1" + System.lineSeparator() + "testItem: 2" + System.lineSeparator(); From 1e4a7e7c1b788017c2483b1ff2786f463e8f964d Mon Sep 17 00:00:00 2001 From: nur-haziq Date: Sat, 16 Mar 2024 14:42:34 +0800 Subject: [PATCH 3/3] Remove unused import statements --- src/main/java/seedu/binbash/ItemList.java | 2 -- src/main/java/seedu/binbash/Parser.java | 7 ++++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/binbash/ItemList.java b/src/main/java/seedu/binbash/ItemList.java index c023380ab4..dd5bbff254 100644 --- a/src/main/java/seedu/binbash/ItemList.java +++ b/src/main/java/seedu/binbash/ItemList.java @@ -1,6 +1,5 @@ package seedu.binbash; -import java.lang.reflect.Array; import java.util.List; import java.util.ArrayList; import java.util.stream.Collectors; @@ -61,7 +60,6 @@ public String searchItem(String keyword) { } /** - * * Returns a string representation of all the items in the list. Each item's string * representation is obtained by calling its `toString` method. * diff --git a/src/main/java/seedu/binbash/Parser.java b/src/main/java/seedu/binbash/Parser.java index 1cc4a8bd07..d406419cb5 100644 --- a/src/main/java/seedu/binbash/Parser.java +++ b/src/main/java/seedu/binbash/Parser.java @@ -2,7 +2,12 @@ import java.util.regex.Matcher; -import seedu.binbash.command.*; +import seedu.binbash.command.Command; +import seedu.binbash.command.AddCommand; +import seedu.binbash.command.DeleteCommand; +import seedu.binbash.command.SearchCommand; +import seedu.binbash.command.ListCommand; +import seedu.binbash.command.ByeCommand; public class Parser { private final ItemList itemList;