From cefd18d551505e4b921773f99522fa42f181bd37 Mon Sep 17 00:00:00 2001 From: Cazh2 Date: Thu, 31 Aug 2023 15:23:41 +0800 Subject: [PATCH 01/46] Add Greet and Exit functions --- README.md | 4 ++-- src/main/java/Duke.java | 10 ---------- src/main/java/Nuke.java | 22 ++++++++++++++++++++++ text-ui-test/runtest.bat | 2 +- 4 files changed, 25 insertions(+), 13 deletions(-) delete mode 100644 src/main/java/Duke.java create mode 100644 src/main/java/Nuke.java diff --git a/README.md b/README.md index 8715d4d91..3673a8679 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# Nuke project template This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. @@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version. 1. If there are any further prompts, accept the defaults. 1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: +3. After that, locate the `src/main/java/Nuke.java` file, right-click it, and choose `Run Nuke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from ____ _ diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334c..000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java new file mode 100644 index 000000000..e6b5c1031 --- /dev/null +++ b/src/main/java/Nuke.java @@ -0,0 +1,22 @@ +public class Nuke { + + public static void greet() { + System.out.println("Hello! I'm Nuke\n" + "What can I do for you?"); + } + + public static void exit() { + System.out.println("Bye. Hope to see you again soon!"); + } + + public static void line() { + System.out.println("____________________________________________________________"); + } + + public static void main(String[] args) { + line(); + greet(); + line(); + exit(); + line(); + } +} diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..d22f2fb7c 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Duke < input.txt > ACTUAL.TXT +java -classpath ..\bin Nuke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From a30a0be2b6923f26cf4034e512b5e5b5458896b2 Mon Sep 17 00:00:00 2001 From: Cazh2 Date: Thu, 31 Aug 2023 15:40:16 +0800 Subject: [PATCH 02/46] Add Echo function --- src/main/java/Nuke.java | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java index e6b5c1031..f41346209 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/Nuke.java @@ -1,22 +1,39 @@ +import java.util.Scanner; public class Nuke { public static void greet() { + line(); System.out.println("Hello! I'm Nuke\n" + "What can I do for you?"); + line(); } public static void exit() { + line(); System.out.println("Bye. Hope to see you again soon!"); + line(); } public static void line() { System.out.println("____________________________________________________________"); } + public static void echo() { + + Scanner input = new Scanner(System.in); + String item = input.nextLine(); + if (item.equals("bye")) { + exit(); + return; + } else { + line(); + System.out.println(item); + line(); + echo(); + } + } + public static void main(String[] args) { - line(); greet(); - line(); - exit(); - line(); + echo(); } } From ff4554d95a5a5c4fced670387498fecaa7278e3f Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Sat, 2 Sep 2023 21:40:10 +0800 Subject: [PATCH 03/46] Add List and Add function --- src/main/java/Nuke.java | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java index f41346209..c2475b165 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/Nuke.java @@ -1,4 +1,6 @@ import java.util.Scanner; +import java.util.ArrayList; + public class Nuke { public static void greet() { @@ -18,7 +20,6 @@ public static void line() { } public static void echo() { - Scanner input = new Scanner(System.in); String item = input.nextLine(); if (item.equals("bye")) { @@ -32,8 +33,36 @@ public static void echo() { } } + public static void add(ArrayList itemList) { + Scanner input = new Scanner(System.in); + String item = input.nextLine(); + + if (item.equals("bye")) { + exit(); + return; + }if (item.equals("list")) { + list(itemList); + } else { + line(); + itemList.add(item); + System.out.printf("added: %s\n", item); + line(); + add(itemList); + } + } + + public static void list(ArrayList itemList) { + line(); + for (int i = 0; i < itemList.size(); i++) { + System.out.printf("%d. %s\n", i, itemList.get(i)); + } + line(); + add(itemList); + } + public static void main(String[] args) { greet(); - echo(); + ArrayList itemList = new ArrayList(); + add(itemList); } } From 899542c6e1fb067156f6cef88817d11a49e32411 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Sat, 2 Sep 2023 22:35:44 +0800 Subject: [PATCH 04/46] Add Mark-as-Done function --- src/main/java/Nuke.java | 59 ++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java index c2475b165..6b6bcd4dc 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/Nuke.java @@ -5,18 +5,18 @@ public class Nuke { public static void greet() { line(); - System.out.println("Hello! I'm Nuke\n" + "What can I do for you?"); + System.out.println(" Hello! I'm Nuke\n" + " What can I do for you?"); line(); } public static void exit() { line(); - System.out.println("Bye. Hope to see you again soon!"); + System.out.println(" Bye. Hope to see you again soon!"); line(); } public static void line() { - System.out.println("____________________________________________________________"); + System.out.println(" ____________________________________________________________"); } public static void echo() { @@ -33,36 +33,65 @@ public static void echo() { } } - public static void add(ArrayList itemList) { + public static void dialogue(ArrayList itemList, ArrayList markList) { Scanner input = new Scanner(System.in); String item = input.nextLine(); - - if (item.equals("bye")) { + if (item.equals("bye")) { exit(); return; - }if (item.equals("list")) { - list(itemList); + } else if (item.equals("list")) { + list(itemList, markList); + } else if (item.length() > 4 && item.substring(0, 4).equals("mark")) { + String[] splitItem = item.split(" "); + int markNum = Integer.parseInt(splitItem[1]); + mark(itemList, markList, markNum); + } else if (item.length() > 6 && item.substring(0, 6).equals("unmark")) { + String[] splitItem = item.split(" "); + int markNum = Integer.parseInt(splitItem[1]); + unmark(itemList, markList, markNum); } else { + add(itemList, markList, item); + } + dialogue(itemList, markList); + } + + public static void add(ArrayList itemList, ArrayList markList, String item) { line(); itemList.add(item); - System.out.printf("added: %s\n", item); + markList.add("[ ]"); + System.out.printf(" added: %s\n", item); line(); - add(itemList); - } } - public static void list(ArrayList itemList) { + public static void list(ArrayList itemList, ArrayList markList) { line(); + System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < itemList.size(); i++) { - System.out.printf("%d. %s\n", i, itemList.get(i)); + System.out.printf(" %d.%s %s\n", i+1, markList.get(i), itemList.get(i)); } line(); - add(itemList); + } + + public static void mark(ArrayList itemList, ArrayList markList, int markNum) { + line(); + System.out.println(" Nice! I've marked this task as done:"); + markList.set(markNum - 1, "[X]"); + System.out.printf(" %s %s\n", markList.get(markNum - 1), itemList.get(markNum - 1)); + line(); + } + + public static void unmark(ArrayList itemList, ArrayList markList, int markNum) { + line(); + System.out.println(" OK, I've marked this task as not done yet:"); + markList.set(markNum - 1, "[ ]"); + System.out.printf(" %s %s\n", markList.get(markNum - 1), itemList.get(markNum - 1)); + line(); } public static void main(String[] args) { greet(); ArrayList itemList = new ArrayList(); - add(itemList); + ArrayList markList = new ArrayList(); + dialogue(itemList, markList); } } From cd10722c7a782ba6ac51f54432d4197fa5bbbe34 Mon Sep 17 00:00:00 2001 From: ZH Date: Thu, 7 Sep 2023 17:11:09 +0800 Subject: [PATCH 05/46] Tweak the code to comply with coding standard --- src/main/java/Nuke.java | 60 ++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java index 6b6bcd4dc..e1aedd52d 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/Nuke.java @@ -3,19 +3,19 @@ public class Nuke { - public static void greet() { - line(); + public static void printGreetingMessage() { + printLine(); System.out.println(" Hello! I'm Nuke\n" + " What can I do for you?"); - line(); + printLine(); } - public static void exit() { - line(); + public static void printExitMessage() { + printLine(); System.out.println(" Bye. Hope to see you again soon!"); - line(); + printLine(); } - public static void line() { + public static void printLine() { System.out.println(" ____________________________________________________________"); } @@ -23,12 +23,12 @@ public static void echo() { Scanner input = new Scanner(System.in); String item = input.nextLine(); if (item.equals("bye")) { - exit(); + printExitMessage(); return; } else { - line(); + printLine(); System.out.println(item); - line(); + printLine(); echo(); } } @@ -37,18 +37,18 @@ public static void dialogue(ArrayList itemList, ArrayList markLi Scanner input = new Scanner(System.in); String item = input.nextLine(); if (item.equals("bye")) { - exit(); + printExitMessage(); return; } else if (item.equals("list")) { list(itemList, markList); } else if (item.length() > 4 && item.substring(0, 4).equals("mark")) { String[] splitItem = item.split(" "); - int markNum = Integer.parseInt(splitItem[1]); - mark(itemList, markList, markNum); + int listIndex = Integer.parseInt(splitItem[1]); + mark(itemList, markList, listIndex); } else if (item.length() > 6 && item.substring(0, 6).equals("unmark")) { String[] splitItem = item.split(" "); - int markNum = Integer.parseInt(splitItem[1]); - unmark(itemList, markList, markNum); + int listIndex = Integer.parseInt(splitItem[1]); + unmark(itemList, markList, listIndex); } else { add(itemList, markList, item); } @@ -56,40 +56,40 @@ public static void dialogue(ArrayList itemList, ArrayList markLi } public static void add(ArrayList itemList, ArrayList markList, String item) { - line(); + printLine(); itemList.add(item); markList.add("[ ]"); System.out.printf(" added: %s\n", item); - line(); + printLine(); } public static void list(ArrayList itemList, ArrayList markList) { - line(); + printLine(); System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < itemList.size(); i++) { System.out.printf(" %d.%s %s\n", i+1, markList.get(i), itemList.get(i)); } - line(); + printLine(); } - public static void mark(ArrayList itemList, ArrayList markList, int markNum) { - line(); + public static void mark(ArrayList itemList, ArrayList markList, int listIndex) { + printLine(); System.out.println(" Nice! I've marked this task as done:"); - markList.set(markNum - 1, "[X]"); - System.out.printf(" %s %s\n", markList.get(markNum - 1), itemList.get(markNum - 1)); - line(); + markList.set(listIndex - 1, "[X]"); + System.out.printf(" %s %s\n", markList.get(listIndex - 1), itemList.get(listIndex - 1)); + printLine(); } - public static void unmark(ArrayList itemList, ArrayList markList, int markNum) { - line(); + public static void unmark(ArrayList itemList, ArrayList markList, int listIndex) { + printLine(); System.out.println(" OK, I've marked this task as not done yet:"); - markList.set(markNum - 1, "[ ]"); - System.out.printf(" %s %s\n", markList.get(markNum - 1), itemList.get(markNum - 1)); - line(); + markList.set(listIndex - 1, "[ ]"); + System.out.printf(" %s %s\n", markList.get(listIndex - 1), itemList.get(listIndex - 1)); + printLine(); } public static void main(String[] args) { - greet(); + printGreetingMessage(); ArrayList itemList = new ArrayList(); ArrayList markList = new ArrayList(); dialogue(itemList, markList); From 95c15bdada1a0a35a62e8660bcf132430d2e49d6 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Thu, 7 Sep 2023 20:54:40 +0800 Subject: [PATCH 06/46] Update code logic to use Object Class --- src/main/java/Nuke.java | 77 ++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java index e1aedd52d..7252070a2 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/Nuke.java @@ -3,6 +3,28 @@ public class Nuke { + public static class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "[X]" : "[ ]"); // mark done task with X + } + + public void mark() { + this.isDone = true; + } + + public void unmark() { + this.isDone = false; + } + } + public static void printGreetingMessage() { printLine(); System.out.println(" Hello! I'm Nuke\n" + " What can I do for you?"); @@ -19,79 +41,64 @@ public static void printLine() { System.out.println(" ____________________________________________________________"); } - public static void echo() { + public static void dialogue(ArrayList taskList) { Scanner input = new Scanner(System.in); String item = input.nextLine(); if (item.equals("bye")) { printExitMessage(); return; - } else { - printLine(); - System.out.println(item); - printLine(); - echo(); - } - } - - public static void dialogue(ArrayList itemList, ArrayList markList) { - Scanner input = new Scanner(System.in); - String item = input.nextLine(); - if (item.equals("bye")) { - printExitMessage(); - return; } else if (item.equals("list")) { - list(itemList, markList); + list(taskList); } else if (item.length() > 4 && item.substring(0, 4).equals("mark")) { String[] splitItem = item.split(" "); int listIndex = Integer.parseInt(splitItem[1]); - mark(itemList, markList, listIndex); + mark(taskList, listIndex); } else if (item.length() > 6 && item.substring(0, 6).equals("unmark")) { String[] splitItem = item.split(" "); int listIndex = Integer.parseInt(splitItem[1]); - unmark(itemList, markList, listIndex); + unmark(taskList, listIndex); } else { - add(itemList, markList, item); + add(taskList, item); } - dialogue(itemList, markList); + dialogue(taskList); } - public static void add(ArrayList itemList, ArrayList markList, String item) { + public static void add(ArrayList taskList, String item) { printLine(); - itemList.add(item); - markList.add("[ ]"); + Task newTask = new Task(item); + taskList.add(newTask); System.out.printf(" added: %s\n", item); printLine(); } - public static void list(ArrayList itemList, ArrayList markList) { + public static void list(ArrayList taskList) { printLine(); System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < itemList.size(); i++) { - System.out.printf(" %d.%s %s\n", i+1, markList.get(i), itemList.get(i)); + for (int i = 0; i < taskList.size(); i++) { + System.out.printf(" %d.%s %s\n", i+1, taskList.get(i).getStatusIcon(), taskList.get(i).description); } printLine(); } - public static void mark(ArrayList itemList, ArrayList markList, int listIndex) { + public static void mark(ArrayList taskList, int listIndex) { printLine(); System.out.println(" Nice! I've marked this task as done:"); - markList.set(listIndex - 1, "[X]"); - System.out.printf(" %s %s\n", markList.get(listIndex - 1), itemList.get(listIndex - 1)); + taskList.get(listIndex - 1).mark();; + System.out.printf(" %s %s\n", taskList.get(listIndex - 1).getStatusIcon(), taskList.get(listIndex - 1).description); printLine(); } - public static void unmark(ArrayList itemList, ArrayList markList, int listIndex) { + public static void unmark(ArrayList taskList, int listIndex) { printLine(); System.out.println(" OK, I've marked this task as not done yet:"); - markList.set(listIndex - 1, "[ ]"); - System.out.printf(" %s %s\n", markList.get(listIndex - 1), itemList.get(listIndex - 1)); + taskList.get(listIndex - 1).unmark(); + System.out.printf(" %s %s\n", taskList.get(listIndex - 1).getStatusIcon(), taskList.get(listIndex - 1).description); printLine(); } public static void main(String[] args) { printGreetingMessage(); - ArrayList itemList = new ArrayList(); - ArrayList markList = new ArrayList(); - dialogue(itemList, markList); + ArrayList taskList = new ArrayList(); + dialogue(taskList); } } From 3d50f0acabef1cc7214c68c4989b4eedab9dd834 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Thu, 7 Sep 2023 22:47:25 +0800 Subject: [PATCH 07/46] Add tracking for types of tasks --- src/main/java/Deadline.java | 15 +++++ src/main/java/Event.java | 15 +++++ src/main/java/Nuke.java | 112 ++++++++++++++++++++++++++---------- src/main/java/Task.java | 32 +++++++++++ src/main/java/Todo.java | 9 +++ 5 files changed, 152 insertions(+), 31 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Task.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..0c019960a --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,15 @@ +public class Deadline extends Task{ + protected String deadline; + public Deadline(String description, String taskType, String deadline) { + super(description, taskType); + this.deadline = deadline; + } + + public String getDeadline() { + return deadline; + } + + public String toString() { + return taskType + super.toString() + deadline; + } +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..67e186791 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,15 @@ +public class Event extends Task{ + protected String dateTime; + public Event(String description, String taskType, String dateTime) { + super(description, taskType); + this.dateTime = dateTime; + } + + public String getDateTime() { + return dateTime; + } + + public String toString() { + return taskType + super.toString() + dateTime; + } +} diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java index 7252070a2..bd43267cf 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/Nuke.java @@ -3,28 +3,6 @@ public class Nuke { - public static class Task { - protected String description; - protected boolean isDone; - - public Task(String description) { - this.description = description; - this.isDone = false; - } - - public String getStatusIcon() { - return (isDone ? "[X]" : "[ ]"); // mark done task with X - } - - public void mark() { - this.isDone = true; - } - - public void unmark() { - this.isDone = false; - } - } - public static void printGreetingMessage() { printLine(); System.out.println(" Hello! I'm Nuke\n" + " What can I do for you?"); @@ -41,7 +19,47 @@ public static void printLine() { System.out.println(" ____________________________________________________________"); } - public static void dialogue(ArrayList taskList) { + public static boolean checkTextForMark(String item) { + if (item.length() > 4 && item.substring(0, 4).equals("mark")) { + return true; + } else { + return false; + } + } + + public static boolean checkTextForUnmark(String item) { + if (item.length() > 6 && item.substring(0, 6).equals("unmark")) { + return true; + } else { + return false; + } + } + + public static boolean checkTextForTodo(String item) { + if (item.length() > 4 && item.substring(0, 4).equals("todo")) { + return true; + } else { + return false; + } + } + + public static boolean checkTextForDeadline(String item) { + if (item.length() > 8 && item.substring(0, 8).equals("deadline")) { + return true; + } else { + return false; + } + } + + public static boolean checkTextForEvent(String item) { + if (item.length() > 5 && item.substring(0, 5).equals("event")) { + return true; + } else { + return false; + } + } + + public static void botDialogue(ArrayList taskList) { Scanner input = new Scanner(System.in); String item = input.nextLine(); if (item.equals("bye")) { @@ -49,25 +67,57 @@ public static void dialogue(ArrayList taskList) { return; } else if (item.equals("list")) { list(taskList); - } else if (item.length() > 4 && item.substring(0, 4).equals("mark")) { + } else if (checkTextForMark(item)) { String[] splitItem = item.split(" "); int listIndex = Integer.parseInt(splitItem[1]); mark(taskList, listIndex); - } else if (item.length() > 6 && item.substring(0, 6).equals("unmark")) { + } else if (checkTextForUnmark(item)) { String[] splitItem = item.split(" "); int listIndex = Integer.parseInt(splitItem[1]); unmark(taskList, listIndex); } else { add(taskList, item); } - dialogue(taskList); + botDialogue(taskList); + } + + public static void addTodo(ArrayList taskList, String item) { + String formattedString = item.replace("todo ", ""); + String taskType = "[T]"; + Task newTask = new Todo(formattedString, taskType); + taskList.add(newTask); + System.out.printf(" Got it. I've added this task:\n %s\n Now you have %d tasks in the list.\n", newTask, taskList.size()); + } + + public static void addDeadline(ArrayList taskList, String item) { + String formattedString[] = item.replace("deadline ", "").split("/"); + String taskType = "[D]"; + String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; + Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); + taskList.add(newTask); + System.out.printf(" Got it. I've added this task:\n %s\n Now you have %d tasks in the list.\n", newTask, taskList.size()); + } + + public static void addEvent(ArrayList taskList, String item) { + String formattedString[] = item.replace("event ", "").split("/"); + String taskType = "[E]"; + String eventFrom = formattedString[1].replace("from", "from:"); + String eventTo = formattedString[2].replace("to", "to:"); + String dateTime = "(" + eventFrom + eventTo + ")"; + Task newTask = new Event(formattedString[0], taskType, dateTime); + taskList.add(newTask); + System.out.printf(" Got it. I've added this task:\n %s\n Now you have %d tasks in the list.\n", newTask, taskList.size()); } public static void add(ArrayList taskList, String item) { printLine(); - Task newTask = new Task(item); - taskList.add(newTask); - System.out.printf(" added: %s\n", item); + if (checkTextForTodo(item)) { + addTodo(taskList, item); + } else if (checkTextForDeadline(item)) { + addDeadline(taskList, item); + } else if (checkTextForEvent(item)) { + addEvent(taskList, item); + } printLine(); } @@ -75,7 +125,7 @@ public static void list(ArrayList taskList) { printLine(); System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < taskList.size(); i++) { - System.out.printf(" %d.%s %s\n", i+1, taskList.get(i).getStatusIcon(), taskList.get(i).description); + System.out.printf(" %d.%s\n", i+1, taskList.get(i)); } printLine(); } @@ -99,6 +149,6 @@ public static void unmark(ArrayList taskList, int listIndex) { public static void main(String[] args) { printGreetingMessage(); ArrayList taskList = new ArrayList(); - dialogue(taskList); + botDialogue(taskList); } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..86eaafae5 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,32 @@ +public class Task { + protected String description; + protected boolean isDone; + protected String taskType; + protected int taskIndex; + + public Task(String description, String taskType) { + this.description = description; + this.isDone = false; + this.taskType = taskType; + } + + public String getStatusIcon() { + return (isDone ? "[X]" : "[ ]"); // mark done task with X + } + + public String getTaskType() { + return taskType; + } + + public void mark() { + this.isDone = true; + } + + public void unmark() { + this.isDone = false; + } + + public String toString() { + return getStatusIcon() + description; + } +} diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..01119fc9b --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,9 @@ +public class Todo extends Task{ + public Todo(String description, String taskType) { + super(description, taskType); + } + + public String toString() { + return taskType + super.toString(); + } +} From c7b6dacaecd9b87bcfff6e96a33a30a76f7c36d9 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Thu, 7 Sep 2023 22:53:19 +0800 Subject: [PATCH 08/46] Update methods to use Object class --- src/main/java/Nuke.java | 4 ++-- src/main/java/Task.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java index bd43267cf..13256f9e8 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/Nuke.java @@ -134,7 +134,7 @@ public static void mark(ArrayList taskList, int listIndex) { printLine(); System.out.println(" Nice! I've marked this task as done:"); taskList.get(listIndex - 1).mark();; - System.out.printf(" %s %s\n", taskList.get(listIndex - 1).getStatusIcon(), taskList.get(listIndex - 1).description); + System.out.printf(" %s\n", taskList.get(listIndex - 1)); printLine(); } @@ -142,7 +142,7 @@ public static void unmark(ArrayList taskList, int listIndex) { printLine(); System.out.println(" OK, I've marked this task as not done yet:"); taskList.get(listIndex - 1).unmark(); - System.out.printf(" %s %s\n", taskList.get(listIndex - 1).getStatusIcon(), taskList.get(listIndex - 1).description); + System.out.printf(" %s\n", taskList.get(listIndex - 1)); printLine(); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 86eaafae5..c3acc76e0 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -27,6 +27,6 @@ public void unmark() { } public String toString() { - return getStatusIcon() + description; + return getStatusIcon() + " " + description; } } From a5d59146d3844d55fa7f185f8bc3eb0e4a099750 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Thu, 7 Sep 2023 22:53:19 +0800 Subject: [PATCH 09/46] Update methods to use Object class --- src/main/java/Nuke.java | 36 ++++++++++++++++++++++++++---------- src/main/java/Task.java | 2 +- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java index bd43267cf..7aaafe69b 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/Nuke.java @@ -19,46 +19,52 @@ public static void printLine() { System.out.println(" ____________________________________________________________"); } + //Returns True if the text contains "mark", False if not public static boolean checkTextForMark(String item) { - if (item.length() > 4 && item.substring(0, 4).equals("mark")) { + if (item.length() > 4 && item.startsWith("mark")) { return true; } else { return false; } } + //Returns True if the text contains "unmark", False if not public static boolean checkTextForUnmark(String item) { - if (item.length() > 6 && item.substring(0, 6).equals("unmark")) { + if (item.length() > 6 && item.startsWith("unmark")) { return true; } else { return false; } } + //Returns True if the text contains "todo", False if not public static boolean checkTextForTodo(String item) { - if (item.length() > 4 && item.substring(0, 4).equals("todo")) { + if (item.length() > 4 && item.startsWith("todo")) { return true; } else { return false; } } + //Returns True if the text contains "deadline", False if not public static boolean checkTextForDeadline(String item) { - if (item.length() > 8 && item.substring(0, 8).equals("deadline")) { + if (item.length() > 8 && item.startsWith("deadline")) { return true; } else { return false; } } + //Returns True if the text contains "event", False if not public static boolean checkTextForEvent(String item) { - if (item.length() > 5 && item.substring(0, 5).equals("event")) { + if (item.length() > 5 && item.startsWith("event")) { return true; } else { return false; } } + //Main logic for the bot's dialogue public static void botDialogue(ArrayList taskList) { Scanner input = new Scanner(System.in); String item = input.nextLine(); @@ -81,23 +87,30 @@ public static void botDialogue(ArrayList taskList) { botDialogue(taskList); } + //Creates a "Todo" object and adds to the taskList public static void addTodo(ArrayList taskList, String item) { String formattedString = item.replace("todo ", ""); String taskType = "[T]"; Task newTask = new Todo(formattedString, taskType); taskList.add(newTask); - System.out.printf(" Got it. I've added this task:\n %s\n Now you have %d tasks in the list.\n", newTask, taskList.size()); + System.out.println(" Got it. I've added this task:"); + System.out.printf(" %s\n", newTask); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); } + //Creates a "Deadline" object and adds to the taskList public static void addDeadline(ArrayList taskList, String item) { String formattedString[] = item.replace("deadline ", "").split("/"); String taskType = "[D]"; String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); taskList.add(newTask); - System.out.printf(" Got it. I've added this task:\n %s\n Now you have %d tasks in the list.\n", newTask, taskList.size()); + System.out.println(" Got it. I've added this task:"); + System.out.printf(" %s\n", newTask); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); } + //Creates a "Event" object and adds to the taskList public static void addEvent(ArrayList taskList, String item) { String formattedString[] = item.replace("event ", "").split("/"); String taskType = "[E]"; @@ -106,9 +119,12 @@ public static void addEvent(ArrayList taskList, String item) { String dateTime = "(" + eventFrom + eventTo + ")"; Task newTask = new Event(formattedString[0], taskType, dateTime); taskList.add(newTask); - System.out.printf(" Got it. I've added this task:\n %s\n Now you have %d tasks in the list.\n", newTask, taskList.size()); + System.out.println(" Got it. I've added this task:"); + System.out.printf(" %s\n", newTask); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); } + //Controls the logic for adding items to the list public static void add(ArrayList taskList, String item) { printLine(); if (checkTextForTodo(item)) { @@ -134,7 +150,7 @@ public static void mark(ArrayList taskList, int listIndex) { printLine(); System.out.println(" Nice! I've marked this task as done:"); taskList.get(listIndex - 1).mark();; - System.out.printf(" %s %s\n", taskList.get(listIndex - 1).getStatusIcon(), taskList.get(listIndex - 1).description); + System.out.printf(" %s\n", taskList.get(listIndex - 1)); printLine(); } @@ -142,7 +158,7 @@ public static void unmark(ArrayList taskList, int listIndex) { printLine(); System.out.println(" OK, I've marked this task as not done yet:"); taskList.get(listIndex - 1).unmark(); - System.out.printf(" %s %s\n", taskList.get(listIndex - 1).getStatusIcon(), taskList.get(listIndex - 1).description); + System.out.printf(" %s\n", taskList.get(listIndex - 1)); printLine(); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 86eaafae5..c3acc76e0 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -27,6 +27,6 @@ public void unmark() { } public String toString() { - return getStatusIcon() + description; + return getStatusIcon() + " " + description; } } From 8e1aead80ee3d1e0a30be66f75a0afa6326e8e3e Mon Sep 17 00:00:00 2001 From: ZH Date: Tue, 12 Sep 2023 22:30:23 +0800 Subject: [PATCH 10/46] Refactor code --- README.md | 4 ++-- src/main/java/{Nuke.java => KenergeticBot.java} | 4 ++-- text-ui-test/runtest.bat | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/main/java/{Nuke.java => KenergeticBot.java} (98%) diff --git a/README.md b/README.md index 3673a8679..626f6f905 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Nuke project template +# KenergeticBot project template This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. @@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version. 1. If there are any further prompts, accept the defaults. 1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Nuke.java` file, right-click it, and choose `Run Nuke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: +3. After that, locate the `src/main/java/KenergeticBot.java` file, right-click it, and choose `Run KenergeticBot.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from ____ _ diff --git a/src/main/java/Nuke.java b/src/main/java/KenergeticBot.java similarity index 98% rename from src/main/java/Nuke.java rename to src/main/java/KenergeticBot.java index 7aaafe69b..d34fd9e61 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/KenergeticBot.java @@ -1,11 +1,11 @@ import java.util.Scanner; import java.util.ArrayList; -public class Nuke { +public class KenergeticBot { public static void printGreetingMessage() { printLine(); - System.out.println(" Hello! I'm Nuke\n" + " What can I do for you?"); + System.out.println(" Hello! I'm KenergeticBot\n" + " What can I do for you?"); printLine(); } diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index d22f2fb7c..d1f753472 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Nuke < input.txt > ACTUAL.TXT +java -classpath ..\bin KenergeticBot < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From 3dbdfbc733d3bcf074f9d28ef04e96eb1bc5300f Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Tue, 12 Sep 2023 23:55:46 +0800 Subject: [PATCH 11/46] Add Error Handling function --- src/main/java/KenergeticBot.java | 35 +++-- src/main/java/KenergeticBotException.java | 3 + src/main/java/Nuke.java | 171 ++++++++++++++++++++++ text-ui-test/input.txt | 4 + 4 files changed, 203 insertions(+), 10 deletions(-) create mode 100644 src/main/java/KenergeticBotException.java create mode 100644 src/main/java/Nuke.java diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index d34fd9e61..a489fa7dd 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -39,7 +39,7 @@ public static boolean checkTextForUnmark(String item) { //Returns True if the text contains "todo", False if not public static boolean checkTextForTodo(String item) { - if (item.length() > 4 && item.startsWith("todo")) { + if (item.length() > 3 && item.startsWith("todo")) { return true; } else { return false; @@ -48,7 +48,7 @@ public static boolean checkTextForTodo(String item) { //Returns True if the text contains "deadline", False if not public static boolean checkTextForDeadline(String item) { - if (item.length() > 8 && item.startsWith("deadline")) { + if (item.length() > 7 && item.startsWith("deadline")) { return true; } else { return false; @@ -57,7 +57,7 @@ public static boolean checkTextForDeadline(String item) { //Returns True if the text contains "event", False if not public static boolean checkTextForEvent(String item) { - if (item.length() > 5 && item.startsWith("event")) { + if (item.length() > 4 && item.startsWith("event")) { return true; } else { return false; @@ -82,14 +82,23 @@ public static void botDialogue(ArrayList taskList) { int listIndex = Integer.parseInt(splitItem[1]); unmark(taskList, listIndex); } else { - add(taskList, item); + try { + add(taskList, item); + } catch (KenergeticBotException e) { + System.out.println(" OOPS!!! I'm sorry, but I don't know what that means :-("); // unable to print sad face ˙◠˙ + } } botDialogue(taskList); } //Creates a "Todo" object and adds to the taskList - public static void addTodo(ArrayList taskList, String item) { - String formattedString = item.replace("todo ", ""); + public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { + String formattedString = item.replace("todo", "").trim(); + System.out.println("length = " + formattedString.length()); + if (formattedString.length() < 1) { + + throw new KenergeticBotException(); + } String taskType = "[T]"; Task newTask = new Todo(formattedString, taskType); taskList.add(newTask); @@ -100,7 +109,7 @@ public static void addTodo(ArrayList taskList, String item) { //Creates a "Deadline" object and adds to the taskList public static void addDeadline(ArrayList taskList, String item) { - String formattedString[] = item.replace("deadline ", "").split("/"); + String formattedString[] = item.replace("deadline", "").trim().split("/"); String taskType = "[D]"; String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); @@ -112,7 +121,7 @@ public static void addDeadline(ArrayList taskList, String item) { //Creates a "Event" object and adds to the taskList public static void addEvent(ArrayList taskList, String item) { - String formattedString[] = item.replace("event ", "").split("/"); + String formattedString[] = item.replace("event", "").split("/"); String taskType = "[E]"; String eventFrom = formattedString[1].replace("from", "from:"); String eventTo = formattedString[2].replace("to", "to:"); @@ -125,14 +134,20 @@ public static void addEvent(ArrayList taskList, String item) { } //Controls the logic for adding items to the list - public static void add(ArrayList taskList, String item) { + public static void add(ArrayList taskList, String item) throws KenergeticBotException { printLine(); if (checkTextForTodo(item)) { - addTodo(taskList, item); + try { + addTodo(taskList, item); + } catch (KenergeticBotException e) { + System.out.println(" \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."); + } } else if (checkTextForDeadline(item)) { addDeadline(taskList, item); } else if (checkTextForEvent(item)) { addEvent(taskList, item); + } else { + throw new KenergeticBotException(); } printLine(); } diff --git a/src/main/java/KenergeticBotException.java b/src/main/java/KenergeticBotException.java new file mode 100644 index 000000000..bf1104721 --- /dev/null +++ b/src/main/java/KenergeticBotException.java @@ -0,0 +1,3 @@ +public class KenergeticBotException extends Exception { + //no other code needed +} \ No newline at end of file diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java new file mode 100644 index 000000000..0414c8e2c --- /dev/null +++ b/src/main/java/Nuke.java @@ -0,0 +1,171 @@ +import java.util.Scanner; +import java.util.ArrayList; + +public class Nuke { + + public static void printGreetingMessage() { + printLine(); + System.out.println(" Hello! I'm Nuke\n" + " What can I do for you?"); + printLine(); + } + + public static void printExitMessage() { + printLine(); + System.out.println(" Bye. Hope to see you again soon!"); + printLine(); + } + + public static void printLine() { + System.out.println(" ____________________________________________________________"); + } + + //Returns True if the text contains "mark", False if not + public static boolean checkTextForMark(String item) { + if (item.length() > 4 && item.startsWith("mark")) { + return true; + } else { + return false; + } + } + + //Returns True if the text contains "unmark", False if not + public static boolean checkTextForUnmark(String item) { + if (item.length() > 6 && item.startsWith("unmark")) { + return true; + } else { + return false; + } + } + + //Returns True if the text contains "todo", False if not + public static boolean checkTextForTodo(String item) { + if (item.length() > 4 && item.startsWith("todo")) { + return true; + } else { + return false; + } + } + + //Returns True if the text contains "deadline", False if not + public static boolean checkTextForDeadline(String item) { + if (item.length() > 8 && item.startsWith("deadline")) { + return true; + } else { + return false; + } + } + + //Returns True if the text contains "event", False if not + public static boolean checkTextForEvent(String item) { + if (item.length() > 5 && item.startsWith("event")) { + return true; + } else { + return false; + } + } + + //Main logic for the bot's dialogue + public static void botDialogue(ArrayList taskList) { + Scanner input = new Scanner(System.in); + String item = input.nextLine(); + if (item.equals("bye")) { + printExitMessage(); + return; + } else if (item.equals("list")) { + list(taskList); + } else if (checkTextForMark(item)) { + String[] splitItem = item.split(" "); + int listIndex = Integer.parseInt(splitItem[1]); + mark(taskList, listIndex); + } else if (checkTextForUnmark(item)) { + String[] splitItem = item.split(" "); + int listIndex = Integer.parseInt(splitItem[1]); + unmark(taskList, listIndex); + } else { + add(taskList, item); + } + botDialogue(taskList); + } + + //Creates a "Todo" object and adds to the taskList + public static void addTodo(ArrayList taskList, String item) { + String formattedString = item.replace("todo ", ""); + String taskType = "[T]"; + Task newTask = new Todo(formattedString, taskType); + taskList.add(newTask); + System.out.println(" Got it. I've added this task:"); + System.out.printf(" %s\n", newTask); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); + } + + //Creates a "Deadline" object and adds to the taskList + public static void addDeadline(ArrayList taskList, String item) { + String formattedString[] = item.replace("deadline ", "").split("/"); + String taskType = "[D]"; + String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; + Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); + taskList.add(newTask); + System.out.println(" Got it. I've added this task:"); + System.out.printf(" %s\n", newTask); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); + } + + //Creates a "Event" object and adds to the taskList + public static void addEvent(ArrayList taskList, String item) { + String formattedString[] = item.replace("event ", "").split("/"); + String taskType = "[E]"; + String eventFrom = formattedString[1].replace("from", "from:"); + String eventTo = formattedString[2].replace("to", "to:"); + String dateTime = "(" + eventFrom + eventTo + ")"; + Task newTask = new Event(formattedString[0], taskType, dateTime); + taskList.add(newTask); + System.out.println(" Got it. I've added this task:"); + System.out.printf(" %s\n", newTask); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); + } + + //Controls the logic for adding items to the list + public static void add(ArrayList taskList, String item) { + printLine(); + if (checkTextForTodo(item)) { + addTodo(taskList, item); + } else if (checkTextForDeadline(item)) { + addDeadline(taskList, item); + } else if (checkTextForEvent(item)) { + addEvent(taskList, item); + } + printLine(); + } + + public static void list(ArrayList taskList) { + printLine(); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < taskList.size(); i++) { + System.out.printf(" %d.%s\n", i+1, taskList.get(i)); + } + printLine(); + + } + + public static void mark(ArrayList taskList, int listIndex) { + printLine(); + System.out.println(" Nice! I've marked this task as done:"); + taskList.get(listIndex - 1).mark();; + System.out.printf(" %s\n", taskList.get(listIndex - 1)); + printLine(); + } + + public static void unmark(ArrayList taskList, int listIndex) { + printLine(); + System.out.println(" OK, I've marked this task as not done yet:"); + taskList.get(listIndex - 1).unmark(); + System.out.printf(" %s\n", taskList.get(listIndex - 1)); + printLine(); + } + + public static void main(String[] args) { + printGreetingMessage(); + ArrayList taskList = new ArrayList(); + botDialogue(taskList); + } +} diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..4e4722e9c 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,4 @@ +todo borrow book +list +deadline return book /by Sunday +event project meeting /from Mon 2pm /to 4pm \ No newline at end of file From 544d7c91f8e5d466c1b76ff8bc8f9b1649924e91 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Wed, 13 Sep 2023 00:11:54 +0800 Subject: [PATCH 12/46] Add Error Handling function --- .../java/{Nuke.java => KenergeticBot.java} | 38 +++++++++++++------ src/main/java/KenergeticBotException.java | 3 ++ 2 files changed, 29 insertions(+), 12 deletions(-) rename src/main/java/{Nuke.java => KenergeticBot.java} (79%) create mode 100644 src/main/java/KenergeticBotException.java diff --git a/src/main/java/Nuke.java b/src/main/java/KenergeticBot.java similarity index 79% rename from src/main/java/Nuke.java rename to src/main/java/KenergeticBot.java index 7aaafe69b..d7adcef4b 100644 --- a/src/main/java/Nuke.java +++ b/src/main/java/KenergeticBot.java @@ -1,11 +1,11 @@ import java.util.Scanner; import java.util.ArrayList; -public class Nuke { +public class KenergeticBot { public static void printGreetingMessage() { printLine(); - System.out.println(" Hello! I'm Nuke\n" + " What can I do for you?"); + System.out.println(" Hello! I'm KenergeticBot\n" + " What can I do for you?"); printLine(); } @@ -39,7 +39,7 @@ public static boolean checkTextForUnmark(String item) { //Returns True if the text contains "todo", False if not public static boolean checkTextForTodo(String item) { - if (item.length() > 4 && item.startsWith("todo")) { + if (item.length() > 3 && item.startsWith("todo")) { return true; } else { return false; @@ -48,7 +48,7 @@ public static boolean checkTextForTodo(String item) { //Returns True if the text contains "deadline", False if not public static boolean checkTextForDeadline(String item) { - if (item.length() > 8 && item.startsWith("deadline")) { + if (item.length() > 7 && item.startsWith("deadline")) { return true; } else { return false; @@ -57,7 +57,7 @@ public static boolean checkTextForDeadline(String item) { //Returns True if the text contains "event", False if not public static boolean checkTextForEvent(String item) { - if (item.length() > 5 && item.startsWith("event")) { + if (item.length() > 4 && item.startsWith("event")) { return true; } else { return false; @@ -82,14 +82,22 @@ public static void botDialogue(ArrayList taskList) { int listIndex = Integer.parseInt(splitItem[1]); unmark(taskList, listIndex); } else { - add(taskList, item); + try { + add(taskList, item); + } catch (KenergeticBotException e) { //exception thrown when user inputs command outside of the usual commands + System.out.println(" OOPS!!! I'm sorry, but I don't know what that means :-("); // unable to print sad face ˙◠˙ + } } botDialogue(taskList); } //Creates a "Todo" object and adds to the taskList - public static void addTodo(ArrayList taskList, String item) { - String formattedString = item.replace("todo ", ""); + public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { + String formattedString = item.replace("todo", "").trim(); + if (formattedString.length() < 1) { + + throw new KenergeticBotException(); + } String taskType = "[T]"; Task newTask = new Todo(formattedString, taskType); taskList.add(newTask); @@ -100,7 +108,7 @@ public static void addTodo(ArrayList taskList, String item) { //Creates a "Deadline" object and adds to the taskList public static void addDeadline(ArrayList taskList, String item) { - String formattedString[] = item.replace("deadline ", "").split("/"); + String formattedString[] = item.replace("deadline", "").trim().split("/"); String taskType = "[D]"; String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); @@ -112,7 +120,7 @@ public static void addDeadline(ArrayList taskList, String item) { //Creates a "Event" object and adds to the taskList public static void addEvent(ArrayList taskList, String item) { - String formattedString[] = item.replace("event ", "").split("/"); + String formattedString[] = item.replace("event", "").split("/"); String taskType = "[E]"; String eventFrom = formattedString[1].replace("from", "from:"); String eventTo = formattedString[2].replace("to", "to:"); @@ -125,14 +133,20 @@ public static void addEvent(ArrayList taskList, String item) { } //Controls the logic for adding items to the list - public static void add(ArrayList taskList, String item) { + public static void add(ArrayList taskList, String item) throws KenergeticBotException { printLine(); if (checkTextForTodo(item)) { - addTodo(taskList, item); + try { + addTodo(taskList, item); + } catch (KenergeticBotException e) { //throws exception when the todo command is not followed with a description + System.out.println(" \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."); + } } else if (checkTextForDeadline(item)) { addDeadline(taskList, item); } else if (checkTextForEvent(item)) { addEvent(taskList, item); + } else { + throw new KenergeticBotException(); } printLine(); } diff --git a/src/main/java/KenergeticBotException.java b/src/main/java/KenergeticBotException.java new file mode 100644 index 000000000..bf1104721 --- /dev/null +++ b/src/main/java/KenergeticBotException.java @@ -0,0 +1,3 @@ +public class KenergeticBotException extends Exception { + //no other code needed +} \ No newline at end of file From 060f8b758466227af156d0dce60c66d96669e3a8 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Wed, 13 Sep 2023 00:20:44 +0800 Subject: [PATCH 13/46] Revert "Add Error Handling function" This reverts commit 3dbdfbc733d3bcf074f9d28ef04e96eb1bc5300f. --- src/main/java/KenergeticBot.java | 35 ++--- src/main/java/KenergeticBotException.java | 3 - src/main/java/Nuke.java | 171 ---------------------- text-ui-test/input.txt | 4 - 4 files changed, 10 insertions(+), 203 deletions(-) delete mode 100644 src/main/java/KenergeticBotException.java delete mode 100644 src/main/java/Nuke.java diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index a489fa7dd..d34fd9e61 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -39,7 +39,7 @@ public static boolean checkTextForUnmark(String item) { //Returns True if the text contains "todo", False if not public static boolean checkTextForTodo(String item) { - if (item.length() > 3 && item.startsWith("todo")) { + if (item.length() > 4 && item.startsWith("todo")) { return true; } else { return false; @@ -48,7 +48,7 @@ public static boolean checkTextForTodo(String item) { //Returns True if the text contains "deadline", False if not public static boolean checkTextForDeadline(String item) { - if (item.length() > 7 && item.startsWith("deadline")) { + if (item.length() > 8 && item.startsWith("deadline")) { return true; } else { return false; @@ -57,7 +57,7 @@ public static boolean checkTextForDeadline(String item) { //Returns True if the text contains "event", False if not public static boolean checkTextForEvent(String item) { - if (item.length() > 4 && item.startsWith("event")) { + if (item.length() > 5 && item.startsWith("event")) { return true; } else { return false; @@ -82,23 +82,14 @@ public static void botDialogue(ArrayList taskList) { int listIndex = Integer.parseInt(splitItem[1]); unmark(taskList, listIndex); } else { - try { - add(taskList, item); - } catch (KenergeticBotException e) { - System.out.println(" OOPS!!! I'm sorry, but I don't know what that means :-("); // unable to print sad face ˙◠˙ - } + add(taskList, item); } botDialogue(taskList); } //Creates a "Todo" object and adds to the taskList - public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { - String formattedString = item.replace("todo", "").trim(); - System.out.println("length = " + formattedString.length()); - if (formattedString.length() < 1) { - - throw new KenergeticBotException(); - } + public static void addTodo(ArrayList taskList, String item) { + String formattedString = item.replace("todo ", ""); String taskType = "[T]"; Task newTask = new Todo(formattedString, taskType); taskList.add(newTask); @@ -109,7 +100,7 @@ public static void addTodo(ArrayList taskList, String item) throws Kenerge //Creates a "Deadline" object and adds to the taskList public static void addDeadline(ArrayList taskList, String item) { - String formattedString[] = item.replace("deadline", "").trim().split("/"); + String formattedString[] = item.replace("deadline ", "").split("/"); String taskType = "[D]"; String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); @@ -121,7 +112,7 @@ public static void addDeadline(ArrayList taskList, String item) { //Creates a "Event" object and adds to the taskList public static void addEvent(ArrayList taskList, String item) { - String formattedString[] = item.replace("event", "").split("/"); + String formattedString[] = item.replace("event ", "").split("/"); String taskType = "[E]"; String eventFrom = formattedString[1].replace("from", "from:"); String eventTo = formattedString[2].replace("to", "to:"); @@ -134,20 +125,14 @@ public static void addEvent(ArrayList taskList, String item) { } //Controls the logic for adding items to the list - public static void add(ArrayList taskList, String item) throws KenergeticBotException { + public static void add(ArrayList taskList, String item) { printLine(); if (checkTextForTodo(item)) { - try { - addTodo(taskList, item); - } catch (KenergeticBotException e) { - System.out.println(" \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."); - } + addTodo(taskList, item); } else if (checkTextForDeadline(item)) { addDeadline(taskList, item); } else if (checkTextForEvent(item)) { addEvent(taskList, item); - } else { - throw new KenergeticBotException(); } printLine(); } diff --git a/src/main/java/KenergeticBotException.java b/src/main/java/KenergeticBotException.java deleted file mode 100644 index bf1104721..000000000 --- a/src/main/java/KenergeticBotException.java +++ /dev/null @@ -1,3 +0,0 @@ -public class KenergeticBotException extends Exception { - //no other code needed -} \ No newline at end of file diff --git a/src/main/java/Nuke.java b/src/main/java/Nuke.java deleted file mode 100644 index 0414c8e2c..000000000 --- a/src/main/java/Nuke.java +++ /dev/null @@ -1,171 +0,0 @@ -import java.util.Scanner; -import java.util.ArrayList; - -public class Nuke { - - public static void printGreetingMessage() { - printLine(); - System.out.println(" Hello! I'm Nuke\n" + " What can I do for you?"); - printLine(); - } - - public static void printExitMessage() { - printLine(); - System.out.println(" Bye. Hope to see you again soon!"); - printLine(); - } - - public static void printLine() { - System.out.println(" ____________________________________________________________"); - } - - //Returns True if the text contains "mark", False if not - public static boolean checkTextForMark(String item) { - if (item.length() > 4 && item.startsWith("mark")) { - return true; - } else { - return false; - } - } - - //Returns True if the text contains "unmark", False if not - public static boolean checkTextForUnmark(String item) { - if (item.length() > 6 && item.startsWith("unmark")) { - return true; - } else { - return false; - } - } - - //Returns True if the text contains "todo", False if not - public static boolean checkTextForTodo(String item) { - if (item.length() > 4 && item.startsWith("todo")) { - return true; - } else { - return false; - } - } - - //Returns True if the text contains "deadline", False if not - public static boolean checkTextForDeadline(String item) { - if (item.length() > 8 && item.startsWith("deadline")) { - return true; - } else { - return false; - } - } - - //Returns True if the text contains "event", False if not - public static boolean checkTextForEvent(String item) { - if (item.length() > 5 && item.startsWith("event")) { - return true; - } else { - return false; - } - } - - //Main logic for the bot's dialogue - public static void botDialogue(ArrayList taskList) { - Scanner input = new Scanner(System.in); - String item = input.nextLine(); - if (item.equals("bye")) { - printExitMessage(); - return; - } else if (item.equals("list")) { - list(taskList); - } else if (checkTextForMark(item)) { - String[] splitItem = item.split(" "); - int listIndex = Integer.parseInt(splitItem[1]); - mark(taskList, listIndex); - } else if (checkTextForUnmark(item)) { - String[] splitItem = item.split(" "); - int listIndex = Integer.parseInt(splitItem[1]); - unmark(taskList, listIndex); - } else { - add(taskList, item); - } - botDialogue(taskList); - } - - //Creates a "Todo" object and adds to the taskList - public static void addTodo(ArrayList taskList, String item) { - String formattedString = item.replace("todo ", ""); - String taskType = "[T]"; - Task newTask = new Todo(formattedString, taskType); - taskList.add(newTask); - System.out.println(" Got it. I've added this task:"); - System.out.printf(" %s\n", newTask); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); - } - - //Creates a "Deadline" object and adds to the taskList - public static void addDeadline(ArrayList taskList, String item) { - String formattedString[] = item.replace("deadline ", "").split("/"); - String taskType = "[D]"; - String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; - Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); - taskList.add(newTask); - System.out.println(" Got it. I've added this task:"); - System.out.printf(" %s\n", newTask); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); - } - - //Creates a "Event" object and adds to the taskList - public static void addEvent(ArrayList taskList, String item) { - String formattedString[] = item.replace("event ", "").split("/"); - String taskType = "[E]"; - String eventFrom = formattedString[1].replace("from", "from:"); - String eventTo = formattedString[2].replace("to", "to:"); - String dateTime = "(" + eventFrom + eventTo + ")"; - Task newTask = new Event(formattedString[0], taskType, dateTime); - taskList.add(newTask); - System.out.println(" Got it. I've added this task:"); - System.out.printf(" %s\n", newTask); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); - } - - //Controls the logic for adding items to the list - public static void add(ArrayList taskList, String item) { - printLine(); - if (checkTextForTodo(item)) { - addTodo(taskList, item); - } else if (checkTextForDeadline(item)) { - addDeadline(taskList, item); - } else if (checkTextForEvent(item)) { - addEvent(taskList, item); - } - printLine(); - } - - public static void list(ArrayList taskList) { - printLine(); - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < taskList.size(); i++) { - System.out.printf(" %d.%s\n", i+1, taskList.get(i)); - } - printLine(); - - } - - public static void mark(ArrayList taskList, int listIndex) { - printLine(); - System.out.println(" Nice! I've marked this task as done:"); - taskList.get(listIndex - 1).mark();; - System.out.printf(" %s\n", taskList.get(listIndex - 1)); - printLine(); - } - - public static void unmark(ArrayList taskList, int listIndex) { - printLine(); - System.out.println(" OK, I've marked this task as not done yet:"); - taskList.get(listIndex - 1).unmark(); - System.out.printf(" %s\n", taskList.get(listIndex - 1)); - printLine(); - } - - public static void main(String[] args) { - printGreetingMessage(); - ArrayList taskList = new ArrayList(); - botDialogue(taskList); - } -} diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 4e4722e9c..e69de29bb 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -1,4 +0,0 @@ -todo borrow book -list -deadline return book /by Sunday -event project meeting /from Mon 2pm /to 4pm \ No newline at end of file From 16e8cbf1f574edff702fcda76fee7ccdb0b035a1 Mon Sep 17 00:00:00 2001 From: ZH Date: Thu, 14 Sep 2023 15:02:08 +0800 Subject: [PATCH 14/46] Add Automated Text UI Testing --- src/main/java/KenergeticBot.java | 7 +++--- src/main/java/KenergeticBotException.java | 5 +++- text-ui-test/EXPECTED.TXT | 30 +++++++++++++++++------ text-ui-test/input.txt | 5 ++++ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index 1f0de5ebc..124107fe2 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -85,7 +85,7 @@ public static void botDialogue(ArrayList taskList) { try { add(taskList, item); } catch (KenergeticBotException e) { //exception thrown when user inputs command outside of the usual commands - System.out.println(" OOPS!!! I'm sorry, but I don't know what that means :-("); // unable to print sad face ˙◠˙ + System.out.println(KenergeticBotException.INVALID_COMMAND); // unable to print sad face ˙◠˙ } } botDialogue(taskList); @@ -94,8 +94,7 @@ public static void botDialogue(ArrayList taskList) { //Creates a "Todo" object and adds to the taskList public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { String formattedString = item.replace("todo", "").trim(); - if (formattedString.length() < 1) { - + if (formattedString.isEmpty()) { throw new KenergeticBotException(); } String taskType = "[T]"; @@ -139,7 +138,7 @@ public static void add(ArrayList taskList, String item) throws KenergeticB try { addTodo(taskList, item); } catch (KenergeticBotException e) { //throws exception when the todo command is not followed with a description - System.out.println(" \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."); + System.out.println(KenergeticBotException.MISSING_DESCRIPTION); } } else if (checkTextForDeadline(item)) { addDeadline(taskList, item); diff --git a/src/main/java/KenergeticBotException.java b/src/main/java/KenergeticBotException.java index bf1104721..a6557ea21 100644 --- a/src/main/java/KenergeticBotException.java +++ b/src/main/java/KenergeticBotException.java @@ -1,3 +1,6 @@ public class KenergeticBotException extends Exception { - //no other code needed + public static String INVALID_COMMAND = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; + public static String MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."; + + } \ No newline at end of file diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..9125f4cab 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,23 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| - + ____________________________________________________________ + Hello! I'm KenergeticBot + What can I do for you? + ____________________________________________________________ + ____________________________________________________________ + Got it. I've added this task: + [T][ ] borrow book + Now you have 1 tasks in the list. + ____________________________________________________________ + ____________________________________________________________ + Here are the tasks in your list: + 1.[T][ ] borrow book + ____________________________________________________________ + ____________________________________________________________ + Got it. I've added this task: + [D][ ] return book (by: Sunday) + Now you have 2 tasks in the list. + ____________________________________________________________ + ____________________________________________________________ + Got it. I've added this task: + [E][ ] project meeting (from: Mon 2pm to: 4pm) + Now you have 3 tasks in the list. + ____________________________________________________________ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..30b2c284e 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,5 @@ +todo borrow book +list +deadline return book /by Sunday +event project meeting /from Mon 2pm /to 4pm +bye \ No newline at end of file From 5cfdcb59ce5ff585cdb1e65e0baeea7333bea00a Mon Sep 17 00:00:00 2001 From: ZH Date: Sat, 16 Sep 2023 00:19:35 +0800 Subject: [PATCH 15/46] Update Exception Handler to cover more exceptions --- src/main/java/KenergeticBot.java | 89 ++++++++++++----------- src/main/java/KenergeticBotException.java | 11 ++- 2 files changed, 55 insertions(+), 45 deletions(-) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index 124107fe2..7183e1e5d 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -3,9 +3,15 @@ public class KenergeticBot { + public static void main(String[] args) { + printGreetingMessage(); + ArrayList taskList = new ArrayList(); + botDialogue(taskList); + } public static void printGreetingMessage() { printLine(); - System.out.println(" Hello! I'm KenergeticBot\n" + " What can I do for you?"); + System.out.println(" Hello! I'm KenergeticBot"); + System.out.println(" What can I do for you?"); printLine(); } @@ -21,47 +27,27 @@ public static void printLine() { //Returns True if the text contains "mark", False if not public static boolean checkTextForMark(String item) { - if (item.length() > 4 && item.startsWith("mark")) { - return true; - } else { - return false; - } + return item.length() > 4 && item.startsWith("mark"); } //Returns True if the text contains "unmark", False if not public static boolean checkTextForUnmark(String item) { - if (item.length() > 6 && item.startsWith("unmark")) { - return true; - } else { - return false; - } + return item.length() > 6 && item.startsWith("unmark"); } //Returns True if the text contains "todo", False if not public static boolean checkTextForTodo(String item) { - if (item.length() > 3 && item.startsWith("todo")) { - return true; - } else { - return false; - } + return item.startsWith("todo"); } //Returns True if the text contains "deadline", False if not public static boolean checkTextForDeadline(String item) { - if (item.length() > 7 && item.startsWith("deadline")) { - return true; - } else { - return false; - } + return item.startsWith("deadline"); } //Returns True if the text contains "event", False if not public static boolean checkTextForEvent(String item) { - if (item.length() > 4 && item.startsWith("event")) { - return true; - } else { - return false; - } + return item.startsWith("event"); } //Main logic for the bot's dialogue @@ -85,7 +71,7 @@ public static void botDialogue(ArrayList taskList) { try { add(taskList, item); } catch (KenergeticBotException e) { //exception thrown when user inputs command outside of the usual commands - System.out.println(KenergeticBotException.INVALID_COMMAND); // unable to print sad face ˙◠˙ + System.out.println(e.getMessage()); // unable to print sad face ˙◠˙ } } botDialogue(taskList); @@ -95,7 +81,7 @@ public static void botDialogue(ArrayList taskList) { public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { String formattedString = item.replace("todo", "").trim(); if (formattedString.isEmpty()) { - throw new KenergeticBotException(); + throw new KenergeticBotException(KenergeticBotException.TODO_MISSING_DESCRIPTION); } String taskType = "[T]"; Task newTask = new Todo(formattedString, taskType); @@ -106,8 +92,13 @@ public static void addTodo(ArrayList taskList, String item) throws Kenerge } //Creates a "Deadline" object and adds to the taskList - public static void addDeadline(ArrayList taskList, String item) { - String formattedString[] = item.replace("deadline", "").trim().split("/"); + public static void addDeadline(ArrayList taskList, String item) throws KenergeticBotException { + String[] formattedString = item.replace("deadline", "").trim().split("/"); + if (formattedString.length < 1) { + throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DESCRIPTION); + } else if (formattedString.length < 2 || formattedString[1].replace("by", "").isEmpty()) { //|| formattedString[1].replace("by ", "") + throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DATE); + } String taskType = "[D]"; String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); @@ -118,8 +109,18 @@ public static void addDeadline(ArrayList taskList, String item) { } //Creates a "Event" object and adds to the taskList - public static void addEvent(ArrayList taskList, String item) { - String formattedString[] = item.replace("event", "").split("/"); + public static void addEvent(ArrayList taskList, String item) throws KenergeticBotException { + String[] formattedString = item.replace("event", "").split("/"); + if (item.replace("event", "").isEmpty()) { + throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_DESCRIPTION); + } + if (formattedString.length < 1) { + throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_DESCRIPTION); + } else if (formattedString.length < 2 || formattedString[1].replace("from", "").isEmpty()) { + throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_START); + } else if (formattedString.length < 3 || formattedString[2].replace("to", "").isEmpty()) { + throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_END); + } String taskType = "[E]"; String eventFrom = formattedString[1].replace("from", "from:"); String eventTo = formattedString[2].replace("to", "to:"); @@ -138,14 +139,22 @@ public static void add(ArrayList taskList, String item) throws KenergeticB try { addTodo(taskList, item); } catch (KenergeticBotException e) { //throws exception when the todo command is not followed with a description - System.out.println(KenergeticBotException.MISSING_DESCRIPTION); + System.out.println(e.getMessage()); } } else if (checkTextForDeadline(item)) { - addDeadline(taskList, item); + try { + addDeadline(taskList, item); + } catch (KenergeticBotException e) { //throws exception when the deadline command is not followed with a description + System.out.println(e.getMessage()); + } } else if (checkTextForEvent(item)) { - addEvent(taskList, item); + try { + addEvent(taskList, item);; + } catch (KenergeticBotException e) { //throws exception when the event command is not followed with a description + System.out.println(e.getMessage()); + } } else { - throw new KenergeticBotException(); + throw new KenergeticBotException(KenergeticBotException.INVALID_COMMAND); } printLine(); } @@ -162,7 +171,7 @@ public static void list(ArrayList taskList) { public static void mark(ArrayList taskList, int listIndex) { printLine(); System.out.println(" Nice! I've marked this task as done:"); - taskList.get(listIndex - 1).mark();; + taskList.get(listIndex - 1).mark(); System.out.printf(" %s\n", taskList.get(listIndex - 1)); printLine(); } @@ -174,10 +183,4 @@ public static void unmark(ArrayList taskList, int listIndex) { System.out.printf(" %s\n", taskList.get(listIndex - 1)); printLine(); } - - public static void main(String[] args) { - printGreetingMessage(); - ArrayList taskList = new ArrayList(); - botDialogue(taskList); - } } diff --git a/src/main/java/KenergeticBotException.java b/src/main/java/KenergeticBotException.java index a6557ea21..3ab5a00c7 100644 --- a/src/main/java/KenergeticBotException.java +++ b/src/main/java/KenergeticBotException.java @@ -1,6 +1,13 @@ public class KenergeticBotException extends Exception { public static String INVALID_COMMAND = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; - public static String MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."; - + public static String TODO_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."; + public static String DEADLINE_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a deadline cannot be empty."; + public static String DEADLINE_MISSING_DATE = " \uD83D\uDCC5 OOPS!!! The date of a deadline cannot be empty."; + public static String EVENT_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a event cannot be empty."; + public static String EVENT_MISSING_START = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty."; + public static String EVENT_MISSING_END = " ⏳ OOPS!!! The end time of a event cannot be empty."; + public KenergeticBotException(String errMSG) { + super(errMSG); + } } \ No newline at end of file From 8ff14d8ba3a9c841f1d73f7a66524d98e609866b Mon Sep 17 00:00:00 2001 From: ZH Date: Sat, 16 Sep 2023 01:02:40 +0800 Subject: [PATCH 16/46] Organise files into Packages --- src/main/java/KenergeticBot.java | 86 ++++--------------- src/main/java/command/booleanChecks.java | 28 ++++++ src/main/java/command/commandList.java | 34 ++++++++ src/main/java/command/messages.java | 19 ++++ .../KenergeticBotException.java | 2 + src/main/java/{ => task}/Deadline.java | 6 +- src/main/java/{ => task}/Event.java | 6 +- src/main/java/{ => task}/Task.java | 1 + src/main/java/{ => task}/Todo.java | 2 + 9 files changed, 113 insertions(+), 71 deletions(-) create mode 100644 src/main/java/command/booleanChecks.java create mode 100644 src/main/java/command/commandList.java create mode 100644 src/main/java/command/messages.java rename src/main/java/{ => exceptionhandler}/KenergeticBotException.java (97%) rename src/main/java/{ => task}/Deadline.java (82%) rename src/main/java/{ => task}/Event.java (83%) rename src/main/java/{ => task}/Task.java (98%) rename src/main/java/{ => task}/Todo.java (93%) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index 7183e1e5d..a57889183 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -1,6 +1,20 @@ +import task.Deadline; +import task.Event; +import task.Task; +import task.Todo; +import exceptionhandler.KenergeticBotException; + import java.util.Scanner; import java.util.ArrayList; +import static command.booleanChecks.*; +import static command.commandList.*; +import static command.messages.*; +/*import static command.messages.printLine; +import static command.messages.printGreetingMessage; +import static command.messages.printExitMessage;*/ + + public class KenergeticBot { public static void main(String[] args) { @@ -8,47 +22,6 @@ public static void main(String[] args) { ArrayList taskList = new ArrayList(); botDialogue(taskList); } - public static void printGreetingMessage() { - printLine(); - System.out.println(" Hello! I'm KenergeticBot"); - System.out.println(" What can I do for you?"); - printLine(); - } - - public static void printExitMessage() { - printLine(); - System.out.println(" Bye. Hope to see you again soon!"); - printLine(); - } - - public static void printLine() { - System.out.println(" ____________________________________________________________"); - } - - //Returns True if the text contains "mark", False if not - public static boolean checkTextForMark(String item) { - return item.length() > 4 && item.startsWith("mark"); - } - - //Returns True if the text contains "unmark", False if not - public static boolean checkTextForUnmark(String item) { - return item.length() > 6 && item.startsWith("unmark"); - } - - //Returns True if the text contains "todo", False if not - public static boolean checkTextForTodo(String item) { - return item.startsWith("todo"); - } - - //Returns True if the text contains "deadline", False if not - public static boolean checkTextForDeadline(String item) { - return item.startsWith("deadline"); - } - - //Returns True if the text contains "event", False if not - public static boolean checkTextForEvent(String item) { - return item.startsWith("event"); - } //Main logic for the bot's dialogue public static void botDialogue(ArrayList taskList) { @@ -77,7 +50,7 @@ public static void botDialogue(ArrayList taskList) { botDialogue(taskList); } - //Creates a "Todo" object and adds to the taskList + //Creates a "task.Todo" object and adds to the taskList public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { String formattedString = item.replace("todo", "").trim(); if (formattedString.isEmpty()) { @@ -91,7 +64,7 @@ public static void addTodo(ArrayList taskList, String item) throws Kenerge System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); } - //Creates a "Deadline" object and adds to the taskList + //Creates a "task.Deadline" object and adds to the taskList public static void addDeadline(ArrayList taskList, String item) throws KenergeticBotException { String[] formattedString = item.replace("deadline", "").trim().split("/"); if (formattedString.length < 1) { @@ -108,7 +81,7 @@ public static void addDeadline(ArrayList taskList, String item) throws Ken System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); } - //Creates a "Event" object and adds to the taskList + //Creates a "task.Event" object and adds to the taskList public static void addEvent(ArrayList taskList, String item) throws KenergeticBotException { String[] formattedString = item.replace("event", "").split("/"); if (item.replace("event", "").isEmpty()) { @@ -158,29 +131,4 @@ public static void add(ArrayList taskList, String item) throws KenergeticB } printLine(); } - - public static void list(ArrayList taskList) { - printLine(); - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < taskList.size(); i++) { - System.out.printf(" %d.%s\n", i+1, taskList.get(i)); - } - printLine(); - } - - public static void mark(ArrayList taskList, int listIndex) { - printLine(); - System.out.println(" Nice! I've marked this task as done:"); - taskList.get(listIndex - 1).mark(); - System.out.printf(" %s\n", taskList.get(listIndex - 1)); - printLine(); - } - - public static void unmark(ArrayList taskList, int listIndex) { - printLine(); - System.out.println(" OK, I've marked this task as not done yet:"); - taskList.get(listIndex - 1).unmark(); - System.out.printf(" %s\n", taskList.get(listIndex - 1)); - printLine(); - } } diff --git a/src/main/java/command/booleanChecks.java b/src/main/java/command/booleanChecks.java new file mode 100644 index 000000000..641cf20cf --- /dev/null +++ b/src/main/java/command/booleanChecks.java @@ -0,0 +1,28 @@ +package command; + +public class booleanChecks { + //Returns True if the text contains "mark", False if not + public static boolean checkTextForMark(String item) { + return item.length() > 4 && item.startsWith("mark"); + } + + //Returns True if the text contains "unmark", False if not + public static boolean checkTextForUnmark(String item) { + return item.length() > 6 && item.startsWith("unmark"); + } + + //Returns True if the text contains "todo", False if not + public static boolean checkTextForTodo(String item) { + return item.startsWith("todo"); + } + + //Returns True if the text contains "deadline", False if not + public static boolean checkTextForDeadline(String item) { + return item.startsWith("deadline"); + } + + //Returns True if the text contains "event", False if not + public static boolean checkTextForEvent(String item) { + return item.startsWith("event"); + } +} diff --git a/src/main/java/command/commandList.java b/src/main/java/command/commandList.java new file mode 100644 index 000000000..3c22e9bc6 --- /dev/null +++ b/src/main/java/command/commandList.java @@ -0,0 +1,34 @@ +package command; + +import task.Task; + +import java.util.ArrayList; + +import static command.messages.printLine; + +public class commandList { + public static void list(ArrayList taskList) { + printLine(); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < taskList.size(); i++) { + System.out.printf(" %d.%s\n", i+1, taskList.get(i)); + } + printLine(); + } + + public static void mark(ArrayList taskList, int listIndex) { + printLine(); + System.out.println(" Nice! I've marked this task as done:"); + taskList.get(listIndex - 1).mark(); + System.out.printf(" %s\n", taskList.get(listIndex - 1)); + printLine(); + } + + public static void unmark(ArrayList taskList, int listIndex) { + printLine(); + System.out.println(" OK, I've marked this task as not done yet:"); + taskList.get(listIndex - 1).unmark(); + System.out.printf(" %s\n", taskList.get(listIndex - 1)); + printLine(); + } +} diff --git a/src/main/java/command/messages.java b/src/main/java/command/messages.java new file mode 100644 index 000000000..a120b472b --- /dev/null +++ b/src/main/java/command/messages.java @@ -0,0 +1,19 @@ +package command; + +public class messages { + public static void printLine() { + System.out.println(" ____________________________________________________________"); + } + public static void printGreetingMessage() { + printLine(); + System.out.println(" Hello! I'm KenergeticBot"); + System.out.println(" What can I do for you?"); + printLine(); + } + + public static void printExitMessage() { + printLine(); + System.out.println(" Bye. Hope to see you again soon!"); + printLine(); + } +} diff --git a/src/main/java/KenergeticBotException.java b/src/main/java/exceptionhandler/KenergeticBotException.java similarity index 97% rename from src/main/java/KenergeticBotException.java rename to src/main/java/exceptionhandler/KenergeticBotException.java index 3ab5a00c7..db7f4e80a 100644 --- a/src/main/java/KenergeticBotException.java +++ b/src/main/java/exceptionhandler/KenergeticBotException.java @@ -1,3 +1,5 @@ +package exceptionhandler; + public class KenergeticBotException extends Exception { public static String INVALID_COMMAND = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; public static String TODO_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."; diff --git a/src/main/java/Deadline.java b/src/main/java/task/Deadline.java similarity index 82% rename from src/main/java/Deadline.java rename to src/main/java/task/Deadline.java index 0c019960a..00aadcda7 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -1,4 +1,8 @@ -public class Deadline extends Task{ +package task; + +import task.Task; + +public class Deadline extends Task { protected String deadline; public Deadline(String description, String taskType, String deadline) { super(description, taskType); diff --git a/src/main/java/Event.java b/src/main/java/task/Event.java similarity index 83% rename from src/main/java/Event.java rename to src/main/java/task/Event.java index 67e186791..2a9baefda 100644 --- a/src/main/java/Event.java +++ b/src/main/java/task/Event.java @@ -1,4 +1,8 @@ -public class Event extends Task{ +package task; + +import task.Task; + +public class Event extends Task { protected String dateTime; public Event(String description, String taskType, String dateTime) { super(description, taskType); diff --git a/src/main/java/Task.java b/src/main/java/task/Task.java similarity index 98% rename from src/main/java/Task.java rename to src/main/java/task/Task.java index c3acc76e0..4236d69c3 100644 --- a/src/main/java/Task.java +++ b/src/main/java/task/Task.java @@ -1,3 +1,4 @@ +package task; public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/Todo.java b/src/main/java/task/Todo.java similarity index 93% rename from src/main/java/Todo.java rename to src/main/java/task/Todo.java index 01119fc9b..0919da656 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/task/Todo.java @@ -1,3 +1,5 @@ +package task; + public class Todo extends Task{ public Todo(String description, String taskType) { super(description, taskType); From 335ff3f3f7bd9e6f59f3472d1858e7a1b14f4312 Mon Sep 17 00:00:00 2001 From: ZH Date: Sat, 16 Sep 2023 02:48:27 +0800 Subject: [PATCH 17/46] Update Organization of Package --- src/main/java/KenergeticBot.java | 89 +-------------- src/main/java/command/commandList.java | 105 +++++++++++++++++- .../{messages.java => commonMessages.java} | 12 +- .../KenergeticBotException.java | 7 ++ 4 files changed, 123 insertions(+), 90 deletions(-) rename src/main/java/command/{messages.java => commonMessages.java} (58%) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index a57889183..1d86ab3ea 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -1,7 +1,4 @@ -import task.Deadline; -import task.Event; import task.Task; -import task.Todo; import exceptionhandler.KenergeticBotException; import java.util.Scanner; @@ -9,11 +6,7 @@ import static command.booleanChecks.*; import static command.commandList.*; -import static command.messages.*; -/*import static command.messages.printLine; -import static command.messages.printGreetingMessage; -import static command.messages.printExitMessage;*/ - +import static command.commonMessages.*; public class KenergeticBot { @@ -50,85 +43,5 @@ public static void botDialogue(ArrayList taskList) { botDialogue(taskList); } - //Creates a "task.Todo" object and adds to the taskList - public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { - String formattedString = item.replace("todo", "").trim(); - if (formattedString.isEmpty()) { - throw new KenergeticBotException(KenergeticBotException.TODO_MISSING_DESCRIPTION); - } - String taskType = "[T]"; - Task newTask = new Todo(formattedString, taskType); - taskList.add(newTask); - System.out.println(" Got it. I've added this task:"); - System.out.printf(" %s\n", newTask); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); - } - - //Creates a "task.Deadline" object and adds to the taskList - public static void addDeadline(ArrayList taskList, String item) throws KenergeticBotException { - String[] formattedString = item.replace("deadline", "").trim().split("/"); - if (formattedString.length < 1) { - throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DESCRIPTION); - } else if (formattedString.length < 2 || formattedString[1].replace("by", "").isEmpty()) { //|| formattedString[1].replace("by ", "") - throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DATE); - } - String taskType = "[D]"; - String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; - Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); - taskList.add(newTask); - System.out.println(" Got it. I've added this task:"); - System.out.printf(" %s\n", newTask); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); - } - - //Creates a "task.Event" object and adds to the taskList - public static void addEvent(ArrayList taskList, String item) throws KenergeticBotException { - String[] formattedString = item.replace("event", "").split("/"); - if (item.replace("event", "").isEmpty()) { - throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_DESCRIPTION); - } - if (formattedString.length < 1) { - throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_DESCRIPTION); - } else if (formattedString.length < 2 || formattedString[1].replace("from", "").isEmpty()) { - throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_START); - } else if (formattedString.length < 3 || formattedString[2].replace("to", "").isEmpty()) { - throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_END); - } - String taskType = "[E]"; - String eventFrom = formattedString[1].replace("from", "from:"); - String eventTo = formattedString[2].replace("to", "to:"); - String dateTime = "(" + eventFrom + eventTo + ")"; - Task newTask = new Event(formattedString[0], taskType, dateTime); - taskList.add(newTask); - System.out.println(" Got it. I've added this task:"); - System.out.printf(" %s\n", newTask); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); - } - //Controls the logic for adding items to the list - public static void add(ArrayList taskList, String item) throws KenergeticBotException { - printLine(); - if (checkTextForTodo(item)) { - try { - addTodo(taskList, item); - } catch (KenergeticBotException e) { //throws exception when the todo command is not followed with a description - System.out.println(e.getMessage()); - } - } else if (checkTextForDeadline(item)) { - try { - addDeadline(taskList, item); - } catch (KenergeticBotException e) { //throws exception when the deadline command is not followed with a description - System.out.println(e.getMessage()); - } - } else if (checkTextForEvent(item)) { - try { - addEvent(taskList, item);; - } catch (KenergeticBotException e) { //throws exception when the event command is not followed with a description - System.out.println(e.getMessage()); - } - } else { - throw new KenergeticBotException(KenergeticBotException.INVALID_COMMAND); - } - printLine(); - } } diff --git a/src/main/java/command/commandList.java b/src/main/java/command/commandList.java index 3c22e9bc6..5955d45ef 100644 --- a/src/main/java/command/commandList.java +++ b/src/main/java/command/commandList.java @@ -1,10 +1,16 @@ package command; +import exceptionhandler.KenergeticBotException; +import task.Deadline; +import task.Event; import task.Task; +import task.Todo; import java.util.ArrayList; -import static command.messages.printLine; +import static command.booleanChecks.*; +import static command.commonMessages.printAddedTaskMessage; +import static command.commonMessages.printLine; public class commandList { public static void list(ArrayList taskList) { @@ -31,4 +37,101 @@ public static void unmark(ArrayList taskList, int listIndex) { System.out.printf(" %s\n", taskList.get(listIndex - 1)); printLine(); } + + //Creates a "task.Todo" object and adds to the taskList + public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { + String formattedString = item.replace("todo", "").trim(); + if (formattedString.isEmpty()) { + throw new KenergeticBotException(KenergeticBotException.TODO_MISSING_DESCRIPTION); + } + String taskType = "[T]"; + Task newTask = new Todo(formattedString, taskType); + taskList.add(newTask); + printAddedTaskMessage(taskList, newTask); + } + + //Creates a "task.Deadline" object and adds to the taskList + public static void addDeadline(ArrayList taskList, String item) throws KenergeticBotException { + String[] formattedString = item.replace("deadline", "").trim().split("/"); + switch (formattedString.length) { + case 1: + if (formattedString[0].isEmpty()) { + throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DESCRIPTION); + } else { + throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DATE_INTERMEDIATE); + } + case 2: + if (!formattedString[1].contains("by")) { + throw new KenergeticBotException(KenergeticBotException.COMMAND_TYPO_DEADLINE_BY); + } else if (formattedString[1].replace("by", "").trim().isEmpty()) { + throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DATE); + } + } + String taskType = "[D]"; + String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; + Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); + taskList.add(newTask); + printAddedTaskMessage(taskList, newTask); + } + + //Creates a "task.Event" object and adds to the taskList + public static void addEvent(ArrayList taskList, String item) throws KenergeticBotException { + String[] formattedString = item.replace("event", "").trim().split("/"); + switch(formattedString.length) { + case 1: + if (formattedString[0].isEmpty()) { + throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_DESCRIPTION); + } else { + throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_START_INTERMEDIATE); + } + case 2: + if (!formattedString[1].contains("from")) { + throw new KenergeticBotException(KenergeticBotException.COMMAND_TYPO_EVENT_FROM); + } else if (formattedString[1].replace("from", "").trim().isEmpty()){ + throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_START); + } else { + throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_END_INTERMEDIATE); + } + case 3: + if (!formattedString[2].contains("to")) { + throw new KenergeticBotException(KenergeticBotException.COMMAND_TYPO_EVENT_TO); + } else if (formattedString[2].replace("to", "").trim().isEmpty()){ + throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_END); + } + } + String taskType = "[E]"; + String eventFrom = formattedString[1].replace("from", "from:"); + String eventTo = formattedString[2].replace("to", "to:"); + String dateTime = "(" + eventFrom + eventTo + ")"; + Task newTask = new Event(formattedString[0], taskType, dateTime); + taskList.add(newTask); + printAddedTaskMessage(taskList, newTask); + } + + //Controls the logic for adding items to the list + public static void add(ArrayList taskList, String item) throws KenergeticBotException { + printLine(); + if (checkTextForTodo(item)) { + try { + addTodo(taskList, item); + } catch (KenergeticBotException e) { //throws exception when the todo command is not followed with a description + System.out.println(e.getMessage()); + } + } else if (checkTextForDeadline(item)) { + try { + addDeadline(taskList, item); + } catch (KenergeticBotException e) { //throws exception when the deadline command is not followed with a description + System.out.println(e.getMessage()); + } + } else if (checkTextForEvent(item)) { + try { + addEvent(taskList, item);; + } catch (KenergeticBotException e) { //throws exception when the event command is not followed with a description + System.out.println(e.getMessage()); + } + } else { + throw new KenergeticBotException(KenergeticBotException.INVALID_COMMAND); + } + printLine(); + } } diff --git a/src/main/java/command/messages.java b/src/main/java/command/commonMessages.java similarity index 58% rename from src/main/java/command/messages.java rename to src/main/java/command/commonMessages.java index a120b472b..04d5bbb1f 100644 --- a/src/main/java/command/messages.java +++ b/src/main/java/command/commonMessages.java @@ -1,6 +1,10 @@ package command; -public class messages { +import task.Task; + +import java.util.ArrayList; + +public class commonMessages { public static void printLine() { System.out.println(" ____________________________________________________________"); } @@ -16,4 +20,10 @@ public static void printExitMessage() { System.out.println(" Bye. Hope to see you again soon!"); printLine(); } + + public static void printAddedTaskMessage(ArrayList taskList, Task newTask) { + System.out.println(" Got it. I've added this task:"); + System.out.printf(" %s\n", newTask); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); + } } diff --git a/src/main/java/exceptionhandler/KenergeticBotException.java b/src/main/java/exceptionhandler/KenergeticBotException.java index db7f4e80a..13f7cdab3 100644 --- a/src/main/java/exceptionhandler/KenergeticBotException.java +++ b/src/main/java/exceptionhandler/KenergeticBotException.java @@ -5,9 +5,16 @@ public class KenergeticBotException extends Exception { public static String TODO_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."; public static String DEADLINE_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a deadline cannot be empty."; public static String DEADLINE_MISSING_DATE = " \uD83D\uDCC5 OOPS!!! The date of a deadline cannot be empty."; + public static String DEADLINE_MISSING_DATE_INTERMEDIATE = " \uD83D\uDCC5 OOPS!!! The date of a deadline cannot be empty. Please use the command /by"; public static String EVENT_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a event cannot be empty."; public static String EVENT_MISSING_START = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty."; public static String EVENT_MISSING_END = " ⏳ OOPS!!! The end time of a event cannot be empty."; + public static String EVENT_MISSING_START_INTERMEDIATE = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty. Please use the command /from"; + public static String EVENT_MISSING_END_INTERMEDIATE = " ⏳ OOPS!!! The end time of a event cannot be empty. Please use the command /to"; + public static String COMMAND_TYPO = " ¯\\_(ツ)_/¯ OOPS!!! There may be a typo in the command used."; + public static String COMMAND_TYPO_DEADLINE_BY = COMMAND_TYPO + " Please use the command /by"; + public static String COMMAND_TYPO_EVENT_FROM = COMMAND_TYPO + " Please use the command /from"; + public static String COMMAND_TYPO_EVENT_TO = COMMAND_TYPO + " Please use the command /to"; public KenergeticBotException(String errMSG) { super(errMSG); From 578c87e043b7956a425c283e9db2210628045c20 Mon Sep 17 00:00:00 2001 From: ZH Date: Sat, 16 Sep 2023 03:31:06 +0800 Subject: [PATCH 18/46] Update Error handling code - clean up --- src/main/java/command/commandList.java | 20 +++++++------ .../KenergeticBotException.java | 28 +++++++++---------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/main/java/command/commandList.java b/src/main/java/command/commandList.java index 5955d45ef..256737fc3 100644 --- a/src/main/java/command/commandList.java +++ b/src/main/java/command/commandList.java @@ -11,6 +11,8 @@ import static command.booleanChecks.*; import static command.commonMessages.printAddedTaskMessage; import static command.commonMessages.printLine; +import static exceptionhandler.KenergeticBotException.*; + public class commandList { public static void list(ArrayList taskList) { @@ -42,7 +44,7 @@ public static void unmark(ArrayList taskList, int listIndex) { public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { String formattedString = item.replace("todo", "").trim(); if (formattedString.isEmpty()) { - throw new KenergeticBotException(KenergeticBotException.TODO_MISSING_DESCRIPTION); + throw new KenergeticBotException(TODO_MISSING_DESCRIPTION); } String taskType = "[T]"; Task newTask = new Todo(formattedString, taskType); @@ -80,23 +82,23 @@ public static void addEvent(ArrayList taskList, String item) throws Kenerg switch(formattedString.length) { case 1: if (formattedString[0].isEmpty()) { - throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_DESCRIPTION); + throw new KenergeticBotException(EVENT_MISSING_DESCRIPTION); } else { - throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_START_INTERMEDIATE); + throw new KenergeticBotException(EVENT_MISSING_START_INTERMEDIATE); } case 2: if (!formattedString[1].contains("from")) { - throw new KenergeticBotException(KenergeticBotException.COMMAND_TYPO_EVENT_FROM); + throw new KenergeticBotException(COMMAND_TYPO_EVENT_FROM); } else if (formattedString[1].replace("from", "").trim().isEmpty()){ - throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_START); + throw new KenergeticBotException(EVENT_MISSING_START); } else { - throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_END_INTERMEDIATE); + throw new KenergeticBotException(EVENT_MISSING_END_INTERMEDIATE); } case 3: if (!formattedString[2].contains("to")) { - throw new KenergeticBotException(KenergeticBotException.COMMAND_TYPO_EVENT_TO); + throw new KenergeticBotException(COMMAND_TYPO_EVENT_TO); } else if (formattedString[2].replace("to", "").trim().isEmpty()){ - throw new KenergeticBotException(KenergeticBotException.EVENT_MISSING_END); + throw new KenergeticBotException(EVENT_MISSING_END); } } String taskType = "[E]"; @@ -130,7 +132,7 @@ public static void add(ArrayList taskList, String item) throws KenergeticB System.out.println(e.getMessage()); } } else { - throw new KenergeticBotException(KenergeticBotException.INVALID_COMMAND); + throw new KenergeticBotException(INVALID_COMMAND); } printLine(); } diff --git a/src/main/java/exceptionhandler/KenergeticBotException.java b/src/main/java/exceptionhandler/KenergeticBotException.java index 13f7cdab3..4519ca3ab 100644 --- a/src/main/java/exceptionhandler/KenergeticBotException.java +++ b/src/main/java/exceptionhandler/KenergeticBotException.java @@ -1,20 +1,20 @@ package exceptionhandler; public class KenergeticBotException extends Exception { - public static String INVALID_COMMAND = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; - public static String TODO_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."; - public static String DEADLINE_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a deadline cannot be empty."; - public static String DEADLINE_MISSING_DATE = " \uD83D\uDCC5 OOPS!!! The date of a deadline cannot be empty."; - public static String DEADLINE_MISSING_DATE_INTERMEDIATE = " \uD83D\uDCC5 OOPS!!! The date of a deadline cannot be empty. Please use the command /by"; - public static String EVENT_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a event cannot be empty."; - public static String EVENT_MISSING_START = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty."; - public static String EVENT_MISSING_END = " ⏳ OOPS!!! The end time of a event cannot be empty."; - public static String EVENT_MISSING_START_INTERMEDIATE = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty. Please use the command /from"; - public static String EVENT_MISSING_END_INTERMEDIATE = " ⏳ OOPS!!! The end time of a event cannot be empty. Please use the command /to"; - public static String COMMAND_TYPO = " ¯\\_(ツ)_/¯ OOPS!!! There may be a typo in the command used."; - public static String COMMAND_TYPO_DEADLINE_BY = COMMAND_TYPO + " Please use the command /by"; - public static String COMMAND_TYPO_EVENT_FROM = COMMAND_TYPO + " Please use the command /from"; - public static String COMMAND_TYPO_EVENT_TO = COMMAND_TYPO + " Please use the command /to"; + public static final String INVALID_COMMAND = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; + public static final String TODO_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."; + public static final String DEADLINE_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a deadline cannot be empty."; + public static final String DEADLINE_MISSING_DATE = " \uD83D\uDCC5 OOPS!!! The date of a deadline cannot be empty."; + public static final String DEADLINE_MISSING_DATE_INTERMEDIATE = " \uD83D\uDCC5 OOPS!!! The date of a deadline cannot be empty. Please use the command /by"; + public static final String EVENT_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a event cannot be empty."; + public static final String EVENT_MISSING_START = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty."; + public static final String EVENT_MISSING_END = " ⏳ OOPS!!! The end time of a event cannot be empty."; + public static final String EVENT_MISSING_START_INTERMEDIATE = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty. Please use the command /from"; + public static final String EVENT_MISSING_END_INTERMEDIATE = " ⏳ OOPS!!! The end time of a event cannot be empty. Please use the command /to"; + public static final String COMMAND_TYPO = " ¯\\_(ツ)_/¯ OOPS!!! There may be a typo in the command used."; + public static final String COMMAND_TYPO_DEADLINE_BY = COMMAND_TYPO + " Please use the command /by"; + public static final String COMMAND_TYPO_EVENT_FROM = COMMAND_TYPO + " Please use the command /from"; + public static final String COMMAND_TYPO_EVENT_TO = COMMAND_TYPO + " Please use the command /to"; public KenergeticBotException(String errMSG) { super(errMSG); From ca8c8e37f97acf87cbc8afd5f9b8c4ef2312e9d6 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Sat, 16 Sep 2023 23:10:15 +0800 Subject: [PATCH 19/46] Update Automated Text UI Testing --- src/main/java/KenergeticBot.java | 40 +++++++++---------- text-ui-test/EXPECTED.TXT | 67 +++++++++++++++++++++++++++++++- text-ui-test/input.txt | 18 +++++++++ 3 files changed, 103 insertions(+), 22 deletions(-) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index 1d86ab3ea..a60c3f470 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -20,28 +20,26 @@ public static void main(String[] args) { public static void botDialogue(ArrayList taskList) { Scanner input = new Scanner(System.in); String item = input.nextLine(); - if (item.equals("bye")) { - printExitMessage(); - return; - } else if (item.equals("list")) { - list(taskList); - } else if (checkTextForMark(item)) { - String[] splitItem = item.split(" "); - int listIndex = Integer.parseInt(splitItem[1]); - mark(taskList, listIndex); - } else if (checkTextForUnmark(item)) { - String[] splitItem = item.split(" "); - int listIndex = Integer.parseInt(splitItem[1]); - unmark(taskList, listIndex); - } else { - try { - add(taskList, item); - } catch (KenergeticBotException e) { //exception thrown when user inputs command outside of the usual commands - System.out.println(e.getMessage()); // unable to print sad face ˙◠˙ + while(!item.equals("bye")) { + if (item.equals("list")) { + list(taskList); + } else if (checkTextForMark(item)) { + String[] splitItem = item.split(" "); + int listIndex = Integer.parseInt(splitItem[1]); + mark(taskList, listIndex); + } else if (checkTextForUnmark(item)) { + String[] splitItem = item.split(" "); + int listIndex = Integer.parseInt(splitItem[1]); + unmark(taskList, listIndex); + } else { + try { + add(taskList, item); + } catch (KenergeticBotException e) { //exception thrown when user inputs command outside of the usual commands + System.out.println(e.getMessage()); + } } + item = input.nextLine(); } - botDialogue(taskList); + printExitMessage(); } - - } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 9125f4cab..fce942bee 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -18,6 +18,71 @@ ____________________________________________________________ ____________________________________________________________ Got it. I've added this task: - [E][ ] project meeting (from: Mon 2pm to: 4pm) + [E][ ] project meeting (from: Mon 2pm to: 4pm) Now you have 3 tasks in the list. ____________________________________________________________ + ____________________________________________________________ + Nice! I've marked this task as done: + [D][X] return book (by: Sunday) + ____________________________________________________________ + ____________________________________________________________ + Here are the tasks in your list: + 1.[T][ ] borrow book + 2.[D][X] return book (by: Sunday) + 3.[E][ ] project meeting (from: Mon 2pm to: 4pm) + ____________________________________________________________ + ____________________________________________________________ + OK, I've marked this task as not done yet: + [D][ ] return book (by: Sunday) + ____________________________________________________________ + ____________________________________________________________ + Here are the tasks in your list: + 1.[T][ ] borrow book + 2.[D][ ] return book (by: Sunday) + 3.[E][ ] project meeting (from: Mon 2pm to: 4pm) + ____________________________________________________________ + ____________________________________________________________ + ??? OOPS!!! The description of a todo cannot be empty. + ____________________________________________________________ + ____________________________________________________________ + ? OOPS!!! The date of a deadline cannot be empty. + ____________________________________________________________ + ____________________________________________________________ + �\_(?)_/� OOPS!!! There may be a typo in the command used. Please use the command /by + ____________________________________________________________ + ____________________________________________________________ + ? OOPS!!! The date of a deadline cannot be empty. Please use the command /by + ____________________________________________________________ + ____________________________________________________________ + ? OOPS!!! The date of a deadline cannot be empty. Please use the command /by + ____________________________________________________________ + ____________________________________________________________ + ??? OOPS!!! The description of a deadline cannot be empty. + ____________________________________________________________ + ____________________________________________________________ + ? OOPS!!! The end time of a event cannot be empty. + ____________________________________________________________ + ____________________________________________________________ + �\_(?)_/� OOPS!!! There may be a typo in the command used. Please use the command /to + ____________________________________________________________ + ____________________________________________________________ + ? OOPS!!! The end time of a event cannot be empty. Please use the command /to + ____________________________________________________________ + ____________________________________________________________ + ? OOPS!!! The end time of a event cannot be empty. Please use the command /to + ____________________________________________________________ + ____________________________________________________________ + ?:?? OOPS!!! The start time of a event cannot be empty. + ____________________________________________________________ + ____________________________________________________________ + �\_(?)_/� OOPS!!! There may be a typo in the command used. Please use the command /from + ____________________________________________________________ + ____________________________________________________________ + ?:?? OOPS!!! The start time of a event cannot be empty. Please use the command /from + ____________________________________________________________ + ____________________________________________________________ + ??? OOPS!!! The description of a event cannot be empty. + ____________________________________________________________ + ____________________________________________________________ + Bye. Hope to see you again soon! + ____________________________________________________________ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 30b2c284e..4a99a6c47 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -2,4 +2,22 @@ todo borrow book list deadline return book /by Sunday event project meeting /from Mon 2pm /to 4pm +mark 2 +list +unmark 2 +list +todo +deadline return book /by +deadline return book /b +deadline return book / +deadline return book +deadline +event project meeting /from Mon 2pm /to +event project meeting /from Mon 2pm /t +event project meeting /from Mon 2pm / +event project meeting /from Mon 2pm +event project meeting /from +event project meeting /fr +event project meeting +event bye \ No newline at end of file From 23dfdee2b45ef23b7b93d7cdadaf0f797e90b980 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Sun, 17 Sep 2023 00:09:08 +0800 Subject: [PATCH 20/46] Update code with regards to TA comments --- src/main/java/KenergeticBot.java | 14 +-- .../command/BooleanChecks.java} | 4 +- .../command/CommandList.java} | 95 +++++++++---------- .../command/CommonMessages.java} | 12 ++- .../KenergeticBotException.java | 2 +- .../{ => kenergeticbot}/task/Deadline.java | 4 +- .../java/{ => kenergeticbot}/task/Event.java | 8 +- .../java/{ => kenergeticbot}/task/Task.java | 3 +- .../java/{ => kenergeticbot}/task/Todo.java | 4 +- 9 files changed, 68 insertions(+), 78 deletions(-) rename src/main/java/{command/booleanChecks.java => kenergeticbot/command/BooleanChecks.java} (93%) rename src/main/java/{command/commandList.java => kenergeticbot/command/CommandList.java} (60%) rename src/main/java/{command/commonMessages.java => kenergeticbot/command/CommonMessages.java} (70%) rename src/main/java/{ => kenergeticbot}/exceptionhandler/KenergeticBotException.java (97%) rename src/main/java/{ => kenergeticbot}/task/Deadline.java (92%) rename src/main/java/{ => kenergeticbot}/task/Event.java (75%) rename src/main/java/{ => kenergeticbot}/task/Task.java (94%) rename src/main/java/{ => kenergeticbot}/task/Todo.java (75%) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index a60c3f470..b2b5d1d16 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -1,12 +1,12 @@ -import task.Task; -import exceptionhandler.KenergeticBotException; +import kenergeticbot.task.Task; +import kenergeticbot.exceptionhandler.KenergeticBotException; import java.util.Scanner; import java.util.ArrayList; -import static command.booleanChecks.*; -import static command.commandList.*; -import static command.commonMessages.*; +import static kenergeticbot.command.BooleanChecks.*; +import static kenergeticbot.command.CommandList.*; +import static kenergeticbot.command.CommonMessages.*; public class KenergeticBot { @@ -16,7 +16,7 @@ public static void main(String[] args) { botDialogue(taskList); } - //Main logic for the bot's dialogue + //Main logic for the bot dialogue public static void botDialogue(ArrayList taskList) { Scanner input = new Scanner(System.in); String item = input.nextLine(); @@ -34,7 +34,7 @@ public static void botDialogue(ArrayList taskList) { } else { try { add(taskList, item); - } catch (KenergeticBotException e) { //exception thrown when user inputs command outside of the usual commands + } catch (KenergeticBotException e) { //exception thrown when user inputs kenergeticbot.exceptionhandler.command outside the usual commands System.out.println(e.getMessage()); } } diff --git a/src/main/java/command/booleanChecks.java b/src/main/java/kenergeticbot/command/BooleanChecks.java similarity index 93% rename from src/main/java/command/booleanChecks.java rename to src/main/java/kenergeticbot/command/BooleanChecks.java index 641cf20cf..00ea17e36 100644 --- a/src/main/java/command/booleanChecks.java +++ b/src/main/java/kenergeticbot/command/BooleanChecks.java @@ -1,6 +1,6 @@ -package command; +package kenergeticbot.command; -public class booleanChecks { +public class BooleanChecks { //Returns True if the text contains "mark", False if not public static boolean checkTextForMark(String item) { return item.length() > 4 && item.startsWith("mark"); diff --git a/src/main/java/command/commandList.java b/src/main/java/kenergeticbot/command/CommandList.java similarity index 60% rename from src/main/java/command/commandList.java rename to src/main/java/kenergeticbot/command/CommandList.java index 256737fc3..79f3ed4ef 100644 --- a/src/main/java/command/commandList.java +++ b/src/main/java/kenergeticbot/command/CommandList.java @@ -1,46 +1,43 @@ -package command; +package kenergeticbot.command; -import exceptionhandler.KenergeticBotException; -import task.Deadline; -import task.Event; -import task.Task; -import task.Todo; +import kenergeticbot.exceptionhandler.KenergeticBotException; +import kenergeticbot.task.Deadline; +import kenergeticbot.task.Event; +import kenergeticbot.task.Task; +import kenergeticbot.task.Todo; import java.util.ArrayList; -import static command.booleanChecks.*; -import static command.commonMessages.printAddedTaskMessage; -import static command.commonMessages.printLine; -import static exceptionhandler.KenergeticBotException.*; +import static kenergeticbot.exceptionhandler.KenergeticBotException.*; -public class commandList { +public class CommandList { public static void list(ArrayList taskList) { - printLine(); + CommonMessages.printLine(); System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < taskList.size(); i++) { System.out.printf(" %d.%s\n", i+1, taskList.get(i)); } - printLine(); + CommonMessages.printLine(); } public static void mark(ArrayList taskList, int listIndex) { - printLine(); + CommonMessages.printLine(); System.out.println(" Nice! I've marked this task as done:"); taskList.get(listIndex - 1).mark(); System.out.printf(" %s\n", taskList.get(listIndex - 1)); - printLine(); + CommonMessages.printLine(); } public static void unmark(ArrayList taskList, int listIndex) { - printLine(); + CommonMessages.printLine(); System.out.println(" OK, I've marked this task as not done yet:"); taskList.get(listIndex - 1).unmark(); System.out.printf(" %s\n", taskList.get(listIndex - 1)); - printLine(); + CommonMessages.printLine(); } - //Creates a "task.Todo" object and adds to the taskList + //Creates a "Todo" object and adds to the taskList public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { String formattedString = item.replace("todo", "").trim(); if (formattedString.isEmpty()) { @@ -49,44 +46,44 @@ public static void addTodo(ArrayList taskList, String item) throws Kenerge String taskType = "[T]"; Task newTask = new Todo(formattedString, taskType); taskList.add(newTask); - printAddedTaskMessage(taskList, newTask); + CommonMessages.printAddedTaskMessage(taskList, newTask); } - //Creates a "task.Deadline" object and adds to the taskList + //Creates a "Deadline" object and adds to the taskList public static void addDeadline(ArrayList taskList, String item) throws KenergeticBotException { String[] formattedString = item.replace("deadline", "").trim().split("/"); - switch (formattedString.length) { - case 1: - if (formattedString[0].isEmpty()) { - throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DESCRIPTION); - } else { - throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DATE_INTERMEDIATE); - } - case 2: - if (!formattedString[1].contains("by")) { - throw new KenergeticBotException(KenergeticBotException.COMMAND_TYPO_DEADLINE_BY); - } else if (formattedString[1].replace("by", "").trim().isEmpty()) { - throw new KenergeticBotException(KenergeticBotException.DEADLINE_MISSING_DATE); - } + switch (formattedString.length) { //no default case needed since finding exceptions + case 1: //no break; is needed since throwing exception + if (formattedString[0].isEmpty()) { + throw new KenergeticBotException(DEADLINE_MISSING_DESCRIPTION); + } else { + throw new KenergeticBotException(DEADLINE_MISSING_DATE_INTERMEDIATE); + } + case 2: //no break; is needed since throwing exception + if (!formattedString[1].contains("by")) { + throw new KenergeticBotException(COMMAND_TYPO_DEADLINE_BY); + } else if (formattedString[1].replace("by", "").trim().isEmpty()) { + throw new KenergeticBotException(DEADLINE_MISSING_DATE); + } } String taskType = "[D]"; String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); taskList.add(newTask); - printAddedTaskMessage(taskList, newTask); + CommonMessages.printAddedTaskMessage(taskList, newTask); } - //Creates a "task.Event" object and adds to the taskList + //Creates a "Event" object and adds to the taskList public static void addEvent(ArrayList taskList, String item) throws KenergeticBotException { String[] formattedString = item.replace("event", "").trim().split("/"); - switch(formattedString.length) { - case 1: + switch(formattedString.length) { //no default case needed since finding exceptions + case 1: //no break; is needed since throwing exception if (formattedString[0].isEmpty()) { throw new KenergeticBotException(EVENT_MISSING_DESCRIPTION); } else { throw new KenergeticBotException(EVENT_MISSING_START_INTERMEDIATE); } - case 2: + case 2: //no break; is needed since throwing exception if (!formattedString[1].contains("from")) { throw new KenergeticBotException(COMMAND_TYPO_EVENT_FROM); } else if (formattedString[1].replace("from", "").trim().isEmpty()){ @@ -94,7 +91,7 @@ public static void addEvent(ArrayList taskList, String item) throws Kenerg } else { throw new KenergeticBotException(EVENT_MISSING_END_INTERMEDIATE); } - case 3: + case 3: //no break; is needed since throwing exception if (!formattedString[2].contains("to")) { throw new KenergeticBotException(COMMAND_TYPO_EVENT_TO); } else if (formattedString[2].replace("to", "").trim().isEmpty()){ @@ -107,33 +104,33 @@ public static void addEvent(ArrayList taskList, String item) throws Kenerg String dateTime = "(" + eventFrom + eventTo + ")"; Task newTask = new Event(formattedString[0], taskType, dateTime); taskList.add(newTask); - printAddedTaskMessage(taskList, newTask); + CommonMessages.printAddedTaskMessage(taskList, newTask); } //Controls the logic for adding items to the list public static void add(ArrayList taskList, String item) throws KenergeticBotException { - printLine(); - if (checkTextForTodo(item)) { + CommonMessages.printLine(); + if (BooleanChecks.checkTextForTodo(item)) { try { addTodo(taskList, item); - } catch (KenergeticBotException e) { //throws exception when the todo command is not followed with a description + } catch (KenergeticBotException e) { //throws exception when the todo command encounters a problem, the error message is tagged to the thrown exception System.out.println(e.getMessage()); } - } else if (checkTextForDeadline(item)) { + } else if (BooleanChecks.checkTextForDeadline(item)) { try { addDeadline(taskList, item); - } catch (KenergeticBotException e) { //throws exception when the deadline command is not followed with a description + } catch (KenergeticBotException e) { //throws exception when the deadline command encounters a problem, the error message is tagged to the thrown exception System.out.println(e.getMessage()); } - } else if (checkTextForEvent(item)) { + } else if (BooleanChecks.checkTextForEvent(item)) { try { addEvent(taskList, item);; - } catch (KenergeticBotException e) { //throws exception when the event command is not followed with a description + } catch (KenergeticBotException e) { //throws exception when the event command encounters a problem, the error message is tagged to the thrown exception System.out.println(e.getMessage()); } } else { - throw new KenergeticBotException(INVALID_COMMAND); + throw new KenergeticBotException(INVALID_COMMAND); //throws exception when input does not match any known command } - printLine(); + CommonMessages.printLine(); } } diff --git a/src/main/java/command/commonMessages.java b/src/main/java/kenergeticbot/command/CommonMessages.java similarity index 70% rename from src/main/java/command/commonMessages.java rename to src/main/java/kenergeticbot/command/CommonMessages.java index 04d5bbb1f..07021283e 100644 --- a/src/main/java/command/commonMessages.java +++ b/src/main/java/kenergeticbot/command/CommonMessages.java @@ -1,12 +1,14 @@ -package command; +package kenergeticbot.command; -import task.Task; +import kenergeticbot.task.Task; import java.util.ArrayList; -public class commonMessages { +public class CommonMessages { + + public static String SEPARATING_LINE = " ____________________________________________________________"; public static void printLine() { - System.out.println(" ____________________________________________________________"); + System.out.println(SEPARATING_LINE); } public static void printGreetingMessage() { printLine(); @@ -23,7 +25,7 @@ public static void printExitMessage() { public static void printAddedTaskMessage(ArrayList taskList, Task newTask) { System.out.println(" Got it. I've added this task:"); - System.out.printf(" %s\n", newTask); + System.out.println(" " + newTask); System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); } } diff --git a/src/main/java/exceptionhandler/KenergeticBotException.java b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java similarity index 97% rename from src/main/java/exceptionhandler/KenergeticBotException.java rename to src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java index 4519ca3ab..a2ef3dc9a 100644 --- a/src/main/java/exceptionhandler/KenergeticBotException.java +++ b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java @@ -1,4 +1,4 @@ -package exceptionhandler; +package kenergeticbot.exceptionhandler; public class KenergeticBotException extends Exception { public static final String INVALID_COMMAND = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; diff --git a/src/main/java/task/Deadline.java b/src/main/java/kenergeticbot/task/Deadline.java similarity index 92% rename from src/main/java/task/Deadline.java rename to src/main/java/kenergeticbot/task/Deadline.java index 00aadcda7..58eca8988 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/kenergeticbot/task/Deadline.java @@ -1,6 +1,4 @@ -package task; - -import task.Task; +package kenergeticbot.task; public class Deadline extends Task { protected String deadline; diff --git a/src/main/java/task/Event.java b/src/main/java/kenergeticbot/task/Event.java similarity index 75% rename from src/main/java/task/Event.java rename to src/main/java/kenergeticbot/task/Event.java index 2a9baefda..beb9e7d77 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/kenergeticbot/task/Event.java @@ -1,6 +1,4 @@ -package task; - -import task.Task; +package kenergeticbot.task; public class Event extends Task { protected String dateTime; @@ -9,10 +7,6 @@ public Event(String description, String taskType, String dateTime) { this.dateTime = dateTime; } - public String getDateTime() { - return dateTime; - } - public String toString() { return taskType + super.toString() + dateTime; } diff --git a/src/main/java/task/Task.java b/src/main/java/kenergeticbot/task/Task.java similarity index 94% rename from src/main/java/task/Task.java rename to src/main/java/kenergeticbot/task/Task.java index 4236d69c3..26c7b097e 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/kenergeticbot/task/Task.java @@ -1,9 +1,8 @@ -package task; +package kenergeticbot.task; public class Task { protected String description; protected boolean isDone; protected String taskType; - protected int taskIndex; public Task(String description, String taskType) { this.description = description; diff --git a/src/main/java/task/Todo.java b/src/main/java/kenergeticbot/task/Todo.java similarity index 75% rename from src/main/java/task/Todo.java rename to src/main/java/kenergeticbot/task/Todo.java index 0919da656..26d384efd 100644 --- a/src/main/java/task/Todo.java +++ b/src/main/java/kenergeticbot/task/Todo.java @@ -1,6 +1,6 @@ -package task; +package kenergeticbot.task; -public class Todo extends Task{ +public class Todo extends Task { public Todo(String description, String taskType) { super(description, taskType); } From ee3e90e90b324f95300273304840e64d45adfb8e Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Sun, 17 Sep 2023 23:13:02 +0800 Subject: [PATCH 21/46] Add Delete function --- src/main/java/KenergeticBot.java | 4 ++++ src/main/java/kenergeticbot/command/BooleanChecks.java | 3 +++ src/main/java/kenergeticbot/command/CommandList.java | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index b2b5d1d16..958629fec 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -31,6 +31,10 @@ public static void botDialogue(ArrayList taskList) { String[] splitItem = item.split(" "); int listIndex = Integer.parseInt(splitItem[1]); unmark(taskList, listIndex); + } else if (checkTextForDelete(item)) { + String[] splitItem = item.split(" "); + int listIndex = Integer.parseInt(splitItem[1]); + delete(taskList, listIndex); } else { try { add(taskList, item); diff --git a/src/main/java/kenergeticbot/command/BooleanChecks.java b/src/main/java/kenergeticbot/command/BooleanChecks.java index 00ea17e36..34e036b4a 100644 --- a/src/main/java/kenergeticbot/command/BooleanChecks.java +++ b/src/main/java/kenergeticbot/command/BooleanChecks.java @@ -25,4 +25,7 @@ public static boolean checkTextForDeadline(String item) { public static boolean checkTextForEvent(String item) { return item.startsWith("event"); } + public static boolean checkTextForDelete(String item) { + return item.startsWith("delete"); + } } diff --git a/src/main/java/kenergeticbot/command/CommandList.java b/src/main/java/kenergeticbot/command/CommandList.java index 79f3ed4ef..76a28535c 100644 --- a/src/main/java/kenergeticbot/command/CommandList.java +++ b/src/main/java/kenergeticbot/command/CommandList.java @@ -133,4 +133,13 @@ public static void add(ArrayList taskList, String item) throws KenergeticB } CommonMessages.printLine(); } + + public static void delete(ArrayList taskList, int listIndex) { + CommonMessages.printLine(); + System.out.println(" Noted. I've removed this task:"); + System.out.println(" " + taskList.get(listIndex - 1)); + taskList.remove(listIndex - 1); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); + CommonMessages.printLine(); + } } From fe43edde32ec563535a0950cecbd58d4ed0b89d2 Mon Sep 17 00:00:00 2001 From: ZH Date: Thu, 21 Sep 2023 17:07:51 +0800 Subject: [PATCH 22/46] Add Save function --- src/main/java/KenergeticBot.java | 11 +++++++++-- src/main/java/kenergeticbot/task/Deadline.java | 3 +++ src/main/java/kenergeticbot/task/Event.java | 5 +++++ src/main/java/kenergeticbot/task/Task.java | 5 +++++ src/main/java/kenergeticbot/task/Todo.java | 4 ++++ text-ui-test/EXPECTED.TXT | 10 +++++----- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index b2b5d1d16..7b2e98785 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -1,6 +1,7 @@ import kenergeticbot.task.Task; import kenergeticbot.exceptionhandler.KenergeticBotException; +import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; @@ -8,7 +9,9 @@ import static kenergeticbot.command.CommandList.*; import static kenergeticbot.command.CommonMessages.*; -public class KenergeticBot { +import kenergeticbot.fileaccess.Save; + +public class KenergeticBot extends Save { public static void main(String[] args) { printGreetingMessage(); @@ -34,12 +37,16 @@ public static void botDialogue(ArrayList taskList) { } else { try { add(taskList, item); - } catch (KenergeticBotException e) { //exception thrown when user inputs kenergeticbot.exceptionhandler.command outside the usual commands + } catch (KenergeticBotException e) { //exception thrown when user inputs command outside the usual commands System.out.println(e.getMessage()); } } item = input.nextLine(); } + Save.checkFileExist(); + for (Task task : taskList) { + Save.writeToFile(task.printTaskToSave()); + } printExitMessage(); } } diff --git a/src/main/java/kenergeticbot/task/Deadline.java b/src/main/java/kenergeticbot/task/Deadline.java index 58eca8988..956e733ac 100644 --- a/src/main/java/kenergeticbot/task/Deadline.java +++ b/src/main/java/kenergeticbot/task/Deadline.java @@ -14,4 +14,7 @@ public String getDeadline() { public String toString() { return taskType + super.toString() + deadline; } + public String printTaskToSave() { + return taskType + super.printTaskToSave() + " | " + deadline; + } } diff --git a/src/main/java/kenergeticbot/task/Event.java b/src/main/java/kenergeticbot/task/Event.java index beb9e7d77..880ddc446 100644 --- a/src/main/java/kenergeticbot/task/Event.java +++ b/src/main/java/kenergeticbot/task/Event.java @@ -10,4 +10,9 @@ public Event(String description, String taskType, String dateTime) { public String toString() { return taskType + super.toString() + dateTime; } + + public String printTaskToSave() { + String task = taskType.replace("[", "").replace("]",""); + return task + super.printTaskToSave() + " | " + dateTime; + } } diff --git a/src/main/java/kenergeticbot/task/Task.java b/src/main/java/kenergeticbot/task/Task.java index 26c7b097e..1ac54eab2 100644 --- a/src/main/java/kenergeticbot/task/Task.java +++ b/src/main/java/kenergeticbot/task/Task.java @@ -29,4 +29,9 @@ public void unmark() { public String toString() { return getStatusIcon() + " " + description; } + + public String printTaskToSave() { + int mark = isDone ? 1 : 0; + return " | " + mark + " | " + description; + } } diff --git a/src/main/java/kenergeticbot/task/Todo.java b/src/main/java/kenergeticbot/task/Todo.java index 26d384efd..caf9cd78a 100644 --- a/src/main/java/kenergeticbot/task/Todo.java +++ b/src/main/java/kenergeticbot/task/Todo.java @@ -8,4 +8,8 @@ public Todo(String description, String taskType) { public String toString() { return taskType + super.toString(); } + public String printTaskToSave() { + String task = taskType.replace("[", "").replace("]",""); + return task + super.printTaskToSave(); + } } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index fce942bee..6af8d43f6 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -48,7 +48,7 @@ ? OOPS!!! The date of a deadline cannot be empty. ____________________________________________________________ ____________________________________________________________ - �\_(?)_/� OOPS!!! There may be a typo in the command used. Please use the command /by + ¯\_(ツ)_/¯ OOPS!!! There may be a typo in the command used. Please use the command /by ____________________________________________________________ ____________________________________________________________ ? OOPS!!! The date of a deadline cannot be empty. Please use the command /by @@ -63,7 +63,7 @@ ? OOPS!!! The end time of a event cannot be empty. ____________________________________________________________ ____________________________________________________________ - �\_(?)_/� OOPS!!! There may be a typo in the command used. Please use the command /to + ¯\_(ツ)_/¯ OOPS!!! There may be a typo in the command used. Please use the command /to ____________________________________________________________ ____________________________________________________________ ? OOPS!!! The end time of a event cannot be empty. Please use the command /to @@ -72,13 +72,13 @@ ? OOPS!!! The end time of a event cannot be empty. Please use the command /to ____________________________________________________________ ____________________________________________________________ - ?:?? OOPS!!! The start time of a event cannot be empty. + ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty. ____________________________________________________________ ____________________________________________________________ - �\_(?)_/� OOPS!!! There may be a typo in the command used. Please use the command /from + ¯\_(ツ)_/¯ OOPS!!! There may be a typo in the command used. Please use the command /from ____________________________________________________________ ____________________________________________________________ - ?:?? OOPS!!! The start time of a event cannot be empty. Please use the command /from + ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty. Please use the command /from ____________________________________________________________ ____________________________________________________________ ??? OOPS!!! The description of a event cannot be empty. From b7a4dd3f63f669ba72c359e4f4d6b93bfcf6237e Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Wed, 27 Sep 2023 15:55:19 +0800 Subject: [PATCH 23/46] Update Saving capability --- .gitignore | 1 + src/main/java/KenergeticBot.java | 19 +-- .../java/kenergeticbot/fileaccess/Save.java | 136 ++++++++++++++++++ src/main/java/kenergeticbot/task/Task.java | 2 +- 4 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 src/main/java/kenergeticbot/fileaccess/Save.java diff --git a/.gitignore b/.gitignore index 2873e189e..9654ef457 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT +/data/ diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index 7b2e98785..142c83e00 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -1,21 +1,27 @@ import kenergeticbot.task.Task; import kenergeticbot.exceptionhandler.KenergeticBotException; -import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; -import static kenergeticbot.command.BooleanChecks.*; -import static kenergeticbot.command.CommandList.*; -import static kenergeticbot.command.CommonMessages.*; +import static kenergeticbot.command.BooleanChecks.checkTextForMark; +import static kenergeticbot.command.BooleanChecks.checkTextForUnmark; +import static kenergeticbot.command.CommandList.add; +import static kenergeticbot.command.CommandList.list; +import static kenergeticbot.command.CommandList.mark; +import static kenergeticbot.command.CommandList.unmark; +import static kenergeticbot.command.CommonMessages.printExitMessage; +import static kenergeticbot.command.CommonMessages.printGreetingMessage; import kenergeticbot.fileaccess.Save; public class KenergeticBot extends Save { + public static void main(String[] args) { printGreetingMessage(); ArrayList taskList = new ArrayList(); + loadPreviousList(taskList); botDialogue(taskList); } @@ -43,10 +49,7 @@ public static void botDialogue(ArrayList taskList) { } item = input.nextLine(); } - Save.checkFileExist(); - for (Task task : taskList) { - Save.writeToFile(task.printTaskToSave()); - } + saveList(taskList); printExitMessage(); } } diff --git a/src/main/java/kenergeticbot/fileaccess/Save.java b/src/main/java/kenergeticbot/fileaccess/Save.java new file mode 100644 index 000000000..8e745c185 --- /dev/null +++ b/src/main/java/kenergeticbot/fileaccess/Save.java @@ -0,0 +1,136 @@ +package kenergeticbot.fileaccess; + +import kenergeticbot.task.Deadline; +import kenergeticbot.task.Event; +import kenergeticbot.task.Task; +import kenergeticbot.task.Todo; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public abstract class Save { + + public static final String filePath = "data/KenergeticBot.txt"; + public static final String folderPath = "data"; + public static void checkFileExist() { + File f = new File(filePath); + checkFolderExist(); + try { + if (f.createNewFile()) { + System.out.println("File created: " + f.getName()); + } else { + System.out.println("File already exists."); + } + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + + System.out.println("full path: " + f.getAbsolutePath()); + System.out.println("file exists?: " + f.exists()); + System.out.println("is Directory?: " + f.isDirectory()); + } + + public static void checkFolderExist() { + File f = new File(folderPath); + if (f.mkdir()) { + System.out.println("Directory is created"); + } else { + System.out.println("Directory cannot be created"); + } + System.out.println("full path: " + f.getAbsolutePath()); + System.out.println("file exists?: " + f.exists()); + System.out.println("is Directory?: " + f.isDirectory()); + } + + public static void loadPreviousList (ArrayList taskList) { + try { + readFromFile(filePath, taskList); + } catch (FileNotFoundException e) { + System.out.println("An error occurred." + e.getMessage()); + } + } + + public static void readFromFile (String filePath, ArrayList taskList) throws FileNotFoundException { + File f = new File(filePath); // create a File for the given file path + if (f.length() == 0) { + return; + } + Scanner s = new Scanner(f); + ArrayList previousTaskList = new ArrayList(); + while (s.hasNext()) { + previousTaskList.add(s.nextLine()); + } + s.close(); + + for (String task : previousTaskList) { + String[] taskVariables = task.split(" \\| "); + String taskType = taskVariables[0]; + String taskMark = taskVariables[1]; + String taskDescription = taskVariables[2]; + + switch (taskType) { + case "T" : + Task previousTodo = new Todo(taskDescription, "[T]"); + taskList.add(previousTodo); + break; + case "D" : + String[] taskDeadline = taskDescription.split("\\("); + Task previousDeadline = new Deadline(taskDeadline[0], "[D]", "(" + taskDeadline[1]); + taskList.add(previousDeadline); + break; + + case "E" : + String[] taskEvent = taskDescription.split("\\("); + Task previousEvent = new Event(taskEvent[0], "[E]", "(" + taskEvent[1]); + taskList.add(previousEvent); + break; + + default : + throw new IllegalStateException("Unexpected value: " + taskType); + } + System.out.println(taskDescription + " added to list"); + } + try { + new FileWriter(filePath, false).close(); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + + } + + public static void writeToFile(String textToAdd) { + File f = new File(filePath); + if (f.length() == 0) { + try { + FileWriter myWriter = new FileWriter(filePath); + myWriter.write(textToAdd); + myWriter.close(); + System.out.println("Successfully wrote to the file."); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + } else { + try { + FileWriter myWriter = new FileWriter(filePath, true); + myWriter.write(textToAdd); + myWriter.close(); + System.out.println("Successfully wrote to the file."); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + } + } + + public static void saveList(ArrayList taskList) { + checkFileExist(); + for (Task task : taskList) { + System.out.println(task); + Save.writeToFile(task.printTaskToSave()); + } + } +} diff --git a/src/main/java/kenergeticbot/task/Task.java b/src/main/java/kenergeticbot/task/Task.java index 1ac54eab2..9cc48c6b4 100644 --- a/src/main/java/kenergeticbot/task/Task.java +++ b/src/main/java/kenergeticbot/task/Task.java @@ -32,6 +32,6 @@ public String toString() { public String printTaskToSave() { int mark = isDone ? 1 : 0; - return " | " + mark + " | " + description; + return " | " + mark + " | " + description + "\n"; } } From 4c2227f4a2ec5be60e411c1a2091ae32324c1c00 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Wed, 27 Sep 2023 16:19:19 +0800 Subject: [PATCH 24/46] Add import for Delete in main --- src/main/java/KenergeticBot.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index cfbf3f16f..b0caf3fe3 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -6,10 +6,12 @@ import static kenergeticbot.command.BooleanChecks.checkTextForMark; import static kenergeticbot.command.BooleanChecks.checkTextForUnmark; +import static kenergeticbot.command.BooleanChecks.checkTextForDelete; import static kenergeticbot.command.CommandList.add; import static kenergeticbot.command.CommandList.list; import static kenergeticbot.command.CommandList.mark; import static kenergeticbot.command.CommandList.unmark; +import static kenergeticbot.command.CommandList.delete; import static kenergeticbot.command.CommonMessages.printExitMessage; import static kenergeticbot.command.CommonMessages.printGreetingMessage; From 0a23d97610bf8fba4404c59a1a93788c0b9a5a1d Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Wed, 27 Sep 2023 17:00:17 +0800 Subject: [PATCH 25/46] Update previousList format --- src/main/java/KenergeticBot.java | 3 +-- src/main/java/META-INF/MANIFEST.MF | 3 +++ src/main/java/kenergeticbot/fileaccess/Save.java | 11 +++++------ src/main/java/kenergeticbot/task/Deadline.java | 3 ++- src/main/java/kenergeticbot/task/Event.java | 2 +- src/main/java/kenergeticbot/task/Task.java | 2 +- src/main/java/kenergeticbot/task/Todo.java | 2 +- 7 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index b0caf3fe3..996028be3 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -18,8 +18,7 @@ import kenergeticbot.fileaccess.Save; public class KenergeticBot extends Save { - - + public static void main(String[] args) { printGreetingMessage(); ArrayList taskList = new ArrayList(); diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..72fcd5b64 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: KenergeticBot + diff --git a/src/main/java/kenergeticbot/fileaccess/Save.java b/src/main/java/kenergeticbot/fileaccess/Save.java index 8e745c185..41b5ce071 100644 --- a/src/main/java/kenergeticbot/fileaccess/Save.java +++ b/src/main/java/kenergeticbot/fileaccess/Save.java @@ -73,23 +73,22 @@ public static void readFromFile (String filePath, ArrayList taskList) thro String taskMark = taskVariables[1]; String taskDescription = taskVariables[2]; + switch (taskType) { case "T" : Task previousTodo = new Todo(taskDescription, "[T]"); taskList.add(previousTodo); break; case "D" : - String[] taskDeadline = taskDescription.split("\\("); - Task previousDeadline = new Deadline(taskDeadline[0], "[D]", "(" + taskDeadline[1]); + String taskDeadline = taskVariables[3]; + Task previousDeadline = new Deadline(taskDescription, "[D]", taskDeadline); taskList.add(previousDeadline); break; - case "E" : - String[] taskEvent = taskDescription.split("\\("); - Task previousEvent = new Event(taskEvent[0], "[E]", "(" + taskEvent[1]); + String taskEventDateTime = taskVariables[3]; + Task previousEvent = new Event(taskDescription, "[E]", taskEventDateTime); taskList.add(previousEvent); break; - default : throw new IllegalStateException("Unexpected value: " + taskType); } diff --git a/src/main/java/kenergeticbot/task/Deadline.java b/src/main/java/kenergeticbot/task/Deadline.java index 956e733ac..8e65d75ce 100644 --- a/src/main/java/kenergeticbot/task/Deadline.java +++ b/src/main/java/kenergeticbot/task/Deadline.java @@ -15,6 +15,7 @@ public String toString() { return taskType + super.toString() + deadline; } public String printTaskToSave() { - return taskType + super.printTaskToSave() + " | " + deadline; + String task = taskType.replace("[", "").replace("]",""); + return task + super.printTaskToSave() + " | " + deadline + "\n"; } } diff --git a/src/main/java/kenergeticbot/task/Event.java b/src/main/java/kenergeticbot/task/Event.java index 880ddc446..d8678a21e 100644 --- a/src/main/java/kenergeticbot/task/Event.java +++ b/src/main/java/kenergeticbot/task/Event.java @@ -13,6 +13,6 @@ public String toString() { public String printTaskToSave() { String task = taskType.replace("[", "").replace("]",""); - return task + super.printTaskToSave() + " | " + dateTime; + return task + super.printTaskToSave() + " | " + dateTime + "\n"; } } diff --git a/src/main/java/kenergeticbot/task/Task.java b/src/main/java/kenergeticbot/task/Task.java index 9cc48c6b4..1ac54eab2 100644 --- a/src/main/java/kenergeticbot/task/Task.java +++ b/src/main/java/kenergeticbot/task/Task.java @@ -32,6 +32,6 @@ public String toString() { public String printTaskToSave() { int mark = isDone ? 1 : 0; - return " | " + mark + " | " + description + "\n"; + return " | " + mark + " | " + description; } } diff --git a/src/main/java/kenergeticbot/task/Todo.java b/src/main/java/kenergeticbot/task/Todo.java index caf9cd78a..b3f3e6141 100644 --- a/src/main/java/kenergeticbot/task/Todo.java +++ b/src/main/java/kenergeticbot/task/Todo.java @@ -10,6 +10,6 @@ public String toString() { } public String printTaskToSave() { String task = taskType.replace("[", "").replace("]",""); - return task + super.printTaskToSave(); + return task + super.printTaskToSave() + "\n"; } } From 0e39d2eee1ca9f728e282e3be9d8d534e821b88b Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Thu, 28 Sep 2023 02:46:30 +0800 Subject: [PATCH 26/46] Add MoreOOP for Storage and Command --- src/main/java/KenergeticBot.java | 94 +++++------ src/main/java/kenergeticbot/TaskList.java | 28 ++++ .../{CommandList.java => AddCommand.java} | 146 ++++++++---------- .../java/kenergeticbot/command/Command.java | 9 ++ .../kenergeticbot/command/DeleteCommand.java | 37 +++++ .../kenergeticbot/command/ExitCommand.java | 16 ++ .../command/IncorrectCommand.java | 16 ++ .../kenergeticbot/command/ListCommand.java | 16 ++ .../kenergeticbot/command/MarkCommand.java | 33 ++++ .../java/kenergeticbot/command/Parser.java | 83 ++++++++++ .../kenergeticbot/command/UnmarkCommand.java | 35 +++++ .../KenergeticBotException.java | 4 + .../fileaccess/{Save.java => Storage.java} | 69 +++++---- .../java/kenergeticbot/task/Deadline.java | 5 +- src/main/java/kenergeticbot/task/Event.java | 5 +- src/main/java/kenergeticbot/task/Task.java | 3 +- src/main/java/kenergeticbot/task/Todo.java | 5 +- .../CommonMessages.java => ui/TextUi.java} | 26 +++- text-ui-test/data/KenergeticBot.txt | 0 19 files changed, 451 insertions(+), 179 deletions(-) create mode 100644 src/main/java/kenergeticbot/TaskList.java rename src/main/java/kenergeticbot/command/{CommandList.java => AddCommand.java} (54%) create mode 100644 src/main/java/kenergeticbot/command/Command.java create mode 100644 src/main/java/kenergeticbot/command/DeleteCommand.java create mode 100644 src/main/java/kenergeticbot/command/ExitCommand.java create mode 100644 src/main/java/kenergeticbot/command/IncorrectCommand.java create mode 100644 src/main/java/kenergeticbot/command/ListCommand.java create mode 100644 src/main/java/kenergeticbot/command/MarkCommand.java create mode 100644 src/main/java/kenergeticbot/command/Parser.java create mode 100644 src/main/java/kenergeticbot/command/UnmarkCommand.java rename src/main/java/kenergeticbot/fileaccess/{Save.java => Storage.java} (64%) rename src/main/java/kenergeticbot/{command/CommonMessages.java => ui/TextUi.java} (58%) create mode 100644 text-ui-test/data/KenergeticBot.txt diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index 996028be3..804db5451 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -1,60 +1,50 @@ -import kenergeticbot.task.Task; -import kenergeticbot.exceptionhandler.KenergeticBotException; +import kenergeticbot.TaskList; +import kenergeticbot.command.Command; import java.util.Scanner; -import java.util.ArrayList; - -import static kenergeticbot.command.BooleanChecks.checkTextForMark; -import static kenergeticbot.command.BooleanChecks.checkTextForUnmark; -import static kenergeticbot.command.BooleanChecks.checkTextForDelete; -import static kenergeticbot.command.CommandList.add; -import static kenergeticbot.command.CommandList.list; -import static kenergeticbot.command.CommandList.mark; -import static kenergeticbot.command.CommandList.unmark; -import static kenergeticbot.command.CommandList.delete; -import static kenergeticbot.command.CommonMessages.printExitMessage; -import static kenergeticbot.command.CommonMessages.printGreetingMessage; - -import kenergeticbot.fileaccess.Save; - -public class KenergeticBot extends Save { - + +import kenergeticbot.fileaccess.Storage; +import kenergeticbot.ui.TextUi; +import kenergeticbot.command.Parser; +import kenergeticbot.command.ExitCommand; + + +public class KenergeticBot { + public static final String filePath = "data/KenergeticBot.txt"; + + private TextUi ui; + private Parser commandParser; + private Storage storage; + public TaskList taskList; + public static void main(String[] args) { - printGreetingMessage(); - ArrayList taskList = new ArrayList(); - loadPreviousList(taskList); - botDialogue(taskList); + new KenergeticBot().run(); } - //Main logic for the bot dialogue - public static void botDialogue(ArrayList taskList) { + private void start(String filePath, TaskList taskList) { + this.storage = new Storage(filePath); + storage.loadPreviousList(taskList); + TextUi.printGreetingMessage(); + } + + public void run() { + this.taskList = new TaskList(); + start(filePath, taskList); + runCommandLoopUntilExitCommand(); + exit(); + } + public void runCommandLoopUntilExitCommand() { Scanner input = new Scanner(System.in); - String item = input.nextLine(); - while(!item.equals("bye")) { - if (item.equals("list")) { - list(taskList); - } else if (checkTextForMark(item)) { - String[] splitItem = item.split(" "); - int listIndex = Integer.parseInt(splitItem[1]); - mark(taskList, listIndex); - } else if (checkTextForUnmark(item)) { - String[] splitItem = item.split(" "); - int listIndex = Integer.parseInt(splitItem[1]); - unmark(taskList, listIndex); - } else if (checkTextForDelete(item)) { - String[] splitItem = item.split(" "); - int listIndex = Integer.parseInt(splitItem[1]); - delete(taskList, listIndex); - } else { - try { - add(taskList, item); - } catch (KenergeticBotException e) { //exception thrown when user inputs command outside the usual commands - System.out.println(e.getMessage()); - } - } - item = input.nextLine(); - } - saveList(taskList); - printExitMessage(); + do { + String item = input.nextLine(); + Command c = Parser.parseCommand(taskList, item); + c.execute(taskList); + } while (!ExitCommand.isExit()); + storage.saveList(taskList); + } + + private static void exit() { + TextUi.printExitMessage(); + System.exit(0); } } diff --git a/src/main/java/kenergeticbot/TaskList.java b/src/main/java/kenergeticbot/TaskList.java new file mode 100644 index 000000000..a3d0c6d05 --- /dev/null +++ b/src/main/java/kenergeticbot/TaskList.java @@ -0,0 +1,28 @@ +package kenergeticbot; + +import kenergeticbot.task.Task; + +import java.util.ArrayList; + +public class TaskList { + protected static ArrayList taskList; + public TaskList() { + taskList = new ArrayList(); + } + public int getSize() { + return taskList.size(); + } + public Task getTask(int taskID) { + return taskList.get(taskID); + } + public void remove(int taskID) { + taskList.remove(taskID); + } + public String toString() { + return "A"; + } + + public void add(Task task) { + taskList.add(task); + } +} diff --git a/src/main/java/kenergeticbot/command/CommandList.java b/src/main/java/kenergeticbot/command/AddCommand.java similarity index 54% rename from src/main/java/kenergeticbot/command/CommandList.java rename to src/main/java/kenergeticbot/command/AddCommand.java index 76a28535c..008859b01 100644 --- a/src/main/java/kenergeticbot/command/CommandList.java +++ b/src/main/java/kenergeticbot/command/AddCommand.java @@ -1,80 +1,95 @@ package kenergeticbot.command; +import kenergeticbot.TaskList; import kenergeticbot.exceptionhandler.KenergeticBotException; import kenergeticbot.task.Deadline; import kenergeticbot.task.Event; import kenergeticbot.task.Task; import kenergeticbot.task.Todo; - -import java.util.ArrayList; +import kenergeticbot.ui.TextUi; import static kenergeticbot.exceptionhandler.KenergeticBotException.*; - -public class CommandList { - public static void list(ArrayList taskList) { - CommonMessages.printLine(); - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < taskList.size(); i++) { - System.out.printf(" %d.%s\n", i+1, taskList.get(i)); +public class AddCommand extends Command { + private static String item = null; + private String taskType; + public AddCommand(String item) throws KenergeticBotException { + this.item = item; + if (BooleanChecks.checkTextForTodo(item)) { + this.taskType = "T"; + } else if (BooleanChecks.checkTextForDeadline(item)) { + this.taskType = "D"; + } else if (BooleanChecks.checkTextForEvent(item)) { + this.taskType = "E"; + }else { + throw new KenergeticBotException(INVALID_COMMAND); //throws exception when input does not match any known command } - CommonMessages.printLine(); - } - - public static void mark(ArrayList taskList, int listIndex) { - CommonMessages.printLine(); - System.out.println(" Nice! I've marked this task as done:"); - taskList.get(listIndex - 1).mark(); - System.out.printf(" %s\n", taskList.get(listIndex - 1)); - CommonMessages.printLine(); } - public static void unmark(ArrayList taskList, int listIndex) { - CommonMessages.printLine(); - System.out.println(" OK, I've marked this task as not done yet:"); - taskList.get(listIndex - 1).unmark(); - System.out.printf(" %s\n", taskList.get(listIndex - 1)); - CommonMessages.printLine(); + public void execute(TaskList taskList){ + TextUi.printLine(); + switch (taskType) { + case "T": + try { + addTodo(taskList, item); + } catch (KenergeticBotException e) { //throws exception when the todo command encounters a problem, the error message is tagged to the thrown exception + System.out.println(e.getMessage()); + } + break; + case "D": + try { + addDeadline(taskList, item); + } catch (KenergeticBotException e) { //throws exception when the deadline command encounters a problem, the error message is tagged to the thrown exception + System.out.println(e.getMessage()); + } + break; + case "E": + try { + addEvent(taskList, item);; + } catch (KenergeticBotException e) { //throws exception when the event command encounters a problem, the error message is tagged to the thrown exception + System.out.println(e.getMessage()); + } + break; + } + TextUi.printLine(); } //Creates a "Todo" object and adds to the taskList - public static void addTodo(ArrayList taskList, String item) throws KenergeticBotException { + public static void addTodo(TaskList taskList ,String item) throws KenergeticBotException { String formattedString = item.replace("todo", "").trim(); if (formattedString.isEmpty()) { throw new KenergeticBotException(TODO_MISSING_DESCRIPTION); } - String taskType = "[T]"; - Task newTask = new Todo(formattedString, taskType); + Task newTask = new Todo(formattedString); taskList.add(newTask); - CommonMessages.printAddedTaskMessage(taskList, newTask); + TextUi.printAddedTaskMessage(taskList, newTask); } //Creates a "Deadline" object and adds to the taskList - public static void addDeadline(ArrayList taskList, String item) throws KenergeticBotException { + public static void addDeadline(TaskList taskList, String item) throws KenergeticBotException { String[] formattedString = item.replace("deadline", "").trim().split("/"); switch (formattedString.length) { //no default case needed since finding exceptions - case 1: //no break; is needed since throwing exception - if (formattedString[0].isEmpty()) { - throw new KenergeticBotException(DEADLINE_MISSING_DESCRIPTION); - } else { - throw new KenergeticBotException(DEADLINE_MISSING_DATE_INTERMEDIATE); - } - case 2: //no break; is needed since throwing exception - if (!formattedString[1].contains("by")) { - throw new KenergeticBotException(COMMAND_TYPO_DEADLINE_BY); - } else if (formattedString[1].replace("by", "").trim().isEmpty()) { - throw new KenergeticBotException(DEADLINE_MISSING_DATE); - } + case 1: //no break; is needed since throwing exception + if (formattedString[0].isEmpty()) { + throw new KenergeticBotException(DEADLINE_MISSING_DESCRIPTION); + } else { + throw new KenergeticBotException(DEADLINE_MISSING_DATE_INTERMEDIATE); + } + case 2: //no break; is needed since throwing exception + if (!formattedString[1].contains("by")) { + throw new KenergeticBotException(COMMAND_TYPO_DEADLINE_BY); + } else if (formattedString[1].replace("by", "").trim().isEmpty()) { + throw new KenergeticBotException(DEADLINE_MISSING_DATE); + } } - String taskType = "[D]"; String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; - Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); + Task newTask = new Deadline(formattedString[0], deadlineDate); taskList.add(newTask); - CommonMessages.printAddedTaskMessage(taskList, newTask); + TextUi.printAddedTaskMessage(taskList, newTask); } //Creates a "Event" object and adds to the taskList - public static void addEvent(ArrayList taskList, String item) throws KenergeticBotException { + public static void addEvent(TaskList taskList, String item) throws KenergeticBotException { String[] formattedString = item.replace("event", "").trim().split("/"); switch(formattedString.length) { //no default case needed since finding exceptions case 1: //no break; is needed since throwing exception @@ -98,48 +113,11 @@ public static void addEvent(ArrayList taskList, String item) throws Kenerg throw new KenergeticBotException(EVENT_MISSING_END); } } - String taskType = "[E]"; String eventFrom = formattedString[1].replace("from", "from:"); String eventTo = formattedString[2].replace("to", "to:"); String dateTime = "(" + eventFrom + eventTo + ")"; - Task newTask = new Event(formattedString[0], taskType, dateTime); + Task newTask = new Event(formattedString[0], dateTime); taskList.add(newTask); - CommonMessages.printAddedTaskMessage(taskList, newTask); - } - - //Controls the logic for adding items to the list - public static void add(ArrayList taskList, String item) throws KenergeticBotException { - CommonMessages.printLine(); - if (BooleanChecks.checkTextForTodo(item)) { - try { - addTodo(taskList, item); - } catch (KenergeticBotException e) { //throws exception when the todo command encounters a problem, the error message is tagged to the thrown exception - System.out.println(e.getMessage()); - } - } else if (BooleanChecks.checkTextForDeadline(item)) { - try { - addDeadline(taskList, item); - } catch (KenergeticBotException e) { //throws exception when the deadline command encounters a problem, the error message is tagged to the thrown exception - System.out.println(e.getMessage()); - } - } else if (BooleanChecks.checkTextForEvent(item)) { - try { - addEvent(taskList, item);; - } catch (KenergeticBotException e) { //throws exception when the event command encounters a problem, the error message is tagged to the thrown exception - System.out.println(e.getMessage()); - } - } else { - throw new KenergeticBotException(INVALID_COMMAND); //throws exception when input does not match any known command - } - CommonMessages.printLine(); - } - - public static void delete(ArrayList taskList, int listIndex) { - CommonMessages.printLine(); - System.out.println(" Noted. I've removed this task:"); - System.out.println(" " + taskList.get(listIndex - 1)); - taskList.remove(listIndex - 1); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); - CommonMessages.printLine(); + TextUi.printAddedTaskMessage(taskList, newTask); } } diff --git a/src/main/java/kenergeticbot/command/Command.java b/src/main/java/kenergeticbot/command/Command.java new file mode 100644 index 000000000..ec928d637 --- /dev/null +++ b/src/main/java/kenergeticbot/command/Command.java @@ -0,0 +1,9 @@ +package kenergeticbot.command; + +import kenergeticbot.TaskList; + +public class Command { + public void execute(TaskList taskList) { + throw new UnsupportedOperationException("This method is to be implemented by child classes"); + }; +} diff --git a/src/main/java/kenergeticbot/command/DeleteCommand.java b/src/main/java/kenergeticbot/command/DeleteCommand.java new file mode 100644 index 000000000..2c262d01d --- /dev/null +++ b/src/main/java/kenergeticbot/command/DeleteCommand.java @@ -0,0 +1,37 @@ +package kenergeticbot.command; + +import kenergeticbot.TaskList; +import kenergeticbot.exceptionhandler.KenergeticBotException; +import kenergeticbot.task.Task; +import kenergeticbot.ui.TextUi; + +import java.util.ArrayList; + +import static kenergeticbot.exceptionhandler.KenergeticBotException.*; + +public class DeleteCommand extends Command { + public static final String COMMAND_WORD = "delete"; + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Deletes the task identified by the index number used in the last task listing.\n" + + "Parameters: INDEX\n" + + "Example: " + COMMAND_WORD + " 1"; + private final int listIndex; + public DeleteCommand(int listIndex) { + this.listIndex = listIndex; + } + public static void checkValid (String item) throws KenergeticBotException { + String formattedString = item.replace("delete", "").trim(); + if (formattedString.isEmpty()) { + throw new KenergeticBotException(COMMAND_TYPO_NO_NUMBER); + } + } + public void execute(TaskList taskList) { + TextUi.printLine(); + System.out.println(" Noted. I've removed this task:"); + System.out.println(" " + taskList.getTask(listIndex - 1)); + taskList.remove(listIndex - 1); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.getSize()); + TextUi.printLine(); + } + +} diff --git a/src/main/java/kenergeticbot/command/ExitCommand.java b/src/main/java/kenergeticbot/command/ExitCommand.java new file mode 100644 index 000000000..64a39f65f --- /dev/null +++ b/src/main/java/kenergeticbot/command/ExitCommand.java @@ -0,0 +1,16 @@ +package kenergeticbot.command; + +import kenergeticbot.TaskList; + +public class ExitCommand extends Command{ + protected static boolean isExit = false; + public static final String COMMAND_WORD = "bye"; + + public void execute(TaskList taskList) { + isExit = true; + } + + public static boolean isExit() { + return isExit; + } +} diff --git a/src/main/java/kenergeticbot/command/IncorrectCommand.java b/src/main/java/kenergeticbot/command/IncorrectCommand.java new file mode 100644 index 000000000..2547a5ce6 --- /dev/null +++ b/src/main/java/kenergeticbot/command/IncorrectCommand.java @@ -0,0 +1,16 @@ +package kenergeticbot.command; + +import kenergeticbot.TaskList; + +public class IncorrectCommand extends Command{ + public final String feedbackToUser; + + public IncorrectCommand(String feedbackToUser) { + this.feedbackToUser = feedbackToUser; + } + + + public void execute(TaskList taskList) { + System.out.println(feedbackToUser); + } +} diff --git a/src/main/java/kenergeticbot/command/ListCommand.java b/src/main/java/kenergeticbot/command/ListCommand.java new file mode 100644 index 000000000..ee96a1f76 --- /dev/null +++ b/src/main/java/kenergeticbot/command/ListCommand.java @@ -0,0 +1,16 @@ +package kenergeticbot.command; + +import kenergeticbot.TaskList; +import kenergeticbot.ui.TextUi; + +public class ListCommand extends Command { + public static final String COMMAND_WORD = "list"; + public void execute(TaskList taskList) { + TextUi.printLine(); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < taskList.getSize(); i++) { + System.out.printf(" %d.%s\n", i+1, taskList.getTask(i)); + } + TextUi.printLine(); + } +} diff --git a/src/main/java/kenergeticbot/command/MarkCommand.java b/src/main/java/kenergeticbot/command/MarkCommand.java new file mode 100644 index 000000000..06cb3df7a --- /dev/null +++ b/src/main/java/kenergeticbot/command/MarkCommand.java @@ -0,0 +1,33 @@ +package kenergeticbot.command; + +import kenergeticbot.TaskList; +import kenergeticbot.exceptionhandler.KenergeticBotException; +import kenergeticbot.ui.TextUi; + +import static kenergeticbot.exceptionhandler.KenergeticBotException.*; + +public class MarkCommand extends Command { + + public static final String COMMAND_WORD = "mark"; + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Marks the task identified by the index number used in the last task listing as done.\n" + + "Parameters: INDEX\n" + + "Example: " + COMMAND_WORD + " 1"; + private final int listIndex; + public MarkCommand(int listIndex) { + this.listIndex = listIndex; + } + public static void checkValid(String item) throws KenergeticBotException { + String formattedString = item.replace("mark", "").trim(); + if (formattedString.isEmpty()) { + throw new KenergeticBotException(COMMAND_TYPO_NO_NUMBER); + } + } + public void execute(TaskList taskList){ + TextUi.printLine(); + System.out.println(" Nice! I've marked this task as done:"); + taskList.getTask(listIndex - 1).mark(); + System.out.printf(" %s\n", taskList.getTask(listIndex - 1)); + TextUi.printLine(); + } +} diff --git a/src/main/java/kenergeticbot/command/Parser.java b/src/main/java/kenergeticbot/command/Parser.java new file mode 100644 index 000000000..4c5f99b29 --- /dev/null +++ b/src/main/java/kenergeticbot/command/Parser.java @@ -0,0 +1,83 @@ +package kenergeticbot.command; + +import kenergeticbot.TaskList; +import kenergeticbot.exceptionhandler.KenergeticBotException; +import java.text.ParseException; + +import static kenergeticbot.exceptionhandler.KenergeticBotException.*; + +public class Parser{ + public static Command parseCommand(TaskList taskList, String item) { + String command[] = item.split(" "); + switch (command[0]) { + case ListCommand.COMMAND_WORD: + return new ListCommand(); + case MarkCommand.COMMAND_WORD: + return prepareMark(taskList,item); + case UnmarkCommand.COMMAND_WORD: + return prepareUnmark(taskList,item); + case DeleteCommand.COMMAND_WORD: + return prepareDelete(taskList,item); + case ExitCommand.COMMAND_WORD: + return new ExitCommand(); + default: + try{ + return new AddCommand(item); + } catch (KenergeticBotException e) { + return new IncorrectCommand(e.getMessage()); + } + } + } + + private static Command prepareMark(TaskList taskList, String item) { + try { + final int listIndex = parseArgsAsDisplayedIndex(taskList ,item, "mark"); + return new MarkCommand(listIndex); + } catch (KenergeticBotException e) { + System.out.println(e.getMessage()); + } catch (ParseException e) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MarkCommand.MESSAGE_USAGE)); + } catch (NumberFormatException nfe) { + return new IncorrectCommand(MESSAGE_INVALID_TASK_DISPLAYED_INDEX); + } + return null; + } + + private static Command prepareUnmark(TaskList taskList, String item) { + try { + final int listIndex = parseArgsAsDisplayedIndex(taskList, item, "unmark"); + return new UnmarkCommand(listIndex); + } catch (KenergeticBotException e) { + System.out.println(e.getMessage()); + } catch (ParseException e) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnmarkCommand.MESSAGE_USAGE)); + } catch (NumberFormatException nfe) { + return new IncorrectCommand(MESSAGE_INVALID_TASK_DISPLAYED_INDEX); + } + return null; + } + + private static Command prepareDelete(TaskList taskList, String item) { + try { + final int listIndex = parseArgsAsDisplayedIndex(taskList ,item, "delete"); + return new DeleteCommand(listIndex); + } catch (KenergeticBotException e) { + System.out.println(e.getMessage()); + } catch (ParseException e) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE)); + } catch (NumberFormatException nfe) { + return new IncorrectCommand(MESSAGE_INVALID_TASK_DISPLAYED_INDEX); + } + return null; + } + private static int parseArgsAsDisplayedIndex(TaskList taskList, String item, String command) throws ParseException, NumberFormatException, KenergeticBotException { + String formattedString = item.replace(command, "").trim(); + int listIndex = Integer.parseInt(formattedString); + if (formattedString.isEmpty()) { + throw new KenergeticBotException(COMMAND_TYPO_NO_NUMBER); + } else if (listIndex > taskList.getSize()) { + throw new KenergeticBotException(OUT_OF_RANGE); + } + return listIndex; + } +} diff --git a/src/main/java/kenergeticbot/command/UnmarkCommand.java b/src/main/java/kenergeticbot/command/UnmarkCommand.java new file mode 100644 index 000000000..8f5d20769 --- /dev/null +++ b/src/main/java/kenergeticbot/command/UnmarkCommand.java @@ -0,0 +1,35 @@ +package kenergeticbot.command; + +import kenergeticbot.TaskList; +import kenergeticbot.exceptionhandler.KenergeticBotException; +import kenergeticbot.task.Task; +import kenergeticbot.ui.TextUi; + +import java.util.ArrayList; + +import static kenergeticbot.exceptionhandler.KenergeticBotException.*; + +public class UnmarkCommand extends Command { + public static final String COMMAND_WORD = "unmark"; + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Marks the task identified by the index number used in the last task listing as not done.\n" + + "Parameters: INDEX\n" + + "Example: " + COMMAND_WORD + " 1"; + private final int listIndex; + public UnmarkCommand(int listIndex) { + this.listIndex = listIndex; + } + public static void checkValid(String item) throws KenergeticBotException { + String formattedString = item.replace("unmark", "").trim(); + if (formattedString.isEmpty()) { + throw new KenergeticBotException(COMMAND_TYPO_NO_NUMBER); + } + } + public void execute(TaskList taskList) { + TextUi.printLine(); + System.out.println(" OK, I've marked this task as not done yet:"); + taskList.getTask(listIndex - 1).unmark(); + System.out.printf(" %s\n", taskList.getTask(listIndex - 1)); + TextUi.printLine(); + } +} diff --git a/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java index a2ef3dc9a..9108bac4e 100644 --- a/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java +++ b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java @@ -11,10 +11,14 @@ public class KenergeticBotException extends Exception { public static final String EVENT_MISSING_END = " ⏳ OOPS!!! The end time of a event cannot be empty."; public static final String EVENT_MISSING_START_INTERMEDIATE = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty. Please use the command /from"; public static final String EVENT_MISSING_END_INTERMEDIATE = " ⏳ OOPS!!! The end time of a event cannot be empty. Please use the command /to"; + public static final String OUT_OF_RANGE = " OOPS!!! Looks like you tried on an item not in the list"; public static final String COMMAND_TYPO = " ¯\\_(ツ)_/¯ OOPS!!! There may be a typo in the command used."; + public static final String COMMAND_TYPO_NO_NUMBER = COMMAND_TYPO + " Please use the command with an integer number"; public static final String COMMAND_TYPO_DEADLINE_BY = COMMAND_TYPO + " Please use the command /by"; public static final String COMMAND_TYPO_EVENT_FROM = COMMAND_TYPO + " Please use the command /from"; public static final String COMMAND_TYPO_EVENT_TO = COMMAND_TYPO + " Please use the command /to"; + public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n"; + public static final String MESSAGE_INVALID_TASK_DISPLAYED_INDEX = "The task index provided is invalid"; public KenergeticBotException(String errMSG) { super(errMSG); diff --git a/src/main/java/kenergeticbot/fileaccess/Save.java b/src/main/java/kenergeticbot/fileaccess/Storage.java similarity index 64% rename from src/main/java/kenergeticbot/fileaccess/Save.java rename to src/main/java/kenergeticbot/fileaccess/Storage.java index 41b5ce071..a8ac39eab 100644 --- a/src/main/java/kenergeticbot/fileaccess/Save.java +++ b/src/main/java/kenergeticbot/fileaccess/Storage.java @@ -1,5 +1,6 @@ package kenergeticbot.fileaccess; +import kenergeticbot.TaskList; import kenergeticbot.task.Deadline; import kenergeticbot.task.Event; import kenergeticbot.task.Task; @@ -12,11 +13,17 @@ import java.util.ArrayList; import java.util.Scanner; -public abstract class Save { +public class Storage { - public static final String filePath = "data/KenergeticBot.txt"; + protected String filePath; public static final String folderPath = "data"; - public static void checkFileExist() { + + public Storage(String filePath) { + this.filePath = filePath; + initializeStorage(filePath); + } + + public void initializeStorage(String filePath) { File f = new File(filePath); checkFolderExist(); try { @@ -35,7 +42,7 @@ public static void checkFileExist() { System.out.println("is Directory?: " + f.isDirectory()); } - public static void checkFolderExist() { + public void checkFolderExist() { File f = new File(folderPath); if (f.mkdir()) { System.out.println("Directory is created"); @@ -47,15 +54,16 @@ public static void checkFolderExist() { System.out.println("is Directory?: " + f.isDirectory()); } - public static void loadPreviousList (ArrayList taskList) { + public void loadPreviousList(TaskList taskList) { try { + System.out.println("Loading previous list"); readFromFile(filePath, taskList); } catch (FileNotFoundException e) { System.out.println("An error occurred." + e.getMessage()); } } - public static void readFromFile (String filePath, ArrayList taskList) throws FileNotFoundException { + public void readFromFile(String filePath, TaskList taskList) throws FileNotFoundException { File f = new File(filePath); // create a File for the given file path if (f.length() == 0) { return; @@ -73,36 +81,36 @@ public static void readFromFile (String filePath, ArrayList taskList) thro String taskMark = taskVariables[1]; String taskDescription = taskVariables[2]; - switch (taskType) { - case "T" : - Task previousTodo = new Todo(taskDescription, "[T]"); - taskList.add(previousTodo); - break; - case "D" : - String taskDeadline = taskVariables[3]; - Task previousDeadline = new Deadline(taskDescription, "[D]", taskDeadline); - taskList.add(previousDeadline); - break; - case "E" : - String taskEventDateTime = taskVariables[3]; - Task previousEvent = new Event(taskDescription, "[E]", taskEventDateTime); - taskList.add(previousEvent); - break; - default : - throw new IllegalStateException("Unexpected value: " + taskType); + case "T" : + Task previousTodo = new Todo(taskDescription); + taskList.add(previousTodo); + System.out.println("adding:" + previousTodo); + break; + case "D" : + String taskDeadline = taskVariables[3]; + Task previousDeadline = new Deadline(taskDescription, taskDeadline); + taskList.add(previousDeadline); + System.out.println("adding:" + previousDeadline); + break; + case "E" : + String taskEventDateTime = taskVariables[3]; + Task previousEvent = new Event(taskDescription, taskEventDateTime); + taskList.add(previousEvent); + System.out.println("adding:" + previousEvent); + break; + default : + throw new IllegalStateException("Unexpected value: " + taskType); } - System.out.println(taskDescription + " added to list"); } try { new FileWriter(filePath, false).close(); } catch (IOException e) { System.out.println(e.getMessage()); } - } - public static void writeToFile(String textToAdd) { + public void writeToFile(String textToAdd) { File f = new File(filePath); if (f.length() == 0) { try { @@ -125,11 +133,10 @@ public static void writeToFile(String textToAdd) { } } - public static void saveList(ArrayList taskList) { - checkFileExist(); - for (Task task : taskList) { - System.out.println(task); - Save.writeToFile(task.printTaskToSave()); + public void saveList(TaskList taskList) { + for (int i = 0; i < taskList.getSize(); i++) { + System.out.println(taskList.getTask(i)); + writeToFile(taskList.getTask(i).printTaskToSave()); } } } diff --git a/src/main/java/kenergeticbot/task/Deadline.java b/src/main/java/kenergeticbot/task/Deadline.java index 8e65d75ce..d14d40346 100644 --- a/src/main/java/kenergeticbot/task/Deadline.java +++ b/src/main/java/kenergeticbot/task/Deadline.java @@ -2,9 +2,10 @@ public class Deadline extends Task { protected String deadline; - public Deadline(String description, String taskType, String deadline) { - super(description, taskType); + public Deadline(String description, String deadline) { + super(description); this.deadline = deadline; + this.taskType = "[D]"; } public String getDeadline() { diff --git a/src/main/java/kenergeticbot/task/Event.java b/src/main/java/kenergeticbot/task/Event.java index d8678a21e..35de50761 100644 --- a/src/main/java/kenergeticbot/task/Event.java +++ b/src/main/java/kenergeticbot/task/Event.java @@ -2,9 +2,10 @@ public class Event extends Task { protected String dateTime; - public Event(String description, String taskType, String dateTime) { - super(description, taskType); + public Event(String description, String dateTime) { + super(description); this.dateTime = dateTime; + this.taskType = "[E]"; } public String toString() { diff --git a/src/main/java/kenergeticbot/task/Task.java b/src/main/java/kenergeticbot/task/Task.java index 1ac54eab2..4e92219f6 100644 --- a/src/main/java/kenergeticbot/task/Task.java +++ b/src/main/java/kenergeticbot/task/Task.java @@ -4,10 +4,9 @@ public class Task { protected boolean isDone; protected String taskType; - public Task(String description, String taskType) { + public Task(String description) { this.description = description; this.isDone = false; - this.taskType = taskType; } public String getStatusIcon() { diff --git a/src/main/java/kenergeticbot/task/Todo.java b/src/main/java/kenergeticbot/task/Todo.java index b3f3e6141..3075a503e 100644 --- a/src/main/java/kenergeticbot/task/Todo.java +++ b/src/main/java/kenergeticbot/task/Todo.java @@ -1,8 +1,9 @@ package kenergeticbot.task; public class Todo extends Task { - public Todo(String description, String taskType) { - super(description, taskType); + public Todo(String description) { + super(description); + this.taskType = "[T]"; } public String toString() { diff --git a/src/main/java/kenergeticbot/command/CommonMessages.java b/src/main/java/kenergeticbot/ui/TextUi.java similarity index 58% rename from src/main/java/kenergeticbot/command/CommonMessages.java rename to src/main/java/kenergeticbot/ui/TextUi.java index 07021283e..5d537a8d7 100644 --- a/src/main/java/kenergeticbot/command/CommonMessages.java +++ b/src/main/java/kenergeticbot/ui/TextUi.java @@ -1,15 +1,29 @@ -package kenergeticbot.command; +package kenergeticbot.ui; +import kenergeticbot.TaskList; import kenergeticbot.task.Task; +import java.io.InputStream; +import java.io.PrintStream; import java.util.ArrayList; +import java.util.Scanner; -public class CommonMessages { +public class TextUi { public static String SEPARATING_LINE = " ____________________________________________________________"; + private final Scanner in; + private final PrintStream out; public static void printLine() { System.out.println(SEPARATING_LINE); } + + public TextUi() { + this(System.in, System.out); + } + public TextUi(InputStream in, PrintStream out) { + this.in = new Scanner(in); + this.out = out; + } public static void printGreetingMessage() { printLine(); System.out.println(" Hello! I'm KenergeticBot"); @@ -23,9 +37,13 @@ public static void printExitMessage() { printLine(); } - public static void printAddedTaskMessage(ArrayList taskList, Task newTask) { + public static void printAddedTaskMessage(TaskList taskList, Task newTask) { System.out.println(" Got it. I've added this task:"); System.out.println(" " + newTask); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); + System.out.printf(" Now you have %d tasks in the list.\n", taskList.getSize()); + } + + public static void printDeleteTaskMessage (TaskList taskList) { + } } diff --git a/text-ui-test/data/KenergeticBot.txt b/text-ui-test/data/KenergeticBot.txt new file mode 100644 index 000000000..e69de29bb From e8b4ef497247f7f20715657e760850fc8fe561f8 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Mon, 2 Oct 2023 22:09:16 +0800 Subject: [PATCH 27/46] Add MoreOOP for TextUi --- .gitignore | 1 + src/main/java/KenergeticBot.java | 23 +++-- .../kenergeticbot/{command => }/Parser.java | 84 +++++++++++++------ src/main/java/kenergeticbot/TaskList.java | 7 +- .../kenergeticbot/command/AddCommand.java | 42 ++++++---- .../java/kenergeticbot/command/Command.java | 10 ++- .../kenergeticbot/command/DeleteCommand.java | 25 ++---- .../kenergeticbot/command/ExitCommand.java | 7 +- .../command/IncorrectCommand.java | 11 ++- .../kenergeticbot/command/ListCommand.java | 10 ++- .../kenergeticbot/command/MarkCommand.java | 20 ++--- .../kenergeticbot/command/UnmarkCommand.java | 23 ++--- .../java/kenergeticbot/common/Messages.java | 16 ++++ .../kenergeticbot/fileaccess/Storage.java | 26 +++++- src/main/java/kenergeticbot/ui/TextUi.java | 77 ++++++++++++----- text-ui-test/data/KenergeticBot.txt | 16 ++++ text-ui-test/input.txt | 1 + 17 files changed, 261 insertions(+), 138 deletions(-) rename src/main/java/kenergeticbot/{command => }/Parser.java (50%) create mode 100644 src/main/java/kenergeticbot/common/Messages.java diff --git a/.gitignore b/.gitignore index 9654ef457..e9ad70d95 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT /data/ +text-ui-test/data/KenergeticBot.txt diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index 804db5451..a4a117c3e 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -1,11 +1,9 @@ import kenergeticbot.TaskList; import kenergeticbot.command.Command; -import java.util.Scanner; - import kenergeticbot.fileaccess.Storage; import kenergeticbot.ui.TextUi; -import kenergeticbot.command.Parser; +import kenergeticbot.Parser; import kenergeticbot.command.ExitCommand; @@ -21,30 +19,31 @@ public static void main(String[] args) { new KenergeticBot().run(); } - private void start(String filePath, TaskList taskList) { + private void start(String filePath, TaskList taskList, TextUi ui) { this.storage = new Storage(filePath); storage.loadPreviousList(taskList); - TextUi.printGreetingMessage(); + ui.printGreetingMessage(); } public void run() { this.taskList = new TaskList(); - start(filePath, taskList); + this.ui = new TextUi(); + start(filePath, taskList, ui); runCommandLoopUntilExitCommand(); - exit(); + exit(ui); } public void runCommandLoopUntilExitCommand() { - Scanner input = new Scanner(System.in); + do { - String item = input.nextLine(); + String item = ui.getUserCommand(); Command c = Parser.parseCommand(taskList, item); - c.execute(taskList); + c.execute(taskList, ui); } while (!ExitCommand.isExit()); storage.saveList(taskList); } - private static void exit() { - TextUi.printExitMessage(); + private static void exit(TextUi ui) { + ui.printExitMessage(); System.exit(0); } } diff --git a/src/main/java/kenergeticbot/command/Parser.java b/src/main/java/kenergeticbot/Parser.java similarity index 50% rename from src/main/java/kenergeticbot/command/Parser.java rename to src/main/java/kenergeticbot/Parser.java index 4c5f99b29..05c2942e8 100644 --- a/src/main/java/kenergeticbot/command/Parser.java +++ b/src/main/java/kenergeticbot/Parser.java @@ -1,77 +1,109 @@ -package kenergeticbot.command; +package kenergeticbot; -import kenergeticbot.TaskList; +import kenergeticbot.command.*; import kenergeticbot.exceptionhandler.KenergeticBotException; import java.text.ParseException; import static kenergeticbot.exceptionhandler.KenergeticBotException.*; public class Parser{ - public static Command parseCommand(TaskList taskList, String item) { - String command[] = item.split(" "); - switch (command[0]) { + + /** + * Parses user input into command for execution. + * + * @param userInput full user input string + * @return the command based on the user input + * @throws KenergeticBotException if input does not match any known command + */ + public static Command parseCommand(TaskList taskList, String userInput) { + String command[] = userInput.split(" "); + switch (command[0].toLowerCase()) { case ListCommand.COMMAND_WORD: return new ListCommand(); case MarkCommand.COMMAND_WORD: - return prepareMark(taskList,item); + return prepareMark(taskList,userInput); case UnmarkCommand.COMMAND_WORD: - return prepareUnmark(taskList,item); + return prepareUnmark(taskList,userInput); case DeleteCommand.COMMAND_WORD: - return prepareDelete(taskList,item); + return prepareDelete(taskList,userInput); case ExitCommand.COMMAND_WORD: return new ExitCommand(); default: try{ - return new AddCommand(item); + return new AddCommand(userInput); } catch (KenergeticBotException e) { return new IncorrectCommand(e.getMessage()); } } } - private static Command prepareMark(TaskList taskList, String item) { + /** + * Parses arguments in the context of the mark task command. + * + * @param userInput full command args string + * @return the prepared command + */ + private static Command prepareMark(TaskList taskList, String userInput) { try { - final int listIndex = parseArgsAsDisplayedIndex(taskList ,item, "mark"); + final int listIndex = parseArgsAsDisplayedIndex(taskList ,userInput, "mark"); return new MarkCommand(listIndex); } catch (KenergeticBotException e) { - System.out.println(e.getMessage()); + return new IncorrectCommand(e.getMessage()); } catch (ParseException e) { - return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MarkCommand.MESSAGE_USAGE)); + return new IncorrectCommand(MESSAGE_INVALID_COMMAND_FORMAT + MarkCommand.MESSAGE_USAGE); } catch (NumberFormatException nfe) { return new IncorrectCommand(MESSAGE_INVALID_TASK_DISPLAYED_INDEX); } - return null; } - private static Command prepareUnmark(TaskList taskList, String item) { + /** + * Parses arguments in the context of the unmark task command. + * + * @param userInput full command args string + * @return the prepared command + */ + private static Command prepareUnmark(TaskList taskList, String userInput) { try { - final int listIndex = parseArgsAsDisplayedIndex(taskList, item, "unmark"); + final int listIndex = parseArgsAsDisplayedIndex(taskList, userInput, "unmark"); return new UnmarkCommand(listIndex); } catch (KenergeticBotException e) { - System.out.println(e.getMessage()); + return new IncorrectCommand(e.getMessage()); } catch (ParseException e) { - return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnmarkCommand.MESSAGE_USAGE)); + return new IncorrectCommand(MESSAGE_INVALID_COMMAND_FORMAT + UnmarkCommand.MESSAGE_USAGE); } catch (NumberFormatException nfe) { return new IncorrectCommand(MESSAGE_INVALID_TASK_DISPLAYED_INDEX); } - return null; } - private static Command prepareDelete(TaskList taskList, String item) { + /** + * Parses arguments in the context of the delete task command. + * + * @param userInput full command args string + * @return the prepared command + */ + private static Command prepareDelete(TaskList taskList, String userInput) { try { - final int listIndex = parseArgsAsDisplayedIndex(taskList ,item, "delete"); + final int listIndex = parseArgsAsDisplayedIndex(taskList, userInput, "delete"); return new DeleteCommand(listIndex); } catch (KenergeticBotException e) { - System.out.println(e.getMessage()); + return new IncorrectCommand(e.getMessage()); } catch (ParseException e) { - return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE)); + return new IncorrectCommand(MESSAGE_INVALID_COMMAND_FORMAT + DeleteCommand.MESSAGE_USAGE); } catch (NumberFormatException nfe) { return new IncorrectCommand(MESSAGE_INVALID_TASK_DISPLAYED_INDEX); } - return null; } - private static int parseArgsAsDisplayedIndex(TaskList taskList, String item, String command) throws ParseException, NumberFormatException, KenergeticBotException { - String formattedString = item.replace(command, "").trim(); + + /** + * Parses the given arguments string to identify task index number. + * + * @param userInput arguments string to parse as index number + * @return the parsed index number + * @throws ParseException if no region of the args string could be found for the index + * @throws NumberFormatException the args string region is not a valid number + */ + private static int parseArgsAsDisplayedIndex(TaskList taskList, String userInput, String command) throws ParseException, NumberFormatException, KenergeticBotException { + String formattedString = userInput.replace(command, "").trim(); int listIndex = Integer.parseInt(formattedString); if (formattedString.isEmpty()) { throw new KenergeticBotException(COMMAND_TYPO_NO_NUMBER); diff --git a/src/main/java/kenergeticbot/TaskList.java b/src/main/java/kenergeticbot/TaskList.java index a3d0c6d05..df8818274 100644 --- a/src/main/java/kenergeticbot/TaskList.java +++ b/src/main/java/kenergeticbot/TaskList.java @@ -1,9 +1,11 @@ package kenergeticbot; import kenergeticbot.task.Task; - import java.util.ArrayList; +/** + * Contains the task list e.g., it has operations to add/delete tasks in the list + */ public class TaskList { protected static ArrayList taskList; public TaskList() { @@ -18,9 +20,6 @@ public Task getTask(int taskID) { public void remove(int taskID) { taskList.remove(taskID); } - public String toString() { - return "A"; - } public void add(Task task) { taskList.add(task); diff --git a/src/main/java/kenergeticbot/command/AddCommand.java b/src/main/java/kenergeticbot/command/AddCommand.java index 008859b01..6ad116013 100644 --- a/src/main/java/kenergeticbot/command/AddCommand.java +++ b/src/main/java/kenergeticbot/command/AddCommand.java @@ -7,14 +7,21 @@ import kenergeticbot.task.Task; import kenergeticbot.task.Todo; import kenergeticbot.ui.TextUi; +import static kenergeticbot.common.Messages.SEPARATING_LINE; import static kenergeticbot.exceptionhandler.KenergeticBotException.*; public class AddCommand extends Command { private static String item = null; - private String taskType; + private final String taskType; + + /** + * Convenience constructor using input string. + * + * @throws KenergeticBotException if input does not match any known command + */ public AddCommand(String item) throws KenergeticBotException { - this.item = item; + AddCommand.item = item; if (BooleanChecks.checkTextForTodo(item)) { this.taskType = "T"; } else if (BooleanChecks.checkTextForDeadline(item)) { @@ -22,51 +29,50 @@ public AddCommand(String item) throws KenergeticBotException { } else if (BooleanChecks.checkTextForEvent(item)) { this.taskType = "E"; }else { - throw new KenergeticBotException(INVALID_COMMAND); //throws exception when input does not match any known command + throw new KenergeticBotException(INVALID_COMMAND); } } - public void execute(TaskList taskList){ - TextUi.printLine(); + @Override + public void execute(TaskList taskList, TextUi ui){ switch (taskType) { case "T": try { - addTodo(taskList, item); + addTodo(taskList, item, ui); } catch (KenergeticBotException e) { //throws exception when the todo command encounters a problem, the error message is tagged to the thrown exception - System.out.println(e.getMessage()); + ui.showToUser(SEPARATING_LINE, e.getMessage(), SEPARATING_LINE); } break; case "D": try { - addDeadline(taskList, item); + addDeadline(taskList, item, ui); } catch (KenergeticBotException e) { //throws exception when the deadline command encounters a problem, the error message is tagged to the thrown exception - System.out.println(e.getMessage()); + ui.showToUser(SEPARATING_LINE, e.getMessage(), SEPARATING_LINE); } break; case "E": try { - addEvent(taskList, item);; + addEvent(taskList, item, ui); } catch (KenergeticBotException e) { //throws exception when the event command encounters a problem, the error message is tagged to the thrown exception - System.out.println(e.getMessage()); + ui.showToUser(SEPARATING_LINE, e.getMessage(), SEPARATING_LINE); } break; } - TextUi.printLine(); } //Creates a "Todo" object and adds to the taskList - public static void addTodo(TaskList taskList ,String item) throws KenergeticBotException { + public static void addTodo(TaskList taskList ,String item, TextUi ui) throws KenergeticBotException { String formattedString = item.replace("todo", "").trim(); if (formattedString.isEmpty()) { throw new KenergeticBotException(TODO_MISSING_DESCRIPTION); } Task newTask = new Todo(formattedString); taskList.add(newTask); - TextUi.printAddedTaskMessage(taskList, newTask); + ui.printAddedTaskMessage(taskList, newTask); } //Creates a "Deadline" object and adds to the taskList - public static void addDeadline(TaskList taskList, String item) throws KenergeticBotException { + public static void addDeadline(TaskList taskList, String item, TextUi ui) throws KenergeticBotException { String[] formattedString = item.replace("deadline", "").trim().split("/"); switch (formattedString.length) { //no default case needed since finding exceptions case 1: //no break; is needed since throwing exception @@ -85,11 +91,11 @@ public static void addDeadline(TaskList taskList, String item) throws Kenergetic String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; Task newTask = new Deadline(formattedString[0], deadlineDate); taskList.add(newTask); - TextUi.printAddedTaskMessage(taskList, newTask); + ui.printAddedTaskMessage(taskList, newTask); } //Creates a "Event" object and adds to the taskList - public static void addEvent(TaskList taskList, String item) throws KenergeticBotException { + public static void addEvent(TaskList taskList, String item, TextUi ui) throws KenergeticBotException { String[] formattedString = item.replace("event", "").trim().split("/"); switch(formattedString.length) { //no default case needed since finding exceptions case 1: //no break; is needed since throwing exception @@ -118,6 +124,6 @@ public static void addEvent(TaskList taskList, String item) throws KenergeticBot String dateTime = "(" + eventFrom + eventTo + ")"; Task newTask = new Event(formattedString[0], dateTime); taskList.add(newTask); - TextUi.printAddedTaskMessage(taskList, newTask); + ui.printAddedTaskMessage(taskList, newTask); } } diff --git a/src/main/java/kenergeticbot/command/Command.java b/src/main/java/kenergeticbot/command/Command.java index ec928d637..7ee619e5a 100644 --- a/src/main/java/kenergeticbot/command/Command.java +++ b/src/main/java/kenergeticbot/command/Command.java @@ -1,9 +1,17 @@ package kenergeticbot.command; import kenergeticbot.TaskList; +import kenergeticbot.ui.TextUi; +/** + * Represents an executable command. + */ public class Command { - public void execute(TaskList taskList) { + + /** + * Executes the command and returns the result. + */ + public void execute(TaskList taskList, TextUi ui) { throw new UnsupportedOperationException("This method is to be implemented by child classes"); }; } diff --git a/src/main/java/kenergeticbot/command/DeleteCommand.java b/src/main/java/kenergeticbot/command/DeleteCommand.java index 2c262d01d..2155b5af1 100644 --- a/src/main/java/kenergeticbot/command/DeleteCommand.java +++ b/src/main/java/kenergeticbot/command/DeleteCommand.java @@ -1,14 +1,11 @@ package kenergeticbot.command; import kenergeticbot.TaskList; -import kenergeticbot.exceptionhandler.KenergeticBotException; -import kenergeticbot.task.Task; import kenergeticbot.ui.TextUi; -import java.util.ArrayList; - -import static kenergeticbot.exceptionhandler.KenergeticBotException.*; - +/** + * Deletes a task identified using it's last displayed index from the task list. + */ public class DeleteCommand extends Command { public static final String COMMAND_WORD = "delete"; public static final String MESSAGE_USAGE = COMMAND_WORD @@ -19,19 +16,11 @@ public class DeleteCommand extends Command { public DeleteCommand(int listIndex) { this.listIndex = listIndex; } - public static void checkValid (String item) throws KenergeticBotException { - String formattedString = item.replace("delete", "").trim(); - if (formattedString.isEmpty()) { - throw new KenergeticBotException(COMMAND_TYPO_NO_NUMBER); - } - } - public void execute(TaskList taskList) { - TextUi.printLine(); - System.out.println(" Noted. I've removed this task:"); - System.out.println(" " + taskList.getTask(listIndex - 1)); + + @Override + public void execute(TaskList taskList, TextUi ui) { + ui.printDeleteTaskMessage(taskList, listIndex); taskList.remove(listIndex - 1); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.getSize()); - TextUi.printLine(); } } diff --git a/src/main/java/kenergeticbot/command/ExitCommand.java b/src/main/java/kenergeticbot/command/ExitCommand.java index 64a39f65f..199230952 100644 --- a/src/main/java/kenergeticbot/command/ExitCommand.java +++ b/src/main/java/kenergeticbot/command/ExitCommand.java @@ -1,12 +1,17 @@ package kenergeticbot.command; import kenergeticbot.TaskList; +import kenergeticbot.ui.TextUi; +/** + * Terminates the program. + */ public class ExitCommand extends Command{ protected static boolean isExit = false; public static final String COMMAND_WORD = "bye"; - public void execute(TaskList taskList) { + @Override + public void execute(TaskList taskList, TextUi ui) { isExit = true; } diff --git a/src/main/java/kenergeticbot/command/IncorrectCommand.java b/src/main/java/kenergeticbot/command/IncorrectCommand.java index 2547a5ce6..982d626ff 100644 --- a/src/main/java/kenergeticbot/command/IncorrectCommand.java +++ b/src/main/java/kenergeticbot/command/IncorrectCommand.java @@ -1,7 +1,12 @@ package kenergeticbot.command; import kenergeticbot.TaskList; +import kenergeticbot.ui.TextUi; +import static kenergeticbot.common.Messages.SEPARATING_LINE; +/** + * Represents an incorrect command. Upon execution, produces some feedback to the user. + */ public class IncorrectCommand extends Command{ public final String feedbackToUser; @@ -9,8 +14,8 @@ public IncorrectCommand(String feedbackToUser) { this.feedbackToUser = feedbackToUser; } - - public void execute(TaskList taskList) { - System.out.println(feedbackToUser); + @Override + public void execute(TaskList taskList, TextUi ui) { + ui.showToUser(SEPARATING_LINE, feedbackToUser, SEPARATING_LINE); } } diff --git a/src/main/java/kenergeticbot/command/ListCommand.java b/src/main/java/kenergeticbot/command/ListCommand.java index ee96a1f76..f67f62c7e 100644 --- a/src/main/java/kenergeticbot/command/ListCommand.java +++ b/src/main/java/kenergeticbot/command/ListCommand.java @@ -3,14 +3,18 @@ import kenergeticbot.TaskList; import kenergeticbot.ui.TextUi; +/** + * Lists all tasks in the task list to the user. + */ public class ListCommand extends Command { public static final String COMMAND_WORD = "list"; - public void execute(TaskList taskList) { - TextUi.printLine(); + @Override + public void execute(TaskList taskList, TextUi ui) { + ui.printLine(); System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < taskList.getSize(); i++) { System.out.printf(" %d.%s\n", i+1, taskList.getTask(i)); } - TextUi.printLine(); + ui.printLine(); } } diff --git a/src/main/java/kenergeticbot/command/MarkCommand.java b/src/main/java/kenergeticbot/command/MarkCommand.java index 06cb3df7a..d975545a1 100644 --- a/src/main/java/kenergeticbot/command/MarkCommand.java +++ b/src/main/java/kenergeticbot/command/MarkCommand.java @@ -1,11 +1,11 @@ package kenergeticbot.command; import kenergeticbot.TaskList; -import kenergeticbot.exceptionhandler.KenergeticBotException; import kenergeticbot.ui.TextUi; -import static kenergeticbot.exceptionhandler.KenergeticBotException.*; - +/** + * Marks a task identified using it's last displayed index from the task list. + */ public class MarkCommand extends Command { public static final String COMMAND_WORD = "mark"; @@ -17,17 +17,9 @@ public class MarkCommand extends Command { public MarkCommand(int listIndex) { this.listIndex = listIndex; } - public static void checkValid(String item) throws KenergeticBotException { - String formattedString = item.replace("mark", "").trim(); - if (formattedString.isEmpty()) { - throw new KenergeticBotException(COMMAND_TYPO_NO_NUMBER); - } - } - public void execute(TaskList taskList){ - TextUi.printLine(); - System.out.println(" Nice! I've marked this task as done:"); + @Override + public void execute(TaskList taskList, TextUi ui){ taskList.getTask(listIndex - 1).mark(); - System.out.printf(" %s\n", taskList.getTask(listIndex - 1)); - TextUi.printLine(); + ui.printMarkTaskMessage(taskList, listIndex); } } diff --git a/src/main/java/kenergeticbot/command/UnmarkCommand.java b/src/main/java/kenergeticbot/command/UnmarkCommand.java index 8f5d20769..d5328a6e8 100644 --- a/src/main/java/kenergeticbot/command/UnmarkCommand.java +++ b/src/main/java/kenergeticbot/command/UnmarkCommand.java @@ -1,14 +1,11 @@ package kenergeticbot.command; import kenergeticbot.TaskList; -import kenergeticbot.exceptionhandler.KenergeticBotException; -import kenergeticbot.task.Task; import kenergeticbot.ui.TextUi; -import java.util.ArrayList; - -import static kenergeticbot.exceptionhandler.KenergeticBotException.*; - +/** + * Unmarks a task identified using it's last displayed index from the task list. + */ public class UnmarkCommand extends Command { public static final String COMMAND_WORD = "unmark"; public static final String MESSAGE_USAGE = COMMAND_WORD @@ -19,17 +16,9 @@ public class UnmarkCommand extends Command { public UnmarkCommand(int listIndex) { this.listIndex = listIndex; } - public static void checkValid(String item) throws KenergeticBotException { - String formattedString = item.replace("unmark", "").trim(); - if (formattedString.isEmpty()) { - throw new KenergeticBotException(COMMAND_TYPO_NO_NUMBER); - } - } - public void execute(TaskList taskList) { - TextUi.printLine(); - System.out.println(" OK, I've marked this task as not done yet:"); + @Override + public void execute(TaskList taskList, TextUi ui) { taskList.getTask(listIndex - 1).unmark(); - System.out.printf(" %s\n", taskList.getTask(listIndex - 1)); - TextUi.printLine(); + ui.printUnmarkTaskMessage(taskList, listIndex); } } diff --git a/src/main/java/kenergeticbot/common/Messages.java b/src/main/java/kenergeticbot/common/Messages.java new file mode 100644 index 000000000..6d71296de --- /dev/null +++ b/src/main/java/kenergeticbot/common/Messages.java @@ -0,0 +1,16 @@ +package kenergeticbot.common; + +/** + * Container for user visible messages. + */ +public class Messages { + public static String SEPARATING_LINE = " ____________________________________________________________"; + public static String GREETING_MESSAGE = " Hello! I'm KenergeticBot\n" + " What can I do for you?"; + public static String GOODBYE_MESSAGE = " Bye. Hope to see you again soon!"; + public static String ADD_TASK_MESSAGE = " Got it. I've added this task:"; + public static String DELETE_TASK_MESSAGE = " Noted. I've removed this task:"; + public static String MARK_TASK_MESSAGE = " Nice! I've marked this task as done:"; + public static String UNMARK_TASK_MESSAGE = " OK, I've marked this task as not done yet:"; + public static String TASK_IN_LIST_MESSAGE_PART_1 = " Now you have "; + public static String TASK_IN_LIST_MESSAGE_PART_2 = " tasks in the list."; +} diff --git a/src/main/java/kenergeticbot/fileaccess/Storage.java b/src/main/java/kenergeticbot/fileaccess/Storage.java index a8ac39eab..58082e482 100644 --- a/src/main/java/kenergeticbot/fileaccess/Storage.java +++ b/src/main/java/kenergeticbot/fileaccess/Storage.java @@ -13,6 +13,9 @@ import java.util.ArrayList; import java.util.Scanner; +/** + * Deals with loading tasks from the file and saving tasks in the file + */ public class Storage { protected String filePath; @@ -20,10 +23,13 @@ public class Storage { public Storage(String filePath) { this.filePath = filePath; - initializeStorage(filePath); + initializeStorage(); } - public void initializeStorage(String filePath) { + /** + * Checks if the storage file exists, creates the file if not + */ + public void initializeStorage() { File f = new File(filePath); checkFolderExist(); try { @@ -42,6 +48,9 @@ public void initializeStorage(String filePath) { System.out.println("is Directory?: " + f.isDirectory()); } + /** + * Checks if the storage folder to store the storage file exists, creates the folder if not + */ public void checkFolderExist() { File f = new File(folderPath); if (f.mkdir()) { @@ -54,6 +63,10 @@ public void checkFolderExist() { System.out.println("is Directory?: " + f.isDirectory()); } + /** + * Loads the storage data file into a TaskList object. + * @throws FileNotFoundException if unable to locate storage file + */ public void loadPreviousList(TaskList taskList) { try { System.out.println("Loading previous list"); @@ -63,6 +76,9 @@ public void loadPreviousList(TaskList taskList) { } } + /** + * Access the storage data file to create the appropriate Task object + */ public void readFromFile(String filePath, TaskList taskList) throws FileNotFoundException { File f = new File(filePath); // create a File for the given file path if (f.length() == 0) { @@ -110,6 +126,9 @@ public void readFromFile(String filePath, TaskList taskList) throws FileNotFound } } + /** + * Writes a String into a data file for storage. + */ public void writeToFile(String textToAdd) { File f = new File(filePath); if (f.length() == 0) { @@ -133,6 +152,9 @@ public void writeToFile(String textToAdd) { } } + /** + * Saves the TaskList object into a data file for storage. + */ public void saveList(TaskList taskList) { for (int i = 0; i < taskList.getSize(); i++) { System.out.println(taskList.getTask(i)); diff --git a/src/main/java/kenergeticbot/ui/TextUi.java b/src/main/java/kenergeticbot/ui/TextUi.java index 5d537a8d7..691b58713 100644 --- a/src/main/java/kenergeticbot/ui/TextUi.java +++ b/src/main/java/kenergeticbot/ui/TextUi.java @@ -5,17 +5,22 @@ import java.io.InputStream; import java.io.PrintStream; -import java.util.ArrayList; import java.util.Scanner; +import static kenergeticbot.common.Messages.ADD_TASK_MESSAGE; +import static kenergeticbot.common.Messages.DELETE_TASK_MESSAGE; +import static kenergeticbot.common.Messages.GOODBYE_MESSAGE; +import static kenergeticbot.common.Messages.GREETING_MESSAGE; +import static kenergeticbot.common.Messages.MARK_TASK_MESSAGE; +import static kenergeticbot.common.Messages.SEPARATING_LINE; +import static kenergeticbot.common.Messages.TASK_IN_LIST_MESSAGE_PART_1; +import static kenergeticbot.common.Messages.TASK_IN_LIST_MESSAGE_PART_2; +import static kenergeticbot.common.Messages.UNMARK_TASK_MESSAGE; + public class TextUi { - public static String SEPARATING_LINE = " ____________________________________________________________"; private final Scanner in; private final PrintStream out; - public static void printLine() { - System.out.println(SEPARATING_LINE); - } public TextUi() { this(System.in, System.out); @@ -24,26 +29,60 @@ public TextUi(InputStream in, PrintStream out) { this.in = new Scanner(in); this.out = out; } - public static void printGreetingMessage() { - printLine(); - System.out.println(" Hello! I'm KenergeticBot"); - System.out.println(" What can I do for you?"); - printLine(); + + public void printLine() { + System.out.println(SEPARATING_LINE); } - public static void printExitMessage() { - printLine(); - System.out.println(" Bye. Hope to see you again soon!"); - printLine(); + /** + * Reads the text entered by the user. + * @return command (full line) entered by the user + */ + public String getUserCommand() { + String fullInputLine = in.nextLine(); + return fullInputLine; + } + public void printGreetingMessage() { + showToUser(SEPARATING_LINE, + GREETING_MESSAGE, + SEPARATING_LINE); } - public static void printAddedTaskMessage(TaskList taskList, Task newTask) { - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + newTask); - System.out.printf(" Now you have %d tasks in the list.\n", taskList.getSize()); + public void printExitMessage() { + showToUser(SEPARATING_LINE, + GOODBYE_MESSAGE, + SEPARATING_LINE); } - public static void printDeleteTaskMessage (TaskList taskList) { + public void printAddedTaskMessage(TaskList taskList, Task newTask) { + showToUser(SEPARATING_LINE, + ADD_TASK_MESSAGE, + " " + newTask, + TASK_IN_LIST_MESSAGE_PART_1 + taskList.getSize() + TASK_IN_LIST_MESSAGE_PART_2, + SEPARATING_LINE); + } + + public void printDeleteTaskMessage(TaskList taskList, int listIndex) { + showToUser(SEPARATING_LINE, + DELETE_TASK_MESSAGE, + " " + taskList.getTask(listIndex - 1), + TASK_IN_LIST_MESSAGE_PART_1 + (taskList.getSize() - 1) + TASK_IN_LIST_MESSAGE_PART_2); + } + + public void printMarkTaskMessage(TaskList taskList, int listIndex) { + showToUser(SEPARATING_LINE, MARK_TASK_MESSAGE, + " " + taskList.getTask(listIndex - 1), SEPARATING_LINE); + } + + public void printUnmarkTaskMessage(TaskList taskList, int listIndex) { + showToUser(SEPARATING_LINE, UNMARK_TASK_MESSAGE, + " " + taskList.getTask(listIndex - 1), SEPARATING_LINE); + } + /** Shows message(s) to the user */ + public void showToUser(String... message) { + for (String m : message) { + System.out.println(m); + } } } diff --git a/text-ui-test/data/KenergeticBot.txt b/text-ui-test/data/KenergeticBot.txt index e69de29bb..f86c3841d 100644 --- a/text-ui-test/data/KenergeticBot.txt +++ b/text-ui-test/data/KenergeticBot.txt @@ -0,0 +1,16 @@ +T | 0 | borrow book +T | 0 | borrow book +D | 0 | return book | (by: Sunday) +E | 0 | project meeting | (from: Mon 2pm to: 4pm) +T | 0 | borrow book +D | 0 | return book | (by: Sunday) +E | 0 | project meeting | (from: Mon 2pm to: 4pm) +T | 0 | borrow book +D | 0 | return book | (by: Sunday) +E | 0 | project meeting | (from: Mon 2pm to: 4pm) +T | 0 | borrow book +D | 0 | return book | (by: Sunday) +E | 0 | project meeting | (from: Mon 2pm to: 4pm) +T | 0 | borrow book +D | 0 | return book | (by: Sunday) +E | 0 | project meeting | (from: Mon 2pm to: 4pm) diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 4a99a6c47..a2b94ca37 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -20,4 +20,5 @@ event project meeting /from event project meeting /fr event project meeting event +delete 2 bye \ No newline at end of file From 32f1853ecd5182bd310c86d92fcd64484af3b689 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Tue, 3 Oct 2023 02:29:56 +0800 Subject: [PATCH 28/46] Add Find function --- src/main/java/KenergeticBot.java | 1 - src/main/java/kenergeticbot/Parser.java | 37 +++++++++++++++++++ .../kenergeticbot/command/FindCommand.java | 32 ++++++++++++++++ .../java/kenergeticbot/common/Messages.java | 1 + .../KenergeticBotException.java | 1 + .../kenergeticbot/fileaccess/Storage.java | 10 +++-- .../java/kenergeticbot/task/Deadline.java | 6 +++ src/main/java/kenergeticbot/task/Event.java | 6 +++ src/main/java/kenergeticbot/task/Todo.java | 5 +++ 9 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/main/java/kenergeticbot/command/FindCommand.java diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index a4a117c3e..b36460ad4 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -33,7 +33,6 @@ public void run() { exit(ui); } public void runCommandLoopUntilExitCommand() { - do { String item = ui.getUserCommand(); Command c = Parser.parseCommand(taskList, item); diff --git a/src/main/java/kenergeticbot/Parser.java b/src/main/java/kenergeticbot/Parser.java index 05c2942e8..ace601fe3 100644 --- a/src/main/java/kenergeticbot/Parser.java +++ b/src/main/java/kenergeticbot/Parser.java @@ -28,6 +28,8 @@ public static Command parseCommand(TaskList taskList, String userInput) { return prepareDelete(taskList,userInput); case ExitCommand.COMMAND_WORD: return new ExitCommand(); + case FindCommand.COMMAND_WORD: + return prepareFind(taskList, userInput); default: try{ return new AddCommand(userInput); @@ -94,6 +96,25 @@ private static Command prepareDelete(TaskList taskList, String userInput) { } } + /** + * Parses arguments in the context of the mark task command. + * + * @param userInput full command args string + * @return the prepared command + */ + private static Command prepareFind(TaskList taskList, String userInput) { + try { + final String listKeyword = parseArgsAsDisplayedKeyword(taskList, userInput, "find"); + return new FindCommand(listKeyword); + } catch (KenergeticBotException e) { + return new IncorrectCommand(e.getMessage()); + } catch (ParseException e) { + return new IncorrectCommand(MESSAGE_INVALID_COMMAND_FORMAT + MarkCommand.MESSAGE_USAGE); + } catch (NumberFormatException nfe) { + return new IncorrectCommand(MESSAGE_INVALID_TASK_DISPLAYED_INDEX); + } + } + /** * Parses the given arguments string to identify task index number. * @@ -112,4 +133,20 @@ private static int parseArgsAsDisplayedIndex(TaskList taskList, String userInput } return listIndex; } + + /** + * Parses the given arguments string to identify task keyword. + * + * @param userInput arguments string to parse as keyword + * @return the parsed keyword + * @throws ParseException if no region of the args string could be found for the index + * @throws NumberFormatException the args string region is not a valid number + */ + private static String parseArgsAsDisplayedKeyword(TaskList taskList, String userInput, String command) throws ParseException, NumberFormatException, KenergeticBotException { + String keywordString = userInput.replace(command, "").trim(); + if (keywordString.isEmpty()) { + throw new KenergeticBotException(COMMAND_TYPO_NO_KEYWORD); + } + return keywordString; + } } diff --git a/src/main/java/kenergeticbot/command/FindCommand.java b/src/main/java/kenergeticbot/command/FindCommand.java new file mode 100644 index 000000000..75904f1a8 --- /dev/null +++ b/src/main/java/kenergeticbot/command/FindCommand.java @@ -0,0 +1,32 @@ +package kenergeticbot.command; + +import kenergeticbot.TaskList; +import kenergeticbot.ui.TextUi; + +import static kenergeticbot.common.Messages.FIND_TASK_MESSAGE; +import static kenergeticbot.common.Messages.SEPARATING_LINE; + +/** + * Finds a task identified using a keyword. + */ +public class FindCommand extends Command { + public static final String COMMAND_WORD = "find"; + private final String keyword; + public FindCommand(String keyword) { + this.keyword = "(.*)" + keyword + "(.*)"; + } + + @Override + public void execute(TaskList taskList, TextUi ui) { + ui.showToUser(SEPARATING_LINE, FIND_TASK_MESSAGE); + int j = 1; + for (int i = 0; i < taskList.getSize(); i++) { + String task = String.valueOf(taskList.getTask(i)); + if (task.matches(keyword)) { + ui.showToUser(" " + j + "." + taskList.getTask(i)); + j++; + } + } + ui.showToUser(SEPARATING_LINE); + } +} diff --git a/src/main/java/kenergeticbot/common/Messages.java b/src/main/java/kenergeticbot/common/Messages.java index 6d71296de..c589037ef 100644 --- a/src/main/java/kenergeticbot/common/Messages.java +++ b/src/main/java/kenergeticbot/common/Messages.java @@ -13,4 +13,5 @@ public class Messages { public static String UNMARK_TASK_MESSAGE = " OK, I've marked this task as not done yet:"; public static String TASK_IN_LIST_MESSAGE_PART_1 = " Now you have "; public static String TASK_IN_LIST_MESSAGE_PART_2 = " tasks in the list."; + public static String FIND_TASK_MESSAGE = " Here are the matching tasks in your list:"; } diff --git a/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java index 9108bac4e..081223e0e 100644 --- a/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java +++ b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java @@ -14,6 +14,7 @@ public class KenergeticBotException extends Exception { public static final String OUT_OF_RANGE = " OOPS!!! Looks like you tried on an item not in the list"; public static final String COMMAND_TYPO = " ¯\\_(ツ)_/¯ OOPS!!! There may be a typo in the command used."; public static final String COMMAND_TYPO_NO_NUMBER = COMMAND_TYPO + " Please use the command with an integer number"; + public static final String COMMAND_TYPO_NO_KEYWORD = COMMAND_TYPO + " Please use the command with a keyword"; public static final String COMMAND_TYPO_DEADLINE_BY = COMMAND_TYPO + " Please use the command /by"; public static final String COMMAND_TYPO_EVENT_FROM = COMMAND_TYPO + " Please use the command /from"; public static final String COMMAND_TYPO_EVENT_TO = COMMAND_TYPO + " Please use the command /to"; diff --git a/src/main/java/kenergeticbot/fileaccess/Storage.java b/src/main/java/kenergeticbot/fileaccess/Storage.java index 58082e482..b3ffbad4c 100644 --- a/src/main/java/kenergeticbot/fileaccess/Storage.java +++ b/src/main/java/kenergeticbot/fileaccess/Storage.java @@ -99,19 +99,19 @@ public void readFromFile(String filePath, TaskList taskList) throws FileNotFound switch (taskType) { case "T" : - Task previousTodo = new Todo(taskDescription); + Task previousTodo = new Todo(taskDescription, checkMark(taskMark)); taskList.add(previousTodo); System.out.println("adding:" + previousTodo); break; case "D" : String taskDeadline = taskVariables[3]; - Task previousDeadline = new Deadline(taskDescription, taskDeadline); + Task previousDeadline = new Deadline(taskDescription, taskDeadline, checkMark(taskMark)); taskList.add(previousDeadline); System.out.println("adding:" + previousDeadline); break; case "E" : String taskEventDateTime = taskVariables[3]; - Task previousEvent = new Event(taskDescription, taskEventDateTime); + Task previousEvent = new Event(taskDescription, taskEventDateTime, checkMark(taskMark)); taskList.add(previousEvent); System.out.println("adding:" + previousEvent); break; @@ -161,4 +161,8 @@ public void saveList(TaskList taskList) { writeToFile(taskList.getTask(i).printTaskToSave()); } } + + public boolean checkMark(String item) { + return item.equals("1"); + } } diff --git a/src/main/java/kenergeticbot/task/Deadline.java b/src/main/java/kenergeticbot/task/Deadline.java index d14d40346..78128493c 100644 --- a/src/main/java/kenergeticbot/task/Deadline.java +++ b/src/main/java/kenergeticbot/task/Deadline.java @@ -7,6 +7,12 @@ public Deadline(String description, String deadline) { this.deadline = deadline; this.taskType = "[D]"; } + public Deadline(String description, String deadline, boolean isDone) { + super(description); + this.deadline = deadline; + this.taskType = "[D]"; + this.isDone = isDone; + } public String getDeadline() { return deadline; diff --git a/src/main/java/kenergeticbot/task/Event.java b/src/main/java/kenergeticbot/task/Event.java index 35de50761..67e15e2d0 100644 --- a/src/main/java/kenergeticbot/task/Event.java +++ b/src/main/java/kenergeticbot/task/Event.java @@ -7,6 +7,12 @@ public Event(String description, String dateTime) { this.dateTime = dateTime; this.taskType = "[E]"; } + public Event(String description, String dateTime, boolean isDone) { + super(description); + this.dateTime = dateTime; + this.taskType = "[E]"; + this.isDone = isDone; + } public String toString() { return taskType + super.toString() + dateTime; diff --git a/src/main/java/kenergeticbot/task/Todo.java b/src/main/java/kenergeticbot/task/Todo.java index 3075a503e..16986df36 100644 --- a/src/main/java/kenergeticbot/task/Todo.java +++ b/src/main/java/kenergeticbot/task/Todo.java @@ -5,6 +5,11 @@ public Todo(String description) { super(description); this.taskType = "[T]"; } + public Todo(String description, boolean isDone) { + super(description); + this.taskType = "[T]"; + this.isDone = isDone; + } public String toString() { return taskType + super.toString(); From 96ac6d1c096fc3b6df3482eab9100b54ce86513d Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Wed, 4 Oct 2023 00:52:27 +0800 Subject: [PATCH 29/46] Add Java Documentation --- src/main/java/KenergeticBot.java | 14 +++++++++++--- src/main/java/kenergeticbot/Parser.java | 9 ++++++++- .../exceptionhandler/KenergeticBotException.java | 3 +++ .../java/kenergeticbot/fileaccess/Storage.java | 16 ++++++++-------- src/main/java/kenergeticbot/task/Deadline.java | 4 ++++ src/main/java/kenergeticbot/task/Event.java | 4 ++++ src/main/java/kenergeticbot/task/Task.java | 4 ++++ src/main/java/kenergeticbot/task/Todo.java | 4 ++++ text-ui-test/data/KenergeticBot.txt | 2 ++ 9 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index a4a117c3e..7ae06f9d3 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -1,17 +1,18 @@ import kenergeticbot.TaskList; import kenergeticbot.command.Command; - import kenergeticbot.fileaccess.Storage; import kenergeticbot.ui.TextUi; import kenergeticbot.Parser; import kenergeticbot.command.ExitCommand; - +/** + * Entry point of the KenergeticBot chatbot. + * Initializes the application and starts the interaction with the user. + */ public class KenergeticBot { public static final String filePath = "data/KenergeticBot.txt"; private TextUi ui; - private Parser commandParser; private Storage storage; public TaskList taskList; @@ -19,12 +20,16 @@ public static void main(String[] args) { new KenergeticBot().run(); } + /** + * Sets up the required objects, loads up the data from the storage file, and prints the welcome message. + */ private void start(String filePath, TaskList taskList, TextUi ui) { this.storage = new Storage(filePath); storage.loadPreviousList(taskList); ui.printGreetingMessage(); } + /** Runs the program until termination. */ public void run() { this.taskList = new TaskList(); this.ui = new TextUi(); @@ -32,6 +37,8 @@ public void run() { runCommandLoopUntilExitCommand(); exit(ui); } + + /** Reads the user command and executes it, until the user issues the exit command. */ public void runCommandLoopUntilExitCommand() { do { @@ -42,6 +49,7 @@ public void runCommandLoopUntilExitCommand() { storage.saveList(taskList); } + /** Prints the Goodbye message and exits. */ private static void exit(TextUi ui) { ui.printExitMessage(); System.exit(0); diff --git a/src/main/java/kenergeticbot/Parser.java b/src/main/java/kenergeticbot/Parser.java index 05c2942e8..c35b3b465 100644 --- a/src/main/java/kenergeticbot/Parser.java +++ b/src/main/java/kenergeticbot/Parser.java @@ -1,6 +1,13 @@ package kenergeticbot; -import kenergeticbot.command.*; +import kenergeticbot.command.AddCommand; +import kenergeticbot.command.Command; +import kenergeticbot.command.DeleteCommand; +import kenergeticbot.command.ExitCommand; +import kenergeticbot.command.IncorrectCommand; +import kenergeticbot.command.ListCommand; +import kenergeticbot.command.MarkCommand; +import kenergeticbot.command.UnmarkCommand; import kenergeticbot.exceptionhandler.KenergeticBotException; import java.text.ParseException; diff --git a/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java index 9108bac4e..b1dd39f40 100644 --- a/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java +++ b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java @@ -1,5 +1,8 @@ package kenergeticbot.exceptionhandler; +/** + * Represents error handling messages + */ public class KenergeticBotException extends Exception { public static final String INVALID_COMMAND = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; public static final String TODO_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be empty."; diff --git a/src/main/java/kenergeticbot/fileaccess/Storage.java b/src/main/java/kenergeticbot/fileaccess/Storage.java index 58082e482..84f3a8858 100644 --- a/src/main/java/kenergeticbot/fileaccess/Storage.java +++ b/src/main/java/kenergeticbot/fileaccess/Storage.java @@ -28,6 +28,7 @@ public Storage(String filePath) { /** * Checks if the storage file exists, creates the file if not + * @throws IOException if the file exists but unable to access */ public void initializeStorage() { File f = new File(filePath); @@ -39,13 +40,9 @@ public void initializeStorage() { System.out.println("File already exists."); } } catch (IOException e) { - System.out.println("An error occurred."); - e.printStackTrace(); + System.out.println("An error occurred." + e.getMessage()); } - System.out.println("full path: " + f.getAbsolutePath()); - System.out.println("file exists?: " + f.exists()); - System.out.println("is Directory?: " + f.isDirectory()); } /** @@ -59,13 +56,12 @@ public void checkFolderExist() { System.out.println("Directory cannot be created"); } System.out.println("full path: " + f.getAbsolutePath()); - System.out.println("file exists?: " + f.exists()); - System.out.println("is Directory?: " + f.isDirectory()); } /** * Loads the storage data file into a TaskList object. - * @throws FileNotFoundException if unable to locate storage file + * Calls readFromFile method + * @throws FileNotFoundException if unable to locate storage file using pathfile */ public void loadPreviousList(TaskList taskList) { try { @@ -78,6 +74,7 @@ public void loadPreviousList(TaskList taskList) { /** * Access the storage data file to create the appropriate Task object + * Creates a scanner to scan the storage data file, parses the text to determine respective Task type, isDone status and description */ public void readFromFile(String filePath, TaskList taskList) throws FileNotFoundException { File f = new File(filePath); // create a File for the given file path @@ -128,6 +125,9 @@ public void readFromFile(String filePath, TaskList taskList) throws FileNotFound /** * Writes a String into a data file for storage. + * If data file does not exist, the method creates data file + * If data file already exist, the method edits the data file + * @throws IOException if the file exists but unable to access */ public void writeToFile(String textToAdd) { File f = new File(filePath); diff --git a/src/main/java/kenergeticbot/task/Deadline.java b/src/main/java/kenergeticbot/task/Deadline.java index d14d40346..3e155d3af 100644 --- a/src/main/java/kenergeticbot/task/Deadline.java +++ b/src/main/java/kenergeticbot/task/Deadline.java @@ -1,5 +1,9 @@ package kenergeticbot.task; +/** + * Represents a Deadline Task + * Contains Task Deadline, Task Description and Task Type = [T] + */ public class Deadline extends Task { protected String deadline; public Deadline(String description, String deadline) { diff --git a/src/main/java/kenergeticbot/task/Event.java b/src/main/java/kenergeticbot/task/Event.java index 35de50761..949eaba31 100644 --- a/src/main/java/kenergeticbot/task/Event.java +++ b/src/main/java/kenergeticbot/task/Event.java @@ -1,5 +1,9 @@ package kenergeticbot.task; +/** + * Represents an Event Task + * Contains Task DateTime, Task Description and Task Type = [T] + */ public class Event extends Task { protected String dateTime; public Event(String description, String dateTime) { diff --git a/src/main/java/kenergeticbot/task/Task.java b/src/main/java/kenergeticbot/task/Task.java index 4e92219f6..8f4fdb2b5 100644 --- a/src/main/java/kenergeticbot/task/Task.java +++ b/src/main/java/kenergeticbot/task/Task.java @@ -1,4 +1,8 @@ package kenergeticbot.task; + +/** + * Represents an executable Task + */ public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/kenergeticbot/task/Todo.java b/src/main/java/kenergeticbot/task/Todo.java index 3075a503e..8b65c82ad 100644 --- a/src/main/java/kenergeticbot/task/Todo.java +++ b/src/main/java/kenergeticbot/task/Todo.java @@ -1,5 +1,9 @@ package kenergeticbot.task; +/** + * Represents a Todo Task + * Contains Task Description and Task Type = [T] + */ public class Todo extends Task { public Todo(String description) { super(description); diff --git a/text-ui-test/data/KenergeticBot.txt b/text-ui-test/data/KenergeticBot.txt index f86c3841d..617554ad9 100644 --- a/text-ui-test/data/KenergeticBot.txt +++ b/text-ui-test/data/KenergeticBot.txt @@ -1,4 +1,6 @@ T | 0 | borrow book +D | 0 | return book | (by: Sunday) +E | 0 | project meeting | (from: Mon 2pm to: 4pm) T | 0 | borrow book D | 0 | return book | (by: Sunday) E | 0 | project meeting | (from: Mon 2pm to: 4pm) From d0f4404f4d9ffe93fa98c4c27d6f34684a45b6a7 Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:22:13 +0800 Subject: [PATCH 30/46] Update README.md --- README.md | 64 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 626f6f905..12435d360 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,52 @@ -# KenergeticBot project template +# Hi there! I am KenergeticBot -This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. +I am a personal Task managing chatbot, able to store tasks such as To dos, Deadlines and Events. -## Setting up in Intellij +## Here are some of the things I can do! -Prerequisites: JDK 11, update Intellij to the most recent version. +1) Create 3 types of Tasks: +
+ 1. Todo: Tasks which are not time sensitive. + 2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. + 3. Event: Tasks which are happening at a specific time frame, using /from and /to. +
-1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) -1. Open the project into Intellij as follows: - 1. Click `Open`. - 1. Select the project directory, and click `OK`. - 1. If there are any further prompts, accept the defaults. -1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
- In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/KenergeticBot.java` file, right-click it, and choose `Run KenergeticBot.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: +3) Keep track of Task completion. +
+ 1. [ ] Indicates Tasks that are not completed + 2. [X] Indicates Tasks that are completed +
+ +4) Store and retreive your tasks into a dedicated save file. + +5) Search for keyword using the Find function. + find [keyword] +
+ ``` + Useage Example + find book + ____________________________________________________________ + Here are the matching tasks in your list: + 1.[T][ ] borrow book + 2.[D][ ] return book (by: Sunday) + ____________________________________________________________ + ``` +
+ +6) Remove stored Tasks using the Delete function. + delete [task index] +
``` - Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| + Useage Example + delete 2 + ____________________________________________________________ + Noted. I've removed this task: + [D][ ] return book (by: Sunday) + Now you have 1 tasks in the list. + ____________________________________________________________ ``` +
+ +## User Guide +To find out more details on how to interact with KenergeticBot, please refer to *(http://Cazh1.github.io/ip/)*. + From f6ef0c4ffee8911096d30f1fb78e1fccbf85a874 Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:31:40 +0800 Subject: [PATCH 31/46] Update README.md --- README.md | 65 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 12435d360..329a47031 100644 --- a/README.md +++ b/README.md @@ -5,46 +5,49 @@ I am a personal Task managing chatbot, able to store tasks such as To dos, Deadl ## Here are some of the things I can do! 1) Create 3 types of Tasks: -
- 1. Todo: Tasks which are not time sensitive. - 2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. - 3. Event: Tasks which are happening at a specific time frame, using /from and /to. -
+
+1. Todo: Tasks which are not time sensitive. +2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. +3. Event: Tasks which are happening at a specific time frame, using /from and /to. +
-3) Keep track of Task completion. +2) Keep track of Task completion.
- 1. [ ] Indicates Tasks that are not completed - 2. [X] Indicates Tasks that are completed + 1. [ ] Indicates Tasks that are not completed + 2. [X] Indicates Tasks that are completed
-4) Store and retreive your tasks into a dedicated save file. +3) Store and retreive your tasks into a dedicated save file. -5) Search for keyword using the Find function. - find [keyword] +4) Search for keyword using the Find function. +find [keyword]
- ``` - Useage Example - find book - ____________________________________________________________ - Here are the matching tasks in your list: - 1.[T][ ] borrow book - 2.[D][ ] return book (by: Sunday) - ____________________________________________________________ - ``` +``` +Useage Example +find book +____________________________________________________________ +Here are the matching tasks in your list: +1.[T][ ] borrow book +2.[D][ ] return book (by: Sunday) +____________________________________________________________ +```
-6) Remove stored Tasks using the Delete function. - delete [task index] +5) Remove stored Tasks using the Delete function. +delete [task index]
- ``` - Useage Example - delete 2 - ____________________________________________________________ - Noted. I've removed this task: - [D][ ] return book (by: Sunday) - Now you have 1 tasks in the list. - ____________________________________________________________ - ``` +Useage Example +``` +delete 2 +____________________________________________________________ +Noted. I've removed this task: +[D][ ] return book (by: Sunday) +Now you have 1 tasks in the list. +____________________________________________________________ +``` +```ruby +puts "Hello World" +```
## User Guide From 5d0ee0d4c03377ef144da49ea3c43d5be2eef58a Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:34:02 +0800 Subject: [PATCH 32/46] Update README.md --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 329a47031..3236ef511 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,22 @@ puts "Hello World" ``` +
+ +Tips for collapsed sections + +### You can add a header + +You can add text within a collapsed section. + +You can add an image or a code block, too. + +```ruby + puts "Hello World" +``` + +
+ ## User Guide To find out more details on how to interact with KenergeticBot, please refer to *(http://Cazh1.github.io/ip/)*. From 2ff6d44341cee67bb7ba717e2475d8e35d578756 Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:36:08 +0800 Subject: [PATCH 33/46] Update README.md --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3236ef511..86dde5a05 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,9 @@ I am a personal Task managing chatbot, able to store tasks such as To dos, Deadl 3) Store and retreive your tasks into a dedicated save file. 4) Search for keyword using the Find function. -find [keyword] +
+ ### find [keyword] ``` Useage Example find book @@ -34,9 +35,10 @@ ____________________________________________________________
5) Remove stored Tasks using the Delete function. -delete [task index] +
Useage Example + ### delete [task index] ``` delete 2 ____________________________________________________________ @@ -46,7 +48,7 @@ Now you have 1 tasks in the list. ____________________________________________________________ ``` ```ruby -puts "Hello World" + puts "Hello World" ```
From 9f93c2e7729d87d6f1d6d275cc78d31841ae7472 Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:37:00 +0800 Subject: [PATCH 34/46] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 86dde5a05..61ed0f860 100644 --- a/README.md +++ b/README.md @@ -4,22 +4,22 @@ I am a personal Task managing chatbot, able to store tasks such as To dos, Deadl ## Here are some of the things I can do! -1) Create 3 types of Tasks: +=Create 3 types of Tasks:
1. Todo: Tasks which are not time sensitive. 2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. 3. Event: Tasks which are happening at a specific time frame, using /from and /to.
-2) Keep track of Task completion. +Keep track of Task completion.
1. [ ] Indicates Tasks that are not completed 2. [X] Indicates Tasks that are completed
-3) Store and retreive your tasks into a dedicated save file. +Store and retreive your tasks into a dedicated save file. -4) Search for keyword using the Find function. +Search for keyword using the Find function.
### find [keyword] @@ -34,7 +34,7 @@ ____________________________________________________________ ```
-5) Remove stored Tasks using the Delete function. +Remove stored Tasks using the Delete function.
Useage Example From a077f5481f327f35b4a3863c5513c00b8537a1f4 Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:37:14 +0800 Subject: [PATCH 35/46] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 61ed0f860..0d2434902 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ I am a personal Task managing chatbot, able to store tasks such as To dos, Deadl ## Here are some of the things I can do! -=Create 3 types of Tasks: +Create 3 types of Tasks:
1. Todo: Tasks which are not time sensitive. 2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. From bd265bba5420d6244b9d81ad8319d190367e2886 Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:37:55 +0800 Subject: [PATCH 36/46] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 0d2434902..2911504be 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,9 @@ Keep track of Task completion.
Store and retreive your tasks into a dedicated save file. +```ruby + puts "Hello World" +``` Search for keyword using the Find function. From e6b2f515809e842ba8cf40c599771851350c1e1f Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:38:18 +0800 Subject: [PATCH 37/46] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2911504be..f80d2722f 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Keep track of Task completion.
Store and retreive your tasks into a dedicated save file. -```ruby +``` puts "Hello World" ``` From 449b98dc0dbd34878d991db7a57c82e51b0f0b19 Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:43:16 +0800 Subject: [PATCH 38/46] Update README.md --- README.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f80d2722f..157a5ced2 100644 --- a/README.md +++ b/README.md @@ -19,22 +19,23 @@ Keep track of Task completion. Store and retreive your tasks into a dedicated save file. ``` - puts "Hello World" + Loading previous list + Successfully wrote to the file. ``` Search for keyword using the Find function.
+ Useage Example ### find [keyword] -``` -Useage Example -find book -____________________________________________________________ -Here are the matching tasks in your list: -1.[T][ ] borrow book -2.[D][ ] return book (by: Sunday) -____________________________________________________________ -``` + + find book + ____________________________________________________________ + Here are the matching tasks in your list: + 1.[T][ ] borrow book + 2.[D][ ] return book (by: Sunday) + ____________________________________________________________ +
Remove stored Tasks using the Delete function. @@ -42,17 +43,14 @@ Remove stored Tasks using the Delete function.
Useage Example ### delete [task index] -``` + delete 2 ____________________________________________________________ Noted. I've removed this task: [D][ ] return book (by: Sunday) Now you have 1 tasks in the list. ____________________________________________________________ -``` -```ruby - puts "Hello World" -``` +
From bfdc87d496ed32a60a9874920c974663f566db72 Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:45:29 +0800 Subject: [PATCH 39/46] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 157a5ced2..74af106ce 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ I am a personal Task managing chatbot, able to store tasks such as To dos, Deadl Create 3 types of Tasks:
-1. Todo: Tasks which are not time sensitive. -2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. -3. Event: Tasks which are happening at a specific time frame, using /from and /to. + 1. Todo: Tasks which are not time sensitive. + 2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. + 3. Event: Tasks which are happening at a specific time frame, using /from and /to.
Keep track of Task completion. From fb8bcff4e2731c6234bdd67644e0ba721ccb462d Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:57:57 +0800 Subject: [PATCH 40/46] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 74af106ce..ccf745f39 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ I am a personal Task managing chatbot, able to store tasks such as To dos, Deadl Create 3 types of Tasks:
+ 1. Todo: Tasks which are not time sensitive. 2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. 3. Event: Tasks which are happening at a specific time frame, using /from and /to. @@ -13,6 +14,7 @@ Create 3 types of Tasks: Keep track of Task completion.
+ 1. [ ] Indicates Tasks that are not completed 2. [X] Indicates Tasks that are completed
@@ -26,7 +28,9 @@ Store and retreive your tasks into a dedicated save file. Search for keyword using the Find function.
+ Useage Example + ### find [keyword] find book @@ -41,7 +45,9 @@ Search for keyword using the Find function. Remove stored Tasks using the Delete function.
+ Useage Example + ### delete [task index] delete 2 From f691c78639c574c1852ef09c4833d7d11ca68e8e Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Wed, 4 Oct 2023 19:48:27 +0800 Subject: [PATCH 41/46] Update README.md --- README.md | 104 ++++------------ docs/README.md | 151 ++++++++++++++++++++++-- src/main/java/kenergeticbot/Parser.java | 1 + text-ui-test/input.txt | 1 + 4 files changed, 165 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index ccf745f39..626f6f905 100644 --- a/README.md +++ b/README.md @@ -1,80 +1,24 @@ -# Hi there! I am KenergeticBot - -I am a personal Task managing chatbot, able to store tasks such as To dos, Deadlines and Events. - -## Here are some of the things I can do! - -Create 3 types of Tasks: -
- - 1. Todo: Tasks which are not time sensitive. - 2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. - 3. Event: Tasks which are happening at a specific time frame, using /from and /to. -
- -Keep track of Task completion. -
- - 1. [ ] Indicates Tasks that are not completed - 2. [X] Indicates Tasks that are completed -
- -Store and retreive your tasks into a dedicated save file. -``` - Loading previous list - Successfully wrote to the file. -``` - -Search for keyword using the Find function. - -
- - Useage Example - - ### find [keyword] - - find book - ____________________________________________________________ - Here are the matching tasks in your list: - 1.[T][ ] borrow book - 2.[D][ ] return book (by: Sunday) - ____________________________________________________________ - -
- -Remove stored Tasks using the Delete function. - -
- -Useage Example - - ### delete [task index] - -delete 2 -____________________________________________________________ -Noted. I've removed this task: -[D][ ] return book (by: Sunday) -Now you have 1 tasks in the list. -____________________________________________________________ - -
- -
- -Tips for collapsed sections - -### You can add a header - -You can add text within a collapsed section. - -You can add an image or a code block, too. - -```ruby - puts "Hello World" -``` - -
- -## User Guide -To find out more details on how to interact with KenergeticBot, please refer to *(http://Cazh1.github.io/ip/)*. - +# KenergeticBot project template + +This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. + +## Setting up in Intellij + +Prerequisites: JDK 11, update Intellij to the most recent version. + +1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) +1. Open the project into Intellij as follows: + 1. Click `Open`. + 1. Select the project directory, and click `OK`. + 1. If there are any further prompts, accept the defaults. +1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
+ In the same dialog, set the **Project language level** field to the `SDK default` option. +3. After that, locate the `src/main/java/KenergeticBot.java` file, right-click it, and choose `Run KenergeticBot.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: + ``` + Hello from + ____ _ + | _ \ _ _| | _____ + | | | | | | | |/ / _ \ + | |_| | |_| | < __/ + |____/ \__,_|_|\_\___| + ``` diff --git a/docs/README.md b/docs/README.md index 8077118eb..84ef4fe6a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,156 @@ # User Guide +## Hi there! I am KenergeticBot +I am a personal Task managing chatbot, able to store tasks such as To dos, Deadlines and Events. + +## Setting up in Intellij + +Prerequisites: JDK 11, update Intellij to the most recent version. + +1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) +1. Open the project into Intellij as follows: + 1. Click `Open`. + 1. Select the project directory, and click `OK`. + 1. If there are any further prompts, accept the defaults. +1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
+ In the same dialog, set the **Project language level** field to the `SDK default` option. +3. After that, locate the `src/main/java/KenergeticBot.java` file, right-click it, and choose `Run KenergeticBot.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: + + ____________________________________________________________ + Hello! I'm KenergeticBot + What can I do for you? + ____________________________________________________________ + ## Features -### Feature-ABC +> **Notes about command format:** +> - Words encased in `{ }` are the arguments to be supplied by user.
+ e.g. `deadline {Description} /by {Deadline Date & Time}`, `Description` and `Deadline Date & Time` are arguments to be specified by user. +> - Parameters need to be in the exact format as specified.
+ e.g. `event {Description} /from {Event Start Date & Time} /to {Event End Date & Time}`, `/from` must come before `/to`. +### Here are some of the things I can do! + +### Task Creation + +Create 3 types of Tasks: +
+ +1. Todo: Tasks which are not time sensitive. + + Format: `todo {Description}` + + Example:
`todo read book` +2. Deadline: Tasks which are time sensitive, able to indicate deadline using /by. + + Format: `deadline {Description} /by {Deadline Date & Time}` + + Example:
`deadline return book /by Feb 14th` +3. Event: Tasks which are happening at a specific time frame, using /from and /to. + + Format: `event {Description} /from {Event Start Date & Time} /to {Event End Date & Time}` + +
+ +### Task Viewing + +Lists all the task in the task list. + +Format: `list` +
+ +Usage Example + + list + ____________________________________________________________ + Here are the tasks in your list: + 1.[T][ ] borrow book + 2.[D][ ] return book (by: Sunday) + ____________________________________________________________ +
+ +### Task Marking + +Keep track of Task completion. +1. '[X]' Indicates Tasks that are completed, using mark command. +2. '[ ]' Indicates Tasks that are not completed, using unmark command. +
-Description of the feature. +Mark Task -### Feature-XYZ +Format: `mark {task index}` -Description of the feature. +Example: -## Usage + mark 2 + ____________________________________________________________ + Nice! I've marked this task as done: + [D][X] return book (by: Sunday) + ____________________________________________________________ -### `Keyword` - Describe action +
-Describe the action and its outcome. +
-Example of usage: +Unmark Task -`keyword (optional arguments)` +Format: `unmark {task index}` -Expected outcome: +Example: -Description of the outcome. + unmark 2 + ____________________________________________________________ + OK, I've marked this task as not done yet: + [D][ ] return book (by: Sunday) + ____________________________________________________________ +
+ +### Task Saving +Store and retrieve your tasks into a dedicated save file. ``` -expected output + Loading previous list + Successfully wrote to the file. ``` + +### Task Finding + +Search for keyword using the Find function. + +Format: `find {keyword}` +
+ + Usage Example + + find book + ____________________________________________________________ + Here are the matching tasks in your list: + 1.[T][ ] borrow book + 2.[D][ ] return book (by: Sunday) + ____________________________________________________________ + +
+ +### Task Deletion + +Remove stored Tasks using the Delete function. + +Format: `delete {task index}` + +
+ +Usage Example + + delete 2 + ____________________________________________________________ + Noted. I've removed this task: + [D][ ] return book (by: Sunday) + Now you have 1 tasks in the list. + ____________________________________________________________ + +
+ +### End Program +Terminates the chatbot program. + +Format: `bye` + diff --git a/src/main/java/kenergeticbot/Parser.java b/src/main/java/kenergeticbot/Parser.java index fd7ef91e9..a96ef18d1 100644 --- a/src/main/java/kenergeticbot/Parser.java +++ b/src/main/java/kenergeticbot/Parser.java @@ -4,6 +4,7 @@ import kenergeticbot.command.Command; import kenergeticbot.command.DeleteCommand; import kenergeticbot.command.ExitCommand; +import kenergeticbot.command.FindCommand; import kenergeticbot.command.IncorrectCommand; import kenergeticbot.command.ListCommand; import kenergeticbot.command.MarkCommand; diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index a2b94ca37..ba52efa0a 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -20,5 +20,6 @@ event project meeting /from event project meeting /fr event project meeting event +find book delete 2 bye \ No newline at end of file From 8f7dbd96ba671ed60a902ebdeab4aded9985df56 Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:55:19 +0800 Subject: [PATCH 42/46] Update README.md --- docs/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 84ef4fe6a..2b241740d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -44,11 +44,13 @@ Create 3 types of Tasks: Format: `deadline {Description} /by {Deadline Date & Time}` - Example:
`deadline return book /by Feb 14th` + Example:
`deadline return book /by Sunday` 3. Event: Tasks which are happening at a specific time frame, using /from and /to. Format: `event {Description} /from {Event Start Date & Time} /to {Event End Date & Time}` + Example:
`event project meeting /from Mon 2pm /to 4pm` +
### Task Viewing From 531399a16ede26f32b96fa0f4a22c0c06b610b3c Mon Sep 17 00:00:00 2001 From: Cazh1 <82131563+Cazh1@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:21:08 +0800 Subject: [PATCH 43/46] Update README.md --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 626f6f905..db3ad882d 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,8 @@ Prerequisites: JDK 11, update Intellij to the most recent version. In the same dialog, set the **Project language level** field to the `SDK default` option. 3. After that, locate the `src/main/java/KenergeticBot.java` file, right-click it, and choose `Run KenergeticBot.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` - Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| + ____________________________________________________________ + Hello! I'm KenergeticBot + What can I do for you? + ____________________________________________________________ ``` From 9b0876a414e7b56ee69be6c9575b034689180721 Mon Sep 17 00:00:00 2001 From: ZH Date: Fri, 6 Oct 2023 21:11:59 +0800 Subject: [PATCH 44/46] Update Code Quality --- src/main/java/KenergeticBot.java | 6 +- src/main/java/kenergeticbot/Parser.java | 19 ++-- src/main/java/kenergeticbot/TaskList.java | 4 +- .../kenergeticbot/command/AddCommand.java | 55 ++++++---- .../kenergeticbot/command/DeleteCommand.java | 5 +- .../KenergeticBotException.java | 4 +- .../kenergeticbot/fileaccess/Storage.java | 101 ++++++++++-------- 7 files changed, 117 insertions(+), 77 deletions(-) diff --git a/src/main/java/KenergeticBot.java b/src/main/java/KenergeticBot.java index 652cee7b8..4e96749fd 100644 --- a/src/main/java/KenergeticBot.java +++ b/src/main/java/KenergeticBot.java @@ -24,8 +24,8 @@ public static void main(String[] args) { * Sets up the required objects, loads up the data from the storage file, and prints the welcome message. */ private void start(String filePath, TaskList taskList, TextUi ui) { - this.storage = new Storage(filePath); - storage.loadPreviousList(taskList); + this.storage = new Storage(filePath, ui); + storage.loadPreviousList(taskList, ui); ui.printGreetingMessage(); } @@ -45,7 +45,7 @@ public void runCommandLoopUntilExitCommand() { Command c = Parser.parseCommand(taskList, item); c.execute(taskList, ui); } while (!ExitCommand.isExit()); - storage.saveList(taskList); + storage.saveList(taskList, ui); } /** Prints the Goodbye message and exits. */ diff --git a/src/main/java/kenergeticbot/Parser.java b/src/main/java/kenergeticbot/Parser.java index a96ef18d1..65ab5a553 100644 --- a/src/main/java/kenergeticbot/Parser.java +++ b/src/main/java/kenergeticbot/Parser.java @@ -20,6 +20,7 @@ public class Parser{ * Parses user input into command for execution. * * @param userInput full user input string + * @param taskList The arraylist object created that stores current tasks * @return the command based on the user input * @throws KenergeticBotException if input does not match any known command */ @@ -37,7 +38,7 @@ public static Command parseCommand(TaskList taskList, String userInput) { case ExitCommand.COMMAND_WORD: return new ExitCommand(); case FindCommand.COMMAND_WORD: - return prepareFind(taskList, userInput); + return prepareFind(userInput); default: try{ return new AddCommand(userInput); @@ -51,6 +52,7 @@ public static Command parseCommand(TaskList taskList, String userInput) { * Parses arguments in the context of the mark task command. * * @param userInput full command args string + * @param taskList The arraylist object created that stores current tasks * @return the prepared command */ private static Command prepareMark(TaskList taskList, String userInput) { @@ -70,6 +72,7 @@ private static Command prepareMark(TaskList taskList, String userInput) { * Parses arguments in the context of the unmark task command. * * @param userInput full command args string + * @param taskList The arraylist object created that stores current tasks * @return the prepared command */ private static Command prepareUnmark(TaskList taskList, String userInput) { @@ -89,6 +92,7 @@ private static Command prepareUnmark(TaskList taskList, String userInput) { * Parses arguments in the context of the delete task command. * * @param userInput full command args string + * @param taskList The arraylist object created that stores current tasks * @return the prepared command */ private static Command prepareDelete(TaskList taskList, String userInput) { @@ -110,9 +114,9 @@ private static Command prepareDelete(TaskList taskList, String userInput) { * @param userInput full command args string * @return the prepared command */ - private static Command prepareFind(TaskList taskList, String userInput) { + private static Command prepareFind(String userInput) { try { - final String listKeyword = parseArgsAsDisplayedKeyword(taskList, userInput, "find"); + final String listKeyword = parseArgsAsDisplayedKeyword(userInput, "find"); return new FindCommand(listKeyword); } catch (KenergeticBotException e) { return new IncorrectCommand(e.getMessage()); @@ -127,6 +131,8 @@ private static Command prepareFind(TaskList taskList, String userInput) { * Parses the given arguments string to identify task index number. * * @param userInput arguments string to parse as index number + * @param command expected String name of the command called + * @param taskList The arraylist object created that stores current tasks * @return the parsed index number * @throws ParseException if no region of the args string could be found for the index * @throws NumberFormatException the args string region is not a valid number @@ -146,11 +152,12 @@ private static int parseArgsAsDisplayedIndex(TaskList taskList, String userInput * Parses the given arguments string to identify task keyword. * * @param userInput arguments string to parse as keyword + * @param command expected String name of command to be called * @return the parsed keyword - * @throws ParseException if no region of the args string could be found for the index - * @throws NumberFormatException the args string region is not a valid number + * @throws ParseException if the keyword is not in an expected format + * @throws KenergeticBotException if no keyword is found */ - private static String parseArgsAsDisplayedKeyword(TaskList taskList, String userInput, String command) throws ParseException, NumberFormatException, KenergeticBotException { + private static String parseArgsAsDisplayedKeyword(String userInput, String command) throws ParseException, KenergeticBotException { String keywordString = userInput.replace(command, "").trim(); if (keywordString.isEmpty()) { throw new KenergeticBotException(COMMAND_TYPO_NO_KEYWORD); diff --git a/src/main/java/kenergeticbot/TaskList.java b/src/main/java/kenergeticbot/TaskList.java index df8818274..590522198 100644 --- a/src/main/java/kenergeticbot/TaskList.java +++ b/src/main/java/kenergeticbot/TaskList.java @@ -17,11 +17,11 @@ public int getSize() { public Task getTask(int taskID) { return taskList.get(taskID); } - public void remove(int taskID) { + public void removeTask(int taskID) { taskList.remove(taskID); } - public void add(Task task) { + public void addTask(Task task) { taskList.add(task); } } diff --git a/src/main/java/kenergeticbot/command/AddCommand.java b/src/main/java/kenergeticbot/command/AddCommand.java index 6ad116013..8aea8bf9c 100644 --- a/src/main/java/kenergeticbot/command/AddCommand.java +++ b/src/main/java/kenergeticbot/command/AddCommand.java @@ -17,16 +17,16 @@ public class AddCommand extends Command { /** * Convenience constructor using input string. - * + * @param userInput User's input used to determine the type of task to be created * @throws KenergeticBotException if input does not match any known command */ - public AddCommand(String item) throws KenergeticBotException { - AddCommand.item = item; - if (BooleanChecks.checkTextForTodo(item)) { + public AddCommand(String userInput) throws KenergeticBotException { + AddCommand.item = userInput; + if (BooleanChecks.checkTextForTodo(userInput)) { this.taskType = "T"; - } else if (BooleanChecks.checkTextForDeadline(item)) { + } else if (BooleanChecks.checkTextForDeadline(userInput)) { this.taskType = "D"; - } else if (BooleanChecks.checkTextForEvent(item)) { + } else if (BooleanChecks.checkTextForEvent(userInput)) { this.taskType = "E"; }else { throw new KenergeticBotException(INVALID_COMMAND); @@ -60,20 +60,32 @@ public void execute(TaskList taskList, TextUi ui){ } } - //Creates a "Todo" object and adds to the taskList - public static void addTodo(TaskList taskList ,String item, TextUi ui) throws KenergeticBotException { - String formattedString = item.replace("todo", "").trim(); + /** + * Creates a "Todo" object and adds to the taskList + * @param taskList The arraylist object created to store the newly created "Todo" Task object + * @param userInput User's input containing the details used to create the "Todo" Task object + * @param ui The TextUI object created to handle I/O with the user + * @throws KenergeticBotException if userInput does not contain sufficient information required to create "Todo" Task object + */ + public static void addTodo(TaskList taskList ,String userInput, TextUi ui) throws KenergeticBotException { + String formattedString = userInput.replace("todo", "").trim(); if (formattedString.isEmpty()) { throw new KenergeticBotException(TODO_MISSING_DESCRIPTION); } Task newTask = new Todo(formattedString); - taskList.add(newTask); + taskList.addTask(newTask); ui.printAddedTaskMessage(taskList, newTask); } - //Creates a "Deadline" object and adds to the taskList - public static void addDeadline(TaskList taskList, String item, TextUi ui) throws KenergeticBotException { - String[] formattedString = item.replace("deadline", "").trim().split("/"); + /** + * Creates a "Deadline" object and adds to the taskList + * @param taskList The arraylist object created to store the newly created "Deadline" Task object + * @param userInput User's input containing the details used to create the "Deadline" Task object + * @param ui The TextUI object created to handle I/O with the user + * @throws KenergeticBotException if userInput does not contain sufficient information required to create "Deadline" Task object + */ + public static void addDeadline(TaskList taskList, String userInput, TextUi ui) throws KenergeticBotException { + String[] formattedString = userInput.replace("deadline", "").trim().split("/"); switch (formattedString.length) { //no default case needed since finding exceptions case 1: //no break; is needed since throwing exception if (formattedString[0].isEmpty()) { @@ -90,13 +102,20 @@ public static void addDeadline(TaskList taskList, String item, TextUi ui) throws } String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; Task newTask = new Deadline(formattedString[0], deadlineDate); - taskList.add(newTask); + taskList.addTask(newTask); ui.printAddedTaskMessage(taskList, newTask); } - //Creates a "Event" object and adds to the taskList - public static void addEvent(TaskList taskList, String item, TextUi ui) throws KenergeticBotException { - String[] formattedString = item.replace("event", "").trim().split("/"); + // + /** + * Creates an "Event" object and adds to the taskList + * @param taskList The arraylist object created to store the newly created "Event" Task object + * @param userInput User's input containing the details used to create the "Event" Task object + * @param ui The TextUI object created to handle I/O with the user + * @throws KenergeticBotException if userInput does not contain sufficient information required to create "Event" Task object + */ + public static void addEvent(TaskList taskList, String userInput, TextUi ui) throws KenergeticBotException { + String[] formattedString = userInput.replace("event", "").trim().split("/"); switch(formattedString.length) { //no default case needed since finding exceptions case 1: //no break; is needed since throwing exception if (formattedString[0].isEmpty()) { @@ -123,7 +142,7 @@ public static void addEvent(TaskList taskList, String item, TextUi ui) throws Ke String eventTo = formattedString[2].replace("to", "to:"); String dateTime = "(" + eventFrom + eventTo + ")"; Task newTask = new Event(formattedString[0], dateTime); - taskList.add(newTask); + taskList.addTask(newTask); ui.printAddedTaskMessage(taskList, newTask); } } diff --git a/src/main/java/kenergeticbot/command/DeleteCommand.java b/src/main/java/kenergeticbot/command/DeleteCommand.java index 2155b5af1..20a171d89 100644 --- a/src/main/java/kenergeticbot/command/DeleteCommand.java +++ b/src/main/java/kenergeticbot/command/DeleteCommand.java @@ -3,6 +3,8 @@ import kenergeticbot.TaskList; import kenergeticbot.ui.TextUi; +import static kenergeticbot.common.Messages.SEPARATING_LINE; + /** * Deletes a task identified using it's last displayed index from the task list. */ @@ -20,7 +22,8 @@ public DeleteCommand(int listIndex) { @Override public void execute(TaskList taskList, TextUi ui) { ui.printDeleteTaskMessage(taskList, listIndex); - taskList.remove(listIndex - 1); + taskList.removeTask(listIndex - 1); + ui.showToUser(SEPARATING_LINE); } } diff --git a/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java index be92b05c0..fdcbc5418 100644 --- a/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java +++ b/src/main/java/kenergeticbot/exceptionhandler/KenergeticBotException.java @@ -11,9 +11,9 @@ public class KenergeticBotException extends Exception { public static final String DEADLINE_MISSING_DATE_INTERMEDIATE = " \uD83D\uDCC5 OOPS!!! The date of a deadline cannot be empty. Please use the command /by"; public static final String EVENT_MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a event cannot be empty."; public static final String EVENT_MISSING_START = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty."; - public static final String EVENT_MISSING_END = " ⏳ OOPS!!! The end time of a event cannot be empty."; + public static final String EVENT_MISSING_END = " OOPS!!! The end time of a event cannot be empty."; public static final String EVENT_MISSING_START_INTERMEDIATE = " ⩇:⩇⩇ OOPS!!! The start time of a event cannot be empty. Please use the command /from"; - public static final String EVENT_MISSING_END_INTERMEDIATE = " ⏳ OOPS!!! The end time of a event cannot be empty. Please use the command /to"; + public static final String EVENT_MISSING_END_INTERMEDIATE = " OOPS!!! The end time of a event cannot be empty. Please use the command /to"; public static final String OUT_OF_RANGE = " OOPS!!! Looks like you tried on an item not in the list"; public static final String COMMAND_TYPO = " ¯\\_(ツ)_/¯ OOPS!!! There may be a typo in the command used."; public static final String COMMAND_TYPO_NO_NUMBER = COMMAND_TYPO + " Please use the command with an integer number"; diff --git a/src/main/java/kenergeticbot/fileaccess/Storage.java b/src/main/java/kenergeticbot/fileaccess/Storage.java index 4912c279c..76d769138 100644 --- a/src/main/java/kenergeticbot/fileaccess/Storage.java +++ b/src/main/java/kenergeticbot/fileaccess/Storage.java @@ -5,6 +5,7 @@ import kenergeticbot.task.Event; import kenergeticbot.task.Task; import kenergeticbot.task.Todo; +import kenergeticbot.ui.TextUi; import java.io.File; import java.io.FileNotFoundException; @@ -21,62 +22,65 @@ public class Storage { protected String filePath; public static final String folderPath = "data"; - public Storage(String filePath) { + public Storage(String filePath, TextUi ui) { this.filePath = filePath; - initializeStorage(); + initializeStorage(ui); } /** * Checks if the storage file exists, creates the file if not + * @param ui The TextUI object created to handle I/O with the user * @throws IOException if the file exists but unable to access */ - public void initializeStorage() { + public void initializeStorage(TextUi ui) { File f = new File(filePath); - checkFolderExist(); + checkFolderExist(ui); try { if (f.createNewFile()) { - System.out.println("File created: " + f.getName()); + ui.showToUser("File created: " + f.getName()); } else { - System.out.println("File already exists."); + ui.showToUser("File already exists."); } } catch (IOException e) { - System.out.println("An error occurred." + e.getMessage()); + ui.showToUser("An error occurred." + e.getMessage()); } - System.out.println("full path: " + f.getAbsolutePath()); + ui.showToUser("Save file is at: " + f.getAbsolutePath()); } /** * Checks if the storage folder to store the storage file exists, creates the folder if not + * @param ui The TextUI object created to handle I/O with the user */ - public void checkFolderExist() { + public void checkFolderExist(TextUi ui) { File f = new File(folderPath); if (f.mkdir()) { - System.out.println("Directory is created"); + ui.showToUser("Directory is created"); } else { - System.out.println("Directory cannot be created"); + ui.showToUser("Directory already exist"); } - System.out.println("full path: " + f.getAbsolutePath()); } /** * Loads the storage data file into a TaskList object. * Calls readFromFile method + * @param taskList The arraylist object created that stores current tasks * @throws FileNotFoundException if unable to locate storage file using pathfile */ - public void loadPreviousList(TaskList taskList) { + public void loadPreviousList(TaskList taskList, TextUi ui) { try { - System.out.println("Loading previous list"); - readFromFile(filePath, taskList); + readFromFile(filePath, taskList, ui); } catch (FileNotFoundException e) { - System.out.println("An error occurred." + e.getMessage()); + ui.showToUser(e.getMessage()); } } /** * Access the storage data file to create the appropriate Task object * Creates a scanner to scan the storage data file, parses the text to determine respective Task type, isDone status and description + * @param filePath The location of the data storage file + * @param taskList The arraylist object created that stores current tasks */ - public void readFromFile(String filePath, TaskList taskList) throws FileNotFoundException { + public void readFromFile(String filePath, TaskList taskList, TextUi ui) throws FileNotFoundException { File f = new File(filePath); // create a File for the given file path if (f.length() == 0) { return; @@ -96,21 +100,21 @@ public void readFromFile(String filePath, TaskList taskList) throws FileNotFound switch (taskType) { case "T" : - Task previousTodo = new Todo(taskDescription, checkMark(taskMark)); - taskList.add(previousTodo); - System.out.println("adding:" + previousTodo); + Task previousTodo = new Todo(taskDescription, isMark(taskMark)); + taskList.addTask(previousTodo); + ui.showToUser("adding:" + previousTodo); break; case "D" : String taskDeadline = taskVariables[3]; - Task previousDeadline = new Deadline(taskDescription, taskDeadline, checkMark(taskMark)); - taskList.add(previousDeadline); - System.out.println("adding:" + previousDeadline); + Task previousDeadline = new Deadline(taskDescription, taskDeadline, isMark(taskMark)); + taskList.addTask(previousDeadline); + ui.showToUser("adding:" + previousDeadline); break; case "E" : String taskEventDateTime = taskVariables[3]; - Task previousEvent = new Event(taskDescription, taskEventDateTime, checkMark(taskMark)); - taskList.add(previousEvent); - System.out.println("adding:" + previousEvent); + Task previousEvent = new Event(taskDescription, taskEventDateTime, isMark(taskMark)); + taskList.addTask(previousEvent); + ui.showToUser("adding:" + previousEvent); break; default : throw new IllegalStateException("Unexpected value: " + taskType); @@ -119,7 +123,7 @@ public void readFromFile(String filePath, TaskList taskList) throws FileNotFound try { new FileWriter(filePath, false).close(); } catch (IOException e) { - System.out.println(e.getMessage()); + ui.showToUser(e.getMessage()); } } @@ -127,42 +131,49 @@ public void readFromFile(String filePath, TaskList taskList) throws FileNotFound * Writes a String into a data file for storage. * If data file does not exist, the method creates data file * If data file already exist, the method edits the data file + * @param textToAdd The intended data to be stored into the data file * @throws IOException if the file exists but unable to access */ - public void writeToFile(String textToAdd) { + public void writeToFile(String textToAdd, TextUi ui) { File f = new File(filePath); - if (f.length() == 0) { + if (f.length() == 0) { //if file is empty try { - FileWriter myWriter = new FileWriter(filePath); - myWriter.write(textToAdd); - myWriter.close(); - System.out.println("Successfully wrote to the file."); + FileWriter saveFileWriter = new FileWriter(filePath); + saveFileWriter.write(textToAdd); + saveFileWriter.close(); + ui.showToUser("Successfully wrote to the file."); } catch (IOException e) { - System.out.println(e.getMessage()); + ui.showToUser(e.getMessage()); } - } else { + } else { //if file is not empty try { - FileWriter myWriter = new FileWriter(filePath, true); - myWriter.write(textToAdd); - myWriter.close(); - System.out.println("Successfully wrote to the file."); + FileWriter saveFileWriter = new FileWriter(filePath, true); + saveFileWriter.write(textToAdd); + saveFileWriter.close(); + ui.showToUser("Successfully wrote to the file."); } catch (IOException e) { - System.out.println(e.getMessage()); + ui.showToUser(e.getMessage()); } } } /** - * Saves the TaskList object into a data file for storage. + * Saves the TaskList object into a data file for storage, iterating through the list. */ - public void saveList(TaskList taskList) { + public void saveList(TaskList taskList, TextUi ui) { for (int i = 0; i < taskList.getSize(); i++) { System.out.println(taskList.getTask(i)); - writeToFile(taskList.getTask(i).printTaskToSave()); + writeToFile(taskList.getTask(i).printTaskToSave(), ui); } } - public boolean checkMark(String item) { - return item.equals("1"); + /** + * Checks if the string has the task marked as done or not done + * @param stringInput The String that stores the done status of the task + * @return True is task is done, False if not done + */ + public boolean isMark(String stringInput) { + boolean isDone = stringInput.equals("1"); + return isDone; } } From d17d6b445ff6e0770e2386fd2047b015002b6e17 Mon Sep 17 00:00:00 2001 From: ZH Date: Fri, 6 Oct 2023 21:22:57 +0800 Subject: [PATCH 45/46] Update README.md --- docs/README.md | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/docs/README.md b/docs/README.md index 2b241740d..67b4c742c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -33,7 +33,6 @@ Prerequisites: JDK 11, update Intellij to the most recent version. ### Task Creation Create 3 types of Tasks: -
1. Todo: Tasks which are not time sensitive. @@ -51,16 +50,13 @@ Create 3 types of Tasks: Example:
`event project meeting /from Mon 2pm /to 4pm` -
### Task Viewing Lists all the task in the task list. Format: `list` -
-Usage Example list ____________________________________________________________ @@ -68,16 +64,13 @@ Format: `list` 1.[T][ ] borrow book 2.[D][ ] return book (by: Sunday) ____________________________________________________________ -
+ ### Task Marking Keep track of Task completion. 1. '[X]' Indicates Tasks that are completed, using mark command. 2. '[ ]' Indicates Tasks that are not completed, using unmark command. -
- -Mark Task Format: `mark {task index}` @@ -89,11 +82,6 @@ Example: [D][X] return book (by: Sunday) ____________________________________________________________ -
- -
- -Unmark Task Format: `unmark {task index}` @@ -105,7 +93,6 @@ Example: [D][ ] return book (by: Sunday) ____________________________________________________________ -
### Task Saving Store and retrieve your tasks into a dedicated save file. @@ -119,9 +106,6 @@ Store and retrieve your tasks into a dedicated save file. Search for keyword using the Find function. Format: `find {keyword}` -
- - Usage Example find book ____________________________________________________________ @@ -130,7 +114,6 @@ Format: `find {keyword}` 2.[D][ ] return book (by: Sunday) ____________________________________________________________ -
### Task Deletion @@ -138,10 +121,6 @@ Remove stored Tasks using the Delete function. Format: `delete {task index}` -
- -Usage Example - delete 2 ____________________________________________________________ Noted. I've removed this task: @@ -149,7 +128,6 @@ Format: `delete {task index}` Now you have 1 tasks in the list. ____________________________________________________________ -
### End Program Terminates the chatbot program. From 7ee16942c8f97e17efd218792143aafc77ac2ba3 Mon Sep 17 00:00:00 2001 From: ZH Date: Fri, 6 Oct 2023 21:39:09 +0800 Subject: [PATCH 46/46] Update Code Quality --- .../java/kenergeticbot/command/BooleanChecks.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/main/java/kenergeticbot/command/BooleanChecks.java b/src/main/java/kenergeticbot/command/BooleanChecks.java index 34e036b4a..ebed4da19 100644 --- a/src/main/java/kenergeticbot/command/BooleanChecks.java +++ b/src/main/java/kenergeticbot/command/BooleanChecks.java @@ -1,16 +1,6 @@ package kenergeticbot.command; public class BooleanChecks { - //Returns True if the text contains "mark", False if not - public static boolean checkTextForMark(String item) { - return item.length() > 4 && item.startsWith("mark"); - } - - //Returns True if the text contains "unmark", False if not - public static boolean checkTextForUnmark(String item) { - return item.length() > 6 && item.startsWith("unmark"); - } - //Returns True if the text contains "todo", False if not public static boolean checkTextForTodo(String item) { return item.startsWith("todo"); @@ -25,7 +15,4 @@ public static boolean checkTextForDeadline(String item) { public static boolean checkTextForEvent(String item) { return item.startsWith("event"); } - public static boolean checkTextForDelete(String item) { - return item.startsWith("delete"); - } }