From bcf20ee31dc2bc2797bf7bc90affa0beccd78d51 Mon Sep 17 00:00:00 2001 From: Edwin Date: Mon, 23 Aug 2021 00:23:58 +0800 Subject: [PATCH 01/28] Week 2 Task --- src/main/java/Duke.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..c8d154376 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -5,6 +5,10 @@ public static void main(String[] args) { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + String line = "____________________________________________________________\n"; + System.out.println(line + "Hello! I'm Duke"); + System.out.println("What can I do for you?\n" + line); + System.out.println("Bye. Hope to see you again soon!\n" + line); + // System.out.println("Hello from\n" + logo); } } From 2c2a310e365ab272ee2ae324d9da810c2280de0e Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 26 Aug 2021 20:11:31 +0800 Subject: [PATCH 02/28] Level-1 --- src/main/java/Circle.java | 2 ++ src/main/java/Duke.java | 21 ++++++++++++++++----- src/main/java/Main.java | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Circle.java create mode 100644 src/main/java/Main.java diff --git a/src/main/java/Circle.java b/src/main/java/Circle.java new file mode 100644 index 000000000..03dd9ec3c --- /dev/null +++ b/src/main/java/Circle.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class Circle { +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index c8d154376..b135f43d0 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,5 @@ +import java.util.Scanner; + public class Duke { public static void main(String[] args) { String logo = " ____ _ \n" @@ -5,10 +7,19 @@ public static void main(String[] args) { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - String line = "____________________________________________________________\n"; - System.out.println(line + "Hello! I'm Duke"); - System.out.println("What can I do for you?\n" + line); - System.out.println("Bye. Hope to see you again soon!\n" + line); + String border = "____________________________________________________________\n"; + System.out.println(border + "Hi bro, my name is Echo"); + System.out.println("What do you want?\n" + border); + System.out.println("Type bye to exit\n" + border); + String line; + do { + Scanner in = new Scanner(System.in); + line = in.nextLine(); + System.out.println(border + line + '\n' + border); + } + while(!line.matches("bye")); + System.out.println("chat again next time!\n" + border); + } + // System.out.println("Hello from\n" + logo); - } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 000000000..fb7039d9c --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class Main { +} From da7c42bad992f14640a26255749509044ef70449 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 26 Aug 2021 20:36:43 +0800 Subject: [PATCH 03/28] Level-2 --- src/main/java/Circle.java | 50 +++++++++++++++++++++++++++++++++++++-- src/main/java/Duke.java | 27 ++++++++++++++++----- src/main/java/Main.java | 49 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 116 insertions(+), 10 deletions(-) diff --git a/src/main/java/Circle.java b/src/main/java/Circle.java index 03dd9ec3c..e90a5f6ea 100644 --- a/src/main/java/Circle.java +++ b/src/main/java/Circle.java @@ -1,2 +1,48 @@ -package PACKAGE_NAME;public class Circle { -} +public class Circle { + private int x; + private int y; + private double radius; + private static double maxRadius = 0; + + public Circle(){ + this(0, 0, 0); + } + + public Circle(int x, int y, double radius){ + setX(x); + setY(y); + setRadius(radius); + } + + public int getX() { + return x; + } + public void setX(int x) { + this.x = x; + } + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + this.radius = Math.max(radius, 0); + if (maxRadius < this.radius){ + maxRadius = this.radius; + } + } + public static double getMaxRadius() { + return maxRadius; + } + public int getArea(){ + double area = Math.PI * Math.pow(radius, 2); + return (int)area; + } +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b135f43d0..1ffb72dd6 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -12,14 +12,29 @@ public static void main(String[] args) { System.out.println("What do you want?\n" + border); System.out.println("Type bye to exit\n" + border); String line; + String[] items = new String[100]; + int i = 0; do { Scanner in = new Scanner(System.in); line = in.nextLine(); - System.out.println(border + line + '\n' + border); + if (line.matches("list")) { + int j = 1; + System.out.println(border); + for (String item : items) { + if (item != null) { + System.out.println(j + ". " + item); + j++; + } + } + System.out.println(border); + } else if (!line.matches("bye")) { + System.out.println(border + "added: " + line + '\n' + border); + items[i] = line; + i++; + } } - while(!line.matches("bye")); - System.out.println("chat again next time!\n" + border); - } - - // System.out.println("Hello from\n" + logo); + while (!line.matches("bye")); + System.out.println(border); + System.out.println("chat again next time!\n" + border); + } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index fb7039d9c..2703bae4e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,2 +1,47 @@ -package PACKAGE_NAME;public class Main { -} + +import java.util.Arrays; +import java.util.Scanner; + +public class Main { + + // You can add more methods here + public static String[] filter(String[] items){ + String[] results = new String[items.length]; + int matchCount = 0; + for(String item: items){ + if (item.contains("$")){ + results[matchCount] = item; + matchCount++; + } + } + return Arrays.copyOf(results, matchCount); + } + + public static double totalExpenses(String[] prices) { + double total = 0; + for (String price : prices) { + total += Double.parseDouble(price.trim().replace("$"," ")); + } + return total; + } + public static double exchangeRate(double overseas) { + return overseas * 1.70; + } + public static String[] splitSentence(String sentence) { + return sentence.split(" "); + } + + public static void main(String[] args) { + String line; + Scanner in = new Scanner(System.in); + + System.out.print("Your expenses while overseas?"); + // TODO: add your code here + line = in.nextLine(); + String[] amounts = filter(splitSentence(line)); + System.out.print("Expenses in overseas currency:"); + System.out.println(Arrays.toString(amounts)); + System.out.print("Total in local currency: "); + System.out.println("$" + String.format("%.2f", exchangeRate(totalExpenses(amounts)))); + } +} \ No newline at end of file From 7e2f13a98b0b3975ab9c16beb8821f1e26585eb7 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 26 Aug 2021 22:01:06 +0800 Subject: [PATCH 04/28] Level-3 --- src/main/java/Duke.java | 23 ++++++++++++++++++----- src/main/java/Task.java | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 1ffb72dd6..09e38d950 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,6 +1,9 @@ import java.util.Scanner; public class Duke { + private static Task[] items = new Task[100]; + private static int i = 0; + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -12,24 +15,34 @@ public static void main(String[] args) { System.out.println("What do you want?\n" + border); System.out.println("Type bye to exit\n" + border); String line; - String[] items = new String[100]; - int i = 0; + do { Scanner in = new Scanner(System.in); line = in.nextLine(); if (line.matches("list")) { int j = 1; System.out.println(border); - for (String item : items) { + for (Task item : items) { if (item != null) { - System.out.println(j + ". " + item); + System.out.println(j + ".[" + item.getStatusIcon() + "] " + item.getDescription()); j++; } } System.out.println(border); + + } else if (line.contains("done")) { + int dividerPosition = line.indexOf(" ") + 1; + int endPosition = line.length(); + if (endPosition > 5) { + String num = line.substring(dividerPosition, endPosition); + int taskNum = Integer.parseInt(num) - 1; + items[taskNum].markDone(); + System.out.println(border + "Nice! task is done " + '\n' + border); + } } else if (!line.matches("bye")) { System.out.println(border + "added: " + line + '\n' + border); - items[i] = line; + Task newItem = new Task(line); + items[i] = newItem; i++; } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..8879bac2b --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,21 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + public String getDescription() { + return this.description; + } + + public void markDone() { + this.isDone = true; + } + //... +} From 0b9ce39f70c674a6c677625c8ab21bbe1626a7a3 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 26 Aug 2021 22:08:36 +0800 Subject: [PATCH 05/28] Completed A-CodingStandard --- src/main/java/Duke.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 09e38d950..40c6085f2 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,7 +2,7 @@ public class Duke { private static Task[] items = new Task[100]; - private static int i = 0; + private static int taskCount = 0; public static void main(String[] args) { String logo = " ____ _ \n" @@ -42,8 +42,8 @@ public static void main(String[] args) { } else if (!line.matches("bye")) { System.out.println(border + "added: " + line + '\n' + border); Task newItem = new Task(line); - items[i] = newItem; - i++; + items[taskCount] = newItem; + taskCount++; } } while (!line.matches("bye")); From 074899d24dba6b8c39f880535bc1748f0e2762c8 Mon Sep 17 00:00:00 2001 From: Edwin Date: Fri, 3 Sep 2021 13:26:51 +0800 Subject: [PATCH 06/28] Level-4 Completed --- .classpath | 6 +++ .project | 17 ++++++++ src/main/java/Circle.java | 48 ----------------------- src/main/java/Deadlines.java | 13 +++++++ src/main/java/Duke.java | 75 ++++++++++++++++++++++++++++-------- src/main/java/Events.java | 12 ++++++ src/main/java/Main.java | 47 ---------------------- src/main/java/Task.java | 4 +- src/main/java/Todo.java | 10 +++++ 9 files changed, 120 insertions(+), 112 deletions(-) create mode 100644 .classpath create mode 100644 .project delete mode 100644 src/main/java/Circle.java create mode 100644 src/main/java/Deadlines.java create mode 100644 src/main/java/Events.java delete mode 100644 src/main/java/Main.java create mode 100644 src/main/java/Todo.java diff --git a/.classpath b/.classpath new file mode 100644 index 000000000..b6cbb3c39 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 000000000..5bfb5bc84 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + ip + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/main/java/Circle.java b/src/main/java/Circle.java deleted file mode 100644 index e90a5f6ea..000000000 --- a/src/main/java/Circle.java +++ /dev/null @@ -1,48 +0,0 @@ -public class Circle { - private int x; - private int y; - private double radius; - private static double maxRadius = 0; - - public Circle(){ - this(0, 0, 0); - } - - public Circle(int x, int y, double radius){ - setX(x); - setY(y); - setRadius(radius); - } - - public int getX() { - return x; - } - public void setX(int x) { - this.x = x; - } - public int getY() { - return y; - } - - public void setY(int y) { - this.y = y; - } - - public double getRadius() { - return radius; - } - - public void setRadius(double radius) { - this.radius = Math.max(radius, 0); - if (maxRadius < this.radius){ - maxRadius = this.radius; - } - } - public static double getMaxRadius() { - return maxRadius; - } - public int getArea(){ - double area = Math.PI * Math.pow(radius, 2); - return (int)area; - } -} \ No newline at end of file diff --git a/src/main/java/Deadlines.java b/src/main/java/Deadlines.java new file mode 100644 index 000000000..eb48cb35d --- /dev/null +++ b/src/main/java/Deadlines.java @@ -0,0 +1,13 @@ +public class Deadlines extends Task { + protected String by; + + public Deadlines(String description, String by) { + super(description); + this.by = by; + } + + public String toString() { + return "[D]" + super.toString() + " (by: " + by + ")"; + } +} + diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 40c6085f2..cb4b348db 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,18 +3,52 @@ public class Duke { private static Task[] items = new Task[100]; private static int taskCount = 0; + private static String logo = " _____ ___ _____\n" + + "|___ | | ||_____| \n" + + " / / | | / / \n" + + " / / | | / / \n" + + " / /___ | | /_/__ \n" + + "|_____| | ||_____| \n"; + private static String border = "____________________________________________________________\n"; - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - String border = "____________________________________________________________\n"; + public static void addedTaskMessage(Task task) { + System.out.println("Got it. I've added this task:"); + System.out.println(task); + System.out.println("Now you have " + taskCount + " tasks in the list."); + } + public static void addEvent(String description, String time) { + Events newEvent = new Events(description, time); + items[taskCount] = newEvent; + taskCount++; + addedTaskMessage(newEvent); + } + public static void addDeadline(String description, String by) { + Deadlines newDeadline = new Deadlines(description, by); + items[taskCount] = newDeadline; + taskCount++; + addedTaskMessage(newDeadline); + } + public static void addTodo(String description) { + Todo newTodo = new Todo(description); + items[taskCount] = newTodo; + taskCount++; + addedTaskMessage(newTodo); + } + public static void addTask(String description) { + System.out.println(border + "added: " + description + '\n' + border); + Task newItem = new Task(description); + items[taskCount] = newItem; + taskCount++; + } + public static void printStartMessage() { + System.out.println(logo); System.out.println(border + "Hi bro, my name is Echo"); System.out.println("What do you want?\n" + border); System.out.println("Type bye to exit\n" + border); + } + public static void main(String[] args) { String line; + printStartMessage(); do { Scanner in = new Scanner(System.in); @@ -22,15 +56,18 @@ public static void main(String[] args) { if (line.matches("list")) { int j = 1; System.out.println(border); + System.out.println("Here is your list"); + for (Task item : items) { if (item != null) { - System.out.println(j + ".[" + item.getStatusIcon() + "] " + item.getDescription()); + System.out.print(j + "."); + System.out.println(item); j++; } } System.out.println(border); - } else if (line.contains("done")) { + } else if (line.length() > 4 && line.substring(0,4).contains("done")) { int dividerPosition = line.indexOf(" ") + 1; int endPosition = line.length(); if (endPosition > 5) { @@ -39,14 +76,20 @@ public static void main(String[] args) { items[taskNum].markDone(); System.out.println(border + "Nice! task is done " + '\n' + border); } - } else if (!line.matches("bye")) { - System.out.println(border + "added: " + line + '\n' + border); - Task newItem = new Task(line); - items[taskCount] = newItem; - taskCount++; + } else if (line.length() > 4 && line.substring(0,4).contains("todo")) { //improve condition to first word + addTodo(line.replace("todo","").trim()); + } else if (line.length() > 5 &&line.substring(0,5).contains("event")) { + String time = line.split("/at")[1].trim(); + String description = line.split("/at")[0].trim().replace("event","").trim(); + addEvent(description, time); + } else if (line.length() > 8 && line.substring(0,8).contains("deadline")) { + String by = line.split("/by")[1].trim(); + String description = line.split("/by")[0].trim().replace("deadline","").trim(); + addDeadline(description, by); + } else { + addTask(line); } - } - while (!line.matches("bye")); + } while (!line.matches("bye")); System.out.println(border); System.out.println("chat again next time!\n" + border); } diff --git a/src/main/java/Events.java b/src/main/java/Events.java new file mode 100644 index 000000000..c275953ed --- /dev/null +++ b/src/main/java/Events.java @@ -0,0 +1,12 @@ +public class Events extends Task { + protected String time; + + public Events(String description, String time) { + super(description); + this.time = time; + } + + public String toString() { + return "[E]" + super.toString() + " (at: " + time + ")"; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java deleted file mode 100644 index 2703bae4e..000000000 --- a/src/main/java/Main.java +++ /dev/null @@ -1,47 +0,0 @@ - -import java.util.Arrays; -import java.util.Scanner; - -public class Main { - - // You can add more methods here - public static String[] filter(String[] items){ - String[] results = new String[items.length]; - int matchCount = 0; - for(String item: items){ - if (item.contains("$")){ - results[matchCount] = item; - matchCount++; - } - } - return Arrays.copyOf(results, matchCount); - } - - public static double totalExpenses(String[] prices) { - double total = 0; - for (String price : prices) { - total += Double.parseDouble(price.trim().replace("$"," ")); - } - return total; - } - public static double exchangeRate(double overseas) { - return overseas * 1.70; - } - public static String[] splitSentence(String sentence) { - return sentence.split(" "); - } - - public static void main(String[] args) { - String line; - Scanner in = new Scanner(System.in); - - System.out.print("Your expenses while overseas?"); - // TODO: add your code here - line = in.nextLine(); - String[] amounts = filter(splitSentence(line)); - System.out.print("Expenses in overseas currency:"); - System.out.println(Arrays.toString(amounts)); - System.out.print("Total in local currency: "); - System.out.println("$" + String.format("%.2f", exchangeRate(totalExpenses(amounts)))); - } -} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 8879bac2b..6a768092d 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -17,5 +17,7 @@ public String getDescription() { public void markDone() { this.isDone = true; } - //... + public String toString() { + return "[" + getStatusIcon() + "]" + getDescription(); + } } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..c3143e89c --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,10 @@ +public class Todo extends Task { + + public Todo(String description) { + super(description); + } + + public String toString() { + return "[T]" + super.toString(); + } +} From 4ce8ac07f16a8bd001973e646dc9608002be42ea Mon Sep 17 00:00:00 2001 From: Edwin Date: Fri, 3 Sep 2021 13:34:39 +0800 Subject: [PATCH 07/28] Level-4, A-CodeQuality --- src/main/java/Duke.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index cb4b348db..0080e72de 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -46,6 +46,10 @@ public static void printStartMessage() { System.out.println("What do you want?\n" + border); System.out.println("Type bye to exit\n" + border); } + public static void printEndMessage() { + System.out.println(border); + System.out.println("chat again next time!\n" + border); + } public static void main(String[] args) { String line; printStartMessage(); @@ -90,7 +94,6 @@ public static void main(String[] args) { addTask(line); } } while (!line.matches("bye")); - System.out.println(border); - System.out.println("chat again next time!\n" + border); + printEndMessage(); } } From c2a8cf3cc49d6d0c8438d1a4621d2074586aa8cc Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 7 Sep 2021 10:19:40 +0800 Subject: [PATCH 08/28] Completed Level-5 --- src/main/java/Duke.java | 93 ++++++++++++++++++++------------ src/main/java/DukeException.java | 41 ++++++++++++++ text-ui-test/runtest.bat | 2 +- 3 files changed, 102 insertions(+), 34 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 0080e72de..f8816a228 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,17 +1,27 @@ import java.util.Scanner; public class Duke { - private static Task[] items = new Task[100]; + private static final int ARRAY_SIZE = 100; + private static final int FOUR = 4; + private static final int FIVE = 5; + private static final int SIX = 6; + private static final int EIGHT = 8; + private static final int NINE = 9; + private static final int ERROR_TODO_IS_EMPTY = 1; + private static final int ERROR_EVENT_IS_EMPTY = 2; + private static final int ERROR_DEADLINE_IS_EMPTY = 3; + private static final int ERROR_COMMAND_NOT_FOUND = 4; + private static final Task[] items = new Task[ARRAY_SIZE]; private static int taskCount = 0; - private static String logo = " _____ ___ _____\n" + private static final String logo = " _____ ___ _____\n" + "|___ | | ||_____| \n" + " / / | | / / \n" + " / / | | / / \n" + " / /___ | | /_/__ \n" + "|_____| | ||_____| \n"; - private static String border = "____________________________________________________________\n"; + private static final String border = "____________________________________________________________\n"; - public static void addedTaskMessage(Task task) { + public static void addTaskMessage(Task task) { System.out.println("Got it. I've added this task:"); System.out.println(task); System.out.println("Now you have " + taskCount + " tasks in the list."); @@ -20,19 +30,19 @@ public static void addEvent(String description, String time) { Events newEvent = new Events(description, time); items[taskCount] = newEvent; taskCount++; - addedTaskMessage(newEvent); + addTaskMessage(newEvent); } public static void addDeadline(String description, String by) { Deadlines newDeadline = new Deadlines(description, by); items[taskCount] = newDeadline; taskCount++; - addedTaskMessage(newDeadline); + addTaskMessage(newDeadline); } public static void addTodo(String description) { Todo newTodo = new Todo(description); items[taskCount] = newTodo; taskCount++; - addedTaskMessage(newTodo); + addTaskMessage(newTodo); } public static void addTask(String description) { System.out.println(border + "added: " + description + '\n' + border); @@ -50,48 +60,65 @@ public static void printEndMessage() { System.out.println(border); System.out.println("chat again next time!\n" + border); } + public static void printTasks(Task[] items) { + int j = 1; + System.out.println(border); + System.out.println("Here is your list"); + for (Task item : items) { + if (item != null) { + System.out.print(j + "."); + System.out.println(item); + j++; + } + } + } + public static void main(String[] args) { String line; printStartMessage(); - do { Scanner in = new Scanner(System.in); line = in.nextLine(); if (line.matches("list")) { - int j = 1; - System.out.println(border); - System.out.println("Here is your list"); - - for (Task item : items) { - if (item != null) { - System.out.print(j + "."); - System.out.println(item); - j++; - } - } + printTasks(items); System.out.println(border); - - } else if (line.length() > 4 && line.substring(0,4).contains("done")) { + } else if (line.length() > FOUR && line.substring(0, FOUR).contains("done")) { int dividerPosition = line.indexOf(" ") + 1; int endPosition = line.length(); - if (endPosition > 5) { + if (endPosition > FIVE) { String num = line.substring(dividerPosition, endPosition); int taskNum = Integer.parseInt(num) - 1; items[taskNum].markDone(); System.out.println(border + "Nice! task is done " + '\n' + border); } - } else if (line.length() > 4 && line.substring(0,4).contains("todo")) { //improve condition to first word - addTodo(line.replace("todo","").trim()); - } else if (line.length() > 5 &&line.substring(0,5).contains("event")) { - String time = line.split("/at")[1].trim(); - String description = line.split("/at")[0].trim().replace("event","").trim(); - addEvent(description, time); - } else if (line.length() > 8 && line.substring(0,8).contains("deadline")) { - String by = line.split("/by")[1].trim(); - String description = line.split("/by")[0].trim().replace("deadline","").trim(); - addDeadline(description, by); + } else if (line.length() >= FOUR && line.substring(0, FOUR).contains("todo")) { //improve condition to first word + if (line.length() <= FIVE) { + DukeException error = new DukeException(ERROR_TODO_IS_EMPTY); + error.printError(error.errorType); + } else { + addTodo(line.replace("todo", "").trim()); + } + } else if (line.length() >= FIVE && line.substring(0, FIVE).contains("event")) { + if (line.length() > SIX && line.contains("/at")) { + String time = line.split("/at")[1].trim(); + String description = line.split("/at")[0].trim().replace("event", "").trim(); + addEvent(description, time); + } else { + DukeException error = new DukeException(ERROR_EVENT_IS_EMPTY); + error.printError(error.errorType); + } + } else if (line.length() >= EIGHT && line.substring(0, EIGHT).contains("deadline")) { + if (line.length() <= NINE && line.contains("by")) { + String by = line.split("/by")[1].trim(); + String description = line.split("/by")[0].trim().replace("deadline", "").trim(); + addDeadline(description, by); + } else { + DukeException error = new DukeException(ERROR_DEADLINE_IS_EMPTY); + error.printError(error.errorType); + } } else { - addTask(line); + DukeException error = new DukeException(ERROR_COMMAND_NOT_FOUND); + error.printError(error.errorType); } } while (!line.matches("bye")); printEndMessage(); diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..692e39a01 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,41 @@ +public class DukeException { + protected int errorType; + private static final int ERROR_TODO_IS_EMPTY = 1; + private static final int ERROR_EVENT_IS_EMPTY = 2; + private static final int ERROR_DEADLINE_IS_EMPTY = 3; + private static final int ERROR_COMMAND_NOT_FOUND = 4; + private static final String border = "____________________________________________________________\n"; + + public static void printEmptyDescriptionErrorMessage(String command) { + System.out.println(border); + System.out.println("\uD83D\uDE00 " + "OOPS!!! The description of a " + command + " cannot be empty."); + System.out.println(border); + } + public static void printCommandMismatchErrorMessage() { + System.out.println(border); + System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, but I don't know what that means :-("); + System.out.println(border); + } + public DukeException(int errorType) { + this.errorType = errorType; + } + public void printError(int type) { + switch (type) { + case ERROR_TODO_IS_EMPTY: + printEmptyDescriptionErrorMessage("todo"); + break; + case ERROR_EVENT_IS_EMPTY: + printEmptyDescriptionErrorMessage("event"); + break; + case ERROR_DEADLINE_IS_EMPTY: + printEmptyDescriptionErrorMessage("deadline"); + break; + case ERROR_COMMAND_NOT_FOUND: + printCommandMismatchErrorMessage(); + break; + } + } + +} + + diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..0484569b2 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,7 @@ REM delete output from previous run if exist ACTUAL.TXT del ACTUAL.TXT REM compile the code into the bin folder -javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java +javac -cp ../src/main/java/Duke.java -Xlint:none -d ..\bin ..\src\main\java\*.java IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 From e61f1e7dd9c518ad0cca87d1e3711a2014165693 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 7 Sep 2021 12:14:59 +0800 Subject: [PATCH 09/28] Completed Level-5,A-Exceptions --- src/main/java/Duke.java | 2 +- text-ui-test/runtest.bat | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f8816a228..3411aeb79 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -116,7 +116,7 @@ public static void main(String[] args) { DukeException error = new DukeException(ERROR_DEADLINE_IS_EMPTY); error.printError(error.errorType); } - } else { + } else if (!line.matches("bye")) { DukeException error = new DukeException(ERROR_COMMAND_NOT_FOUND); error.printError(error.errorType); } diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 0484569b2..e4ba5be1f 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,7 @@ REM delete output from previous run if exist ACTUAL.TXT del ACTUAL.TXT REM compile the code into the bin folder -javac -cp ../src/main/java/Duke.java -Xlint:none -d ..\bin ..\src\main\java\*.java +javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Duke < input.txt > ACTUAL.TXT +java -classpath ..\src\main\java Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From daef42c5e1e17f8756f3082bff09466527238b30 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 7 Sep 2021 13:59:54 +0800 Subject: [PATCH 10/28] Updated Ui Testing --- src/main/java/Duke.java | 21 ++++++++----- text-ui-test/EXPECTED.TXT | 65 +++++++++++++++++++++++++++++++++++---- text-ui-test/input.txt | 9 ++++++ text-ui-test/runtest.bat | 6 ++-- text-ui-test/runtest.sh | 38 ----------------------- 5 files changed, 84 insertions(+), 55 deletions(-) delete mode 100644 text-ui-test/runtest.sh diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3411aeb79..77b0e2717 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,4 @@ +import javax.sound.midi.SysexMessage; import java.util.Scanner; public class Duke { @@ -13,18 +14,22 @@ public class Duke { private static final int ERROR_COMMAND_NOT_FOUND = 4; private static final Task[] items = new Task[ARRAY_SIZE]; private static int taskCount = 0; - private static final String logo = " _____ ___ _____\n" - + "|___ | | ||_____| \n" - + " / / | | / / \n" - + " / / | | / / \n" - + " / /___ | | /_/__ \n" - + "|_____| | ||_____| \n"; + private static final String logo = + " _____ ___ _____ ______\n" + + "|___ | | | |_____| / / -- \\ \\ \n" + + " / / | | / / | | | | \n" + + " / / | | / / | | | |\n" + + " / /___ | | /_/__ | | -- | |\n" + + "|_____| | | |_____| \\ \\____/ /\n"; private static final String border = "____________________________________________________________\n"; public static void addTaskMessage(Task task) { + System.out.println(border); System.out.println("Got it. I've added this task:"); System.out.println(task); System.out.println("Now you have " + taskCount + " tasks in the list."); + System.out.println(border); + } public static void addEvent(String description, String time) { Events newEvent = new Events(description, time); @@ -74,10 +79,10 @@ public static void printTasks(Task[] items) { } public static void main(String[] args) { + Scanner in = new Scanner(System.in); String line; printStartMessage(); do { - Scanner in = new Scanner(System.in); line = in.nextLine(); if (line.matches("list")) { printTasks(items); @@ -108,7 +113,7 @@ public static void main(String[] args) { error.printError(error.errorType); } } else if (line.length() >= EIGHT && line.substring(0, EIGHT).contains("deadline")) { - if (line.length() <= NINE && line.contains("by")) { + if (line.length() >= NINE && line.contains("/by")) { String by = line.split("/by")[1].trim(); String description = line.split("/by")[0].trim().replace("deadline", "").trim(); addDeadline(description, by); diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..d2b80f3b0 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,60 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| + _____ ___ _____ ______ +|___ | | | |_____| / / -- \ \ + / / | | / / | | | | + / / | | / / | | | | + / /___ | | /_/__ | | -- | | +|_____| | | |_____| \ \____/ / +____________________________________________________________ +Hi bro, my name is Echo +What do you want? +____________________________________________________________ + +Type bye to exit +____________________________________________________________ + +Got it. I've added this task: +[E][ ]CCA (at: Tues & Thurs) +Now you have 1 tasks in the list. +Got it. I've added this task: +[D][ ]CS2113T (by: Thursday 2359) +Now you have 2 tasks in the list. +Got it. I've added this task: +[T][ ]CFG1002 +Now you have 3 tasks in the list. +____________________________________________________________ + +Here is your list +1.[E][ ]CCA (at: Tues & Thurs) +2.[D][ ]CS2113T (by: Thursday 2359) +3.[T][ ]CFG1002 +____________________________________________________________ + +____________________________________________________________ +Nice! task is done +____________________________________________________________ + +____________________________________________________________ + +Here is your list +1.[E][X]CCA (at: Tues & Thurs) +2.[D][ ]CS2113T (by: Thursday 2359) +3.[T][ ]CFG1002 +____________________________________________________________ + +____________________________________________________________ +Nice! task is done +____________________________________________________________ + +____________________________________________________________ + +Here is your list +1.[E][X]CCA (at: Tues & Thurs) +2.[D][X]CS2113T (by: Thursday 2359) +3.[T][ ]CFG1002 +____________________________________________________________ + +____________________________________________________________ + +chat again next time! +____________________________________________________________ \ No newline at end of file diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..edcf35c89 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,9 @@ +event CCA /at Tues & Thurs +deadline CS2113T /by Thursday 2359 +todo CFG1002 +list +done 1 +list +done 2 +list +bye \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index e4ba5be1f..0b643f82e 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -4,7 +4,7 @@ REM create bin directory if it doesn't exist if not exist ..\bin mkdir ..\bin REM delete output from previous run -if exist ACTUAL.TXT del ACTUAL.TXT +del ACTUAL.TXT REM compile the code into the bin folder javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\src\main\java Duke < input.txt > ACTUAL.TXT +java -classpath ..\bin Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output -FC ACTUAL.TXT EXPECTED.TXT +FC ACTUAL.TXT EXPECTED.TXT \ No newline at end of file diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh deleted file mode 100644 index c9ec87003..000000000 --- a/text-ui-test/runtest.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -# create bin directory if it doesn't exist -if [ ! -d "../bin" ] -then - mkdir ../bin -fi - -# delete output from previous run -if [ -e "./ACTUAL.TXT" ] -then - rm ACTUAL.TXT -fi - -# compile the code into the bin folder, terminates if error occurred -if ! javac -cp ../src/main/java -Xlint:none -d ../bin ../src/main/java/*.java -then - echo "********** BUILD FAILURE **********" - exit 1 -fi - -# run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ../bin Duke < input.txt > ACTUAL.TXT - -# convert to UNIX format -cp EXPECTED.TXT EXPECTED-UNIX.TXT -dos2unix ACTUAL.TXT EXPECTED-UNIX.TXT - -# compare the output to the expected output -diff ACTUAL.TXT EXPECTED-UNIX.TXT -if [ $? -eq 0 ] -then - echo "Test result: PASSED" - exit 0 -else - echo "Test result: FAILED" - exit 1 -fi \ No newline at end of file From 3fc7b816f33274334ef7077a377e85385fc19ff7 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 7 Sep 2021 14:15:19 +0800 Subject: [PATCH 11/28] Created new branch as part of increments --- text-ui-test/EXPECTED.TXT | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index d2b80f3b0..c53b9a459 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -13,17 +13,29 @@ ____________________________________________________________ Type bye to exit ____________________________________________________________ +____________________________________________________________ + Got it. I've added this task: [E][ ]CCA (at: Tues & Thurs) Now you have 1 tasks in the list. +____________________________________________________________ + +____________________________________________________________ + Got it. I've added this task: [D][ ]CS2113T (by: Thursday 2359) Now you have 2 tasks in the list. +____________________________________________________________ + +____________________________________________________________ + Got it. I've added this task: [T][ ]CFG1002 Now you have 3 tasks in the list. ____________________________________________________________ +____________________________________________________________ + Here is your list 1.[E][ ]CCA (at: Tues & Thurs) 2.[D][ ]CS2113T (by: Thursday 2359) From e8ce66fb0744ca1cb676a410f0d18675e867f418 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 7 Sep 2021 16:53:11 +0800 Subject: [PATCH 12/28] Completed A-Packages and improve coding standards --- src/main/java/Duke.java | 131 ------------------- src/main/java/duke/Duke.java | 91 +++++++++++++ src/main/java/{ => duke}/DukeException.java | 12 +- src/main/java/duke/command/CommandList.java | 114 ++++++++++++++++ src/main/java/{ => duke/task}/Deadlines.java | 2 + src/main/java/{ => duke/task}/Events.java | 2 + src/main/java/{ => duke/task}/Task.java | 5 + src/main/java/{ => duke/task}/Todo.java | 2 + 8 files changed, 226 insertions(+), 133 deletions(-) delete mode 100644 src/main/java/Duke.java create mode 100644 src/main/java/duke/Duke.java rename src/main/java/{ => duke}/DukeException.java (83%) create mode 100644 src/main/java/duke/command/CommandList.java rename src/main/java/{ => duke/task}/Deadlines.java (88%) rename src/main/java/{ => duke/task}/Events.java (89%) rename src/main/java/{ => duke/task}/Task.java (91%) rename src/main/java/{ => duke/task}/Todo.java (85%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 77b0e2717..000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,131 +0,0 @@ -import javax.sound.midi.SysexMessage; -import java.util.Scanner; - -public class Duke { - private static final int ARRAY_SIZE = 100; - private static final int FOUR = 4; - private static final int FIVE = 5; - private static final int SIX = 6; - private static final int EIGHT = 8; - private static final int NINE = 9; - private static final int ERROR_TODO_IS_EMPTY = 1; - private static final int ERROR_EVENT_IS_EMPTY = 2; - private static final int ERROR_DEADLINE_IS_EMPTY = 3; - private static final int ERROR_COMMAND_NOT_FOUND = 4; - private static final Task[] items = new Task[ARRAY_SIZE]; - private static int taskCount = 0; - private static final String logo = - " _____ ___ _____ ______\n" - + "|___ | | | |_____| / / -- \\ \\ \n" - + " / / | | / / | | | | \n" - + " / / | | / / | | | |\n" - + " / /___ | | /_/__ | | -- | |\n" - + "|_____| | | |_____| \\ \\____/ /\n"; - private static final String border = "____________________________________________________________\n"; - - public static void addTaskMessage(Task task) { - System.out.println(border); - System.out.println("Got it. I've added this task:"); - System.out.println(task); - System.out.println("Now you have " + taskCount + " tasks in the list."); - System.out.println(border); - - } - public static void addEvent(String description, String time) { - Events newEvent = new Events(description, time); - items[taskCount] = newEvent; - taskCount++; - addTaskMessage(newEvent); - } - public static void addDeadline(String description, String by) { - Deadlines newDeadline = new Deadlines(description, by); - items[taskCount] = newDeadline; - taskCount++; - addTaskMessage(newDeadline); - } - public static void addTodo(String description) { - Todo newTodo = new Todo(description); - items[taskCount] = newTodo; - taskCount++; - addTaskMessage(newTodo); - } - public static void addTask(String description) { - System.out.println(border + "added: " + description + '\n' + border); - Task newItem = new Task(description); - items[taskCount] = newItem; - taskCount++; - } - public static void printStartMessage() { - System.out.println(logo); - System.out.println(border + "Hi bro, my name is Echo"); - System.out.println("What do you want?\n" + border); - System.out.println("Type bye to exit\n" + border); - } - public static void printEndMessage() { - System.out.println(border); - System.out.println("chat again next time!\n" + border); - } - public static void printTasks(Task[] items) { - int j = 1; - System.out.println(border); - System.out.println("Here is your list"); - for (Task item : items) { - if (item != null) { - System.out.print(j + "."); - System.out.println(item); - j++; - } - } - } - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - String line; - printStartMessage(); - do { - line = in.nextLine(); - if (line.matches("list")) { - printTasks(items); - System.out.println(border); - } else if (line.length() > FOUR && line.substring(0, FOUR).contains("done")) { - int dividerPosition = line.indexOf(" ") + 1; - int endPosition = line.length(); - if (endPosition > FIVE) { - String num = line.substring(dividerPosition, endPosition); - int taskNum = Integer.parseInt(num) - 1; - items[taskNum].markDone(); - System.out.println(border + "Nice! task is done " + '\n' + border); - } - } else if (line.length() >= FOUR && line.substring(0, FOUR).contains("todo")) { //improve condition to first word - if (line.length() <= FIVE) { - DukeException error = new DukeException(ERROR_TODO_IS_EMPTY); - error.printError(error.errorType); - } else { - addTodo(line.replace("todo", "").trim()); - } - } else if (line.length() >= FIVE && line.substring(0, FIVE).contains("event")) { - if (line.length() > SIX && line.contains("/at")) { - String time = line.split("/at")[1].trim(); - String description = line.split("/at")[0].trim().replace("event", "").trim(); - addEvent(description, time); - } else { - DukeException error = new DukeException(ERROR_EVENT_IS_EMPTY); - error.printError(error.errorType); - } - } else if (line.length() >= EIGHT && line.substring(0, EIGHT).contains("deadline")) { - if (line.length() >= NINE && line.contains("/by")) { - String by = line.split("/by")[1].trim(); - String description = line.split("/by")[0].trim().replace("deadline", "").trim(); - addDeadline(description, by); - } else { - DukeException error = new DukeException(ERROR_DEADLINE_IS_EMPTY); - error.printError(error.errorType); - } - } else if (!line.matches("bye")) { - DukeException error = new DukeException(ERROR_COMMAND_NOT_FOUND); - error.printError(error.errorType); - } - } while (!line.matches("bye")); - printEndMessage(); - } -} diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java new file mode 100644 index 000000000..9715a43e6 --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,91 @@ +package duke; + +import duke.command.CommandList; +import duke.task.Task; +import java.util.Scanner; + +public class Duke { + private static final int ARRAY_SIZE = 100; + private static final int FOUR = 4; + private static final int FIVE = 5; + private static final int SIX = 6; + private static final int EIGHT = 8; + private static final int NINE = 9; + private static final int ERROR_TODO_IS_EMPTY = 1; + private static final int ERROR_EVENT_IS_EMPTY = 2; + private static final int ERROR_DEADLINE_IS_EMPTY = 3; + private static final int ERROR_COMMAND_NOT_FOUND = 4; + private static final int CMD_NOT_FOUND = 0; + private static final int CMD_TODO = 1; + private static final int CMD_EVENT = 2; + private static final int CMD_DEADLINE = 3; + private static final int CMD_LIST = 4; + private static final int CMD_DONE = 5; + private static final int CMD_TERMINATE = 6; + private static final Task[] items = new Task[ARRAY_SIZE]; + private static final String logo = + " _____ ___ _____ ______\n" + + "|___ | | | |_____| / / -- \\ \\ \n" + + " / / | | / / | | | | \n" + + " / / | | / / | | | |\n" + + " / /___ | | /_/__ | | -- | |\n" + + "|_____| | | |_____| \\ \\____/ /\n"; + private static final String border = "____________________________________________________________\n"; + + + public static void printStartMessage() { + System.out.println(logo); + System.out.println(border + "Hi bro, my name is Echo"); + System.out.println("What do you want?\n" + border); + System.out.println("Type bye to exit\n" + border); + } + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String line; + printStartMessage(); + CommandList task = new CommandList(); + DukeException error = new DukeException(); + do { + line = in.nextLine(); + if (line.matches("list")) { + task.setCommand(CMD_LIST); + task.executeCommand(items, error, line); + System.out.println(border); + } else if (line.length() > FOUR && line.substring(0, FOUR).contains("done")) { + task.setCommand(CMD_DONE); + task.executeCommand(items, error, line); + } else if (line.length() >= FOUR && line.substring(0, FOUR).contains("todo")) { //improve condition to first word + if (line.length() <= FIVE) { + error.setErrorType(ERROR_TODO_IS_EMPTY); + task.setCommand(CMD_NOT_FOUND); + } else { + task.setCommand(CMD_TODO); + } + task.executeCommand(items, error, line); + } else if (line.length() >= FIVE && line.substring(0, FIVE).contains("event")) { + if (line.length() > SIX && line.contains("/at")) { + task.setCommand(CMD_EVENT); + } else { + error.setErrorType(ERROR_EVENT_IS_EMPTY); + task.setCommand(CMD_NOT_FOUND); + } + task.executeCommand(items, error, line); + } else if (line.length() >= EIGHT && line.substring(0, EIGHT).contains("deadline")) { + if (line.length() >= NINE && line.contains("/by")) { + task.setCommand(CMD_DEADLINE); + } else { + error.setErrorType(ERROR_DEADLINE_IS_EMPTY); + task.setCommand(CMD_NOT_FOUND); + } + task.executeCommand(items, error, line); + } else if (!line.matches("bye")) { + error.setErrorType(ERROR_COMMAND_NOT_FOUND); + task.setCommand(CMD_NOT_FOUND); + task.executeCommand(items, error, line); + } + } while (!line.matches("bye")); + task.setCommand(CMD_TERMINATE); + task.executeCommand(items, error, line); + } +} diff --git a/src/main/java/DukeException.java b/src/main/java/duke/DukeException.java similarity index 83% rename from src/main/java/DukeException.java rename to src/main/java/duke/DukeException.java index 692e39a01..94715fd22 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/duke/DukeException.java @@ -1,3 +1,5 @@ +package duke; + public class DukeException { protected int errorType; private static final int ERROR_TODO_IS_EMPTY = 1; @@ -16,8 +18,14 @@ public static void printCommandMismatchErrorMessage() { System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, but I don't know what that means :-("); System.out.println(border); } - public DukeException(int errorType) { - this.errorType = errorType; + public DukeException() { + this.errorType = ERROR_COMMAND_NOT_FOUND; + } + public void setErrorType(int type) { + this.errorType = type; + } + public int getErrorType() { + return errorType; } public void printError(int type) { switch (type) { diff --git a/src/main/java/duke/command/CommandList.java b/src/main/java/duke/command/CommandList.java new file mode 100644 index 000000000..2ac32aef3 --- /dev/null +++ b/src/main/java/duke/command/CommandList.java @@ -0,0 +1,114 @@ +package duke.command; +import duke.DukeException; +import duke.task.Task; +import duke.task.Deadlines; +import duke.task.Todo; +import duke.task.Events + ; +public class CommandList { + private static final int CMD_NOT_FOUND = 0; + private static final int CMD_TODO = 1; + private static final int CMD_EVENT = 2; + private static final int CMD_DEADLINE = 3; + private static final int CMD_LIST = 4; + private static final int CMD_DONE = 5; + private static final int CMD_TERMINATE = 6; + private static final int FIVE = 5; + private static final String border = "____________________________________________________________\n"; + + protected int command; + protected int taskCount; + + public CommandList() { + + this.command = CMD_NOT_FOUND; + } + + public void setCommand(int command) { + this.command = command; + } + + public void addTaskMessage(Task task) { + System.out.println(border); + System.out.println("Got it. I've added this task:"); + System.out.println(task); + System.out.println("Now you have " + taskCount + " tasks in the list."); + System.out.println(border); + + } + + public void addEvent(Task[] items, String description, String time) { + Events newEvent = new Events(description, time); + items[taskCount] = newEvent; + taskCount++; + addTaskMessage(newEvent); + } + + public void addDeadline(Task[] items, String description, String by) { + Deadlines newDeadline = new Deadlines(description, by); + items[taskCount] = newDeadline; + taskCount++; + addTaskMessage(newDeadline); + } + + public void addTodo(Task[] items, String description) { + Todo newTodo = new Todo(description); + items[taskCount] = newTodo; + taskCount++; + addTaskMessage(newTodo); + } + + public static void printEndMessage() { + System.out.println(border); + System.out.println("chat again next time!\n" + border); + } + + public void printErrorMessage(DukeException error) { + error.printError(error.getErrorType()); + } + + public void executeCommand(Task[] items, DukeException error, String input) { + + switch (command) { + case CMD_TODO: + addTodo(items, input.replace("todo", "").trim()); + break; + case CMD_EVENT: + addEvent(items, input.split("/at")[1].trim(), input.split("/at")[0].trim().replace("event", "").trim()); + break; + case CMD_DEADLINE: + addDeadline(items, input.split("/by")[1].trim(), input.split("/by")[0].trim().replace("deadline", "").trim()); + break; + case CMD_LIST: + int j = 1; + System.out.println(border); + System.out.println("Here is your list"); + for (Task item : items) { + if (item != null) { + System.out.print(j + "."); + System.out.println(item); + j++; + } + } + break; + case CMD_DONE: + int dividerPosition = input.indexOf(" ") + 1; + int endPosition = input.length(); + if (endPosition > FIVE) { + String num = input.substring(dividerPosition, endPosition); + int taskNum = Integer.parseInt(num) - 1; + items[taskNum].markDone(); + System.out.println(border + "Nice! task is done " + '\n' + border); + } + break; + case CMD_TERMINATE: + printEndMessage(); + break; + default: + printErrorMessage(error); + break; + } + } +} + + diff --git a/src/main/java/Deadlines.java b/src/main/java/duke/task/Deadlines.java similarity index 88% rename from src/main/java/Deadlines.java rename to src/main/java/duke/task/Deadlines.java index eb48cb35d..af3267f31 100644 --- a/src/main/java/Deadlines.java +++ b/src/main/java/duke/task/Deadlines.java @@ -1,3 +1,5 @@ +package duke.task; + public class Deadlines extends Task { protected String by; diff --git a/src/main/java/Events.java b/src/main/java/duke/task/Events.java similarity index 89% rename from src/main/java/Events.java rename to src/main/java/duke/task/Events.java index c275953ed..94a13159d 100644 --- a/src/main/java/Events.java +++ b/src/main/java/duke/task/Events.java @@ -1,3 +1,5 @@ +package duke.task; + public class Events extends Task { protected String time; diff --git a/src/main/java/Task.java b/src/main/java/duke/task/Task.java similarity index 91% rename from src/main/java/Task.java rename to src/main/java/duke/task/Task.java index 6a768092d..640a5dba0 100644 --- a/src/main/java/Task.java +++ b/src/main/java/duke/task/Task.java @@ -1,3 +1,5 @@ +package duke.task; + public class Task { protected String description; protected boolean isDone; @@ -8,13 +10,16 @@ public Task(String description) { } public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X } public String getDescription() { + return this.description; } public void markDone() { + this.isDone = true; } public String toString() { diff --git a/src/main/java/Todo.java b/src/main/java/duke/task/Todo.java similarity index 85% rename from src/main/java/Todo.java rename to src/main/java/duke/task/Todo.java index c3143e89c..81fd3390d 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 88ad15e6c184249c92f53eeba0f25011f507e7d6 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 7 Sep 2021 17:12:36 +0800 Subject: [PATCH 13/28] Updated runtest.bat --- text-ui-test/runtest.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 0b643f82e..e798e0298 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,7 @@ REM delete output from previous run del ACTUAL.TXT REM compile the code into the bin folder -javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java +javac -cp ..\src\main\java\ -Xlint:none -d ..\bin ..\src\main\java\duke\*.java IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 From d3cbc2976f86970cc83b2d171cde5af5a88f68c1 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 7 Sep 2021 17:14:15 +0800 Subject: [PATCH 14/28] Formatted some codes --- src/main/java/duke/command/CommandList.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/duke/command/CommandList.java b/src/main/java/duke/command/CommandList.java index 2ac32aef3..36e7fae7c 100644 --- a/src/main/java/duke/command/CommandList.java +++ b/src/main/java/duke/command/CommandList.java @@ -34,7 +34,6 @@ public void addTaskMessage(Task task) { System.out.println(task); System.out.println("Now you have " + taskCount + " tasks in the list."); System.out.println(border); - } public void addEvent(Task[] items, String description, String time) { From b573d4ce762db61931b8f3a73e4526129cc27f5c Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 7 Sep 2021 17:22:22 +0800 Subject: [PATCH 15/28] Code Fixes --- src/main/java/duke/command/CommandList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/duke/command/CommandList.java b/src/main/java/duke/command/CommandList.java index 36e7fae7c..a664f5f96 100644 --- a/src/main/java/duke/command/CommandList.java +++ b/src/main/java/duke/command/CommandList.java @@ -17,7 +17,7 @@ public class CommandList { private static final String border = "____________________________________________________________\n"; protected int command; - protected int taskCount; + protected int taskCount = 0; public CommandList() { From 3974bd3366e20210dcf51ac731fe741030c3bf70 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 7 Sep 2021 17:54:22 +0800 Subject: [PATCH 16/28] Improved A-Exception --- src/main/java/duke/Duke.java | 18 ++++++++++++++++-- src/main/java/duke/DukeException.java | 12 +++++++++++- src/main/java/duke/command/CommandList.java | 4 ++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 9715a43e6..cd01bddb3 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -15,6 +15,7 @@ public class Duke { private static final int ERROR_EVENT_IS_EMPTY = 2; private static final int ERROR_DEADLINE_IS_EMPTY = 3; private static final int ERROR_COMMAND_NOT_FOUND = 4; + private static final int ERROR_IS_INVALID = 5; private static final int CMD_NOT_FOUND = 0; private static final int CMD_TODO = 1; private static final int CMD_EVENT = 2; @@ -39,6 +40,14 @@ public static void printStartMessage() { System.out.println("What do you want?\n" + border); System.out.println("Type bye to exit\n" + border); } + public static boolean isInvalid(CommandList task, String line) { + if (!line.split("done")[1].trim().isEmpty()) { + if (Integer.parseInt(line.split("done")[1].trim()) > task.getTaskCount()) { + return true; + } + } + return (line.length() <= FIVE); + } public static void main(String[] args) { Scanner in = new Scanner(System.in); @@ -53,8 +62,13 @@ public static void main(String[] args) { task.executeCommand(items, error, line); System.out.println(border); } else if (line.length() > FOUR && line.substring(0, FOUR).contains("done")) { - task.setCommand(CMD_DONE); - task.executeCommand(items, error, line); + if (isInvalid(task, line)) { + task.setCommand(CMD_NOT_FOUND); + error.setErrorType(ERROR_IS_INVALID); + } else { + task.setCommand(CMD_DONE); + } + task.executeCommand(items, error, line); } else if (line.length() >= FOUR && line.substring(0, FOUR).contains("todo")) { //improve condition to first word if (line.length() <= FIVE) { error.setErrorType(ERROR_TODO_IS_EMPTY); diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java index 94715fd22..78a423665 100644 --- a/src/main/java/duke/DukeException.java +++ b/src/main/java/duke/DukeException.java @@ -6,6 +6,8 @@ public class DukeException { private static final int ERROR_EVENT_IS_EMPTY = 2; private static final int ERROR_DEADLINE_IS_EMPTY = 3; private static final int ERROR_COMMAND_NOT_FOUND = 4; + private static final int ERROR_IS_INVALID = 5; + private static final String border = "____________________________________________________________\n"; public static void printEmptyDescriptionErrorMessage(String command) { @@ -18,6 +20,11 @@ public static void printCommandMismatchErrorMessage() { System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, but I don't know what that means :-("); System.out.println(border); } + public static void printCommandIsInvalid() { + System.out.println(border); + System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, the command is invalid."); + System.out.println(border); + } public DukeException() { this.errorType = ERROR_COMMAND_NOT_FOUND; } @@ -38,7 +45,10 @@ public void printError(int type) { case ERROR_DEADLINE_IS_EMPTY: printEmptyDescriptionErrorMessage("deadline"); break; - case ERROR_COMMAND_NOT_FOUND: + case ERROR_IS_INVALID: + printCommandIsInvalid(); + break; + default: printCommandMismatchErrorMessage(); break; } diff --git a/src/main/java/duke/command/CommandList.java b/src/main/java/duke/command/CommandList.java index a664f5f96..3a0f2eebd 100644 --- a/src/main/java/duke/command/CommandList.java +++ b/src/main/java/duke/command/CommandList.java @@ -28,6 +28,10 @@ public void setCommand(int command) { this.command = command; } + public int getTaskCount() { + return taskCount; + } + public void addTaskMessage(Task task) { System.out.println(border); System.out.println("Got it. I've added this task:"); From 22af07950a995082819985d3226b5e8aaae4b01a Mon Sep 17 00:00:00 2001 From: Edwin Date: Wed, 15 Sep 2021 15:07:37 +0800 Subject: [PATCH 17/28] Added delete feature --- src/main/java/duke/Duke.java | 24 ++++++++---- src/main/java/duke/command/CommandList.java | 43 ++++++++++++++++----- src/main/java/duke/task/Task.java | 2 +- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index cd01bddb3..60612bb47 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -2,13 +2,14 @@ import duke.command.CommandList; import duke.task.Task; + +import java.util.ArrayList; import java.util.Scanner; public class Duke { - private static final int ARRAY_SIZE = 100; private static final int FOUR = 4; private static final int FIVE = 5; - private static final int SIX = 6; + private static final int SIX = 6;; private static final int EIGHT = 8; private static final int NINE = 9; private static final int ERROR_TODO_IS_EMPTY = 1; @@ -23,7 +24,8 @@ public class Duke { private static final int CMD_LIST = 4; private static final int CMD_DONE = 5; private static final int CMD_TERMINATE = 6; - private static final Task[] items = new Task[ARRAY_SIZE]; + private static final int CMD_DELETE = 7; + private static final ArrayList items = new ArrayList<>(); private static final String logo = " _____ ___ _____ ______\n" + "|___ | | | |_____| / / -- \\ \\ \n" @@ -40,9 +42,9 @@ public static void printStartMessage() { System.out.println("What do you want?\n" + border); System.out.println("Type bye to exit\n" + border); } - public static boolean isInvalid(CommandList task, String line) { - if (!line.split("done")[1].trim().isEmpty()) { - if (Integer.parseInt(line.split("done")[1].trim()) > task.getTaskCount()) { + public static boolean isInvalid(CommandList task, String line , String key) { + if (!line.split(key)[1].trim().isEmpty()) { + if (Integer.parseInt(line.split(key)[1].trim()) > task.getTaskCount()) { return true; } } @@ -62,7 +64,7 @@ public static void main(String[] args) { task.executeCommand(items, error, line); System.out.println(border); } else if (line.length() > FOUR && line.substring(0, FOUR).contains("done")) { - if (isInvalid(task, line)) { + if (isInvalid(task, line, "done")) { task.setCommand(CMD_NOT_FOUND); error.setErrorType(ERROR_IS_INVALID); } else { @@ -93,6 +95,14 @@ public static void main(String[] args) { task.setCommand(CMD_NOT_FOUND); } task.executeCommand(items, error, line); + } else if (line.length() >= SIX && line.substring(0, SIX).contains("delete")) { + if (isInvalid(task, line,"delete")) { + task.setCommand(CMD_NOT_FOUND); + error.setErrorType(ERROR_IS_INVALID); + } else { + task.setCommand(CMD_DELETE); + } + task.executeCommand(items, error, line); } else if (!line.matches("bye")) { error.setErrorType(ERROR_COMMAND_NOT_FOUND); task.setCommand(CMD_NOT_FOUND); diff --git a/src/main/java/duke/command/CommandList.java b/src/main/java/duke/command/CommandList.java index 3a0f2eebd..b5ba614c0 100644 --- a/src/main/java/duke/command/CommandList.java +++ b/src/main/java/duke/command/CommandList.java @@ -5,6 +5,9 @@ import duke.task.Todo; import duke.task.Events ; + +import java.util.ArrayList; + public class CommandList { private static final int CMD_NOT_FOUND = 0; private static final int CMD_TODO = 1; @@ -13,7 +16,9 @@ public class CommandList { private static final int CMD_LIST = 4; private static final int CMD_DONE = 5; private static final int CMD_TERMINATE = 6; + private static final int CMD_DELETE = 7; private static final int FIVE = 5; + private static final int SEVEN = 7; private static final String border = "____________________________________________________________\n"; protected int command; @@ -39,27 +44,38 @@ public void addTaskMessage(Task task) { System.out.println("Now you have " + taskCount + " tasks in the list."); System.out.println(border); } + public void removeTaskMessage(Task task) { + System.out.println(border); + System.out.println("Noted. I've removed this task:"); + System.out.println(task); + System.out.println("Now you have " + taskCount + " tasks in the list."); + System.out.println(border); + } - public void addEvent(Task[] items, String description, String time) { + public void addEvent(ArrayList items, String description, String time) { Events newEvent = new Events(description, time); - items[taskCount] = newEvent; + items.add(taskCount, newEvent); taskCount++; addTaskMessage(newEvent); } - public void addDeadline(Task[] items, String description, String by) { + public void addDeadline(ArrayList items, String description, String by) { Deadlines newDeadline = new Deadlines(description, by); - items[taskCount] = newDeadline; + items.add(taskCount, newDeadline); taskCount++; addTaskMessage(newDeadline); } - public void addTodo(Task[] items, String description) { + public void addTodo(ArrayList items, String description) { Todo newTodo = new Todo(description); - items[taskCount] = newTodo; + items.add(taskCount, newTodo); taskCount++; addTaskMessage(newTodo); } + public void removeItem(ArrayList items, int taskNum) { + removeTaskMessage(items.get(taskNum)); + items.remove(taskNum); + } public static void printEndMessage() { System.out.println(border); @@ -70,7 +86,7 @@ public void printErrorMessage(DukeException error) { error.printError(error.getErrorType()); } - public void executeCommand(Task[] items, DukeException error, String input) { + public void executeCommand(ArrayList items, DukeException error, String input) { switch (command) { case CMD_TODO: @@ -85,7 +101,7 @@ public void executeCommand(Task[] items, DukeException error, String input) { case CMD_LIST: int j = 1; System.out.println(border); - System.out.println("Here is your list"); + System.out.println("Here are the task in your list:"); for (Task item : items) { if (item != null) { System.out.print(j + "."); @@ -100,13 +116,22 @@ public void executeCommand(Task[] items, DukeException error, String input) { if (endPosition > FIVE) { String num = input.substring(dividerPosition, endPosition); int taskNum = Integer.parseInt(num) - 1; - items[taskNum].markDone(); + items.get(taskNum).markDone(); System.out.println(border + "Nice! task is done " + '\n' + border); } break; case CMD_TERMINATE: printEndMessage(); break; + case CMD_DELETE: + dividerPosition = input.indexOf(" ") + 1; + endPosition = input.length(); + if (endPosition > SEVEN) { + String num = input.substring(dividerPosition, endPosition); + int taskNum = Integer.parseInt(num) - 1; + removeItem(items, taskNum); + } + break; default: printErrorMessage(error); break; diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 640a5dba0..b4ee86a9f 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -23,6 +23,6 @@ public void markDone() { this.isDone = true; } public String toString() { - return "[" + getStatusIcon() + "]" + getDescription(); + return "[" + getStatusIcon() + "] " + getDescription(); } } From 3c647fc8c0f5b6aa802c9a137e6e44952e1801f1 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 16 Sep 2021 19:06:11 +0800 Subject: [PATCH 18/28] Added save feature and some personality for Level-7 requirement --- data/save.txt | 3 + src/main/java/duke/Duke.java | 131 +++++++++++++++++--- src/main/java/duke/command/CommandList.java | 34 +++-- src/main/java/duke/task/Deadlines.java | 4 + src/main/java/duke/task/Events.java | 5 +- src/main/java/duke/task/Task.java | 7 +- 6 files changed, 152 insertions(+), 32 deletions(-) create mode 100644 data/save.txt diff --git a/data/save.txt b/data/save.txt new file mode 100644 index 000000000..9c5865bfc --- /dev/null +++ b/data/save.txt @@ -0,0 +1,3 @@ +T/0/cs1010 +E/1/cs2020/com +D/0/cs10102/monday diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 60612bb47..e51c3fa08 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -3,8 +3,13 @@ import duke.command.CommandList; import duke.task.Task; +import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; +import java.io.FileWriter; +import java.io.IOException; + public class Duke { private static final int FOUR = 4; @@ -25,7 +30,18 @@ public class Duke { private static final int CMD_DONE = 5; private static final int CMD_TERMINATE = 6; private static final int CMD_DELETE = 7; + private static final String LIST = "list"; + private static final String TODO = "todo"; + private static final String EVENT = "event"; + private static final String DEADLINE = "deadline"; + private static final String DONE = "done"; + private static final String BYE = "bye"; + private static final String DELETE = "delete"; + private static final String BY = "/by"; + private static final String AT = "/at"; private static final ArrayList items = new ArrayList<>(); + private static final String fileDir = "./data"; + private static final String fileName = "./data/save.txt"; private static final String logo = " _____ ___ _____ ______\n" + "|___ | | | |_____| / / -- \\ \\ \n" @@ -50,28 +66,94 @@ public static boolean isInvalid(CommandList task, String line , String key) { } return (line.length() <= FIVE); } + private static void createSave() { + File saveFile = new File(Duke.fileName); + File saveDir = new File(Duke.fileDir); + if (saveDir.mkdirs()) { + System.out.println("Successfully created save dir"); + } else { + System.out.println("Save folder already exists."); + } + try { + if (saveFile.createNewFile()) { + System.out.println("Successfully created save file"); + } else { + System.out.println("Save file already exists"); + } + } catch (IOException e) { + e.printStackTrace(); + } + } - public static void main(String[] args) { - Scanner in = new Scanner(System.in); + private static void updateSave() { + Task[] saveLists = new Task[items.size()]; + items.toArray(saveLists); + try { + FileWriter fw = new FileWriter(Duke.fileName); + for (Task saveList : saveLists) { + fw.write(saveList.toString().charAt(1) + "/"); + fw.write(saveList.getStatus() + "/" + saveList.getDescription()); + if (!saveList.getDate().equals("empty")) { + fw.write("/" + saveList.getDate()); + } + fw.write("\n"); + } + System.out.println("Automatically saved to " + fileName); + fw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void readSave(CommandList task) throws FileNotFoundException { + File saveFile = new File(Duke.fileName); + Scanner s = new Scanner(saveFile); + while (s.hasNext()) { + String[] chars = s.nextLine().split("/"); + String type = chars[0].trim(); + String status = chars[1].trim(); + String description = chars[2].trim(); + switch (type) { + case "E": + String date = chars[3].trim(); + task.addEvent(Duke.items, description, date); + if (status.equals("1")) { + items.get(task.getTaskCount()).markDone(); + } + break; + case "D": + date = chars[3].trim(); + task.addDeadline(Duke.items, description, date); + if (status.equals("1")) { + items.get(task.getTaskCount()).markDone(); + } + break; + case "T": + task.addTodo(Duke.items, description); + break; + } + } + task.loadTaskCount(items); + System.out.println("Save file successfully loaded"); + } + + public static String readCommand(Scanner in, CommandList task, DukeException error) { String line; - printStartMessage(); - CommandList task = new CommandList(); - DukeException error = new DukeException(); do { line = in.nextLine(); - if (line.matches("list")) { + if (line.matches(LIST)) { task.setCommand(CMD_LIST); task.executeCommand(items, error, line); System.out.println(border); - } else if (line.length() > FOUR && line.substring(0, FOUR).contains("done")) { - if (isInvalid(task, line, "done")) { + } else if (line.length() > FOUR && line.substring(0, FOUR).contains(DONE)) { + if (isInvalid(task, line, DONE)) { task.setCommand(CMD_NOT_FOUND); error.setErrorType(ERROR_IS_INVALID); } else { task.setCommand(CMD_DONE); } - task.executeCommand(items, error, line); - } else if (line.length() >= FOUR && line.substring(0, FOUR).contains("todo")) { //improve condition to first word + task.executeCommand(items, error, line); + } else if (line.length() >= FOUR && line.substring(0, FOUR).contains(TODO)) { if (line.length() <= FIVE) { error.setErrorType(ERROR_TODO_IS_EMPTY); task.setCommand(CMD_NOT_FOUND); @@ -79,36 +161,49 @@ public static void main(String[] args) { task.setCommand(CMD_TODO); } task.executeCommand(items, error, line); - } else if (line.length() >= FIVE && line.substring(0, FIVE).contains("event")) { - if (line.length() > SIX && line.contains("/at")) { + } else if (line.length() >= FIVE && line.substring(0, FIVE).contains(EVENT)) { + if (line.length() > SIX && line.contains(AT)) { task.setCommand(CMD_EVENT); } else { error.setErrorType(ERROR_EVENT_IS_EMPTY); task.setCommand(CMD_NOT_FOUND); } task.executeCommand(items, error, line); - } else if (line.length() >= EIGHT && line.substring(0, EIGHT).contains("deadline")) { - if (line.length() >= NINE && line.contains("/by")) { + } else if (line.length() >= EIGHT && line.substring(0, EIGHT).contains(DEADLINE)) { + if (line.length() >= NINE && line.contains(BY)) { task.setCommand(CMD_DEADLINE); } else { error.setErrorType(ERROR_DEADLINE_IS_EMPTY); task.setCommand(CMD_NOT_FOUND); } task.executeCommand(items, error, line); - } else if (line.length() >= SIX && line.substring(0, SIX).contains("delete")) { - if (isInvalid(task, line,"delete")) { + } else if (line.length() >= SIX && line.substring(0, SIX).contains(DELETE)) { + if (isInvalid(task, line,DELETE)) { task.setCommand(CMD_NOT_FOUND); error.setErrorType(ERROR_IS_INVALID); } else { task.setCommand(CMD_DELETE); } task.executeCommand(items, error, line); - } else if (!line.matches("bye")) { + } else if (!line.matches(BYE)) { error.setErrorType(ERROR_COMMAND_NOT_FOUND); task.setCommand(CMD_NOT_FOUND); task.executeCommand(items, error, line); } - } while (!line.matches("bye")); + } while (!line.matches(BYE)); + return line; + } + + public static void main(String[] args) throws IOException { + Scanner in = new Scanner(System.in); + CommandList task = new CommandList(); + DukeException error = new DukeException(); + String line; + createSave(); + readSave(task); + printStartMessage(); + line = readCommand(in, task, error); + updateSave(); task.setCommand(CMD_TERMINATE); task.executeCommand(items, error, line); } diff --git a/src/main/java/duke/command/CommandList.java b/src/main/java/duke/command/CommandList.java index b5ba614c0..599593b99 100644 --- a/src/main/java/duke/command/CommandList.java +++ b/src/main/java/duke/command/CommandList.java @@ -3,9 +3,9 @@ import duke.task.Task; import duke.task.Deadlines; import duke.task.Todo; -import duke.task.Events - ; +import duke.task.Events; +import java.lang.reflect.Array; import java.util.ArrayList; public class CommandList { @@ -17,6 +17,11 @@ public class CommandList { private static final int CMD_DONE = 5; private static final int CMD_TERMINATE = 6; private static final int CMD_DELETE = 7; + private static final String TODO = "todo"; + private static final String EVENT = "event"; + private static final String DEADLINE = "deadline"; + private static final String BY = "/by"; + private static final String AT = "/at"; private static final int FIVE = 5; private static final int SEVEN = 7; private static final String border = "____________________________________________________________\n"; @@ -37,7 +42,12 @@ public int getTaskCount() { return taskCount; } + public void loadTaskCount(ArrayList items) { + this.taskCount = items.size(); + } + public void addTaskMessage(Task task) { + taskCount++; System.out.println(border); System.out.println("Got it. I've added this task:"); System.out.println(task); @@ -55,24 +65,19 @@ public void removeTaskMessage(Task task) { public void addEvent(ArrayList items, String description, String time) { Events newEvent = new Events(description, time); items.add(taskCount, newEvent); - taskCount++; - addTaskMessage(newEvent); } public void addDeadline(ArrayList items, String description, String by) { Deadlines newDeadline = new Deadlines(description, by); items.add(taskCount, newDeadline); - taskCount++; - addTaskMessage(newDeadline); } public void addTodo(ArrayList items, String description) { Todo newTodo = new Todo(description); items.add(taskCount, newTodo); - taskCount++; - addTaskMessage(newTodo); } public void removeItem(ArrayList items, int taskNum) { + taskCount--; removeTaskMessage(items.get(taskNum)); items.remove(taskNum); } @@ -87,16 +92,21 @@ public void printErrorMessage(DukeException error) { } public void executeCommand(ArrayList items, DukeException error, String input) { - + String[] inputs; switch (command) { case CMD_TODO: - addTodo(items, input.replace("todo", "").trim()); + addTodo(items, input.replace(TODO, "").trim()); + addTaskMessage(items.get(taskCount)); break; case CMD_EVENT: - addEvent(items, input.split("/at")[1].trim(), input.split("/at")[0].trim().replace("event", "").trim()); + inputs = input.split(AT); + addEvent(items, inputs[0].trim().replace(EVENT, "").trim(), inputs[1].trim()); + addTaskMessage(items.get(taskCount)); break; case CMD_DEADLINE: - addDeadline(items, input.split("/by")[1].trim(), input.split("/by")[0].trim().replace("deadline", "").trim()); + inputs = input.split(BY); + addDeadline(items, inputs[0].trim().replace(DEADLINE, "").trim(), inputs[1].trim()); + addTaskMessage(items.get(taskCount)); break; case CMD_LIST: int j = 1; diff --git a/src/main/java/duke/task/Deadlines.java b/src/main/java/duke/task/Deadlines.java index af3267f31..c978afe2a 100644 --- a/src/main/java/duke/task/Deadlines.java +++ b/src/main/java/duke/task/Deadlines.java @@ -7,6 +7,10 @@ public Deadlines(String description, String by) { super(description); this.by = by; } + @Override + public String getDate() { + return this.by; + } public String toString() { return "[D]" + super.toString() + " (by: " + by + ")"; diff --git a/src/main/java/duke/task/Events.java b/src/main/java/duke/task/Events.java index 94a13159d..4937992a0 100644 --- a/src/main/java/duke/task/Events.java +++ b/src/main/java/duke/task/Events.java @@ -7,7 +7,10 @@ public Events(String description, String time) { super(description); this.time = time; } - + @Override + public String getDate() { + return this.time; + } public String toString() { return "[E]" + super.toString() + " (at: " + time + ")"; } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index b4ee86a9f..9ffad5ef9 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -17,7 +17,12 @@ public String getDescription() { return this.description; } - + public String getStatus() { + return (isDone ? "1" : "0"); + } + public String getDate() { + return "empty"; + } public void markDone() { this.isDone = true; From a294af85e05d5f7c983bdaa898540e1bf71163c2 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 16 Sep 2021 19:38:19 +0800 Subject: [PATCH 19/28] Minor changes to save.txt --- data/save.txt | 4 ++-- src/main/java/META-INF/MANIFEST.MF | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/data/save.txt b/data/save.txt index 9c5865bfc..6b79d67fd 100644 --- a/data/save.txt +++ b/data/save.txt @@ -1,3 +1,3 @@ -T/0/cs1010 -E/1/cs2020/com D/0/cs10102/monday +E/1/cs2020/com +T/0/cs1010 diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..6e864153e --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.Duke + From 92cbe7d08ddca483e8a919d0db80a5b9f66afb03 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 28 Sep 2021 02:16:34 +0800 Subject: [PATCH 20/28] Added more OOP and fixed exception handling --- data/save.txt | 3 - src/main/java/duke/Duke.java | 228 +++--------------- src/main/java/duke/DukeException.java | 59 ----- src/main/java/duke/command/Command.java | 108 +++++++++ src/main/java/duke/command/CommandList.java | 152 ------------ .../CommandDoesNotExistException.java | 4 + .../exception/CommandInvalidException.java | 4 + .../java/duke/exception/DukeException.java | 4 + .../exception/EmptyDescriptionException.java | 4 + src/main/java/duke/parser/Parser.java | 135 +++++++++++ src/main/java/duke/storage/Storage.java | 97 ++++++++ src/main/java/duke/tasklist/TaskList.java | 48 ++++ .../duke/{ => tasklist}/task/Deadlines.java | 2 +- .../java/duke/{ => tasklist}/task/Events.java | 2 +- .../java/duke/{ => tasklist}/task/Task.java | 2 +- .../java/duke/{ => tasklist}/task/Todo.java | 2 +- src/main/java/duke/ui/Ui.java | 74 ++++++ 17 files changed, 520 insertions(+), 408 deletions(-) delete mode 100644 src/main/java/duke/DukeException.java create mode 100644 src/main/java/duke/command/Command.java delete mode 100644 src/main/java/duke/command/CommandList.java create mode 100644 src/main/java/duke/exception/CommandDoesNotExistException.java create mode 100644 src/main/java/duke/exception/CommandInvalidException.java create mode 100644 src/main/java/duke/exception/DukeException.java create mode 100644 src/main/java/duke/exception/EmptyDescriptionException.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 rename src/main/java/duke/{ => tasklist}/task/Deadlines.java (88%) rename src/main/java/duke/{ => tasklist}/task/Events.java (88%) rename src/main/java/duke/{ => tasklist}/task/Task.java (91%) rename src/main/java/duke/{ => tasklist}/task/Todo.java (82%) create mode 100644 src/main/java/duke/ui/Ui.java diff --git a/data/save.txt b/data/save.txt index 6b79d67fd..e69de29bb 100644 --- a/data/save.txt +++ b/data/save.txt @@ -1,3 +0,0 @@ -D/0/cs10102/monday -E/1/cs2020/com -T/0/cs1010 diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index e51c3fa08..7387996f4 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,210 +1,58 @@ package duke; -import duke.command.CommandList; -import duke.task.Task; - -import java.io.File; +import duke.command.Command; +import duke.exception.CommandDoesNotExistException; +import duke.exception.DukeException; +import duke.exception.EmptyDescriptionException; +import duke.parser.Parser; +import duke.storage.Storage; +import duke.tasklist.TaskList; +import duke.ui.Ui; import java.io.FileNotFoundException; -import java.util.ArrayList; import java.util.Scanner; -import java.io.FileWriter; -import java.io.IOException; public class Duke { - private static final int FOUR = 4; - private static final int FIVE = 5; - private static final int SIX = 6;; - private static final int EIGHT = 8; - private static final int NINE = 9; - private static final int ERROR_TODO_IS_EMPTY = 1; - private static final int ERROR_EVENT_IS_EMPTY = 2; - private static final int ERROR_DEADLINE_IS_EMPTY = 3; - private static final int ERROR_COMMAND_NOT_FOUND = 4; - private static final int ERROR_IS_INVALID = 5; - private static final int CMD_NOT_FOUND = 0; - private static final int CMD_TODO = 1; - private static final int CMD_EVENT = 2; - private static final int CMD_DEADLINE = 3; - private static final int CMD_LIST = 4; - private static final int CMD_DONE = 5; - private static final int CMD_TERMINATE = 6; - private static final int CMD_DELETE = 7; - private static final String LIST = "list"; - private static final String TODO = "todo"; - private static final String EVENT = "event"; - private static final String DEADLINE = "deadline"; - private static final String DONE = "done"; - private static final String BYE = "bye"; - private static final String DELETE = "delete"; - private static final String BY = "/by"; - private static final String AT = "/at"; - private static final ArrayList items = new ArrayList<>(); private static final String fileDir = "./data"; private static final String fileName = "./data/save.txt"; - private static final String logo = - " _____ ___ _____ ______\n" - + "|___ | | | |_____| / / -- \\ \\ \n" - + " / / | | / / | | | | \n" - + " / / | | / / | | | |\n" - + " / /___ | | /_/__ | | -- | |\n" - + "|_____| | | |_____| \\ \\____/ /\n"; - private static final String border = "____________________________________________________________\n"; + private final Storage storage; + private TaskList task; + private final Ui ui; - public static void printStartMessage() { - System.out.println(logo); - System.out.println(border + "Hi bro, my name is Echo"); - System.out.println("What do you want?\n" + border); - System.out.println("Type bye to exit\n" + border); - } - public static boolean isInvalid(CommandList task, String line , String key) { - if (!line.split(key)[1].trim().isEmpty()) { - if (Integer.parseInt(line.split(key)[1].trim()) > task.getTaskCount()) { - return true; - } - } - return (line.length() <= FIVE); - } - private static void createSave() { - File saveFile = new File(Duke.fileName); - File saveDir = new File(Duke.fileDir); - if (saveDir.mkdirs()) { - System.out.println("Successfully created save dir"); - } else { - System.out.println("Save folder already exists."); - } + public Duke(String fileName, String fileDir) { + storage = new Storage(fileName, fileDir); + ui = new Ui(); try { - if (saveFile.createNewFile()) { - System.out.println("Successfully created save file"); - } else { - System.out.println("Save file already exists"); - } - } catch (IOException e) { - e.printStackTrace(); + task = new TaskList(storage.items); + storage.load(task); + } catch (FileNotFoundException e) { + ui.showSaveFileError(); + storage.create(); + task = new TaskList(storage.items); } } - - private static void updateSave() { - Task[] saveLists = new Task[items.size()]; - items.toArray(saveLists); - try { - FileWriter fw = new FileWriter(Duke.fileName); - for (Task saveList : saveLists) { - fw.write(saveList.toString().charAt(1) + "/"); - fw.write(saveList.getStatus() + "/" + saveList.getDescription()); - if (!saveList.getDate().equals("empty")) { - fw.write("/" + saveList.getDate()); + public void run() { + Scanner in = new Scanner(System.in); + boolean isExit = false; + while (!isExit) { + try { + String fullCommand = ui.readCommand(in); + Command c = Parser.parse(fullCommand); + boolean hasError = Parser.verifyCommand(task, c, ui); + if (hasError) { + throw new DukeException(); } - fw.write("\n"); - } - System.out.println("Automatically saved to " + fileName); - fw.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static void readSave(CommandList task) throws FileNotFoundException { - File saveFile = new File(Duke.fileName); - Scanner s = new Scanner(saveFile); - while (s.hasNext()) { - String[] chars = s.nextLine().split("/"); - String type = chars[0].trim(); - String status = chars[1].trim(); - String description = chars[2].trim(); - switch (type) { - case "E": - String date = chars[3].trim(); - task.addEvent(Duke.items, description, date); - if (status.equals("1")) { - items.get(task.getTaskCount()).markDone(); - } - break; - case "D": - date = chars[3].trim(); - task.addDeadline(Duke.items, description, date); - if (status.equals("1")) { - items.get(task.getTaskCount()).markDone(); - } - break; - case "T": - task.addTodo(Duke.items, description); - break; + c.execute(task, ui, storage); + isExit = c.isExit(); + } catch (DukeException e) { + ui.showError(); } } - task.loadTaskCount(items); - System.out.println("Save file successfully loaded"); - } - - public static String readCommand(Scanner in, CommandList task, DukeException error) { - String line; - do { - line = in.nextLine(); - if (line.matches(LIST)) { - task.setCommand(CMD_LIST); - task.executeCommand(items, error, line); - System.out.println(border); - } else if (line.length() > FOUR && line.substring(0, FOUR).contains(DONE)) { - if (isInvalid(task, line, DONE)) { - task.setCommand(CMD_NOT_FOUND); - error.setErrorType(ERROR_IS_INVALID); - } else { - task.setCommand(CMD_DONE); - } - task.executeCommand(items, error, line); - } else if (line.length() >= FOUR && line.substring(0, FOUR).contains(TODO)) { - if (line.length() <= FIVE) { - error.setErrorType(ERROR_TODO_IS_EMPTY); - task.setCommand(CMD_NOT_FOUND); - } else { - task.setCommand(CMD_TODO); - } - task.executeCommand(items, error, line); - } else if (line.length() >= FIVE && line.substring(0, FIVE).contains(EVENT)) { - if (line.length() > SIX && line.contains(AT)) { - task.setCommand(CMD_EVENT); - } else { - error.setErrorType(ERROR_EVENT_IS_EMPTY); - task.setCommand(CMD_NOT_FOUND); - } - task.executeCommand(items, error, line); - } else if (line.length() >= EIGHT && line.substring(0, EIGHT).contains(DEADLINE)) { - if (line.length() >= NINE && line.contains(BY)) { - task.setCommand(CMD_DEADLINE); - } else { - error.setErrorType(ERROR_DEADLINE_IS_EMPTY); - task.setCommand(CMD_NOT_FOUND); - } - task.executeCommand(items, error, line); - } else if (line.length() >= SIX && line.substring(0, SIX).contains(DELETE)) { - if (isInvalid(task, line,DELETE)) { - task.setCommand(CMD_NOT_FOUND); - error.setErrorType(ERROR_IS_INVALID); - } else { - task.setCommand(CMD_DELETE); - } - task.executeCommand(items, error, line); - } else if (!line.matches(BYE)) { - error.setErrorType(ERROR_COMMAND_NOT_FOUND); - task.setCommand(CMD_NOT_FOUND); - task.executeCommand(items, error, line); - } - } while (!line.matches(BYE)); - return line; + storage.save(); + ui.printEndMessage(); } - - public static void main(String[] args) throws IOException { - Scanner in = new Scanner(System.in); - CommandList task = new CommandList(); - DukeException error = new DukeException(); - String line; - createSave(); - readSave(task); - printStartMessage(); - line = readCommand(in, task, error); - updateSave(); - task.setCommand(CMD_TERMINATE); - task.executeCommand(items, error, line); + public static void main(String[] args) { + new Duke(fileName, fileDir).run(); } } diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java deleted file mode 100644 index 78a423665..000000000 --- a/src/main/java/duke/DukeException.java +++ /dev/null @@ -1,59 +0,0 @@ -package duke; - -public class DukeException { - protected int errorType; - private static final int ERROR_TODO_IS_EMPTY = 1; - private static final int ERROR_EVENT_IS_EMPTY = 2; - private static final int ERROR_DEADLINE_IS_EMPTY = 3; - private static final int ERROR_COMMAND_NOT_FOUND = 4; - private static final int ERROR_IS_INVALID = 5; - - private static final String border = "____________________________________________________________\n"; - - public static void printEmptyDescriptionErrorMessage(String command) { - System.out.println(border); - System.out.println("\uD83D\uDE00 " + "OOPS!!! The description of a " + command + " cannot be empty."); - System.out.println(border); - } - public static void printCommandMismatchErrorMessage() { - System.out.println(border); - System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, but I don't know what that means :-("); - System.out.println(border); - } - public static void printCommandIsInvalid() { - System.out.println(border); - System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, the command is invalid."); - System.out.println(border); - } - public DukeException() { - this.errorType = ERROR_COMMAND_NOT_FOUND; - } - public void setErrorType(int type) { - this.errorType = type; - } - public int getErrorType() { - return errorType; - } - public void printError(int type) { - switch (type) { - case ERROR_TODO_IS_EMPTY: - printEmptyDescriptionErrorMessage("todo"); - break; - case ERROR_EVENT_IS_EMPTY: - printEmptyDescriptionErrorMessage("event"); - break; - case ERROR_DEADLINE_IS_EMPTY: - printEmptyDescriptionErrorMessage("deadline"); - break; - case ERROR_IS_INVALID: - printCommandIsInvalid(); - break; - default: - printCommandMismatchErrorMessage(); - break; - } - } - -} - - diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java new file mode 100644 index 000000000..971d8502a --- /dev/null +++ b/src/main/java/duke/command/Command.java @@ -0,0 +1,108 @@ +package duke.command; + +import duke.storage.Storage; +import duke.tasklist.TaskList; +import duke.tasklist.task.Task; +import duke.ui.Ui; + +public class Command { + private static final int CMD_NOT_FOUND = 0; + private static final int CMD_TODO = 1; + private static final int CMD_EVENT = 2; + private static final int CMD_DEADLINE = 3; + private static final int CMD_LIST = 4; + private static final int CMD_DONE = 5; + private static final int CMD_DELETE = 6; + private static final int CMD_TERMINATE = 0; + private static final String TODO = "todo"; + private static final String EVENT = "event"; + private static final String DEADLINE = "deadline"; + private static final int INDEX_NUM_DONE = 5; + private static final int INDEX_NUM_DELETE = 7; + private static final String BY = "/by"; + private static final String AT = "/at"; + + protected int command; + protected boolean isExit = false; + protected String userInput; + + public Command() { + this.command = CMD_NOT_FOUND; + } + public void setCommand(int command) { + this.command = command; + } + public void setUserInput(String userInput) { + this.userInput = userInput; + + } + public String getUserInput() { + return this.userInput; + } + public int getCommand() { + return this.command; + } + + public void execute(TaskList tasks, Ui ui, Storage storage) { + int taskCount = tasks.getTaskCount(); + String[] userInputs; + switch (command) { + case CMD_TODO: + tasks.addTodo(storage.items, userInput.replace(TODO, "").trim()); + tasks.loadTaskCount(storage.items); + ui.addTaskMessage(tasks, storage.items.get(taskCount)); + break; + case CMD_EVENT: + userInputs = userInput.split(AT); + tasks.addEvent(storage.items, userInputs[0].trim().replace(EVENT, "").trim(), userInputs[1].trim()); + tasks.loadTaskCount(storage.items); + ui.addTaskMessage(tasks, storage.items.get(taskCount)); + break; + case CMD_DEADLINE: + userInputs = userInput.split(BY); + tasks.addDeadline(storage.items, userInputs[0].trim().replace(DEADLINE, "").trim(), userInputs[1].trim()); + tasks.loadTaskCount(storage.items); + ui.addTaskMessage(tasks, storage.items.get(taskCount)); + break; + case CMD_LIST: + int j = 1; + System.out.println(Ui.border); + System.out.println("Here are the task in your list:"); + for (Task item : storage.items) { + if (item != null) { + System.out.print(j + "."); + System.out.println(item); + j++; + } + } + break; + case CMD_DONE: + int dividerPosition = userInput.indexOf(" ") + 1; + int endPosition = userInput.length(); + if (endPosition > INDEX_NUM_DONE) { + String num = userInput.substring(dividerPosition, endPosition); + int taskNum = Integer.parseInt(num) - 1; + storage.items.get(taskNum).markDone(); + ui.showLine(); + System.out.println("Nice! task is done " + '\n'); + ui.showLine(); + } + break; + case CMD_DELETE: + dividerPosition = userInput.indexOf(" ") + 1; + endPosition = userInput.length(); + if (endPosition > INDEX_NUM_DELETE) { + String num = userInput.substring(dividerPosition, endPosition); + int taskNum = Integer.parseInt(num) - 1; + tasks.removeItem(ui, storage.items, taskNum); + } + break; + case CMD_TERMINATE: + isExit = true; + break; + } + } + public boolean isExit() { + return this.isExit; + } +} diff --git a/src/main/java/duke/command/CommandList.java b/src/main/java/duke/command/CommandList.java deleted file mode 100644 index 599593b99..000000000 --- a/src/main/java/duke/command/CommandList.java +++ /dev/null @@ -1,152 +0,0 @@ -package duke.command; -import duke.DukeException; -import duke.task.Task; -import duke.task.Deadlines; -import duke.task.Todo; -import duke.task.Events; - -import java.lang.reflect.Array; -import java.util.ArrayList; - -public class CommandList { - private static final int CMD_NOT_FOUND = 0; - private static final int CMD_TODO = 1; - private static final int CMD_EVENT = 2; - private static final int CMD_DEADLINE = 3; - private static final int CMD_LIST = 4; - private static final int CMD_DONE = 5; - private static final int CMD_TERMINATE = 6; - private static final int CMD_DELETE = 7; - private static final String TODO = "todo"; - private static final String EVENT = "event"; - private static final String DEADLINE = "deadline"; - private static final String BY = "/by"; - private static final String AT = "/at"; - private static final int FIVE = 5; - private static final int SEVEN = 7; - private static final String border = "____________________________________________________________\n"; - - protected int command; - protected int taskCount = 0; - - public CommandList() { - - this.command = CMD_NOT_FOUND; - } - - public void setCommand(int command) { - this.command = command; - } - - public int getTaskCount() { - return taskCount; - } - - public void loadTaskCount(ArrayList items) { - this.taskCount = items.size(); - } - - public void addTaskMessage(Task task) { - taskCount++; - System.out.println(border); - System.out.println("Got it. I've added this task:"); - System.out.println(task); - System.out.println("Now you have " + taskCount + " tasks in the list."); - System.out.println(border); - } - public void removeTaskMessage(Task task) { - System.out.println(border); - System.out.println("Noted. I've removed this task:"); - System.out.println(task); - System.out.println("Now you have " + taskCount + " tasks in the list."); - System.out.println(border); - } - - public void addEvent(ArrayList items, String description, String time) { - Events newEvent = new Events(description, time); - items.add(taskCount, newEvent); - } - - public void addDeadline(ArrayList items, String description, String by) { - Deadlines newDeadline = new Deadlines(description, by); - items.add(taskCount, newDeadline); - } - - public void addTodo(ArrayList items, String description) { - Todo newTodo = new Todo(description); - items.add(taskCount, newTodo); - } - public void removeItem(ArrayList items, int taskNum) { - taskCount--; - removeTaskMessage(items.get(taskNum)); - items.remove(taskNum); - } - - public static void printEndMessage() { - System.out.println(border); - System.out.println("chat again next time!\n" + border); - } - - public void printErrorMessage(DukeException error) { - error.printError(error.getErrorType()); - } - - public void executeCommand(ArrayList items, DukeException error, String input) { - String[] inputs; - switch (command) { - case CMD_TODO: - addTodo(items, input.replace(TODO, "").trim()); - addTaskMessage(items.get(taskCount)); - break; - case CMD_EVENT: - inputs = input.split(AT); - addEvent(items, inputs[0].trim().replace(EVENT, "").trim(), inputs[1].trim()); - addTaskMessage(items.get(taskCount)); - break; - case CMD_DEADLINE: - inputs = input.split(BY); - addDeadline(items, inputs[0].trim().replace(DEADLINE, "").trim(), inputs[1].trim()); - addTaskMessage(items.get(taskCount)); - break; - case CMD_LIST: - int j = 1; - System.out.println(border); - System.out.println("Here are the task in your list:"); - for (Task item : items) { - if (item != null) { - System.out.print(j + "."); - System.out.println(item); - j++; - } - } - break; - case CMD_DONE: - int dividerPosition = input.indexOf(" ") + 1; - int endPosition = input.length(); - if (endPosition > FIVE) { - String num = input.substring(dividerPosition, endPosition); - int taskNum = Integer.parseInt(num) - 1; - items.get(taskNum).markDone(); - System.out.println(border + "Nice! task is done " + '\n' + border); - } - break; - case CMD_TERMINATE: - printEndMessage(); - break; - case CMD_DELETE: - dividerPosition = input.indexOf(" ") + 1; - endPosition = input.length(); - if (endPosition > SEVEN) { - String num = input.substring(dividerPosition, endPosition); - int taskNum = Integer.parseInt(num) - 1; - removeItem(items, taskNum); - } - break; - default: - printErrorMessage(error); - break; - } - } -} - - diff --git a/src/main/java/duke/exception/CommandDoesNotExistException.java b/src/main/java/duke/exception/CommandDoesNotExistException.java new file mode 100644 index 000000000..c3c6f33a0 --- /dev/null +++ b/src/main/java/duke/exception/CommandDoesNotExistException.java @@ -0,0 +1,4 @@ +package duke.exception; + +public class CommandDoesNotExistException extends Exception { +} diff --git a/src/main/java/duke/exception/CommandInvalidException.java b/src/main/java/duke/exception/CommandInvalidException.java new file mode 100644 index 000000000..e6cb9ee3e --- /dev/null +++ b/src/main/java/duke/exception/CommandInvalidException.java @@ -0,0 +1,4 @@ +package duke.exception; + +public class CommandInvalidException extends Exception { +} diff --git a/src/main/java/duke/exception/DukeException.java b/src/main/java/duke/exception/DukeException.java new file mode 100644 index 000000000..6464da39f --- /dev/null +++ b/src/main/java/duke/exception/DukeException.java @@ -0,0 +1,4 @@ +package duke.exception; + +public class DukeException extends Exception { +} diff --git a/src/main/java/duke/exception/EmptyDescriptionException.java b/src/main/java/duke/exception/EmptyDescriptionException.java new file mode 100644 index 000000000..5f6a65836 --- /dev/null +++ b/src/main/java/duke/exception/EmptyDescriptionException.java @@ -0,0 +1,4 @@ +package duke.exception; + +public class EmptyDescriptionException extends Exception{ +} diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java new file mode 100644 index 000000000..5fa6bfb4c --- /dev/null +++ b/src/main/java/duke/parser/Parser.java @@ -0,0 +1,135 @@ +package duke.parser; + +import duke.command.Command; +import duke.exception.CommandDoesNotExistException; +import duke.exception.CommandInvalidException; +import duke.exception.EmptyDescriptionException; +import duke.tasklist.TaskList; +import duke.ui.Ui; + +public class Parser { + private static final int LENGTH_OF_DONE = 4; + private static final int LENGTH_OF_TODO = 4; + private static final int LENGTH_OF_EVENT = 5; + private static final int LENGTH_OF_DELETE = 6; + private static final int LENGTH_OF_DEADLINE = 8; + private static final int MIN_SPLIT_SIZE = 2; + private static final int CMD_NOT_FOUND = -1; + private static final int CMD_TODO = 1; + private static final int CMD_EVENT = 2; + private static final int CMD_DEADLINE = 3; + private static final int CMD_LIST = 4; + private static final int CMD_DONE = 5; + private static final int CMD_DELETE = 6; + private static final int CMD_TERMINATE = 0; + private static final String LIST = "list"; + private static final String TODO = "todo"; + private static final String EVENT = "event"; + private static final String DEADLINE = "deadline"; + private static final String DONE = "done"; + private static final String BYE = "bye"; + private static final String DELETE = "delete"; + private static final String BY = "/by"; + private static final String AT = "/at"; + + public static boolean isInvalid(TaskList task, String line, String key) { + if (!line.split(key)[1].trim().isEmpty()) { + if (Integer.parseInt(line.split(key)[1].trim()) > task.getTaskCount()) { + return true; + } + } + return (line.length() <= LENGTH_OF_EVENT); + } + + public static Command parse(String fullCommand) { + Command c; + c = new Command(); + c.setUserInput(fullCommand); + if (fullCommand.matches(LIST)) { + c.setCommand(CMD_LIST); + } else if (fullCommand.length() > LENGTH_OF_DONE && fullCommand.substring(0, LENGTH_OF_DONE).contains(DONE)) { + c.setCommand(CMD_DONE); + } else if (fullCommand.length() >= LENGTH_OF_TODO && fullCommand.substring(0, LENGTH_OF_TODO).contains(TODO)) { + c.setCommand(CMD_TODO); + } else if (fullCommand.length() >= LENGTH_OF_EVENT && fullCommand.substring(0, LENGTH_OF_EVENT).contains(EVENT)) { + c.setCommand(CMD_EVENT); + } else if (fullCommand.length() >= LENGTH_OF_DEADLINE && fullCommand.substring(0, LENGTH_OF_DEADLINE).contains(DEADLINE)) { + c.setCommand(CMD_DEADLINE); + } else if (fullCommand.length() >= LENGTH_OF_DELETE && fullCommand.substring(0, LENGTH_OF_DELETE).contains(DELETE)) { + c.setCommand(CMD_DELETE); + } else if (fullCommand.matches(BYE)) { + c.setCommand(CMD_TERMINATE); + } else { + c.setCommand(CMD_NOT_FOUND); + } + return c; + } + + public static boolean verifyCommand(TaskList task, Command c, Ui ui) { + String userInput = c.getUserInput(); + switch (c.getCommand()) { + case CMD_TODO: + try { + if (userInput.length() <= LENGTH_OF_TODO + 1) { + throw new EmptyDescriptionException(); + } + } catch (EmptyDescriptionException e) { + ui.printEmptyDescriptionError(TODO); + return true; + } + return false; + case CMD_EVENT: + try { + String[] keys = userInput.replace(EVENT, "").split(AT); + if (keys.length < MIN_SPLIT_SIZE || keys[0].isBlank() || keys[1].isBlank()) { + throw new EmptyDescriptionException(); + } + } catch (EmptyDescriptionException e) { + ui.printEmptyDescriptionError(EVENT); + return true; + } + return false; + case CMD_DEADLINE: + try { + String[] keys = userInput.replace(DEADLINE, "").split(BY); + if (keys.length < MIN_SPLIT_SIZE || keys[0].isBlank() || keys[1].isBlank()) { + throw new EmptyDescriptionException(); + } + } catch (EmptyDescriptionException e) { + ui.printEmptyDescriptionError(DEADLINE); + return true; + } + return false; + case CMD_DONE: + try { + if (isInvalid(task, userInput, DONE)) { + throw new CommandInvalidException(); + } + } catch (CommandInvalidException e) { + ui.printCommandIsInvalid(); + return true; + } + return false; + case CMD_DELETE: + try { + if (isInvalid(task, userInput, DELETE)) { + throw new CommandInvalidException(); + } + } catch (CommandInvalidException e) { + ui.printCommandIsInvalid(); + return true; + } + return false; + case CMD_LIST: + case CMD_TERMINATE: + return false; + default: + try { + throw new CommandDoesNotExistException(); + } catch (CommandDoesNotExistException e) { + ui.printCommandDoesNotExist(); + return true; + } + } + } +} \ No newline at end of file diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java new file mode 100644 index 000000000..197f47877 --- /dev/null +++ b/src/main/java/duke/storage/Storage.java @@ -0,0 +1,97 @@ +package duke.storage; + +import duke.tasklist.TaskList; +import duke.tasklist.task.Task; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + + +public class Storage { + protected String fileName; + protected String fileDir; + public final ArrayList items = new ArrayList<>(); + + + public Storage(String fileName, String fileDir) { + this.fileDir = fileDir; + this.fileName = fileName; + } + + public void create() { + File saveFile = new File(fileName); + File saveDir = new File(fileDir); + if (saveDir.mkdirs()) { + System.out.println("Successfully created save dir"); + } else { + System.out.println("Save folder already exists."); + } + try { + if (saveFile.createNewFile()) { + System.out.println("Successfully created save file"); + } else { + System.out.println("Save file already exists"); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void save() { + Task[] saveLists = new Task[items.size()]; + items.toArray(saveLists); + try { + FileWriter fw = new FileWriter(fileName); + for (Task saveList : saveLists) { + fw.write(saveList.toString().charAt(1) + "/"); + fw.write(saveList.getStatus() + "/" + saveList.getDescription()); + if (!saveList.getDate().equals("empty")) { + fw.write("/" + saveList.getDate()); + } + fw.write("\n"); + } + System.out.println("Automatically saved to " + fileName); + fw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + public void load(TaskList task) throws FileNotFoundException { + File saveFile = new File(fileName); + Scanner s = new Scanner(saveFile); + while (s.hasNext()) { + String[] chars = s.nextLine().split("/"); + String type = chars[0].trim(); + String status = chars[1].trim(); + String description = chars[2].trim(); + switch (type) { + case "E": + String date = chars[3].trim(); + task.addEvent(items, description, date); + if (status.equals("1")) { + items.get(task.getTaskCount()).markDone(); + } + break; + case "D": + date = chars[3].trim(); + task.addDeadline(items, description, date); + if (status.equals("1")) { + items.get(task.getTaskCount()).markDone(); + } + break; + case "T": + task.addTodo(items, description); + break; + } + } + task.loadTaskCount(items); + System.out.println("Save file successfully loaded"); + } + +} diff --git a/src/main/java/duke/tasklist/TaskList.java b/src/main/java/duke/tasklist/TaskList.java new file mode 100644 index 000000000..b9c861dc4 --- /dev/null +++ b/src/main/java/duke/tasklist/TaskList.java @@ -0,0 +1,48 @@ +package duke.tasklist; +import duke.tasklist.task.Task; +import duke.tasklist.task.Deadlines; +import duke.tasklist.task.Todo; +import duke.tasklist.task.Events; +import duke.ui.Ui; +import java.util.ArrayList; + +public class TaskList { + + protected int taskCount; + + public TaskList(ArrayList items) { + loadTaskCount(items); + } + + public int getTaskCount() { + return taskCount; + } + + public void loadTaskCount(ArrayList items) { + this.taskCount = items.size(); + } + + public void addEvent(ArrayList items, String description, String time) { + Events newEvent = new Events(description, time); + items.add(taskCount, newEvent); + } + + public void addDeadline(ArrayList items, String description, String by) { + Deadlines newDeadline = new Deadlines(description, by); + items.add(taskCount, newDeadline); + } + + public void addTodo(ArrayList items, String description) { + Todo newTodo = new Todo(description); + items.add(taskCount, newTodo); + } + + public void removeItem(Ui ui, ArrayList items, int taskNum) { + taskCount--; + ui.removeTaskMessage(items.get(taskNum), taskCount); + items.remove(taskNum); + } + +} + + diff --git a/src/main/java/duke/task/Deadlines.java b/src/main/java/duke/tasklist/task/Deadlines.java similarity index 88% rename from src/main/java/duke/task/Deadlines.java rename to src/main/java/duke/tasklist/task/Deadlines.java index c978afe2a..b8f51e7b6 100644 --- a/src/main/java/duke/task/Deadlines.java +++ b/src/main/java/duke/tasklist/task/Deadlines.java @@ -1,4 +1,4 @@ -package duke.task; +package duke.tasklist.task; public class Deadlines extends Task { protected String by; diff --git a/src/main/java/duke/task/Events.java b/src/main/java/duke/tasklist/task/Events.java similarity index 88% rename from src/main/java/duke/task/Events.java rename to src/main/java/duke/tasklist/task/Events.java index 4937992a0..383613432 100644 --- a/src/main/java/duke/task/Events.java +++ b/src/main/java/duke/tasklist/task/Events.java @@ -1,4 +1,4 @@ -package duke.task; +package duke.tasklist.task; public class Events extends Task { protected String time; diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/tasklist/task/Task.java similarity index 91% rename from src/main/java/duke/task/Task.java rename to src/main/java/duke/tasklist/task/Task.java index 9ffad5ef9..293ec997d 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/tasklist/task/Task.java @@ -1,4 +1,4 @@ -package duke.task; +package duke.tasklist.task; public class Task { protected String description; diff --git a/src/main/java/duke/task/Todo.java b/src/main/java/duke/tasklist/task/Todo.java similarity index 82% rename from src/main/java/duke/task/Todo.java rename to src/main/java/duke/tasklist/task/Todo.java index 81fd3390d..01f22cc7e 100644 --- a/src/main/java/duke/task/Todo.java +++ b/src/main/java/duke/tasklist/task/Todo.java @@ -1,4 +1,4 @@ -package duke.task; +package duke.tasklist.task; public class Todo extends Task { diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java new file mode 100644 index 000000000..b03300644 --- /dev/null +++ b/src/main/java/duke/ui/Ui.java @@ -0,0 +1,74 @@ +package duke.ui; + +import duke.tasklist.TaskList; +import duke.tasklist.task.Task; + +import java.util.Scanner; + +public class Ui { + private static final String logo = + " _____ ___ _____ ______\n" + + "|___ | | | |_____| / / -- \\ \\ \n" + + " / / | | / / | | | | \n" + + " / / | | / / | | | |\n" + + " / /___ | | /_/__ | | -- | |\n" + + "|_____| | | |_____| \\ \\____/ /\n"; + public static final String border = "____________________________________________________________\n"; + + public void showLine() { + System.out.println(border); + } + + public void showError() { + System.out.println("Error occurred! Please try again."); + } + public void showSaveFileError() { + System.out.println("Could not find existing save file!"); + } + public void printStartMessage() { + System.out.println(logo); + System.out.println(border + "Hi bro, my name is Echo"); + System.out.println("What do you want?\n" + border); + System.out.println("Type bye to exit\n" + border); + } + public void printEndMessage() { + System.out.println(border); + System.out.println("chat again next time!\n" + border); + } + public void addTaskMessage(TaskList tasks, Task task) { + int taskCount = tasks.getTaskCount(); + System.out.println(border); + System.out.println("Got it. I've added this task:"); + System.out.println(task); + System.out.println("Now you have " + taskCount + " tasks in the list."); + System.out.println(border); + } + public void removeTaskMessage(Task task, int taskCount) { + System.out.println(border); + System.out.println("Noted. I've removed this task:"); + System.out.println(task); + System.out.println("Now you have " + taskCount + " tasks in the list."); + System.out.println(border); + } + public void printEmptyDescriptionError(String command) { + System.out.println(border); + System.out.println("\uD83D\uDE00 " + "OOPS!!! The description of a " + command + " cannot be empty."); + System.out.println(border); + } + public void printCommandDoesNotExist() { + System.out.println(border); + System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, but I don't know what that means :-("); + System.out.println(border); + } + public void printCommandIsInvalid() { + System.out.println(border); + System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, the command is invalid."); + System.out.println(border); + } + public String readCommand(Scanner in) { + return in.nextLine(); + } + public Ui() { + printStartMessage(); + } +} From ea440362f63a039122eac17c327d50462d1760f1 Mon Sep 17 00:00:00 2001 From: Edwin Date: Wed, 29 Sep 2021 11:50:01 +0800 Subject: [PATCH 21/28] Added Date and Time functionality --- data/save.txt | 1 + src/main/java/duke/Duke.java | 3 ++ src/main/java/duke/command/Command.java | 19 ++++++++++ .../exception/ShowDateIsEmptyException.java | 4 +++ src/main/java/duke/parser/Parser.java | 17 +++++++++ src/main/java/duke/storage/Storage.java | 8 ++--- .../java/duke/tasklist/task/Deadlines.java | 28 +++++++++++++-- src/main/java/duke/tasklist/task/Events.java | 35 ++++++++++++++++--- src/main/java/duke/tasklist/task/Task.java | 7 ++++ src/main/java/duke/ui/Ui.java | 7 ++++ 10 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 src/main/java/duke/exception/ShowDateIsEmptyException.java diff --git a/data/save.txt b/data/save.txt index e69de29bb..43fe8e7e5 100644 --- a/data/save.txt +++ b/data/save.txt @@ -0,0 +1 @@ +D|0|by|2010/12/12 1800 diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 7387996f4..40f6e6cb4 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -8,7 +8,10 @@ import duke.storage.Storage; import duke.tasklist.TaskList; import duke.ui.Ui; + +import javax.sound.midi.SysexMessage; import java.io.FileNotFoundException; +import java.util.Arrays; import java.util.Scanner; diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java index 971d8502a..5631c57bb 100644 --- a/src/main/java/duke/command/Command.java +++ b/src/main/java/duke/command/Command.java @@ -5,6 +5,10 @@ import duke.tasklist.task.Task; import duke.ui.Ui; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; + public class Command { private static final int CMD_NOT_FOUND = 0; private static final int CMD_TODO = 1; @@ -13,6 +17,7 @@ public class Command { private static final int CMD_LIST = 4; private static final int CMD_DONE = 5; private static final int CMD_DELETE = 6; + private static final int CMD_SHOW_DATE = 7; private static final int CMD_TERMINATE = 0; private static final String TODO = "todo"; private static final String EVENT = "event"; @@ -21,6 +26,7 @@ public class Command { private static final int INDEX_NUM_DELETE = 7; private static final String BY = "/by"; private static final String AT = "/at"; + private static final String SHOW_DATE = "show date"; protected int command; protected boolean isExit = false; @@ -76,6 +82,19 @@ public void execute(TaskList tasks, Ui ui, Storage storage) { } } break; + case CMD_SHOW_DATE: + int index = 1; + LocalDate deadline = LocalDate.parse(getUserInput().replace(SHOW_DATE, "").trim(), DateTimeFormatter.ofPattern("yyyy/MM/dd")); + System.out.println(Ui.border); + System.out.println("Here are the all the task in your list to be done by:" + deadline.format(DateTimeFormatter.ofPattern("dd MMM yyyy"))); + for (Task item : storage.items) { + if (item.getDeadline().isEqual(deadline)) { + System.out.print(index + "."); + System.out.println(item); + } + index++; + } + break; case CMD_DONE: int dividerPosition = userInput.indexOf(" ") + 1; int endPosition = userInput.length(); diff --git a/src/main/java/duke/exception/ShowDateIsEmptyException.java b/src/main/java/duke/exception/ShowDateIsEmptyException.java new file mode 100644 index 000000000..18d532507 --- /dev/null +++ b/src/main/java/duke/exception/ShowDateIsEmptyException.java @@ -0,0 +1,4 @@ +package duke.exception; + +public class ShowDateIsEmptyException extends Exception { +} diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 5fa6bfb4c..04bce0f5b 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -4,6 +4,7 @@ import duke.exception.CommandDoesNotExistException; import duke.exception.CommandInvalidException; import duke.exception.EmptyDescriptionException; +import duke.exception.ShowDateIsEmptyException; import duke.tasklist.TaskList; import duke.ui.Ui; @@ -13,6 +14,7 @@ public class Parser { private static final int LENGTH_OF_EVENT = 5; private static final int LENGTH_OF_DELETE = 6; private static final int LENGTH_OF_DEADLINE = 8; + private static final int LENGTH_OF_SHOW_DATE = 9; private static final int MIN_SPLIT_SIZE = 2; private static final int CMD_NOT_FOUND = -1; private static final int CMD_TODO = 1; @@ -21,8 +23,10 @@ public class Parser { private static final int CMD_LIST = 4; private static final int CMD_DONE = 5; private static final int CMD_DELETE = 6; + private static final int CMD_SHOW_DATE = 7; private static final int CMD_TERMINATE = 0; private static final String LIST = "list"; + private static final String SHOW_DATE = "show date"; private static final String TODO = "todo"; private static final String EVENT = "event"; private static final String DEADLINE = "deadline"; @@ -57,6 +61,8 @@ public static Command parse(String fullCommand) { c.setCommand(CMD_DEADLINE); } else if (fullCommand.length() >= LENGTH_OF_DELETE && fullCommand.substring(0, LENGTH_OF_DELETE).contains(DELETE)) { c.setCommand(CMD_DELETE); + } else if (fullCommand.length() >= LENGTH_OF_SHOW_DATE && fullCommand.substring(0, LENGTH_OF_SHOW_DATE).contains(SHOW_DATE)) { + c.setCommand(CMD_SHOW_DATE); } else if (fullCommand.matches(BYE)) { c.setCommand(CMD_TERMINATE); } else { @@ -100,6 +106,17 @@ public static boolean verifyCommand(TaskList task, Command c, Ui ui) { return true; } return false; + case CMD_SHOW_DATE: + try { + String key = userInput.replace(SHOW_DATE, ""); + if (key.isBlank()) { + throw new ShowDateIsEmptyException(); + } + } catch (ShowDateIsEmptyException e) { + ui.printEmptyDateError(); + return true; + } + return false; case CMD_DONE: try { if (isInvalid(task, userInput, DONE)) { diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index 197f47877..637818b9d 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -47,10 +47,10 @@ public void save() { try { FileWriter fw = new FileWriter(fileName); for (Task saveList : saveLists) { - fw.write(saveList.toString().charAt(1) + "/"); - fw.write(saveList.getStatus() + "/" + saveList.getDescription()); + fw.write(saveList.toString().charAt(1) + "|"); + fw.write(saveList.getStatus() + "|" + saveList.getDescription()); if (!saveList.getDate().equals("empty")) { - fw.write("/" + saveList.getDate()); + fw.write("|" + saveList.getDate()); } fw.write("\n"); } @@ -66,7 +66,7 @@ public void load(TaskList task) throws FileNotFoundException { File saveFile = new File(fileName); Scanner s = new Scanner(saveFile); while (s.hasNext()) { - String[] chars = s.nextLine().split("/"); + String[] chars = s.nextLine().split("\\|"); String type = chars[0].trim(); String status = chars[1].trim(); String description = chars[2].trim(); diff --git a/src/main/java/duke/tasklist/task/Deadlines.java b/src/main/java/duke/tasklist/task/Deadlines.java index b8f51e7b6..114d0e43c 100644 --- a/src/main/java/duke/tasklist/task/Deadlines.java +++ b/src/main/java/duke/tasklist/task/Deadlines.java @@ -1,19 +1,43 @@ package duke.tasklist.task; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; public class Deadlines extends Task { protected String by; + protected LocalDate date; + protected LocalTime time; public Deadlines(String description, String by) { super(description); this.by = by; + this.date = LocalDate.parse(by.split(" ")[0], DateTimeFormatter.ofPattern("yyyy/MM/dd")); + this.time = LocalTime.parse(readTime(by)); + + } + private String readTime(String str) { + String hour = str.split(" ")[1].substring(0, 2); + String colon = ":"; + String minutes = str.split(" ")[1].substring(2, 4); + return hour.concat(colon).concat(minutes); } @Override public String getDate() { return this.by; } - + private String formatDate(LocalDate date) { + return date.format(DateTimeFormatter.ofPattern("dd MMM yyyy")); + } + private String formatTime(LocalTime time) { + return time.format(DateTimeFormatter.ofPattern("hh mm")); + } + @Override + public LocalDate getDeadline() { + return this.date; + } public String toString() { - return "[D]" + super.toString() + " (by: " + by + ")"; + return "[D]" + super.toString() + " (by: " + formatDate(this.date) + " " + formatTime(this.time) + ")"; } } diff --git a/src/main/java/duke/tasklist/task/Events.java b/src/main/java/duke/tasklist/task/Events.java index 383613432..c0546d150 100644 --- a/src/main/java/duke/tasklist/task/Events.java +++ b/src/main/java/duke/tasklist/task/Events.java @@ -1,17 +1,42 @@ package duke.tasklist.task; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; + public class Events extends Task { - protected String time; + protected String at; + protected LocalDate date; + protected LocalTime time; - public Events(String description, String time) { + public Events(String description, String at) { super(description); - this.time = time; + this.at = at; + this.date = LocalDate.parse(at.split(" ")[0], DateTimeFormatter.ofPattern("yyyy/MM/dd")); + this.time = LocalTime.parse(readTime(at)); + } + private String readTime(String str) { + String hour = str.split(" ")[1].substring(0, 2); + String colon = ":"; + String minutes = str.split(" ")[1].substring(2, 4); + return hour.concat(colon).concat(minutes); } @Override public String getDate() { - return this.time; + return this.at; + } + private String formatDate(LocalDate date) { + return date.format(DateTimeFormatter.ofPattern("dd MMM yyyy")); + } + + private String formatTime(LocalTime time) { + return time.format(DateTimeFormatter.ofPattern("hh mm")); + } + @Override + public LocalDate getDeadline() { + return this.date; } public String toString() { - return "[E]" + super.toString() + " (at: " + time + ")"; + return "[E]" + super.toString() + " (at: " + formatDate(this.date) + " " + formatTime(this.time) + ")"; } } diff --git a/src/main/java/duke/tasklist/task/Task.java b/src/main/java/duke/tasklist/task/Task.java index 293ec997d..a28580c2e 100644 --- a/src/main/java/duke/tasklist/task/Task.java +++ b/src/main/java/duke/tasklist/task/Task.java @@ -1,5 +1,8 @@ package duke.tasklist.task; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + public class Task { protected String description; protected boolean isDone; @@ -23,6 +26,10 @@ public String getStatus() { public String getDate() { return "empty"; } + + public LocalDate getDeadline() { + return null; + } public void markDone() { this.isDone = true; diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index b03300644..4cf4726f8 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -65,6 +65,13 @@ public void printCommandIsInvalid() { System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, the command is invalid."); System.out.println(border); } + public void printEmptyDateError() { + System.out.println(border); + System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, please input the date you would like to search for " + + "in yyyy/mm/dd format" ); + System.out.println(border); + + } public String readCommand(Scanner in) { return in.nextLine(); } From a2984d60e889c97e7233a996376dc5b394560670 Mon Sep 17 00:00:00 2001 From: Edwin Date: Wed, 29 Sep 2021 12:15:08 +0800 Subject: [PATCH 22/28] Added Find Feature --- data/save.txt | 4 +++- src/main/java/duke/command/Command.java | 17 ++++++++++++++- .../duke/exception/FindIsEmptyException.java | 4 ++++ src/main/java/duke/parser/Parser.java | 21 +++++++++++++++---- src/main/java/duke/ui/Ui.java | 7 ++++++- 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 src/main/java/duke/exception/FindIsEmptyException.java diff --git a/data/save.txt b/data/save.txt index 43fe8e7e5..952bebb39 100644 --- a/data/save.txt +++ b/data/save.txt @@ -1 +1,3 @@ -D|0|by|2010/12/12 1800 +T|0|read book +D|0|write book|1999/03/12 1200 +E|0|collect book|1999/03/13 1300 diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java index 5631c57bb..083bb37a4 100644 --- a/src/main/java/duke/command/Command.java +++ b/src/main/java/duke/command/Command.java @@ -18,6 +18,7 @@ public class Command { private static final int CMD_DONE = 5; private static final int CMD_DELETE = 6; private static final int CMD_SHOW_DATE = 7; + private static final int CMD_FIND = 8; private static final int CMD_TERMINATE = 0; private static final String TODO = "todo"; private static final String EVENT = "event"; @@ -27,6 +28,7 @@ public class Command { private static final String BY = "/by"; private static final String AT = "/at"; private static final String SHOW_DATE = "show date"; + private static final String FIND = "find"; protected int command; protected boolean isExit = false; @@ -91,8 +93,21 @@ public void execute(TaskList tasks, Ui ui, Storage storage) { if (item.getDeadline().isEqual(deadline)) { System.out.print(index + "."); System.out.println(item); + index++; + } + } + break; + case CMD_FIND: + index = 1; + String key = userInput.replace(FIND, "").trim(); + System.out.println(Ui.border); + System.out.println("Here are the all the task in your list with keyword: " + key); + for (Task item : storage.items) { + if (item.getDescription().contains(key)) { + System.out.print(index + "."); + System.out.println(item); + index++; } - index++; } break; case CMD_DONE: diff --git a/src/main/java/duke/exception/FindIsEmptyException.java b/src/main/java/duke/exception/FindIsEmptyException.java new file mode 100644 index 000000000..04859def0 --- /dev/null +++ b/src/main/java/duke/exception/FindIsEmptyException.java @@ -0,0 +1,4 @@ +package duke.exception; + +public class FindIsEmptyException extends Exception { +} diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 04bce0f5b..fe1f7db6f 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -1,16 +1,14 @@ package duke.parser; import duke.command.Command; -import duke.exception.CommandDoesNotExistException; -import duke.exception.CommandInvalidException; -import duke.exception.EmptyDescriptionException; -import duke.exception.ShowDateIsEmptyException; +import duke.exception.*; import duke.tasklist.TaskList; import duke.ui.Ui; public class Parser { private static final int LENGTH_OF_DONE = 4; private static final int LENGTH_OF_TODO = 4; + private static final int LENGTH_OF_FIND = 4; private static final int LENGTH_OF_EVENT = 5; private static final int LENGTH_OF_DELETE = 6; private static final int LENGTH_OF_DEADLINE = 8; @@ -24,6 +22,7 @@ public class Parser { private static final int CMD_DONE = 5; private static final int CMD_DELETE = 6; private static final int CMD_SHOW_DATE = 7; + private static final int CMD_FIND = 8; private static final int CMD_TERMINATE = 0; private static final String LIST = "list"; private static final String SHOW_DATE = "show date"; @@ -31,6 +30,7 @@ public class Parser { private static final String EVENT = "event"; private static final String DEADLINE = "deadline"; private static final String DONE = "done"; + private static final String FIND = "find"; private static final String BYE = "bye"; private static final String DELETE = "delete"; private static final String BY = "/by"; @@ -63,6 +63,8 @@ public static Command parse(String fullCommand) { c.setCommand(CMD_DELETE); } else if (fullCommand.length() >= LENGTH_OF_SHOW_DATE && fullCommand.substring(0, LENGTH_OF_SHOW_DATE).contains(SHOW_DATE)) { c.setCommand(CMD_SHOW_DATE); + } else if (fullCommand.length() >= LENGTH_OF_FIND && fullCommand.substring(0, LENGTH_OF_FIND).contains(FIND)) { + c.setCommand(CMD_FIND); } else if (fullCommand.matches(BYE)) { c.setCommand(CMD_TERMINATE); } else { @@ -117,6 +119,17 @@ public static boolean verifyCommand(TaskList task, Command c, Ui ui) { return true; } return false; + case CMD_FIND: + try { + String key = userInput.replace(FIND, ""); + if (key.isBlank()) { + throw new FindIsEmptyException(); + } + } catch (FindIsEmptyException e) { + ui.printFindFieldEmpty(); + return true; + } + return false; case CMD_DONE: try { if (isInvalid(task, userInput, DONE)) { diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 4cf4726f8..0ae65138b 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -70,7 +70,12 @@ public void printEmptyDateError() { System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, please input the date you would like to search for " + "in yyyy/mm/dd format" ); System.out.println(border); - + } + public void printFindFieldEmpty() { + System.out.println(border); + System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, please input a keyword for the task " + + "you would like to search for"); + System.out.println(border); } public String readCommand(Scanner in) { return in.nextLine(); From 8a47b700fa764bdf865afc3ea61649a936fcb95f Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 30 Sep 2021 01:23:12 +0800 Subject: [PATCH 23/28] Documentation for A-JavaDoc --- src/main/java/duke/Duke.java | 10 ++--- src/main/java/duke/command/AddCommand.java | 4 ++ src/main/java/duke/command/Command.java | 15 ++++--- src/main/java/duke/command/DeleteCommand.java | 4 ++ src/main/java/duke/command/FindCommand.java | 4 ++ src/main/java/duke/command/ListCommand.java | 4 ++ .../java/duke/command/ShowDateCommand.java | 4 ++ .../exception/DateDoesNotExistException.java | 4 ++ .../exception/WrongDateFormatException.java | 4 ++ src/main/java/duke/parser/Parser.java | 42 +++++++++++++++++-- src/main/java/duke/storage/Storage.java | 14 ++++++- src/main/java/duke/tasklist/TaskList.java | 25 ++++++++++- .../java/duke/tasklist/task/Deadlines.java | 11 +++-- src/main/java/duke/tasklist/task/Events.java | 10 ++++- src/main/java/duke/tasklist/task/Task.java | 3 +- src/main/java/duke/ui/Ui.java | 12 +++++- 16 files changed, 141 insertions(+), 29 deletions(-) create mode 100644 src/main/java/duke/command/AddCommand.java create mode 100644 src/main/java/duke/command/DeleteCommand.java create mode 100644 src/main/java/duke/command/FindCommand.java create mode 100644 src/main/java/duke/command/ListCommand.java create mode 100644 src/main/java/duke/command/ShowDateCommand.java create mode 100644 src/main/java/duke/exception/DateDoesNotExistException.java create mode 100644 src/main/java/duke/exception/WrongDateFormatException.java diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 40f6e6cb4..579a5841b 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,17 +1,13 @@ package duke; import duke.command.Command; -import duke.exception.CommandDoesNotExistException; import duke.exception.DukeException; -import duke.exception.EmptyDescriptionException; import duke.parser.Parser; import duke.storage.Storage; import duke.tasklist.TaskList; import duke.ui.Ui; - -import javax.sound.midi.SysexMessage; import java.io.FileNotFoundException; -import java.util.Arrays; +import java.util.Locale; import java.util.Scanner; @@ -35,12 +31,12 @@ public Duke(String fileName, String fileDir) { task = new TaskList(storage.items); } } - public void run() { + private void run() { Scanner in = new Scanner(System.in); boolean isExit = false; while (!isExit) { try { - String fullCommand = ui.readCommand(in); + String fullCommand = ui.readCommand(in).toLowerCase(Locale.ROOT); Command c = Parser.parse(fullCommand); boolean hasError = Parser.verifyCommand(task, c, ui); if (hasError) { diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java new file mode 100644 index 000000000..efc3aaa5e --- /dev/null +++ b/src/main/java/duke/command/AddCommand.java @@ -0,0 +1,4 @@ +package duke.command; + +public class AddCommand { +} diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java index 083bb37a4..065fcaeeb 100644 --- a/src/main/java/duke/command/Command.java +++ b/src/main/java/duke/command/Command.java @@ -1,13 +1,10 @@ package duke.command; - import duke.storage.Storage; import duke.tasklist.TaskList; import duke.tasklist.task.Task; import duke.ui.Ui; - import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.util.Arrays; public class Command { private static final int CMD_NOT_FOUND = 0; @@ -50,7 +47,13 @@ public String getUserInput() { public int getCommand() { return this.command; } - + /** + * Executes the command based on the command type + * + * @param tasks An object that contains the list of tasks + * @param storage An object to allow saving and loading of the list of tasks + * @param ui An object to interacts with the user + */ public void execute(TaskList tasks, Ui ui, Storage storage) { int taskCount = tasks.getTaskCount(); String[] userInputs; @@ -86,11 +89,11 @@ public void execute(TaskList tasks, Ui ui, Storage storage) { break; case CMD_SHOW_DATE: int index = 1; - LocalDate deadline = LocalDate.parse(getUserInput().replace(SHOW_DATE, "").trim(), DateTimeFormatter.ofPattern("yyyy/MM/dd")); + LocalDate deadline = LocalDate.parse(userInput.replace(SHOW_DATE, "").trim(), DateTimeFormatter.ofPattern("yyyy/MM/dd")); System.out.println(Ui.border); System.out.println("Here are the all the task in your list to be done by:" + deadline.format(DateTimeFormatter.ofPattern("dd MMM yyyy"))); for (Task item : storage.items) { - if (item.getDeadline().isEqual(deadline)) { + if (!(item.getDate().contains("empty")) && item.getDeadline().isEqual(deadline)) { System.out.print(index + "."); System.out.println(item); index++; diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java new file mode 100644 index 000000000..38b5d9e01 --- /dev/null +++ b/src/main/java/duke/command/DeleteCommand.java @@ -0,0 +1,4 @@ +package duke.command; + +public class DeleteCommand { +} diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java new file mode 100644 index 000000000..e45b2d3a6 --- /dev/null +++ b/src/main/java/duke/command/FindCommand.java @@ -0,0 +1,4 @@ +package duke.command; + +public class FindCommand { +} diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java new file mode 100644 index 000000000..74e12fc53 --- /dev/null +++ b/src/main/java/duke/command/ListCommand.java @@ -0,0 +1,4 @@ +package duke.command; + +public class ListCommand { +} diff --git a/src/main/java/duke/command/ShowDateCommand.java b/src/main/java/duke/command/ShowDateCommand.java new file mode 100644 index 000000000..acc9bd6ec --- /dev/null +++ b/src/main/java/duke/command/ShowDateCommand.java @@ -0,0 +1,4 @@ +package duke.command; + +public class ShowDateCommand { +} diff --git a/src/main/java/duke/exception/DateDoesNotExistException.java b/src/main/java/duke/exception/DateDoesNotExistException.java new file mode 100644 index 000000000..28918e340 --- /dev/null +++ b/src/main/java/duke/exception/DateDoesNotExistException.java @@ -0,0 +1,4 @@ +package duke.exception; + +public class DateDoesNotExistException extends Exception { +} diff --git a/src/main/java/duke/exception/WrongDateFormatException.java b/src/main/java/duke/exception/WrongDateFormatException.java new file mode 100644 index 000000000..13458314a --- /dev/null +++ b/src/main/java/duke/exception/WrongDateFormatException.java @@ -0,0 +1,4 @@ +package duke.exception; + +public class WrongDateFormatException extends Exception { +} diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index fe1f7db6f..773bc5f88 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -9,10 +9,13 @@ public class Parser { private static final int LENGTH_OF_DONE = 4; private static final int LENGTH_OF_TODO = 4; private static final int LENGTH_OF_FIND = 4; + private static final int LEN_OF_YEAR = 4; private static final int LENGTH_OF_EVENT = 5; private static final int LENGTH_OF_DELETE = 6; private static final int LENGTH_OF_DEADLINE = 8; private static final int LENGTH_OF_SHOW_DATE = 9; + private static final int TOTAL_NUM_MONTH = 12; + private static final int TOTAL_NUM_DAY = 31; private static final int MIN_SPLIT_SIZE = 2; private static final int CMD_NOT_FOUND = -1; private static final int CMD_TODO = 1; @@ -36,7 +39,7 @@ public class Parser { private static final String BY = "/by"; private static final String AT = "/at"; - public static boolean isInvalid(TaskList task, String line, String key) { + private static boolean isInvalid(TaskList task, String line, String key) { if (!line.split(key)[1].trim().isEmpty()) { if (Integer.parseInt(line.split(key)[1].trim()) > task.getTaskCount()) { return true; @@ -45,6 +48,12 @@ public static boolean isInvalid(TaskList task, String line, String key) { return (line.length() <= LENGTH_OF_EVENT); } + /** + * Extracts the command parameter from the user input + * + * @param fullCommand Input provided by user + * @return Command object of the given command + */ public static Command parse(String fullCommand) { Command c; c = new Command(); @@ -73,6 +82,14 @@ public static Command parse(String fullCommand) { return c; } + /** + * Verify the if command is valid + * + * @param task List of task so far + * @param c Command object to access and execute the command + * @param ui Ui object to interact with the user + * @return boolean value true if command is valid + */ public static boolean verifyCommand(TaskList task, Command c, Ui ui) { String userInput = c.getUserInput(); switch (c.getCommand()) { @@ -109,8 +126,8 @@ public static boolean verifyCommand(TaskList task, Command c, Ui ui) { } return false; case CMD_SHOW_DATE: + String key = userInput.replace(SHOW_DATE, ""); try { - String key = userInput.replace(SHOW_DATE, ""); if (key.isBlank()) { throw new ShowDateIsEmptyException(); } @@ -118,10 +135,29 @@ public static boolean verifyCommand(TaskList task, Command c, Ui ui) { ui.printEmptyDateError(); return true; } + try { + if (Integer.parseInt(key.split("/")[1].trim()) > TOTAL_NUM_MONTH) { + throw new WrongDateFormatException(); + } + if (Integer.parseInt(key.split("/")[2].trim()) > TOTAL_NUM_DAY) { + throw new WrongDateFormatException(); + } + } catch (WrongDateFormatException e) { + ui.printWrongDateFormatError(); + return true; + } + try { + if (key.split("/")[0].trim().length() < LEN_OF_YEAR) { + throw new WrongDateFormatException(); + } + } catch (WrongDateFormatException e) { + ui.printWrongDateFormatError(); + return true; + } return false; case CMD_FIND: try { - String key = userInput.replace(FIND, ""); + key = userInput.replace(FIND, ""); if (key.isBlank()) { throw new FindIsEmptyException(); } diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index 637818b9d..01312f685 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -2,7 +2,6 @@ import duke.tasklist.TaskList; import duke.tasklist.task.Task; - import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; @@ -22,6 +21,10 @@ public Storage(String fileName, String fileDir) { this.fileName = fileName; } + /** + * Check and create save folder and save file if it does not exist + * + */ public void create() { File saveFile = new File(fileName); File saveDir = new File(fileDir); @@ -41,6 +44,10 @@ public void create() { } } + /** + * Save the current list of tasks to save file + * + */ public void save() { Task[] saveLists = new Task[items.size()]; items.toArray(saveLists); @@ -61,7 +68,10 @@ public void save() { } } - + /** + * Loads the tasks list from save file + * + */ public void load(TaskList task) throws FileNotFoundException { File saveFile = new File(fileName); Scanner s = new Scanner(saveFile); diff --git a/src/main/java/duke/tasklist/TaskList.java b/src/main/java/duke/tasklist/TaskList.java index b9c861dc4..c0e895efa 100644 --- a/src/main/java/duke/tasklist/TaskList.java +++ b/src/main/java/duke/tasklist/TaskList.java @@ -22,16 +22,37 @@ public void loadTaskCount(ArrayList items) { this.taskCount = items.size(); } - public void addEvent(ArrayList items, String description, String time) { - Events newEvent = new Events(description, time); + /** + * Create a new Event object and add it to the tasks list + * + * @param items list of items stored in an ArrayList + * @param description task description input by user + * @param at the deadline for the event input by the user + */ + public void addEvent(ArrayList items, String description, String at) { + Events newEvent = new Events(description, at); items.add(taskCount, newEvent); } + /** + * Create a new Deadline object and add it to the tasks list + * + * @param items list of items stored in an ArrayList + * @param description task description input by user + * @param by the date for the deadline input by the user + */ public void addDeadline(ArrayList items, String description, String by) { Deadlines newDeadline = new Deadlines(description, by); items.add(taskCount, newDeadline); } + /** + * Create a new Todo object and add it to the tasks list + * + * @param items list of items stored in an ArrayList + * @param description task description input by user + * + */ public void addTodo(ArrayList items, String description) { Todo newTodo = new Todo(description); items.add(taskCount, newTodo); diff --git a/src/main/java/duke/tasklist/task/Deadlines.java b/src/main/java/duke/tasklist/task/Deadlines.java index 114d0e43c..8e7e43990 100644 --- a/src/main/java/duke/tasklist/task/Deadlines.java +++ b/src/main/java/duke/tasklist/task/Deadlines.java @@ -2,7 +2,6 @@ import java.time.LocalDate; import java.time.LocalTime; import java.time.format.DateTimeFormatter; -import java.time.format.FormatStyle; public class Deadlines extends Task { protected String by; @@ -16,6 +15,12 @@ public Deadlines(String description, String by) { this.time = LocalTime.parse(readTime(by)); } + /** + * Reformat the time parameter input to one that is readable by the library + * + * @param str is the string input by the user after '/by' + * @return a string that is reformatted to the right format for LocalTime.parse() + */ private String readTime(String str) { String hour = str.split(" ")[1].substring(0, 2); String colon = ":"; @@ -30,14 +35,14 @@ private String formatDate(LocalDate date) { return date.format(DateTimeFormatter.ofPattern("dd MMM yyyy")); } private String formatTime(LocalTime time) { - return time.format(DateTimeFormatter.ofPattern("hh mm")); + return time.format(DateTimeFormatter.ofPattern("hh mm a")); } @Override public LocalDate getDeadline() { return this.date; } public String toString() { - return "[D]" + super.toString() + " (by: " + formatDate(this.date) + " " + formatTime(this.time) + ")"; + return "[D]" + super.toString() + " (by: " + formatDate(this.date) + ", " + formatTime(this.time) + ")"; } } diff --git a/src/main/java/duke/tasklist/task/Events.java b/src/main/java/duke/tasklist/task/Events.java index c0546d150..34f11748b 100644 --- a/src/main/java/duke/tasklist/task/Events.java +++ b/src/main/java/duke/tasklist/task/Events.java @@ -15,6 +15,12 @@ public Events(String description, String at) { this.date = LocalDate.parse(at.split(" ")[0], DateTimeFormatter.ofPattern("yyyy/MM/dd")); this.time = LocalTime.parse(readTime(at)); } + /** + * Reformat the time parameter input to one that is readable by the library + * + * @param str is the string input by the user after '/at' + * @return a string that is reformatted to the right format for LocalTime.parse() + */ private String readTime(String str) { String hour = str.split(" ")[1].substring(0, 2); String colon = ":"; @@ -30,13 +36,13 @@ private String formatDate(LocalDate date) { } private String formatTime(LocalTime time) { - return time.format(DateTimeFormatter.ofPattern("hh mm")); + return time.format(DateTimeFormatter.ofPattern("hh mm a")); } @Override public LocalDate getDeadline() { return this.date; } public String toString() { - return "[E]" + super.toString() + " (at: " + formatDate(this.date) + " " + formatTime(this.time) + ")"; + return "[E]" + super.toString() + " (at: " + formatDate(this.date) + ", " + formatTime(this.time) + ")"; } } diff --git a/src/main/java/duke/tasklist/task/Task.java b/src/main/java/duke/tasklist/task/Task.java index a28580c2e..997df076c 100644 --- a/src/main/java/duke/tasklist/task/Task.java +++ b/src/main/java/duke/tasklist/task/Task.java @@ -1,7 +1,6 @@ package duke.tasklist.task; import java.time.LocalDate; -import java.time.format.DateTimeFormatter; public class Task { protected String description; @@ -14,7 +13,7 @@ public Task(String description) { public String getStatusIcon() { - return (isDone ? "X" : " "); // mark done task with X + return (isDone ? "X" : " "); } public String getDescription() { diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 0ae65138b..ea5971e69 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -2,7 +2,6 @@ import duke.tasklist.TaskList; import duke.tasklist.task.Task; - import java.util.Scanner; public class Ui { @@ -15,10 +14,13 @@ public class Ui { + "|_____| | | |_____| \\ \\____/ /\n"; public static final String border = "____________________________________________________________\n"; + /** + * Check and create save folder and save file if it does not exist + * + */ public void showLine() { System.out.println(border); } - public void showError() { System.out.println("Error occurred! Please try again."); } @@ -77,6 +79,12 @@ public void printFindFieldEmpty() { "you would like to search for"); System.out.println(border); } + public void printWrongDateFormatError() { + System.out.println(border); + System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, please input the correct date format: " + + "yyyy/dd/mm"); + System.out.println(border); + } public String readCommand(Scanner in) { return in.nextLine(); } From d3eb8f06f5030b42090b2051f75dc057131ac351 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 30 Sep 2021 01:35:46 +0800 Subject: [PATCH 24/28] Updated JavaDoc --- src/main/java/duke/Duke.java | 6 ++++++ src/main/java/duke/storage/Storage.java | 4 ++++ src/main/java/duke/tasklist/TaskList.java | 3 +++ src/main/java/duke/tasklist/task/Deadlines.java | 4 ++++ src/main/java/duke/tasklist/task/Events.java | 4 ++++ src/main/java/duke/tasklist/task/Task.java | 3 +++ src/main/java/duke/tasklist/task/Todo.java | 3 +++ 7 files changed, 27 insertions(+) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 579a5841b..217c7bea9 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -19,6 +19,12 @@ public class Duke { private final Ui ui; + /** + * Initialise all classes and load save file if it exists otherwise create a new save file + * + * @param fileName The name of the save file + * @param fileDir The directory of the save file + */ public Duke(String fileName, String fileDir) { storage = new Storage(fileName, fileDir); ui = new Ui(); diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index 01312f685..71c45d63b 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -16,6 +16,10 @@ public class Storage { public final ArrayList items = new ArrayList<>(); + /** + * @param fileDir the directory of the save file + * @param fileName the name of the save file + */ public Storage(String fileName, String fileDir) { this.fileDir = fileDir; this.fileName = fileName; diff --git a/src/main/java/duke/tasklist/TaskList.java b/src/main/java/duke/tasklist/TaskList.java index c0e895efa..1558c8b92 100644 --- a/src/main/java/duke/tasklist/TaskList.java +++ b/src/main/java/duke/tasklist/TaskList.java @@ -10,6 +10,9 @@ public class TaskList { protected int taskCount; + /** + * @param items The list of tasks stored in an ArrayList + */ public TaskList(ArrayList items) { loadTaskCount(items); } diff --git a/src/main/java/duke/tasklist/task/Deadlines.java b/src/main/java/duke/tasklist/task/Deadlines.java index 8e7e43990..883055da4 100644 --- a/src/main/java/duke/tasklist/task/Deadlines.java +++ b/src/main/java/duke/tasklist/task/Deadlines.java @@ -8,6 +8,10 @@ public class Deadlines extends Task { protected LocalDate date; protected LocalTime time; + /** + * @param description The description of the task given by the user + * @param by the deadline given by the user for the task + */ public Deadlines(String description, String by) { super(description); this.by = by; diff --git a/src/main/java/duke/tasklist/task/Events.java b/src/main/java/duke/tasklist/task/Events.java index 34f11748b..843c55485 100644 --- a/src/main/java/duke/tasklist/task/Events.java +++ b/src/main/java/duke/tasklist/task/Events.java @@ -9,6 +9,10 @@ public class Events extends Task { protected LocalDate date; protected LocalTime time; + /** + * @param description The description of the task given by the user + * @param at the deadline given by the user for the task + */ public Events(String description, String at) { super(description); this.at = at; diff --git a/src/main/java/duke/tasklist/task/Task.java b/src/main/java/duke/tasklist/task/Task.java index 997df076c..177ddb176 100644 --- a/src/main/java/duke/tasklist/task/Task.java +++ b/src/main/java/duke/tasklist/task/Task.java @@ -6,6 +6,9 @@ public class Task { protected String description; protected boolean isDone; + /** + * @param description The description of the task given by the user + */ public Task(String description) { this.description = description; this.isDone = false; diff --git a/src/main/java/duke/tasklist/task/Todo.java b/src/main/java/duke/tasklist/task/Todo.java index 01f22cc7e..ee004a042 100644 --- a/src/main/java/duke/tasklist/task/Todo.java +++ b/src/main/java/duke/tasklist/task/Todo.java @@ -2,6 +2,9 @@ public class Todo extends Task { + /** + * @param description The description of the task given by the user + */ public Todo(String description) { super(description); } From 468e2f1e77c802c0b72ca54d3057d67c762b1df5 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 30 Sep 2021 22:26:58 +0800 Subject: [PATCH 25/28] Completed User Guide --- data/save.txt | 3 +- docs/README.md | 205 ++++++++++++++++++++++++++++++++-- src/main/java/duke/ui/Ui.java | 2 +- 3 files changed, 196 insertions(+), 14 deletions(-) diff --git a/data/save.txt b/data/save.txt index 952bebb39..240bba63e 100644 --- a/data/save.txt +++ b/data/save.txt @@ -1,3 +1,2 @@ T|0|read book -D|0|write book|1999/03/12 1200 -E|0|collect book|1999/03/13 1300 +D|1|write book|1999/03/12 1200 diff --git a/docs/README.md b/docs/README.md index 8077118eb..16e3bd60b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,212 @@ # User Guide -## Features -### Feature-ABC + |___ | | | |_____| / / -- \ \ + / / | | / / | | | | + / / | | / / | | | | + / /___ | | /_/__ | | -- | | + |_____| | | |_____| \ \____/ / -Description of the feature. +Zizo is a desktop app for managing tasks, optimized for use via a Command Line Interface (CLI). +If you can type fast, Zizo can get your task management done faster than traditional GUI apps. +## Features + private static final int CMD_FIND = 8; + private static final int CMD_TERMINATE = 0; +### Add Todo -### Feature-XYZ +Adds a **Todo** task -Description of the feature. +### Add Event -## Usage +Adds an **Event** task + +### Add Deadline + +Adds a **Deadline** task + +### List + +Show all the tasks in a ordered list +### Done + +Mark a specific task as completed +### Delete + +Remove a specific task from the tasks list +### Show Date + +Show all tasks with the given deadline +### Find + +Search for a tasks given a keyword +### Terminate + +Ends the program -### `Keyword` - Describe action +## Usage -Describe the action and its outcome. +### `todo` - Adds a todo task to the tasks list Example of usage: -`keyword (optional arguments)` +`todo Read a book` + + +###Expected output +``` +____________________________________________________________ + +Got it. I've added this task: +[T][ ]Read a book +Now you have 1 tasks in the list. +____________________________________________________________ +``` +### `deadline` - Adds a deadline task to the tasks list + + +Example of usage: + +`deadline Read a book /by 2021/09/25 1800` + + +###Expected output +``` +____________________________________________________________ + +Got it. I've added this task: +[D][ ]Read a book (by: 25 Sep 2021, 6 00 PM) +Now you have 1 tasks in the list. +____________________________________________________________ +``` +### `event` - Adds a event task to the tasks list + + +Example of usage: + +`event Read a book /at 2021/09/25 1800` + +###Expected output +``` +____________________________________________________________ + +Got it. I've added this task: +[E][ ]Read a book (by: 25 Sep 2021, 6 00 PM) +Now you have 1 tasks in the list. +____________________________________________________________ +``` +### `list` - Shows a list of tasks + + +Example of usage: + +`list` + + +###Expected output +``` +____________________________________________________________ + +Here are the task in your list: +1.[D][ ]Read a book (by: 25 Sep 2021, 6 00 PM) +____________________________________________________________ +``` +### `done` - Mark a task as done + +Selects which task base on its index in the list to mark as done + +Format: done (Task index) +* Index must be a positive integer + +Example of usage: + +`done 1` Expected outcome: +``` + Here are the task in your list: +1. [D][X]Read a book (by: 25 Sep 2021, 6 00 PM) +``` + +###Expected output +``` +____________________________________________________________ + +Nice! task is done +____________________________________________________________ +``` + +### `delete` - Delete task + +Selects which task to delete based on its index in the tasks list + +Format: delete (Task Index) +* Index must be a positive integer + + +Example of usage: + +`delete 1` + + +###Expected output +``` +____________________________________________________________ +Noted. I've removed this task: +[D][X]Read a book (by: 25 Sep 2021, 6 00 PM) +Now you have 0 tasks in the list. +____________________________________________________________ +``` + +### `show date` Show tasks based on date + +Show tasks with the specific deadline + +Example of usage: + +`show date (date in yyyy/MM/dd format)` + +`show date 2012/12/12` + + +###Expected output +``` +____________________________________________________________ +Here are the all the task in your list to be done by:13 Mar 1999 +[E][X] collect book (at: 13 Mar 1999, 01 00 PM) +____________________________________________________________ +``` + +### `find` - Search for a task + +Show tasks with the specific deadline + +Example of usage: + +`find (keyword of task)` + +`find book` + +###Expected output +``` +____________________________________________________________ +Here are the all the task in your list with keyword: book +1.[D][ ] write book (by: 12 Mar 1999, 12 00 PM) +2.[T][ ] read book + +____________________________________________________________ +____________________________________________ +``` +### `Bye` - Ends the program + +End the program + +Example of usage: -Description of the outcome. +`bye` +###Expected output ``` -expected output +____________________________________________________________ +chat again next time! +____________________________________________________________ ``` diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index ea5971e69..b957b706b 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -29,7 +29,7 @@ public void showSaveFileError() { } public void printStartMessage() { System.out.println(logo); - System.out.println(border + "Hi bro, my name is Echo"); + System.out.println(border + "Hi bro, my name is Zizo"); System.out.println("What do you want?\n" + border); System.out.println("Type bye to exit\n" + border); } From 9c9496a41861881b674525d5a2bbf63c110df760 Mon Sep 17 00:00:00 2001 From: yzhedwin <69493564+yzhedwin@users.noreply.github.com> Date: Thu, 30 Sep 2021 22:29:07 +0800 Subject: [PATCH 26/28] Set theme jekyll-theme-cayman --- docs/_config.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/_config.yml diff --git a/docs/_config.yml b/docs/_config.yml new file mode 100644 index 000000000..c4192631f --- /dev/null +++ b/docs/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-cayman \ No newline at end of file From 0f9c8b7a85e926a683b0a233dd446936a8c4e611 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 30 Sep 2021 22:31:40 +0800 Subject: [PATCH 27/28] Completed User Guide --- docs/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 16e3bd60b..c6b83c7f6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -10,8 +10,7 @@ Zizo is a desktop app for managing tasks, optimized for use via a Command Line Interface (CLI). If you can type fast, Zizo can get your task management done faster than traditional GUI apps. ## Features - private static final int CMD_FIND = 8; - private static final int CMD_TERMINATE = 0; + ### Add Todo Adds a **Todo** task From e8616f6aee0aa09243b883593140e2bb1eddea43 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 30 Sep 2021 22:41:16 +0800 Subject: [PATCH 28/28] Build ip.jar --- data/save.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/save.txt b/data/save.txt index 240bba63e..fd7182a7d 100644 --- a/data/save.txt +++ b/data/save.txt @@ -1,2 +1,2 @@ -T|0|read book D|1|write book|1999/03/12 1200 +T|0|read book