From 077f642aea683bf2182595dcb0839e0c69a84968 Mon Sep 17 00:00:00 2001 From: Cohii Date: Fri, 8 Mar 2024 02:28:55 +0800 Subject: [PATCH 1/3] Create find function Give users a way to find a task by searching for a keyword. --- src/main/java/duke/Parser.java | 3 ++- src/main/java/duke/TaskList.java | 35 +++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index 0fe08fe74..618fad549 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -30,7 +30,8 @@ public void executeCommand(String command, String argument) switch (command) { case "list": - taskList.printList(); + String message = ""; + taskList.printList(message); break; case "mark": taskList.markTask(argument, true); diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 79c642e00..0571d243c 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -1,5 +1,6 @@ package duke; +import java.util.ArrayList; import java.util.List; import static duke.print.*; @@ -17,9 +18,17 @@ public List getTaskList() { /** * Prints list of tasks. + * If message is not empty, prints message in front of list. + * + * @param message Message to be printed before list. */ - public void printList(){ + public void printList(String message){ printLine(); + + if (!message.isEmpty()) { + System.out.println(message); + } + for(int i = 0; i < taskList.size(); i++){ Task task = taskList.get(i); System.out.println((i + 1) + "." + task); @@ -166,4 +175,28 @@ public void deleteTask(String instruction) throw new DukeException.IntegerOutOfBoundsException(); } } + + /** + * Finds tasks which descriptions match the target, + * before printing out list of matches. + * + * @param target Target phrase to search for. + */ + public void find(String target){ + List matches = new ArrayList<>(); + + for (Task task : taskList){ + if(task.getDescription().contains(target)){ + matches.add(task); + } + } + + if (matches.isEmpty()){ + printMessage("No matches!!"); + } + else { + new TaskList(matches).printList( + " Here are the matching tasks in your list-gari:"); + } + } } From 8002b32b7a43b7101f56abdf1f6e3c8ffed408e3 Mon Sep 17 00:00:00 2001 From: Cohii Date: Fri, 8 Mar 2024 02:50:05 +0800 Subject: [PATCH 2/3] Added find function to Parser Class --- .gitignore | 1 + src/main/java/duke/MissingParamsException.java | 3 +++ src/main/java/duke/Parser.java | 5 ++++- src/main/java/duke/TaskList.java | 14 ++++++++++---- src/main/java/duke/TaskParams.java | 2 +- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 2873e189e..84acdadd4 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT +*.txt diff --git a/src/main/java/duke/MissingParamsException.java b/src/main/java/duke/MissingParamsException.java index db1372ae5..703851e47 100644 --- a/src/main/java/duke/MissingParamsException.java +++ b/src/main/java/duke/MissingParamsException.java @@ -27,6 +27,9 @@ public String toString() { case END: errorMessage.append("to "); break; + case TARGET: + errorMessage.append("target "); + break; } } diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index 618fad549..70d7e41c5 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -5,7 +5,7 @@ public class Parser { public static final String[] validCommands = - {"list", "mark", "unmark", "todo", "deadline", "event", "bye", "delete"}; + {"list", "mark", "unmark", "todo", "deadline", "event", "bye", "delete", "find"}; protected TaskList taskList; public Parser(TaskList taskList) { @@ -51,6 +51,9 @@ public void executeCommand(String command, String argument) case "delete": taskList.deleteTask(argument); break; + case "find": + taskList.findTask(argument); + break; } } } diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 0571d243c..17e0da3cb 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -182,7 +182,14 @@ public void deleteTask(String instruction) * * @param target Target phrase to search for. */ - public void find(String target){ + public void findTask(String target) throws MissingParamsException { + if (target.isEmpty()) { + List missingParams = new ArrayList<>(); + missingParams.add(TaskParams.TARGET); + + throw new MissingParamsException(missingParams); + } + List matches = new ArrayList<>(); for (Task task : taskList){ @@ -193,10 +200,9 @@ public void find(String target){ if (matches.isEmpty()){ printMessage("No matches!!"); - } - else { + } else { new TaskList(matches).printList( - " Here are the matching tasks in your list-gari:"); + "Here are the matching tasks in your list-gari:"); } } } diff --git a/src/main/java/duke/TaskParams.java b/src/main/java/duke/TaskParams.java index 4bb0751ce..488e439f1 100644 --- a/src/main/java/duke/TaskParams.java +++ b/src/main/java/duke/TaskParams.java @@ -1,5 +1,5 @@ package duke; public enum TaskParams { - DESCRIPTION, BY, START, END + DESCRIPTION, BY, START, END, TARGET } From 5c6b134fec6fb7d0e06454d5939d02c5972f52fb Mon Sep 17 00:00:00 2001 From: Cohii Date: Fri, 8 Mar 2024 02:56:38 +0800 Subject: [PATCH 3/3] Update IO testing for find function --- text-ui-test/EXPECTED.TXT | 131 ++++++++++++++++++++++++-------------- text-ui-test/input.txt | 14 ++-- 2 files changed, 93 insertions(+), 52 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 91d2dd532..e034cba65 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -21,6 +21,11 @@ Got it. I've added this task: Now you have 2 tasks in the list. ______________________________ ______________________________ +Got it. I've added this task: + [T][ ] Watch CEG Video +Now you have 3 tasks in the list. +______________________________ +______________________________ The following parameters are missing: description YOU GOTTA SAVE ME AND MY HEART!! @@ -28,16 +33,17 @@ ______________________________ ______________________________ 1.[T][ ] Eat Breakfast 2.[T][ ] Eat Lunch +3.[T][ ] Watch CEG Video ______________________________ ______________________________ Got it. I've added this task: [E][ ] Tutorial (from: 1pm to: 3pm) -Now you have 3 tasks in the list. +Now you have 4 tasks in the list. ______________________________ ______________________________ Got it. I've added this task: - [E][ ] Lecture (from: 3pm to: 5pm) -Now you have 4 tasks in the list. + [E][ ] CEG 1 Lecture (from: 3pm to: 5pm) +Now you have 5 tasks in the list. ______________________________ ______________________________ The following parameters are missing: @@ -67,24 +73,25 @@ ______________________________ ______________________________ Got it. I've added this task: [E][ ] Seminar (from: 5pm to: 7pm) -Now you have 5 tasks in the list. +Now you have 6 tasks in the list. ______________________________ ______________________________ 1.[T][ ] Eat Breakfast 2.[T][ ] Eat Lunch -3.[E][ ] Tutorial (from: 1pm to: 3pm) -4.[E][ ] Lecture (from: 3pm to: 5pm) -5.[E][ ] Seminar (from: 5pm to: 7pm) +3.[T][ ] Watch CEG Video +4.[E][ ] Tutorial (from: 1pm to: 3pm) +5.[E][ ] CEG 1 Lecture (from: 3pm to: 5pm) +6.[E][ ] Seminar (from: 5pm to: 7pm) ______________________________ ______________________________ Got it. I've added this task: [D][ ] RealizeIt (by: Thursday) -Now you have 6 tasks in the list. +Now you have 7 tasks in the list. ______________________________ ______________________________ Got it. I've added this task: - [D][ ] Coursemology (by: Friday) -Now you have 7 tasks in the list. + [D][ ] CEG 1 Coursemology (by: Friday) +Now you have 8 tasks in the list. ______________________________ ______________________________ The following parameters are missing: @@ -104,17 +111,18 @@ ______________________________ ______________________________ Got it. I've added this task: [D][ ] Teammates (by: Saturday) -Now you have 8 tasks in the list. +Now you have 9 tasks in the list. ______________________________ ______________________________ 1.[T][ ] Eat Breakfast 2.[T][ ] Eat Lunch -3.[E][ ] Tutorial (from: 1pm to: 3pm) -4.[E][ ] Lecture (from: 3pm to: 5pm) -5.[E][ ] Seminar (from: 5pm to: 7pm) -6.[D][ ] RealizeIt (by: Thursday) -7.[D][ ] Coursemology (by: Friday) -8.[D][ ] Teammates (by: Saturday) +3.[T][ ] Watch CEG Video +4.[E][ ] Tutorial (from: 1pm to: 3pm) +5.[E][ ] CEG 1 Lecture (from: 3pm to: 5pm) +6.[E][ ] Seminar (from: 5pm to: 7pm) +7.[D][ ] RealizeIt (by: Thursday) +8.[D][ ] CEG 1 Coursemology (by: Friday) +9.[D][ ] Teammates (by: Saturday) ______________________________ ______________________________ Nice! I've marked this task as done @@ -122,51 +130,53 @@ Nice! I've marked this task as done ______________________________ ______________________________ Nice! I've marked this task as done - [E][X] Tutorial (from: 1pm to: 3pm) + [T][X] Watch CEG Video ______________________________ ______________________________ Nice! I've marked this task as done - [E][X] Seminar (from: 5pm to: 7pm) + [E][X] CEG 1 Lecture (from: 3pm to: 5pm) ______________________________ ______________________________ Nice! I've marked this task as done - [D][X] Coursemology (by: Friday) + [D][X] RealizeIt (by: Thursday) ______________________________ ______________________________ 1.[T][X] Eat Breakfast 2.[T][ ] Eat Lunch -3.[E][X] Tutorial (from: 1pm to: 3pm) -4.[E][ ] Lecture (from: 3pm to: 5pm) -5.[E][X] Seminar (from: 5pm to: 7pm) -6.[D][ ] RealizeIt (by: Thursday) -7.[D][X] Coursemology (by: Friday) -8.[D][ ] Teammates (by: Saturday) +3.[T][X] Watch CEG Video +4.[E][ ] Tutorial (from: 1pm to: 3pm) +5.[E][X] CEG 1 Lecture (from: 3pm to: 5pm) +6.[E][ ] Seminar (from: 5pm to: 7pm) +7.[D][X] RealizeIt (by: Thursday) +8.[D][ ] CEG 1 Coursemology (by: Friday) +9.[D][ ] Teammates (by: Saturday) ______________________________ ______________________________ Nice! I've marked this task as undone - [E][ ] Tutorial (from: 1pm to: 3pm) + [T][ ] Watch CEG Video ______________________________ ______________________________ Nice! I've marked this task as undone - [E][ ] Lecture (from: 3pm to: 5pm) + [E][ ] Tutorial (from: 1pm to: 3pm) ______________________________ ______________________________ Nice! I've marked this task as undone - [E][ ] Seminar (from: 5pm to: 7pm) + [E][ ] CEG 1 Lecture (from: 3pm to: 5pm) ______________________________ ______________________________ Nice! I've marked this task as undone - [D][ ] RealizeIt (by: Thursday) + [E][ ] Seminar (from: 5pm to: 7pm) ______________________________ ______________________________ 1.[T][X] Eat Breakfast 2.[T][ ] Eat Lunch -3.[E][ ] Tutorial (from: 1pm to: 3pm) -4.[E][ ] Lecture (from: 3pm to: 5pm) -5.[E][ ] Seminar (from: 5pm to: 7pm) -6.[D][ ] RealizeIt (by: Thursday) -7.[D][X] Coursemology (by: Friday) -8.[D][ ] Teammates (by: Saturday) +3.[T][ ] Watch CEG Video +4.[E][ ] Tutorial (from: 1pm to: 3pm) +5.[E][ ] CEG 1 Lecture (from: 3pm to: 5pm) +6.[E][ ] Seminar (from: 5pm to: 7pm) +7.[D][X] RealizeIt (by: Thursday) +8.[D][ ] CEG 1 Coursemology (by: Friday) +9.[D][ ] Teammates (by: Saturday) ______________________________ ______________________________ Please input an integer!! @@ -185,14 +195,38 @@ Task does not exist!! Invalid instruction ZENBU FAKE!! ______________________________ ______________________________ +The following parameters are missing: +target +YOU GOTTA SAVE ME AND MY HEART!! +______________________________ +______________________________ +No matches!! +______________________________ +______________________________ +Here are the matching tasks in your list-gari: +1.[T][ ] Watch CEG Video +2.[E][ ] CEG 1 Lecture (from: 3pm to: 5pm) +3.[D][ ] CEG 1 Coursemology (by: Friday) +______________________________ +______________________________ +Here are the matching tasks in your list-gari: +1.[E][ ] CEG 1 Lecture (from: 3pm to: 5pm) +2.[D][ ] CEG 1 Coursemology (by: Friday) +______________________________ +______________________________ +Here are the matching tasks in your list-gari: +1.[T][ ] Watch CEG Video +______________________________ +______________________________ 1.[T][X] Eat Breakfast 2.[T][ ] Eat Lunch -3.[E][ ] Tutorial (from: 1pm to: 3pm) -4.[E][ ] Lecture (from: 3pm to: 5pm) -5.[E][ ] Seminar (from: 5pm to: 7pm) -6.[D][ ] RealizeIt (by: Thursday) -7.[D][X] Coursemology (by: Friday) -8.[D][ ] Teammates (by: Saturday) +3.[T][ ] Watch CEG Video +4.[E][ ] Tutorial (from: 1pm to: 3pm) +5.[E][ ] CEG 1 Lecture (from: 3pm to: 5pm) +6.[E][ ] Seminar (from: 5pm to: 7pm) +7.[D][X] RealizeIt (by: Thursday) +8.[D][ ] CEG 1 Coursemology (by: Friday) +9.[D][ ] Teammates (by: Saturday) ______________________________ ______________________________ Noted. I've removed this task: @@ -200,11 +234,11 @@ ______________________________ ______________________________ ______________________________ Noted. I've removed this task: - [E][ ] Tutorial (from: 1pm to: 3pm) + [T][ ] Watch CEG Video ______________________________ ______________________________ Noted. I've removed this task: - [D][ ] RealizeIt (by: Thursday) + [E][ ] Seminar (from: 5pm to: 7pm) ______________________________ ______________________________ Task does not exist!! @@ -220,10 +254,11 @@ Invalid instruction ZENBU FAKE!! ______________________________ ______________________________ 1.[T][ ] Eat Lunch -2.[E][ ] Lecture (from: 3pm to: 5pm) -3.[E][ ] Seminar (from: 5pm to: 7pm) -4.[D][X] Coursemology (by: Friday) -5.[D][ ] Teammates (by: Saturday) +2.[E][ ] Tutorial (from: 1pm to: 3pm) +3.[E][ ] CEG 1 Lecture (from: 3pm to: 5pm) +4.[D][X] RealizeIt (by: Thursday) +5.[D][ ] CEG 1 Coursemology (by: Friday) +6.[D][ ] Teammates (by: Saturday) ______________________________ ______________________________ Bye! Hope to see you again soon! MEGANE!! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 3af8c0107..ca778ffa3 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -1,10 +1,11 @@ -todo Eat Breakfast +todo Eat Breakfast todo Eat Lunch +todo Watch CEG Video todo list event Tutorial /from 1pm /to 3pm -event Lecture /from 3pm /to 5pm -event Seminar +event CEG 1 Lecture /from 3pm /to 5pm +event Seminar event Seminar /from /to event Seminar /from 5pm event Seminar /to 7pm @@ -12,7 +13,7 @@ event /from 5pm /to 7pm event Seminar /from 5pm /to 7pm list deadline RealizeIt /by Thursday -deadline Coursemology /by Friday +deadline CEG 1 Coursemology /by Friday deadline deadline Teammates /by deadline /by Saturday @@ -32,6 +33,11 @@ mark string mark 1000 unmark string unmark 2000 +find +find target that does not exist +find CEG +find CEG 1 +find Watch CEG Video list delete 1 delete 2