Skip to content

Commit

Permalink
Implement the find feature
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LIN_CHIEH committed Jan 26, 2023
1 parent 5d2b82f commit 55341ac
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>String[]</code> provided by <code>Parser</code>.
* @param ui Instance of <code>Ui</code> belonging to the calling instance of <code>Duke</code>.
* @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<Integer> 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();
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke;

import java.util.List;

public class Ui {
void displayMessage(String message) {
System.out.println(
Expand Down Expand Up @@ -28,4 +30,22 @@ void displayItemList(TaskList tasks) {
}
displayMessage(sb.toString());
}

/**
* Method to display the <code>Task</code> items at the indices specified.
* @param tasks Instance of <code>TaskList</code> belonging to the calling instance of <code>Duke</code>.
* @param indices <code>List<Integer></code> containing desired indices to be displayed
*/
void displayItemsAtIndices(TaskList tasks, List<Integer> 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());
}
}

0 comments on commit 55341ac

Please sign in to comment.