From 3eca3f35527e51ab9a6508b3d89179b85dde7329 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Mon, 31 Aug 2020 21:27:25 +0800 Subject: [PATCH 01/24] Level-0: Greet --- src/main/java/Duke.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334cc..b40280bf2e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -5,6 +5,13 @@ public static void main(String[] args) { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; + String Greet = "____________________________________________________________\n" + + " Hello! I'm Duke\n" + + " What can I do for you?\n" + + "____________________________________________________________\n" + + " Bye. Hope to see you again soon!\n" + + "____________________________________________________________\n"; System.out.println("Hello from\n" + logo); + System.out.println(Greet); } } From dc6fb157ad680e59d87852fd77b56438f8276be7 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 1 Sep 2020 09:29:31 +0800 Subject: [PATCH 02/24] Level-1: Greet, Echo, Exit & a little bit customizes: 1. add online of " Here is yuqiaoluolong's Duke: " before each echo. 2. add a couple of "<" and ">" in the beginning and the end of the horizontalLine --- src/main/java/Duke.java | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b40280bf2e..74e305750b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,17 +1,39 @@ +import java.util.Scanner; +import java.io.*; + public class Duke { + public static String indentation = " "; + public static String horizontalLine = indentation + "<------------------------------------------------------------>\n"; + + public static void printStatement(String statement){ + System.out.println(horizontalLine + indentation + "Here is yuqiaoluolong's Duke: \n" + statement + horizontalLine); + } + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - String Greet = "____________________________________________________________\n" - + " Hello! I'm Duke\n" - + " What can I do for you?\n" - + "____________________________________________________________\n" - + " Bye. Hope to see you again soon!\n" - + "____________________________________________________________\n"; - System.out.println("Hello from\n" + logo); - System.out.println(Greet); + String Greet = indentation + " Hello! I'm Duke\n" + + indentation + " What can I do for you?\n"; + String Bye = indentation + " Bye. Hope to see you again soon!\n"; + String inputCommand = " "; + boolean isBye = false; + + printStatement("Hello from\n" + logo); + printStatement(Greet); + + Scanner in = new Scanner(System.in); + while(!isBye) { + inputCommand = in.nextLine(); + if(inputCommand.trim().equals("bye")){ //must use .equal() instead of == + printStatement(Bye); + isBye = true; + break; + } + printStatement(indentation + " " + inputCommand + "\n"); + } + } } From 4879598521f6c5504022787c5b86117534352754 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 1 Sep 2020 10:03:07 +0800 Subject: [PATCH 03/24] Level-2: Add, List &customizes: 1. add the second indentation 2. add a line between Duke's response and user's input command &optimizations: 1. delete unused import java.io.*; --- src/main/java/Duke.java | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 74e305750b..8e1cd97567 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,9 +1,11 @@ import java.util.Scanner; -import java.io.*; public class Duke { public static String indentation = " "; + public static String secondIndentation = " "; public static String horizontalLine = indentation + "<------------------------------------------------------------>\n"; + public static String[] Lists = new String[100]; + public static int numberOfList = 0; public static void printStatement(String statement){ System.out.println(horizontalLine + indentation + "Here is yuqiaoluolong's Duke: \n" + statement + horizontalLine); @@ -18,7 +20,7 @@ public static void main(String[] args) { String Greet = indentation + " Hello! I'm Duke\n" + indentation + " What can I do for you?\n"; String Bye = indentation + " Bye. Hope to see you again soon!\n"; - String inputCommand = " "; + String inputCommand; boolean isBye = false; printStatement("Hello from\n" + logo); @@ -27,12 +29,30 @@ public static void main(String[] args) { Scanner in = new Scanner(System.in); while(!isBye) { inputCommand = in.nextLine(); + switch (inputCommand.trim()){ + case "bye": + printStatement(Bye); + isBye = true; + break; + case "list": + System.out.print(horizontalLine); + System.out.println(indentation + "Here is yuqiaoluolong's Duke: "); + for(int i = 0; i < numberOfList; i++){ + System.out.println(indentation + secondIndentation + (i+1) + ". " + Lists[i]); + } + System.out.println(horizontalLine); + break; + default: + Lists[numberOfList] = inputCommand.trim(); + numberOfList++; + printStatement(indentation + secondIndentation + "added: " + inputCommand + "\n"); + }/* if(inputCommand.trim().equals("bye")){ //must use .equal() instead of == printStatement(Bye); isBye = true; break; } - printStatement(indentation + " " + inputCommand + "\n"); + printStatement(indentation + " added: " + inputCommand + "\n");*/ } } From ae2b8b188c013c4fdadfad9965819bcb862ddb05 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 1 Sep 2020 11:16:58 +0800 Subject: [PATCH 04/24] A-Classes: add markAsDone method --- src/main/java/Duke.java | 39 +++++++++++++++++++++++---------------- src/main/java/Task.java | 17 +++++++++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 8e1cd97567..20d052dc2b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -4,8 +4,8 @@ public class Duke { public static String indentation = " "; public static String secondIndentation = " "; public static String horizontalLine = indentation + "<------------------------------------------------------------>\n"; - public static String[] Lists = new String[100]; - public static int numberOfList = 0; + public static Task[] Tasks = new Task[100]; + public static int numberOfTasks = 0; public static void printStatement(String statement){ System.out.println(horizontalLine + indentation + "Here is yuqiaoluolong's Duke: \n" + statement + horizontalLine); @@ -22,6 +22,10 @@ public static void main(String[] args) { String Bye = indentation + " Bye. Hope to see you again soon!\n"; String inputCommand; boolean isBye = false; + int doneNum; + + Tasks[0] = new Task("abc"); + System.out.println(Tasks[0].description); printStatement("Hello from\n" + logo); printStatement(Greet); @@ -30,29 +34,32 @@ public static void main(String[] args) { while(!isBye) { inputCommand = in.nextLine(); switch (inputCommand.trim()){ - case "bye": - printStatement(Bye); - isBye = true; - break; case "list": System.out.print(horizontalLine); System.out.println(indentation + "Here is yuqiaoluolong's Duke: "); - for(int i = 0; i < numberOfList; i++){ - System.out.println(indentation + secondIndentation + (i+1) + ". " + Lists[i]); + for(int i = 0; i < numberOfTasks; i++){ + System.out.println(indentation + secondIndentation + (i+1) + ".[" + + Tasks[i].getStatusIcon() + "] " + Tasks[i].description); } System.out.println(horizontalLine); break; + case "bye": + printStatement(Bye); + isBye = true; + break; default: - Lists[numberOfList] = inputCommand.trim(); - numberOfList++; + if(inputCommand.contains("done")){ + doneNum = Integer.parseInt(inputCommand.replace("done", " ").trim()); + Tasks[doneNum-1].markAsDone(); + printStatement(indentation + secondIndentation + "Nice! I've marked this task as done: \n" + + indentation + secondIndentation + secondIndentation + " [" + + Tasks[doneNum-1].getStatusIcon() + "] " + Tasks[doneNum-1].description + "\n"); + break; + } + Tasks[numberOfTasks] = new Task(inputCommand.trim()); + numberOfTasks++; printStatement(indentation + secondIndentation + "added: " + inputCommand + "\n"); - }/* - if(inputCommand.trim().equals("bye")){ //must use .equal() instead of == - printStatement(Bye); - isBye = true; - break; } - printStatement(indentation + " added: " + inputCommand + "\n");*/ } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 0000000000..1b3a7c3ab7 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,17 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public void markAsDone(){ + this.isDone = true; + } + + public String getStatusIcon() { + return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols + } +} From 67c6c0ce869ff2d81f62465d16b396bb7d5a7cba Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 1 Sep 2020 11:18:06 +0800 Subject: [PATCH 05/24] Level-3: Mark as Done --- src/main/java/Duke.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 20d052dc2b..225795a144 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -24,9 +24,6 @@ public static void main(String[] args) { boolean isBye = false; int doneNum; - Tasks[0] = new Task("abc"); - System.out.println(Tasks[0].description); - printStatement("Hello from\n" + logo); printStatement(Greet); From 3fb55520d0775aa63127f91745a535b56da4b739 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 1 Sep 2020 11:54:33 +0800 Subject: [PATCH 06/24] Naming: Some variables are named incorrectly and switch and while do not have correct spacing It is messy if the naming and spacing does not follow some rules Let's *change String name Greet to greet *change String name Bye to bye *add a space between while and "(" *delete the redundant spacing before "case" in switch statement --- src/main/java/Duke.java | 58 ++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 225795a144..bde5b0e64e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -17,45 +17,45 @@ public static void main(String[] args) { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - String Greet = indentation + " Hello! I'm Duke\n" + String greet = indentation + " Hello! I'm Duke\n" + indentation + " What can I do for you?\n"; - String Bye = indentation + " Bye. Hope to see you again soon!\n"; + String bye = indentation + " Bye. Hope to see you again soon!\n"; String inputCommand; boolean isBye = false; int doneNum; printStatement("Hello from\n" + logo); - printStatement(Greet); + printStatement(greet); Scanner in = new Scanner(System.in); - while(!isBye) { + while (!isBye) { inputCommand = in.nextLine(); - switch (inputCommand.trim()){ - case "list": - System.out.print(horizontalLine); - System.out.println(indentation + "Here is yuqiaoluolong's Duke: "); - for(int i = 0; i < numberOfTasks; i++){ - System.out.println(indentation + secondIndentation + (i+1) + ".[" - + Tasks[i].getStatusIcon() + "] " + Tasks[i].description); - } - System.out.println(horizontalLine); + switch (inputCommand.trim()) { + case "list": + System.out.print(horizontalLine); + System.out.println(indentation + "Here is yuqiaoluolong's Duke: "); + for(int i = 0; i < numberOfTasks; i++){ + System.out.println(indentation + secondIndentation + (i+1) + ".[" + + Tasks[i].getStatusIcon() + "] " + Tasks[i].description); + } + System.out.println(horizontalLine); + break; + case "bye": + printStatement(bye); + isBye = true; + break; + default: + if(inputCommand.contains("done")){ + doneNum = Integer.parseInt(inputCommand.replace("done", " ").trim()); + Tasks[doneNum-1].markAsDone(); + printStatement(indentation + secondIndentation + "Nice! I've marked this task as done: \n" + + indentation + secondIndentation + secondIndentation + " [" + + Tasks[doneNum-1].getStatusIcon() + "] " + Tasks[doneNum-1].description + "\n"); break; - case "bye": - printStatement(Bye); - isBye = true; - break; - default: - if(inputCommand.contains("done")){ - doneNum = Integer.parseInt(inputCommand.replace("done", " ").trim()); - Tasks[doneNum-1].markAsDone(); - printStatement(indentation + secondIndentation + "Nice! I've marked this task as done: \n" - + indentation + secondIndentation + secondIndentation + " [" - + Tasks[doneNum-1].getStatusIcon() + "] " + Tasks[doneNum-1].description + "\n"); - break; - } - Tasks[numberOfTasks] = new Task(inputCommand.trim()); - numberOfTasks++; - printStatement(indentation + secondIndentation + "added: " + inputCommand + "\n"); + } + Tasks[numberOfTasks] = new Task(inputCommand.trim()); + numberOfTasks++; + printStatement(indentation + secondIndentation + "added: " + inputCommand + "\n"); } } From 833fd24256c92c207744768134d550fb616eb7c9 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 1 Sep 2020 17:07:02 +0800 Subject: [PATCH 07/24] Level 4. ToDos, Events, Deadlines & A-Inheritance original main classes: level-3 original Task classes: level-3 To achieve level-4, need to apply polymorphism Task classes: add a method toString() Todo classes: extends Task Deadline classes: extends Task add one more variable by (ddl time) Event classes: extends Task add one more variable at (happening time) main class: edit the format of output according to level-4 requirements and add a condition when user typed unexpected strings --- src/main/java/Deadline.java | 14 +++++++++++ src/main/java/Duke.java | 48 +++++++++++++++++++++++++++---------- src/main/java/Event.java | 14 +++++++++++ src/main/java/Task.java | 4 ++++ src/main/java/Todo.java | 11 +++++++++ 5 files changed, 79 insertions(+), 12 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/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 0000000000..2de70b7540 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,14 @@ +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.toString() + " (by: " + by + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index bde5b0e64e..6c18f57a2d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,13 +2,16 @@ public class Duke { public static String indentation = " "; - public static String secondIndentation = " "; - public static String horizontalLine = indentation + "<------------------------------------------------------------>\n"; + public static String doubleIndentation = indentation + " "; + public static String tripleIndentation = doubleIndentation + " "; + public static String horizontalLine = indentation + + "<------------------------------------------------------------>\n"; public static Task[] Tasks = new Task[100]; public static int numberOfTasks = 0; public static void printStatement(String statement){ - System.out.println(horizontalLine + indentation + "Here is yuqiaoluolong's Duke: \n" + statement + horizontalLine); + System.out.println(horizontalLine + indentation + "Here is yuqiaoluolong's Duke: \n" + + statement + horizontalLine); } public static void main(String[] args) { @@ -23,6 +26,7 @@ public static void main(String[] args) { String inputCommand; boolean isBye = false; int doneNum; + String respond = " "; printStatement("Hello from\n" + logo); printStatement(greet); @@ -33,10 +37,10 @@ public static void main(String[] args) { switch (inputCommand.trim()) { case "list": System.out.print(horizontalLine); - System.out.println(indentation + "Here is yuqiaoluolong's Duke: "); + System.out.println(indentation + "Here is yuqiaoluolong's Duke: \n" + + " Here are the tasks in your list:"); for(int i = 0; i < numberOfTasks; i++){ - System.out.println(indentation + secondIndentation + (i+1) + ".[" - + Tasks[i].getStatusIcon() + "] " + Tasks[i].description); + System.out.println(doubleIndentation + (i+1) + "." + Tasks[i].toString()); } System.out.println(horizontalLine); break; @@ -45,17 +49,37 @@ public static void main(String[] args) { isBye = true; break; default: - if(inputCommand.contains("done")){ + if (inputCommand.contains("done")) { doneNum = Integer.parseInt(inputCommand.replace("done", " ").trim()); Tasks[doneNum-1].markAsDone(); - printStatement(indentation + secondIndentation + "Nice! I've marked this task as done: \n" - + indentation + secondIndentation + secondIndentation + " [" - + Tasks[doneNum-1].getStatusIcon() + "] " + Tasks[doneNum-1].description + "\n"); - break; + respond = doubleIndentation + "Nice! I've marked this task as done: \n" + + tripleIndentation + Tasks[doneNum-1].toString() + "\n"; + } else if (inputCommand.contains("todo")||inputCommand.contains("deadline") + ||inputCommand.contains("event")){ + if (inputCommand.contains("todo")) { + Tasks[numberOfTasks] = new Todo(inputCommand.replace("todo"," ").trim()); + } else if (inputCommand.contains("deadline")) { + Tasks[numberOfTasks] = new Deadline(inputCommand.substring(inputCommand.indexOf("deadline")+8, + inputCommand.indexOf("/")).trim(), + inputCommand.substring(inputCommand.indexOf("/by")+3).trim()); + + } else if (inputCommand.contains("event")) { + Tasks[numberOfTasks] = new Event(inputCommand.substring(inputCommand.indexOf("event")+5, + inputCommand.indexOf("/")).trim(), + inputCommand.substring(inputCommand.indexOf("/at")+3).trim()); + } + respond = doubleIndentation + "Got it. I've added this task:\n" + + tripleIndentation + Tasks[numberOfTasks].toString() + "\n" + doubleIndentation + + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"; + numberOfTasks++; + } else { + respond = doubleIndentation + "Sorry, I don't know what's your command. Can you type again?\n"; } + printStatement(respond); + /* Tasks[numberOfTasks] = new Task(inputCommand.trim()); numberOfTasks++; - printStatement(indentation + secondIndentation + "added: " + inputCommand + "\n"); + printStatement(doubleIndentation + "added: " + inputCommand + "\n");*/ } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 0000000000..a306d9fcbf --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,14 @@ +public class Event extends Task { + + protected String at; + + public Event(String description, String at){ + super(description); + this.at = at; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (at: " + at + ")"; + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 1b3a7c3ab7..92262ff2e4 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -14,4 +14,8 @@ public void markAsDone(){ public String getStatusIcon() { return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols } + + public String toString() { + return "[" + this.getStatusIcon() + "] " + this.description; + } } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 0000000000..7f7d46a5fa --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,11 @@ +public class Todo extends Task { + + public Todo(String description){ + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} From fd3f7f5075bb20b04395ea918073920616dcbc37 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 1 Sep 2020 17:59:32 +0800 Subject: [PATCH 08/24] A-TextUiTesting Edit the file called input.txt and Expected.TXT to test There is still a problem of Troubleshooting and don't know how to use the dos2unix utility --- .gitignore | 2 +- text-ui-test/ACTUAL.TXT | 21 +++++++++++++++++++++ text-ui-test/EXPECTED-UNIX.TXT | 21 +++++++++++++++++++++ text-ui-test/EXPECTED.TXT | 18 ++++++++++++++++-- text-ui-test/input.txt | 1 + text-ui-test/runtest.sh | 2 +- 6 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 text-ui-test/ACTUAL.TXT create mode 100644 text-ui-test/EXPECTED-UNIX.TXT diff --git a/.gitignore b/.gitignore index f69985ef1f..f2d4270f7b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,4 @@ src/main/resources/docs/ bin/ /text-ui-test/ACTUAL.txt -text-ui-test/EXPECTED-UNIX.TXT +text-ui-test/EXPECTED.TXT diff --git a/text-ui-test/ACTUAL.TXT b/text-ui-test/ACTUAL.TXT new file mode 100644 index 0000000000..97e74e8523 --- /dev/null +++ b/text-ui-test/ACTUAL.TXT @@ -0,0 +1,21 @@ + <------------------------------------------------------------> + Here is yuqiaoluolong's Duke: +Hello from + ____ _ +| _ \ _ _| | _____ +| | | | | | | |/ / _ \ +| |_| | |_| | < __/ +|____/ \__,_|_|\_\___| + <------------------------------------------------------------> + + <------------------------------------------------------------> + Here is yuqiaoluolong's Duke: + Hello! I'm Duke + What can I do for you? + <------------------------------------------------------------> + + <------------------------------------------------------------> + Here is yuqiaoluolong's Duke: + Bye. Hope to see you again soon! + <------------------------------------------------------------> + diff --git a/text-ui-test/EXPECTED-UNIX.TXT b/text-ui-test/EXPECTED-UNIX.TXT new file mode 100644 index 0000000000..0b3737bbe0 --- /dev/null +++ b/text-ui-test/EXPECTED-UNIX.TXT @@ -0,0 +1,21 @@ + <------------------------------------------------------------> + Here is yuqiaoluolong's Duke: +Hello from + ____ _ +| _ \ _ _| | _____ +| | | | | | | |/ / _ \ +| |_| | |_| | < __/ +|____/ \__,_|_|\_\___| + <------------------------------------------------------------> + + <------------------------------------------------------------> + Here is yuqiaoluolong's Duke: + Hello! I'm Duke + What can I do for you? + <------------------------------------------------------------> + + <------------------------------------------------------------> + Here is yuqiaoluolong's Duke: + Bye. Hope to see you again soon! + <------------------------------------------------------------> + diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e7..0b3737bbe0 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,21 @@ + <------------------------------------------------------------> + Here is yuqiaoluolong's Duke: Hello from - ____ _ -| _ \ _ _| | _____ + ____ _ +| _ \ _ _| | _____ | | | | | | | |/ / _ \ | |_| | |_| | < __/ |____/ \__,_|_|\_\___| + <------------------------------------------------------------> + + <------------------------------------------------------------> + Here is yuqiaoluolong's Duke: + Hello! I'm Duke + What can I do for you? + <------------------------------------------------------------> + + <------------------------------------------------------------> + Here is yuqiaoluolong's Duke: + Bye. Hope to see you again soon! + <------------------------------------------------------------> diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb2..b023018cab 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1 @@ +bye diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index e169618a34..d47aa28099 100644 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -13,7 +13,7 @@ then fi # compile the code into the bin folder, terminates if error occurred -if ! javac -cp ../src -Xlint:none -d ../bin ../src/main/java/Duke.java +if ! javac -cp ../src -Xlint:none -d ../bin ../src/main/java/*.java then echo "********** BUILD FAILURE **********" exit 1 From d86b76731b688d4b081a0a0c8fde01c974aa17e0 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Wed, 2 Sep 2020 21:18:58 +0800 Subject: [PATCH 09/24] A-CodeQuality Change the name of string respond to outputStatement Add one String variable wrongWarning to hold a long string Add two String variables description and date to hold the this.description and by or at date entered by user --- src/main/java/Duke.java | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 6c18f57a2d..f1bf07393d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -23,12 +23,15 @@ public static void main(String[] args) { String greet = indentation + " Hello! I'm Duke\n" + indentation + " What can I do for you?\n"; String bye = indentation + " Bye. Hope to see you again soon!\n"; + String wrongWarning = "Sorry, I don't know what's your command. Can you type again?\n"; String inputCommand; boolean isBye = false; int doneNum; - String respond = " "; + String outputStatement; + String description; + String date; - printStatement("Hello from\n" + logo); + printStatement("Hello from\n" + logo); // greet in the beginning printStatement(greet); Scanner in = new Scanner(System.in); @@ -52,30 +55,32 @@ public static void main(String[] args) { if (inputCommand.contains("done")) { doneNum = Integer.parseInt(inputCommand.replace("done", " ").trim()); Tasks[doneNum-1].markAsDone(); - respond = doubleIndentation + "Nice! I've marked this task as done: \n" + outputStatement = doubleIndentation + "Nice! I've marked this task as done: \n" + tripleIndentation + Tasks[doneNum-1].toString() + "\n"; } else if (inputCommand.contains("todo")||inputCommand.contains("deadline") ||inputCommand.contains("event")){ if (inputCommand.contains("todo")) { Tasks[numberOfTasks] = new Todo(inputCommand.replace("todo"," ").trim()); } else if (inputCommand.contains("deadline")) { - Tasks[numberOfTasks] = new Deadline(inputCommand.substring(inputCommand.indexOf("deadline")+8, - inputCommand.indexOf("/")).trim(), - inputCommand.substring(inputCommand.indexOf("/by")+3).trim()); + description = inputCommand.substring(inputCommand.indexOf("deadline")+8, + inputCommand.indexOf("/")).trim(); + date = inputCommand.substring(inputCommand.indexOf("/by")+3).trim(); + Tasks[numberOfTasks] = new Deadline(description, date); } else if (inputCommand.contains("event")) { - Tasks[numberOfTasks] = new Event(inputCommand.substring(inputCommand.indexOf("event")+5, - inputCommand.indexOf("/")).trim(), - inputCommand.substring(inputCommand.indexOf("/at")+3).trim()); + description = inputCommand.substring(inputCommand.indexOf("event")+5, + inputCommand.indexOf("/")).trim(); + date = inputCommand.substring(inputCommand.indexOf("/at")+3).trim(); + Tasks[numberOfTasks] = new Event(description, date); } - respond = doubleIndentation + "Got it. I've added this task:\n" + outputStatement = doubleIndentation + "Got it. I've added this task:\n" + tripleIndentation + Tasks[numberOfTasks].toString() + "\n" + doubleIndentation + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"; numberOfTasks++; } else { - respond = doubleIndentation + "Sorry, I don't know what's your command. Can you type again?\n"; + outputStatement = doubleIndentation + wrongWarning; } - printStatement(respond); + printStatement(outputStatement); /* Tasks[numberOfTasks] = new Task(inputCommand.trim()); numberOfTasks++; From 8ad02295f7c1e5057c08734cdbd3dbbffb9a2709 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 8 Sep 2020 23:25:57 +0800 Subject: [PATCH 10/24] CodeQuality correct some names of constants add some methods to make the code inside main class clearer --- src/main/java/Duke.java | 121 ++++++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 47 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f1bf07393d..ea00f35ee7 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,29 +1,78 @@ import java.util.Scanner; public class Duke { - public static String indentation = " "; - public static String doubleIndentation = indentation + " "; - public static String tripleIndentation = doubleIndentation + " "; - public static String horizontalLine = indentation + public static final int MAX_NUM_TASKS = 100; + public static final String INDENTATION = " "; + public static final String DOUBLEINDENTATION = INDENTATION + " "; + public static final String TRIPLEINDENTATION = DOUBLEINDENTATION + " "; + public static final String HORIZONTALLINE = INDENTATION + "<------------------------------------------------------------>\n"; - public static Task[] Tasks = new Task[100]; + public static Task[] tasks = new Task[MAX_NUM_TASKS]; public static int numberOfTasks = 0; - public static void printStatement(String statement){ - System.out.println(horizontalLine + indentation + "Here is yuqiaoluolong's Duke: \n" - + statement + horizontalLine); + public static void printStatement(String statement) { + System.out.println(HORIZONTALLINE + INDENTATION + "Here is yuqiaoluolong's Duke: \n" + + statement + HORIZONTALLINE); + } + public static int getDoneNum(String inputCommand) { + return Integer.parseInt(inputCommand.replace("done", " ").trim()); + } + public static String getDescriptiong(String index, String inputCommand) { + if(index == "deadline") { + return inputCommand.substring(inputCommand.indexOf("deadline")+8, inputCommand.indexOf("/")).trim(); + } else { + return inputCommand.substring(inputCommand.indexOf("event")+5, inputCommand.indexOf("/")).trim(); + } + } + public static void executeListCommand(String inputcommanc) { + System.out.print(HORIZONTALLINE); + System.out.println(INDENTATION + "Here is yuqiaoluolong's Duke: \n" + + " Here are the tasks in your list:"); + for(int i = 0; i < numberOfTasks; i++){ + System.out.println(DOUBLEINDENTATION + (i+1) + "." + tasks[i].toString()); + } + System.out.println(HORIZONTALLINE); + } + public static void executeDoneCommand(String inputCommand) { + int doneNum = getDoneNum(inputCommand); + tasks[doneNum-1].markAsDone(); + printStatement(DOUBLEINDENTATION + "Nice! I've marked this task as done: \n" + + TRIPLEINDENTATION + tasks[doneNum-1].toString() + "\n"); + } + public static void executeTodoCommand(Task[] tasks, int numberOfTasks, String inputCommand) { + tasks[numberOfTasks] = new Todo(inputCommand.replace("todo"," ").trim()); + printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + + TRIPLEINDENTATION + tasks[numberOfTasks].toString() + "\n" + DOUBLEINDENTATION + + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); + } + public static void executeDeadlineCommand(Task[] tasks, int numberOfTasks, String inputCommand) { + String description = getDescriptiong("deadline", inputCommand); + String date = inputCommand.substring(inputCommand.indexOf("/by")+3).trim(); + tasks[numberOfTasks] = new Deadline(description, date); + printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + + TRIPLEINDENTATION + tasks[numberOfTasks].toString() + "\n" + DOUBLEINDENTATION + + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); + } + public static void executeEventCommand(Task[] tasks, int numberOfTasks, String inputCommand) { + String description = getDescriptiong("event", inputCommand); + String date = inputCommand.substring(inputCommand.indexOf("/at")+3).trim(); + tasks[numberOfTasks] = new Event(description, date); + printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + + TRIPLEINDENTATION + tasks[numberOfTasks].toString() + "\n" + DOUBLEINDENTATION + + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); } public static void main(String[] args) { - String logo = " ____ _ \n" + final String LOGO = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - String greet = indentation + " Hello! I'm Duke\n" - + indentation + " What can I do for you?\n"; - String bye = indentation + " Bye. Hope to see you again soon!\n"; - String wrongWarning = "Sorry, I don't know what's your command. Can you type again?\n"; + final String GREET = INDENTATION + " Hello! I'm Duke\n" + + INDENTATION + " What can I do for you?\n"; + final String BYE = INDENTATION + " Bye. Hope to see you again soon!\n"; + final String WRONGMESSAGE = "Sorry, I don't know what's your command. Can you type again?\n"; + boolean isNewTask; String inputCommand; boolean isBye = false; int doneNum; @@ -31,60 +80,38 @@ public static void main(String[] args) { String description; String date; - printStatement("Hello from\n" + logo); // greet in the beginning - printStatement(greet); + printStatement("Hello from\n" + LOGO); // greet in the beginning + printStatement(GREET); Scanner in = new Scanner(System.in); while (!isBye) { inputCommand = in.nextLine(); + isNewTask = inputCommand.contains("todo") || inputCommand.contains("deadline") + || inputCommand.contains("event"); switch (inputCommand.trim()) { case "list": - System.out.print(horizontalLine); - System.out.println(indentation + "Here is yuqiaoluolong's Duke: \n" + - " Here are the tasks in your list:"); - for(int i = 0; i < numberOfTasks; i++){ - System.out.println(doubleIndentation + (i+1) + "." + Tasks[i].toString()); - } - System.out.println(horizontalLine); + executeListCommand(inputCommand); break; case "bye": - printStatement(bye); + printStatement(BYE); isBye = true; break; default: if (inputCommand.contains("done")) { - doneNum = Integer.parseInt(inputCommand.replace("done", " ").trim()); - Tasks[doneNum-1].markAsDone(); - outputStatement = doubleIndentation + "Nice! I've marked this task as done: \n" - + tripleIndentation + Tasks[doneNum-1].toString() + "\n"; - } else if (inputCommand.contains("todo")||inputCommand.contains("deadline") - ||inputCommand.contains("event")){ + executeDoneCommand(inputCommand); + } else if (isNewTask) { if (inputCommand.contains("todo")) { - Tasks[numberOfTasks] = new Todo(inputCommand.replace("todo"," ").trim()); + executeTodoCommand(tasks, numberOfTasks, inputCommand); } else if (inputCommand.contains("deadline")) { - description = inputCommand.substring(inputCommand.indexOf("deadline")+8, - inputCommand.indexOf("/")).trim(); - date = inputCommand.substring(inputCommand.indexOf("/by")+3).trim(); - Tasks[numberOfTasks] = new Deadline(description, date); + executeDeadlineCommand(tasks, numberOfTasks, inputCommand); } else if (inputCommand.contains("event")) { - description = inputCommand.substring(inputCommand.indexOf("event")+5, - inputCommand.indexOf("/")).trim(); - date = inputCommand.substring(inputCommand.indexOf("/at")+3).trim(); - Tasks[numberOfTasks] = new Event(description, date); + executeEventCommand(tasks, numberOfTasks, inputCommand); } - outputStatement = doubleIndentation + "Got it. I've added this task:\n" - + tripleIndentation + Tasks[numberOfTasks].toString() + "\n" + doubleIndentation - + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"; numberOfTasks++; } else { - outputStatement = doubleIndentation + wrongWarning; + printStatement(DOUBLEINDENTATION + WRONGMESSAGE); } - printStatement(outputStatement); - /* - Tasks[numberOfTasks] = new Task(inputCommand.trim()); - numberOfTasks++; - printStatement(doubleIndentation + "added: " + inputCommand + "\n");*/ } } From 3fbb3df6a785a5a5c094cd3349f9c05750bfc616 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Wed, 9 Sep 2020 13:37:34 +0800 Subject: [PATCH 11/24] Level-5: Handle Errors 4 conditions of exceptions are added the empty todo command exception is caught the empty deadline command exception is caught the empty event command exception is caught the command out of the scope exception is caught --- src/main/java/Duke.java | 69 +++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ea00f35ee7..adfa0b1e81 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -10,6 +10,10 @@ public class Duke { public static Task[] tasks = new Task[MAX_NUM_TASKS]; public static int numberOfTasks = 0; + public static class TodoNullException extends Exception { + //no other code needed + } + public static void printStatement(String statement) { System.out.println(HORIZONTALLINE + INDENTATION + "Here is yuqiaoluolong's Duke: \n" + statement + HORIZONTALLINE); @@ -39,27 +43,42 @@ public static void executeDoneCommand(String inputCommand) { printStatement(DOUBLEINDENTATION + "Nice! I've marked this task as done: \n" + TRIPLEINDENTATION + tasks[doneNum-1].toString() + "\n"); } - public static void executeTodoCommand(Task[] tasks, int numberOfTasks, String inputCommand) { - tasks[numberOfTasks] = new Todo(inputCommand.replace("todo"," ").trim()); + public static void executeTodoCommand(Task[] tasks, int numberOfTasks, String inputCommand) throws TodoNullException{ + tasks[numberOfTasks] = new Todo(inputCommand.substring(inputCommand.indexOf("todo")+4).trim()); + if(tasks[numberOfTasks].description.length() == 0){ + throw new TodoNullException(); + } printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + TRIPLEINDENTATION + tasks[numberOfTasks].toString() + "\n" + DOUBLEINDENTATION - + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); + + "Now you have " + (numberOfTasks + 1) + " tasks in the list.\n"); } - public static void executeDeadlineCommand(Task[] tasks, int numberOfTasks, String inputCommand) { - String description = getDescriptiong("deadline", inputCommand); - String date = inputCommand.substring(inputCommand.indexOf("/by")+3).trim(); - tasks[numberOfTasks] = new Deadline(description, date); + public static boolean executeDeadlineCommand(Task[] tasks, int numberOfTasks, String inputCommand) { + try { + String description = getDescriptiong("deadline", inputCommand); + String date = inputCommand.substring(inputCommand.indexOf("/by") + 3).trim(); + tasks[numberOfTasks] = new Deadline(description, date); + } catch(StringIndexOutOfBoundsException e) { + printStatement(DOUBLEINDENTATION + " ☹ OOPS!!! The description of a deadline cannot be empty.\n"); + return true; + } //catch the empty deadline command exception printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + TRIPLEINDENTATION + tasks[numberOfTasks].toString() + "\n" + DOUBLEINDENTATION + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); + return false; } - public static void executeEventCommand(Task[] tasks, int numberOfTasks, String inputCommand) { - String description = getDescriptiong("event", inputCommand); - String date = inputCommand.substring(inputCommand.indexOf("/at")+3).trim(); - tasks[numberOfTasks] = new Event(description, date); + public static boolean executeEventCommand(Task[] tasks, int numberOfTasks, String inputCommand) { + try { + String description = getDescriptiong("event", inputCommand); + String date = inputCommand.substring(inputCommand.indexOf("/at") + 3).trim(); + tasks[numberOfTasks] = new Event(description, date); + } catch (StringIndexOutOfBoundsException e){ + printStatement(DOUBLEINDENTATION + " ☹ OOPS!!! The description of an event cannot be empty.\n"); + return true; + } //catch the empty event command exception printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + TRIPLEINDENTATION + tasks[numberOfTasks].toString() + "\n" + DOUBLEINDENTATION + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); + return false; } public static void main(String[] args) { @@ -71,7 +90,7 @@ public static void main(String[] args) { final String GREET = INDENTATION + " Hello! I'm Duke\n" + INDENTATION + " What can I do for you?\n"; final String BYE = INDENTATION + " Bye. Hope to see you again soon!\n"; - final String WRONGMESSAGE = "Sorry, I don't know what's your command. Can you type again?\n"; + final String WRONGMESSAGE = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n"; boolean isNewTask; String inputCommand; boolean isBye = false; @@ -79,6 +98,7 @@ public static void main(String[] args) { String outputStatement; String description; String date; + boolean isCommandEmpty; printStatement("Hello from\n" + LOGO); // greet in the beginning printStatement(GREET); @@ -90,7 +110,11 @@ public static void main(String[] args) { || inputCommand.contains("event"); switch (inputCommand.trim()) { case "list": - executeListCommand(inputCommand); + //try { + executeListCommand(inputCommand); + /*} catch (NullPointerException e) { + numberOfTasks--; + }*/ break; case "bye": printStatement(BYE); @@ -101,12 +125,23 @@ public static void main(String[] args) { executeDoneCommand(inputCommand); } else if (isNewTask) { if (inputCommand.contains("todo")) { - executeTodoCommand(tasks, numberOfTasks, inputCommand); + try { + executeTodoCommand(tasks, numberOfTasks, inputCommand); + } catch (TodoNullException e){ //catch the empty todo command exception + printStatement(DOUBLEINDENTATION + + " ☹ OOPS!!! The description of a todo cannot be empty.\n"); + numberOfTasks--; //to deal with "numberOfTasks++ below + } } else if (inputCommand.contains("deadline")) { - executeDeadlineCommand(tasks, numberOfTasks, inputCommand); - + isCommandEmpty = executeDeadlineCommand(tasks, numberOfTasks, inputCommand); + if(isCommandEmpty){ + numberOfTasks--; //to deal with "numberOfTasks++ below + } } else if (inputCommand.contains("event")) { - executeEventCommand(tasks, numberOfTasks, inputCommand); + isCommandEmpty = executeEventCommand(tasks, numberOfTasks, inputCommand); + if(isCommandEmpty){ + numberOfTasks--; //to deal with "numberOfTasks++ below + } } numberOfTasks++; } else { From 8e64f22c1c4a144680fa3530db29e562063ad09d Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Wed, 9 Sep 2020 17:21:11 +0800 Subject: [PATCH 12/24] A-Packages: Organize into Packages & two more exceptions Duke.java is put into the package Duke.command Task.java, Todo.java, Deadline.java, and Event.java are put into the package Duke.task The exceptions caused by the command "done" and "done x" where x is out of the boundary of the task list. --- src/main/java/{ => Duke/command}/Duke.java | 28 +++++++++++++++------ src/main/java/{ => Duke/task}/Deadline.java | 2 ++ src/main/java/{ => Duke/task}/Event.java | 2 ++ src/main/java/{ => Duke/task}/Task.java | 6 +++-- src/main/java/{ => Duke/task}/Todo.java | 2 ++ 5 files changed, 30 insertions(+), 10 deletions(-) rename src/main/java/{ => Duke/command}/Duke.java (84%) rename src/main/java/{ => Duke/task}/Deadline.java (93%) rename src/main/java/{ => Duke/task}/Event.java (93%) rename src/main/java/{ => Duke/task}/Task.java (84%) rename src/main/java/{ => Duke/task}/Todo.java (90%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke/command/Duke.java similarity index 84% rename from src/main/java/Duke.java rename to src/main/java/Duke/command/Duke.java index adfa0b1e81..9bfecdfd53 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke/command/Duke.java @@ -1,3 +1,7 @@ +package Duke.command; + +import Duke.task.*; + import java.util.Scanner; public class Duke { @@ -39,9 +43,13 @@ public static void executeListCommand(String inputcommanc) { } public static void executeDoneCommand(String inputCommand) { int doneNum = getDoneNum(inputCommand); - tasks[doneNum-1].markAsDone(); - printStatement(DOUBLEINDENTATION + "Nice! I've marked this task as done: \n" - + TRIPLEINDENTATION + tasks[doneNum-1].toString() + "\n"); + try { + tasks[doneNum - 1].markAsDone(); + printStatement(DOUBLEINDENTATION + "Nice! I've marked this task as done: \n" + + TRIPLEINDENTATION + tasks[doneNum - 1].toString() + "\n"); + } catch (NullPointerException e) { + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There is no task with such index.\n"); + } //catch the command "done x" and x is out of the doundary of the task list } public static void executeTodoCommand(Task[] tasks, int numberOfTasks, String inputCommand) throws TodoNullException{ tasks[numberOfTasks] = new Todo(inputCommand.substring(inputCommand.indexOf("todo")+4).trim()); @@ -58,7 +66,7 @@ public static boolean executeDeadlineCommand(Task[] tasks, int numberOfTasks, St String date = inputCommand.substring(inputCommand.indexOf("/by") + 3).trim(); tasks[numberOfTasks] = new Deadline(description, date); } catch(StringIndexOutOfBoundsException e) { - printStatement(DOUBLEINDENTATION + " ☹ OOPS!!! The description of a deadline cannot be empty.\n"); + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The description of a deadline cannot be empty.\n"); return true; } //catch the empty deadline command exception printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" @@ -72,7 +80,7 @@ public static boolean executeEventCommand(Task[] tasks, int numberOfTasks, Strin String date = inputCommand.substring(inputCommand.indexOf("/at") + 3).trim(); tasks[numberOfTasks] = new Event(description, date); } catch (StringIndexOutOfBoundsException e){ - printStatement(DOUBLEINDENTATION + " ☹ OOPS!!! The description of an event cannot be empty.\n"); + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The description of an event cannot be empty.\n"); return true; } //catch the empty event command exception printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" @@ -90,7 +98,7 @@ public static void main(String[] args) { final String GREET = INDENTATION + " Hello! I'm Duke\n" + INDENTATION + " What can I do for you?\n"; final String BYE = INDENTATION + " Bye. Hope to see you again soon!\n"; - final String WRONGMESSAGE = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n"; + final String WRONGMESSAGE = "☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n"; boolean isNewTask; String inputCommand; boolean isBye = false; @@ -122,14 +130,18 @@ public static void main(String[] args) { break; default: if (inputCommand.contains("done")) { - executeDoneCommand(inputCommand); + try { + executeDoneCommand(inputCommand); + } catch (NumberFormatException e) { + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! You did not indicate which task is done.\n"); + } //catch the cammand "done" whithout any number } else if (isNewTask) { if (inputCommand.contains("todo")) { try { executeTodoCommand(tasks, numberOfTasks, inputCommand); } catch (TodoNullException e){ //catch the empty todo command exception printStatement(DOUBLEINDENTATION + - " ☹ OOPS!!! The description of a todo cannot be empty.\n"); + "☹ OOPS!!! The description of a todo cannot be empty.\n"); numberOfTasks--; //to deal with "numberOfTasks++ below } } else if (inputCommand.contains("deadline")) { diff --git a/src/main/java/Deadline.java b/src/main/java/Duke/task/Deadline.java similarity index 93% rename from src/main/java/Deadline.java rename to src/main/java/Duke/task/Deadline.java index 2de70b7540..405a6655b0 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Duke/task/Deadline.java @@ -1,3 +1,5 @@ +package Duke.task; + public class Deadline extends Task { protected String by; diff --git a/src/main/java/Event.java b/src/main/java/Duke/task/Event.java similarity index 93% rename from src/main/java/Event.java rename to src/main/java/Duke/task/Event.java index a306d9fcbf..7c9f80dc24 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Duke/task/Event.java @@ -1,3 +1,5 @@ +package Duke.task; + public class Event extends Task { protected String at; diff --git a/src/main/java/Task.java b/src/main/java/Duke/task/Task.java similarity index 84% rename from src/main/java/Task.java rename to src/main/java/Duke/task/Task.java index 92262ff2e4..f2b2873173 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Duke/task/Task.java @@ -1,6 +1,8 @@ +package Duke.task; + public class Task { - protected String description; - protected boolean isDone; + public String description; + public boolean isDone; public Task(String description) { this.description = description; diff --git a/src/main/java/Todo.java b/src/main/java/Duke/task/Todo.java similarity index 90% rename from src/main/java/Todo.java rename to src/main/java/Duke/task/Todo.java index 7f7d46a5fa..9a73c9aafc 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Duke/task/Todo.java @@ -1,3 +1,5 @@ +package Duke.task; + public class Todo extends Task { public Todo(String description){ From b8c114e49bfe4f02875fd2d5222c2ded6bd0c665 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 15 Sep 2020 15:29:42 +0800 Subject: [PATCH 13/24] add one more exception catching done commands exceeding total index limit of Tasks[] --- src/main/java/Duke/command/Duke.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/Duke/command/Duke.java b/src/main/java/Duke/command/Duke.java index 9bfecdfd53..45719f4b3a 100644 --- a/src/main/java/Duke/command/Duke.java +++ b/src/main/java/Duke/command/Duke.java @@ -48,7 +48,7 @@ public static void executeDoneCommand(String inputCommand) { printStatement(DOUBLEINDENTATION + "Nice! I've marked this task as done: \n" + TRIPLEINDENTATION + tasks[doneNum - 1].toString() + "\n"); } catch (NullPointerException e) { - printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There is no task with such index.\n"); + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There is no task with such an index.\n"); } //catch the command "done x" and x is out of the doundary of the task list } public static void executeTodoCommand(Task[] tasks, int numberOfTasks, String inputCommand) throws TodoNullException{ @@ -132,9 +132,11 @@ public static void main(String[] args) { if (inputCommand.contains("done")) { try { executeDoneCommand(inputCommand); - } catch (NumberFormatException e) { + } catch (NumberFormatException e) { //catch the cammand "done" whithout any number printStatement(DOUBLEINDENTATION + "☹ OOPS!!! You did not indicate which task is done.\n"); - } //catch the cammand "done" whithout any number + } catch (ArrayIndexOutOfBoundsException e) { //catch commands like "done555" + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The index is out of the list boundary.\n"); + } } else if (isNewTask) { if (inputCommand.contains("todo")) { try { @@ -163,4 +165,4 @@ public static void main(String[] args) { } } -} +} \ No newline at end of file From 9baa0af1024bdb9816823908d54dc031e81e2464 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 15 Sep 2020 16:10:42 +0800 Subject: [PATCH 14/24] A-Collections use an ArrayList to store the tasks instead of Task[ ] --- src/main/java/Duke/command/Duke.java | 50 ++++++++++++++++------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/main/java/Duke/command/Duke.java b/src/main/java/Duke/command/Duke.java index 45719f4b3a..2eb12737ca 100644 --- a/src/main/java/Duke/command/Duke.java +++ b/src/main/java/Duke/command/Duke.java @@ -1,8 +1,8 @@ package Duke.command; import Duke.task.*; - import java.util.Scanner; +import java.util.ArrayList; public class Duke { public static final int MAX_NUM_TASKS = 100; @@ -11,7 +11,7 @@ public class Duke { public static final String TRIPLEINDENTATION = DOUBLEINDENTATION + " "; public static final String HORIZONTALLINE = INDENTATION + "<------------------------------------------------------------>\n"; - public static Task[] tasks = new Task[MAX_NUM_TASKS]; + public static ArrayList tasks = new ArrayList<>(); public static int numberOfTasks = 0; public static class TodoNullException extends Exception { @@ -22,9 +22,11 @@ public static void printStatement(String statement) { System.out.println(HORIZONTALLINE + INDENTATION + "Here is yuqiaoluolong's Duke: \n" + statement + HORIZONTALLINE); } + public static int getDoneNum(String inputCommand) { return Integer.parseInt(inputCommand.replace("done", " ").trim()); } + public static String getDescriptiong(String index, String inputCommand) { if(index == "deadline") { return inputCommand.substring(inputCommand.indexOf("deadline")+8, inputCommand.indexOf("/")).trim(); @@ -32,59 +34,69 @@ public static String getDescriptiong(String index, String inputCommand) { return inputCommand.substring(inputCommand.indexOf("event")+5, inputCommand.indexOf("/")).trim(); } } + public static void executeListCommand(String inputcommanc) { System.out.print(HORIZONTALLINE); System.out.println(INDENTATION + "Here is yuqiaoluolong's Duke: \n" + " Here are the tasks in your list:"); for(int i = 0; i < numberOfTasks; i++){ - System.out.println(DOUBLEINDENTATION + (i+1) + "." + tasks[i].toString()); + System.out.println(DOUBLEINDENTATION + (i+1) + "." + tasks.get(i).toString()); } System.out.println(HORIZONTALLINE); } + public static void executeDoneCommand(String inputCommand) { int doneNum = getDoneNum(inputCommand); try { - tasks[doneNum - 1].markAsDone(); + tasks.get(doneNum - 1).markAsDone(); printStatement(DOUBLEINDENTATION + "Nice! I've marked this task as done: \n" - + TRIPLEINDENTATION + tasks[doneNum - 1].toString() + "\n"); - } catch (NullPointerException e) { + + TRIPLEINDENTATION + tasks.get(doneNum - 1).toString() + "\n"); + } catch (IndexOutOfBoundsException e) { printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There is no task with such an index.\n"); } //catch the command "done x" and x is out of the doundary of the task list } - public static void executeTodoCommand(Task[] tasks, int numberOfTasks, String inputCommand) throws TodoNullException{ - tasks[numberOfTasks] = new Todo(inputCommand.substring(inputCommand.indexOf("todo")+4).trim()); - if(tasks[numberOfTasks].description.length() == 0){ + + public static void executeTodoCommand(ArrayList tasks, int numberOfTasks, String inputCommand) + throws TodoNullException { + try { + tasks.add(numberOfTasks, new Todo(inputCommand.substring(inputCommand.indexOf("todo") + 4).trim())); + } catch (IndexOutOfBoundsException e) { + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There must be an index following toso\n"); + } + if(tasks.get(numberOfTasks).description.length() == 0){ throw new TodoNullException(); } printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" - + TRIPLEINDENTATION + tasks[numberOfTasks].toString() + "\n" + DOUBLEINDENTATION + + TRIPLEINDENTATION + tasks.get(numberOfTasks).toString() + "\n" + DOUBLEINDENTATION + "Now you have " + (numberOfTasks + 1) + " tasks in the list.\n"); } - public static boolean executeDeadlineCommand(Task[] tasks, int numberOfTasks, String inputCommand) { + + public static boolean executeDeadlineCommand(ArrayList tasks, int numberOfTasks, String inputCommand) { try { String description = getDescriptiong("deadline", inputCommand); String date = inputCommand.substring(inputCommand.indexOf("/by") + 3).trim(); - tasks[numberOfTasks] = new Deadline(description, date); + tasks.add(numberOfTasks, new Deadline(description, date)); } catch(StringIndexOutOfBoundsException e) { printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The description of a deadline cannot be empty.\n"); return true; } //catch the empty deadline command exception printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" - + TRIPLEINDENTATION + tasks[numberOfTasks].toString() + "\n" + DOUBLEINDENTATION + + TRIPLEINDENTATION + tasks.get(numberOfTasks).toString() + "\n" + DOUBLEINDENTATION + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); return false; } - public static boolean executeEventCommand(Task[] tasks, int numberOfTasks, String inputCommand) { + + public static boolean executeEventCommand(ArrayList tasks, int numberOfTasks, String inputCommand) { try { String description = getDescriptiong("event", inputCommand); String date = inputCommand.substring(inputCommand.indexOf("/at") + 3).trim(); - tasks[numberOfTasks] = new Event(description, date); + tasks.add(numberOfTasks, new Event(description, date)); } catch (StringIndexOutOfBoundsException e){ printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The description of an event cannot be empty.\n"); return true; } //catch the empty event command exception printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" - + TRIPLEINDENTATION + tasks[numberOfTasks].toString() + "\n" + DOUBLEINDENTATION + + TRIPLEINDENTATION + tasks.get(numberOfTasks).toString() + "\n" + DOUBLEINDENTATION + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); return false; } @@ -118,11 +130,7 @@ public static void main(String[] args) { || inputCommand.contains("event"); switch (inputCommand.trim()) { case "list": - //try { - executeListCommand(inputCommand); - /*} catch (NullPointerException e) { - numberOfTasks--; - }*/ + executeListCommand(inputCommand); break; case "bye": printStatement(BYE); From 4c431729b404f351ea7e06ace16f1539778f06e6 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 15 Sep 2020 16:44:21 +0800 Subject: [PATCH 15/24] A-Collections 2nd version use an ArrayList to store the tasks --- src/main/java/Duke/command/Duke.java | 30 +++++++++++----------------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/main/java/Duke/command/Duke.java b/src/main/java/Duke/command/Duke.java index 2eb12737ca..69aaba3736 100644 --- a/src/main/java/Duke/command/Duke.java +++ b/src/main/java/Duke/command/Duke.java @@ -11,7 +11,7 @@ public class Duke { public static final String TRIPLEINDENTATION = DOUBLEINDENTATION + " "; public static final String HORIZONTALLINE = INDENTATION + "<------------------------------------------------------------>\n"; - public static ArrayList tasks = new ArrayList<>(); + private static ArrayList tasks = new ArrayList<>(); public static int numberOfTasks = 0; public static class TodoNullException extends Exception { @@ -22,11 +22,9 @@ public static void printStatement(String statement) { System.out.println(HORIZONTALLINE + INDENTATION + "Here is yuqiaoluolong's Duke: \n" + statement + HORIZONTALLINE); } - public static int getDoneNum(String inputCommand) { return Integer.parseInt(inputCommand.replace("done", " ").trim()); } - public static String getDescriptiong(String index, String inputCommand) { if(index == "deadline") { return inputCommand.substring(inputCommand.indexOf("deadline")+8, inputCommand.indexOf("/")).trim(); @@ -44,25 +42,19 @@ public static void executeListCommand(String inputcommanc) { } System.out.println(HORIZONTALLINE); } - public static void executeDoneCommand(String inputCommand) { int doneNum = getDoneNum(inputCommand); try { tasks.get(doneNum - 1).markAsDone(); printStatement(DOUBLEINDENTATION + "Nice! I've marked this task as done: \n" + TRIPLEINDENTATION + tasks.get(doneNum - 1).toString() + "\n"); - } catch (IndexOutOfBoundsException e) { + } catch (NullPointerException e) { printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There is no task with such an index.\n"); } //catch the command "done x" and x is out of the doundary of the task list } - public static void executeTodoCommand(ArrayList tasks, int numberOfTasks, String inputCommand) - throws TodoNullException { - try { - tasks.add(numberOfTasks, new Todo(inputCommand.substring(inputCommand.indexOf("todo") + 4).trim())); - } catch (IndexOutOfBoundsException e) { - printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There must be an index following toso\n"); - } + public static void executeTodoCommand(ArrayList tasks, int numberOfTasks, String inputCommand) throws TodoNullException{ + tasks.add(numberOfTasks, new Todo(inputCommand.substring(inputCommand.indexOf("todo") + 4).trim())); if(tasks.get(numberOfTasks).description.length() == 0){ throw new TodoNullException(); } @@ -70,22 +62,20 @@ public static void executeTodoCommand(ArrayList tasks, int numberOfTasks, + TRIPLEINDENTATION + tasks.get(numberOfTasks).toString() + "\n" + DOUBLEINDENTATION + "Now you have " + (numberOfTasks + 1) + " tasks in the list.\n"); } - public static boolean executeDeadlineCommand(ArrayList tasks, int numberOfTasks, String inputCommand) { try { String description = getDescriptiong("deadline", inputCommand); String date = inputCommand.substring(inputCommand.indexOf("/by") + 3).trim(); tasks.add(numberOfTasks, new Deadline(description, date)); - } catch(StringIndexOutOfBoundsException e) { - printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The description of a deadline cannot be empty.\n"); + } catch (StringIndexOutOfBoundsException e) { //catch the empty deadline command exception + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There is nothing following \"deadline\".\n"); return true; - } //catch the empty deadline command exception + } printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + TRIPLEINDENTATION + tasks.get(numberOfTasks).toString() + "\n" + DOUBLEINDENTATION + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); return false; } - public static boolean executeEventCommand(ArrayList tasks, int numberOfTasks, String inputCommand) { try { String description = getDescriptiong("event", inputCommand); @@ -130,7 +120,11 @@ public static void main(String[] args) { || inputCommand.contains("event"); switch (inputCommand.trim()) { case "list": - executeListCommand(inputCommand); + //try { + executeListCommand(inputCommand); + /*} catch (NullPointerException e) { + numberOfTasks--; + }*/ break; case "bye": printStatement(BYE); From e5a1fbac91525e6a88bcd6e40eb21fd640ced056 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Tue, 15 Sep 2020 17:14:49 +0800 Subject: [PATCH 16/24] Level 6. Delete Added the support for deleting tasks from the list. --- src/main/java/Duke/command/Duke.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/Duke/command/Duke.java b/src/main/java/Duke/command/Duke.java index 69aaba3736..51387e78eb 100644 --- a/src/main/java/Duke/command/Duke.java +++ b/src/main/java/Duke/command/Duke.java @@ -25,6 +25,9 @@ public static void printStatement(String statement) { public static int getDoneNum(String inputCommand) { return Integer.parseInt(inputCommand.replace("done", " ").trim()); } + public static int getDeleteNum(String inputCommand) { + return Integer.parseInt(inputCommand.replace("delete", " ").trim()); + } public static String getDescriptiong(String index, String inputCommand) { if(index == "deadline") { return inputCommand.substring(inputCommand.indexOf("deadline")+8, inputCommand.indexOf("/")).trim(); @@ -52,6 +55,14 @@ public static void executeDoneCommand(String inputCommand) { printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There is no task with such an index.\n"); } //catch the command "done x" and x is out of the doundary of the task list } + public static void executeDeleteCommand(String inputCommand) { + int deleteNum = getDeleteNum(inputCommand); + printStatement(DOUBLEINDENTATION + "Noted. I've removed this task: \n" + + TRIPLEINDENTATION + tasks.get(deleteNum-1).toString() + "\n" + + DOUBLEINDENTATION + "Now you have " + (numberOfTasks-1) + " tasks in the list.\n"); + tasks.remove(deleteNum-1); + numberOfTasks--; + } public static void executeTodoCommand(ArrayList tasks, int numberOfTasks, String inputCommand) throws TodoNullException{ tasks.add(numberOfTasks, new Todo(inputCommand.substring(inputCommand.indexOf("todo") + 4).trim())); @@ -120,11 +131,7 @@ public static void main(String[] args) { || inputCommand.contains("event"); switch (inputCommand.trim()) { case "list": - //try { - executeListCommand(inputCommand); - /*} catch (NullPointerException e) { - numberOfTasks--; - }*/ + executeListCommand(inputCommand); break; case "bye": printStatement(BYE); @@ -139,6 +146,8 @@ public static void main(String[] args) { } catch (ArrayIndexOutOfBoundsException e) { //catch commands like "done555" printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The index is out of the list boundary.\n"); } + } else if (inputCommand.contains("delete")) { + executeDeleteCommand(inputCommand); } else if (isNewTask) { if (inputCommand.contains("todo")) { try { From 520a406f92c3d77e47fd6548d117c83bd4e5286e Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Wed, 16 Sep 2020 22:04:07 +0800 Subject: [PATCH 17/24] Level-7: Save Add fileWriting method so that whenever the task list changes, the tasks would be saved in the hard disk automatically --- data/duke.txt | 1 + src/main/java/Duke/command/Duke.java | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 data/duke.txt diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 0000000000..141e2e920d --- /dev/null +++ b/data/duke.txt @@ -0,0 +1 @@ +[T][✘] 1 diff --git a/src/main/java/Duke/command/Duke.java b/src/main/java/Duke/command/Duke.java index 45719f4b3a..284db5460c 100644 --- a/src/main/java/Duke/command/Duke.java +++ b/src/main/java/Duke/command/Duke.java @@ -1,8 +1,9 @@ package Duke.command; import Duke.task.*; - import java.util.Scanner; +import java.io.FileWriter; +import java.io.IOException; public class Duke { public static final int MAX_NUM_TASKS = 100; @@ -89,6 +90,24 @@ public static boolean executeEventCommand(Task[] tasks, int numberOfTasks, Strin return false; } + private static void writeToFile(String filePath, String textToAdd) throws IOException { + FileWriter fw = new FileWriter(filePath); + fw.write(textToAdd); + fw.close(); + } + + private static void write(int numberOfTasks, String file) { + String textToAdd = ""; + for(int i = 0; i Date: Wed, 16 Sep 2020 22:19:05 +0800 Subject: [PATCH 18/24] Addressed conflicts between master and branch level-7 --- data/duke.txt | 3 ++- src/main/java/Duke/command/Duke.java | 9 ++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/data/duke.txt b/data/duke.txt index 141e2e920d..e19e5b3679 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1 +1,2 @@ -[T][✘] 1 +[T][✓] 1 +[T][✘] 2 diff --git a/src/main/java/Duke/command/Duke.java b/src/main/java/Duke/command/Duke.java index 9c8ebcb546..49e7936063 100644 --- a/src/main/java/Duke/command/Duke.java +++ b/src/main/java/Duke/command/Duke.java @@ -2,12 +2,9 @@ import Duke.task.*; import java.util.Scanner; -<<<<<<< HEAD import java.util.ArrayList; -======= import java.io.FileWriter; import java.io.IOException; ->>>>>>> branch-Level-7 public class Duke { public static final int MAX_NUM_TASKS = 100; @@ -116,7 +113,7 @@ private static void writeToFile(String filePath, String textToAdd) throws IOExce private static void write(int numberOfTasks, String file) { String textToAdd = ""; for(int i = 0; i>>>>>> branch-Level-7 } else if (isNewTask) { if (inputCommand.contains("todo")) { try { From 2ccf567756fde490aee130684fc8fbadb56af60d Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Wed, 30 Sep 2020 16:26:31 +0800 Subject: [PATCH 19/24] changed the location of the text file storing the list. --- data/duke.txt | 3 +-- src/main/java/Duke/command/Duke.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/data/duke.txt b/data/duke.txt index e19e5b3679..57518d7c6e 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,2 +1 @@ -[T][✓] 1 -[T][✘] 2 +[T][✘] task1 diff --git a/src/main/java/Duke/command/Duke.java b/src/main/java/Duke/command/Duke.java index 49e7936063..351204cbd6 100644 --- a/src/main/java/Duke/command/Duke.java +++ b/src/main/java/Duke/command/Duke.java @@ -140,8 +140,7 @@ public static void main(String[] args) { String description; String date; boolean isCommandEmpty; - String file = "data/duke.txt"; - + String file = "/Users/yuqiao/Desktop/CS2113T/ip/data/duke.txt"; printStatement("Hello from\n" + LOGO); // greet in the beginning printStatement(GREET); From b1714d998c6c3b73418d6ef236ec48b949db050b Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Fri, 2 Oct 2020 10:25:21 +0800 Subject: [PATCH 20/24] A-MoreOOP: Use More OOP extracted ui class extracted storage class extracted parser class extracted taskList class extracted command class extracted DukeException class --- data/duke.txt | 5 +- src/main/java/Duke/Duke.java | 84 ++++++++ .../InvalidStorageFilePathException.java | 7 + .../Duke/DukeException/TodoNullException.java | 5 + src/main/java/Duke/command/Command.java | 37 ++++ .../java/Duke/command/DeadlineCommand.java | 33 +++ src/main/java/Duke/command/DeleteCommand.java | 26 +++ src/main/java/Duke/command/DoneCommand.java | 31 +++ src/main/java/Duke/command/Duke.java | 204 ------------------ src/main/java/Duke/command/EventCommand.java | 33 +++ .../java/Duke/command/ExceptionCommand.java | 19 ++ src/main/java/Duke/command/ExitCommand.java | 16 ++ src/main/java/Duke/command/ListCommand.java | 25 +++ src/main/java/Duke/command/NullCommand.java | 16 ++ src/main/java/Duke/command/TodoCommand.java | 34 +++ src/main/java/Duke/parser/Parser.java | 75 +++++++ src/main/java/Duke/storage/Storage.java | 110 ++++++++++ src/main/java/Duke/taskList/TaskList.java | 31 +++ src/main/java/Duke/ui/UI.java | 55 +++++ 19 files changed, 641 insertions(+), 205 deletions(-) create mode 100644 src/main/java/Duke/Duke.java create mode 100644 src/main/java/Duke/DukeException/InvalidStorageFilePathException.java create mode 100644 src/main/java/Duke/DukeException/TodoNullException.java create mode 100644 src/main/java/Duke/command/Command.java create mode 100644 src/main/java/Duke/command/DeadlineCommand.java create mode 100644 src/main/java/Duke/command/DeleteCommand.java create mode 100644 src/main/java/Duke/command/DoneCommand.java delete mode 100644 src/main/java/Duke/command/Duke.java create mode 100644 src/main/java/Duke/command/EventCommand.java create mode 100644 src/main/java/Duke/command/ExceptionCommand.java create mode 100644 src/main/java/Duke/command/ExitCommand.java create mode 100644 src/main/java/Duke/command/ListCommand.java create mode 100644 src/main/java/Duke/command/NullCommand.java create mode 100644 src/main/java/Duke/command/TodoCommand.java create mode 100644 src/main/java/Duke/parser/Parser.java create mode 100644 src/main/java/Duke/storage/Storage.java create mode 100644 src/main/java/Duke/taskList/TaskList.java create mode 100644 src/main/java/Duke/ui/UI.java diff --git a/data/duke.txt b/data/duke.txt index 57518d7c6e..f85f80820c 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1 +1,4 @@ -[T][✘] task1 +[T][✓] task +[T][✘] task2 +[D][✘] d2 (by: 6pm) +[E][✘] e1 (at: 7pm, tommorow) diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java new file mode 100644 index 0000000000..9083c29fec --- /dev/null +++ b/src/main/java/Duke/Duke.java @@ -0,0 +1,84 @@ +package Duke; + +import Duke.command.Command; +import Duke.DukeException.TodoNullException; +import Duke.parser.Parser; +import Duke.storage.Storage; +import Duke.taskList.TaskList; +import Duke.ui.UI; + +import static Duke.ui.UI.DOUBLEINDENTATION; +import static Duke.ui.UI.printStatement; + +public class Duke { + + private Storage storage; + private TaskList tasks; + private UI ui; + + public Duke(String filePath) { + ui = new UI(); + storage = new Storage(filePath); + //try { + tasks = new TaskList(storage.load()); + /*} catch (DukeException e) { + ui.showLoadingError(); + tasks = new TaskList(); + }*/ + } + + public void run() { + ui.showWelcome(); + boolean isExit = false; + int numberOfTasks = storage.loadNumberOfTasks(); + while (!isExit) { + //try { + String fullCommand = ui.readCommand(); + ui.showLine(); // show the divider line ("_______") + Command c; + if(fullCommand.contains("todo")) { + c = Parser.parseTodoCommand(fullCommand, numberOfTasks); + numberOfTasks++; + } else if(fullCommand.contains("deadline")){ + c = Parser.parseDeadlineCommand(fullCommand, numberOfTasks); + numberOfTasks++; + } else if(fullCommand.contains("event")) { + c = Parser.parseEventCommand(fullCommand, numberOfTasks); + numberOfTasks++; + } else if(fullCommand.contains("list")){ + c = Parser.parseListCommand(numberOfTasks); + } else if(fullCommand.contains("done")) { + c = Parser.parseDoneCommand(fullCommand, numberOfTasks); + } else if(fullCommand.contains("delete")) { + c = Parser.parseDeleteCommand(fullCommand, numberOfTasks); + numberOfTasks--; + } else if(fullCommand.contains("bye")){ + c = Parser.parseExitCommand(numberOfTasks); + } else { + c = Parser.parseExceptionCommand(numberOfTasks); + } + try{ + c.execute(tasks, ui, storage); + c.sava(tasks, ui, storage); + isExit = c.isExit(); + } catch (NullPointerException e) { + numberOfTasks--; + } catch (TodoNullException e) { + printStatement(DOUBLEINDENTATION + + "☹ OOPS!!! The description of a todo cannot be empty.\n"); + tasks.remove(numberOfTasks-1); + numberOfTasks--; + } + + /*} catch (DukeException e) { + ui.showError(e.getMessage()); + } finally { + ui.showLine(); + }*/ + } + } + + public static void main(String[] args) { + new Duke("/Users/yuqiao/Desktop/CS2113T/ip/data/duke.txt").run(); + } +} \ No newline at end of file diff --git a/src/main/java/Duke/DukeException/InvalidStorageFilePathException.java b/src/main/java/Duke/DukeException/InvalidStorageFilePathException.java new file mode 100644 index 0000000000..d98b853e10 --- /dev/null +++ b/src/main/java/Duke/DukeException/InvalidStorageFilePathException.java @@ -0,0 +1,7 @@ +package Duke.DukeException; + +public class InvalidStorageFilePathException extends Exception { + public InvalidStorageFilePathException(String s) { + } + //no other code needed +} diff --git a/src/main/java/Duke/DukeException/TodoNullException.java b/src/main/java/Duke/DukeException/TodoNullException.java new file mode 100644 index 0000000000..9108c2d8c3 --- /dev/null +++ b/src/main/java/Duke/DukeException/TodoNullException.java @@ -0,0 +1,5 @@ +package Duke.DukeException; + +public class TodoNullException extends Exception { + //no other code needed +} diff --git a/src/main/java/Duke/command/Command.java b/src/main/java/Duke/command/Command.java new file mode 100644 index 0000000000..44bec017cd --- /dev/null +++ b/src/main/java/Duke/command/Command.java @@ -0,0 +1,37 @@ +package Duke.command; + +import Duke.DukeException.TodoNullException; +import Duke.storage.Storage; +import Duke.taskList.TaskList; +import Duke.ui.UI; + +public class Command { + protected String description; + protected int numberOfTasks = 0; + + public Command(String description, int numberOfTasks) { + this.description = description; + this.numberOfTasks = numberOfTasks; + } + + public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException { + numberOfTasks++; + } + + public void sava(TaskList tasks, UI ui, Storage storage){ + + } + + public boolean isExit() { + if(description == "bye") { + return true; + } + else { + return false; + } + } + + public Command change() { + return null; + } +} diff --git a/src/main/java/Duke/command/DeadlineCommand.java b/src/main/java/Duke/command/DeadlineCommand.java new file mode 100644 index 0000000000..1afae343cd --- /dev/null +++ b/src/main/java/Duke/command/DeadlineCommand.java @@ -0,0 +1,33 @@ +package Duke.command; + +import Duke.DukeException.TodoNullException; +import Duke.storage.Storage; +import Duke.task.Deadline; +import Duke.taskList.TaskList; +import Duke.ui.UI; + +import static Duke.ui.UI.DOUBLEINDENTATION; +import static Duke.ui.UI.TRIPLEINDENTATION; + +public class DeadlineCommand extends Command { + protected String by; + + public DeadlineCommand(String description, int numberOfTasks, String by) { + super(description, numberOfTasks); + this.by = by; + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException { + super.execute(tasks, ui, storage); + tasks.add(new Deadline(this.description, this.by)); + ui.printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + + TRIPLEINDENTATION + tasks.get(numberOfTasks - 1).toString() + "\n" + DOUBLEINDENTATION + + "Now you have " + (numberOfTasks) + " tasks in the list.\n"); + } + + @Override + public void sava(TaskList tasks, UI ui, Storage storage){ + Storage.save(tasks, numberOfTasks); + } +} diff --git a/src/main/java/Duke/command/DeleteCommand.java b/src/main/java/Duke/command/DeleteCommand.java new file mode 100644 index 0000000000..840b7bd2d6 --- /dev/null +++ b/src/main/java/Duke/command/DeleteCommand.java @@ -0,0 +1,26 @@ +package Duke.command; + +import Duke.storage.Storage; +import Duke.taskList.TaskList; +import Duke.ui.UI; + +import static Duke.ui.UI.*; + +public class DeleteCommand extends Command { + public DeleteCommand(String description, int numberOfTasks) { + super(description, numberOfTasks); + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage){ + printStatement(DOUBLEINDENTATION + "Noted. I've removed this task: \n" + + TRIPLEINDENTATION + tasks.get(Integer.parseInt(this.description)-1).toString() + "\n" + + DOUBLEINDENTATION + "Now you have " + (numberOfTasks-1) + " tasks in the list.\n"); + tasks.remove(Integer.parseInt(this.description)-1); + } + + @Override + public void sava(TaskList tasks, UI ui, Storage storage){ + Storage.save(tasks, numberOfTasks-1); + } +} diff --git a/src/main/java/Duke/command/DoneCommand.java b/src/main/java/Duke/command/DoneCommand.java new file mode 100644 index 0000000000..1ffc9ed5cf --- /dev/null +++ b/src/main/java/Duke/command/DoneCommand.java @@ -0,0 +1,31 @@ +package Duke.command; + +import Duke.storage.Storage; +import Duke.taskList.TaskList; +import Duke.ui.UI; + +import static Duke.ui.UI.*; + +public class DoneCommand extends Command { + public DoneCommand(String description, int numberOfTasks) { + super(description, numberOfTasks); + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage){ + try { + tasks.get(Integer.parseInt(this.description)- 1).markAsDone(); + printStatement(DOUBLEINDENTATION + "Nice! I've marked this task as done: \n" + + TRIPLEINDENTATION + tasks.get(Integer.parseInt(this.description) - 1).toString() + "\n"); + } catch (NumberFormatException e) { + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! You did not indicate which task is done.\n"); + } catch (IndexOutOfBoundsException e) { + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The index is out of the list boundary.\n"); + } + } + + @Override + public void sava(TaskList tasks, UI ui, Storage storage){ + Storage.save(tasks, numberOfTasks); + } +} diff --git a/src/main/java/Duke/command/Duke.java b/src/main/java/Duke/command/Duke.java deleted file mode 100644 index 351204cbd6..0000000000 --- a/src/main/java/Duke/command/Duke.java +++ /dev/null @@ -1,204 +0,0 @@ -package Duke.command; - -import Duke.task.*; -import java.util.Scanner; -import java.util.ArrayList; -import java.io.FileWriter; -import java.io.IOException; - -public class Duke { - public static final int MAX_NUM_TASKS = 100; - public static final String INDENTATION = " "; - public static final String DOUBLEINDENTATION = INDENTATION + " "; - public static final String TRIPLEINDENTATION = DOUBLEINDENTATION + " "; - public static final String HORIZONTALLINE = INDENTATION - + "<------------------------------------------------------------>\n"; - private static ArrayList tasks = new ArrayList<>(); - public static int numberOfTasks = 0; - - public static class TodoNullException extends Exception { - //no other code needed - } - - public static void printStatement(String statement) { - System.out.println(HORIZONTALLINE + INDENTATION + "Here is yuqiaoluolong's Duke: \n" - + statement + HORIZONTALLINE); - } - public static int getDoneNum(String inputCommand) { - return Integer.parseInt(inputCommand.replace("done", " ").trim()); - } - public static int getDeleteNum(String inputCommand) { - return Integer.parseInt(inputCommand.replace("delete", " ").trim()); - } - public static String getDescriptiong(String index, String inputCommand) { - if(index == "deadline") { - return inputCommand.substring(inputCommand.indexOf("deadline")+8, inputCommand.indexOf("/")).trim(); - } else { - return inputCommand.substring(inputCommand.indexOf("event")+5, inputCommand.indexOf("/")).trim(); - } - } - - public static void executeListCommand(String inputcommanc) { - System.out.print(HORIZONTALLINE); - System.out.println(INDENTATION + "Here is yuqiaoluolong's Duke: \n" + - " Here are the tasks in your list:"); - for(int i = 0; i < numberOfTasks; i++){ - System.out.println(DOUBLEINDENTATION + (i+1) + "." + tasks.get(i).toString()); - } - System.out.println(HORIZONTALLINE); - } - public static void executeDoneCommand(String inputCommand) { - int doneNum = getDoneNum(inputCommand); - try { - tasks.get(doneNum - 1).markAsDone(); - printStatement(DOUBLEINDENTATION + "Nice! I've marked this task as done: \n" - + TRIPLEINDENTATION + tasks.get(doneNum - 1).toString() + "\n"); - } catch (NullPointerException e) { - printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There is no task with such an index.\n"); - } //catch the command "done x" and x is out of the doundary of the task list - } - public static void executeDeleteCommand(String inputCommand) { - int deleteNum = getDeleteNum(inputCommand); - printStatement(DOUBLEINDENTATION + "Noted. I've removed this task: \n" + - TRIPLEINDENTATION + tasks.get(deleteNum-1).toString() + "\n" + - DOUBLEINDENTATION + "Now you have " + (numberOfTasks-1) + " tasks in the list.\n"); - tasks.remove(deleteNum-1); - numberOfTasks--; - } - - public static void executeTodoCommand(ArrayList tasks, int numberOfTasks, String inputCommand) throws TodoNullException{ - tasks.add(numberOfTasks, new Todo(inputCommand.substring(inputCommand.indexOf("todo") + 4).trim())); - if(tasks.get(numberOfTasks).description.length() == 0){ - throw new TodoNullException(); - } - printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" - + TRIPLEINDENTATION + tasks.get(numberOfTasks).toString() + "\n" + DOUBLEINDENTATION - + "Now you have " + (numberOfTasks + 1) + " tasks in the list.\n"); - } - public static boolean executeDeadlineCommand(ArrayList tasks, int numberOfTasks, String inputCommand) { - try { - String description = getDescriptiong("deadline", inputCommand); - String date = inputCommand.substring(inputCommand.indexOf("/by") + 3).trim(); - tasks.add(numberOfTasks, new Deadline(description, date)); - } catch (StringIndexOutOfBoundsException e) { //catch the empty deadline command exception - printStatement(DOUBLEINDENTATION + "☹ OOPS!!! There is nothing following \"deadline\".\n"); - return true; - } - printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" - + TRIPLEINDENTATION + tasks.get(numberOfTasks).toString() + "\n" + DOUBLEINDENTATION - + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); - return false; - } - public static boolean executeEventCommand(ArrayList tasks, int numberOfTasks, String inputCommand) { - try { - String description = getDescriptiong("event", inputCommand); - String date = inputCommand.substring(inputCommand.indexOf("/at") + 3).trim(); - tasks.add(numberOfTasks, new Event(description, date)); - } catch (StringIndexOutOfBoundsException e){ - printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The description of an event cannot be empty.\n"); - return true; - } //catch the empty event command exception - printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" - + TRIPLEINDENTATION + tasks.get(numberOfTasks).toString() + "\n" + DOUBLEINDENTATION - + "Now you have " + (numberOfTasks+1) + " tasks in the list.\n"); - return false; - } - - private static void writeToFile(String filePath, String textToAdd) throws IOException { - FileWriter fw = new FileWriter(filePath); - fw.write(textToAdd); - fw.close(); - } - - private static void write(int numberOfTasks, String file) { - String textToAdd = ""; - for(int i = 0; i load(){ + ArrayList tasklist = new ArrayList<>(); + try { + for (String encodedTask : Files.readAllLines(path)) { + if(encodedTask.contains("D")) { + tasklist.add(new Deadline(getDescriptiongFromFile(encodedTask), + encodedTask.substring(encodedTask.indexOf("by")+3, + encodedTask.lastIndexOf(")")).trim())); + if(encodedTask.contains("\u2713")) { + tasklist.get(tasklist.size()-1).markAsDone(); + } + } else if(encodedTask.contains("E")) { + tasklist.add(new Event(getDescriptiongFromFile(encodedTask), + encodedTask.substring(encodedTask.indexOf("at")+3, + encodedTask.lastIndexOf(")")).trim())); + if(encodedTask.contains("\u2713")) { + tasklist.get(tasklist.size()-1).markAsDone(); + } + } else { + tasklist.add(new Todo(getDescriptiongFromFile(encodedTask))); + if(encodedTask.contains("\u2713")) { + tasklist.get(tasklist.size()-1).markAsDone(); + } + } + } + return tasklist; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public static String getDescriptiongFromFile(String inputCommand) { + if(inputCommand.contains(":")){ + return inputCommand.substring(inputCommand.lastIndexOf("]") + 1, inputCommand.indexOf((":"))-4).trim(); + } else { + return inputCommand.substring(inputCommand.lastIndexOf("]") + 1).trim(); + } + } + + public int loadNumberOfTasks(){ + int number = 0; + try { + for (int i = 0; i< Files.readAllLines(path).size(); i++) { + number++; + } + return number; + } catch (IOException e) { + e.printStackTrace(); + } + return -1; + } + public String getPath() { + return path.toString(); + } +} + diff --git a/src/main/java/Duke/taskList/TaskList.java b/src/main/java/Duke/taskList/TaskList.java new file mode 100644 index 0000000000..fa5cb17934 --- /dev/null +++ b/src/main/java/Duke/taskList/TaskList.java @@ -0,0 +1,31 @@ +package Duke.taskList; + +import Duke.task.Task; + +import java.util.ArrayList; + +public class TaskList { + public static final int MAX_NUM_TASKS = 100; + private static ArrayList taskList = new ArrayList<>(); + + public TaskList() { + } + + public TaskList(ArrayList tasks) { + for(int i=0; i< tasks.size(); i++){ + taskList.add(tasks.get(i)); + } + } + + public void add(Task oneTask){ + taskList.add(oneTask); + } + + public Task get(int index){ + return taskList.get(index); + } + + public void remove(int i) { + taskList.remove(i); + } +} diff --git a/src/main/java/Duke/ui/UI.java b/src/main/java/Duke/ui/UI.java new file mode 100644 index 0000000000..603af41cda --- /dev/null +++ b/src/main/java/Duke/ui/UI.java @@ -0,0 +1,55 @@ +package Duke.ui; + +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Scanner; + +public class UI { + private static final String LS = System.lineSeparator(); + public static final int MAX_NUM_TASKS = 100; + public static final String INDENTATION = " "; + public static final String DOUBLEINDENTATION = INDENTATION + " "; + public static final String TRIPLEINDENTATION = DOUBLEINDENTATION + " "; + public static final String HORIZONTALLINE = INDENTATION + + "<------------------------------------------------------------>\n"; + public final String LOGO = TRIPLEINDENTATION + " ____ _ \n" + + TRIPLEINDENTATION + "| _ \\ _ _| | _____ \n" + + TRIPLEINDENTATION + "| | | | | | | |/ / _ \\\n" + + TRIPLEINDENTATION + "| |_| | |_| | < __/\n" + + TRIPLEINDENTATION + "|____/ \\__,_|_|\\_\\___|\n"; + public final String GREET = INDENTATION + " Hello! I'm Duke\n" + + INDENTATION + " What can I do for you?\n"; + public final String BYE = INDENTATION + " Bye. Hope to see you again soon!\n"; + public final String WRONGMESSAGE = "☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n"; + + 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 static void printStatement(String statement) { + System.out.println(HORIZONTALLINE + INDENTATION + "Here is yuqiaoluolong's Duke: \n" + + statement + HORIZONTALLINE); + } + + public void showWelcome() { + printStatement(TRIPLEINDENTATION + "Hello from\n" + LOGO); + printStatement(GREET); + } + + public String readCommand(){ + String input = in.nextLine(); + return input; + } + + public void showLine(){ + System.out.printf(LS); + } + +} From 87c7bfcadcab50a9789160d8cd51c37f83d7c519 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Fri, 2 Oct 2020 20:06:43 +0800 Subject: [PATCH 21/24] Level-8: Cates and Times --- data/duke.txt | 7 ++- src/main/java/Duke/Duke.java | 19 ++++++- .../DukeException/DeadlineNullException.java | 4 ++ .../DukeException/EventNullException.java | 4 ++ src/main/java/Duke/command/Command.java | 6 ++- .../java/Duke/command/DeadlineCommand.java | 22 ++++++-- src/main/java/Duke/command/DeleteCommand.java | 12 +++-- src/main/java/Duke/command/EventCommand.java | 14 +++-- src/main/java/Duke/command/FindCommand.java | 27 ++++++++++ src/main/java/Duke/command/HelpCommand.java | 32 ++++++++++++ src/main/java/Duke/command/TodoCommand.java | 5 +- src/main/java/Duke/parser/Parser.java | 8 +++ src/main/java/Duke/storage/Storage.java | 52 ++++++++++++++++--- src/main/java/Duke/task/Deadline.java | 9 ++-- src/main/java/Duke/task/Event.java | 9 ++-- src/main/java/Duke/ui/UI.java | 2 +- 16 files changed, 198 insertions(+), 34 deletions(-) create mode 100644 src/main/java/Duke/DukeException/DeadlineNullException.java create mode 100644 src/main/java/Duke/DukeException/EventNullException.java create mode 100644 src/main/java/Duke/command/FindCommand.java create mode 100644 src/main/java/Duke/command/HelpCommand.java diff --git a/data/duke.txt b/data/duke.txt index f85f80820c..9c12016843 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,4 +1,3 @@ -[T][✓] task -[T][✘] task2 -[D][✘] d2 (by: 6pm) -[E][✘] e1 (at: 7pm, tommorow) +[T][✘] task1 +[D][✘] d2 (by: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30) +[E][✘] e1 (at: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30) diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java index 9083c29fec..e725c52048 100644 --- a/src/main/java/Duke/Duke.java +++ b/src/main/java/Duke/Duke.java @@ -1,12 +1,16 @@ package Duke; -import Duke.command.Command; +import Duke.DukeException.DeadlineNullException; +import Duke.DukeException.EventNullException; import Duke.DukeException.TodoNullException; +import Duke.command.Command; import Duke.parser.Parser; import Duke.storage.Storage; import Duke.taskList.TaskList; import Duke.ui.UI; +import java.time.Month; + import static Duke.ui.UI.DOUBLEINDENTATION; import static Duke.ui.UI.printStatement; @@ -45,13 +49,20 @@ public void run() { } else if(fullCommand.contains("event")) { c = Parser.parseEventCommand(fullCommand, numberOfTasks); numberOfTasks++; + } else if(fullCommand.contains("help")) { + c = Parser.parseHelpCommand(numberOfTasks); } else if(fullCommand.contains("list")){ c = Parser.parseListCommand(numberOfTasks); + } else if(fullCommand.contains("find")) { + c = Parser.parseFindCommand(fullCommand.substring(fullCommand.indexOf("find")+4).trim(), + numberOfTasks); } else if(fullCommand.contains("done")) { c = Parser.parseDoneCommand(fullCommand, numberOfTasks); } else if(fullCommand.contains("delete")) { c = Parser.parseDeleteCommand(fullCommand, numberOfTasks); - numberOfTasks--; + if(c.description.length() != 0) { + numberOfTasks--; + } } else if(fullCommand.contains("bye")){ c = Parser.parseExitCommand(numberOfTasks); } else { @@ -68,6 +79,10 @@ public void run() { "☹ OOPS!!! The description of a todo cannot be empty.\n"); tasks.remove(numberOfTasks-1); numberOfTasks--; + } catch (DeadlineNullException e) { + numberOfTasks--; + } catch (EventNullException e) { + numberOfTasks--; } /*} catch (DukeException e) { diff --git a/src/main/java/Duke/DukeException/DeadlineNullException.java b/src/main/java/Duke/DukeException/DeadlineNullException.java new file mode 100644 index 0000000000..be6faa2379 --- /dev/null +++ b/src/main/java/Duke/DukeException/DeadlineNullException.java @@ -0,0 +1,4 @@ +package Duke.DukeException; + +public class DeadlineNullException extends Exception { +} diff --git a/src/main/java/Duke/DukeException/EventNullException.java b/src/main/java/Duke/DukeException/EventNullException.java new file mode 100644 index 0000000000..4fdbf71de1 --- /dev/null +++ b/src/main/java/Duke/DukeException/EventNullException.java @@ -0,0 +1,4 @@ +package Duke.DukeException; + +public class EventNullException extends Throwable { +} diff --git a/src/main/java/Duke/command/Command.java b/src/main/java/Duke/command/Command.java index 44bec017cd..bd6c324ca2 100644 --- a/src/main/java/Duke/command/Command.java +++ b/src/main/java/Duke/command/Command.java @@ -1,12 +1,14 @@ package Duke.command; +import Duke.DukeException.DeadlineNullException; +import Duke.DukeException.EventNullException; import Duke.DukeException.TodoNullException; import Duke.storage.Storage; import Duke.taskList.TaskList; import Duke.ui.UI; public class Command { - protected String description; + public String description; protected int numberOfTasks = 0; public Command(String description, int numberOfTasks) { @@ -14,7 +16,7 @@ public Command(String description, int numberOfTasks) { this.numberOfTasks = numberOfTasks; } - public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException { + public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException, DeadlineNullException, EventNullException { numberOfTasks++; } diff --git a/src/main/java/Duke/command/DeadlineCommand.java b/src/main/java/Duke/command/DeadlineCommand.java index 1afae343cd..e5b158f666 100644 --- a/src/main/java/Duke/command/DeadlineCommand.java +++ b/src/main/java/Duke/command/DeadlineCommand.java @@ -1,25 +1,37 @@ package Duke.command; +import Duke.DukeException.DeadlineNullException; +import Duke.DukeException.EventNullException; import Duke.DukeException.TodoNullException; import Duke.storage.Storage; import Duke.task.Deadline; import Duke.taskList.TaskList; import Duke.ui.UI; -import static Duke.ui.UI.DOUBLEINDENTATION; -import static Duke.ui.UI.TRIPLEINDENTATION; +import java.time.LocalDateTime; +import java.time.format.DateTimeParseException; + +import static Duke.ui.UI.*; public class DeadlineCommand extends Command { - protected String by; + protected LocalDateTime by; public DeadlineCommand(String description, int numberOfTasks, String by) { super(description, numberOfTasks); - this.by = by; + try { + this.by = LocalDateTime.parse(by); + } catch (DateTimeParseException e) { + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! The time entered is not in the format(yyyy-mm-dd).\n"); + } } @Override - public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException { + public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException, DeadlineNullException, + EventNullException { super.execute(tasks, ui, storage); + if(this.by.toString().length() == 0 && this.by.toString().length() == 0){ + throw new DeadlineNullException(); + } tasks.add(new Deadline(this.description, this.by)); ui.printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + TRIPLEINDENTATION + tasks.get(numberOfTasks - 1).toString() + "\n" + DOUBLEINDENTATION diff --git a/src/main/java/Duke/command/DeleteCommand.java b/src/main/java/Duke/command/DeleteCommand.java index 840b7bd2d6..5c41e2dd78 100644 --- a/src/main/java/Duke/command/DeleteCommand.java +++ b/src/main/java/Duke/command/DeleteCommand.java @@ -13,10 +13,14 @@ public DeleteCommand(String description, int numberOfTasks) { @Override public void execute(TaskList tasks, UI ui, Storage storage){ - printStatement(DOUBLEINDENTATION + "Noted. I've removed this task: \n" + - TRIPLEINDENTATION + tasks.get(Integer.parseInt(this.description)-1).toString() + "\n" + - DOUBLEINDENTATION + "Now you have " + (numberOfTasks-1) + " tasks in the list.\n"); - tasks.remove(Integer.parseInt(this.description)-1); + try { + printStatement(DOUBLEINDENTATION + "Noted. I've removed this task: \n" + + TRIPLEINDENTATION + tasks.get(Integer.parseInt(this.description)-1).toString() + "\n" + + DOUBLEINDENTATION + "Now you have " + (numberOfTasks-1) + " tasks in the list.\n"); + tasks.remove(Integer.parseInt(this.description)-1); + } catch (NumberFormatException e) { + printStatement(DOUBLEINDENTATION + "☹ OOPS!!! You did not indicate which task to delete.\n"); + } } @Override diff --git a/src/main/java/Duke/command/EventCommand.java b/src/main/java/Duke/command/EventCommand.java index f33e5ec7b4..94118efe3f 100644 --- a/src/main/java/Duke/command/EventCommand.java +++ b/src/main/java/Duke/command/EventCommand.java @@ -1,25 +1,33 @@ package Duke.command; +import Duke.DukeException.DeadlineNullException; +import Duke.DukeException.EventNullException; import Duke.DukeException.TodoNullException; import Duke.storage.Storage; import Duke.task.Event; import Duke.taskList.TaskList; import Duke.ui.UI; +import java.time.LocalDateTime; + import static Duke.ui.UI.DOUBLEINDENTATION; import static Duke.ui.UI.TRIPLEINDENTATION; public class EventCommand extends Command { - protected String at; + protected LocalDateTime at; public EventCommand(String description, int numberOfTasks, String at) { super(description, numberOfTasks); - this.at = at; + this.at = LocalDateTime.parse(at); } @Override - public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException { + public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException, DeadlineNullException, + EventNullException { super.execute(tasks, ui, storage); + if(this.at.toString().length() == 0 && this.at.toString().length() == 0){ + throw new EventNullException(); + } tasks.add(new Event(this.description, this.at)); ui.printStatement(DOUBLEINDENTATION + "Got it. I've added this task:\n" + TRIPLEINDENTATION + tasks.get(numberOfTasks - 1).toString() + "\n" + DOUBLEINDENTATION diff --git a/src/main/java/Duke/command/FindCommand.java b/src/main/java/Duke/command/FindCommand.java new file mode 100644 index 0000000000..c9c0cd34a7 --- /dev/null +++ b/src/main/java/Duke/command/FindCommand.java @@ -0,0 +1,27 @@ +package Duke.command; + +import Duke.storage.Storage; +import Duke.taskList.TaskList; +import Duke.ui.UI; + +import static Duke.ui.UI.*; + +public class FindCommand extends Command { + public FindCommand(String description, int numberOfTasks) { + super(description, numberOfTasks); + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + System.out.print(HORIZONTALLINE); + System.out.println(INDENTATION + "Here is yuqiaoluolong's Duke: \n" + + " Here are the matching tasks in your list:"); + for(int i = 0; i < numberOfTasks; i++){ + if(tasks.get(i).toString().contains(description)) { + System.out.println(DOUBLEINDENTATION + (i + 1) + "." + tasks.get(i).toString()); + } + } + System.out.println(HORIZONTALLINE); + } +} + diff --git a/src/main/java/Duke/command/HelpCommand.java b/src/main/java/Duke/command/HelpCommand.java new file mode 100644 index 0000000000..faf1d497f7 --- /dev/null +++ b/src/main/java/Duke/command/HelpCommand.java @@ -0,0 +1,32 @@ +package Duke.command; + +import Duke.storage.Storage; +import Duke.taskList.TaskList; +import Duke.ui.UI; + +import static Duke.ui.UI.DOUBLEINDENTATION; +import static Duke.ui.UI.TRIPLEINDENTATION; + +public class HelpCommand extends Command { + public HelpCommand(String description, int numberOfTasks) { + super(description, numberOfTasks); + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + UI.printStatement(DOUBLEINDENTATION + "Hello, below is the explanation of the functions:\n" + + TRIPLEINDENTATION + "1. you can add todo tasks follwed by the description\n" + + TRIPLEINDENTATION + "example: todo task1 --> [T][✘] task1\n" + + TRIPLEINDENTATION + "2. you can add deadlines followed by the description and the due time\n" + + TRIPLEINDENTATION + "example: deadline d2 /by 2015-02-20T06:30:00 --> " + + "[D][✘] d2 (by: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30)\n" + + TRIPLEINDENTATION + "3. you can add events followed by the description and the happening time\n" + + TRIPLEINDENTATION + "example: event e2 /by 2015-02-20T06:30:00 --> " + + "[E][✘] e2 (by: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30)\n" + + TRIPLEINDENTATION + "4. you can view all the tasks by typing \" list\" \n" + + TRIPLEINDENTATION + "5. you can search for the tasks containing some words\n" + + TRIPLEINDENTATION + "example: find book --> Duke will show all the tasks related with book\n" + + TRIPLEINDENTATION + "6. you can mark one task as done by byping \"done\" followed its number\n" + + TRIPLEINDENTATION + "7. you can delete one task as done by byping \"delete\" followed its number\n"); + } +} diff --git a/src/main/java/Duke/command/TodoCommand.java b/src/main/java/Duke/command/TodoCommand.java index 0f39ff52a7..135cf33333 100644 --- a/src/main/java/Duke/command/TodoCommand.java +++ b/src/main/java/Duke/command/TodoCommand.java @@ -1,5 +1,7 @@ package Duke.command; +import Duke.DukeException.DeadlineNullException; +import Duke.DukeException.EventNullException; import Duke.DukeException.TodoNullException; import Duke.storage.Storage; import Duke.task.Todo; @@ -16,7 +18,8 @@ public TodoCommand(String description, int numberOfTasks) { } @Override - public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException{ + public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException, DeadlineNullException, + EventNullException { super.execute(tasks, ui, storage); tasks.add(new Todo(this.description)); if(tasks.get(numberOfTasks-1).description.length() == 0){ diff --git a/src/main/java/Duke/parser/Parser.java b/src/main/java/Duke/parser/Parser.java index cef13afa07..7545882f24 100644 --- a/src/main/java/Duke/parser/Parser.java +++ b/src/main/java/Duke/parser/Parser.java @@ -72,4 +72,12 @@ public static Command parseDeleteCommand(String fullCommand, int numberOfTasks) public static Command parseExceptionCommand(int numberOfTasks) { return new ExceptionCommand("exception", numberOfTasks); } + + public static Command parseFindCommand(String index, int numberOfTasks) { + return new FindCommand(index, numberOfTasks); + } + + public static Command parseHelpCommand(int numberOfTasks) { + return new HelpCommand("help", numberOfTasks); + } } diff --git a/src/main/java/Duke/storage/Storage.java b/src/main/java/Duke/storage/Storage.java index 179e9a5ff7..15ba6e0e04 100644 --- a/src/main/java/Duke/storage/Storage.java +++ b/src/main/java/Duke/storage/Storage.java @@ -12,8 +12,14 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.time.LocalDateTime; +import java.time.Month; +import java.time.format.DateTimeParseException; import java.util.ArrayList; +import static Duke.ui.UI.INDENTATION; +import static Duke.ui.UI.printStatement; + public class Storage { public static final String DEFAULT_STORAGE_FILEPATH = "/Users/yuqiao/Desktop/CS2113T/ip/data/duke.txt"; public static Path path = Path.of(DEFAULT_STORAGE_FILEPATH); @@ -56,16 +62,50 @@ public ArrayList load(){ try { for (String encodedTask : Files.readAllLines(path)) { if(encodedTask.contains("D")) { - tasklist.add(new Deadline(getDescriptiongFromFile(encodedTask), - encodedTask.substring(encodedTask.indexOf("by")+3, - encodedTask.lastIndexOf(")")).trim())); + int year, dayOfMonth, hour, minute; + Month month; + try{ + year =Integer.parseInt(encodedTask.substring(encodedTask.indexOf("Year") + 5, + encodedTask.indexOf("Month") - 2)); + month = Month.valueOf(encodedTask.substring(encodedTask.indexOf("Month") + 6, + encodedTask.indexOf("Day") - 2)); + dayOfMonth = Integer.parseInt(encodedTask.substring(encodedTask.indexOf("Day") + 4, + encodedTask.indexOf("Time") - 2)); + hour = Integer.parseInt(encodedTask.substring(encodedTask.lastIndexOf("h") + 2, + encodedTask.lastIndexOf("m") - 2)); + minute = Integer.parseInt(encodedTask.substring(encodedTask.lastIndexOf("m") + 2, + encodedTask.indexOf(")"))); + tasklist.add(new Deadline(getDescriptiongFromFile(encodedTask), + LocalDateTime.of(year, month, dayOfMonth, hour, minute))); + } catch (DateTimeParseException e) { + tasklist.remove(tasklist.size()-1); + printStatement(INDENTATION + "☹ OOPS!!! There are time entered wrongly in the text file.\n" + + INDENTATION + "Please delete them.\n"); + } if(encodedTask.contains("\u2713")) { tasklist.get(tasklist.size()-1).markAsDone(); } } else if(encodedTask.contains("E")) { - tasklist.add(new Event(getDescriptiongFromFile(encodedTask), - encodedTask.substring(encodedTask.indexOf("at")+3, - encodedTask.lastIndexOf(")")).trim())); + int year, dayOfMonth, hour, minute; + Month month; + try { + year =Integer.parseInt(encodedTask.substring(encodedTask.indexOf("Year") + 5, + encodedTask.indexOf("Month") - 2)); + month = Month.valueOf(encodedTask.substring(encodedTask.indexOf("Month") + 6, + encodedTask.indexOf("Day") - 2)); + dayOfMonth = Integer.parseInt(encodedTask.substring(encodedTask.indexOf("Day") + 4, + encodedTask.indexOf("Time") - 2)); + hour = Integer.parseInt(encodedTask.substring(encodedTask.lastIndexOf("h") + 2, + encodedTask.lastIndexOf("m") - 2)); + minute = Integer.parseInt(encodedTask.substring(encodedTask.lastIndexOf("m") + 2, + encodedTask.indexOf(")"))); + tasklist.add(new Event(getDescriptiongFromFile(encodedTask), + LocalDateTime.of(year, month, dayOfMonth, hour, minute))); + } catch (DateTimeParseException e) { + tasklist.remove(tasklist.size()-1); + printStatement(INDENTATION + "☹ OOPS!!! There are time entered wrongly in the text file.\n" + + INDENTATION + "Please delete them.\n"); + } if(encodedTask.contains("\u2713")) { tasklist.get(tasklist.size()-1).markAsDone(); } diff --git a/src/main/java/Duke/task/Deadline.java b/src/main/java/Duke/task/Deadline.java index 405a6655b0..43d99b3cd7 100644 --- a/src/main/java/Duke/task/Deadline.java +++ b/src/main/java/Duke/task/Deadline.java @@ -1,16 +1,19 @@ package Duke.task; +import java.time.LocalDateTime; + public class Deadline extends Task { - protected String by; + protected LocalDateTime by; - public Deadline(String description, String by) { + public Deadline(String description, LocalDateTime by) { super(description); this.by = by; } @Override public String toString() { - return "[D]" + super.toString() + " (by: " + by + ")"; + return "[D]" + super.toString() + " (by: Year." + by.getYear() +", Month." + by.getMonth() + + ", Day." + by.getDayOfMonth() +", Time: h." + by.getHour() +", m." + by.getMinute() + ")"; } } \ No newline at end of file diff --git a/src/main/java/Duke/task/Event.java b/src/main/java/Duke/task/Event.java index 7c9f80dc24..ccd59105ac 100644 --- a/src/main/java/Duke/task/Event.java +++ b/src/main/java/Duke/task/Event.java @@ -1,16 +1,19 @@ package Duke.task; +import java.time.LocalDateTime; + public class Event extends Task { - protected String at; + protected LocalDateTime at; - public Event(String description, String at){ + public Event(String description, LocalDateTime at){ super(description); this.at = at; } @Override public String toString() { - return "[E]" + super.toString() + " (at: " + at + ")"; + return "[E]" + super.toString() + " (at: Year." + at.getYear() +", Month." + at.getMonth() + + ", Day." + at.getDayOfMonth() +", Time: h." + at.getHour() +", m." + at.getMinute() + ")"; } } diff --git a/src/main/java/Duke/ui/UI.java b/src/main/java/Duke/ui/UI.java index 603af41cda..1f2fa32194 100644 --- a/src/main/java/Duke/ui/UI.java +++ b/src/main/java/Duke/ui/UI.java @@ -18,7 +18,7 @@ public class UI { + TRIPLEINDENTATION + "| |_| | |_| | < __/\n" + TRIPLEINDENTATION + "|____/ \\__,_|_|\\_\\___|\n"; public final String GREET = INDENTATION + " Hello! I'm Duke\n" - + INDENTATION + " What can I do for you?\n"; + + INDENTATION + " What can I do for you? (type\"help\" to get explanation)\n"; public final String BYE = INDENTATION + " Bye. Hope to see you again soon!\n"; public final String WRONGMESSAGE = "☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n"; From 08b6d71ffc191430f86fbe87606e0484de88ebe2 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Fri, 2 Oct 2020 20:14:49 +0800 Subject: [PATCH 22/24] Level-8(v2.0): Dates and Times displayed the time of deadlines and events in a different way from the way user entered them. --- data/duke.txt | 3 +- src/main/java/Duke/Duke.java | 5 ---- src/main/java/Duke/command/FindCommand.java | 27 ----------------- src/main/java/Duke/command/HelpCommand.java | 32 --------------------- src/main/java/Duke/parser/Parser.java | 8 ------ 5 files changed, 2 insertions(+), 73 deletions(-) delete mode 100644 src/main/java/Duke/command/FindCommand.java delete mode 100644 src/main/java/Duke/command/HelpCommand.java diff --git a/data/duke.txt b/data/duke.txt index 9c12016843..069fbaac5c 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,3 +1,4 @@ [T][✘] task1 [D][✘] d2 (by: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30) -[E][✘] e1 (at: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30) +[D][✘] e1 (by: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30) +[T][✘] task4 diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java index e725c52048..467fe1c751 100644 --- a/src/main/java/Duke/Duke.java +++ b/src/main/java/Duke/Duke.java @@ -49,13 +49,8 @@ public void run() { } else if(fullCommand.contains("event")) { c = Parser.parseEventCommand(fullCommand, numberOfTasks); numberOfTasks++; - } else if(fullCommand.contains("help")) { - c = Parser.parseHelpCommand(numberOfTasks); } else if(fullCommand.contains("list")){ c = Parser.parseListCommand(numberOfTasks); - } else if(fullCommand.contains("find")) { - c = Parser.parseFindCommand(fullCommand.substring(fullCommand.indexOf("find")+4).trim(), - numberOfTasks); } else if(fullCommand.contains("done")) { c = Parser.parseDoneCommand(fullCommand, numberOfTasks); } else if(fullCommand.contains("delete")) { diff --git a/src/main/java/Duke/command/FindCommand.java b/src/main/java/Duke/command/FindCommand.java deleted file mode 100644 index c9c0cd34a7..0000000000 --- a/src/main/java/Duke/command/FindCommand.java +++ /dev/null @@ -1,27 +0,0 @@ -package Duke.command; - -import Duke.storage.Storage; -import Duke.taskList.TaskList; -import Duke.ui.UI; - -import static Duke.ui.UI.*; - -public class FindCommand extends Command { - public FindCommand(String description, int numberOfTasks) { - super(description, numberOfTasks); - } - - @Override - public void execute(TaskList tasks, UI ui, Storage storage) { - System.out.print(HORIZONTALLINE); - System.out.println(INDENTATION + "Here is yuqiaoluolong's Duke: \n" + - " Here are the matching tasks in your list:"); - for(int i = 0; i < numberOfTasks; i++){ - if(tasks.get(i).toString().contains(description)) { - System.out.println(DOUBLEINDENTATION + (i + 1) + "." + tasks.get(i).toString()); - } - } - System.out.println(HORIZONTALLINE); - } -} - diff --git a/src/main/java/Duke/command/HelpCommand.java b/src/main/java/Duke/command/HelpCommand.java deleted file mode 100644 index faf1d497f7..0000000000 --- a/src/main/java/Duke/command/HelpCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package Duke.command; - -import Duke.storage.Storage; -import Duke.taskList.TaskList; -import Duke.ui.UI; - -import static Duke.ui.UI.DOUBLEINDENTATION; -import static Duke.ui.UI.TRIPLEINDENTATION; - -public class HelpCommand extends Command { - public HelpCommand(String description, int numberOfTasks) { - super(description, numberOfTasks); - } - - @Override - public void execute(TaskList tasks, UI ui, Storage storage) { - UI.printStatement(DOUBLEINDENTATION + "Hello, below is the explanation of the functions:\n" - + TRIPLEINDENTATION + "1. you can add todo tasks follwed by the description\n" - + TRIPLEINDENTATION + "example: todo task1 --> [T][✘] task1\n" - + TRIPLEINDENTATION + "2. you can add deadlines followed by the description and the due time\n" - + TRIPLEINDENTATION + "example: deadline d2 /by 2015-02-20T06:30:00 --> " + - "[D][✘] d2 (by: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30)\n" - + TRIPLEINDENTATION + "3. you can add events followed by the description and the happening time\n" - + TRIPLEINDENTATION + "example: event e2 /by 2015-02-20T06:30:00 --> " + - "[E][✘] e2 (by: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30)\n" - + TRIPLEINDENTATION + "4. you can view all the tasks by typing \" list\" \n" - + TRIPLEINDENTATION + "5. you can search for the tasks containing some words\n" - + TRIPLEINDENTATION + "example: find book --> Duke will show all the tasks related with book\n" - + TRIPLEINDENTATION + "6. you can mark one task as done by byping \"done\" followed its number\n" - + TRIPLEINDENTATION + "7. you can delete one task as done by byping \"delete\" followed its number\n"); - } -} diff --git a/src/main/java/Duke/parser/Parser.java b/src/main/java/Duke/parser/Parser.java index 7545882f24..cef13afa07 100644 --- a/src/main/java/Duke/parser/Parser.java +++ b/src/main/java/Duke/parser/Parser.java @@ -72,12 +72,4 @@ public static Command parseDeleteCommand(String fullCommand, int numberOfTasks) public static Command parseExceptionCommand(int numberOfTasks) { return new ExceptionCommand("exception", numberOfTasks); } - - public static Command parseFindCommand(String index, int numberOfTasks) { - return new FindCommand(index, numberOfTasks); - } - - public static Command parseHelpCommand(int numberOfTasks) { - return new HelpCommand("help", numberOfTasks); - } } From 286a236f3923a775eb3313c61bcbea9dad6bc4a0 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Fri, 2 Oct 2020 20:18:15 +0800 Subject: [PATCH 23/24] Level-9: Find added a function for users to search for tasks with certain key words. --- src/main/java/Duke/Duke.java | 5 ++++ src/main/java/Duke/command/FindCommand.java | 26 +++++++++++++++++ src/main/java/Duke/command/HelpCommand.java | 32 +++++++++++++++++++++ src/main/java/Duke/parser/Parser.java | 8 ++++++ 4 files changed, 71 insertions(+) create mode 100644 src/main/java/Duke/command/FindCommand.java create mode 100644 src/main/java/Duke/command/HelpCommand.java diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java index 9083c29fec..b4f73700f9 100644 --- a/src/main/java/Duke/Duke.java +++ b/src/main/java/Duke/Duke.java @@ -45,8 +45,13 @@ public void run() { } else if(fullCommand.contains("event")) { c = Parser.parseEventCommand(fullCommand, numberOfTasks); numberOfTasks++; + } else if(fullCommand.contains("help")) { + c = Parser.parseHelpCommand(numberOfTasks); } else if(fullCommand.contains("list")){ c = Parser.parseListCommand(numberOfTasks); + } else if(fullCommand.contains("find")) { + c = Parser.parseFindCommand(fullCommand.substring(fullCommand.indexOf("find")+4).trim(), + numberOfTasks); } else if(fullCommand.contains("done")) { c = Parser.parseDoneCommand(fullCommand, numberOfTasks); } else if(fullCommand.contains("delete")) { diff --git a/src/main/java/Duke/command/FindCommand.java b/src/main/java/Duke/command/FindCommand.java new file mode 100644 index 0000000000..f997a92328 --- /dev/null +++ b/src/main/java/Duke/command/FindCommand.java @@ -0,0 +1,26 @@ +package Duke.command; + +import Duke.storage.Storage; +import Duke.taskList.TaskList; +import Duke.ui.UI; + +import static Duke.ui.UI.*; + +public class FindCommand extends Command { + public FindCommand(String description, int numberOfTasks) { + super(description, numberOfTasks); + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + System.out.print(HORIZONTALLINE); + System.out.println(INDENTATION + "Here is yuqiaoluolong's Duke: \n" + + " Here are the matching tasks in your list:"); + for(int i = 0; i < numberOfTasks; i++){ + if(tasks.get(i).toString().contains(description)) { + System.out.println(DOUBLEINDENTATION + (i + 1) + "." + tasks.get(i).toString()); + } + } + System.out.println(HORIZONTALLINE); + } +} \ No newline at end of file diff --git a/src/main/java/Duke/command/HelpCommand.java b/src/main/java/Duke/command/HelpCommand.java new file mode 100644 index 0000000000..4976bfbd18 --- /dev/null +++ b/src/main/java/Duke/command/HelpCommand.java @@ -0,0 +1,32 @@ +package Duke.command; + +import Duke.storage.Storage; +import Duke.taskList.TaskList; +import Duke.ui.UI; + +import static Duke.ui.UI.DOUBLEINDENTATION; +import static Duke.ui.UI.TRIPLEINDENTATION; + +public class HelpCommand extends Command { + public HelpCommand(String description, int numberOfTasks) { + super(description, numberOfTasks); + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + UI.printStatement(DOUBLEINDENTATION + "Hello, below is the explanation of the functions:\n" + + TRIPLEINDENTATION + "1. you can add todo tasks follwed by the description\n" + + TRIPLEINDENTATION + "example: todo task1 --> [T][✘] task1\n" + + TRIPLEINDENTATION + "2. you can add deadlines followed by the description and the due time\n" + + TRIPLEINDENTATION + "example: deadline d2 /by 2015-02-20T06:30:00 --> " + + "[D][✘] d2 (by: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30)\n" + + TRIPLEINDENTATION + "3. you can add events followed by the description and the happening time\n" + + TRIPLEINDENTATION + "example: event e2 /by 2015-02-20T06:30:00 --> " + + "[E][✘] e2 (by: Year.2015, Month.FEBRUARY, Day.20, Time: h.6, m.30)\n" + + TRIPLEINDENTATION + "4. you can view all the tasks by typing \" list\" \n" + + TRIPLEINDENTATION + "5. you can search for the tasks containing some words\n" + + TRIPLEINDENTATION + "example: find book --> Duke will show all the tasks related with book\n" + + TRIPLEINDENTATION + "6. you can mark one task as done by byping \"done\" followed its number\n" + + TRIPLEINDENTATION + "7. you can delete one task as done by byping \"delete\" followed its number\n"); + } +} \ No newline at end of file diff --git a/src/main/java/Duke/parser/Parser.java b/src/main/java/Duke/parser/Parser.java index cef13afa07..7545882f24 100644 --- a/src/main/java/Duke/parser/Parser.java +++ b/src/main/java/Duke/parser/Parser.java @@ -72,4 +72,12 @@ public static Command parseDeleteCommand(String fullCommand, int numberOfTasks) public static Command parseExceptionCommand(int numberOfTasks) { return new ExceptionCommand("exception", numberOfTasks); } + + public static Command parseFindCommand(String index, int numberOfTasks) { + return new FindCommand(index, numberOfTasks); + } + + public static Command parseHelpCommand(int numberOfTasks) { + return new HelpCommand("help", numberOfTasks); + } } From d805b90e6e0889c56d1513abd7326c8769719a85 Mon Sep 17 00:00:00 2001 From: yuqiaoluolong Date: Fri, 2 Oct 2020 21:16:35 +0800 Subject: [PATCH 24/24] A-JavaDoc --- .../InvalidStorageFilePathException.java | 3 + .../Duke/DukeException/TodoNullException.java | 3 + src/main/java/Duke/command/Command.java | 23 +++++- .../java/Duke/command/DeadlineCommand.java | 13 +++ src/main/java/Duke/command/DeleteCommand.java | 15 ++++ src/main/java/Duke/command/DoneCommand.java | 15 ++++ src/main/java/Duke/command/EventCommand.java | 13 +++ .../java/Duke/command/ExceptionCommand.java | 8 ++ src/main/java/Duke/command/ExitCommand.java | 7 ++ src/main/java/Duke/command/ListCommand.java | 7 ++ src/main/java/Duke/command/NullCommand.java | 16 ---- src/main/java/Duke/command/TodoCommand.java | 13 +++ src/main/java/Duke/parser/Parser.java | 79 +++++++++++++++---- src/main/java/Duke/storage/Storage.java | 43 +++++++++- src/main/java/Duke/task/Deadline.java | 7 ++ src/main/java/Duke/task/Event.java | 7 ++ src/main/java/Duke/task/Task.java | 9 +++ src/main/java/Duke/task/Todo.java | 7 ++ src/main/java/Duke/taskList/TaskList.java | 18 ++++- src/main/java/Duke/ui/UI.java | 20 +++++ 20 files changed, 285 insertions(+), 41 deletions(-) delete mode 100644 src/main/java/Duke/command/NullCommand.java diff --git a/src/main/java/Duke/DukeException/InvalidStorageFilePathException.java b/src/main/java/Duke/DukeException/InvalidStorageFilePathException.java index d98b853e10..71781ccb83 100644 --- a/src/main/java/Duke/DukeException/InvalidStorageFilePathException.java +++ b/src/main/java/Duke/DukeException/InvalidStorageFilePathException.java @@ -1,5 +1,8 @@ package Duke.DukeException; +/** + * build InvalidStorageNullException extending Exception. + */ public class InvalidStorageFilePathException extends Exception { public InvalidStorageFilePathException(String s) { } diff --git a/src/main/java/Duke/DukeException/TodoNullException.java b/src/main/java/Duke/DukeException/TodoNullException.java index 9108c2d8c3..f9bb43a8a2 100644 --- a/src/main/java/Duke/DukeException/TodoNullException.java +++ b/src/main/java/Duke/DukeException/TodoNullException.java @@ -1,5 +1,8 @@ package Duke.DukeException; +/** + * build TodoNullException extending Exception. + */ public class TodoNullException extends Exception { //no other code needed } diff --git a/src/main/java/Duke/command/Command.java b/src/main/java/Duke/command/Command.java index 44bec017cd..fc7d0d809d 100644 --- a/src/main/java/Duke/command/Command.java +++ b/src/main/java/Duke/command/Command.java @@ -5,6 +5,10 @@ import Duke.taskList.TaskList; import Duke.ui.UI; +/** + * Represents a Command with description and numberOfTasks. + * Is it an abstracted type. + */ public class Command { protected String description; protected int numberOfTasks = 0; @@ -14,14 +18,29 @@ public Command(String description, int numberOfTasks) { this.numberOfTasks = numberOfTasks; } + /** + * Execute the Command. + */ public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException { numberOfTasks++; } + /** + * Execute the save function. + * Save the changed taskList into the file + */ public void sava(TaskList tasks, UI ui, Storage storage){ } + /** + * Return a boolean variable indicating whether the command + * is an ExitCommand. + * If it is, return true. Else wise, return false. + * + * @return Boolean variable indicating whether the command + * is an ExitCommand. + */ public boolean isExit() { if(description == "bye") { return true; @@ -30,8 +49,4 @@ public boolean isExit() { return false; } } - - public Command change() { - return null; - } } diff --git a/src/main/java/Duke/command/DeadlineCommand.java b/src/main/java/Duke/command/DeadlineCommand.java index 1afae343cd..ce32eb6a0e 100644 --- a/src/main/java/Duke/command/DeadlineCommand.java +++ b/src/main/java/Duke/command/DeadlineCommand.java @@ -9,6 +9,11 @@ import static Duke.ui.UI.DOUBLEINDENTATION; import static Duke.ui.UI.TRIPLEINDENTATION; +/** + * Represents a DeadlineCommand with description and numberOfTasks. + * A deadline object corresponds to a TodoCommand + * with the description and due time extracted from what entered by users. + */ public class DeadlineCommand extends Command { protected String by; @@ -17,6 +22,10 @@ public DeadlineCommand(String description, int numberOfTasks, String by) { this.by = by; } + /** + * Override: Execute the Command. + * add a new Deadline into the taskList. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException { super.execute(tasks, ui, storage); @@ -26,6 +35,10 @@ public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullExcep + "Now you have " + (numberOfTasks) + " tasks in the list.\n"); } + /** + * Execute the save function. + * Save the changed taskList into the file + */ @Override public void sava(TaskList tasks, UI ui, Storage storage){ Storage.save(tasks, numberOfTasks); diff --git a/src/main/java/Duke/command/DeleteCommand.java b/src/main/java/Duke/command/DeleteCommand.java index 840b7bd2d6..92e25a14e6 100644 --- a/src/main/java/Duke/command/DeleteCommand.java +++ b/src/main/java/Duke/command/DeleteCommand.java @@ -6,11 +6,22 @@ import static Duke.ui.UI.*; +/** + * Represents a DeleteCommand with description and numberOfTasks. + * A delete object corresponds to a DeleteCommand + * with the description extracted from what entered by users, + * which is the index of the task users intend to delete. + */ public class DeleteCommand extends Command { public DeleteCommand(String description, int numberOfTasks) { super(description, numberOfTasks); } + /** + * Override: Execute the Command. + * Delete the task with entered index. + * Print out the delete message. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage){ printStatement(DOUBLEINDENTATION + "Noted. I've removed this task: \n" + @@ -19,6 +30,10 @@ public void execute(TaskList tasks, UI ui, Storage storage){ tasks.remove(Integer.parseInt(this.description)-1); } + /** + * Execute the save function. + * Save the changed taskList into the file + */ @Override public void sava(TaskList tasks, UI ui, Storage storage){ Storage.save(tasks, numberOfTasks-1); diff --git a/src/main/java/Duke/command/DoneCommand.java b/src/main/java/Duke/command/DoneCommand.java index 1ffc9ed5cf..191754793e 100644 --- a/src/main/java/Duke/command/DoneCommand.java +++ b/src/main/java/Duke/command/DoneCommand.java @@ -6,11 +6,22 @@ import static Duke.ui.UI.*; +/** + * Represents a DoneCommand with description and numberOfTasks. + * A done object corresponds to a DoneCommand + * with the description extracted from what entered by users, + * which is the index of the task users intend to mark as done. + */ public class DoneCommand extends Command { public DoneCommand(String description, int numberOfTasks) { super(description, numberOfTasks); } + /** + * Override: Execute the Command. + * Mark the task with the entered index as done. + * Print out the done message. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage){ try { @@ -24,6 +35,10 @@ public void execute(TaskList tasks, UI ui, Storage storage){ } } + /** + * Execute the save function. + * Save the changed taskList into the file + */ @Override public void sava(TaskList tasks, UI ui, Storage storage){ Storage.save(tasks, numberOfTasks); diff --git a/src/main/java/Duke/command/EventCommand.java b/src/main/java/Duke/command/EventCommand.java index f33e5ec7b4..3ef7f71b94 100644 --- a/src/main/java/Duke/command/EventCommand.java +++ b/src/main/java/Duke/command/EventCommand.java @@ -9,6 +9,11 @@ import static Duke.ui.UI.DOUBLEINDENTATION; import static Duke.ui.UI.TRIPLEINDENTATION; +/** + * Represents an EventCommand with description and numberOfTasks. + * A event object corresponds to a TodoCommand + * with the description and happening time extracted from what entered by users. + */ public class EventCommand extends Command { protected String at; @@ -17,6 +22,10 @@ public EventCommand(String description, int numberOfTasks, String at) { this.at = at; } + /** + * Override: Execute the Command. + * add a new Event into the taskList. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException { super.execute(tasks, ui, storage); @@ -26,6 +35,10 @@ public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullExcep + "Now you have " + (numberOfTasks) + " tasks in the list.\n"); } + /** + * Execute the save function. + * Save the changed taskList into the file + */ @Override public void sava(TaskList tasks, UI ui, Storage storage){ Storage.save(tasks, numberOfTasks); diff --git a/src/main/java/Duke/command/ExceptionCommand.java b/src/main/java/Duke/command/ExceptionCommand.java index eab40e1b21..3d7fb7f58b 100644 --- a/src/main/java/Duke/command/ExceptionCommand.java +++ b/src/main/java/Duke/command/ExceptionCommand.java @@ -7,11 +7,19 @@ import static Duke.ui.UI.DOUBLEINDENTATION; import static Duke.ui.UI.printStatement; +/** + * Represents an ExceptionCommand with description and numberOfTasks. + * It indicates that there are no corresponding functions in this Duke. + */ public class ExceptionCommand extends Command { public ExceptionCommand(String description, int numberOfTasks) { super(description, numberOfTasks); } + /** + * Override: Execute the Command. + * Print out the wrong message. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { printStatement(DOUBLEINDENTATION + ui.WRONGMESSAGE); diff --git a/src/main/java/Duke/command/ExitCommand.java b/src/main/java/Duke/command/ExitCommand.java index 80e083ac98..6e15639769 100644 --- a/src/main/java/Duke/command/ExitCommand.java +++ b/src/main/java/Duke/command/ExitCommand.java @@ -4,11 +4,18 @@ import Duke.taskList.TaskList; import Duke.ui.UI; +/** + * Represents an ExitCommand with description and numberOfTasks. + */ public class ExitCommand extends Command { public ExitCommand(String description, int numberOfTasks) { super(description, numberOfTasks); } + /** + * Override: Execute the Command. + * Print out the Exit message and exit. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { ui.printStatement(ui.BYE); diff --git a/src/main/java/Duke/command/ListCommand.java b/src/main/java/Duke/command/ListCommand.java index 574f3b5b3a..55356ad6a2 100644 --- a/src/main/java/Duke/command/ListCommand.java +++ b/src/main/java/Duke/command/ListCommand.java @@ -6,11 +6,18 @@ import static Duke.ui.UI.*; +/** + * Represents a ListCommand with description and numberOfTasks. + */ public class ListCommand extends Command { public ListCommand(String description, int numberOfTasks) { super(description, numberOfTasks); } + /** + * Override: Execute the Command. + * Print out all the tasks in the taskList. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { System.out.print(HORIZONTALLINE); diff --git a/src/main/java/Duke/command/NullCommand.java b/src/main/java/Duke/command/NullCommand.java deleted file mode 100644 index 942b68db25..0000000000 --- a/src/main/java/Duke/command/NullCommand.java +++ /dev/null @@ -1,16 +0,0 @@ -package Duke.command; - -import Duke.storage.Storage; -import Duke.taskList.TaskList; -import Duke.ui.UI; - -public class NullCommand extends Command { - public NullCommand(String description, int numberOfTasks) { - super(description, numberOfTasks); - } - - @Override - public void execute(TaskList tasks, UI ui, Storage storage) { - - } -} diff --git a/src/main/java/Duke/command/TodoCommand.java b/src/main/java/Duke/command/TodoCommand.java index 0f39ff52a7..444bb63381 100644 --- a/src/main/java/Duke/command/TodoCommand.java +++ b/src/main/java/Duke/command/TodoCommand.java @@ -9,12 +9,21 @@ import static Duke.ui.UI.DOUBLEINDENTATION; import static Duke.ui.UI.TRIPLEINDENTATION; +/** + * Represents a TodoCommand with description and numberOfTasks. + * A todo object corresponds to a TodoCommand + * with the description extracted from what entered by users. + */ public class TodoCommand extends Command { public TodoCommand(String description, int numberOfTasks) { super(description, numberOfTasks); } + /** + * Override: Execute the Command. + * add a new Todo into the taskList. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullException{ super.execute(tasks, ui, storage); @@ -27,6 +36,10 @@ public void execute(TaskList tasks, UI ui, Storage storage) throws TodoNullExcep + "Now you have " + (numberOfTasks) + " tasks in the list.\n"); } + /** + * Execute the save function. + * Save the changed taskList into the file + */ @Override public void sava(TaskList tasks, UI ui, Storage storage){ Storage.save(tasks, numberOfTasks); diff --git a/src/main/java/Duke/parser/Parser.java b/src/main/java/Duke/parser/Parser.java index cef13afa07..ac05e23125 100644 --- a/src/main/java/Duke/parser/Parser.java +++ b/src/main/java/Duke/parser/Parser.java @@ -5,24 +5,20 @@ import static Duke.ui.UI.DOUBLEINDENTATION; import static Duke.ui.UI.printStatement; +/** + * parse the command entered into different format + * for methods to take in + */ public class Parser { - /*public static Command parse(String fullCommand) { - if(fullCommand.contains("todo")){ - return new Command(fullCommand.substring(fullCommand.indexOf("Todo")+4).trim()); - } else if(fullCommand.contains("event")) { - return new Command(fullCommand.substring(fullCommand.indexOf("Event")+5, - fullCommand.indexOf("/")).trim()); - } else if(fullCommand.contains("deadline")) { - return new Command(fullCommand.substring(fullCommand.indexOf("Deadline")+7, - fullCommand.indexOf("/")).trim()); - } else if(fullCommand.contains("bye")) { - return new Command(("bye")); - } else { - return null; - } - }*/ - + /** + * Returns TodolineCommand with the description + * extracted from the fullCommand + * + * @param fullCommand Command entered by users. + * @param numberOfTasks Number of tasks. + * @return TodolineCommand with the description. + */ public static TodoCommand parseTodoCommand(String fullCommand, int numberOfTasks) { String description = fullCommand.substring(fullCommand.indexOf("todo")+4).trim(); if(description =="") { @@ -31,6 +27,14 @@ public static TodoCommand parseTodoCommand(String fullCommand, int numberOfTasks return new TodoCommand(description, numberOfTasks); } + /** + * Returns DeadlineCommand with the description and due time + * extracted from the fullCommand + * + * @param fullCommand Command entered by users. + * @param numberOfTasks Number of tasks. + * @return DeadlineCommand with the description and due time. + */ public static DeadlineCommand parseDeadlineCommand(String fullCommand, int numberOfTasks) { try { return new DeadlineCommand(fullCommand.substring(fullCommand.indexOf("deadline") + 8, @@ -42,6 +46,14 @@ public static DeadlineCommand parseDeadlineCommand(String fullCommand, int numbe return null; } + /** + * Returns EventCommand with the description and happening time + * extracted from the fullCommand + * + * @param fullCommand Command entered by users. + * @param numberOfTasks Number of tasks. + * @return EventCommand with the description and happening time. + */ public static EventCommand parseEventCommand(String fullCommand, int numberOfTasks) { try { return new EventCommand(fullCommand.substring(fullCommand.indexOf("event")+5, @@ -53,22 +65,55 @@ public static EventCommand parseEventCommand(String fullCommand, int numberOfTas return null; } + /** + * Returns ListCommand with the description of "list" + * + * @param numberOfTasks Number of tasks. + * @return ListCommand with the description of "list". + */ public static Command parseListCommand(int numberOfTasks) { return new ListCommand("list", numberOfTasks); } - + /** + * Returns ListCommand with the description of "bye" + * + * @param numberOfTasks Number of tasks. + * @return ListCommand with the description of "bye". + */ public static Command parseExitCommand(int numberOfTasks) { return new ExitCommand("bye", numberOfTasks); } + /** + * Returns DoneCommand with the description which is the index of tasks to change to done + * and extracted from the fullCommand. + * + * @param fullCommand Command entered by users + * @param numberOfTasks Number of tasks. + * @return DoneCommand with the description of the index of tasks to change to done. + */ public static Command parseDoneCommand(String fullCommand, int numberOfTasks) { return new DoneCommand(fullCommand.replace("done", " ").trim(), numberOfTasks); } + /** + * Returns DeleteCommand with the description which is the index of tasks to delete + * and extracted from the fullCommand. + * + * @param fullCommand Command entered by users + * @param numberOfTasks Number of tasks. + * @return DeleteCommand with the description that is the index of tasks to delete. + */ public static Command parseDeleteCommand(String fullCommand, int numberOfTasks) { return new DeleteCommand(fullCommand.replace("delete", " ").trim(), numberOfTasks); } + /** + * Returns Exception Command with the description "exception" + * + * @param numberOfTasks Number of tasks. + * @return Exception Command with the description "exception" + */ public static Command parseExceptionCommand(int numberOfTasks) { return new ExceptionCommand("exception", numberOfTasks); } diff --git a/src/main/java/Duke/storage/Storage.java b/src/main/java/Duke/storage/Storage.java index 179e9a5ff7..50bc6a4bc1 100644 --- a/src/main/java/Duke/storage/Storage.java +++ b/src/main/java/Duke/storage/Storage.java @@ -14,6 +14,9 @@ import java.nio.file.Paths; import java.util.ArrayList; +/** + * Represents a class saving the tasklist to a file and load the content from it. + */ public class Storage { public static final String DEFAULT_STORAGE_FILEPATH = "/Users/yuqiao/Desktop/CS2113T/ip/data/duke.txt"; public static Path path = Path.of(DEFAULT_STORAGE_FILEPATH); @@ -29,10 +32,22 @@ public Storage(String filePath) /*throws InvalidStorageFilePathException*/ { } } + /** + * Returns a boolean variable indicating whethe the path is valid or not + * + * @param filePath Path of the file to be tested. + * @return the validity of the filePath. + */ private static boolean isValidPath(Path filePath) { return filePath.toString().endsWith(".txt"); } + /** + * save the changed task list into the file + * + * @param tasks Task list to save + * @param numberOfTasks Number of the tasks in the task list. + */ public static void save(TaskList tasks, int numberOfTasks) { String text = ""; for(int i = 0; i + * + * @return Task list containing tasks inside the + * file of the type ArrayList + */ public ArrayList load(){ ArrayList tasklist = new ArrayList<>(); try { @@ -83,6 +111,13 @@ public ArrayList load(){ return null; } + /** + * Returns the description of a task as a String variable. + * extracted from the inputCommand + * + * @param inputCommand Command entered by users. + * @return Description of a task as a String variable. + */ public static String getDescriptiongFromFile(String inputCommand) { if(inputCommand.contains(":")){ return inputCommand.substring(inputCommand.lastIndexOf("]") + 1, inputCommand.indexOf((":"))-4).trim(); @@ -91,6 +126,11 @@ public static String getDescriptiongFromFile(String inputCommand) { } } + /** + * Returns the number of tasks inside the file when loading + * + * @return Number of tasks inside the file. + */ public int loadNumberOfTasks(){ int number = 0; try { @@ -103,8 +143,5 @@ public int loadNumberOfTasks(){ } return -1; } - public String getPath() { - return path.toString(); - } } diff --git a/src/main/java/Duke/task/Deadline.java b/src/main/java/Duke/task/Deadline.java index 405a6655b0..33469d5be3 100644 --- a/src/main/java/Duke/task/Deadline.java +++ b/src/main/java/Duke/task/Deadline.java @@ -1,5 +1,9 @@ package Duke.task; +/** + * Represents a Deadline Task. Adeadline corresponds to a task + * described by description ,isDone, and by. + */ public class Deadline extends Task { protected String by; @@ -9,6 +13,9 @@ public Deadline(String description, String by) { this.by = by; } + /** + * Override: convert the task to a String in a certain format + */ @Override public String toString() { return "[D]" + super.toString() + " (by: " + by + ")"; diff --git a/src/main/java/Duke/task/Event.java b/src/main/java/Duke/task/Event.java index 7c9f80dc24..461e5b959a 100644 --- a/src/main/java/Duke/task/Event.java +++ b/src/main/java/Duke/task/Event.java @@ -1,5 +1,9 @@ package Duke.task; +/** + * Represents a Event Task. Aevent corresponds to a task + * described by description ,isDone, and at. + */ public class Event extends Task { protected String at; @@ -9,6 +13,9 @@ public Event(String description, String at){ this.at = at; } + /** + * Override: convert the task to a String in a certain format + */ @Override public String toString() { return "[E]" + super.toString() + " (at: " + at + ")"; diff --git a/src/main/java/Duke/task/Task.java b/src/main/java/Duke/task/Task.java index f2b2873173..2d06198c6a 100644 --- a/src/main/java/Duke/task/Task.java +++ b/src/main/java/Duke/task/Task.java @@ -1,5 +1,8 @@ package Duke.task; +/** + * Represents a Task. It is an abstract type + */ public class Task { public String description; public boolean isDone; @@ -9,6 +12,9 @@ public Task(String description) { this.isDone = false; } + /** + * mark the task as done. + */ public void markAsDone(){ this.isDone = true; } @@ -17,6 +23,9 @@ public String getStatusIcon() { return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols } + /** + * convert the task to a String in a certain format + */ public String toString() { return "[" + this.getStatusIcon() + "] " + this.description; } diff --git a/src/main/java/Duke/task/Todo.java b/src/main/java/Duke/task/Todo.java index 9a73c9aafc..ad910fd702 100644 --- a/src/main/java/Duke/task/Todo.java +++ b/src/main/java/Duke/task/Todo.java @@ -1,11 +1,18 @@ package Duke.task; +/** + * Represents a Todo Task. Atodo corresponds to a task + * described by description and isDone. + */ public class Todo extends Task { public Todo(String description){ super(description); } + /** + * Override: convert the task to a String in a certain format + */ @Override public String toString() { return "[T]" + super.toString(); diff --git a/src/main/java/Duke/taskList/TaskList.java b/src/main/java/Duke/taskList/TaskList.java index fa5cb17934..0b32a6baac 100644 --- a/src/main/java/Duke/taskList/TaskList.java +++ b/src/main/java/Duke/taskList/TaskList.java @@ -4,8 +4,10 @@ import java.util.ArrayList; +/** + * Represents an ArrayList of Task. + */ public class TaskList { - public static final int MAX_NUM_TASKS = 100; private static ArrayList taskList = new ArrayList<>(); public TaskList() { @@ -17,14 +19,28 @@ public TaskList(ArrayList tasks) { } } + /** + * add one element to the taskList + */ public void add(Task oneTask){ taskList.add(oneTask); } + /** + * get the element in the taskList with entered index + * + * @param index the index of the element in taskList that we want. + * @return the element of type Task + */ public Task get(int index){ return taskList.get(index); } + /** + * remove one element in the taskList with entered index + * + * @param i the index of the element in taskList that we want to remove. + */ public void remove(int i) { taskList.remove(i); } diff --git a/src/main/java/Duke/ui/UI.java b/src/main/java/Duke/ui/UI.java index 603af41cda..da22257dc8 100644 --- a/src/main/java/Duke/ui/UI.java +++ b/src/main/java/Duke/ui/UI.java @@ -4,6 +4,10 @@ import java.io.PrintStream; import java.util.Scanner; +/** + * Represents a ui + * Contains the common constant Strings and methods + */ public class UI { private static final String LS = System.lineSeparator(); public static final int MAX_NUM_TASKS = 100; @@ -33,21 +37,37 @@ public UI(InputStream in, PrintStream out) { this.out = out; } + /** + * print out the input String + * @param statement The String need to print. + */ public static void printStatement(String statement) { System.out.println(HORIZONTALLINE + INDENTATION + "Here is yuqiaoluolong's Duke: \n" + statement + HORIZONTALLINE); } + /** + * print out the Welcome message + */ public void showWelcome() { printStatement(TRIPLEINDENTATION + "Hello from\n" + LOGO); printStatement(GREET); } + /** + * Returns what is entered by users as a String variable. + * + * @return inputcommand. + */ + public String readCommand(){ String input = in.nextLine(); return input; } + /** + * Show a line. + */ public void showLine(){ System.out.printf(LS); }