From c606c3c09b6a5889046492624c0b739199e54c52 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Thu, 24 Aug 2023 22:48:20 +0800 Subject: [PATCH 01/46] Rename main class --- README.md | 4 ++-- src/main/java/{Duke.java => Alan.java} | 2 +- text-ui-test/runtest.bat | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename src/main/java/{Duke.java => Alan.java} (94%) diff --git a/README.md b/README.md index 8715d4d91..6cf65d922 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# Alan 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/Alan.java` file, right-click it, and choose `Run Alan.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/Alan.java similarity index 94% rename from src/main/java/Duke.java rename to src/main/java/Alan.java index 5d313334c..4ef3a3e3e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Alan.java @@ -1,4 +1,4 @@ -public class Duke { +public class Alan { public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..c0bce8993 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 Alan < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From e3f01fab05b620aae891073d626a8ffae648f870 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Thu, 24 Aug 2023 23:16:00 +0800 Subject: [PATCH 02/46] Add greeting and exit functions --- src/main/java/Alan.java | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 4ef3a3e3e..d20281122 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -1,10 +1,27 @@ public class Alan { + public static void printGreet() { + String man = " @/\n" + + "/| \n" + + "/ \\"; + + System.out.println(man); + System.out.println("Hello! I'm Alan"); + System.out.println("What can I do for you?"); + } + + public static void printExit() { + System.out.println("Bye. Hope to see you again soon!"); + } + + public static void printHorizontalLine() { + System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + } + public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + printHorizontalLine(); + printGreet(); + printHorizontalLine(); + printExit(); + printHorizontalLine(); } } From 3afcb6bfec2f0126f9f0fa9ff2001286d0418853 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Thu, 31 Aug 2023 14:17:21 +0800 Subject: [PATCH 03/46] Refactor printHorrizontalLine function --- src/main/java/Alan.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index d20281122..65c233cd6 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -1,5 +1,6 @@ public class Alan { public static void printGreet() { + printHorizontalLine(); String man = " @/\n" + "/| \n" + "/ \\"; @@ -7,10 +8,12 @@ public static void printGreet() { System.out.println(man); System.out.println("Hello! I'm Alan"); System.out.println("What can I do for you?"); + printHorizontalLine(); } public static void printExit() { System.out.println("Bye. Hope to see you again soon!"); + printHorizontalLine(); } public static void printHorizontalLine() { @@ -18,10 +21,7 @@ public static void printHorizontalLine() { } public static void main(String[] args) { - printHorizontalLine(); printGreet(); - printHorizontalLine(); printExit(); - printHorizontalLine(); } } From 184e7083e32fcfe09150b75a1832ed810ebccde5 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Thu, 31 Aug 2023 14:56:21 +0800 Subject: [PATCH 04/46] Add echo function --- src/main/java/Alan.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 65c233cd6..61ddbc3cb 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -1,4 +1,7 @@ +import java.util.Scanner; + public class Alan { + public static void printGreet() { printHorizontalLine(); String man = " @/\n" + @@ -20,8 +23,26 @@ public static void printHorizontalLine() { System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } + public static void echoUserInput() { + String userLine; + Scanner userInput = new Scanner(System.in); + + System.out.print("Input: "); + userLine = userInput.nextLine(); + + printHorizontalLine(); + if (userLine.equals("bye")) { + printExit(); + } else { + System.out.println(userLine); + printHorizontalLine(); + echoUserInput(); + } + + } + public static void main(String[] args) { printGreet(); - printExit(); + echoUserInput(); } } From cf80b829e767790f634c7240ca29c8a4700a85b3 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Thu, 31 Aug 2023 17:44:29 +0800 Subject: [PATCH 05/46] Add list functionality --- src/main/java/Alan.java | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 61ddbc3cb..2a4b781e2 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -23,26 +23,32 @@ public static void printHorizontalLine() { System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } - public static void echoUserInput() { - String userLine; - Scanner userInput = new Scanner(System.in); + public static void main(String[] args) { + String[] taskList = new String[100]; + int currentTaskListIndex = 0; - System.out.print("Input: "); - userLine = userInput.nextLine(); + printGreet(); - printHorizontalLine(); - if (userLine.equals("bye")) { - printExit(); - } else { - System.out.println(userLine); + String userInput = " "; + Scanner in = new Scanner(System.in); + + while(!userInput.equals("bye")) { + System.out.print("Input: "); + userInput = in.nextLine(); printHorizontalLine(); - echoUserInput(); - } - } + if (userInput.equals("list")) { + for (int i = 0; i < currentTaskListIndex; i++) { + System.out.println((i + 1) + ". " + taskList[i]); + } + } else { + taskList[currentTaskListIndex] = userInput; + currentTaskListIndex++; + System.out.println("added: " + userInput); + } + printHorizontalLine(); + } - public static void main(String[] args) { - printGreet(); - echoUserInput(); + printExit(); } } From 1afaaf11dc7210b33d7b12b561c1cd55ef9d646a Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Sun, 3 Sep 2023 23:10:20 +0800 Subject: [PATCH 06/46] Add Task.java class --- src/main/java/Task.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..cff46e380 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,29 @@ +public class Task { + private String description; + private boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public boolean getDone() { + return isDone; + } + + public void setDone(boolean isDone) { + this.isDone = isDone; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); + } +} From e331bb98f7339b80f2a004f791c0dbaf9325b7cf Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Sun, 3 Sep 2023 23:43:13 +0800 Subject: [PATCH 07/46] Add mark and unmark tasks feature --- src/main/java/Alan.java | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 2a4b781e2..38d0125b2 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -24,8 +24,8 @@ public static void printHorizontalLine() { } public static void main(String[] args) { - String[] taskList = new String[100]; - int currentTaskListIndex = 0; + Task[] taskList = new Task[101]; + int currentTaskListIndex = 1; printGreet(); @@ -33,16 +33,38 @@ public static void main(String[] args) { Scanner in = new Scanner(System.in); while(!userInput.equals("bye")) { + //Read user input System.out.print("Input: "); userInput = in.nextLine(); + printHorizontalLine(); - if (userInput.equals("list")) { - for (int i = 0; i < currentTaskListIndex; i++) { - System.out.println((i + 1) + ". " + taskList[i]); + if (userInput.equals("list")) { //print the tasks in the lists + System.out.println("Here are the tasks in your list:"); + for (int i = 1; i < currentTaskListIndex; i++) { + System.out.println((i) + ". [" + taskList[i].getStatusIcon() + "] " + taskList[i].getDescription()); } - } else { - taskList[currentTaskListIndex] = userInput; + } else if (userInput.startsWith("mark")) { //mark tasks as done + String[] words = userInput.split(" "); + + taskList[Integer.parseInt(words[1])].setDone(true); + + System.out.println("Alright! I've marked this task as done:"); + System.out.println("\t[" + taskList[Integer.parseInt(words[1])].getStatusIcon() + "] " + + taskList[Integer.parseInt(words[1])].getDescription()); + + } else if (userInput.startsWith("unmark")) { //unmark tasks as undone + String[] words = userInput.split(" "); + + taskList[Integer.parseInt(words[1])].setDone(false); + + System.out.println("Ok, I've marked this task as not done yet:"); + System.out.println("\t[" + taskList[Integer.parseInt(words[1])].getStatusIcon() + "] " + + taskList[Integer.parseInt(words[1])].getDescription()); + + } else { //add a new task to the list + Task task = new Task(userInput); + taskList[currentTaskListIndex] = task; currentTaskListIndex++; System.out.println("added: " + userInput); } From 951a3e6d1d1498ec0ca14f7eb092f146f1fbe114 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Sun, 3 Sep 2023 23:56:50 +0800 Subject: [PATCH 08/46] Main.java: Fix handling exit command --- src/main/java/Alan.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 38d0125b2..cbe07ba1b 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -16,7 +16,6 @@ public static void printGreet() { public static void printExit() { System.out.println("Bye. Hope to see you again soon!"); - printHorizontalLine(); } public static void printHorizontalLine() { @@ -32,14 +31,15 @@ public static void main(String[] args) { String userInput = " "; Scanner in = new Scanner(System.in); - while(!userInput.equals("bye")) { + do { //Read user input System.out.print("Input: "); userInput = in.nextLine(); printHorizontalLine(); - - if (userInput.equals("list")) { //print the tasks in the lists + if (userInput.equals("bye")) { + printExit(); + } else if (userInput.equals("list")) { //print the tasks in the lists System.out.println("Here are the tasks in your list:"); for (int i = 1; i < currentTaskListIndex; i++) { System.out.println((i) + ". [" + taskList[i].getStatusIcon() + "] " + taskList[i].getDescription()); @@ -68,9 +68,10 @@ public static void main(String[] args) { currentTaskListIndex++; System.out.println("added: " + userInput); } + printHorizontalLine(); - } - printExit(); + } while(!userInput.equals("bye")); + } } From 7a7066d49ab828c0ea359b7ac3c0ca81c40762cd Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Mon, 4 Sep 2023 00:06:58 +0800 Subject: [PATCH 09/46] Update Alan.java to follow coding standard --- src/main/java/Alan.java | 45 +++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index cbe07ba1b..3a210be55 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -2,7 +2,7 @@ public class Alan { - public static void printGreet() { + public static void printGreetingMessage() { printHorizontalLine(); String man = " @/\n" + "/| \n" + @@ -14,7 +14,7 @@ public static void printGreet() { printHorizontalLine(); } - public static void printExit() { + public static void printExitMessage() { System.out.println("Bye. Hope to see you again soon!"); } @@ -23,10 +23,10 @@ public static void printHorizontalLine() { } public static void main(String[] args) { - Task[] taskList = new Task[101]; - int currentTaskListIndex = 1; + Task[] tasks = new Task[101]; + int currentTasksIndex = 1; - printGreet(); + printGreetingMessage(); String userInput = " "; Scanner in = new Scanner(System.in); @@ -37,35 +37,40 @@ public static void main(String[] args) { userInput = in.nextLine(); printHorizontalLine(); + if (userInput.equals("bye")) { - printExit(); - } else if (userInput.equals("list")) { //print the tasks in the lists + printExitMessage(); + } else if (userInput.equals("list")) { + //print the tasks in the lists System.out.println("Here are the tasks in your list:"); - for (int i = 1; i < currentTaskListIndex; i++) { - System.out.println((i) + ". [" + taskList[i].getStatusIcon() + "] " + taskList[i].getDescription()); + for (int i = 1; i < currentTasksIndex; i++) { + System.out.println((i) + ". [" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); } - } else if (userInput.startsWith("mark")) { //mark tasks as done + } else if (userInput.startsWith("mark")) { + //mark tasks as done String[] words = userInput.split(" "); - taskList[Integer.parseInt(words[1])].setDone(true); + tasks[Integer.parseInt(words[1])].setDone(true); System.out.println("Alright! I've marked this task as done:"); - System.out.println("\t[" + taskList[Integer.parseInt(words[1])].getStatusIcon() + "] " + - taskList[Integer.parseInt(words[1])].getDescription()); + System.out.println("\t[" + tasks[Integer.parseInt(words[1])].getStatusIcon() + "] " + + tasks[Integer.parseInt(words[1])].getDescription()); - } else if (userInput.startsWith("unmark")) { //unmark tasks as undone + } else if (userInput.startsWith("unmark")) { + //unmark tasks as undone String[] words = userInput.split(" "); - taskList[Integer.parseInt(words[1])].setDone(false); + tasks[Integer.parseInt(words[1])].setDone(false); System.out.println("Ok, I've marked this task as not done yet:"); - System.out.println("\t[" + taskList[Integer.parseInt(words[1])].getStatusIcon() + "] " + - taskList[Integer.parseInt(words[1])].getDescription()); + System.out.println("\t[" + tasks[Integer.parseInt(words[1])].getStatusIcon() + "] " + + tasks[Integer.parseInt(words[1])].getDescription()); - } else { //add a new task to the list + } else { + //add a new task to the list Task task = new Task(userInput); - taskList[currentTaskListIndex] = task; - currentTaskListIndex++; + tasks[currentTasksIndex] = task; + currentTasksIndex++; System.out.println("added: " + userInput); } From 32601dd6f58fc09fa54cb3ef250a65fd2b5ad2bc Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Thu, 7 Sep 2023 21:22:09 +0800 Subject: [PATCH 10/46] Add todo, deadline and event tasks --- src/main/java/Alan.java | 53 ++++++++++++++++++++++++++++--------- src/main/java/Deadline.java | 13 +++++++++ src/main/java/Event.java | 16 +++++++++++ src/main/java/Task.java | 6 +++++ src/main/java/Todo.java | 13 +++++++++ 5 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 3a210be55..0b971d9e1 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -19,7 +19,15 @@ public static void printExitMessage() { } public static void printHorizontalLine() { - System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + } + + public static void printTaskMessage(String taskTypeIcon, String taskIcon, String taskDescription) { + System.out.println("\t[" + taskTypeIcon + "][" + taskIcon + "] " + taskDescription); + } + + public static String getStringBetweenTwoCharacters (String input, char from, char to) { + return input.substring(input.indexOf(from) + 1, input.indexOf(to)); } public static void main(String[] args) { @@ -44,7 +52,8 @@ public static void main(String[] args) { //print the tasks in the lists System.out.println("Here are the tasks in your list:"); for (int i = 1; i < currentTasksIndex; i++) { - System.out.println((i) + ". [" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); + System.out.print((i) + ". "); + System.out.println(tasks[i]); } } else if (userInput.startsWith("mark")) { //mark tasks as done @@ -53,9 +62,7 @@ public static void main(String[] args) { tasks[Integer.parseInt(words[1])].setDone(true); System.out.println("Alright! I've marked this task as done:"); - System.out.println("\t[" + tasks[Integer.parseInt(words[1])].getStatusIcon() + "] " + - tasks[Integer.parseInt(words[1])].getDescription()); - + System.out.println(tasks[Integer.parseInt(words[1])]); } else if (userInput.startsWith("unmark")) { //unmark tasks as undone String[] words = userInput.split(" "); @@ -63,15 +70,35 @@ public static void main(String[] args) { tasks[Integer.parseInt(words[1])].setDone(false); System.out.println("Ok, I've marked this task as not done yet:"); - System.out.println("\t[" + tasks[Integer.parseInt(words[1])].getStatusIcon() + "] " + - tasks[Integer.parseInt(words[1])].getDescription()); - - } else { - //add a new task to the list - Task task = new Task(userInput); - tasks[currentTasksIndex] = task; + System.out.println(tasks[Integer.parseInt(words[1])]); + } else if (userInput.startsWith("todo")) { + //add todo task to the list + tasks[currentTasksIndex] = new Todo(userInput.replace("todo ", "")); + System.out.println("added: " + tasks[currentTasksIndex]); + System.out.println("Now you have " + currentTasksIndex + " in the list."); + currentTasksIndex++; + } else if (userInput.startsWith("deadline")) { + //add deadline task to the list + String filteredUserInput = userInput.replace("deadline ", ""); + String[] words = filteredUserInput.split(" /by "); + tasks[currentTasksIndex] = new Deadline(words[0], words[1]); + System.out.println("added: " + tasks[currentTasksIndex]); + System.out.println("Now you have " + currentTasksIndex + " in the list."); + currentTasksIndex++; + } else if (userInput.startsWith("event")) { + //add event to task + String filteredUserInput = userInput.replace("event ", ""); + String[] words = filteredUserInput.split(" /"); + String description = words[0]; + String from = words[1].substring(5); + String to = words[2].substring(3); + + System.out.println(from + "\n" + to); + + tasks[currentTasksIndex] = new Event(description, from, to); + System.out.println("added: " + tasks[currentTasksIndex]); + System.out.println("Now you have " + currentTasksIndex + " in the list."); currentTasksIndex++; - System.out.println("added: " + userInput); } printHorizontalLine(); diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..1e235d849 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,13 @@ +public class Deadline extends Task { + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + "[" + super.getStatusIcon() + "] " + super.toString() + " (by: " + by + ")"; + } +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..92081102c --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,16 @@ +public class Event extends Task { + protected String from; + protected String to; + + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + } + + @Override + public String toString() { + return "[E]" + "[" + super.getStatusIcon() + "] " + super.toString() + + " (from: " + from + " | to: "+ to + ")"; + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index cff46e380..a7578ae77 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -26,4 +26,10 @@ public void setDone(boolean isDone) { public String getStatusIcon() { return (isDone ? "X" : " "); } + + @Override + public String toString() { + return getDescription(); + } } + diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..b3e18f1c9 --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,13 @@ +public class Todo extends Task { + private boolean isDone; + + public Todo(String description) { + super(description); + isDone = false; + } + + @Override + public String toString() { + return "[T]" + "[" + super.getStatusIcon() + "] " + super.toString(); + } +} From ff607090c6301f2b661520d7fe41e3f91c21c036 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Thu, 7 Sep 2023 21:47:07 +0800 Subject: [PATCH 11/46] Update input.txt and EXPECTED.TXT for automated testing --- text-ui-test/EXPECTED.TXT | 49 +++++++++++++++++++++++++++++++++------ text-ui-test/input.txt | 8 +++++++ 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..9466b7119 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,42 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| - +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + @/ +/| +/ \ +Hello! I'm Alan +What can I do for you? +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Input: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +added: [T][ ] borrow book +Now you have 1 in the list. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Input: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Here are the tasks in your list: +1. [T][ ] borrow book +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Input: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +added: [D][ ] return book (by: Sunday) +Now you have 2 in the list. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Input: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Mon 2pm +4pm +added: [E][ ] project meeting (from: Mon 2pm | to: 4pm) +Now you have 3 in the list. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Input: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Alright! I've marked this task as done: +[T][X] borrow book +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Input: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Ok, I've marked this task as not done yet: +[T][ ] borrow book +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Input: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +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) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Input: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Bye. Hope to see you again soon! +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..11d693542 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,8 @@ +todo borrow book +list +deadline return book /by Sunday +event project meeting /from Mon 2pm /to 4pm +mark 1 +unmark 1 +list +bye \ No newline at end of file From 3550db10ed604b487f5cdc5244f4b76fbcbb3ae9 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Thu, 7 Sep 2023 23:46:18 +0800 Subject: [PATCH 12/46] Refactor Alan class --- src/main/java/Alan.java | 116 +++++++++++++++++++++++---------------- src/main/java/Event.java | 3 +- 2 files changed, 71 insertions(+), 48 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 0b971d9e1..2e38ad646 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -1,14 +1,16 @@ import java.util.Scanner; public class Alan { + static final int FROM_KEYWORD_END_INDEX = 5; + static final int TO_KEYWORD_END_INDEX = 3; public static void printGreetingMessage() { printHorizontalLine(); - String man = " @/\n" + - "/| \n" + - "/ \\"; + String manDrawing = " @/\n" + + "/| \n" + + "/ \\"; - System.out.println(man); + System.out.println(manDrawing); System.out.println("Hello! I'm Alan"); System.out.println("What can I do for you?"); printHorizontalLine(); @@ -22,12 +24,64 @@ public static void printHorizontalLine() { System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } - public static void printTaskMessage(String taskTypeIcon, String taskIcon, String taskDescription) { - System.out.println("\t[" + taskTypeIcon + "][" + taskIcon + "] " + taskDescription); + public static void printTaskAddedMessage(Task[] tasks, int currentTasksIndex) { + System.out.println("added: " + tasks[currentTasksIndex]); + System.out.println("Now you have " + currentTasksIndex + " in the list."); } - public static String getStringBetweenTwoCharacters (String input, char from, char to) { - return input.substring(input.indexOf(from) + 1, input.indexOf(to)); + public static void listCommandHandler(Task[] tasks, int currentTasksIndex) { + System.out.println("Here are the tasks in your list:"); + + for (int i = 1; i < currentTasksIndex; i++) { + System.out.print((i) + ". "); + System.out.println(tasks[i]); + } + } + + public static void markingCommandHandler(String userInput, Task[] tasks, boolean isMark) { + String[] words = userInput.split(" "); + int selectedTaskIndex = Integer.parseInt(words[1]); + + if (isMark) { + tasks[selectedTaskIndex].setDone(true); + System.out.println("Alright! I've marked this task as done:"); + } else { + tasks[selectedTaskIndex].setDone(false); + System.out.println("Ok, I've marked this task as not done yet:"); + } + + System.out.println(tasks[selectedTaskIndex]); + } + + public static void todoCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) { + String description = userInput.replace("todo ", ""); + tasks[currentTasksIndex] = new Todo(description); + + printTaskAddedMessage(tasks, currentTasksIndex); + } + + public static void deadlineCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) { + String filteredUserInput = userInput.replace("deadline ", ""); + String[] words = filteredUserInput.split(" /by "); + String description = words[0]; + String by = words[1]; + + tasks[currentTasksIndex] = new Deadline(description, by); + + printTaskAddedMessage(tasks, currentTasksIndex); + } + + public static void eventCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) { + String filteredUserInput = userInput.replace("event ", ""); + String[] words = filteredUserInput.split(" /"); + String description = words[0]; + + String from = words[1].substring(FROM_KEYWORD_END_INDEX); + String to = words[2].substring(TO_KEYWORD_END_INDEX); + + tasks[currentTasksIndex] = new Event(description, from, to); + + printTaskAddedMessage(tasks, currentTasksIndex); } public static void main(String[] args) { @@ -36,7 +90,7 @@ public static void main(String[] args) { printGreetingMessage(); - String userInput = " "; + String userInput = null; Scanner in = new Scanner(System.in); do { @@ -50,60 +104,30 @@ public static void main(String[] args) { printExitMessage(); } else if (userInput.equals("list")) { //print the tasks in the lists - System.out.println("Here are the tasks in your list:"); - for (int i = 1; i < currentTasksIndex; i++) { - System.out.print((i) + ". "); - System.out.println(tasks[i]); - } + listCommandHandler(tasks, currentTasksIndex); } else if (userInput.startsWith("mark")) { //mark tasks as done - String[] words = userInput.split(" "); - - tasks[Integer.parseInt(words[1])].setDone(true); - - System.out.println("Alright! I've marked this task as done:"); - System.out.println(tasks[Integer.parseInt(words[1])]); + markingCommandHandler(userInput, tasks, true); } else if (userInput.startsWith("unmark")) { //unmark tasks as undone - String[] words = userInput.split(" "); - - tasks[Integer.parseInt(words[1])].setDone(false); - - System.out.println("Ok, I've marked this task as not done yet:"); - System.out.println(tasks[Integer.parseInt(words[1])]); + markingCommandHandler(userInput, tasks, false); } else if (userInput.startsWith("todo")) { //add todo task to the list - tasks[currentTasksIndex] = new Todo(userInput.replace("todo ", "")); - System.out.println("added: " + tasks[currentTasksIndex]); - System.out.println("Now you have " + currentTasksIndex + " in the list."); + todoCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; } else if (userInput.startsWith("deadline")) { //add deadline task to the list - String filteredUserInput = userInput.replace("deadline ", ""); - String[] words = filteredUserInput.split(" /by "); - tasks[currentTasksIndex] = new Deadline(words[0], words[1]); - System.out.println("added: " + tasks[currentTasksIndex]); - System.out.println("Now you have " + currentTasksIndex + " in the list."); + deadlineCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; } else if (userInput.startsWith("event")) { //add event to task - String filteredUserInput = userInput.replace("event ", ""); - String[] words = filteredUserInput.split(" /"); - String description = words[0]; - String from = words[1].substring(5); - String to = words[2].substring(3); - - System.out.println(from + "\n" + to); - - tasks[currentTasksIndex] = new Event(description, from, to); - System.out.println("added: " + tasks[currentTasksIndex]); - System.out.println("Now you have " + currentTasksIndex + " in the list."); + eventCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; } printHorizontalLine(); - } while(!userInput.equals("bye")); + } while (!userInput.equals("bye")); } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 92081102c..e1ae42614 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -10,7 +10,6 @@ public Event(String description, String from, String to) { @Override public String toString() { - return "[E]" + "[" + super.getStatusIcon() + "] " + super.toString() + - " (from: " + from + " | to: "+ to + ")"; + return "[E]" + "[" + super.getStatusIcon() + "] " + super.toString() + " (from: " + from + " | to: "+ to + ")"; } } From ae095d10b5583835c5f9c33b705a14a0724e7384 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 13 Sep 2023 20:57:14 +0800 Subject: [PATCH 13/46] Refactor Alan.java --- src/main/java/Alan.java | 63 +++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 2e38ad646..6047a7146 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -3,6 +3,7 @@ public class Alan { static final int FROM_KEYWORD_END_INDEX = 5; static final int TO_KEYWORD_END_INDEX = 3; + public static int currentTasksIndex = 1; public static void printGreetingMessage() { printHorizontalLine(); @@ -29,6 +30,36 @@ public static void printTaskAddedMessage(Task[] tasks, int currentTasksIndex) { System.out.println("Now you have " + currentTasksIndex + " in the list."); } + public static void processCommandHandler(String userInput, Task[] tasks) { + String[] userInputWords = userInput.split(" "); + String command = userInputWords[0]; + + if (command.equals("bye")) { + printExitMessage(); + } else if (command.equals("list")) { + //print the tasks in the lists + listCommandHandler(tasks, currentTasksIndex); + } else if (command.equals("mark")) { + //mark tasks as done + markingCommandHandler(userInput, tasks, true); + } else if (command.equals("unmark")) { + //unmark tasks as undone + markingCommandHandler(userInput, tasks, false); + } else if (command.equals("todo")) { + //add to-do task to the list + todoCommandHandler(userInput, tasks, currentTasksIndex); + currentTasksIndex++; + } else if (command.equals("deadline")) { + //add deadline task to the list + deadlineCommandHandler(userInput, tasks, currentTasksIndex); + currentTasksIndex++; + } else if (command.equals("event")) { + //add event task to the list + eventCommandHandler(userInput, tasks, currentTasksIndex); + currentTasksIndex++; + } + } + public static void listCommandHandler(Task[] tasks, int currentTasksIndex) { System.out.println("Here are the tasks in your list:"); @@ -86,44 +117,20 @@ public static void eventCommandHandler(String userInput, Task[] tasks, int curre public static void main(String[] args) { Task[] tasks = new Task[101]; - int currentTasksIndex = 1; printGreetingMessage(); - String userInput = null; - Scanner in = new Scanner(System.in); + String userInput; + Scanner scanner = new Scanner(System.in); do { //Read user input System.out.print("Input: "); - userInput = in.nextLine(); + userInput = scanner.nextLine(); printHorizontalLine(); - if (userInput.equals("bye")) { - printExitMessage(); - } else if (userInput.equals("list")) { - //print the tasks in the lists - listCommandHandler(tasks, currentTasksIndex); - } else if (userInput.startsWith("mark")) { - //mark tasks as done - markingCommandHandler(userInput, tasks, true); - } else if (userInput.startsWith("unmark")) { - //unmark tasks as undone - markingCommandHandler(userInput, tasks, false); - } else if (userInput.startsWith("todo")) { - //add todo task to the list - todoCommandHandler(userInput, tasks, currentTasksIndex); - currentTasksIndex++; - } else if (userInput.startsWith("deadline")) { - //add deadline task to the list - deadlineCommandHandler(userInput, tasks, currentTasksIndex); - currentTasksIndex++; - } else if (userInput.startsWith("event")) { - //add event to task - eventCommandHandler(userInput, tasks, currentTasksIndex); - currentTasksIndex++; - } + processCommandHandler(userInput, tasks); printHorizontalLine(); From 31437f81ffbd0d92071e54e6f9d1025fa90718b4 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 13 Sep 2023 21:02:47 +0800 Subject: [PATCH 14/46] Add error handling for invalid command and todo tasks --- src/main/java/Alan.java | 34 ++++++++++++++++++++++--------- src/main/java/AlanExceptions.java | 11 ++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 src/main/java/AlanExceptions.java diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 6047a7146..2019d308e 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -4,6 +4,8 @@ public class Alan { static final int FROM_KEYWORD_END_INDEX = 5; static final int TO_KEYWORD_END_INDEX = 3; public static int currentTasksIndex = 1; + public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; + public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty, man"; public static void printGreetingMessage() { printHorizontalLine(); @@ -30,7 +32,7 @@ public static void printTaskAddedMessage(Task[] tasks, int currentTasksIndex) { System.out.println("Now you have " + currentTasksIndex + " in the list."); } - public static void processCommandHandler(String userInput, Task[] tasks) { + public static void processCommandHandler(String userInput, Task[] tasks) throws AlanExceptions { String[] userInputWords = userInput.split(" "); String command = userInputWords[0]; @@ -57,6 +59,8 @@ public static void processCommandHandler(String userInput, Task[] tasks) { //add event task to the list eventCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; + } else { + throw new AlanExceptions(INVALID_INPUT_COMMAND_MESSAGE); } } @@ -84,7 +88,13 @@ public static void markingCommandHandler(String userInput, Task[] tasks, boolean System.out.println(tasks[selectedTaskIndex]); } - public static void todoCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) { + public static void todoCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) throws AlanExceptions { + String[] userInputWords = userInput.split(" "); + + if (userInputWords.length == 1) { + throw new AlanExceptions(EMPTY_DESCRIPTION_MESSAGE); + } + String description = userInput.replace("todo ", ""); tasks[currentTasksIndex] = new Todo(description); @@ -120,20 +130,24 @@ public static void main(String[] args) { printGreetingMessage(); - String userInput; + String userInput = null; Scanner scanner = new Scanner(System.in); do { - //Read user input - System.out.print("Input: "); - userInput = scanner.nextLine(); - - printHorizontalLine(); + try { + //Read user input + System.out.print("Input: "); + userInput = scanner.nextLine(); - processCommandHandler(userInput, tasks); + printHorizontalLine(); - printHorizontalLine(); + processCommandHandler(userInput, tasks); + } catch (AlanExceptions e) { + System.out.println(e); + } finally { + printHorizontalLine(); + } } while (!userInput.equals("bye")); } diff --git a/src/main/java/AlanExceptions.java b/src/main/java/AlanExceptions.java new file mode 100644 index 000000000..fe81230d1 --- /dev/null +++ b/src/main/java/AlanExceptions.java @@ -0,0 +1,11 @@ +public class AlanExceptions extends Exception { + String errorMessage; + public AlanExceptions(String errorMessage) { + this.errorMessage = errorMessage; + } + + @Override + public String toString() { + return errorMessage; + } +} From 511f4d2a1a82668912d558e37abfc21b786099e4 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 13 Sep 2023 21:10:31 +0800 Subject: [PATCH 15/46] Add error handling for deadline and event invalid input formatting --- src/main/java/Alan.java | 50 +++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 2019d308e..d853bb141 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -6,7 +6,9 @@ public class Alan { public static int currentTasksIndex = 1; public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty, man"; - + public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro, remember it's: /by "; + public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack"; + public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting"; public static void printGreetingMessage() { printHorizontalLine(); String manDrawing = " @/\n" + @@ -49,14 +51,17 @@ public static void processCommandHandler(String userInput, Task[] tasks) throws markingCommandHandler(userInput, tasks, false); } else if (command.equals("todo")) { //add to-do task to the list + checkEmptyDescription(userInput); todoCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; } else if (command.equals("deadline")) { //add deadline task to the list + checkEmptyDescription(userInput); deadlineCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; } else if (command.equals("event")) { //add event task to the list + checkEmptyDescription(userInput); eventCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; } else { @@ -88,22 +93,21 @@ public static void markingCommandHandler(String userInput, Task[] tasks, boolean System.out.println(tasks[selectedTaskIndex]); } - public static void todoCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) throws AlanExceptions { - String[] userInputWords = userInput.split(" "); - - if (userInputWords.length == 1) { - throw new AlanExceptions(EMPTY_DESCRIPTION_MESSAGE); - } - + public static void todoCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) { String description = userInput.replace("todo ", ""); tasks[currentTasksIndex] = new Todo(description); printTaskAddedMessage(tasks, currentTasksIndex); } - public static void deadlineCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) { + public static void deadlineCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) throws AlanExceptions { String filteredUserInput = userInput.replace("deadline ", ""); String[] words = filteredUserInput.split(" /by "); + + if (words.length == 1) { + throw new AlanExceptions(INVALID_DEADLINE_FORMAT_MESSAGE); + } + String description = words[0]; String by = words[1]; @@ -112,19 +116,37 @@ public static void deadlineCommandHandler(String userInput, Task[] tasks, int cu printTaskAddedMessage(tasks, currentTasksIndex); } - public static void eventCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) { + public static void eventCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) throws AlanExceptions { String filteredUserInput = userInput.replace("event ", ""); - String[] words = filteredUserInput.split(" /"); - String description = words[0]; + String[] splitDescriptionAndDate = filteredUserInput.split(" /from "); - String from = words[1].substring(FROM_KEYWORD_END_INDEX); - String to = words[2].substring(TO_KEYWORD_END_INDEX); + if (splitDescriptionAndDate.length == 1) { + throw new AlanExceptions(INVALID_EVENT_FROM_FORMAT_MESSAGE); + } + + String[] splitFromAndTo = splitDescriptionAndDate[1].split(" /to "); + + if (splitFromAndTo.length == 1) { + throw new AlanExceptions(INVALID_EVENT_TO_FORMAT_MESSAGE); + } + + String description = splitDescriptionAndDate[0]; + String from = splitFromAndTo[0]; + String to = splitFromAndTo[1]; tasks[currentTasksIndex] = new Event(description, from, to); printTaskAddedMessage(tasks, currentTasksIndex); } + public static void checkEmptyDescription(String userInput) throws AlanExceptions { + String[] userInputWords = userInput.split(" "); + + if (userInputWords.length == 1) { + throw new AlanExceptions(EMPTY_DESCRIPTION_MESSAGE); + } + } + public static void main(String[] args) { Task[] tasks = new Task[101]; From eb7f6aec66e05a2e3be9990f925693709a96c348 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 13 Sep 2023 21:19:00 +0800 Subject: [PATCH 16/46] Add error handling for mark/unmark unknown tasks --- src/main/java/Alan.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index d853bb141..16246be82 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -1,14 +1,13 @@ import java.util.Scanner; public class Alan { - static final int FROM_KEYWORD_END_INDEX = 5; - static final int TO_KEYWORD_END_INDEX = 3; public static int currentTasksIndex = 1; public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty, man"; public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro, remember it's: /by "; public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack"; public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting"; + public static final String INVALID_TASK_NUMBER_MESSAGE = "Hey man there's no such task"; public static void printGreetingMessage() { printHorizontalLine(); String manDrawing = " @/\n" + @@ -78,10 +77,12 @@ public static void listCommandHandler(Task[] tasks, int currentTasksIndex) { } } - public static void markingCommandHandler(String userInput, Task[] tasks, boolean isMark) { + public static void markingCommandHandler(String userInput, Task[] tasks, boolean isMark) throws AlanExceptions { String[] words = userInput.split(" "); int selectedTaskIndex = Integer.parseInt(words[1]); + checkOutOfTasksIndex(selectedTaskIndex); + if (isMark) { tasks[selectedTaskIndex].setDone(true); System.out.println("Alright! I've marked this task as done:"); @@ -147,6 +148,14 @@ public static void checkEmptyDescription(String userInput) throws AlanExceptions } } + public static void checkOutOfTasksIndex(int selectedIndex) throws AlanExceptions { + int numberOfTasks = currentTasksIndex - 1; + + if (selectedIndex > numberOfTasks) { + throw new AlanExceptions(INVALID_TASK_NUMBER_MESSAGE); + } + } + public static void main(String[] args) { Task[] tasks = new Task[101]; From 4194ba341e2166c0de419d5cc99fd4eaee0f96ee Mon Sep 17 00:00:00 2001 From: Dexter Date: Thu, 14 Sep 2023 21:35:57 +0800 Subject: [PATCH 17/46] Change message texts --- src/main/java/Alan.java | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/java/Alan.java b/src/main/java/Alan.java index 16246be82..023123dd4 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/Alan.java @@ -4,24 +4,29 @@ public class Alan { public static int currentTasksIndex = 1; public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty, man"; - public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro, remember it's: /by "; - public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack"; - public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting"; - public static final String INVALID_TASK_NUMBER_MESSAGE = "Hey man there's no such task"; + public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro...\n[Remember it's: /by ]"; + public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack\n[Remember it's: /from /to ]"; + public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting\n[Remember it's: /from /to ]"; + public static final String INVALID_TASK_NUMBER_MESSAGE = "Oof man there's no such task"; public static void printGreetingMessage() { - printHorizontalLine(); String manDrawing = " @/\n" + - "/| \n" + - "/ \\"; + "/| \n" + + "/ \\"; + String alanText = " ______ __ ______ __ __ \n" + + "/\\ __ \\ /\\ \\ /\\ __ \\ /\\ \"-.\\ \\ \n" + + "\\ \\ __ \\ \\ \\ \\____ \\ \\ __ \\ \\ \\ \\-. \\ \n" + + " \\ \\_\\ \\_\\ \\ \\_____\\ \\ \\_\\ \\_\\ \\ \\_\\\\\"\\_\\ \n" + + " \\/_/\\/_/ \\/_____/ \\/_/\\/_/ \\/_/ \\/_/ "; + + String greetMessage = "Sup dude! I'm \n" + alanText + "\n" + manDrawing + "\n" + "What can I do for you, my man?"; - System.out.println(manDrawing); - System.out.println("Hello! I'm Alan"); - System.out.println("What can I do for you?"); + printHorizontalLine(); + System.out.println(greetMessage); printHorizontalLine(); } public static void printExitMessage() { - System.out.println("Bye. Hope to see you again soon!"); + System.out.println("Later, dude! Can't wait to catch up again real soon!"); } public static void printHorizontalLine() { @@ -30,7 +35,7 @@ public static void printHorizontalLine() { public static void printTaskAddedMessage(Task[] tasks, int currentTasksIndex) { System.out.println("added: " + tasks[currentTasksIndex]); - System.out.println("Now you have " + currentTasksIndex + " in the list."); + System.out.println("Dude! You've got a solid " + currentTasksIndex + " tasks lined up on your list now!"); } public static void processCommandHandler(String userInput, Task[] tasks) throws AlanExceptions { @@ -69,7 +74,7 @@ public static void processCommandHandler(String userInput, Task[] tasks) throws } public static void listCommandHandler(Task[] tasks, int currentTasksIndex) { - System.out.println("Here are the tasks in your list:"); + System.out.println("Dude, check out these tasks on your list:"); for (int i = 1; i < currentTasksIndex; i++) { System.out.print((i) + ". "); @@ -85,10 +90,10 @@ public static void markingCommandHandler(String userInput, Task[] tasks, boolean if (isMark) { tasks[selectedTaskIndex].setDone(true); - System.out.println("Alright! I've marked this task as done:"); + System.out.println("Alright bro! This task is officially checked off:"); } else { tasks[selectedTaskIndex].setDone(false); - System.out.println("Ok, I've marked this task as not done yet:"); + System.out.println("Ok dude, I've marked this task as ain't done yet amigo:"); } System.out.println(tasks[selectedTaskIndex]); From 8365fc30a22f6a615f23494bb3f8bd5c37f6a5d4 Mon Sep 17 00:00:00 2001 From: Dexter Date: Thu, 14 Sep 2023 21:42:42 +0800 Subject: [PATCH 18/46] Organize classes into java packages --- src/main/java/{ => alan}/Alan.java | 7 +++++++ src/main/java/{ => alan}/AlanExceptions.java | 2 ++ src/main/java/{ => alan/task}/Deadline.java | 2 ++ src/main/java/{ => alan/task}/Event.java | 2 ++ src/main/java/{ => alan/task}/Task.java | 2 ++ src/main/java/{ => alan/task}/Todo.java | 2 ++ 6 files changed, 17 insertions(+) rename src/main/java/{ => alan}/Alan.java (98%) rename src/main/java/{ => alan}/AlanExceptions.java (94%) rename src/main/java/{ => alan/task}/Deadline.java (94%) rename src/main/java/{ => alan/task}/Event.java (95%) rename src/main/java/{ => alan/task}/Task.java (97%) rename src/main/java/{ => alan/task}/Todo.java (93%) diff --git a/src/main/java/Alan.java b/src/main/java/alan/Alan.java similarity index 98% rename from src/main/java/Alan.java rename to src/main/java/alan/Alan.java index 023123dd4..b6cce70b7 100644 --- a/src/main/java/Alan.java +++ b/src/main/java/alan/Alan.java @@ -1,3 +1,10 @@ +package alan; + +import alan.task.Deadline; +import alan.task.Event; +import alan.task.Task; +import alan.task.Todo; + import java.util.Scanner; public class Alan { diff --git a/src/main/java/AlanExceptions.java b/src/main/java/alan/AlanExceptions.java similarity index 94% rename from src/main/java/AlanExceptions.java rename to src/main/java/alan/AlanExceptions.java index fe81230d1..00694c3ee 100644 --- a/src/main/java/AlanExceptions.java +++ b/src/main/java/alan/AlanExceptions.java @@ -1,3 +1,5 @@ +package alan; + public class AlanExceptions extends Exception { String errorMessage; public AlanExceptions(String errorMessage) { diff --git a/src/main/java/Deadline.java b/src/main/java/alan/task/Deadline.java similarity index 94% rename from src/main/java/Deadline.java rename to src/main/java/alan/task/Deadline.java index 1e235d849..a6d2e4b00 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/alan/task/Deadline.java @@ -1,3 +1,5 @@ +package alan.task; + public class Deadline extends Task { protected String by; diff --git a/src/main/java/Event.java b/src/main/java/alan/task/Event.java similarity index 95% rename from src/main/java/Event.java rename to src/main/java/alan/task/Event.java index e1ae42614..b79850ce7 100644 --- a/src/main/java/Event.java +++ b/src/main/java/alan/task/Event.java @@ -1,3 +1,5 @@ +package alan.task; + public class Event extends Task { protected String from; protected String to; diff --git a/src/main/java/Task.java b/src/main/java/alan/task/Task.java similarity index 97% rename from src/main/java/Task.java rename to src/main/java/alan/task/Task.java index a7578ae77..d233b189a 100644 --- a/src/main/java/Task.java +++ b/src/main/java/alan/task/Task.java @@ -1,3 +1,5 @@ +package alan.task; + public class Task { private String description; private boolean isDone; diff --git a/src/main/java/Todo.java b/src/main/java/alan/task/Todo.java similarity index 93% rename from src/main/java/Todo.java rename to src/main/java/alan/task/Todo.java index b3e18f1c9..d205787ec 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/alan/task/Todo.java @@ -1,3 +1,5 @@ +package alan.task; + public class Todo extends Task { private boolean isDone; From c7bcaaa9a37f836b65a8146732dca5656880b409 Mon Sep 17 00:00:00 2001 From: Dexter Date: Thu, 14 Sep 2023 23:19:09 +0800 Subject: [PATCH 19/46] Refactor error exception and command handling --- src/main/java/alan/Alan.java | 75 ++++++++++++-------------- src/main/java/alan/AlanExceptions.java | 49 +++++++++++++++-- 2 files changed, 80 insertions(+), 44 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index b6cce70b7..35de6cf34 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -7,14 +7,16 @@ import java.util.Scanner; +import static alan.AlanExceptions.checkDeadlineInputFormat; +import static alan.AlanExceptions.checkEmptyDescription; +import static alan.AlanExceptions.checkEventInputFromFormat; +import static alan.AlanExceptions.checkEventInputToFormat; +import static alan.AlanExceptions.checkOutOfTasksIndex; +import static alan.AlanExceptions.invalidInputCommand; + public class Alan { public static int currentTasksIndex = 1; - public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; - public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty, man"; - public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro...\n[Remember it's: /by ]"; - public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack\n[Remember it's: /from /to ]"; - public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting\n[Remember it's: /from /to ]"; - public static final String INVALID_TASK_NUMBER_MESSAGE = "Oof man there's no such task"; + public static void printGreetingMessage() { String manDrawing = " @/\n" + "/| \n" + @@ -42,41 +44,53 @@ public static void printHorizontalLine() { public static void printTaskAddedMessage(Task[] tasks, int currentTasksIndex) { System.out.println("added: " + tasks[currentTasksIndex]); - System.out.println("Dude! You've got a solid " + currentTasksIndex + " tasks lined up on your list now!"); + if (currentTasksIndex == 1) { + System.out.println("Dude! You've got a solid " + currentTasksIndex + " task lined up on your list now!"); + } else { + System.out.println("Dude! You've got a solid " + currentTasksIndex + " tasks lined up on your list now!"); + } } public static void processCommandHandler(String userInput, Task[] tasks) throws AlanExceptions { String[] userInputWords = userInput.split(" "); String command = userInputWords[0]; - if (command.equals("bye")) { + switch (command) { + case "bye": printExitMessage(); - } else if (command.equals("list")) { + break; + case "list": //print the tasks in the lists listCommandHandler(tasks, currentTasksIndex); - } else if (command.equals("mark")) { + break; + case "mark": //mark tasks as done markingCommandHandler(userInput, tasks, true); - } else if (command.equals("unmark")) { + break; + case "unmark": //unmark tasks as undone markingCommandHandler(userInput, tasks, false); - } else if (command.equals("todo")) { + break; + case "todo": //add to-do task to the list checkEmptyDescription(userInput); todoCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; - } else if (command.equals("deadline")) { + break; + case "deadline": //add deadline task to the list checkEmptyDescription(userInput); deadlineCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; - } else if (command.equals("event")) { + break; + case "event": //add event task to the list checkEmptyDescription(userInput); eventCommandHandler(userInput, tasks, currentTasksIndex); currentTasksIndex++; - } else { - throw new AlanExceptions(INVALID_INPUT_COMMAND_MESSAGE); + break; + default: + invalidInputCommand(); } } @@ -93,7 +107,7 @@ public static void markingCommandHandler(String userInput, Task[] tasks, boolean String[] words = userInput.split(" "); int selectedTaskIndex = Integer.parseInt(words[1]); - checkOutOfTasksIndex(selectedTaskIndex); + checkOutOfTasksIndex(selectedTaskIndex, currentTasksIndex); if (isMark) { tasks[selectedTaskIndex].setDone(true); @@ -117,9 +131,7 @@ public static void deadlineCommandHandler(String userInput, Task[] tasks, int cu String filteredUserInput = userInput.replace("deadline ", ""); String[] words = filteredUserInput.split(" /by "); - if (words.length == 1) { - throw new AlanExceptions(INVALID_DEADLINE_FORMAT_MESSAGE); - } + checkDeadlineInputFormat(words); String description = words[0]; String by = words[1]; @@ -133,15 +145,11 @@ public static void eventCommandHandler(String userInput, Task[] tasks, int curre String filteredUserInput = userInput.replace("event ", ""); String[] splitDescriptionAndDate = filteredUserInput.split(" /from "); - if (splitDescriptionAndDate.length == 1) { - throw new AlanExceptions(INVALID_EVENT_FROM_FORMAT_MESSAGE); - } + checkEventInputFromFormat(splitDescriptionAndDate); String[] splitFromAndTo = splitDescriptionAndDate[1].split(" /to "); - if (splitFromAndTo.length == 1) { - throw new AlanExceptions(INVALID_EVENT_TO_FORMAT_MESSAGE); - } + checkEventInputToFormat(splitFromAndTo); String description = splitDescriptionAndDate[0]; String from = splitFromAndTo[0]; @@ -152,21 +160,6 @@ public static void eventCommandHandler(String userInput, Task[] tasks, int curre printTaskAddedMessage(tasks, currentTasksIndex); } - public static void checkEmptyDescription(String userInput) throws AlanExceptions { - String[] userInputWords = userInput.split(" "); - - if (userInputWords.length == 1) { - throw new AlanExceptions(EMPTY_DESCRIPTION_MESSAGE); - } - } - - public static void checkOutOfTasksIndex(int selectedIndex) throws AlanExceptions { - int numberOfTasks = currentTasksIndex - 1; - - if (selectedIndex > numberOfTasks) { - throw new AlanExceptions(INVALID_TASK_NUMBER_MESSAGE); - } - } public static void main(String[] args) { Task[] tasks = new Task[101]; diff --git a/src/main/java/alan/AlanExceptions.java b/src/main/java/alan/AlanExceptions.java index 00694c3ee..d87922256 100644 --- a/src/main/java/alan/AlanExceptions.java +++ b/src/main/java/alan/AlanExceptions.java @@ -1,13 +1,56 @@ package alan; public class AlanExceptions extends Exception { - String errorMessage; + public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; + public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty"; + public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro...\n[Remember it's: /by ]"; + public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack\n[Remember it's: /from /to ]"; + public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting\n[Remember it's: /from /to ]"; + public static final String INVALID_TASK_NUMBER_MESSAGE = "Oof maaaan there's no such task"; public AlanExceptions(String errorMessage) { - this.errorMessage = errorMessage; + super(errorMessage); + } + + public static void checkOutOfTasksIndex(int selectedIndex, int currentTasksIndex) throws AlanExceptions { + int numberOfTasks = currentTasksIndex - 1; + + if (selectedIndex > numberOfTasks) { + throw new AlanExceptions(INVALID_TASK_NUMBER_MESSAGE); + } + } + + public static void invalidInputCommand() throws AlanExceptions { + throw new AlanExceptions(INVALID_INPUT_COMMAND_MESSAGE); + } + + public static void checkEmptyDescription(String userInput) throws AlanExceptions { + String[] userInputWords = userInput.split(" "); + + if (userInputWords.length == 1) { + throw new AlanExceptions(EMPTY_DESCRIPTION_MESSAGE); + } + } + + public static void checkDeadlineInputFormat(String[] words) throws AlanExceptions { + if (words.length == 1) { + throw new AlanExceptions(INVALID_DEADLINE_FORMAT_MESSAGE); + } + } + + public static void checkEventInputFromFormat(String[] splitDescriptionAndDate) throws AlanExceptions { + if (splitDescriptionAndDate.length == 1) { + throw new AlanExceptions(INVALID_EVENT_FROM_FORMAT_MESSAGE); + } + } + + public static void checkEventInputToFormat(String[] splitFromAndTo) throws AlanExceptions { + if (splitFromAndTo.length == 1) { + throw new AlanExceptions(INVALID_EVENT_TO_FORMAT_MESSAGE); + } } @Override public String toString() { - return errorMessage; + return super.getMessage(); } } From 17b65ef6999d4392da9de393b6bbb437ceada0c1 Mon Sep 17 00:00:00 2001 From: Dexter Date: Wed, 20 Sep 2023 02:04:52 +0800 Subject: [PATCH 20/46] Change Alan to store tasks in ArrayList --- src/main/java/alan/Alan.java | 76 +++++++++++++------------- src/main/java/alan/AlanExceptions.java | 10 +++- 2 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index 35de6cf34..4ffc83698 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -5,6 +5,7 @@ import alan.task.Task; import alan.task.Todo; +import java.util.ArrayList; import java.util.Scanner; import static alan.AlanExceptions.checkDeadlineInputFormat; @@ -15,8 +16,6 @@ import static alan.AlanExceptions.invalidInputCommand; public class Alan { - public static int currentTasksIndex = 1; - public static void printGreetingMessage() { String manDrawing = " @/\n" + "/| \n" + @@ -42,16 +41,20 @@ public static void printHorizontalLine() { System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } - public static void printTaskAddedMessage(Task[] tasks, int currentTasksIndex) { - System.out.println("added: " + tasks[currentTasksIndex]); - if (currentTasksIndex == 1) { - System.out.println("Dude! You've got a solid " + currentTasksIndex + " task lined up on your list now!"); + public static void printTaskAddedMessage(ArrayList taskList) { + int numberOfTasks = taskList.size(); + int lastTaskIndex = taskList.size() - 1; + + System.out.println("added: " + taskList.get(lastTaskIndex)); + + if (numberOfTasks == 1) { + System.out.println("Dude! You've got a solid " + numberOfTasks + " task lined up on your list now!"); } else { - System.out.println("Dude! You've got a solid " + currentTasksIndex + " tasks lined up on your list now!"); + System.out.println("Dude! You've got a solid " + numberOfTasks + " tasks lined up on your list now!"); } } - public static void processCommandHandler(String userInput, Task[] tasks) throws AlanExceptions { + public static void processCommandHandler(String userInput, ArrayList taskList) throws AlanExceptions { String[] userInputWords = userInput.split(" "); String command = userInputWords[0]; @@ -61,73 +64,70 @@ public static void processCommandHandler(String userInput, Task[] tasks) throws break; case "list": //print the tasks in the lists - listCommandHandler(tasks, currentTasksIndex); + listCommandHandler(taskList); break; case "mark": //mark tasks as done - markingCommandHandler(userInput, tasks, true); + markingCommandHandler(userInput, taskList, true); break; case "unmark": //unmark tasks as undone - markingCommandHandler(userInput, tasks, false); + markingCommandHandler(userInput, taskList, false); break; case "todo": //add to-do task to the list checkEmptyDescription(userInput); - todoCommandHandler(userInput, tasks, currentTasksIndex); - currentTasksIndex++; + todoCommandHandler(userInput, taskList); break; case "deadline": //add deadline task to the list checkEmptyDescription(userInput); - deadlineCommandHandler(userInput, tasks, currentTasksIndex); - currentTasksIndex++; + deadlineCommandHandler(userInput, taskList); break; case "event": //add event task to the list checkEmptyDescription(userInput); - eventCommandHandler(userInput, tasks, currentTasksIndex); - currentTasksIndex++; + eventCommandHandler(userInput, taskList); break; default: invalidInputCommand(); } } - public static void listCommandHandler(Task[] tasks, int currentTasksIndex) { + public static void listCommandHandler(ArrayList taskList) { System.out.println("Dude, check out these tasks on your list:"); - for (int i = 1; i < currentTasksIndex; i++) { - System.out.print((i) + ". "); - System.out.println(tasks[i]); + for (int i = 0; i < taskList.size(); i++) { + System.out.print((i + 1) + ". "); + System.out.println(taskList.get(i)); } } - public static void markingCommandHandler(String userInput, Task[] tasks, boolean isMark) throws AlanExceptions { + public static void markingCommandHandler(String userInput, ArrayList taskList, boolean isMark) throws AlanExceptions { String[] words = userInput.split(" "); - int selectedTaskIndex = Integer.parseInt(words[1]); + int selectedTaskIndex = Integer.parseInt(words[1]) - 1; - checkOutOfTasksIndex(selectedTaskIndex, currentTasksIndex); + checkOutOfTasksIndex(selectedTaskIndex, taskList); if (isMark) { - tasks[selectedTaskIndex].setDone(true); + taskList.get(selectedTaskIndex).setDone(true); System.out.println("Alright bro! This task is officially checked off:"); } else { - tasks[selectedTaskIndex].setDone(false); + taskList.get(selectedTaskIndex).setDone(false); System.out.println("Ok dude, I've marked this task as ain't done yet amigo:"); } - System.out.println(tasks[selectedTaskIndex]); + System.out.println(taskList.get(selectedTaskIndex)); } - public static void todoCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) { + public static void todoCommandHandler(String userInput, ArrayList taskList) { String description = userInput.replace("todo ", ""); - tasks[currentTasksIndex] = new Todo(description); + taskList.add(new Todo(description)); - printTaskAddedMessage(tasks, currentTasksIndex); + printTaskAddedMessage(taskList); } - public static void deadlineCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) throws AlanExceptions { + public static void deadlineCommandHandler(String userInput, ArrayList taskList) throws AlanExceptions { String filteredUserInput = userInput.replace("deadline ", ""); String[] words = filteredUserInput.split(" /by "); @@ -136,12 +136,12 @@ public static void deadlineCommandHandler(String userInput, Task[] tasks, int cu String description = words[0]; String by = words[1]; - tasks[currentTasksIndex] = new Deadline(description, by); + taskList.add(new Deadline(description, by)); - printTaskAddedMessage(tasks, currentTasksIndex); + printTaskAddedMessage(taskList); } - public static void eventCommandHandler(String userInput, Task[] tasks, int currentTasksIndex) throws AlanExceptions { + public static void eventCommandHandler(String userInput, ArrayList taskList) throws AlanExceptions { String filteredUserInput = userInput.replace("event ", ""); String[] splitDescriptionAndDate = filteredUserInput.split(" /from "); @@ -155,14 +155,14 @@ public static void eventCommandHandler(String userInput, Task[] tasks, int curre String from = splitFromAndTo[0]; String to = splitFromAndTo[1]; - tasks[currentTasksIndex] = new Event(description, from, to); + taskList.add(new Event(description, from, to)); - printTaskAddedMessage(tasks, currentTasksIndex); + printTaskAddedMessage(taskList); } public static void main(String[] args) { - Task[] tasks = new Task[101]; + ArrayList taskList = new ArrayList<>(); printGreetingMessage(); @@ -177,7 +177,7 @@ public static void main(String[] args) { printHorizontalLine(); - processCommandHandler(userInput, tasks); + processCommandHandler(userInput, taskList); } catch (AlanExceptions e) { System.out.println(e); diff --git a/src/main/java/alan/AlanExceptions.java b/src/main/java/alan/AlanExceptions.java index d87922256..7195ea5f4 100644 --- a/src/main/java/alan/AlanExceptions.java +++ b/src/main/java/alan/AlanExceptions.java @@ -1,5 +1,9 @@ package alan; +import alan.task.Task; + +import java.util.ArrayList; + public class AlanExceptions extends Exception { public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty"; @@ -11,10 +15,10 @@ public AlanExceptions(String errorMessage) { super(errorMessage); } - public static void checkOutOfTasksIndex(int selectedIndex, int currentTasksIndex) throws AlanExceptions { - int numberOfTasks = currentTasksIndex - 1; + public static void checkOutOfTasksIndex(int selectedIndex, ArrayList taskList) throws AlanExceptions { + int lastTaskIndex = taskList.size() - 1; - if (selectedIndex > numberOfTasks) { + if (selectedIndex > lastTaskIndex) { throw new AlanExceptions(INVALID_TASK_NUMBER_MESSAGE); } } From 1933454f5ebfab79566ed0fdcc0f69d04a0718cb Mon Sep 17 00:00:00 2001 From: Dexter Date: Wed, 20 Sep 2023 02:21:05 +0800 Subject: [PATCH 21/46] Add delete function --- src/main/java/alan/Alan.java | 38 ++++++++++++++++++++------ src/main/java/alan/AlanExceptions.java | 2 +- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index 4ffc83698..f57ef76b2 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -12,7 +12,7 @@ import static alan.AlanExceptions.checkEmptyDescription; import static alan.AlanExceptions.checkEventInputFromFormat; import static alan.AlanExceptions.checkEventInputToFormat; -import static alan.AlanExceptions.checkOutOfTasksIndex; +import static alan.AlanExceptions.checkOutOfTaskListIndex; import static alan.AlanExceptions.invalidInputCommand; public class Alan { @@ -41,12 +41,7 @@ public static void printHorizontalLine() { System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } - public static void printTaskAddedMessage(ArrayList taskList) { - int numberOfTasks = taskList.size(); - int lastTaskIndex = taskList.size() - 1; - - System.out.println("added: " + taskList.get(lastTaskIndex)); - + public static void printNumberOfTasksMessage(int numberOfTasks) { if (numberOfTasks == 1) { System.out.println("Dude! You've got a solid " + numberOfTasks + " task lined up on your list now!"); } else { @@ -54,6 +49,14 @@ public static void printTaskAddedMessage(ArrayList taskList) { } } + public static void printTaskAddedMessage(ArrayList taskList) { + int numberOfTasks = taskList.size(); + int lastTaskIndex = taskList.size() - 1; + + System.out.println("added: " + taskList.get(lastTaskIndex)); + printNumberOfTasksMessage(numberOfTasks); + } + public static void processCommandHandler(String userInput, ArrayList taskList) throws AlanExceptions { String[] userInputWords = userInput.split(" "); String command = userInputWords[0]; @@ -89,6 +92,10 @@ public static void processCommandHandler(String userInput, ArrayList taskL checkEmptyDescription(userInput); eventCommandHandler(userInput, taskList); break; + case "delete": + //delete task from the list + deleteCommandHandler(userInput, taskList); + break; default: invalidInputCommand(); } @@ -107,7 +114,7 @@ public static void markingCommandHandler(String userInput, ArrayList taskL String[] words = userInput.split(" "); int selectedTaskIndex = Integer.parseInt(words[1]) - 1; - checkOutOfTasksIndex(selectedTaskIndex, taskList); + checkOutOfTaskListIndex(selectedTaskIndex, taskList); if (isMark) { taskList.get(selectedTaskIndex).setDone(true); @@ -160,6 +167,21 @@ public static void eventCommandHandler(String userInput, ArrayList taskLis printTaskAddedMessage(taskList); } + public static void deleteCommandHandler(String userInput, ArrayList taskList) throws AlanExceptions { + String[] words = userInput.split(" "); + int selectedTaskIndex = Integer.parseInt(words[1]) - 1; + + checkOutOfTaskListIndex(selectedTaskIndex, taskList); + + System.out.println("Got it, dude. This task is outta here:"); + System.out.println(taskList.get(selectedTaskIndex)); + + taskList.remove(selectedTaskIndex); + + int numberOfTasks = taskList.size(); + printNumberOfTasksMessage(numberOfTasks); + } + public static void main(String[] args) { ArrayList taskList = new ArrayList<>(); diff --git a/src/main/java/alan/AlanExceptions.java b/src/main/java/alan/AlanExceptions.java index 7195ea5f4..e5acb38f0 100644 --- a/src/main/java/alan/AlanExceptions.java +++ b/src/main/java/alan/AlanExceptions.java @@ -15,7 +15,7 @@ public AlanExceptions(String errorMessage) { super(errorMessage); } - public static void checkOutOfTasksIndex(int selectedIndex, ArrayList taskList) throws AlanExceptions { + public static void checkOutOfTaskListIndex(int selectedIndex, ArrayList taskList) throws AlanExceptions { int lastTaskIndex = taskList.size() - 1; if (selectedIndex > lastTaskIndex) { From 41ce6df4089cdbcc836a9f00285cf3a2a19deb98 Mon Sep 17 00:00:00 2001 From: Dexter Date: Wed, 20 Sep 2023 22:01:56 +0800 Subject: [PATCH 22/46] Update tasks to set and get task contents --- src/main/java/alan/task/Deadline.java | 12 ++++++++++-- src/main/java/alan/task/Event.java | 20 ++++++++++++++++++-- src/main/java/alan/task/Task.java | 16 +++++++++++++++- src/main/java/alan/task/TaskType.java | 5 +++++ src/main/java/alan/task/Todo.java | 4 ++-- 5 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/main/java/alan/task/TaskType.java diff --git a/src/main/java/alan/task/Deadline.java b/src/main/java/alan/task/Deadline.java index a6d2e4b00..b76981605 100644 --- a/src/main/java/alan/task/Deadline.java +++ b/src/main/java/alan/task/Deadline.java @@ -4,12 +4,20 @@ public class Deadline extends Task { protected String by; public Deadline(String description, String by) { - super(description); + super(description, TaskType.D); + this.by = by; + } + + public String getBy() { + return by; + } + + public void setBy(String by) { this.by = by; } @Override public String toString() { - return "[D]" + "[" + super.getStatusIcon() + "] " + super.toString() + " (by: " + by + ")"; + return "[" + super.getTaskType() + "]" + "[" + super.getStatusIcon() + "] " + super.toString() + " (by: " + by + ")"; } } diff --git a/src/main/java/alan/task/Event.java b/src/main/java/alan/task/Event.java index b79850ce7..d708ad6d0 100644 --- a/src/main/java/alan/task/Event.java +++ b/src/main/java/alan/task/Event.java @@ -5,13 +5,29 @@ public class Event extends Task { protected String to; public Event(String description, String from, String to) { - super(description); + super(description, TaskType.E); this.from = from; this.to = to; } + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + @Override public String toString() { - return "[E]" + "[" + super.getStatusIcon() + "] " + super.toString() + " (from: " + from + " | to: "+ to + ")"; + return "[" + super.getTaskType() + "]" + "[" + super.getStatusIcon() + "] " + super.toString() + " (from: " + from + " | to: "+ to + ")"; } } diff --git a/src/main/java/alan/task/Task.java b/src/main/java/alan/task/Task.java index d233b189a..b861a7590 100644 --- a/src/main/java/alan/task/Task.java +++ b/src/main/java/alan/task/Task.java @@ -2,10 +2,12 @@ public class Task { private String description; + private TaskType type; private boolean isDone; - public Task(String description) { + public Task(String description, TaskType type) { this.description = description; + this.type = type; this.isDone = false; } @@ -25,10 +27,22 @@ public void setDone(boolean isDone) { this.isDone = isDone; } + public TaskType getTaskType() { + return type; + } + + public void setTaskType(TaskType type) { + this.type = type; + } + public String getStatusIcon() { return (isDone ? "X" : " "); } + public int getStatusValue() { + return (isDone ? 1 : 0); + } + @Override public String toString() { return getDescription(); diff --git a/src/main/java/alan/task/TaskType.java b/src/main/java/alan/task/TaskType.java new file mode 100644 index 000000000..4f049c9b7 --- /dev/null +++ b/src/main/java/alan/task/TaskType.java @@ -0,0 +1,5 @@ +package alan.task; + +public enum TaskType { + T, D, E +} diff --git a/src/main/java/alan/task/Todo.java b/src/main/java/alan/task/Todo.java index d205787ec..9f4c2d180 100644 --- a/src/main/java/alan/task/Todo.java +++ b/src/main/java/alan/task/Todo.java @@ -4,12 +4,12 @@ public class Todo extends Task { private boolean isDone; public Todo(String description) { - super(description); + super(description, TaskType.T); isDone = false; } @Override public String toString() { - return "[T]" + "[" + super.getStatusIcon() + "] " + super.toString(); + return "[" + super.getTaskType() + "]" + "[" + super.getStatusIcon() + "] " + super.toString(); } } From 88bace67d1a9569847d2dfec465a264133773934 Mon Sep 17 00:00:00 2001 From: Dexter Date: Wed, 20 Sep 2023 22:09:04 +0800 Subject: [PATCH 23/46] Add saving task list to text file functionality --- src/main/java/alan/Alan.java | 76 ++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index 4ffc83698..27e3aad4f 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -1,11 +1,13 @@ package alan; -import alan.task.Deadline; -import alan.task.Event; -import alan.task.Task; -import alan.task.Todo; +import alan.task.*; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; import java.util.ArrayList; +import java.util.Objects; import java.util.Scanner; import static alan.AlanExceptions.checkDeadlineInputFormat; @@ -160,6 +162,65 @@ public static void eventCommandHandler(String userInput, ArrayList taskLis printTaskAddedMessage(taskList); } + public static void saveFileHandler(ArrayList taskList) throws Exception { + String userWorkingDirectory = System.getProperty("user.dir"); + java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, "data", "tasks.txt"); + java.nio.file.Path dataFolderPath = java.nio.file.Paths.get(userWorkingDirectory, "data"); + File textFile = new File(String.valueOf(tasksFilePath)); + File folder = new File(String.valueOf(dataFolderPath)); + + //check if folder exists + if (!Files.exists(dataFolderPath)) { + folder.mkdir(); + System.out.println("Data Folder was not found!\nNew data folder has been created in" + userWorkingDirectory); + } + + //check if file exists + if (!Files.exists(tasksFilePath)) { + textFile.createNewFile(); + System.out.println("tasks.txt was not found!\nNew tasks.txt has been created in" + dataFolderPath); + } + + //input arraylist data into text file + for (int i = 0; i < taskList.size(); i++) { + String taskDataRow = getStringOfTaskInformation(taskList, i); + + if (i == 0) { + writeToFile(tasksFilePath.toString(), taskDataRow); + } else { + appendToFile(tasksFilePath.toString(), taskDataRow); + } + } + } + + private static String getStringOfTaskInformation(ArrayList taskList, int i) { + Task task = taskList.get(i); + String taskDataRow = task.getTaskType() + " | " + task.getStatusValue() + " | " + task.getDescription(); + + if (task.getTaskType() == TaskType.D) { + Deadline deadline = (Deadline) task; + taskDataRow = taskDataRow + " | " + deadline.getBy(); + } + + if (task.getTaskType() == TaskType.E) { + Event event = (Event) task; + taskDataRow = taskDataRow + " | " + event.getFrom() + "-" +event.getTo(); + } + + taskDataRow = taskDataRow + "\n"; + return taskDataRow; + } + + private static void writeToFile(String filePath, String textToAdd) throws IOException { + FileWriter fw = new FileWriter(filePath); + fw.write(textToAdd); + fw.close(); + } + private static void appendToFile(String filePath, String textToAdd) throws IOException { + FileWriter fw = new FileWriter(filePath, true); + fw.write(textToAdd); + fw.close(); + } public static void main(String[] args) { ArrayList taskList = new ArrayList<>(); @@ -186,5 +247,12 @@ public static void main(String[] args) { } } while (!userInput.equals("bye")); + //Store TaskList in Text file tasks.txt + try { + saveFileHandler(taskList); + } catch (Exception exception) { + System.out.println(exception.getMessage()); + } + } } From 94bcc13c93109c0053c6b77b346c864f5e2217fa Mon Sep 17 00:00:00 2001 From: Dexter Date: Wed, 20 Sep 2023 22:10:23 +0800 Subject: [PATCH 24/46] Update gitignore to exclude tasks.txt --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2873e189e..ccb4f7cc5 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT +data/tasks.txt From a49e395612fadf1a379407ff76bbf10c106bc2d3 Mon Sep 17 00:00:00 2001 From: Dexter Date: Wed, 20 Sep 2023 23:55:23 +0800 Subject: [PATCH 25/46] Add saving and loading tasks from text file --- src/main/java/alan/Alan.java | 84 +++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 5 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index 27e3aad4f..6476b097f 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -3,11 +3,11 @@ import alan.task.*; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; -import java.util.Objects; import java.util.Scanner; import static alan.AlanExceptions.checkDeadlineInputFormat; @@ -172,13 +172,13 @@ public static void saveFileHandler(ArrayList taskList) throws Exception { //check if folder exists if (!Files.exists(dataFolderPath)) { folder.mkdir(); - System.out.println("Data Folder was not found!\nNew data folder has been created in" + userWorkingDirectory); + System.out.println("Data Folder was not found!\nIt's ok...new data folder has been created in" + userWorkingDirectory); } //check if file exists if (!Files.exists(tasksFilePath)) { textFile.createNewFile(); - System.out.println("tasks.txt was not found!\nNew tasks.txt has been created in" + dataFolderPath); + System.out.println("tasks.txt was not found!\nIt's ok...new tasks.txt has been created in " + dataFolderPath); } //input arraylist data into text file @@ -222,9 +222,83 @@ private static void appendToFile(String filePath, String textToAdd) throws IOExc fw.close(); } + private static void readFileHandler(ArrayList taskList) throws FileNotFoundException { + String userWorkingDirectory = System.getProperty("user.dir"); + java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, "data", "tasks.txt"); + File textFile = new File(String.valueOf(tasksFilePath)); + if (textFile.length() != 0) { //check if the file is empty + //read the file and store into ArrayList + Scanner s = new Scanner(textFile); + ArrayList list = new ArrayList(); + + while (s.hasNext()){ + list.add(s.nextLine()); + } + s.close(); + + //extract data and store into taskList + //todo: after this line should check if the data is in correct format + for (String task : list) { + String[] splitTaskString = task.split(" \\| "); + String taskType = splitTaskString[0]; + String isDoneString = splitTaskString[1]; + String description = splitTaskString[2]; + + boolean isDone = isDoneStringToBoolean(isDoneString); + + switch (taskType) { + case "T": + taskList.add(new Todo(description)); + break; + case "D": + String by = splitTaskString[3]; + taskList.add(new Deadline(description, by)); + break; + case "E": + String date = splitTaskString[3]; + String[] splitDate = date.split("-"); + String from = splitDate[0]; + String to = splitDate[1]; + + taskList.add(new Event(description, from, to)); + break; + default: + //todo handle invalid task type + break; + } + + int lastTaskIndex = taskList.size() - 1; + taskList.get(lastTaskIndex).setDone(isDone); + } + + } + } + + private static boolean isDoneStringToBoolean (String string) { + boolean isDone = false; + + if (string.equals("1")) { + isDone = true; + } + + if (string.equals("0")) { + isDone = false; + } + + //todo check if isDone is correct value, error handling + + return isDone; + } + public static void main(String[] args) { ArrayList taskList = new ArrayList<>(); + try { + readFileHandler(taskList); + } catch (Exception exception) { + System.out.println(exception.getMessage()); + } + printGreetingMessage(); String userInput = null; @@ -240,8 +314,8 @@ public static void main(String[] args) { processCommandHandler(userInput, taskList); - } catch (AlanExceptions e) { - System.out.println(e); + } catch (AlanExceptions alanExceptions) { + System.out.println(alanExceptions.getMessage()); } finally { printHorizontalLine(); } From 425e388e9d27cc774adee099833aab90ba26ac36 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Thu, 21 Sep 2023 00:14:42 +0800 Subject: [PATCH 26/46] Refactor read function in Alan.java --- src/main/java/alan/Alan.java | 76 +++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index efd515f83..f83e4b963 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -250,48 +250,54 @@ private static void readFileHandler(ArrayList taskList) throws FileNotFoun if (textFile.length() != 0) { //check if the file is empty //read the file and store into ArrayList Scanner s = new Scanner(textFile); - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList<>(); while (s.hasNext()){ list.add(s.nextLine()); } s.close(); - //extract data and store into taskList - //todo: after this line should check if the data is in correct format - for (String task : list) { - String[] splitTaskString = task.split(" \\| "); - String taskType = splitTaskString[0]; - String isDoneString = splitTaskString[1]; - String description = splitTaskString[2]; - - boolean isDone = isDoneStringToBoolean(isDoneString); - - switch (taskType) { - case "T": - taskList.add(new Todo(description)); - break; - case "D": - String by = splitTaskString[3]; - taskList.add(new Deadline(description, by)); - break; - case "E": - String date = splitTaskString[3]; - String[] splitDate = date.split("-"); - String from = splitDate[0]; - String to = splitDate[1]; - - taskList.add(new Event(description, from, to)); - break; - default: - //todo handle invalid task type - break; - } - - int lastTaskIndex = taskList.size() - 1; - taskList.get(lastTaskIndex).setDone(isDone); - } + extractAndStoreDataInTaskList(taskList, list); + + } + } + private static void extractAndStoreDataInTaskList(ArrayList taskList, ArrayList list) { + //todo: after this line should check if the data is in correct format + for (String task : list) { + String[] splitTaskString = task.split(" \\| "); + String taskType = splitTaskString[0]; + String isDoneString = splitTaskString[1]; + String description = splitTaskString[2]; + boolean isDone = isDoneStringToBoolean(isDoneString); + + addTaskToTaskList(taskList, taskType, description, splitTaskString); + + int lastTaskIndex = taskList.size() - 1; + taskList.get(lastTaskIndex).setDone(isDone); + } + } + + private static void addTaskToTaskList(ArrayList taskList, String taskType, String description, String[] splitTaskString) { + switch (taskType) { + case "T": + taskList.add(new Todo(description)); + break; + case "D": + String by = splitTaskString[3]; + taskList.add(new Deadline(description, by)); + break; + case "E": + String date = splitTaskString[3]; + String[] splitDate = date.split("-"); + String from = splitDate[0]; + String to = splitDate[1]; + + taskList.add(new Event(description, from, to)); + break; + default: + //todo handle invalid task type + break; } } From b6ecfbedf14d43a5d14c51916fa67f6c7b4615a2 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 27 Sep 2023 00:27:19 +0800 Subject: [PATCH 27/46] .gitignore: add MANIFEST.MF --- src/main/java/META-INF/MANIFEST.MF | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..07c1f045a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: alan.Alan + From eeee528819e28ba7766c2d2a090bddc3795f3b45 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Sat, 30 Sep 2023 12:03:13 +0800 Subject: [PATCH 28/46] Create packages to organise java classes --- src/main/java/alan/Alan.java | 23 +++++++++++-------- .../{ => data/exception}/AlanExceptions.java | 4 ++-- .../java/alan/{ => data}/task/Deadline.java | 2 +- src/main/java/alan/{ => data}/task/Event.java | 2 +- src/main/java/alan/{ => data}/task/Task.java | 2 +- .../java/alan/{ => data}/task/TaskType.java | 2 +- src/main/java/alan/{ => data}/task/Todo.java | 2 +- 7 files changed, 21 insertions(+), 16 deletions(-) rename src/main/java/alan/{ => data/exception}/AlanExceptions.java (97%) rename src/main/java/alan/{ => data}/task/Deadline.java (95%) rename src/main/java/alan/{ => data}/task/Event.java (96%) rename src/main/java/alan/{ => data}/task/Task.java (97%) rename src/main/java/alan/{ => data}/task/TaskType.java (61%) rename src/main/java/alan/{ => data}/task/Todo.java (93%) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index f83e4b963..dd2f72ba2 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -1,6 +1,11 @@ package alan; -import alan.task.*; +import alan.data.exception.AlanExceptions; +import alan.data.task.Deadline; +import alan.data.task.Event; +import alan.data.task.Task; +import alan.data.task.TaskType; +import alan.data.task.Todo; import java.io.File; import java.io.FileNotFoundException; @@ -10,12 +15,12 @@ import java.util.ArrayList; import java.util.Scanner; -import static alan.AlanExceptions.checkDeadlineInputFormat; -import static alan.AlanExceptions.checkEmptyDescription; -import static alan.AlanExceptions.checkEventInputFromFormat; -import static alan.AlanExceptions.checkEventInputToFormat; -import static alan.AlanExceptions.checkOutOfTaskListIndex; -import static alan.AlanExceptions.invalidInputCommand; +import static alan.data.exception.AlanExceptions.checkDeadlineInputFormat; +import static alan.data.exception.AlanExceptions.checkEmptyDescription; +import static alan.data.exception.AlanExceptions.checkEventInputFromFormat; +import static alan.data.exception.AlanExceptions.checkEventInputToFormat; +import static alan.data.exception.AlanExceptions.checkOutOfTaskListIndex; +import static alan.data.exception.AlanExceptions.invalidInputCommand; public class Alan { public static void printGreetingMessage() { @@ -193,13 +198,13 @@ public static void saveFileHandler(ArrayList taskList) throws Exception { //check if folder exists if (!Files.exists(dataFolderPath)) { folder.mkdir(); - System.out.println("Data Folder was not found!\nIt's ok...new data folder has been created in" + userWorkingDirectory); + System.out.println("Data Folder was not found!\nIt's ok... new data folder has been created in " + userWorkingDirectory); } //check if file exists if (!Files.exists(tasksFilePath)) { textFile.createNewFile(); - System.out.println("tasks.txt was not found!\nIt's ok...new tasks.txt has been created in " + dataFolderPath); + System.out.println("tasks.txt was not found!\nIt's ok... new tasks.txt has been created in " + dataFolderPath); } //input arraylist data into text file diff --git a/src/main/java/alan/AlanExceptions.java b/src/main/java/alan/data/exception/AlanExceptions.java similarity index 97% rename from src/main/java/alan/AlanExceptions.java rename to src/main/java/alan/data/exception/AlanExceptions.java index e5acb38f0..10e77accd 100644 --- a/src/main/java/alan/AlanExceptions.java +++ b/src/main/java/alan/data/exception/AlanExceptions.java @@ -1,6 +1,6 @@ -package alan; +package alan.data.exception; -import alan.task.Task; +import alan.data.task.Task; import java.util.ArrayList; diff --git a/src/main/java/alan/task/Deadline.java b/src/main/java/alan/data/task/Deadline.java similarity index 95% rename from src/main/java/alan/task/Deadline.java rename to src/main/java/alan/data/task/Deadline.java index b76981605..ab80c221d 100644 --- a/src/main/java/alan/task/Deadline.java +++ b/src/main/java/alan/data/task/Deadline.java @@ -1,4 +1,4 @@ -package alan.task; +package alan.data.task; public class Deadline extends Task { protected String by; diff --git a/src/main/java/alan/task/Event.java b/src/main/java/alan/data/task/Event.java similarity index 96% rename from src/main/java/alan/task/Event.java rename to src/main/java/alan/data/task/Event.java index d708ad6d0..0e45aefd5 100644 --- a/src/main/java/alan/task/Event.java +++ b/src/main/java/alan/data/task/Event.java @@ -1,4 +1,4 @@ -package alan.task; +package alan.data.task; public class Event extends Task { protected String from; diff --git a/src/main/java/alan/task/Task.java b/src/main/java/alan/data/task/Task.java similarity index 97% rename from src/main/java/alan/task/Task.java rename to src/main/java/alan/data/task/Task.java index b861a7590..9c2fd1acf 100644 --- a/src/main/java/alan/task/Task.java +++ b/src/main/java/alan/data/task/Task.java @@ -1,4 +1,4 @@ -package alan.task; +package alan.data.task; public class Task { private String description; diff --git a/src/main/java/alan/task/TaskType.java b/src/main/java/alan/data/task/TaskType.java similarity index 61% rename from src/main/java/alan/task/TaskType.java rename to src/main/java/alan/data/task/TaskType.java index 4f049c9b7..bbffa329d 100644 --- a/src/main/java/alan/task/TaskType.java +++ b/src/main/java/alan/data/task/TaskType.java @@ -1,4 +1,4 @@ -package alan.task; +package alan.data.task; public enum TaskType { T, D, E diff --git a/src/main/java/alan/task/Todo.java b/src/main/java/alan/data/task/Todo.java similarity index 93% rename from src/main/java/alan/task/Todo.java rename to src/main/java/alan/data/task/Todo.java index 9f4c2d180..c8668047f 100644 --- a/src/main/java/alan/task/Todo.java +++ b/src/main/java/alan/data/task/Todo.java @@ -1,4 +1,4 @@ -package alan.task; +package alan.data.task; public class Todo extends Task { private boolean isDone; From d7ccba5d1004761725312e7d735624091aa0904b Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Sun, 1 Oct 2023 12:17:01 +0800 Subject: [PATCH 29/46] Create classes to organise code --- src/main/java/alan/common/Messages.java | 11 ++++++++ src/main/java/alan/data/TaskList.java | 13 +++++++++ ...AlanExceptions.java => AlanException.java} | 28 +++++++++---------- src/main/java/alan/storage/Storage.java | 5 ++++ src/main/java/alan/ui/Ui.java | 25 +++++++++++++++++ 5 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 src/main/java/alan/common/Messages.java create mode 100644 src/main/java/alan/data/TaskList.java rename src/main/java/alan/data/exception/{AlanExceptions.java => AlanException.java} (72%) create mode 100644 src/main/java/alan/storage/Storage.java create mode 100644 src/main/java/alan/ui/Ui.java diff --git a/src/main/java/alan/common/Messages.java b/src/main/java/alan/common/Messages.java new file mode 100644 index 000000000..e196c052e --- /dev/null +++ b/src/main/java/alan/common/Messages.java @@ -0,0 +1,11 @@ +package alan.common; + +import java.security.PublicKey; + +public class Messages { + public static String MESSAGE_GOODBYE = "Later, dude! Can't wait to catch up again real soon!"; + public static String MESSAGE_LIST_COMMAND = "Dude, check out these tasks on your list:"; + public static String MESSAGE_MARK_TASK = "Alright bro! This task is officially checked off:"; + public static String MESSAGE_UNMARK_TASK = "Ok dude, I've marked this task as ain't done yet amigo:"; + public static String MESSAGE_DELETE_TASK = "Got it, dude. This task is outta here:"; +} diff --git a/src/main/java/alan/data/TaskList.java b/src/main/java/alan/data/TaskList.java new file mode 100644 index 000000000..23d3c333c --- /dev/null +++ b/src/main/java/alan/data/TaskList.java @@ -0,0 +1,13 @@ +package alan.data; + +import alan.data.task.Task; + +import java.util.ArrayList; + +public class TaskList { + private final ArrayList tasks; + + public TaskList() { + tasks = new ArrayList(); + } +} diff --git a/src/main/java/alan/data/exception/AlanExceptions.java b/src/main/java/alan/data/exception/AlanException.java similarity index 72% rename from src/main/java/alan/data/exception/AlanExceptions.java rename to src/main/java/alan/data/exception/AlanException.java index 10e77accd..2a237f733 100644 --- a/src/main/java/alan/data/exception/AlanExceptions.java +++ b/src/main/java/alan/data/exception/AlanException.java @@ -4,52 +4,52 @@ import java.util.ArrayList; -public class AlanExceptions extends Exception { +public class AlanException extends Exception { public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty"; public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro...\n[Remember it's: /by ]"; public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack\n[Remember it's: /from /to ]"; public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting\n[Remember it's: /from /to ]"; public static final String INVALID_TASK_NUMBER_MESSAGE = "Oof maaaan there's no such task"; - public AlanExceptions(String errorMessage) { + public AlanException(String errorMessage) { super(errorMessage); } - public static void checkOutOfTaskListIndex(int selectedIndex, ArrayList taskList) throws AlanExceptions { + public static void checkOutOfTaskListIndex(int selectedIndex, ArrayList taskList) throws AlanException { int lastTaskIndex = taskList.size() - 1; if (selectedIndex > lastTaskIndex) { - throw new AlanExceptions(INVALID_TASK_NUMBER_MESSAGE); + throw new AlanException(INVALID_TASK_NUMBER_MESSAGE); } } - public static void invalidInputCommand() throws AlanExceptions { - throw new AlanExceptions(INVALID_INPUT_COMMAND_MESSAGE); + public static void invalidInputCommand() throws AlanException { + throw new AlanException(INVALID_INPUT_COMMAND_MESSAGE); } - public static void checkEmptyDescription(String userInput) throws AlanExceptions { + public static void checkEmptyDescription(String userInput) throws AlanException { String[] userInputWords = userInput.split(" "); if (userInputWords.length == 1) { - throw new AlanExceptions(EMPTY_DESCRIPTION_MESSAGE); + throw new AlanException(EMPTY_DESCRIPTION_MESSAGE); } } - public static void checkDeadlineInputFormat(String[] words) throws AlanExceptions { + public static void checkDeadlineInputFormat(String[] words) throws AlanException { if (words.length == 1) { - throw new AlanExceptions(INVALID_DEADLINE_FORMAT_MESSAGE); + throw new AlanException(INVALID_DEADLINE_FORMAT_MESSAGE); } } - public static void checkEventInputFromFormat(String[] splitDescriptionAndDate) throws AlanExceptions { + public static void checkEventInputFromFormat(String[] splitDescriptionAndDate) throws AlanException { if (splitDescriptionAndDate.length == 1) { - throw new AlanExceptions(INVALID_EVENT_FROM_FORMAT_MESSAGE); + throw new AlanException(INVALID_EVENT_FROM_FORMAT_MESSAGE); } } - public static void checkEventInputToFormat(String[] splitFromAndTo) throws AlanExceptions { + public static void checkEventInputToFormat(String[] splitFromAndTo) throws AlanException { if (splitFromAndTo.length == 1) { - throw new AlanExceptions(INVALID_EVENT_TO_FORMAT_MESSAGE); + throw new AlanException(INVALID_EVENT_TO_FORMAT_MESSAGE); } } diff --git a/src/main/java/alan/storage/Storage.java b/src/main/java/alan/storage/Storage.java new file mode 100644 index 000000000..e989d3c52 --- /dev/null +++ b/src/main/java/alan/storage/Storage.java @@ -0,0 +1,5 @@ +package alan.storage; + +public class Storage { + +} diff --git a/src/main/java/alan/ui/Ui.java b/src/main/java/alan/ui/Ui.java new file mode 100644 index 000000000..4a5db0935 --- /dev/null +++ b/src/main/java/alan/ui/Ui.java @@ -0,0 +1,25 @@ +package alan.ui; + +import java.util.Scanner; + +public class Ui { + public static final int DISPLAYED_INDEX_OFFSET = 1; + private static final String horizontalDivider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; + Scanner scanner = new Scanner(System.in); + + public String getUserCommand() { + System.out.print("Input: "); + + String userInput = scanner.nextLine(); + + showToUser(horizontalDivider); + + return userInput; + } + + public void showToUser(String... message) { + for (String m : message) { + System.out.println(m); + } + } +} From 361eb399d85387ae7e853b63930a2dfd7a6da88b Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Sun, 1 Oct 2023 16:36:24 +0800 Subject: [PATCH 30/46] Implement UI Class --- src/main/java/alan/Alan.java | 130 ++++++------------ src/main/java/alan/common/Messages.java | 26 +++- .../alan/data/exception/AlanException.java | 15 +- src/main/java/alan/ui/Ui.java | 88 +++++++++++- 4 files changed, 157 insertions(+), 102 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index dd2f72ba2..1af333214 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -1,11 +1,13 @@ package alan; -import alan.data.exception.AlanExceptions; +import alan.data.exception.AlanException; import alan.data.task.Deadline; import alan.data.task.Event; import alan.data.task.Task; import alan.data.task.TaskType; import alan.data.task.Todo; +import alan.storage.Storage; +import alan.ui.Ui; import java.io.File; import java.io.FileNotFoundException; @@ -15,62 +17,22 @@ import java.util.ArrayList; import java.util.Scanner; -import static alan.data.exception.AlanExceptions.checkDeadlineInputFormat; -import static alan.data.exception.AlanExceptions.checkEmptyDescription; -import static alan.data.exception.AlanExceptions.checkEventInputFromFormat; -import static alan.data.exception.AlanExceptions.checkEventInputToFormat; -import static alan.data.exception.AlanExceptions.checkOutOfTaskListIndex; -import static alan.data.exception.AlanExceptions.invalidInputCommand; +import static alan.common.Messages.*; +import static alan.data.exception.AlanException.checkDeadlineInputFormat; +import static alan.data.exception.AlanException.checkEmptyDescription; +import static alan.data.exception.AlanException.checkEventInputFromFormat; +import static alan.data.exception.AlanException.checkEventInputToFormat; +import static alan.data.exception.AlanException.checkOutOfTaskListIndex; +import static alan.data.exception.AlanException.invalidInputCommand; public class Alan { - public static void printGreetingMessage() { - String manDrawing = " @/\n" + - "/| \n" + - "/ \\"; - String alanText = " ______ __ ______ __ __ \n" + - "/\\ __ \\ /\\ \\ /\\ __ \\ /\\ \"-.\\ \\ \n" + - "\\ \\ __ \\ \\ \\ \\____ \\ \\ __ \\ \\ \\ \\-. \\ \n" + - " \\ \\_\\ \\_\\ \\ \\_____\\ \\ \\_\\ \\_\\ \\ \\_\\\\\"\\_\\ \n" + - " \\/_/\\/_/ \\/_____/ \\/_/\\/_/ \\/_/ \\/_/ "; - - String greetMessage = "Sup dude! I'm \n" + alanText + "\n" + manDrawing + "\n" + "What can I do for you, my man?"; - - printHorizontalLine(); - System.out.println(greetMessage); - printHorizontalLine(); - } - - public static void printExitMessage() { - System.out.println("Later, dude! Can't wait to catch up again real soon!"); - } - - public static void printHorizontalLine() { - System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); - } - - public static void printNumberOfTasksMessage(int numberOfTasks) { - if (numberOfTasks == 1) { - System.out.println("Dude! You've got a solid " + numberOfTasks + " task lined up on your list now!"); - } else { - System.out.println("Dude! You've got a solid " + numberOfTasks + " tasks lined up on your list now!"); - } - } - - public static void printTaskAddedMessage(ArrayList taskList) { - int numberOfTasks = taskList.size(); - int lastTaskIndex = taskList.size() - 1; - - System.out.println("added: " + taskList.get(lastTaskIndex)); - printNumberOfTasksMessage(numberOfTasks); - } - - public static void processCommandHandler(String userInput, ArrayList taskList) throws AlanExceptions { + public static void processCommandHandler(String userInput, ArrayList taskList) throws AlanException { String[] userInputWords = userInput.split(" "); String command = userInputWords[0]; switch (command) { case "bye": - printExitMessage(); + ui.showExitMessage(); break; case "list": //print the tasks in the lists @@ -109,39 +71,33 @@ public static void processCommandHandler(String userInput, ArrayList taskL } public static void listCommandHandler(ArrayList taskList) { - System.out.println("Dude, check out these tasks on your list:"); - - for (int i = 0; i < taskList.size(); i++) { - System.out.print((i + 1) + ". "); - System.out.println(taskList.get(i)); - } + ui.showListMessage(taskList); } - public static void markingCommandHandler(String userInput, ArrayList taskList, boolean isMark) throws AlanExceptions { + public static void markingCommandHandler(String userInput, ArrayList taskList, boolean isMark) throws AlanException { String[] words = userInput.split(" "); int selectedTaskIndex = Integer.parseInt(words[1]) - 1; checkOutOfTaskListIndex(selectedTaskIndex, taskList); + Task targetTask = taskList.get(selectedTaskIndex); if (isMark) { taskList.get(selectedTaskIndex).setDone(true); - System.out.println("Alright bro! This task is officially checked off:"); + ui.showMarkTaskMessage(targetTask); } else { taskList.get(selectedTaskIndex).setDone(false); - System.out.println("Ok dude, I've marked this task as ain't done yet amigo:"); + ui.showUnmarkTaskMessage(targetTask); } - - System.out.println(taskList.get(selectedTaskIndex)); } public static void todoCommandHandler(String userInput, ArrayList taskList) { String description = userInput.replace("todo ", ""); taskList.add(new Todo(description)); - printTaskAddedMessage(taskList); + ui.showTaskAddedMessage(taskList); } - public static void deadlineCommandHandler(String userInput, ArrayList taskList) throws AlanExceptions { + public static void deadlineCommandHandler(String userInput, ArrayList taskList) throws AlanException { String filteredUserInput = userInput.replace("deadline ", ""); String[] words = filteredUserInput.split(" /by "); @@ -152,10 +108,10 @@ public static void deadlineCommandHandler(String userInput, ArrayList task taskList.add(new Deadline(description, by)); - printTaskAddedMessage(taskList); + ui.showTaskAddedMessage(taskList); } - public static void eventCommandHandler(String userInput, ArrayList taskList) throws AlanExceptions { + public static void eventCommandHandler(String userInput, ArrayList taskList) throws AlanException { String filteredUserInput = userInput.replace("event ", ""); String[] splitDescriptionAndDate = filteredUserInput.split(" /from "); @@ -171,22 +127,20 @@ public static void eventCommandHandler(String userInput, ArrayList taskLis taskList.add(new Event(description, from, to)); - printTaskAddedMessage(taskList); + ui.showTaskAddedMessage(taskList); } - public static void deleteCommandHandler(String userInput, ArrayList taskList) throws AlanExceptions { + public static void deleteCommandHandler(String userInput, ArrayList taskList) throws AlanException { String[] words = userInput.split(" "); int selectedTaskIndex = Integer.parseInt(words[1]) - 1; - checkOutOfTaskListIndex(selectedTaskIndex, taskList); - System.out.println("Got it, dude. This task is outta here:"); - System.out.println(taskList.get(selectedTaskIndex)); - + Task targetTask = taskList.get(selectedTaskIndex); + ui.showDeleteTaskMessage(targetTask); taskList.remove(selectedTaskIndex); int numberOfTasks = taskList.size(); - printNumberOfTasksMessage(numberOfTasks); + ui.showNumberOfTasksMessage(numberOfTasks); } public static void saveFileHandler(ArrayList taskList) throws Exception { String userWorkingDirectory = System.getProperty("user.dir"); @@ -198,13 +152,13 @@ public static void saveFileHandler(ArrayList taskList) throws Exception { //check if folder exists if (!Files.exists(dataFolderPath)) { folder.mkdir(); - System.out.println("Data Folder was not found!\nIt's ok... new data folder has been created in " + userWorkingDirectory); + ui.showFolderNotFoundMessage(userWorkingDirectory); } //check if file exists if (!Files.exists(tasksFilePath)) { textFile.createNewFile(); - System.out.println("tasks.txt was not found!\nIt's ok... new tasks.txt has been created in " + dataFolderPath); + ui.showFileNotFoundMessage(dataFolderPath); } //input arraylist data into text file @@ -322,34 +276,31 @@ private static boolean isDoneStringToBoolean (String string) { return isDone; } - public static void main(String[] args) { + /** MORE OOP Increment **/ + private static Ui ui; + + public static void runAlan() { + ui = new Ui(); ArrayList taskList = new ArrayList<>(); try { readFileHandler(taskList); } catch (Exception exception) { - System.out.println(exception.getMessage()); + ui.showToUser(exception.getMessage()); } - printGreetingMessage(); + ui.showWelcomeMessage(); String userInput = null; - Scanner scanner = new Scanner(System.in); do { try { - //Read user input - System.out.print("Input: "); - userInput = scanner.nextLine(); - - printHorizontalLine(); - + userInput = ui.getUserCommand(); processCommandHandler(userInput, taskList); - - } catch (AlanExceptions alanExceptions) { - System.out.println(alanExceptions.getMessage()); + } catch (AlanException e) { + ui.showToUser(e.getMessage()); } finally { - printHorizontalLine(); + ui.printHorizontalLine(); } } while (!userInput.equals("bye")); @@ -359,6 +310,9 @@ public static void main(String[] args) { } catch (Exception exception) { System.out.println(exception.getMessage()); } + } + public static void main(String[] args) { + Alan.runAlan(); } } diff --git a/src/main/java/alan/common/Messages.java b/src/main/java/alan/common/Messages.java index e196c052e..7afa9f397 100644 --- a/src/main/java/alan/common/Messages.java +++ b/src/main/java/alan/common/Messages.java @@ -3,9 +3,25 @@ import java.security.PublicKey; public class Messages { - public static String MESSAGE_GOODBYE = "Later, dude! Can't wait to catch up again real soon!"; - public static String MESSAGE_LIST_COMMAND = "Dude, check out these tasks on your list:"; - public static String MESSAGE_MARK_TASK = "Alright bro! This task is officially checked off:"; - public static String MESSAGE_UNMARK_TASK = "Ok dude, I've marked this task as ain't done yet amigo:"; - public static String MESSAGE_DELETE_TASK = "Got it, dude. This task is outta here:"; + public static final String manDrawing = " @/\n" + + "/| \n" + + "/ \\"; + public static final String alanText = " ______ __ ______ __ __ \n" + + "/\\ __ \\ /\\ \\ /\\ __ \\ /\\ \"-.\\ \\ \n" + + "\\ \\ __ \\ \\ \\ \\____ \\ \\ __ \\ \\ \\ \\-. \\ \n" + + " \\ \\_\\ \\_\\ \\ \\_____\\ \\ \\_\\ \\_\\ \\ \\_\\\\\"\\_\\ \n" + + " \\/_/\\/_/ \\/_____/ \\/_/\\/_/ \\/_/ \\/_/ "; + + public static final String MESSAGE_GREET = "Sup dude! I'm \n" + alanText + "\n" + manDrawing + "\n" + "What can I do for you, my man?"; + public static final String MESSAGE_GOODBYE = "Later, dude! Can't wait to catch up again real soon!"; + public static final String MESSAGE_LIST_COMMAND = "Dude, check out these tasks on your list:"; + public static final String MESSAGE_MARK_TASK = "Alright bro! This task is officially checked off:"; + public static final String MESSAGE_UNMARK_TASK = "Ok dude, I've marked this task as ain't done yet amigo:"; + public static final String MESSAGE_DELETE_TASK = "Got it, dude. This task is outta here:"; + public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; + public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty"; + public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro...\n[Remember it's: /by ]"; + public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack\n[Remember it's: /from /to ]"; + public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting\n[Remember it's: /from /to ]"; + public static final String INVALID_TASK_NUMBER_MESSAGE = "Oof maaaan there's no such task"; } diff --git a/src/main/java/alan/data/exception/AlanException.java b/src/main/java/alan/data/exception/AlanException.java index 2a237f733..253bd1b81 100644 --- a/src/main/java/alan/data/exception/AlanException.java +++ b/src/main/java/alan/data/exception/AlanException.java @@ -4,14 +4,15 @@ import java.util.ArrayList; +import static alan.common.Messages.EMPTY_DESCRIPTION_MESSAGE; +import static alan.common.Messages.INVALID_DEADLINE_FORMAT_MESSAGE; +import static alan.common.Messages.INVALID_EVENT_FROM_FORMAT_MESSAGE; +import static alan.common.Messages.INVALID_EVENT_TO_FORMAT_MESSAGE; +import static alan.common.Messages.INVALID_INPUT_COMMAND_MESSAGE; +import static alan.common.Messages.INVALID_TASK_NUMBER_MESSAGE; + public class AlanException extends Exception { - public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; - public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty"; - public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro...\n[Remember it's: /by ]"; - public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack\n[Remember it's: /from /to ]"; - public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting\n[Remember it's: /from /to ]"; - public static final String INVALID_TASK_NUMBER_MESSAGE = "Oof maaaan there's no such task"; - public AlanException(String errorMessage) { + public AlanException(String errorMessage) { super(errorMessage); } diff --git a/src/main/java/alan/ui/Ui.java b/src/main/java/alan/ui/Ui.java index 4a5db0935..13fac363d 100644 --- a/src/main/java/alan/ui/Ui.java +++ b/src/main/java/alan/ui/Ui.java @@ -1,22 +1,106 @@ package alan.ui; +import alan.data.task.Task; + +import java.io.InputStream; +import java.io.PrintStream; +import java.util.ArrayList; import java.util.Scanner; +import static alan.common.Messages.MESSAGE_DELETE_TASK; +import static alan.common.Messages.MESSAGE_GOODBYE; +import static alan.common.Messages.MESSAGE_GREET; +import static alan.common.Messages.MESSAGE_LIST_COMMAND; +import static alan.common.Messages.MESSAGE_MARK_TASK; +import static alan.common.Messages.MESSAGE_UNMARK_TASK; + public class Ui { public static final int DISPLAYED_INDEX_OFFSET = 1; private static final String horizontalDivider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; - Scanner scanner = new Scanner(System.in); + private final Scanner in; + private final PrintStream out; + + public Ui() { + this(System.in, System.out); + } + + public Ui(InputStream in, PrintStream out) { + this.in = new Scanner(in); + this.out = out; + } public String getUserCommand() { System.out.print("Input: "); - String userInput = scanner.nextLine(); + String userInput = in.nextLine(); showToUser(horizontalDivider); return userInput; } + public void printHorizontalLine() { + showToUser(horizontalDivider); + } + + public void showWelcomeMessage() { + showToUser( + horizontalDivider, + MESSAGE_GREET, + horizontalDivider); + } + + public void showExitMessage() { + showToUser(MESSAGE_GOODBYE); + } + + public void showListMessage(ArrayList taskList) { + showToUser(MESSAGE_LIST_COMMAND); + for (int i = 0; i < taskList.size(); i++) { + System.out.print((i + DISPLAYED_INDEX_OFFSET) + ". "); + System.out.println(taskList.get(i)); + } + } + + public void showMarkTaskMessage(Task task) { + showToUser(MESSAGE_MARK_TASK); + System.out.println(task); + } + + public void showUnmarkTaskMessage(Task task) { + showToUser(MESSAGE_UNMARK_TASK); + System.out.println(task); + } + + public void showDeleteTaskMessage(Task task) { + showToUser(MESSAGE_DELETE_TASK); + System.out.println(task); + } + + public void showNumberOfTasksMessage(int numberOfTasks) { + if (numberOfTasks == 1) { + System.out.println("Dude! You've got a solid " + numberOfTasks + " task lined up on your list now!"); + } else { + System.out.println("Dude! You've got a solid " + numberOfTasks + " tasks lined up on your list now!"); + } + } + + public void showTaskAddedMessage(ArrayList taskList) { + int numberOfTasks = taskList.size(); + int lastTaskIndex = taskList.size() - 1; + + System.out.println("added: " + taskList.get(lastTaskIndex)); + showNumberOfTasksMessage(numberOfTasks); + } + + public void showFolderNotFoundMessage(String userWorkingDirectory) { + System.out.println("Data Folder was not found!\nIt's ok... new data folder has been created in " + userWorkingDirectory); + } + + public void showFileNotFoundMessage(java.nio.file.Path dataFolderPath) { + System.out.println("tasks.txt was not found!\nIt's ok... new tasks.txt has been created in " + dataFolderPath); + } + public void showToUser(String... message) { for (String m : message) { System.out.println(m); From 4935bbadac49d68eb529b77fc8e7d803a56c3dda Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Mon, 2 Oct 2023 01:28:22 +0800 Subject: [PATCH 31/46] Implement TaskList class --- src/main/java/alan/Alan.java | 130 ++++++++++---------------- src/main/java/alan/data/TaskList.java | 66 ++++++++++++- 2 files changed, 113 insertions(+), 83 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index 1af333214..328df7d0f 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -1,12 +1,12 @@ package alan; +import alan.data.TaskList; import alan.data.exception.AlanException; import alan.data.task.Deadline; import alan.data.task.Event; import alan.data.task.Task; import alan.data.task.TaskType; import alan.data.task.Todo; -import alan.storage.Storage; import alan.ui.Ui; import java.io.File; @@ -17,7 +17,6 @@ import java.util.ArrayList; import java.util.Scanner; -import static alan.common.Messages.*; import static alan.data.exception.AlanException.checkDeadlineInputFormat; import static alan.data.exception.AlanException.checkEmptyDescription; import static alan.data.exception.AlanException.checkEventInputFromFormat; @@ -26,7 +25,7 @@ import static alan.data.exception.AlanException.invalidInputCommand; public class Alan { - public static void processCommandHandler(String userInput, ArrayList taskList) throws AlanException { + public static void processCommandHandler(String userInput) throws AlanException { String[] userInputWords = userInput.split(" "); String command = userInputWords[0]; @@ -36,68 +35,68 @@ public static void processCommandHandler(String userInput, ArrayList taskL break; case "list": //print the tasks in the lists - listCommandHandler(taskList); + listCommandHandler(); break; case "mark": //mark tasks as done - markingCommandHandler(userInput, taskList, true); + markingCommandHandler(userInput, true); break; case "unmark": //unmark tasks as undone - markingCommandHandler(userInput, taskList, false); + markingCommandHandler(userInput, false); break; case "todo": //add to-do task to the list checkEmptyDescription(userInput); - todoCommandHandler(userInput, taskList); + todoCommandHandler(userInput); break; case "deadline": //add deadline task to the list checkEmptyDescription(userInput); - deadlineCommandHandler(userInput, taskList); + deadlineCommandHandler(userInput); break; case "event": //add event task to the list checkEmptyDescription(userInput); - eventCommandHandler(userInput, taskList); + eventCommandHandler(userInput); break; case "delete": //delete task from the list - deleteCommandHandler(userInput, taskList); + deleteCommandHandler(userInput); break; default: invalidInputCommand(); } } - public static void listCommandHandler(ArrayList taskList) { - ui.showListMessage(taskList); + public static void listCommandHandler() { + ui.showListMessage(tasks.getTaskList()); } - public static void markingCommandHandler(String userInput, ArrayList taskList, boolean isMark) throws AlanException { + public static void markingCommandHandler(String userInput, boolean isMark) throws AlanException { String[] words = userInput.split(" "); int selectedTaskIndex = Integer.parseInt(words[1]) - 1; - checkOutOfTaskListIndex(selectedTaskIndex, taskList); - Task targetTask = taskList.get(selectedTaskIndex); + checkOutOfTaskListIndex(selectedTaskIndex, tasks.getTaskList()); + Task targetTask = tasks.getSelectedTask(selectedTaskIndex); if (isMark) { - taskList.get(selectedTaskIndex).setDone(true); + tasks.markTask(selectedTaskIndex, true); ui.showMarkTaskMessage(targetTask); } else { - taskList.get(selectedTaskIndex).setDone(false); + tasks.markTask(selectedTaskIndex, false); ui.showUnmarkTaskMessage(targetTask); } } - public static void todoCommandHandler(String userInput, ArrayList taskList) { + public static void todoCommandHandler(String userInput) { String description = userInput.replace("todo ", ""); - taskList.add(new Todo(description)); + tasks.addToDo(description); - ui.showTaskAddedMessage(taskList); + ui.showTaskAddedMessage(tasks.getTaskList()); } - public static void deadlineCommandHandler(String userInput, ArrayList taskList) throws AlanException { + public static void deadlineCommandHandler(String userInput) throws AlanException { String filteredUserInput = userInput.replace("deadline ", ""); String[] words = filteredUserInput.split(" /by "); @@ -106,12 +105,12 @@ public static void deadlineCommandHandler(String userInput, ArrayList task String description = words[0]; String by = words[1]; - taskList.add(new Deadline(description, by)); + tasks.addDeadline(description, by); - ui.showTaskAddedMessage(taskList); + ui.showTaskAddedMessage(tasks.getTaskList()); } - public static void eventCommandHandler(String userInput, ArrayList taskList) throws AlanException { + public static void eventCommandHandler(String userInput) throws AlanException { String filteredUserInput = userInput.replace("event ", ""); String[] splitDescriptionAndDate = filteredUserInput.split(" /from "); @@ -125,24 +124,24 @@ public static void eventCommandHandler(String userInput, ArrayList taskLis String from = splitFromAndTo[0]; String to = splitFromAndTo[1]; - taskList.add(new Event(description, from, to)); + tasks.addEvent(description, from, to); - ui.showTaskAddedMessage(taskList); + ui.showTaskAddedMessage(tasks.getTaskList()); } - public static void deleteCommandHandler(String userInput, ArrayList taskList) throws AlanException { + public static void deleteCommandHandler(String userInput) throws AlanException { String[] words = userInput.split(" "); int selectedTaskIndex = Integer.parseInt(words[1]) - 1; - checkOutOfTaskListIndex(selectedTaskIndex, taskList); + checkOutOfTaskListIndex(selectedTaskIndex, tasks.getTaskList()); - Task targetTask = taskList.get(selectedTaskIndex); + Task targetTask = tasks.getSelectedTask(selectedTaskIndex); ui.showDeleteTaskMessage(targetTask); - taskList.remove(selectedTaskIndex); + tasks.removeTask(selectedTaskIndex); - int numberOfTasks = taskList.size(); + int numberOfTasks = tasks.getTaskListSize(); ui.showNumberOfTasksMessage(numberOfTasks); } - public static void saveFileHandler(ArrayList taskList) throws Exception { + public static void saveFileHandler() throws Exception { String userWorkingDirectory = System.getProperty("user.dir"); java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, "data", "tasks.txt"); java.nio.file.Path dataFolderPath = java.nio.file.Paths.get(userWorkingDirectory, "data"); @@ -162,8 +161,8 @@ public static void saveFileHandler(ArrayList taskList) throws Exception { } //input arraylist data into text file - for (int i = 0; i < taskList.size(); i++) { - String taskDataRow = getStringOfTaskInformation(taskList, i); + for (int i = 0; i < tasks.getTaskListSize(); i++) { + String taskDataRow = getStringOfTaskInformation(i); if (i == 0) { writeToFile(tasksFilePath.toString(), taskDataRow); @@ -173,8 +172,8 @@ public static void saveFileHandler(ArrayList taskList) throws Exception { } } - private static String getStringOfTaskInformation(ArrayList taskList, int i) { - Task task = taskList.get(i); + private static String getStringOfTaskInformation(int i) { + Task task = tasks.getSelectedTask(i); String taskDataRow = task.getTaskType() + " | " + task.getStatusValue() + " | " + task.getDescription(); if (task.getTaskType() == TaskType.D) { @@ -202,7 +201,7 @@ private static void appendToFile(String filePath, String textToAdd) throws IOExc fw.close(); } - private static void readFileHandler(ArrayList taskList) throws FileNotFoundException { + private static void readFileHandler() throws FileNotFoundException { String userWorkingDirectory = System.getProperty("user.dir"); java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, "data", "tasks.txt"); File textFile = new File(String.valueOf(tasksFilePath)); @@ -216,12 +215,12 @@ private static void readFileHandler(ArrayList taskList) throws FileNotFoun } s.close(); - extractAndStoreDataInTaskList(taskList, list); + extractAndStoreDataInTaskList(list); } } - private static void extractAndStoreDataInTaskList(ArrayList taskList, ArrayList list) { + private static void extractAndStoreDataInTaskList(ArrayList list) { //todo: after this line should check if the data is in correct format for (String task : list) { String[] splitTaskString = task.split(" \\| "); @@ -230,61 +229,28 @@ private static void extractAndStoreDataInTaskList(ArrayList taskList, Arra String description = splitTaskString[2]; boolean isDone = isDoneStringToBoolean(isDoneString); - addTaskToTaskList(taskList, taskType, description, splitTaskString); + tasks.addTaskToTaskList(taskType, description, splitTaskString); - int lastTaskIndex = taskList.size() - 1; - taskList.get(lastTaskIndex).setDone(isDone); - } - } - - private static void addTaskToTaskList(ArrayList taskList, String taskType, String description, String[] splitTaskString) { - switch (taskType) { - case "T": - taskList.add(new Todo(description)); - break; - case "D": - String by = splitTaskString[3]; - taskList.add(new Deadline(description, by)); - break; - case "E": - String date = splitTaskString[3]; - String[] splitDate = date.split("-"); - String from = splitDate[0]; - String to = splitDate[1]; - - taskList.add(new Event(description, from, to)); - break; - default: - //todo handle invalid task type - break; + int lastTaskIndex = tasks.getLastTaskIndex(); + tasks.getSelectedTask(lastTaskIndex).setDone(isDone); } } private static boolean isDoneStringToBoolean (String string) { - boolean isDone = false; - - if (string.equals("1")) { - isDone = true; - } - - if (string.equals("0")) { - isDone = false; - } - - //todo check if isDone is correct value, error handling - - return isDone; + // todo check if isDone is correct value, error handling + return string.equals("1"); } /** MORE OOP Increment **/ private static Ui ui; + private static TaskList tasks; public static void runAlan() { ui = new Ui(); - ArrayList taskList = new ArrayList<>(); + tasks = new TaskList(); try { - readFileHandler(taskList); + readFileHandler(); } catch (Exception exception) { ui.showToUser(exception.getMessage()); } @@ -296,7 +262,7 @@ public static void runAlan() { do { try { userInput = ui.getUserCommand(); - processCommandHandler(userInput, taskList); + processCommandHandler(userInput); } catch (AlanException e) { ui.showToUser(e.getMessage()); } finally { @@ -306,7 +272,7 @@ public static void runAlan() { //Store TaskList in Text file tasks.txt try { - saveFileHandler(taskList); + saveFileHandler(); } catch (Exception exception) { System.out.println(exception.getMessage()); } diff --git a/src/main/java/alan/data/TaskList.java b/src/main/java/alan/data/TaskList.java index 23d3c333c..6bcc50ecf 100644 --- a/src/main/java/alan/data/TaskList.java +++ b/src/main/java/alan/data/TaskList.java @@ -1,6 +1,9 @@ package alan.data; +import alan.data.task.Deadline; +import alan.data.task.Event; import alan.data.task.Task; +import alan.data.task.Todo; import java.util.ArrayList; @@ -8,6 +11,67 @@ public class TaskList { private final ArrayList tasks; public TaskList() { - tasks = new ArrayList(); + tasks = new ArrayList<>(); } + + public ArrayList getTaskList() { + return tasks; + } + + public Task getSelectedTask(int selectedTaskIndex) { + return tasks.get(selectedTaskIndex); + } + + public int getLastTaskIndex() { + return tasks.size() - 1; + } + + public int getTaskListSize() { + return tasks.size(); + } + + public void addToDo(String description) { + tasks.add(new Todo(description)); + } + + public void addDeadline(String description, String by) { + tasks.add(new Deadline(description, by)); + } + + public void addEvent(String description, String from, String to) { + tasks.add(new Event(description, from, to)); + } + + public void markTask(int selectedTaskIndex, boolean isDone) { + tasks.get(selectedTaskIndex).setDone(isDone); + } + + public void removeTask(int selectedTaskIndex) { + tasks.remove(selectedTaskIndex); + } + + public void addTaskToTaskList(String taskType, String description, String[] splitTaskString) { + switch (taskType) { + case "T": + tasks.add(new Todo(description)); + break; + case "D": + String by = splitTaskString[3]; + tasks.add(new Deadline(description, by)); + break; + case "E": + String date = splitTaskString[3]; + String[] splitDate = date.split("-"); + String from = splitDate[0]; + String to = splitDate[1]; + + tasks.add(new Event(description, from, to)); + break; + default: + //todo handle invalid task type + break; + } + } + + } From 7f3c631ae119429ed095e6481786b484333ce575 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Mon, 2 Oct 2023 14:37:40 +0800 Subject: [PATCH 32/46] Implement Parser class --- src/main/java/alan/Alan.java | 130 +----------------------- src/main/java/alan/parser/Parser.java | 136 ++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 125 deletions(-) create mode 100644 src/main/java/alan/parser/Parser.java diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index 328df7d0f..c18836559 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -6,7 +6,7 @@ import alan.data.task.Event; import alan.data.task.Task; import alan.data.task.TaskType; -import alan.data.task.Todo; +import alan.parser.Parser; import alan.ui.Ui; import java.io.File; @@ -17,130 +17,8 @@ import java.util.ArrayList; import java.util.Scanner; -import static alan.data.exception.AlanException.checkDeadlineInputFormat; -import static alan.data.exception.AlanException.checkEmptyDescription; -import static alan.data.exception.AlanException.checkEventInputFromFormat; -import static alan.data.exception.AlanException.checkEventInputToFormat; -import static alan.data.exception.AlanException.checkOutOfTaskListIndex; -import static alan.data.exception.AlanException.invalidInputCommand; - public class Alan { - public static void processCommandHandler(String userInput) throws AlanException { - String[] userInputWords = userInput.split(" "); - String command = userInputWords[0]; - - switch (command) { - case "bye": - ui.showExitMessage(); - break; - case "list": - //print the tasks in the lists - listCommandHandler(); - break; - case "mark": - //mark tasks as done - markingCommandHandler(userInput, true); - break; - case "unmark": - //unmark tasks as undone - markingCommandHandler(userInput, false); - break; - case "todo": - //add to-do task to the list - checkEmptyDescription(userInput); - todoCommandHandler(userInput); - break; - case "deadline": - //add deadline task to the list - checkEmptyDescription(userInput); - deadlineCommandHandler(userInput); - break; - case "event": - //add event task to the list - checkEmptyDescription(userInput); - eventCommandHandler(userInput); - break; - case "delete": - //delete task from the list - deleteCommandHandler(userInput); - break; - default: - invalidInputCommand(); - } - } - - public static void listCommandHandler() { - ui.showListMessage(tasks.getTaskList()); - } - - public static void markingCommandHandler(String userInput, boolean isMark) throws AlanException { - String[] words = userInput.split(" "); - int selectedTaskIndex = Integer.parseInt(words[1]) - 1; - - checkOutOfTaskListIndex(selectedTaskIndex, tasks.getTaskList()); - Task targetTask = tasks.getSelectedTask(selectedTaskIndex); - - if (isMark) { - tasks.markTask(selectedTaskIndex, true); - ui.showMarkTaskMessage(targetTask); - } else { - tasks.markTask(selectedTaskIndex, false); - ui.showUnmarkTaskMessage(targetTask); - } - } - - public static void todoCommandHandler(String userInput) { - String description = userInput.replace("todo ", ""); - tasks.addToDo(description); - - ui.showTaskAddedMessage(tasks.getTaskList()); - } - - public static void deadlineCommandHandler(String userInput) throws AlanException { - String filteredUserInput = userInput.replace("deadline ", ""); - String[] words = filteredUserInput.split(" /by "); - - checkDeadlineInputFormat(words); - - String description = words[0]; - String by = words[1]; - - tasks.addDeadline(description, by); - - ui.showTaskAddedMessage(tasks.getTaskList()); - } - - public static void eventCommandHandler(String userInput) throws AlanException { - String filteredUserInput = userInput.replace("event ", ""); - String[] splitDescriptionAndDate = filteredUserInput.split(" /from "); - - checkEventInputFromFormat(splitDescriptionAndDate); - - String[] splitFromAndTo = splitDescriptionAndDate[1].split(" /to "); - - checkEventInputToFormat(splitFromAndTo); - - String description = splitDescriptionAndDate[0]; - String from = splitFromAndTo[0]; - String to = splitFromAndTo[1]; - - tasks.addEvent(description, from, to); - - ui.showTaskAddedMessage(tasks.getTaskList()); - } - - public static void deleteCommandHandler(String userInput) throws AlanException { - String[] words = userInput.split(" "); - int selectedTaskIndex = Integer.parseInt(words[1]) - 1; - checkOutOfTaskListIndex(selectedTaskIndex, tasks.getTaskList()); - - Task targetTask = tasks.getSelectedTask(selectedTaskIndex); - ui.showDeleteTaskMessage(targetTask); - tasks.removeTask(selectedTaskIndex); - - int numberOfTasks = tasks.getTaskListSize(); - ui.showNumberOfTasksMessage(numberOfTasks); - } + /* File storage */ public static void saveFileHandler() throws Exception { String userWorkingDirectory = System.getProperty("user.dir"); java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, "data", "tasks.txt"); @@ -244,10 +122,12 @@ private static boolean isDoneStringToBoolean (String string) { /** MORE OOP Increment **/ private static Ui ui; private static TaskList tasks; + private static Parser parser; public static void runAlan() { ui = new Ui(); tasks = new TaskList(); + parser = new Parser(tasks, ui); try { readFileHandler(); @@ -262,7 +142,7 @@ public static void runAlan() { do { try { userInput = ui.getUserCommand(); - processCommandHandler(userInput); + parser.processCommandHandler(userInput); } catch (AlanException e) { ui.showToUser(e.getMessage()); } finally { diff --git a/src/main/java/alan/parser/Parser.java b/src/main/java/alan/parser/Parser.java new file mode 100644 index 000000000..b3f32e1ee --- /dev/null +++ b/src/main/java/alan/parser/Parser.java @@ -0,0 +1,136 @@ +package alan.parser; + +import alan.data.TaskList; +import alan.data.exception.AlanException; +import alan.data.task.Task; +import alan.ui.Ui; + +import static alan.data.exception.AlanException.checkEmptyDescription; +import static alan.data.exception.AlanException.checkEventInputFromFormat; +import static alan.data.exception.AlanException.checkEventInputToFormat; +import static alan.data.exception.AlanException.checkOutOfTaskListIndex; +import static alan.data.exception.AlanException.invalidInputCommand; + +public class Parser { + private TaskList tasks; + private Ui ui; + + public Parser(TaskList tasks, Ui ui) { + this.tasks = tasks; + this.ui = ui; + } + public void processCommandHandler(String userInput) throws AlanException { + String[] userInputWords = userInput.split(" "); + String command = userInputWords[0]; + + switch (command) { + case "bye": + ui.showExitMessage(); + break; + case "list": + //print the tasks in the lists + listCommandHandler(); + break; + case "mark": + //mark tasks as done + markingCommandHandler(userInput, true); + break; + case "unmark": + //unmark tasks as undone + markingCommandHandler(userInput, false); + break; + case "todo": + //add to-do task to the list + checkEmptyDescription(userInput); + todoCommandHandler(userInput); + break; + case "deadline": + //add deadline task to the list + checkEmptyDescription(userInput); + deadlineCommandHandler(userInput); + break; + case "event": + //add event task to the list + checkEmptyDescription(userInput); + eventCommandHandler(userInput); + break; + case "delete": + //delete task from the list + deleteCommandHandler(userInput); + break; + default: + invalidInputCommand(); + } + } + + public void listCommandHandler() { + ui.showListMessage(tasks.getTaskList()); + } + + public void markingCommandHandler(String userInput, boolean isMark) throws AlanException { + String[] words = userInput.split(" "); + int selectedTaskIndex = Integer.parseInt(words[1]) - 1; + + checkOutOfTaskListIndex(selectedTaskIndex, tasks.getTaskList()); + Task targetTask = tasks.getSelectedTask(selectedTaskIndex); + + if (isMark) { + tasks.markTask(selectedTaskIndex, true); + ui.showMarkTaskMessage(targetTask); + } else { + tasks.markTask(selectedTaskIndex, false); + ui.showUnmarkTaskMessage(targetTask); + } + } + + public void todoCommandHandler(String userInput) { + String description = userInput.replace("todo ", ""); + tasks.addToDo(description); + + ui.showTaskAddedMessage(tasks.getTaskList()); + } + + public void deadlineCommandHandler(String userInput) throws AlanException { + String filteredUserInput = userInput.replace("deadline ", ""); + String[] data = filteredUserInput.split(" /by "); + + String description = data[0]; + String by = data[1]; + + tasks.addDeadline(description, by); + + ui.showTaskAddedMessage(tasks.getTaskList()); + } + + public void eventCommandHandler(String userInput) throws AlanException { + String filteredUserInput = userInput.replace("event ", ""); + String[] splitDescriptionAndDate = filteredUserInput.split(" /from "); + + checkEventInputFromFormat(splitDescriptionAndDate); + + String[] splitFromAndTo = splitDescriptionAndDate[1].split(" /to "); + + checkEventInputToFormat(splitFromAndTo); + + String description = splitDescriptionAndDate[0]; + String from = splitFromAndTo[0]; + String to = splitFromAndTo[1]; + + tasks.addEvent(description, from, to); + + ui.showTaskAddedMessage(tasks.getTaskList()); + } + + public void deleteCommandHandler(String userInput) throws AlanException { + String[] words = userInput.split(" "); + int selectedTaskIndex = Integer.parseInt(words[1]) - 1; + checkOutOfTaskListIndex(selectedTaskIndex, tasks.getTaskList()); + + Task targetTask = tasks.getSelectedTask(selectedTaskIndex); + ui.showDeleteTaskMessage(targetTask); + tasks.removeTask(selectedTaskIndex); + + int numberOfTasks = tasks.getTaskListSize(); + ui.showNumberOfTasksMessage(numberOfTasks); + } +} From d14095650323930b0391dd6abb9570463b430483 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Mon, 2 Oct 2023 16:36:04 +0800 Subject: [PATCH 33/46] Implement Storage class --- src/main/java/alan/Alan.java | 130 +++----------------- src/main/java/alan/data/TaskList.java | 27 +---- src/main/java/alan/storage/Storage.java | 155 ++++++++++++++++++++++++ src/main/java/alan/ui/Ui.java | 8 ++ 4 files changed, 182 insertions(+), 138 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index c18836559..ca14a45a7 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -7,6 +7,7 @@ import alan.data.task.Task; import alan.data.task.TaskType; import alan.parser.Parser; +import alan.storage.Storage; import alan.ui.Ui; import java.io.File; @@ -18,122 +19,22 @@ import java.util.Scanner; public class Alan { - /* File storage */ - public static void saveFileHandler() throws Exception { - String userWorkingDirectory = System.getProperty("user.dir"); - java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, "data", "tasks.txt"); - java.nio.file.Path dataFolderPath = java.nio.file.Paths.get(userWorkingDirectory, "data"); - File textFile = new File(String.valueOf(tasksFilePath)); - File folder = new File(String.valueOf(dataFolderPath)); - - //check if folder exists - if (!Files.exists(dataFolderPath)) { - folder.mkdir(); - ui.showFolderNotFoundMessage(userWorkingDirectory); - } - - //check if file exists - if (!Files.exists(tasksFilePath)) { - textFile.createNewFile(); - ui.showFileNotFoundMessage(dataFolderPath); - } - - //input arraylist data into text file - for (int i = 0; i < tasks.getTaskListSize(); i++) { - String taskDataRow = getStringOfTaskInformation(i); - - if (i == 0) { - writeToFile(tasksFilePath.toString(), taskDataRow); - } else { - appendToFile(tasksFilePath.toString(), taskDataRow); - } - } - } - - private static String getStringOfTaskInformation(int i) { - Task task = tasks.getSelectedTask(i); - String taskDataRow = task.getTaskType() + " | " + task.getStatusValue() + " | " + task.getDescription(); - - if (task.getTaskType() == TaskType.D) { - Deadline deadline = (Deadline) task; - taskDataRow = taskDataRow + " | " + deadline.getBy(); - } - - if (task.getTaskType() == TaskType.E) { - Event event = (Event) task; - taskDataRow = taskDataRow + " | " + event.getFrom() + "-" +event.getTo(); - } - - taskDataRow = taskDataRow + "\n"; - return taskDataRow; - } - - private static void writeToFile(String filePath, String textToAdd) throws IOException { - FileWriter fw = new FileWriter(filePath); - fw.write(textToAdd); - fw.close(); - } - private static void appendToFile(String filePath, String textToAdd) throws IOException { - FileWriter fw = new FileWriter(filePath, true); - fw.write(textToAdd); - fw.close(); - } - - private static void readFileHandler() throws FileNotFoundException { - String userWorkingDirectory = System.getProperty("user.dir"); - java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, "data", "tasks.txt"); - File textFile = new File(String.valueOf(tasksFilePath)); - if (textFile.length() != 0) { //check if the file is empty - //read the file and store into ArrayList - Scanner s = new Scanner(textFile); - ArrayList list = new ArrayList<>(); - - while (s.hasNext()){ - list.add(s.nextLine()); - } - s.close(); - - extractAndStoreDataInTaskList(list); - - } - } - - private static void extractAndStoreDataInTaskList(ArrayList list) { - //todo: after this line should check if the data is in correct format - for (String task : list) { - String[] splitTaskString = task.split(" \\| "); - String taskType = splitTaskString[0]; - String isDoneString = splitTaskString[1]; - String description = splitTaskString[2]; - boolean isDone = isDoneStringToBoolean(isDoneString); - - tasks.addTaskToTaskList(taskType, description, splitTaskString); - - int lastTaskIndex = tasks.getLastTaskIndex(); - tasks.getSelectedTask(lastTaskIndex).setDone(isDone); - } - } - - private static boolean isDoneStringToBoolean (String string) { - // todo check if isDone is correct value, error handling - return string.equals("1"); - } - - /** MORE OOP Increment **/ private static Ui ui; private static TaskList tasks; - private static Parser parser; + private static Storage storage; - public static void runAlan() { + public Alan(String filePath) { ui = new Ui(); - tasks = new TaskList(); - parser = new Parser(tasks, ui); - + storage = new Storage(filePath); try { - readFileHandler(); - } catch (Exception exception) { - ui.showToUser(exception.getMessage()); + tasks = new TaskList(storage.load()); + } catch (FileNotFoundException e) { + ui.showLoadingError(); + tasks = new TaskList(); } + } + public static void runAlan() { + Parser parser = new Parser(tasks, ui); ui.showWelcomeMessage(); @@ -150,15 +51,14 @@ public static void runAlan() { } } while (!userInput.equals("bye")); - //Store TaskList in Text file tasks.txt try { - saveFileHandler(); - } catch (Exception exception) { - System.out.println(exception.getMessage()); + storage.save(); + } catch (Exception e) { + ui.showSavingError(); } } public static void main(String[] args) { - Alan.runAlan(); + new Alan("data/tasks.txt").runAlan(); } } diff --git a/src/main/java/alan/data/TaskList.java b/src/main/java/alan/data/TaskList.java index 6bcc50ecf..98853d696 100644 --- a/src/main/java/alan/data/TaskList.java +++ b/src/main/java/alan/data/TaskList.java @@ -14,6 +14,10 @@ public TaskList() { tasks = new ArrayList<>(); } + public TaskList(ArrayList tasks) { + this.tasks = tasks; + } + public ArrayList getTaskList() { return tasks; } @@ -50,28 +54,5 @@ public void removeTask(int selectedTaskIndex) { tasks.remove(selectedTaskIndex); } - public void addTaskToTaskList(String taskType, String description, String[] splitTaskString) { - switch (taskType) { - case "T": - tasks.add(new Todo(description)); - break; - case "D": - String by = splitTaskString[3]; - tasks.add(new Deadline(description, by)); - break; - case "E": - String date = splitTaskString[3]; - String[] splitDate = date.split("-"); - String from = splitDate[0]; - String to = splitDate[1]; - - tasks.add(new Event(description, from, to)); - break; - default: - //todo handle invalid task type - break; - } - } - } diff --git a/src/main/java/alan/storage/Storage.java b/src/main/java/alan/storage/Storage.java index e989d3c52..5db9c3c03 100644 --- a/src/main/java/alan/storage/Storage.java +++ b/src/main/java/alan/storage/Storage.java @@ -1,5 +1,160 @@ package alan.storage; +import alan.data.task.Deadline; +import alan.data.task.Event; +import alan.data.task.Task; +import alan.data.task.TaskType; +import alan.data.task.Todo; +import alan.ui.Ui; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Scanner; + public class Storage { + private String filePath; + private ArrayList taskArrayList; + private Ui ui; + + public Storage(String filePath) { + this.filePath = filePath; + this.taskArrayList = new ArrayList<>(); + this.ui = new Ui(); + } + + public ArrayList load() throws FileNotFoundException { + readFileHandler(); + return taskArrayList; + } + + public void save() throws Exception { + saveFileHandler(); + } + + // load from text file + private void readFileHandler() throws FileNotFoundException { + String userWorkingDirectory = System.getProperty("user.dir"); + java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, filePath); + File textFile = new File(String.valueOf(tasksFilePath)); + if (textFile.length() != 0) { + Scanner s = new Scanner(textFile); + ArrayList list = new ArrayList<>(); + + while (s.hasNext()){ + list.add(s.nextLine()); + } + s.close(); + + taskArrayList = extractAndStoreDataInTaskList(list); + } + } + + private ArrayList extractAndStoreDataInTaskList(ArrayList list) { + //todo: after this line should check if the data is in correct format + for (String task : list) { + String[] splitTaskString = task.split(" \\| "); + String taskType = splitTaskString[0]; + String isDoneString = splitTaskString[1]; + String description = splitTaskString[2]; + boolean isDone = isDoneStringToBoolean(isDoneString); + + addTaskToTaskList(taskType, description, splitTaskString); + + int lastTaskIndex = taskArrayList.size() - 1; + taskArrayList.get(lastTaskIndex).setDone(isDone); + } + + return taskArrayList; + } + + public void addTaskToTaskList(String taskType, String description, String[] splitTaskString) { + switch (taskType) { + case "T": + taskArrayList.add(new Todo(description)); + break; + case "D": + String by = splitTaskString[3]; + taskArrayList.add(new Deadline(description, by)); + break; + case "E": + String date = splitTaskString[3]; + String[] splitDate = date.split("-"); + String from = splitDate[0]; + String to = splitDate[1]; + + taskArrayList.add(new Event(description, from, to)); + break; + default: + //todo handle invalid task type + break; + } + } + + private boolean isDoneStringToBoolean (String string) { + // todo check if isDone is correct value, error handling + return string.equals("1"); + } + + // Save to text file + private void saveFileHandler() throws Exception { + String userWorkingDirectory = System.getProperty("user.dir"); + java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, filePath); + java.nio.file.Path dataFolderPath = tasksFilePath.getParent(); + File textFile = new File(String.valueOf(tasksFilePath)); + File folder = new File(String.valueOf(dataFolderPath)); + + if (!Files.exists(dataFolderPath)) { + folder.mkdir(); + ui.showFolderNotFoundMessage(userWorkingDirectory); + } + + if (!Files.exists(tasksFilePath)) { + textFile.createNewFile(); + ui.showFileNotFoundMessage(dataFolderPath); + } + + //input arraylist data into text file + for (int i = 0; i < taskArrayList.size(); i++) { + String taskDataRow = getStringOfTaskInformation(i); + + if (i == 0) { + writeToFile(tasksFilePath.toString(), taskDataRow); + } else { + appendToFile(tasksFilePath.toString(), taskDataRow); + } + } + } + + private String getStringOfTaskInformation(int i) { + Task task = taskArrayList.get(i); + String taskDataRow = task.getTaskType() + " | " + task.getStatusValue() + " | " + task.getDescription(); + + if (task.getTaskType() == TaskType.D) { + Deadline deadline = (Deadline) task; + taskDataRow = taskDataRow + " | " + deadline.getBy(); + } + + if (task.getTaskType() == TaskType.E) { + Event event = (Event) task; + taskDataRow = taskDataRow + " | " + event.getFrom() + "-" +event.getTo(); + } + + taskDataRow = taskDataRow + "\n"; + return taskDataRow; + } + private void writeToFile(String filePath, String textToAdd) throws IOException { + FileWriter fw = new FileWriter(filePath); + fw.write(textToAdd); + fw.close(); + } + private void appendToFile(String filePath, String textToAdd) throws IOException { + FileWriter fw = new FileWriter(filePath, true); + fw.write(textToAdd); + fw.close(); + } } diff --git a/src/main/java/alan/ui/Ui.java b/src/main/java/alan/ui/Ui.java index 13fac363d..11505ff4e 100644 --- a/src/main/java/alan/ui/Ui.java +++ b/src/main/java/alan/ui/Ui.java @@ -101,6 +101,14 @@ public void showFileNotFoundMessage(java.nio.file.Path dataFolderPath) { System.out.println("tasks.txt was not found!\nIt's ok... new tasks.txt has been created in " + dataFolderPath); } + public void showLoadingError() { + System.out.println("Yo dude something ain't loading right :/"); + } + + public void showSavingError() { + System.out.println("Sorry man I can't seem to save to the text file D:"); + } + public void showToUser(String... message) { for (String m : message) { System.out.println(m); From 2d45afffa08cb88238612eee02153ae7345b534c Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Mon, 2 Oct 2023 16:52:07 +0800 Subject: [PATCH 34/46] Add reading invalid task type in Storage class --- src/main/java/alan/Alan.java | 10 ---------- src/main/java/alan/common/Messages.java | 3 +-- src/main/java/alan/storage/Storage.java | 20 ++++++++++++++------ 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index ca14a45a7..19c8f4316 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -2,21 +2,11 @@ import alan.data.TaskList; import alan.data.exception.AlanException; -import alan.data.task.Deadline; -import alan.data.task.Event; -import alan.data.task.Task; -import alan.data.task.TaskType; import alan.parser.Parser; import alan.storage.Storage; import alan.ui.Ui; -import java.io.File; import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.Scanner; public class Alan { private static Ui ui; diff --git a/src/main/java/alan/common/Messages.java b/src/main/java/alan/common/Messages.java index 7afa9f397..5586cb8bd 100644 --- a/src/main/java/alan/common/Messages.java +++ b/src/main/java/alan/common/Messages.java @@ -1,7 +1,5 @@ package alan.common; -import java.security.PublicKey; - public class Messages { public static final String manDrawing = " @/\n" + "/| \n" + @@ -24,4 +22,5 @@ public class Messages { public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack\n[Remember it's: /from /to ]"; public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting\n[Remember it's: /from /to ]"; public static final String INVALID_TASK_NUMBER_MESSAGE = "Oof maaaan there's no such task"; + public static final String INVALID_TASK_TYPE_FOUND = "Yo I found a task type that doesn't make sense"; } diff --git a/src/main/java/alan/storage/Storage.java b/src/main/java/alan/storage/Storage.java index 5db9c3c03..a198b1ea7 100644 --- a/src/main/java/alan/storage/Storage.java +++ b/src/main/java/alan/storage/Storage.java @@ -1,5 +1,6 @@ package alan.storage; +import alan.data.exception.AlanException; import alan.data.task.Deadline; import alan.data.task.Event; import alan.data.task.Task; @@ -15,6 +16,8 @@ import java.util.ArrayList; import java.util.Scanner; +import static alan.common.Messages.INVALID_TASK_TYPE_FOUND; + public class Storage { private String filePath; private ArrayList taskArrayList; @@ -61,8 +64,11 @@ private ArrayList extractAndStoreDataInTaskList(ArrayList list) { String isDoneString = splitTaskString[1]; String description = splitTaskString[2]; boolean isDone = isDoneStringToBoolean(isDoneString); - - addTaskToTaskList(taskType, description, splitTaskString); + try { + addTaskToTaskList(taskType, description, splitTaskString); + } catch (AlanException e) { + ui.showToUser(e.getMessage()); + } int lastTaskIndex = taskArrayList.size() - 1; taskArrayList.get(lastTaskIndex).setDone(isDone); @@ -71,7 +77,7 @@ private ArrayList extractAndStoreDataInTaskList(ArrayList list) { return taskArrayList; } - public void addTaskToTaskList(String taskType, String description, String[] splitTaskString) { + public void addTaskToTaskList(String taskType, String description, String[] splitTaskString) throws AlanException { switch (taskType) { case "T": taskArrayList.add(new Todo(description)); @@ -89,14 +95,16 @@ public void addTaskToTaskList(String taskType, String description, String[] spli taskArrayList.add(new Event(description, from, to)); break; default: - //todo handle invalid task type + new AlanException(INVALID_TASK_TYPE_FOUND); break; } } private boolean isDoneStringToBoolean (String string) { - // todo check if isDone is correct value, error handling - return string.equals("1"); + if (string.equals("1")) { + return true; + } + return false; } // Save to text file From 310e2b46ca5d7efe0691ee3013577b7fb67ded41 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Mon, 2 Oct 2023 18:00:21 +0800 Subject: [PATCH 35/46] Add date parsing --- src/main/java/alan/parser/Parser.java | 35 ++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main/java/alan/parser/Parser.java b/src/main/java/alan/parser/Parser.java index b3f32e1ee..999feb9fc 100644 --- a/src/main/java/alan/parser/Parser.java +++ b/src/main/java/alan/parser/Parser.java @@ -5,6 +5,11 @@ import alan.data.task.Task; import alan.ui.Ui; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + import static alan.data.exception.AlanException.checkEmptyDescription; import static alan.data.exception.AlanException.checkEventInputFromFormat; import static alan.data.exception.AlanException.checkEventInputToFormat; @@ -12,6 +17,7 @@ import static alan.data.exception.AlanException.invalidInputCommand; public class Parser { + public static final String DATE_PARSE_PATTERN = "dd MMM yyyy"; private TaskList tasks; private Ui ui; @@ -90,18 +96,21 @@ public void todoCommandHandler(String userInput) { ui.showTaskAddedMessage(tasks.getTaskList()); } - public void deadlineCommandHandler(String userInput) throws AlanException { + public void deadlineCommandHandler(String userInput) { String filteredUserInput = userInput.replace("deadline ", ""); String[] data = filteredUserInput.split(" /by "); String description = data[0]; String by = data[1]; - tasks.addDeadline(description, by); + String parsedBy = parseDate(by); + tasks.addDeadline(description, parsedBy); ui.showTaskAddedMessage(tasks.getTaskList()); } + + public void eventCommandHandler(String userInput) throws AlanException { String filteredUserInput = userInput.replace("event ", ""); String[] splitDescriptionAndDate = filteredUserInput.split(" /from "); @@ -116,7 +125,10 @@ public void eventCommandHandler(String userInput) throws AlanException { String from = splitFromAndTo[0]; String to = splitFromAndTo[1]; - tasks.addEvent(description, from, to); + String parsedFrom = parseDate(from); + String parsedTo = parseDate(to); + + tasks.addEvent(description, parsedFrom, parsedTo); ui.showTaskAddedMessage(tasks.getTaskList()); } @@ -133,4 +145,21 @@ public void deleteCommandHandler(String userInput) throws AlanException { int numberOfTasks = tasks.getTaskListSize(); ui.showNumberOfTasksMessage(numberOfTasks); } + private String parseDate(String inputDate) { + if (isValidDate(inputDate)) { + LocalDate parsedDate = LocalDate.parse(inputDate); + inputDate = parsedDate.format(DateTimeFormatter.ofPattern(DATE_PARSE_PATTERN)); + } + return inputDate; + } + public boolean isValidDate(String inDate) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd"); + dateFormat.setLenient(false); + try { + dateFormat.parse(inDate.trim()); + } catch (ParseException pe) { + return false; + } + return true; + } } From 61e0f3c9d3d8c495a68b8f8242adb3d83aca5197 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Mon, 2 Oct 2023 18:22:44 +0800 Subject: [PATCH 36/46] Add find task functionality --- src/main/java/alan/common/Messages.java | 15 ++++----- .../alan/data/exception/AlanException.java | 24 +++++++------- src/main/java/alan/parser/Parser.java | 31 ++++++++++++++----- src/main/java/alan/storage/Storage.java | 4 +-- src/main/java/alan/ui/Ui.java | 4 +-- 5 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/main/java/alan/common/Messages.java b/src/main/java/alan/common/Messages.java index 5586cb8bd..a93f1cd0d 100644 --- a/src/main/java/alan/common/Messages.java +++ b/src/main/java/alan/common/Messages.java @@ -16,11 +16,12 @@ public class Messages { public static final String MESSAGE_MARK_TASK = "Alright bro! This task is officially checked off:"; public static final String MESSAGE_UNMARK_TASK = "Ok dude, I've marked this task as ain't done yet amigo:"; public static final String MESSAGE_DELETE_TASK = "Got it, dude. This task is outta here:"; - public static final String INVALID_INPUT_COMMAND_MESSAGE = "Oof, I have no idea what are you saying duuude"; - public static final String EMPTY_DESCRIPTION_MESSAGE = "Oof Dude, you can't leave the description empty"; - public static final String INVALID_DEADLINE_FORMAT_MESSAGE = "Oof the deadline command isn't quite right you gotta fix the format, bro...\n[Remember it's: /by ]"; - public static final String INVALID_EVENT_FROM_FORMAT_MESSAGE = "Oof duude, your /from formatting is whack\n[Remember it's: /from /to ]"; - public static final String INVALID_EVENT_TO_FORMAT_MESSAGE = "Oof my man, you need to work on that /to formatting\n[Remember it's: /from /to ]"; - public static final String INVALID_TASK_NUMBER_MESSAGE = "Oof maaaan there's no such task"; - public static final String INVALID_TASK_TYPE_FOUND = "Yo I found a task type that doesn't make sense"; + public static final String MESSAGE_INVALID_INPUT_COMMAND = "Oof, I have no idea what are you saying duuude"; + public static final String MESSAGE_EMPTY_DESCRIPTION = "Oof Dude, you can't leave the description empty"; + public static final String MESSAGE_INVALID_DEADLINE_FORMAT = "Oof the deadline command isn't quite right you gotta fix the format, bro...\n[Remember it's: /by ]"; + public static final String MESSAGE_INVALID_EVENT_FROM_FORMAT = "Oof duude, your /from formatting is whack\n[Remember it's: /from /to ]"; + public static final String MESSAGE_INVALID_EVENT_TO_FORMAT = "Oof my man, you need to work on that /to formatting\n[Remember it's: /from /to ]"; + public static final String MESSAGE_INVALID_TASK_NUMBER = "Oof maaaan there's no such task"; + public static final String MESSAGE_INVALID_TASK_TYPE_FOUND = "Yo I found a task type that doesn't make sense"; + public static final String MESSAGE_FIND_TASK = "Dude check it out, here are the matching tasks in your list:"; } diff --git a/src/main/java/alan/data/exception/AlanException.java b/src/main/java/alan/data/exception/AlanException.java index 253bd1b81..0f33cffe0 100644 --- a/src/main/java/alan/data/exception/AlanException.java +++ b/src/main/java/alan/data/exception/AlanException.java @@ -4,12 +4,12 @@ import java.util.ArrayList; -import static alan.common.Messages.EMPTY_DESCRIPTION_MESSAGE; -import static alan.common.Messages.INVALID_DEADLINE_FORMAT_MESSAGE; -import static alan.common.Messages.INVALID_EVENT_FROM_FORMAT_MESSAGE; -import static alan.common.Messages.INVALID_EVENT_TO_FORMAT_MESSAGE; -import static alan.common.Messages.INVALID_INPUT_COMMAND_MESSAGE; -import static alan.common.Messages.INVALID_TASK_NUMBER_MESSAGE; +import static alan.common.Messages.MESSAGE_EMPTY_DESCRIPTION; +import static alan.common.Messages.MESSAGE_INVALID_DEADLINE_FORMAT; +import static alan.common.Messages.MESSAGE_INVALID_EVENT_FROM_FORMAT; +import static alan.common.Messages.MESSAGE_INVALID_EVENT_TO_FORMAT; +import static alan.common.Messages.MESSAGE_INVALID_INPUT_COMMAND; +import static alan.common.Messages.MESSAGE_INVALID_TASK_NUMBER; public class AlanException extends Exception { public AlanException(String errorMessage) { @@ -20,37 +20,37 @@ public static void checkOutOfTaskListIndex(int selectedIndex, ArrayList ta int lastTaskIndex = taskList.size() - 1; if (selectedIndex > lastTaskIndex) { - throw new AlanException(INVALID_TASK_NUMBER_MESSAGE); + throw new AlanException(MESSAGE_INVALID_TASK_NUMBER); } } public static void invalidInputCommand() throws AlanException { - throw new AlanException(INVALID_INPUT_COMMAND_MESSAGE); + throw new AlanException(MESSAGE_INVALID_INPUT_COMMAND); } public static void checkEmptyDescription(String userInput) throws AlanException { String[] userInputWords = userInput.split(" "); if (userInputWords.length == 1) { - throw new AlanException(EMPTY_DESCRIPTION_MESSAGE); + throw new AlanException(MESSAGE_EMPTY_DESCRIPTION); } } public static void checkDeadlineInputFormat(String[] words) throws AlanException { if (words.length == 1) { - throw new AlanException(INVALID_DEADLINE_FORMAT_MESSAGE); + throw new AlanException(MESSAGE_INVALID_DEADLINE_FORMAT); } } public static void checkEventInputFromFormat(String[] splitDescriptionAndDate) throws AlanException { if (splitDescriptionAndDate.length == 1) { - throw new AlanException(INVALID_EVENT_FROM_FORMAT_MESSAGE); + throw new AlanException(MESSAGE_INVALID_EVENT_FROM_FORMAT); } } public static void checkEventInputToFormat(String[] splitFromAndTo) throws AlanException { if (splitFromAndTo.length == 1) { - throw new AlanException(INVALID_EVENT_TO_FORMAT_MESSAGE); + throw new AlanException(MESSAGE_INVALID_EVENT_TO_FORMAT); } } diff --git a/src/main/java/alan/parser/Parser.java b/src/main/java/alan/parser/Parser.java index b3f32e1ee..63b33da62 100644 --- a/src/main/java/alan/parser/Parser.java +++ b/src/main/java/alan/parser/Parser.java @@ -5,6 +5,8 @@ import alan.data.task.Task; import alan.ui.Ui; +import static alan.common.Messages.MESSAGE_FIND_TASK; +import static alan.common.Messages.MESSAGE_LIST_COMMAND; import static alan.data.exception.AlanException.checkEmptyDescription; import static alan.data.exception.AlanException.checkEventInputFromFormat; import static alan.data.exception.AlanException.checkEventInputToFormat; @@ -28,43 +30,40 @@ public void processCommandHandler(String userInput) throws AlanException { ui.showExitMessage(); break; case "list": - //print the tasks in the lists listCommandHandler(); break; case "mark": - //mark tasks as done markingCommandHandler(userInput, true); break; case "unmark": - //unmark tasks as undone markingCommandHandler(userInput, false); break; case "todo": - //add to-do task to the list checkEmptyDescription(userInput); todoCommandHandler(userInput); break; case "deadline": - //add deadline task to the list checkEmptyDescription(userInput); deadlineCommandHandler(userInput); break; case "event": - //add event task to the list checkEmptyDescription(userInput); eventCommandHandler(userInput); break; case "delete": - //delete task from the list deleteCommandHandler(userInput); break; + case "find": + findCommandHandler(userInput); + break; default: invalidInputCommand(); } } public void listCommandHandler() { - ui.showListMessage(tasks.getTaskList()); + ui.showToUser(MESSAGE_LIST_COMMAND); + ui.printTasks(tasks.getTaskList()); } public void markingCommandHandler(String userInput, boolean isMark) throws AlanException { @@ -133,4 +132,20 @@ public void deleteCommandHandler(String userInput) throws AlanException { int numberOfTasks = tasks.getTaskListSize(); ui.showNumberOfTasksMessage(numberOfTasks); } + + public void findCommandHandler(String userInput) { + String findText = userInput.replace("find ", ""); + TaskList findResultTasks = new TaskList(); + + for (Task task: tasks.getTaskList()) { + String taskDescription = task.getDescription(); + + if (taskDescription.matches("(.*)" + findText + "(.*)")) { + findResultTasks.getTaskList().add(task); + } + } + + ui.showToUser(MESSAGE_FIND_TASK); + ui.printTasks(findResultTasks.getTaskList()); + } } diff --git a/src/main/java/alan/storage/Storage.java b/src/main/java/alan/storage/Storage.java index a198b1ea7..702182fd7 100644 --- a/src/main/java/alan/storage/Storage.java +++ b/src/main/java/alan/storage/Storage.java @@ -16,7 +16,7 @@ import java.util.ArrayList; import java.util.Scanner; -import static alan.common.Messages.INVALID_TASK_TYPE_FOUND; +import static alan.common.Messages.MESSAGE_INVALID_TASK_TYPE_FOUND; public class Storage { private String filePath; @@ -95,7 +95,7 @@ public void addTaskToTaskList(String taskType, String description, String[] spli taskArrayList.add(new Event(description, from, to)); break; default: - new AlanException(INVALID_TASK_TYPE_FOUND); + new AlanException(MESSAGE_INVALID_TASK_TYPE_FOUND); break; } } diff --git a/src/main/java/alan/ui/Ui.java b/src/main/java/alan/ui/Ui.java index 11505ff4e..9bea055b6 100644 --- a/src/main/java/alan/ui/Ui.java +++ b/src/main/java/alan/ui/Ui.java @@ -10,7 +10,6 @@ import static alan.common.Messages.MESSAGE_DELETE_TASK; import static alan.common.Messages.MESSAGE_GOODBYE; import static alan.common.Messages.MESSAGE_GREET; -import static alan.common.Messages.MESSAGE_LIST_COMMAND; import static alan.common.Messages.MESSAGE_MARK_TASK; import static alan.common.Messages.MESSAGE_UNMARK_TASK; @@ -54,8 +53,7 @@ public void showExitMessage() { showToUser(MESSAGE_GOODBYE); } - public void showListMessage(ArrayList taskList) { - showToUser(MESSAGE_LIST_COMMAND); + public void printTasks(ArrayList taskList) { for (int i = 0; i < taskList.size(); i++) { System.out.print((i + DISPLAYED_INDEX_OFFSET) + ". "); System.out.println(taskList.get(i)); From 51959a1ba98feba0e742944fd050622ec81b4f74 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Tue, 3 Oct 2023 12:41:12 +0800 Subject: [PATCH 37/46] Add JavaDoc comments --- src/main/java/alan/Alan.java | 17 ++++++ src/main/java/alan/common/Messages.java | 4 +- src/main/java/alan/data/TaskList.java | 4 +- .../alan/data/exception/AlanException.java | 44 ++++++++++++-- src/main/java/alan/data/task/Deadline.java | 5 +- src/main/java/alan/data/task/Event.java | 5 +- src/main/java/alan/data/task/Task.java | 5 +- src/main/java/alan/data/task/Todo.java | 5 +- src/main/java/alan/parser/Parser.java | 59 +++++++++++++++---- src/main/java/alan/storage/Storage.java | 51 +++++++++++++++- src/main/java/alan/ui/Ui.java | 20 ++++++- 11 files changed, 195 insertions(+), 24 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index 19c8f4316..d09f519bf 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -8,11 +8,20 @@ import java.io.FileNotFoundException; +/** + * Represents the main class and entry point of the Alan chatbot program. + */ public class Alan { private static Ui ui; private static TaskList tasks; private static Storage storage; + /** + * Represents the Alan constructor. + * Initializes the Ui, Storage and TaskList. + * + * @param filePath filePath of the text file used to store task data. + */ public Alan(String filePath) { ui = new Ui(); storage = new Storage(filePath); @@ -23,6 +32,10 @@ public Alan(String filePath) { tasks = new TaskList(); } } + + /** + * Runs the Alan chatbot program. + */ public static void runAlan() { Parser parser = new Parser(tasks, ui); @@ -48,6 +61,10 @@ public static void runAlan() { } } + /** + * Main method of the Alan program. + * @param args + */ public static void main(String[] args) { new Alan("data/tasks.txt").runAlan(); } diff --git a/src/main/java/alan/common/Messages.java b/src/main/java/alan/common/Messages.java index 5586cb8bd..1c7bf66a8 100644 --- a/src/main/java/alan/common/Messages.java +++ b/src/main/java/alan/common/Messages.java @@ -1,5 +1,7 @@ package alan.common; - +/** + * Represents a class that stores all the messages sent to the user. + */ public class Messages { public static final String manDrawing = " @/\n" + "/| \n" + diff --git a/src/main/java/alan/data/TaskList.java b/src/main/java/alan/data/TaskList.java index 98853d696..f0c823371 100644 --- a/src/main/java/alan/data/TaskList.java +++ b/src/main/java/alan/data/TaskList.java @@ -6,7 +6,9 @@ import alan.data.task.Todo; import java.util.ArrayList; - +/** + * Represents a list of the Task objects. + */ public class TaskList { private final ArrayList tasks; diff --git a/src/main/java/alan/data/exception/AlanException.java b/src/main/java/alan/data/exception/AlanException.java index 253bd1b81..15ec453d8 100644 --- a/src/main/java/alan/data/exception/AlanException.java +++ b/src/main/java/alan/data/exception/AlanException.java @@ -10,12 +10,22 @@ import static alan.common.Messages.INVALID_EVENT_TO_FORMAT_MESSAGE; import static alan.common.Messages.INVALID_INPUT_COMMAND_MESSAGE; import static alan.common.Messages.INVALID_TASK_NUMBER_MESSAGE; - +/** + * Represents the error handling class. + */ public class AlanException extends Exception { - public AlanException(String errorMessage) { + + public AlanException(String errorMessage) { super(errorMessage); } + /** + * Checks lateral location of the specified position. + * + * @param selectedIndex index of the selected task. + * @param taskList contains the list of tasks. + * @throws AlanException If selected index is out of range of taskList. + */ public static void checkOutOfTaskListIndex(int selectedIndex, ArrayList taskList) throws AlanException { int lastTaskIndex = taskList.size() - 1; @@ -24,10 +34,20 @@ public static void checkOutOfTaskListIndex(int selectedIndex, ArrayList ta } } + /** + * Outputs message when user enters an invalid/unknown command. + * + * @throws AlanException If selected index is out of range of taskList. + */ public static void invalidInputCommand() throws AlanException { throw new AlanException(INVALID_INPUT_COMMAND_MESSAGE); } + /** + * Checks for any description text found after the command. + * + * @throws AlanException If there is only 1 String element found in array. + */ public static void checkEmptyDescription(String userInput) throws AlanException { String[] userInputWords = userInput.split(" "); @@ -36,18 +56,34 @@ public static void checkEmptyDescription(String userInput) throws AlanException } } + /** + * Checks for any text found after /by parameter. + * + * @param words contains an array of the information text split by parameter /by. + * @throws AlanException If there is only 1 String element found in array. + */ public static void checkDeadlineInputFormat(String[] words) throws AlanException { if (words.length == 1) { throw new AlanException(INVALID_DEADLINE_FORMAT_MESSAGE); } } - + /** + * Checks for any text found after /from parameter. + * + * @param splitDescriptionAndDate contains an array of the information text split by parameter /from. + * @throws AlanException If there is only 1 String element found in array. + */ public static void checkEventInputFromFormat(String[] splitDescriptionAndDate) throws AlanException { if (splitDescriptionAndDate.length == 1) { throw new AlanException(INVALID_EVENT_FROM_FORMAT_MESSAGE); } } - + /** + * Checks for any text found after /to parameter. + * + * @param splitFromAndTo contains an array of the information text split by parameter /to. + * @throws AlanException If there is only 1 String element found in array. + */ public static void checkEventInputToFormat(String[] splitFromAndTo) throws AlanException { if (splitFromAndTo.length == 1) { throw new AlanException(INVALID_EVENT_TO_FORMAT_MESSAGE); diff --git a/src/main/java/alan/data/task/Deadline.java b/src/main/java/alan/data/task/Deadline.java index ab80c221d..22bd5c5ab 100644 --- a/src/main/java/alan/data/task/Deadline.java +++ b/src/main/java/alan/data/task/Deadline.java @@ -1,5 +1,8 @@ package alan.data.task; - +/** + * Represents a deadline task. A Deadline object corresponds to + * a task consisting of description, by time period and D task type + */ public class Deadline extends Task { protected String by; diff --git a/src/main/java/alan/data/task/Event.java b/src/main/java/alan/data/task/Event.java index 0e45aefd5..dd3804de9 100644 --- a/src/main/java/alan/data/task/Event.java +++ b/src/main/java/alan/data/task/Event.java @@ -1,5 +1,8 @@ package alan.data.task; - +/** + * Represents a event task. A Event object corresponds to + * a task consisting of description, from time period, to time period and E task type + */ public class Event extends Task { protected String from; protected String to; diff --git a/src/main/java/alan/data/task/Task.java b/src/main/java/alan/data/task/Task.java index 9c2fd1acf..a0d71c877 100644 --- a/src/main/java/alan/data/task/Task.java +++ b/src/main/java/alan/data/task/Task.java @@ -1,5 +1,8 @@ package alan.data.task; - +/** + * Represents a task. + * The Task class is always instantiated as a Todo, Deadline or Event task + */ public class Task { private String description; private TaskType type; diff --git a/src/main/java/alan/data/task/Todo.java b/src/main/java/alan/data/task/Todo.java index c8668047f..8d7817df2 100644 --- a/src/main/java/alan/data/task/Todo.java +++ b/src/main/java/alan/data/task/Todo.java @@ -1,5 +1,8 @@ package alan.data.task; - +/** + * Represents a todo task. A Todo object corresponds to + * a task consisting of description and 'T' task type + */ public class Todo extends Task { private boolean isDone; diff --git a/src/main/java/alan/parser/Parser.java b/src/main/java/alan/parser/Parser.java index b3f32e1ee..3e16bd7df 100644 --- a/src/main/java/alan/parser/Parser.java +++ b/src/main/java/alan/parser/Parser.java @@ -5,12 +5,16 @@ import alan.data.task.Task; import alan.ui.Ui; +import static alan.data.exception.AlanException.checkDeadlineInputFormat; import static alan.data.exception.AlanException.checkEmptyDescription; import static alan.data.exception.AlanException.checkEventInputFromFormat; import static alan.data.exception.AlanException.checkEventInputToFormat; import static alan.data.exception.AlanException.checkOutOfTaskListIndex; import static alan.data.exception.AlanException.invalidInputCommand; +/** + * Represents a parser that parses the user input. + */ public class Parser { private TaskList tasks; private Ui ui; @@ -19,6 +23,12 @@ public Parser(TaskList tasks, Ui ui) { this.tasks = tasks; this.ui = ui; } + /** + * Handles extracting the command from the user input and executes the respective command. + * + * @param userInput text of the user input. + * @throws AlanException If an error is detected from any of the command handlers. + */ public void processCommandHandler(String userInput) throws AlanException { String[] userInputWords = userInput.split(" "); String command = userInputWords[0]; @@ -28,34 +38,27 @@ public void processCommandHandler(String userInput) throws AlanException { ui.showExitMessage(); break; case "list": - //print the tasks in the lists listCommandHandler(); break; case "mark": - //mark tasks as done markingCommandHandler(userInput, true); break; case "unmark": - //unmark tasks as undone markingCommandHandler(userInput, false); break; case "todo": - //add to-do task to the list checkEmptyDescription(userInput); todoCommandHandler(userInput); break; case "deadline": - //add deadline task to the list checkEmptyDescription(userInput); deadlineCommandHandler(userInput); break; case "event": - //add event task to the list checkEmptyDescription(userInput); eventCommandHandler(userInput); break; case "delete": - //delete task from the list deleteCommandHandler(userInput); break; default: @@ -63,10 +66,20 @@ public void processCommandHandler(String userInput) throws AlanException { } } + /** + * Handles displaying all the tasks within the TaskList. + */ public void listCommandHandler() { ui.showListMessage(tasks.getTaskList()); } + /** + * Checks for any text found after /from parameter. + * + * @param userInput text of the user input. + * @param isMark mark or unmark the selected task. + * @throws AlanException If there is only 1 String element found in array. + */ public void markingCommandHandler(String userInput, boolean isMark) throws AlanException { String[] words = userInput.split(" "); int selectedTaskIndex = Integer.parseInt(words[1]) - 1; @@ -83,17 +96,31 @@ public void markingCommandHandler(String userInput, boolean isMark) throws AlanE } } + /** + * Handles extracting description text. + * Adds Todo object to the TaskList. + * + * @param userInput text of the user input. + */ public void todoCommandHandler(String userInput) { String description = userInput.replace("todo ", ""); tasks.addToDo(description); ui.showTaskAddedMessage(tasks.getTaskList()); } - + /** + * Handles extracting description and 'by' time period text. + * Adds Deadline object to the TaskList. + * + * @param userInput text of the user input. + * @throws AlanException If input format of /by is not correct. + */ public void deadlineCommandHandler(String userInput) throws AlanException { String filteredUserInput = userInput.replace("deadline ", ""); String[] data = filteredUserInput.split(" /by "); + checkDeadlineInputFormat(data); + String description = data[0]; String by = data[1]; @@ -101,7 +128,13 @@ public void deadlineCommandHandler(String userInput) throws AlanException { ui.showTaskAddedMessage(tasks.getTaskList()); } - + /** + * Handles extracting description, 'from' time period and 'to' time period text. + * Adds Event object to the TaskList. + * + * @param userInput text of the user input. + * @throws AlanException If input format of /from or /to is not correct. + */ public void eventCommandHandler(String userInput) throws AlanException { String filteredUserInput = userInput.replace("event ", ""); String[] splitDescriptionAndDate = filteredUserInput.split(" /from "); @@ -120,7 +153,13 @@ public void eventCommandHandler(String userInput) throws AlanException { ui.showTaskAddedMessage(tasks.getTaskList()); } - + /** + * Handles extracting the user's selected task index. + * Deletes selected task from TaskList. + * + * @param userInput text of the user input. + * @throws AlanException If the task index is not found in TaskList. + */ public void deleteCommandHandler(String userInput) throws AlanException { String[] words = userInput.split(" "); int selectedTaskIndex = Integer.parseInt(words[1]) - 1; diff --git a/src/main/java/alan/storage/Storage.java b/src/main/java/alan/storage/Storage.java index a198b1ea7..10374b6a4 100644 --- a/src/main/java/alan/storage/Storage.java +++ b/src/main/java/alan/storage/Storage.java @@ -1,5 +1,6 @@ package alan.storage; +import alan.data.TaskList; import alan.data.exception.AlanException; import alan.data.task.Deadline; import alan.data.task.Event; @@ -17,7 +18,9 @@ import java.util.Scanner; import static alan.common.Messages.INVALID_TASK_TYPE_FOUND; - +/** + * Handles reading from and storing data to the text file. + */ public class Storage { private String filePath; private ArrayList taskArrayList; @@ -38,7 +41,11 @@ public void save() throws Exception { saveFileHandler(); } - // load from text file + /** + * Reads the text file from the specified file path and stores the tasks in an ArrayList. + * + * @throws FileNotFoundException If file is not found at the specified file path. + */ private void readFileHandler() throws FileNotFoundException { String userWorkingDirectory = System.getProperty("user.dir"); java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, filePath); @@ -56,6 +63,12 @@ private void readFileHandler() throws FileNotFoundException { } } + /** + * Handles the extraction of each parameter. + * Creates a task using the extracted parameters and adds in TaskList. + * + * @return ArrayList containing tasks extracted from the list of strings. + */ private ArrayList extractAndStoreDataInTaskList(ArrayList list) { //todo: after this line should check if the data is in correct format for (String task : list) { @@ -77,6 +90,11 @@ private ArrayList extractAndStoreDataInTaskList(ArrayList list) { return taskArrayList; } + /** + * Adds task to ArrayList according to the respective taskType. + * + * @throws AlanException If taskType read is invalid. + */ public void addTaskToTaskList(String taskType, String description, String[] splitTaskString) throws AlanException { switch (taskType) { case "T": @@ -107,7 +125,11 @@ private boolean isDoneStringToBoolean (String string) { return false; } - // Save to text file + /** + * Saves the tasks in a text file at th specified path. + * + * @throws Exception If file or folder does not exist in the specified path. + */ private void saveFileHandler() throws Exception { String userWorkingDirectory = System.getProperty("user.dir"); java.nio.file.Path tasksFilePath = java.nio.file.Paths.get(userWorkingDirectory, filePath); @@ -137,6 +159,12 @@ private void saveFileHandler() throws Exception { } } + /** + * Formats the task into a string. + * + * @param i index of the task in ArrayList. + * @return a string of the formatted task information to be stored in the text file. + */ private String getStringOfTaskInformation(int i) { Task task = taskArrayList.get(i); String taskDataRow = task.getTaskType() + " | " + task.getStatusValue() + " | " + task.getDescription(); @@ -155,11 +183,28 @@ private String getStringOfTaskInformation(int i) { return taskDataRow; } + /** + * Writes text to the text file at the specified file path. + * Will overwrite all text in text file. + * + * @param filePath file path of the text file. + * @param textToAdd text to be written to the text file. + * @throws IOException If I/O operations are interrupted. + */ private void writeToFile(String filePath, String textToAdd) throws IOException { FileWriter fw = new FileWriter(filePath); fw.write(textToAdd); fw.close(); } + + /** + * Appends text to the text file at the specified file path. + * Will add text to text file. + * + * @param filePath file path of the text file. + * @param textToAdd text to be added to the text file. + * @throws IOException If I/O operations are interrupted. + */ private void appendToFile(String filePath, String textToAdd) throws IOException { FileWriter fw = new FileWriter(filePath, true); fw.write(textToAdd); diff --git a/src/main/java/alan/ui/Ui.java b/src/main/java/alan/ui/Ui.java index 11505ff4e..759f21914 100644 --- a/src/main/java/alan/ui/Ui.java +++ b/src/main/java/alan/ui/Ui.java @@ -1,5 +1,6 @@ package alan.ui; +import alan.data.exception.AlanException; import alan.data.task.Task; import java.io.InputStream; @@ -13,7 +14,9 @@ import static alan.common.Messages.MESSAGE_LIST_COMMAND; import static alan.common.Messages.MESSAGE_MARK_TASK; import static alan.common.Messages.MESSAGE_UNMARK_TASK; - +/** + * Represents the text UI of the application + */ public class Ui { public static final int DISPLAYED_INDEX_OFFSET = 1; private static final String horizontalDivider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; @@ -29,6 +32,11 @@ public Ui(InputStream in, PrintStream out) { this.out = out; } + /** + * Reads the user input + * + * @return string of the user's input text + */ public String getUserCommand() { System.out.print("Input: "); @@ -54,6 +62,11 @@ public void showExitMessage() { showToUser(MESSAGE_GOODBYE); } + /** + * Prints all the Task objects in a list format + * + * @param taskList tasks in the list + */ public void showListMessage(ArrayList taskList) { showToUser(MESSAGE_LIST_COMMAND); for (int i = 0; i < taskList.size(); i++) { @@ -85,6 +98,11 @@ public void showNumberOfTasksMessage(int numberOfTasks) { } } + /** + * Prints the last Task object added to the taskList + * + * @param taskList tasks in the list + */ public void showTaskAddedMessage(ArrayList taskList) { int numberOfTasks = taskList.size(); int lastTaskIndex = taskList.size() - 1; From 7efbd2b29329fdb7f7900e351a30153ce05a644a Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Tue, 3 Oct 2023 22:19:41 +0800 Subject: [PATCH 38/46] Update README.md --- docs/README.md | 159 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 147 insertions(+), 12 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..5d0b50369 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,164 @@ # User Guide +Alan is a Personal Assistant chatbot application that can help with tracking tasks such as todos, deadlines and events. -## Features +* Quick Start +* Features + * Adding a todo : `todo` + * Adding a deadline : `deadline` + * Adding a event : `event` + * Listing all tasks: `list` + * Mark task as done : `mark` + * Mark task as not done : `unmark` + * Deleting a task : `delete` + * Finding tasks : `find` + * Exit program : `bye` +* Command summary -### Feature-ABC +## Quick start +1. Ensure you have Java `11` or above installed on your computer. +2. Download the latest `ip.jar` from [here](https://github.com/DextheChik3n/ip/releases) +3. Copy the file to the folder you want to use as the home folder for your Personal Assistant Chatbot. +4. Open a command terminal, `cd` into the folder you put the jar file in, and use the `java -jar ip.jar` command to run the application. +5. If the setup is correct, you should see something like the below as the output: +``` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Sup dude! I'm + ______ __ ______ __ __ +/\ __ \ /\ \ /\ __ \ /\ "-.\ \ +\ \ __ \ \ \ \____ \ \ __ \ \ \ \-. \ + \ \_\ \_\ \ \_____\ \ \_\ \_\ \ \_\\"\_\ + \/_/\/_/ \/_____/ \/_/\/_/ \/_/ \/_/ + @/ +/| +/ \ +What can I do for you, my man? +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Input: + + ``` + +## Features +> **Notes about command format:** +> - Words in `UPPER_CASE` are the arguments to be supplied by user.
+ e.g. `deadline DESCRIPTION /by DATETIME`, `DESCRIPTION` and `DATETIME` are arguments to be specified by user. +> - Parameters need to be in the exact format as specified.
+ e.g. `event DESCRIPTION /from DATETIME /to DATETIME`, `/from` must come before `/to`. + +### Adding a todo : `todo` + +Adds a todo task in the task list. + +Format: `todo DESCRIPTION` + +Example:
`todo read book` + +### Adding a deadline : `deadline` + +Adds a deadline task in the task list. -Description of the feature. +Format: `deadline DESCRIPTION /by DATETIME` -### Feature-XYZ +Example:
`deadline return book /by Feb 14th` -Description of the feature. +### Adding a event : `event` -## Usage +Adds a event task in the task list. -### `Keyword` - Describe action +Format: `event DESCRIPTION /from DATETIME /to DATETIME` -Describe the action and its outcome. +Example:
`event project meeting /from Aug 6th 2pm /to Aug 6th 4pm` -Example of usage: +### Listing all tasks : `list` -`keyword (optional arguments)` +Lists all the task in the task list. + +Format: `list` Expected outcome: -Description of the outcome. +`list` Prints all the task currently in the task list. + +``` +Input: list +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Dude, check out these tasks on your list: +1. [T][X] read book +2. [D][X] return book (by: June 6th) +3. [E][ ] project meeting (from: Aug 6th 2pm | to: 4pm) +4. [T][X] join sports club +5. [T][ ] borrow book +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``` + +### Mark task as done : `mark` + +Mark the task status as done. + +Format: `mark TASK_INDEX` + +- Marks task as done at specified `TASK_INDEX` +- The `TASK_INDEX` refers to the index number shown in the displayed task list. +The index must be a positive integer 1, 2, 3, …​ + +Example:
`mark 1` Marks the first task in the list as done. + +### Mark task as not done : `unmark` + +Unmark the task status as not done. + +Format: `unmark TASK_INDEX` + +- Marks task as not done at specified `TASK_INDEX` +- The `TASK_INDEX` refers to the index number shown in the displayed task list. + The index must be a positive integer 1, 2, 3, …​ + +Example:
`unmark 1` Marks the first task in the list as not done. + +### Deleting a task : `delete` + +Deletes the task in the task list. + +Format: `delete TASK_INDEX` + +- Deletes task at specified `TASK_INDEX` +- The `TASK_INDEX` refers to the index number shown in the displayed task list. + The index must be a positive integer 1, 2, 3, …​ + +Example:
`list` followed by `delete 2` deletes the 2nd task in the task list. + +### Finding tasks : `find` + +Finds tasks whose description matches the given keyword. + +Format: `find KEYWORD` + +Example:
`find book` will find tasks with the keyword `book` in the list. ``` -expected output +Input: find book +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Dude check it out, here are the matching tasks in your list: +1. [T][X] read book +2. [D][X] return book (by: June 6th) +3. [T][ ] borrow book +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` + +### Exit program : `bye` + +Exits the chatbot program. + +Format: `bye` + +## Command Summary +| Action | Format | Example | +|----------|-------------------------------------------------|------------------------------------------------------------| +| Todo | `todo DESCRIPTION` | `todo ` | +| Deadline | `deadline DESCRIPTION /by DATETIME` | `deadline return book /by 23-10-13` | +| Event | `event DESCRIPTION /from DATETIME /to DATETIME` | `event project meeting /from Aug 1st 3pm /to Aug 1st 10pm` | +| List | `list` | `list` | +| Mark | `mark TASK_INDEX` | `mark 1` | +| Unmark | `unmark TASK_INDEX` | `unmark 2` | +| Delete | `delete TASK_INDEX` | `delete 3` | +| Find | `find KEYWORD` | `find book` | +| Exit | `bye` | `bye` | \ No newline at end of file From 4a0b8b89162891953e30db1ced1fa72f347b99d8 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Tue, 3 Oct 2023 23:18:00 +0800 Subject: [PATCH 39/46] Remove Command Summary section from README.md --- docs/README.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/docs/README.md b/docs/README.md index 5d0b50369..2f803e1cb 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,6 @@ Alan is a Personal Assistant chatbot application that can help with tracking tas * Deleting a task : `delete` * Finding tasks : `find` * Exit program : `bye` -* Command summary ## Quick start 1. Ensure you have Java `11` or above installed on your computer. @@ -149,16 +148,3 @@ Dude check it out, here are the matching tasks in your list: Exits the chatbot program. Format: `bye` - -## Command Summary -| Action | Format | Example | -|----------|-------------------------------------------------|------------------------------------------------------------| -| Todo | `todo DESCRIPTION` | `todo ` | -| Deadline | `deadline DESCRIPTION /by DATETIME` | `deadline return book /by 23-10-13` | -| Event | `event DESCRIPTION /from DATETIME /to DATETIME` | `event project meeting /from Aug 1st 3pm /to Aug 1st 10pm` | -| List | `list` | `list` | -| Mark | `mark TASK_INDEX` | `mark 1` | -| Unmark | `unmark TASK_INDEX` | `unmark 2` | -| Delete | `delete TASK_INDEX` | `delete 3` | -| Find | `find KEYWORD` | `find book` | -| Exit | `bye` | `bye` | \ No newline at end of file From 151f23cf7cbb111efbb2a0892e3633e03b8c7d80 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Tue, 3 Oct 2023 23:36:26 +0800 Subject: [PATCH 40/46] Refactor magic strings --- src/main/java/alan/common/Messages.java | 7 +++++++ src/main/java/alan/ui/Ui.java | 19 ++++++------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/alan/common/Messages.java b/src/main/java/alan/common/Messages.java index ebffc1735..9e90d67c4 100644 --- a/src/main/java/alan/common/Messages.java +++ b/src/main/java/alan/common/Messages.java @@ -12,6 +12,7 @@ public class Messages { " \\ \\_\\ \\_\\ \\ \\_____\\ \\ \\_\\ \\_\\ \\ \\_\\\\\"\\_\\ \n" + " \\/_/\\/_/ \\/_____/ \\/_/\\/_/ \\/_/ \\/_/ "; + public static final String horizontalDivider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; public static final String MESSAGE_GREET = "Sup dude! I'm \n" + alanText + "\n" + manDrawing + "\n" + "What can I do for you, my man?"; public static final String MESSAGE_GOODBYE = "Later, dude! Can't wait to catch up again real soon!"; public static final String MESSAGE_LIST_COMMAND = "Dude, check out these tasks on your list:"; @@ -26,4 +27,10 @@ public class Messages { public static final String MESSAGE_INVALID_TASK_NUMBER = "Oof maaaan there's no such task"; public static final String MESSAGE_INVALID_TASK_TYPE_FOUND = "Yo I found a task type that doesn't make sense"; public static final String MESSAGE_FIND_TASK = "Dude check it out, here are the matching tasks in your list:"; + public static final String MESSAGE_ASK_INPUT = "Input: "; + public static final String MESSAGE_DATA_FOLDER_NOT_FOUND = "Data Folder was not found!\nIt's ok... new data folder has been created in "; + public static final String MESSAGE_DATA_FILE_NOT_FOUND = "tasks.txt was not found!\nIt's ok... new tasks.txt has been created in "; + public static final String MESSAGE_LOAD_FILE_ERROR = "Yo dude something ain't loading right :/"; + public static final String MESSAGE_SAVE_FILE_ERROR = "Sorry man I can't seem to save to the text file D:"; + } diff --git a/src/main/java/alan/ui/Ui.java b/src/main/java/alan/ui/Ui.java index 25cba383a..610b69c54 100644 --- a/src/main/java/alan/ui/Ui.java +++ b/src/main/java/alan/ui/Ui.java @@ -8,19 +8,13 @@ import java.util.ArrayList; import java.util.Scanner; -import static alan.common.Messages.MESSAGE_DELETE_TASK; -import static alan.common.Messages.MESSAGE_GOODBYE; -import static alan.common.Messages.MESSAGE_GREET; -import static alan.common.Messages.MESSAGE_MARK_TASK; -import static alan.common.Messages.MESSAGE_UNMARK_TASK; -import static alan.common.Messages.MESSAGE_LIST_COMMAND; +import static alan.common.Messages.*; /** * Represents the text UI of the application */ public class Ui { public static final int DISPLAYED_INDEX_OFFSET = 1; - private static final String horizontalDivider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; private final Scanner in; private final PrintStream out; @@ -39,7 +33,7 @@ public Ui(InputStream in, PrintStream out) { * @return string of the user's input text */ public String getUserCommand() { - System.out.print("Input: "); + showToUser(MESSAGE_ASK_INPUT); String userInput = in.nextLine(); @@ -69,7 +63,6 @@ public void showExitMessage() { * @param taskList tasks in the list */ public void printTasks(ArrayList taskList) { - showToUser(MESSAGE_LIST_COMMAND); for (int i = 0; i < taskList.size(); i++) { System.out.print((i + DISPLAYED_INDEX_OFFSET) + ". "); System.out.println(taskList.get(i)); @@ -113,19 +106,19 @@ public void showTaskAddedMessage(ArrayList taskList) { } public void showFolderNotFoundMessage(String userWorkingDirectory) { - System.out.println("Data Folder was not found!\nIt's ok... new data folder has been created in " + userWorkingDirectory); + showToUser(MESSAGE_DATA_FOLDER_NOT_FOUND + userWorkingDirectory); } public void showFileNotFoundMessage(java.nio.file.Path dataFolderPath) { - System.out.println("tasks.txt was not found!\nIt's ok... new tasks.txt has been created in " + dataFolderPath); + showToUser(MESSAGE_DATA_FILE_NOT_FOUND + dataFolderPath); } public void showLoadingError() { - System.out.println("Yo dude something ain't loading right :/"); + System.out.println(MESSAGE_LOAD_FILE_ERROR); } public void showSavingError() { - System.out.println("Sorry man I can't seem to save to the text file D:"); + System.out.println(MESSAGE_SAVE_FILE_ERROR); } public void showToUser(String... message) { From 1f42faaea3c89e634aa1e96c06c671ac7aa668b0 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 4 Oct 2023 00:11:11 +0800 Subject: [PATCH 41/46] Add error handling for empty user input --- src/main/java/alan/Alan.java | 5 ++-- src/main/java/alan/common/Messages.java | 1 - .../alan/data/exception/AlanException.java | 6 +++++ src/main/java/alan/parser/Parser.java | 26 ++++++++++++------- src/main/java/alan/ui/Ui.java | 2 +- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/main/java/alan/Alan.java b/src/main/java/alan/Alan.java index d09f519bf..8ee9bb44e 100644 --- a/src/main/java/alan/Alan.java +++ b/src/main/java/alan/Alan.java @@ -42,17 +42,18 @@ public static void runAlan() { ui.showWelcomeMessage(); String userInput = null; + String command = ""; do { try { userInput = ui.getUserCommand(); - parser.processCommandHandler(userInput); + command = parser.processCommandHandler(userInput); } catch (AlanException e) { ui.showToUser(e.getMessage()); } finally { ui.printHorizontalLine(); } - } while (!userInput.equals("bye")); + } while (!command.equals("bye")); try { storage.save(); diff --git a/src/main/java/alan/common/Messages.java b/src/main/java/alan/common/Messages.java index 9e90d67c4..c9e1adf9d 100644 --- a/src/main/java/alan/common/Messages.java +++ b/src/main/java/alan/common/Messages.java @@ -27,7 +27,6 @@ public class Messages { public static final String MESSAGE_INVALID_TASK_NUMBER = "Oof maaaan there's no such task"; public static final String MESSAGE_INVALID_TASK_TYPE_FOUND = "Yo I found a task type that doesn't make sense"; public static final String MESSAGE_FIND_TASK = "Dude check it out, here are the matching tasks in your list:"; - public static final String MESSAGE_ASK_INPUT = "Input: "; public static final String MESSAGE_DATA_FOLDER_NOT_FOUND = "Data Folder was not found!\nIt's ok... new data folder has been created in "; public static final String MESSAGE_DATA_FILE_NOT_FOUND = "tasks.txt was not found!\nIt's ok... new tasks.txt has been created in "; public static final String MESSAGE_LOAD_FILE_ERROR = "Yo dude something ain't loading right :/"; diff --git a/src/main/java/alan/data/exception/AlanException.java b/src/main/java/alan/data/exception/AlanException.java index 2029772b7..18a2de7af 100644 --- a/src/main/java/alan/data/exception/AlanException.java +++ b/src/main/java/alan/data/exception/AlanException.java @@ -91,6 +91,12 @@ public static void checkEventInputToFormat(String[] splitFromAndTo) throws AlanE } } + public static void checkEmptyInput(String inputString) throws AlanException { + if (inputString.isBlank()) { + throw new AlanException(MESSAGE_INVALID_INPUT_COMMAND); + } + } + @Override public String toString() { return super.getMessage(); diff --git a/src/main/java/alan/parser/Parser.java b/src/main/java/alan/parser/Parser.java index 035a24757..5245745ef 100644 --- a/src/main/java/alan/parser/Parser.java +++ b/src/main/java/alan/parser/Parser.java @@ -8,17 +8,18 @@ import static alan.common.Messages.MESSAGE_FIND_TASK; import static alan.common.Messages.MESSAGE_LIST_COMMAND; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - +import static alan.data.exception.AlanException.checkDeadlineInputFormat; import static alan.data.exception.AlanException.checkEmptyDescription; +import static alan.data.exception.AlanException.checkEmptyInput; import static alan.data.exception.AlanException.checkEventInputFromFormat; import static alan.data.exception.AlanException.checkEventInputToFormat; import static alan.data.exception.AlanException.checkOutOfTaskListIndex; import static alan.data.exception.AlanException.invalidInputCommand; -import static alan.data.exception.AlanException.checkDeadlineInputFormat; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; /** * Represents a parser that parses the user input. @@ -38,9 +39,8 @@ public Parser(TaskList tasks, Ui ui) { * @param userInput text of the user input. * @throws AlanException If an error is detected from any of the command handlers. */ - public void processCommandHandler(String userInput) throws AlanException { - String[] userInputWords = userInput.split(" "); - String command = userInputWords[0]; + public String processCommandHandler(String userInput) throws AlanException { + String command = extractCommand(userInput); switch (command) { case "bye": @@ -76,6 +76,14 @@ public void processCommandHandler(String userInput) throws AlanException { default: invalidInputCommand(); } + + return command; + } + + public String extractCommand(String userInput) throws AlanException { + checkEmptyInput(userInput); + String[] userInputWords = userInput.split(" "); + return userInputWords[0]; } /** diff --git a/src/main/java/alan/ui/Ui.java b/src/main/java/alan/ui/Ui.java index 610b69c54..baea3a125 100644 --- a/src/main/java/alan/ui/Ui.java +++ b/src/main/java/alan/ui/Ui.java @@ -33,7 +33,7 @@ public Ui(InputStream in, PrintStream out) { * @return string of the user's input text */ public String getUserCommand() { - showToUser(MESSAGE_ASK_INPUT); + System.out.print("Input: "); String userInput = in.nextLine(); From efc4ab25cbaea1f6f62ab6cfccf76b875c8bf8f6 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 4 Oct 2023 01:07:31 +0800 Subject: [PATCH 42/46] Add handling of printing empty lists --- src/main/java/alan/common/Messages.java | 2 +- src/main/java/alan/ui/Ui.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/alan/common/Messages.java b/src/main/java/alan/common/Messages.java index c9e1adf9d..07238915e 100644 --- a/src/main/java/alan/common/Messages.java +++ b/src/main/java/alan/common/Messages.java @@ -31,5 +31,5 @@ public class Messages { public static final String MESSAGE_DATA_FILE_NOT_FOUND = "tasks.txt was not found!\nIt's ok... new tasks.txt has been created in "; public static final String MESSAGE_LOAD_FILE_ERROR = "Yo dude something ain't loading right :/"; public static final String MESSAGE_SAVE_FILE_ERROR = "Sorry man I can't seem to save to the text file D:"; - + public static final String MESSAGE_EMPTY_LIST = "...errr I don't see any tasks here ¯\\_(ツ)_/¯"; } diff --git a/src/main/java/alan/ui/Ui.java b/src/main/java/alan/ui/Ui.java index baea3a125..874e45bf9 100644 --- a/src/main/java/alan/ui/Ui.java +++ b/src/main/java/alan/ui/Ui.java @@ -63,6 +63,11 @@ public void showExitMessage() { * @param taskList tasks in the list */ public void printTasks(ArrayList taskList) { + if (taskList.isEmpty()) { + showToUser(MESSAGE_EMPTY_LIST); + return; + } + for (int i = 0; i < taskList.size(); i++) { System.out.print((i + DISPLAYED_INDEX_OFFSET) + ". "); System.out.println(taskList.get(i)); From 036f277c62d25f67f56c2c40e9b6bbdf4ff63565 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 4 Oct 2023 01:15:33 +0800 Subject: [PATCH 43/46] Add error handling for mark, unmark and delete command --- .../alan/data/exception/AlanException.java | 44 +++++++++++++++++++ src/main/java/alan/parser/Parser.java | 3 ++ 2 files changed, 47 insertions(+) diff --git a/src/main/java/alan/data/exception/AlanException.java b/src/main/java/alan/data/exception/AlanException.java index 18a2de7af..4ca815731 100644 --- a/src/main/java/alan/data/exception/AlanException.java +++ b/src/main/java/alan/data/exception/AlanException.java @@ -97,6 +97,50 @@ public static void checkEmptyInput(String inputString) throws AlanException { } } + public static void checkIndexInput(String[] inputString) throws AlanException { + if (inputString.length == 1) { //checks for task index after the command + throw new AlanException(MESSAGE_INVALID_INPUT_COMMAND); + } + + if (inputString[1].isBlank()) { //checks for whitespace characters after command + throw new AlanException(MESSAGE_INVALID_INPUT_COMMAND); + } + + if (!isInteger(inputString[1])) { //checks if the text after the command is an integer + throw new AlanException(MESSAGE_INVALID_INPUT_COMMAND); + } + } + + /** + * Checks if the string is integer.
+ * Code taken from Jonas K from: here + * + * @param str text string + */ + public static boolean isInteger(String str) { + if (str == null) { + return false; + } + int length = str.length(); + if (length == 0) { + return false; + } + int i = 0; + if (str.charAt(0) == '-') { + if (length == 1) { + return false; + } + i = 1; + } + for (; i < length; i++) { + char c = str.charAt(i); + if (c < '0' || c > '9') { + return false; + } + } + return true; + } + @Override public String toString() { return super.getMessage(); diff --git a/src/main/java/alan/parser/Parser.java b/src/main/java/alan/parser/Parser.java index 5245745ef..e1cf432e4 100644 --- a/src/main/java/alan/parser/Parser.java +++ b/src/main/java/alan/parser/Parser.java @@ -13,6 +13,7 @@ import static alan.data.exception.AlanException.checkEmptyInput; import static alan.data.exception.AlanException.checkEventInputFromFormat; import static alan.data.exception.AlanException.checkEventInputToFormat; +import static alan.data.exception.AlanException.checkIndexInput; import static alan.data.exception.AlanException.checkOutOfTaskListIndex; import static alan.data.exception.AlanException.invalidInputCommand; @@ -103,6 +104,7 @@ public void listCommandHandler() { */ public void markingCommandHandler(String userInput, boolean isMark) throws AlanException { String[] words = userInput.split(" "); + checkIndexInput(words); int selectedTaskIndex = Integer.parseInt(words[1]) - 1; checkOutOfTaskListIndex(selectedTaskIndex, tasks.getTaskList()); @@ -190,6 +192,7 @@ public void eventCommandHandler(String userInput) throws AlanException { */ public void deleteCommandHandler(String userInput) throws AlanException { String[] words = userInput.split(" "); + checkIndexInput(words); int selectedTaskIndex = Integer.parseInt(words[1]) - 1; checkOutOfTaskListIndex(selectedTaskIndex, tasks.getTaskList()); From f67cbbf4d2b74256efaff654cb6616ede4547cc3 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 4 Oct 2023 01:19:22 +0800 Subject: [PATCH 44/46] Remove unused imports --- src/main/java/alan/storage/Storage.java | 1 - src/main/java/alan/ui/Ui.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/alan/storage/Storage.java b/src/main/java/alan/storage/Storage.java index 0941c2a5b..f685d3a79 100644 --- a/src/main/java/alan/storage/Storage.java +++ b/src/main/java/alan/storage/Storage.java @@ -1,6 +1,5 @@ package alan.storage; -import alan.data.TaskList; import alan.data.exception.AlanException; import alan.data.task.Deadline; import alan.data.task.Event; diff --git a/src/main/java/alan/ui/Ui.java b/src/main/java/alan/ui/Ui.java index 874e45bf9..b6c1cf5b5 100644 --- a/src/main/java/alan/ui/Ui.java +++ b/src/main/java/alan/ui/Ui.java @@ -1,6 +1,5 @@ package alan.ui; -import alan.data.exception.AlanException; import alan.data.task.Task; import java.io.InputStream; From 6cc4255b669a3aa847bfd092c30a5e2af5042b3f Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 4 Oct 2023 01:51:42 +0800 Subject: [PATCH 45/46] Update README.md --- docs/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/README.md b/docs/README.md index 2f803e1cb..f7e4b68b3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -57,6 +57,9 @@ Adds a deadline task in the task list. Format: `deadline DESCRIPTION /by DATETIME` +- `DATETIME` can be input as `yyyy-mm-dd` to be stored as `dd MMM yyyy` format + - e.g. the input `2020-02-14` will be stored as `14 Feb 2020` in the task list + Example:
`deadline return book /by Feb 14th` ### Adding a event : `event` @@ -64,6 +67,8 @@ Example:
`deadline return book /by Feb 14th` Adds a event task in the task list. Format: `event DESCRIPTION /from DATETIME /to DATETIME` +- `DATETIME` can be input as `yyyy-mm-dd` to be stored as `dd MMM yyyy` format + - e.g. the input `2020-02-14` will be stored as `14 Feb 2020` in the task list Example:
`event project meeting /from Aug 6th 2pm /to Aug 6th 4pm` From e4f770ef3dc72dc0d648b51d249894368d3cd060 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Wed, 4 Oct 2023 01:59:22 +0800 Subject: [PATCH 46/46] Fix storing empty TaskList to text file --- src/main/java/alan/storage/Storage.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/alan/storage/Storage.java b/src/main/java/alan/storage/Storage.java index f685d3a79..3816a48b1 100644 --- a/src/main/java/alan/storage/Storage.java +++ b/src/main/java/alan/storage/Storage.java @@ -147,6 +147,11 @@ private void saveFileHandler() throws Exception { ui.showFileNotFoundMessage(dataFolderPath); } + + if (taskArrayList.isEmpty()) { + writeToFile(tasksFilePath.toString(), ""); //overwrite text file to store empty text + } + //input arraylist data into text file for (int i = 0; i < taskArrayList.size(); i++) { String taskDataRow = getStringOfTaskInformation(i);