diff --git a/src/main/java/seedu/binbash/ItemList.java b/src/main/java/seedu/binbash/ItemList.java index ecb12b094b..dd5bbff254 100644 --- a/src/main/java/seedu/binbash/ItemList.java +++ b/src/main/java/seedu/binbash/ItemList.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.ArrayList; +import java.util.stream.Collectors; public class ItemList { private final List itemList; @@ -40,17 +41,31 @@ 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..d406419cb5 100644 --- a/src/main/java/seedu/binbash/Parser.java +++ b/src/main/java/seedu/binbash/Parser.java @@ -2,11 +2,12 @@ import java.util.regex.Matcher; -import seedu.binbash.command.AddCommand; -import seedu.binbash.command.ByeCommand; 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; @@ -27,13 +28,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 +45,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 +56,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/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); + } +} 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();