diff --git a/src/main/java/duke/AddCommand.java b/src/main/java/duke/AddCommand.java index 3a8683fae0..65934e4107 100644 --- a/src/main/java/duke/AddCommand.java +++ b/src/main/java/duke/AddCommand.java @@ -52,6 +52,6 @@ public void run(Duke duke) { default: task = new Task(""); // error } - duke.add(task); + duke.addTask(task); } } diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 62872d32d4..82c9211b8c 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -28,7 +28,7 @@ public void start() { * * @param task Task to add. */ - public void add(Task task) { // error if description is empty + public void addTask(Task task) { // error if description is empty tasks.add(task); ui.addTask(task); ui.infoCount(tasks.size()); @@ -75,6 +75,17 @@ public void deleteTask(int index) { ui.infoCount(tasks.size()); } + /** + * Print all tasks that matches the keyword to the UI. + * + * @param keyword Keyword to match. + */ + public void findTasks(String keyword) { + TaskList matches = (TaskList) tasks.clone(); + matches.removeIf(task -> !task.toString().toLowerCase().contains(keyword.toLowerCase())); + ui.findTasks(matches); + } + /** * Exit the Duke Chatterbot. */ diff --git a/src/main/java/duke/FindCommand.java b/src/main/java/duke/FindCommand.java new file mode 100644 index 0000000000..fc0c188415 --- /dev/null +++ b/src/main/java/duke/FindCommand.java @@ -0,0 +1,27 @@ +package duke; + +/** + * Represents a Command to find tasks in Duke that matches a certain keyword. + */ +public class FindCommand extends Command { + protected String keyword; + + /** + * Constructor of FindCommand with keyword to match. + * + * @param keyword Keyword to match. + */ + public FindCommand(String keyword) { + this.keyword = keyword; + } + + /** + * Run the FindCommand, print all tasks in Duke that matches the keyword to the UI. + * + * @param duke Duke instance to run the AddCommand at. + */ + @Override + public void run(Duke duke) { + duke.findTasks(keyword); + } +} diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index 29dd1209b8..dc29146e6b 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -45,6 +45,8 @@ public static Command parseInput(String input) { return new MarkCommand(Integer.parseInt(inputParts[1]) - 1, true); case "unmark": return new MarkCommand(Integer.parseInt(inputParts[1]) - 1, false); + case "find": + return new FindCommand(inputParts[1]); case "todo": return new AddCommand('T', inputParts[1]); case "deadline": @@ -57,7 +59,7 @@ public static Command parseInput(String input) { return new DeleteCommand(Integer.parseInt(inputParts[1]) - 1); default: return new ExitCommand(); - // throw new duke.DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + // throw new duke.DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } } diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index 8e795a6968..f447ce068a 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -95,6 +95,18 @@ public void deleteTask(Task task) { print(task.toString()); } + /** + * Print all matching tasks to the UI. + * + * @param tasks List of matching tasks. + */ + public void findTasks(TaskList tasks) { + print("Here are the matching tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + print((i + 1) + ". " + tasks.get(i)); + } + } + /** * Prints a goodbye message and closes the UI. */