Skip to content

Commit

Permalink
Add find function
Browse files Browse the repository at this point in the history
  • Loading branch information
jehousoh committed Aug 24, 2022
1 parent 3a38698 commit 815b445
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/duke/helper/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
public class Parser {

public static void parse(String in, TaskList list) {
//"find": Find tasks based on keywords
if (in.startsWith("find")) {
String keywords = in.split(" ", 2)[1];
list.find(keywords);
//"clear": Clears the list
if (in.equals("clear")) {
} else if (in.equals("clear")) {
list.clear();
//"list": Shows current list
} else if (in.equals("list")) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/duke/helper/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public static void countTasks(TaskList list) {
public static void clear() {
System.out.println("The list has been successfully cleared!");
}

public static void taskFound() {
System.out.println("Woohoo here are some matches found!");
}

public static void noTaskFound() {
System.out.println("Ohno I could not find any tasks fitting the keywords...");
}

public static void line() {
System.out.println("-------------------------------------------");
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/duke/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,38 @@ public void unmark(int index) {
taskList.get(index).markAsUndone();
Ui.unmark();
}

/**
* Method to find and print tasks based on keywords
*
* @param keywords the string of keywords to be searched
*/
public void find(String keywords) {
String[] keywordArr = keywords.split(" ");
int arrLen = keywordArr.length;
int pointer;
TaskList result = new TaskList();

for (Task task : taskList) {
pointer = 0;

while (pointer < arrLen) {
if (!task.getDescription().contains(keywordArr[pointer])) {
break;
}
pointer++;
}

if (pointer == arrLen) {
result.add(task);
}
}

if (result.getSize() == 0) {
Ui.noTaskFound();
} else {
Ui.taskFound();
result.printTasks();
}
}
}

0 comments on commit 815b445

Please sign in to comment.