diff --git a/src/main/java/speed/command/FindCommand.java b/src/main/java/speed/command/FindCommand.java new file mode 100644 index 000000000..c85ee5ed9 --- /dev/null +++ b/src/main/java/speed/command/FindCommand.java @@ -0,0 +1,22 @@ +package speed.command; + +import speed.exception.SpeedException; +import speed.storage.Storage; +import speed.task.Task; +import speed.task.TaskList; +import speed.ui.Ui; + +import java.util.ArrayList; + +public class FindCommand extends Command { + private final String keyword; + public FindCommand(String keyword) { + this.keyword = keyword; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws SpeedException { + ArrayList matchedTasks = tasks.find(keyword); + ui.showFindList(matchedTasks); + } +} diff --git a/src/main/java/speed/parser/Parser.java b/src/main/java/speed/parser/Parser.java index cb2eb3a2e..822cb4360 100644 --- a/src/main/java/speed/parser/Parser.java +++ b/src/main/java/speed/parser/Parser.java @@ -5,6 +5,7 @@ import speed.command.DeleteCommand; import speed.command.EventCommand; import speed.command.ExitCommand; +import speed.command.FindCommand; import speed.command.HelpCommand; import speed.command.ListCommand; import speed.command.MarkCommand; @@ -63,6 +64,9 @@ public static Command parseCommand(String input, int totalTaskCount) throws Spee Event task = parseEvent(input); return new EventCommand(task); + } else if (input.equals("find") || input.startsWith("find ") ) { + String keyword = parseFind(input); + return new FindCommand(keyword); } else { throw new SpeedException(Ui.ERROR_UNKNOWN_COMMAND); } @@ -164,4 +168,14 @@ private static Todo parseTodo(String input) throws SpeedException { } return new Todo(description); } + + private static String parseFind(String input) throws SpeedException { + String[] parts = input.split(" ", 2); + String keyword = parts.length < 2 ? "" : parts[1].trim(); + + if (keyword.isEmpty()) { + throw new SpeedException(Ui.ERROR_MISSING_KEYWORD); + } + return keyword; + } } diff --git a/src/main/java/speed/task/TaskList.java b/src/main/java/speed/task/TaskList.java index b2e783508..b67d27463 100644 --- a/src/main/java/speed/task/TaskList.java +++ b/src/main/java/speed/task/TaskList.java @@ -2,6 +2,8 @@ import java.util.ArrayList; +import static java.util.stream.Collectors.toList; + /** * Represents a list of tasks with operations to manage them. */ @@ -98,4 +100,15 @@ public boolean isEmpty() { public ArrayList getTasks() { return tasks; } + + public ArrayList find (String keyword) { + String key = keyword.toLowerCase(); + + ArrayList matchedTasks = (ArrayList) tasks.stream() + .filter(t -> t.getDescription() + .toLowerCase() + .contains(key)) + .collect(toList()); + return matchedTasks; + } } diff --git a/src/main/java/speed/ui/Ui.java b/src/main/java/speed/ui/Ui.java index 396e436d3..e073f96f6 100644 --- a/src/main/java/speed/ui/Ui.java +++ b/src/main/java/speed/ui/Ui.java @@ -3,6 +3,7 @@ import speed.task.Task; import speed.task.TaskList; +import java.util.ArrayList; import java.util.Scanner; /** @@ -16,6 +17,9 @@ public class Ui { public static final String ERROR_EMPTY_TODO = "Hold up! You forgot to give a description of the todo!!"; + public static final String ERROR_MISSING_KEYWORD = + "Give me a keyword of what you want me to find bro!"; + public static final String ERROR_NO_TASK_NUMBER = "Please give me the task number as well bro."; @@ -117,7 +121,8 @@ public void printCommandList() { System.out.println("5.mark "); System.out.println("6.unmark "); System.out.println("7.delete "); - System.out.println("8.bye"); + System.out.println("8.find "); + System.out.println("9.bye"); printLine(); } @@ -202,4 +207,21 @@ public void showTaskDeleted(Task task, int remainingTasksCount) { System.out.println("Now you have " + remainingTasksCount + " tasks in the list."); printLine(); } + + public void showFindList(ArrayList matchedTasks) { + if (matchedTasks.isEmpty()) { + printLine(); + System.out.println("No matches found!"); + printLine(); + } else { + printLine(); + System.out.println("Here are the matching tasks in your list:"); + int taskCount = 1; + for (Task task : matchedTasks) { + System.out.println((taskCount++) + "." + task.displayString()); + } + printLine(); + } + + } }