From 55341acb8547e071773bae0343d92a26a86295d9 Mon Sep 17 00:00:00 2001 From: LIN_CHIEH Date: Thu, 26 Jan 2023 23:40:27 +0800 Subject: [PATCH] Implement the find feature Users can now input the find command followed by any number of keywords to search for Tasks that have names containing the search target keywords --- src/main/java/duke/Duke.java | 6 ++++++ src/main/java/duke/TaskList.java | 26 ++++++++++++++++++++++++++ src/main/java/duke/Ui.java | 20 ++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 46b8397f35..11ce645409 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -57,6 +57,12 @@ void run() { } catch (DukeException e) { ui.displayMessage(e.getMessage()); } + } else if (tokens[0].equals("find")) { + try { + taskList.findItemInList(tokens, ui); + } catch (DukeException e) { + ui.displayMessage(e.getMessage()); + } } else { ui.displayMessage("unknown command\n"); } diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index d6cc75cd91..d4e64d488d 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -166,6 +166,32 @@ void deleteItem(String[] tokens, Ui ui) throws DukeException { } } + /** + * Method to find all items containing keyword in the list, and invokes the associated Ui event to + * display matching items + * @param tokens String[] provided by Parser. + * @param ui Instance of Ui belonging to the calling instance of Duke. + * @throws DukeException In the event that no keyword is specified. + */ + void findItemInList(String[] tokens, Ui ui) throws DukeException { + if (tokens.length == 1) { + throw new DukeException("please provide a keyword or keywords to search for"); + } + StringBuilder sb = new StringBuilder(); + for (int i = 1; i < tokens.length; i++) { + sb.append(tokens[i]).append(' '); + } + sb.deleteCharAt(sb.length()-1); + List indices = new ArrayList<>(); + for (int i = 0; i < tasks.size(); i++) { + Task task = tasks.get(i); + if (task.getName().matches(".*" + sb + ".*")) { + indices.add(i); + } + } + ui.displayItemsAtIndices(this, indices); + } + public int size() { return tasks.size(); } diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index 9c92c083f3..0e44062680 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -1,5 +1,7 @@ package duke; +import java.util.List; + public class Ui { void displayMessage(String message) { System.out.println( @@ -28,4 +30,22 @@ void displayItemList(TaskList tasks) { } displayMessage(sb.toString()); } + + /** + * Method to display the Task items at the indices specified. + * @param tasks Instance of TaskList belonging to the calling instance of Duke. + * @param indices List containing desired indices to be displayed + */ + void displayItemsAtIndices(TaskList tasks, List indices) { + StringBuilder sb = new StringBuilder(); + sb.append("Here are the wanted tasks in your list:\n"); + for (int i : indices) { + sb + .append(i+1) + .append(".") + .append(tasks.get(i).toString()) + .append("\n"); + } + displayMessage(sb.toString()); + } }