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

Implement search functionality #45

Merged
merged 3 commits into from
Mar 16, 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
25 changes: 20 additions & 5 deletions src/main/java/seedu/binbash/ItemList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

public class ItemList {
private final List<Item> itemList;
Expand Down Expand Up @@ -40,17 +41,31 @@ public String deleteItem(int index) {
return output;
}

public String searchItem(String keyword) {
ArrayList<Item> filteredList = (ArrayList<Item>) 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<Item> itemList) {
String output = "";

for (Item item: itemList) {
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/seedu/binbash/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/binbash/command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public ListCommand(ItemList itemList) {
}

public String execute() {
return itemList.printList();
return itemList.printList(itemList.getItemList());
}
}
23 changes: 23 additions & 0 deletions src/main/java/seedu/binbash/command/SearchCommand.java
Original file line number Diff line number Diff line change
@@ -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+(?<keyword>.+?)\\s*$");
protected String keyword;

public SearchCommand(ItemList itemList, String keyword) {
super(itemList);

this.keyword = keyword;
}

public String execute() {
return itemList.searchItem(keyword);
}
}
2 changes: 1 addition & 1 deletion src/test/java/seedu/binbash/ItemListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading