From a05464fcf0108070fe05a77c24c723ff7581e863 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Tue, 24 Aug 2021 14:42:36 +0800 Subject: [PATCH 01/25] Level 0: Greet --- src/main/java/Greet.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/java/Greet.java diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java new file mode 100644 index 000000000..a2ee65191 --- /dev/null +++ b/src/main/java/Greet.java @@ -0,0 +1,6 @@ +public class Greet { + public static void main (String[] args) { + System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); + System.out.println("Bye. Hope to see you again soon!"); + } +} \ No newline at end of file From efd9e5bc401cb761aa42cc5f85d28ca235fcf735 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 26 Aug 2021 00:25:38 +0800 Subject: [PATCH 02/25] Level-1 --- src/main/java/Greet.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java index a2ee65191..60f8ea3f4 100644 --- a/src/main/java/Greet.java +++ b/src/main/java/Greet.java @@ -1,6 +1,21 @@ +import java.util.Arrays; +import java.util.Scanner; + public class Greet { public static void main (String[] args) { - System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); + System.out.println("Hello! I'm Duke"); + String line; + Scanner in = new Scanner(System.in); + System.out.println("What can I do for you?"); + line = in.nextLine(); + boolean bool1 = false; + while (!bool1) { + System.out.println(line); + line = in.nextLine(); + if (line.contains("bye")) { + bool1 = true; + } + } System.out.println("Bye. Hope to see you again soon!"); } } \ No newline at end of file From 99d6469029ae643e13f890c148c98798fe6edbb4 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 26 Aug 2021 00:42:33 +0800 Subject: [PATCH 03/25] Level-2 --- src/main/java/Greet.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java index 60f8ea3f4..e056d41cf 100644 --- a/src/main/java/Greet.java +++ b/src/main/java/Greet.java @@ -3,6 +3,8 @@ public class Greet { public static void main (String[] args) { + String[] tasks = new String[100]; + int ntasks = 0; System.out.println("Hello! I'm Duke"); String line; Scanner in = new Scanner(System.in); @@ -10,8 +12,15 @@ public static void main (String[] args) { line = in.nextLine(); boolean bool1 = false; while (!bool1) { - System.out.println(line); + System.out.println("added: " + line); + tasks[ntasks]= line; + ntasks += 1; line = in.nextLine(); + if (line.equals("list")) { + for (int i = 0; i < ntasks; i += 1) { + System.out.println((i+1) + ". " + tasks[i]); + } + } if (line.contains("bye")) { bool1 = true; } From 1814bd1edf5636476a63a7c6d0167a9fa4dc6857 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 26 Aug 2021 10:28:33 +0800 Subject: [PATCH 04/25] Level-3 --- src/main/java/Greet.java | 27 ++++++++++++++++----------- src/main/java/Task.java | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java index e056d41cf..c83fac83e 100644 --- a/src/main/java/Greet.java +++ b/src/main/java/Greet.java @@ -2,28 +2,33 @@ import java.util.Scanner; public class Greet { - public static void main (String[] args) { - String[] tasks = new String[100]; + public static void main(String[] args) { + Task[] tasks = new Task[100]; int ntasks = 0; System.out.println("Hello! I'm Duke"); String line; Scanner in = new Scanner(System.in); System.out.println("What can I do for you?"); line = in.nextLine(); - boolean bool1 = false; - while (!bool1) { - System.out.println("added: " + line); - tasks[ntasks]= line; - ntasks += 1; - line = in.nextLine(); + while (!line.equals("bye")) { if (line.equals("list")) { for (int i = 0; i < ntasks; i += 1) { - System.out.println((i+1) + ". " + tasks[i]); + System.out.println((i + 1) + ".[" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); } } - if (line.contains("bye")) { - bool1 = true; + else if (line.contains("done")) { + String[] splitTask = line.split(" "); + int index = Integer.parseInt(splitTask[1]) - 1; + tasks[index].setDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println("[" + tasks[index].getStatusIcon() + "] " + tasks[index].getDescription()); + } + else { + tasks[ntasks] = new Task(line); + ntasks += 1; + System.out.println("added: " + line); } + line = in.nextLine(); } System.out.println("Bye. Hope to see you again soon!"); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..7bca99c9b --- /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 description; + } + + public void setDone() { + isDone = true; + } +} \ No newline at end of file From 0e868fe0ecc2e57c5eb2cb4421688bc5a44e6d27 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 26 Aug 2021 10:33:50 +0800 Subject: [PATCH 05/25] A-CodingStandard --- src/main/java/Greet.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java index c83fac83e..24ef4adbe 100644 --- a/src/main/java/Greet.java +++ b/src/main/java/Greet.java @@ -15,15 +15,13 @@ public static void main(String[] args) { for (int i = 0; i < ntasks; i += 1) { System.out.println((i + 1) + ".[" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); } - } - else if (line.contains("done")) { + } else if (line.contains("done")) { String[] splitTask = line.split(" "); int index = Integer.parseInt(splitTask[1]) - 1; tasks[index].setDone(); System.out.println("Nice! I've marked this task as done:"); System.out.println("[" + tasks[index].getStatusIcon() + "] " + tasks[index].getDescription()); - } - else { + } else { tasks[ntasks] = new Task(line); ntasks += 1; System.out.println("added: " + line); From de9dc0ab589cb23f954cd43b0a698f67371d029e Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Wed, 1 Sep 2021 23:44:33 +0800 Subject: [PATCH 06/25] Level-4 --- src/main/java/Deadline.java | 19 ++++++++ src/main/java/Duke.java | 87 ++++++++++++++++++++++++++++++++++--- src/main/java/Event.java | 19 ++++++++ src/main/java/Greet.java | 33 -------------- src/main/java/Task.java | 6 ++- src/main/java/Todo.java | 10 +++++ 6 files changed, 134 insertions(+), 40 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java delete mode 100644 src/main/java/Greet.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..8b7afb552 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,19 @@ +public class Deadline extends Todo{ + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + public String getBy() { + return by; + } + public void setBy(String by) { + this.by = by; + } + + public void printTask() { + System.out.print("[D][" + getStatusIcon() + "] " + getDescription()); + System.out.println("(by:" + by + ")"); + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..8f396b0f7 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,85 @@ +import java.util.Arrays; +import java.util.Scanner; + public class Duke { + public static Task[] tasks = new Task[100]; + public static int taskCount = 0; + + public static void printGreeting() { + System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); + } + public static void printConfused() { + System.out.println("Could you say that again?"); + } + public static void printExit() { + System.out.println("Bye. Hope to see you again soon!"); + } + public static void printGotIt() { + System.out.println("Got it. I've added this task:"); + } + public static void printTaskCount() { + System.out.println("Now you have " + taskCount + " tasks in the list."); + } + public static void printTaskTypeResponse() { + printGotIt(); + tasks[taskCount - 1].printTask(); + printTaskCount(); + } + public static void printList() { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskCount; i += 1) { + System.out.print((i + 1) + "."); + tasks[i].printTask(); + } + } + public static void TaskDone(int index) { + tasks[index].setDone(); + System.out.println("Nice! I've marked this task as done:"); + tasks[index].printTask(); + } + public static void addTodo(String line) { + tasks[taskCount] = new Todo(line); + taskCount = taskCount + 1; + printTaskTypeResponse(); + } + public static void addDeadline(String line) { + String description = line.replaceAll("/.+", ""); + String by = line.replaceAll(".+/by", ""); + tasks[taskCount] = new Deadline(description, by); + taskCount = taskCount + 1; + printTaskTypeResponse(); + } + public static void addEvent(String line) { + String description = line.replaceAll("/.+", ""); + String by = line.replaceAll(".+/at", ""); + tasks[taskCount] = new Event(description, by); + taskCount = taskCount + 1; + printTaskTypeResponse(); + } public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + printGreeting(); + String line; + Scanner in = new Scanner(System.in); + line = in.nextLine(); + while (!line.equals("bye")) { + if (line.equals("list")) { + printList(); + } else if (line.contains("done")) { + String[] splitTask = line.split(" "); + int index = Integer.parseInt(splitTask[1]) - 1; + tasks[index].setDone(); + TaskDone(index); + } else if (line.contains("todo")) { + addTodo(line); + } else if (line.contains("deadline")) { + addDeadline(line); + } else if (line.contains("event")) { + addEvent(line); + } else { + printConfused(); + } + line = in.nextLine(); + } + printExit(); } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..5c182d07d --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,19 @@ +public class Event extends Todo{ + protected String at; + + public Event(String description, String at) { + super(description); + this.at = at; + } + public String getAt() { + return at; + } + public void setAt(String at) { + this.at = at; + } + + public void printTask() { + System.out.print("[E][" + getStatusIcon() + "] " + getDescription()); + System.out.println("(at:" + at + ")"); + } +} diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java deleted file mode 100644 index 24ef4adbe..000000000 --- a/src/main/java/Greet.java +++ /dev/null @@ -1,33 +0,0 @@ -import java.util.Arrays; -import java.util.Scanner; - -public class Greet { - public static void main(String[] args) { - Task[] tasks = new Task[100]; - int ntasks = 0; - System.out.println("Hello! I'm Duke"); - String line; - Scanner in = new Scanner(System.in); - System.out.println("What can I do for you?"); - line = in.nextLine(); - while (!line.equals("bye")) { - if (line.equals("list")) { - for (int i = 0; i < ntasks; i += 1) { - System.out.println((i + 1) + ".[" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); - } - } else if (line.contains("done")) { - String[] splitTask = line.split(" "); - int index = Integer.parseInt(splitTask[1]) - 1; - tasks[index].setDone(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println("[" + tasks[index].getStatusIcon() + "] " + tasks[index].getDescription()); - } else { - tasks[ntasks] = new Task(line); - ntasks += 1; - System.out.println("added: " + line); - } - line = in.nextLine(); - } - System.out.println("Bye. Hope to see you again soon!"); - } -} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 7bca99c9b..fb44f843e 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -4,7 +4,7 @@ public class Task { public Task(String description) { this.description = description; - this.isDone = false; + isDone = false; } public String getStatusIcon() { @@ -18,4 +18,8 @@ public String getDescription() { public void setDone() { isDone = true; } + + public void printTask() { + System.out.println(description); + } } \ No newline at end of file diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..908ca3a20 --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,10 @@ +public class Todo extends Task{ + + public Todo(String description) { + super(description); + } + + public void printTask() { + System.out.println("[T][" + getStatusIcon() + "] " + getDescription()); + } +} From 0061596a35990d9ea5b80f6115c4a1793cbded14 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Wed, 1 Sep 2021 23:58:08 +0800 Subject: [PATCH 07/25] Revert "Level-4" This reverts commit de9dc0ab589cb23f954cd43b0a698f67371d029e. --- src/main/java/Deadline.java | 19 -------- src/main/java/Duke.java | 87 +++---------------------------------- src/main/java/Event.java | 19 -------- src/main/java/Greet.java | 33 ++++++++++++++ src/main/java/Task.java | 6 +-- src/main/java/Todo.java | 10 ----- 6 files changed, 40 insertions(+), 134 deletions(-) delete mode 100644 src/main/java/Deadline.java delete mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Greet.java delete mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java deleted file mode 100644 index 8b7afb552..000000000 --- a/src/main/java/Deadline.java +++ /dev/null @@ -1,19 +0,0 @@ -public class Deadline extends Todo{ - protected String by; - - public Deadline(String description, String by) { - super(description); - this.by = by; - } - public String getBy() { - return by; - } - public void setBy(String by) { - this.by = by; - } - - public void printTask() { - System.out.print("[D][" + getStatusIcon() + "] " + getDescription()); - System.out.println("(by:" + by + ")"); - } -} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 8f396b0f7..5d313334c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,85 +1,10 @@ -import java.util.Arrays; -import java.util.Scanner; - public class Duke { - public static Task[] tasks = new Task[100]; - public static int taskCount = 0; - - public static void printGreeting() { - System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); - } - public static void printConfused() { - System.out.println("Could you say that again?"); - } - public static void printExit() { - System.out.println("Bye. Hope to see you again soon!"); - } - public static void printGotIt() { - System.out.println("Got it. I've added this task:"); - } - public static void printTaskCount() { - System.out.println("Now you have " + taskCount + " tasks in the list."); - } - public static void printTaskTypeResponse() { - printGotIt(); - tasks[taskCount - 1].printTask(); - printTaskCount(); - } - public static void printList() { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskCount; i += 1) { - System.out.print((i + 1) + "."); - tasks[i].printTask(); - } - } - public static void TaskDone(int index) { - tasks[index].setDone(); - System.out.println("Nice! I've marked this task as done:"); - tasks[index].printTask(); - } - public static void addTodo(String line) { - tasks[taskCount] = new Todo(line); - taskCount = taskCount + 1; - printTaskTypeResponse(); - } - public static void addDeadline(String line) { - String description = line.replaceAll("/.+", ""); - String by = line.replaceAll(".+/by", ""); - tasks[taskCount] = new Deadline(description, by); - taskCount = taskCount + 1; - printTaskTypeResponse(); - } - public static void addEvent(String line) { - String description = line.replaceAll("/.+", ""); - String by = line.replaceAll(".+/at", ""); - tasks[taskCount] = new Event(description, by); - taskCount = taskCount + 1; - printTaskTypeResponse(); - } public static void main(String[] args) { - printGreeting(); - String line; - Scanner in = new Scanner(System.in); - line = in.nextLine(); - while (!line.equals("bye")) { - if (line.equals("list")) { - printList(); - } else if (line.contains("done")) { - String[] splitTask = line.split(" "); - int index = Integer.parseInt(splitTask[1]) - 1; - tasks[index].setDone(); - TaskDone(index); - } else if (line.contains("todo")) { - addTodo(line); - } else if (line.contains("deadline")) { - addDeadline(line); - } else if (line.contains("event")) { - addEvent(line); - } else { - printConfused(); - } - line = in.nextLine(); - } - printExit(); + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java deleted file mode 100644 index 5c182d07d..000000000 --- a/src/main/java/Event.java +++ /dev/null @@ -1,19 +0,0 @@ -public class Event extends Todo{ - protected String at; - - public Event(String description, String at) { - super(description); - this.at = at; - } - public String getAt() { - return at; - } - public void setAt(String at) { - this.at = at; - } - - public void printTask() { - System.out.print("[E][" + getStatusIcon() + "] " + getDescription()); - System.out.println("(at:" + at + ")"); - } -} diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java new file mode 100644 index 000000000..24ef4adbe --- /dev/null +++ b/src/main/java/Greet.java @@ -0,0 +1,33 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class Greet { + public static void main(String[] args) { + Task[] tasks = new Task[100]; + int ntasks = 0; + System.out.println("Hello! I'm Duke"); + String line; + Scanner in = new Scanner(System.in); + System.out.println("What can I do for you?"); + line = in.nextLine(); + while (!line.equals("bye")) { + if (line.equals("list")) { + for (int i = 0; i < ntasks; i += 1) { + System.out.println((i + 1) + ".[" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); + } + } else if (line.contains("done")) { + String[] splitTask = line.split(" "); + int index = Integer.parseInt(splitTask[1]) - 1; + tasks[index].setDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println("[" + tasks[index].getStatusIcon() + "] " + tasks[index].getDescription()); + } else { + tasks[ntasks] = new Task(line); + ntasks += 1; + System.out.println("added: " + line); + } + line = in.nextLine(); + } + System.out.println("Bye. Hope to see you again soon!"); + } +} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index fb44f843e..7bca99c9b 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -4,7 +4,7 @@ public class Task { public Task(String description) { this.description = description; - isDone = false; + this.isDone = false; } public String getStatusIcon() { @@ -18,8 +18,4 @@ public String getDescription() { public void setDone() { isDone = true; } - - public void printTask() { - System.out.println(description); - } } \ No newline at end of file diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java deleted file mode 100644 index 908ca3a20..000000000 --- a/src/main/java/Todo.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Todo extends Task{ - - public Todo(String description) { - super(description); - } - - public void printTask() { - System.out.println("[T][" + getStatusIcon() + "] " + getDescription()); - } -} From 860304e6965777b41bca9684b1516f70b0d0c5be Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 2 Sep 2021 00:17:11 +0800 Subject: [PATCH 08/25] Fixing accidental revert --- src/main/java/Deadline.java | 19 ++++++++ src/main/java/Duke.java | 87 ++++++++++++++++++++++++++++++++++--- src/main/java/Event.java | 19 ++++++++ src/main/java/Greet.java | 33 -------------- src/main/java/Task.java | 8 +++- src/main/java/Todo.java | 10 +++++ 6 files changed, 135 insertions(+), 41 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java delete mode 100644 src/main/java/Greet.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..8b7afb552 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,19 @@ +public class Deadline extends Todo{ + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + public String getBy() { + return by; + } + public void setBy(String by) { + this.by = by; + } + + public void printTask() { + System.out.print("[D][" + getStatusIcon() + "] " + getDescription()); + System.out.println("(by:" + by + ")"); + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..8f396b0f7 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,85 @@ +import java.util.Arrays; +import java.util.Scanner; + public class Duke { + public static Task[] tasks = new Task[100]; + public static int taskCount = 0; + + public static void printGreeting() { + System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); + } + public static void printConfused() { + System.out.println("Could you say that again?"); + } + public static void printExit() { + System.out.println("Bye. Hope to see you again soon!"); + } + public static void printGotIt() { + System.out.println("Got it. I've added this task:"); + } + public static void printTaskCount() { + System.out.println("Now you have " + taskCount + " tasks in the list."); + } + public static void printTaskTypeResponse() { + printGotIt(); + tasks[taskCount - 1].printTask(); + printTaskCount(); + } + public static void printList() { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskCount; i += 1) { + System.out.print((i + 1) + "."); + tasks[i].printTask(); + } + } + public static void TaskDone(int index) { + tasks[index].setDone(); + System.out.println("Nice! I've marked this task as done:"); + tasks[index].printTask(); + } + public static void addTodo(String line) { + tasks[taskCount] = new Todo(line); + taskCount = taskCount + 1; + printTaskTypeResponse(); + } + public static void addDeadline(String line) { + String description = line.replaceAll("/.+", ""); + String by = line.replaceAll(".+/by", ""); + tasks[taskCount] = new Deadline(description, by); + taskCount = taskCount + 1; + printTaskTypeResponse(); + } + public static void addEvent(String line) { + String description = line.replaceAll("/.+", ""); + String by = line.replaceAll(".+/at", ""); + tasks[taskCount] = new Event(description, by); + taskCount = taskCount + 1; + printTaskTypeResponse(); + } public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + printGreeting(); + String line; + Scanner in = new Scanner(System.in); + line = in.nextLine(); + while (!line.equals("bye")) { + if (line.equals("list")) { + printList(); + } else if (line.contains("done")) { + String[] splitTask = line.split(" "); + int index = Integer.parseInt(splitTask[1]) - 1; + tasks[index].setDone(); + TaskDone(index); + } else if (line.contains("todo")) { + addTodo(line); + } else if (line.contains("deadline")) { + addDeadline(line); + } else if (line.contains("event")) { + addEvent(line); + } else { + printConfused(); + } + line = in.nextLine(); + } + printExit(); } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..5c182d07d --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,19 @@ +public class Event extends Todo{ + protected String at; + + public Event(String description, String at) { + super(description); + this.at = at; + } + public String getAt() { + return at; + } + public void setAt(String at) { + this.at = at; + } + + public void printTask() { + System.out.print("[E][" + getStatusIcon() + "] " + getDescription()); + System.out.println("(at:" + at + ")"); + } +} diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java deleted file mode 100644 index 24ef4adbe..000000000 --- a/src/main/java/Greet.java +++ /dev/null @@ -1,33 +0,0 @@ -import java.util.Arrays; -import java.util.Scanner; - -public class Greet { - public static void main(String[] args) { - Task[] tasks = new Task[100]; - int ntasks = 0; - System.out.println("Hello! I'm Duke"); - String line; - Scanner in = new Scanner(System.in); - System.out.println("What can I do for you?"); - line = in.nextLine(); - while (!line.equals("bye")) { - if (line.equals("list")) { - for (int i = 0; i < ntasks; i += 1) { - System.out.println((i + 1) + ".[" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); - } - } else if (line.contains("done")) { - String[] splitTask = line.split(" "); - int index = Integer.parseInt(splitTask[1]) - 1; - tasks[index].setDone(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println("[" + tasks[index].getStatusIcon() + "] " + tasks[index].getDescription()); - } else { - tasks[ntasks] = new Task(line); - ntasks += 1; - System.out.println("added: " + line); - } - line = in.nextLine(); - } - System.out.println("Bye. Hope to see you again soon!"); - } -} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 7bca99c9b..900881e8a 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -4,7 +4,7 @@ public class Task { public Task(String description) { this.description = description; - this.isDone = false; + isDone = false; } public String getStatusIcon() { @@ -18,4 +18,8 @@ public String getDescription() { public void setDone() { isDone = true; } -} \ No newline at end of file + + public void printTask() { + System.out.println(description); + } +} diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..908ca3a20 --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,10 @@ +public class Todo extends Task{ + + public Todo(String description) { + super(description); + } + + public void printTask() { + System.out.println("[T][" + getStatusIcon() + "] " + getDescription()); + } +} From 466cbeea100643e8d21e8e7f8def93afa8c08c17 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 2 Sep 2021 00:49:56 +0800 Subject: [PATCH 09/25] Incomplete A-TextUiTesting --- text-ui-test/EXPECTED.TXT | 33 +++++++++++++++++++++++++++------ text-ui-test/input.txt | 8 ++++++++ text-ui-test/runtest.bat | 4 ++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..e17c0c057 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,28 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| +Hello! I'm Duke +What can I do for you? +Could you say that again? + +Got it. I've added this task: +[T][ ] todo eat cake +Now you have 1 tasks in the list. + +Got it. I've added this task: +[D][ ] deadline eat chocolate cake (by: Friday 2pm) +Now you have 2 tasks in the list. + +Got it. I've added this task: +[E][ ] event attend cake buffet (at: Saturday 5pm) +Now you have 3 tasks in the list. + +Nice! I've marked this task as done: +[T][X] todo eat cake + +Here are the tasks in your list: +1.[T][X] todo eat cake +2.[D][ ] deadline eat chocolate cake (by: Friday 2pm) +3.[E][ ] event attend cake buffet (at: Saturday 5pm) + +Could you say that again? + +Bye. Hope to see you again soon! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..bc9f12c2f 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,8 @@ +eat cake +todo eat cake +deadline eat chocolate cake /by Friday 2pm +event attend cake buffet /at Saturday 5pm +done 1 +list +cake +bye \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..f0f1ef7b2 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 D:\NUS\Sem 3\CS2113\ip-master\src\main\java -Xlint:none -d D:\NUS\Sem 3\CS2113\jdk11.0.12_7\bin D:\NUS\Sem 3\CS2113\ip-master\src\main\java\Duke.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 D:\NUS\Sem 3\CS2113\jdk11.0.12_7\bin Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From 781043a4ea55f88f6a5f4c0f96194824d92131ff Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 2 Sep 2021 01:00:20 +0800 Subject: [PATCH 10/25] A-CodeQuality --- src/main/java/Duke.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 8f396b0f7..a732853ec 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -21,6 +21,7 @@ public static void printTaskCount() { System.out.println("Now you have " + taskCount + " tasks in the list."); } public static void printTaskTypeResponse() { + //printing different responses depending if its Todo/Deadline/Event printGotIt(); tasks[taskCount - 1].printTask(); printTaskCount(); @@ -43,6 +44,7 @@ public static void addTodo(String line) { printTaskTypeResponse(); } public static void addDeadline(String line) { + //extracting the description and date String description = line.replaceAll("/.+", ""); String by = line.replaceAll(".+/by", ""); tasks[taskCount] = new Deadline(description, by); @@ -50,6 +52,7 @@ public static void addDeadline(String line) { printTaskTypeResponse(); } public static void addEvent(String line) { + //extracting the description and date String description = line.replaceAll("/.+", ""); String by = line.replaceAll(".+/at", ""); tasks[taskCount] = new Event(description, by); @@ -61,10 +64,12 @@ public static void main(String[] args) { String line; Scanner in = new Scanner(System.in); line = in.nextLine(); + //continue to run program unless types "bye" to exit program while (!line.equals("bye")) { if (line.equals("list")) { printList(); } else if (line.contains("done")) { + //find which task user is done with String[] splitTask = line.split(" "); int index = Integer.parseInt(splitTask[1]) - 1; tasks[index].setDone(); @@ -76,6 +81,7 @@ public static void main(String[] args) { } else if (line.contains("event")) { addEvent(line); } else { + //error with input printConfused(); } line = in.nextLine(); From 50e7e6b721f0f8d6a1579fff267dc8c997e65a6d Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 2 Sep 2021 01:00:53 +0800 Subject: [PATCH 11/25] Shifting comment line --- src/main/java/Task.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 900881e8a..b384a5060 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -8,7 +8,8 @@ public Task(String description) { } public String getStatusIcon() { - return (isDone ? "X" : " "); // mark done task with X + // mark done task with X + return (isDone ? "X" : " "); } public String getDescription() { From a6b0cb4690b1781b6d871c67b3ad91537aa4d43d Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 2 Sep 2021 02:13:58 +0800 Subject: [PATCH 12/25] Altering runtest.bat --- text-ui-test/runtest.bat | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index f0f1ef7b2..4883b1056 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 D:\NUS\Sem 3\CS2113\ip-master\src\main\java -Xlint:none -d D:\NUS\Sem 3\CS2113\jdk11.0.12_7\bin D:\NUS\Sem 3\CS2113\ip-master\src\main\java\Duke.java +javac -cp D:\NUS\Sem3\CS2113\ip\src\main\java -Xlint:none -d D:\NUS\Sem3\CS2113\jdk11.0.12_7\bin D:\NUS\Sem 3\CS2113\ip\src\main\java\Duke.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 D:\NUS\Sem 3\CS2113\jdk11.0.12_7\bin Duke < input.txt > ACTUAL.TXT +java -classpath D:\NUS\Sem3\CS2113\jdk11.0.12_7\bin Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From 81aba6144bbde54f674aa1eeb322bca16cccdc74 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 2 Sep 2021 22:15:55 +0800 Subject: [PATCH 13/25] A-TextUiTesting update --- text-ui-test/EXPECTED.TXT | 8 -------- text-ui-test/runtest.bat | 6 +++--- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index e17c0c057..f1aa31c20 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,28 +1,20 @@ Hello! I'm Duke What can I do for you? - Could you say that again? - Got it. I've added this task: [T][ ] todo eat cake Now you have 1 tasks in the list. - Got it. I've added this task: [D][ ] deadline eat chocolate cake (by: Friday 2pm) Now you have 2 tasks in the list. - Got it. I've added this task: [E][ ] event attend cake buffet (at: Saturday 5pm) Now you have 3 tasks in the list. - Nice! I've marked this task as done: [T][X] todo eat cake - Here are the tasks in your list: 1.[T][X] todo eat cake 2.[D][ ] deadline eat chocolate cake (by: Friday 2pm) 3.[E][ ] event attend cake buffet (at: Saturday 5pm) - Could you say that again? - Bye. Hope to see you again soon! diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 4883b1056..1a491cd91 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -4,10 +4,10 @@ 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 D:\NUS\Sem3\CS2113\ip\src\main\java -Xlint:none -d D:\NUS\Sem3\CS2113\jdk11.0.12_7\bin D:\NUS\Sem 3\CS2113\ip\src\main\java\Duke.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 D:\NUS\Sem3\CS2113\jdk11.0.12_7\bin 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 From dcd1d9122cbbce8050e0655f7f5aa60556d6b45e Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 9 Sep 2021 00:10:06 +0800 Subject: [PATCH 14/25] Level-5 --- src/main/java/DeadlineException.java | 2 + src/main/java/Duke.java | 132 +++++++++++++++++++-------- src/main/java/EventException.java | 2 + src/main/java/TodoException.java | 2 + 4 files changed, 98 insertions(+), 40 deletions(-) create mode 100644 src/main/java/DeadlineException.java create mode 100644 src/main/java/EventException.java create mode 100644 src/main/java/TodoException.java diff --git a/src/main/java/DeadlineException.java b/src/main/java/DeadlineException.java new file mode 100644 index 000000000..133d1183a --- /dev/null +++ b/src/main/java/DeadlineException.java @@ -0,0 +1,2 @@ +public class DeadlineException extends Exception{ +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a732853ec..3928da0cd 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -8,24 +8,42 @@ public class Duke { public static void printGreeting() { System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); } + public static void printConfused() { System.out.println("Could you say that again?"); } + public static void printExit() { System.out.println("Bye. Hope to see you again soon!"); } + public static void printGotIt() { System.out.println("Got it. I've added this task:"); } + public static void printTaskCount() { System.out.println("Now you have " + taskCount + " tasks in the list."); } + + public static void printTodoException() { + System.out.println("The description of todo cannot be empty."); + } + + public static void printDeadlineException() { + System.out.println("The description of deadline cannot be empty."); + } + + public static void printEventException() { + System.out.println("The description of event cannot be empty."); + } + public static void printTaskTypeResponse() { //printing different responses depending if its Todo/Deadline/Event printGotIt(); tasks[taskCount - 1].printTask(); printTaskCount(); } + public static void printList() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskCount; i += 1) { @@ -33,59 +51,93 @@ public static void printList() { tasks[i].printTask(); } } + public static void TaskDone(int index) { tasks[index].setDone(); System.out.println("Nice! I've marked this task as done:"); tasks[index].printTask(); } - public static void addTodo(String line) { - tasks[taskCount] = new Todo(line); - taskCount = taskCount + 1; - printTaskTypeResponse(); - } - public static void addDeadline(String line) { - //extracting the description and date - String description = line.replaceAll("/.+", ""); - String by = line.replaceAll(".+/by", ""); - tasks[taskCount] = new Deadline(description, by); - taskCount = taskCount + 1; - printTaskTypeResponse(); - } - public static void addEvent(String line) { - //extracting the description and date - String description = line.replaceAll("/.+", ""); - String by = line.replaceAll(".+/at", ""); - tasks[taskCount] = new Event(description, by); - taskCount = taskCount + 1; - printTaskTypeResponse(); + + public static void addTodo(String line) throws TodoException { + if (line.equals("") || line.equals("todo")) { + throw new TodoException(); + } else { + tasks[taskCount] = new Todo(line); + taskCount = taskCount + 1; + printTaskTypeResponse(); + } + } + + public static void addDeadline(String line) throws DeadlineException { + if (!line.matches("(.*)/by(.*)")) { + throw new DeadlineException(); + } else { + //extracting the description and date + String description = line.replaceAll("/.+", ""); + String by = line.replaceAll(".+/by", ""); + tasks[taskCount] = new Deadline(description, by); + taskCount = taskCount + 1; + printTaskTypeResponse(); + } } + + public static void addEvent(String line) throws EventException { + if (!line.matches("(.*)/at(.*)")) { + throw new EventException(); + } else { + //extracting the description and date + String description = line.replaceAll("/.+", ""); + String by = line.replaceAll(".+/at", ""); + tasks[taskCount] = new Event(description, by); + taskCount = taskCount + 1; + printTaskTypeResponse(); + } + } + + public static void readInput(String line) { + String[] splitLine = line.split(" ", 2); + String command = splitLine[0]; + line = line.replaceAll("^" + command + " ", ""); + if (command.equals("list")) { + printList(); + } else if (command.equals("done")) { + //find which task user is done with + int index = Integer.parseInt(line) - 1; + tasks[index].setDone(); + TaskDone(index); + } else if (command.equals("todo")) { + try { + addTodo(line); + } catch (TodoException e) { + printTodoException(); + } + } else if (command.equals("deadline")) { + try { + addDeadline(line); + } catch (DeadlineException e) { + printDeadlineException(); + } + } else if (command.equals("event")) { + try { + addEvent(line); + } catch (EventException e) { + printEventException(); + } + } else { + //error with input + printConfused(); + } + } + public static void main(String[] args) { printGreeting(); String line; Scanner in = new Scanner(System.in); line = in.nextLine(); //continue to run program unless types "bye" to exit program - while (!line.equals("bye")) { - if (line.equals("list")) { - printList(); - } else if (line.contains("done")) { - //find which task user is done with - String[] splitTask = line.split(" "); - int index = Integer.parseInt(splitTask[1]) - 1; - tasks[index].setDone(); - TaskDone(index); - } else if (line.contains("todo")) { - addTodo(line); - } else if (line.contains("deadline")) { - addDeadline(line); - } else if (line.contains("event")) { - addEvent(line); - } else { - //error with input - printConfused(); - } + while(!line.equals("bye")) { + readInput(line); line = in.nextLine(); } - printExit(); } } diff --git a/src/main/java/EventException.java b/src/main/java/EventException.java new file mode 100644 index 000000000..14ba8baaa --- /dev/null +++ b/src/main/java/EventException.java @@ -0,0 +1,2 @@ +public class EventException extends Exception{ +} diff --git a/src/main/java/TodoException.java b/src/main/java/TodoException.java new file mode 100644 index 000000000..737bb4f60 --- /dev/null +++ b/src/main/java/TodoException.java @@ -0,0 +1,2 @@ +public class TodoException extends Exception{ +} From 63405a4f96096f0ba9c0bd1ce369ed7589f31cce Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 9 Sep 2021 00:50:57 +0800 Subject: [PATCH 15/25] A-Packages --- src/main/java/{ => duke}/Deadline.java | 2 ++ src/main/java/{ => duke}/DeadlineException.java | 2 ++ src/main/java/{ => duke}/Duke.java | 5 +++-- src/main/java/{ => duke}/Event.java | 2 ++ src/main/java/{ => duke}/EventException.java | 2 ++ src/main/java/{ => duke}/Task.java | 2 ++ src/main/java/{ => duke}/Todo.java | 2 ++ src/main/java/{ => duke}/TodoException.java | 2 ++ 8 files changed, 17 insertions(+), 2 deletions(-) rename src/main/java/{ => duke}/Deadline.java (96%) rename src/main/java/{ => duke}/DeadlineException.java (77%) rename src/main/java/{ => duke}/Duke.java (97%) rename src/main/java/{ => duke}/Event.java (96%) rename src/main/java/{ => duke}/EventException.java (76%) rename src/main/java/{ => duke}/Task.java (97%) rename src/main/java/{ => duke}/Todo.java (93%) rename src/main/java/{ => duke}/TodoException.java (76%) diff --git a/src/main/java/Deadline.java b/src/main/java/duke/Deadline.java similarity index 96% rename from src/main/java/Deadline.java rename to src/main/java/duke/Deadline.java index 8b7afb552..f8cae570c 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/duke/Deadline.java @@ -1,3 +1,5 @@ +package duke; + public class Deadline extends Todo{ protected String by; diff --git a/src/main/java/DeadlineException.java b/src/main/java/duke/DeadlineException.java similarity index 77% rename from src/main/java/DeadlineException.java rename to src/main/java/duke/DeadlineException.java index 133d1183a..96eb5dda9 100644 --- a/src/main/java/DeadlineException.java +++ b/src/main/java/duke/DeadlineException.java @@ -1,2 +1,4 @@ +package duke; + public class DeadlineException extends Exception{ } diff --git a/src/main/java/Duke.java b/src/main/java/duke/Duke.java similarity index 97% rename from src/main/java/Duke.java rename to src/main/java/duke/Duke.java index 3928da0cd..1a04898d6 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,4 +1,5 @@ -import java.util.Arrays; +package duke; + import java.util.Scanner; public class Duke { @@ -38,7 +39,7 @@ public static void printEventException() { } public static void printTaskTypeResponse() { - //printing different responses depending if its Todo/Deadline/Event + //printing different responses depending if its duke.Todo/duke.Deadline/duke.Event printGotIt(); tasks[taskCount - 1].printTask(); printTaskCount(); diff --git a/src/main/java/Event.java b/src/main/java/duke/Event.java similarity index 96% rename from src/main/java/Event.java rename to src/main/java/duke/Event.java index 5c182d07d..df8c6a880 100644 --- a/src/main/java/Event.java +++ b/src/main/java/duke/Event.java @@ -1,3 +1,5 @@ +package duke; + public class Event extends Todo{ protected String at; diff --git a/src/main/java/EventException.java b/src/main/java/duke/EventException.java similarity index 76% rename from src/main/java/EventException.java rename to src/main/java/duke/EventException.java index 14ba8baaa..b4366936d 100644 --- a/src/main/java/EventException.java +++ b/src/main/java/duke/EventException.java @@ -1,2 +1,4 @@ +package duke; + public class EventException extends Exception{ } diff --git a/src/main/java/Task.java b/src/main/java/duke/Task.java similarity index 97% rename from src/main/java/Task.java rename to src/main/java/duke/Task.java index b384a5060..8c6172d16 100644 --- a/src/main/java/Task.java +++ b/src/main/java/duke/Task.java @@ -1,3 +1,5 @@ +package duke; + public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/Todo.java b/src/main/java/duke/Todo.java similarity index 93% rename from src/main/java/Todo.java rename to src/main/java/duke/Todo.java index 908ca3a20..80195adbc 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/duke/Todo.java @@ -1,3 +1,5 @@ +package duke; + public class Todo extends Task{ public Todo(String description) { diff --git a/src/main/java/TodoException.java b/src/main/java/duke/TodoException.java similarity index 76% rename from src/main/java/TodoException.java rename to src/main/java/duke/TodoException.java index 737bb4f60..66545fe54 100644 --- a/src/main/java/TodoException.java +++ b/src/main/java/duke/TodoException.java @@ -1,2 +1,4 @@ +package duke; + public class TodoException extends Exception{ } From 9096123dd780d6ec95247807f715ffa840f45e65 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Thu, 9 Sep 2021 09:58:49 +0800 Subject: [PATCH 16/25] Updated runtest and exceptions. --- src/main/java/duke/DoneException.java | 4 ++++ src/main/java/duke/Duke.java | 28 +++++++++++++++------- src/main/java/duke/TaskCountException.java | 4 ++++ text-ui-test/EXPECTED.TXT | 16 ++++++------- text-ui-test/input.txt | 1 + text-ui-test/runtest.bat | 4 ++-- 6 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 src/main/java/duke/DoneException.java create mode 100644 src/main/java/duke/TaskCountException.java diff --git a/src/main/java/duke/DoneException.java b/src/main/java/duke/DoneException.java new file mode 100644 index 000000000..674670a8a --- /dev/null +++ b/src/main/java/duke/DoneException.java @@ -0,0 +1,4 @@ +package duke; + +public class DoneException extends Exception{ +} diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 1a04898d6..1e1c36654 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -38,6 +38,10 @@ public static void printEventException() { System.out.println("The description of event cannot be empty."); } + public static void printDoneException() { + System.out.println("The description of done cannot be empty."); + } + public static void printTaskTypeResponse() { //printing different responses depending if its duke.Todo/duke.Deadline/duke.Event printGotIt(); @@ -53,10 +57,15 @@ public static void printList() { } } - public static void TaskDone(int index) { - tasks[index].setDone(); - System.out.println("Nice! I've marked this task as done:"); - tasks[index].printTask(); + public static void taskDone(String line) throws DoneException { + if (line.equals("") || line.equals("done")) { + throw new DoneException(); + } else { + int index = Integer.parseInt(line) - 1; + tasks[index].setDone(); + System.out.println("Nice! I've marked this task as done:"); + tasks[index].printTask(); + } } public static void addTodo(String line) throws TodoException { @@ -102,10 +111,12 @@ public static void readInput(String line) { if (command.equals("list")) { printList(); } else if (command.equals("done")) { - //find which task user is done with - int index = Integer.parseInt(line) - 1; - tasks[index].setDone(); - TaskDone(index); + try { + //find which task user is done with + taskDone(line); + } catch (DoneException e) { + printDoneException(); + } } else if (command.equals("todo")) { try { addTodo(line); @@ -140,5 +151,6 @@ public static void main(String[] args) { readInput(line); line = in.nextLine(); } + printExit(); } } diff --git a/src/main/java/duke/TaskCountException.java b/src/main/java/duke/TaskCountException.java new file mode 100644 index 000000000..b42b9e248 --- /dev/null +++ b/src/main/java/duke/TaskCountException.java @@ -0,0 +1,4 @@ +package duke; + +public class TaskCountException extends Exception{ +} diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index f1aa31c20..0cdc00662 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -2,19 +2,19 @@ Hello! I'm Duke What can I do for you? Could you say that again? Got it. I've added this task: -[T][ ] todo eat cake +[T][ ] eat cake Now you have 1 tasks in the list. Got it. I've added this task: -[D][ ] deadline eat chocolate cake (by: Friday 2pm) +[D][ ] eat chocolate cake (by: Friday 2pm) Now you have 2 tasks in the list. Got it. I've added this task: -[E][ ] event attend cake buffet (at: Saturday 5pm) +[E][ ] attend cake buffet (at: Saturday 5pm) Now you have 3 tasks in the list. Nice! I've marked this task as done: -[T][X] todo eat cake +[T][X] eat cake +The description of todo cannot be empty. Here are the tasks in your list: -1.[T][X] todo eat cake -2.[D][ ] deadline eat chocolate cake (by: Friday 2pm) -3.[E][ ] event attend cake buffet (at: Saturday 5pm) +1.[T][X] eat cake +2.[D][ ] eat chocolate cake (by: Friday 2pm) +3.[E][ ] attend cake buffet (at: Saturday 5pm) Could you say that again? -Bye. Hope to see you again soon! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index bc9f12c2f..bec17eb26 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -3,6 +3,7 @@ todo eat cake deadline eat chocolate cake /by Friday 2pm event attend cake buffet /at Saturday 5pm done 1 +todo list cake bye \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 1a491cd91..9dec60887 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 @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Duke < input.txt > ACTUAL.TXT +java -classpath ..\bin duke.Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From f6335dc152bda89f5ee3910f8541ac77a709198f Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Wed, 15 Sep 2021 00:00:20 +0800 Subject: [PATCH 17/25] Reformat files, add branch-Level-6, Level-6 and A-Collections --- src/main/java/duke/DeadlineException.java | 2 +- src/main/java/duke/DeleteException.java | 4 ++ src/main/java/duke/DoneException.java | 2 +- src/main/java/duke/Duke.java | 57 ++++++++++++++++------ src/main/java/duke/EventException.java | 2 +- src/main/java/duke/TaskCountException.java | 2 +- src/main/java/duke/TodoException.java | 2 +- 7 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 src/main/java/duke/DeleteException.java diff --git a/src/main/java/duke/DeadlineException.java b/src/main/java/duke/DeadlineException.java index 96eb5dda9..b6c92aff2 100644 --- a/src/main/java/duke/DeadlineException.java +++ b/src/main/java/duke/DeadlineException.java @@ -1,4 +1,4 @@ package duke; -public class DeadlineException extends Exception{ +public class DeadlineException extends Exception { } diff --git a/src/main/java/duke/DeleteException.java b/src/main/java/duke/DeleteException.java new file mode 100644 index 000000000..baea749ed --- /dev/null +++ b/src/main/java/duke/DeleteException.java @@ -0,0 +1,4 @@ +package duke; + +public class DeleteException extends Exception { +} diff --git a/src/main/java/duke/DoneException.java b/src/main/java/duke/DoneException.java index 674670a8a..e0512ebec 100644 --- a/src/main/java/duke/DoneException.java +++ b/src/main/java/duke/DoneException.java @@ -1,4 +1,4 @@ package duke; -public class DoneException extends Exception{ +public class DoneException extends Exception { } diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 1e1c36654..db4c5746a 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,10 +1,10 @@ package duke; +import java.util.ArrayList; import java.util.Scanner; public class Duke { - public static Task[] tasks = new Task[100]; - public static int taskCount = 0; + public static ArrayList tasks = new ArrayList<>(); public static void printGreeting() { System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); @@ -22,8 +22,12 @@ public static void printGotIt() { System.out.println("Got it. I've added this task:"); } + public static void printRemoveIt() { + System.out.println("Noted. I've removed this task:"); + } + public static void printTaskCount() { - System.out.println("Now you have " + taskCount + " tasks in the list."); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } public static void printTodoException() { @@ -42,18 +46,27 @@ public static void printDoneException() { System.out.println("The description of done cannot be empty."); } + public static void printDeleteException() { + System.out.println("The description of delete cannot be empty."); + } + public static void printTaskTypeResponse() { //printing different responses depending if its duke.Todo/duke.Deadline/duke.Event printGotIt(); - tasks[taskCount - 1].printTask(); + tasks.get(tasks.size() - 1).printTask(); printTaskCount(); } + public static void printDeleteResponse() { + printRemoveIt(); + tasks.get(tasks.size() - 1).printTask(); + } + public static void printList() { System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskCount; i += 1) { + for (int i = 0; i < tasks.size(); i += 1) { System.out.print((i + 1) + "."); - tasks[i].printTask(); + tasks.get(i).printTask(); } } @@ -62,9 +75,9 @@ public static void taskDone(String line) throws DoneException { throw new DoneException(); } else { int index = Integer.parseInt(line) - 1; - tasks[index].setDone(); + tasks.get(index).setDone(); System.out.println("Nice! I've marked this task as done:"); - tasks[index].printTask(); + tasks.get(index).printTask(); } } @@ -72,8 +85,7 @@ public static void addTodo(String line) throws TodoException { if (line.equals("") || line.equals("todo")) { throw new TodoException(); } else { - tasks[taskCount] = new Todo(line); - taskCount = taskCount + 1; + tasks.add(new Todo (line)); printTaskTypeResponse(); } } @@ -85,8 +97,7 @@ public static void addDeadline(String line) throws DeadlineException { //extracting the description and date String description = line.replaceAll("/.+", ""); String by = line.replaceAll(".+/by", ""); - tasks[taskCount] = new Deadline(description, by); - taskCount = taskCount + 1; + tasks.add(new Deadline (description, by)); printTaskTypeResponse(); } } @@ -97,13 +108,23 @@ public static void addEvent(String line) throws EventException { } else { //extracting the description and date String description = line.replaceAll("/.+", ""); - String by = line.replaceAll(".+/at", ""); - tasks[taskCount] = new Event(description, by); - taskCount = taskCount + 1; + String at = line.replaceAll(".+/at", ""); + tasks.add(new Event (description, at)); printTaskTypeResponse(); } } + public static void deleteTask(String line) throws DeleteException { + if (line.equals("") || line.equals("delete")) { + throw new DeleteException(); + } else { + int index = Integer.parseInt(line) - 1; + printDeleteResponse(); + tasks.remove(index); + printTaskCount(); + } + } + public static void readInput(String line) { String[] splitLine = line.split(" ", 2); String command = splitLine[0]; @@ -135,6 +156,12 @@ public static void readInput(String line) { } catch (EventException e) { printEventException(); } + } else if (command.equals("delete")) { + try { + deleteTask(line); + } catch (DeleteException e) { + printDeleteException(); + } } else { //error with input printConfused(); diff --git a/src/main/java/duke/EventException.java b/src/main/java/duke/EventException.java index b4366936d..1241ce337 100644 --- a/src/main/java/duke/EventException.java +++ b/src/main/java/duke/EventException.java @@ -1,4 +1,4 @@ package duke; -public class EventException extends Exception{ +public class EventException extends Exception { } diff --git a/src/main/java/duke/TaskCountException.java b/src/main/java/duke/TaskCountException.java index b42b9e248..3662e528a 100644 --- a/src/main/java/duke/TaskCountException.java +++ b/src/main/java/duke/TaskCountException.java @@ -1,4 +1,4 @@ package duke; -public class TaskCountException extends Exception{ +public class TaskCountException extends Exception { } diff --git a/src/main/java/duke/TodoException.java b/src/main/java/duke/TodoException.java index 66545fe54..541e2220f 100644 --- a/src/main/java/duke/TodoException.java +++ b/src/main/java/duke/TodoException.java @@ -1,4 +1,4 @@ package duke; -public class TodoException extends Exception{ +public class TodoException extends Exception { } From a5faa2b620073871c9489ad814b3394446218758 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Wed, 15 Sep 2021 01:13:10 +0800 Subject: [PATCH 18/25] Add Level-7 --- src/main/java/duke/Deadline.java | 11 +++ src/main/java/duke/DeadlineException.java | 2 +- src/main/java/duke/DoneException.java | 2 +- src/main/java/duke/Duke.java | 96 ++++++++++++++++++---- src/main/java/duke/Event.java | 10 +++ src/main/java/duke/EventException.java | 2 +- src/main/java/duke/Task.java | 9 ++ src/main/java/duke/TaskCountException.java | 2 +- src/main/java/duke/Todo.java | 8 ++ src/main/java/duke/TodoException.java | 2 +- 10 files changed, 124 insertions(+), 20 deletions(-) diff --git a/src/main/java/duke/Deadline.java b/src/main/java/duke/Deadline.java index f8cae570c..6e3fa5fa7 100644 --- a/src/main/java/duke/Deadline.java +++ b/src/main/java/duke/Deadline.java @@ -7,9 +7,15 @@ public Deadline(String description, String by) { super(description); this.by = by; } + public Deadline(String description, boolean isDone, String by) { + super(description, isDone); + this.by = by; + } + public String getBy() { return by; } + public void setBy(String by) { this.by = by; } @@ -18,4 +24,9 @@ public void printTask() { System.out.print("[D][" + getStatusIcon() + "] " + getDescription()); System.out.println("(by:" + by + ")"); } + + @Override + public String saveTask() { + return "D | " + (isDone ? 1 : 0) + " | " + description + " | " + by; + } } diff --git a/src/main/java/duke/DeadlineException.java b/src/main/java/duke/DeadlineException.java index 96eb5dda9..b6c92aff2 100644 --- a/src/main/java/duke/DeadlineException.java +++ b/src/main/java/duke/DeadlineException.java @@ -1,4 +1,4 @@ package duke; -public class DeadlineException extends Exception{ +public class DeadlineException extends Exception { } diff --git a/src/main/java/duke/DoneException.java b/src/main/java/duke/DoneException.java index 674670a8a..e0512ebec 100644 --- a/src/main/java/duke/DoneException.java +++ b/src/main/java/duke/DoneException.java @@ -1,4 +1,4 @@ package duke; -public class DoneException extends Exception{ +public class DoneException extends Exception { } diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 1e1c36654..d3c4f7514 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,10 +1,39 @@ package duke; +import java.util.ArrayList; import java.util.Scanner; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + public class Duke { - public static Task[] tasks = new Task[100]; - public static int taskCount = 0; + public static ArrayList tasks = new ArrayList<>(); + public static File f = new File("data/duke.txt"); // create a File for the given file path + + public static void loadTasks() throws FileNotFoundException { + Scanner s = new Scanner(f); // create a Scanner using the File as the source + while (s.hasNext()) { + String task = s.nextLine(); + String[] taskSplit= task.split(" \\| "); + String taskType = taskSplit[0]; + boolean isDone = taskSplit[1].equals("1"); + String description = taskSplit[2]; + String preposition = taskSplit.length >= 4 ? taskSplit[3] : ""; + if (taskType.equals("E")){ + tasks.add(new Event(description, isDone, preposition)); + } else if (taskType.equals("D")){ + tasks.add(new Deadline(description, isDone, preposition)); + + } else { + tasks.add(new Todo(description, isDone)); + } + } + } public static void printGreeting() { System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); @@ -23,7 +52,7 @@ public static void printGotIt() { } public static void printTaskCount() { - System.out.println("Now you have " + taskCount + " tasks in the list."); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } public static void printTodoException() { @@ -45,15 +74,51 @@ public static void printDoneException() { public static void printTaskTypeResponse() { //printing different responses depending if its duke.Todo/duke.Deadline/duke.Event printGotIt(); - tasks[taskCount - 1].printTask(); + tasks.get(tasks.size() - 1).printTask(); printTaskCount(); } public static void printList() { System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskCount; i += 1) { + for (int i = 0; i < tasks.size(); i += 1) { System.out.print((i + 1) + "."); - tasks[i].printTask(); + tasks.get(i).printTask(); + } + } + + public static void createFile() throws IOException{ + Path pathToFile = Paths.get("data/duke.txt"); + Files.createDirectories(pathToFile.getParent()); + f.createNewFile(); + } + + public static void initialiseTasks(){ + try { + loadTasks(); + } catch (FileNotFoundException e0){ + System.out.println("The file data/duke.txt is not found."); + try { + createFile(); + } catch (IOException e1){ + System.out.println("There has been an error creating a new file."); + } + System.out.println("A new file data/duke.txt has been created."); + } + } + + private static void writeToFile() throws IOException { + FileWriter fw = new FileWriter("data/duke.txt"); + for (Task list : tasks) { + fw.write(list.saveTask() + "\n"); + } + fw.close(); + } + + public static void write() { + try { + writeToFile(); + } catch (IOException e) { + System.out.println("There has been an error writing to file."); } } @@ -62,9 +127,9 @@ public static void taskDone(String line) throws DoneException { throw new DoneException(); } else { int index = Integer.parseInt(line) - 1; - tasks[index].setDone(); + tasks.get(index).setDone(); System.out.println("Nice! I've marked this task as done:"); - tasks[index].printTask(); + tasks.get(index).printTask(); } } @@ -72,9 +137,9 @@ public static void addTodo(String line) throws TodoException { if (line.equals("") || line.equals("todo")) { throw new TodoException(); } else { - tasks[taskCount] = new Todo(line); - taskCount = taskCount + 1; + tasks.add(new Todo(line)); printTaskTypeResponse(); + write(); } } @@ -85,9 +150,9 @@ public static void addDeadline(String line) throws DeadlineException { //extracting the description and date String description = line.replaceAll("/.+", ""); String by = line.replaceAll(".+/by", ""); - tasks[taskCount] = new Deadline(description, by); - taskCount = taskCount + 1; + tasks.add(new Deadline (description, by)); printTaskTypeResponse(); + write(); } } @@ -97,10 +162,10 @@ public static void addEvent(String line) throws EventException { } else { //extracting the description and date String description = line.replaceAll("/.+", ""); - String by = line.replaceAll(".+/at", ""); - tasks[taskCount] = new Event(description, by); - taskCount = taskCount + 1; + String at = line.replaceAll(".+/at", ""); + tasks.add(new Event (description, at)); printTaskTypeResponse(); + write(); } } @@ -142,6 +207,7 @@ public static void readInput(String line) { } public static void main(String[] args) { + initialiseTasks(); printGreeting(); String line; Scanner in = new Scanner(System.in); diff --git a/src/main/java/duke/Event.java b/src/main/java/duke/Event.java index df8c6a880..720852d68 100644 --- a/src/main/java/duke/Event.java +++ b/src/main/java/duke/Event.java @@ -7,6 +7,11 @@ public Event(String description, String at) { super(description); this.at = at; } + + public Event(String description, boolean isDone, String at) { + super(description, isDone); + this.at = at; + } public String getAt() { return at; } @@ -18,4 +23,9 @@ public void printTask() { System.out.print("[E][" + getStatusIcon() + "] " + getDescription()); System.out.println("(at:" + at + ")"); } + + @Override + public String saveTask() { + return "E | " + (isDone ? 1 : 0) + " | " + description + " | " + at; + } } diff --git a/src/main/java/duke/EventException.java b/src/main/java/duke/EventException.java index b4366936d..1241ce337 100644 --- a/src/main/java/duke/EventException.java +++ b/src/main/java/duke/EventException.java @@ -1,4 +1,4 @@ package duke; -public class EventException extends Exception{ +public class EventException extends Exception { } diff --git a/src/main/java/duke/Task.java b/src/main/java/duke/Task.java index 8c6172d16..061f21fa8 100644 --- a/src/main/java/duke/Task.java +++ b/src/main/java/duke/Task.java @@ -9,6 +9,11 @@ public Task(String description) { isDone = false; } + public Task(String description, boolean isDone) { + this.description = description; + this.isDone = isDone; + } + public String getStatusIcon() { // mark done task with X return (isDone ? "X" : " "); @@ -25,4 +30,8 @@ public void setDone() { public void printTask() { System.out.println(description); } + + public String saveTask() { + return null; + } } diff --git a/src/main/java/duke/TaskCountException.java b/src/main/java/duke/TaskCountException.java index b42b9e248..3662e528a 100644 --- a/src/main/java/duke/TaskCountException.java +++ b/src/main/java/duke/TaskCountException.java @@ -1,4 +1,4 @@ package duke; -public class TaskCountException extends Exception{ +public class TaskCountException extends Exception { } diff --git a/src/main/java/duke/Todo.java b/src/main/java/duke/Todo.java index 80195adbc..3001483b5 100644 --- a/src/main/java/duke/Todo.java +++ b/src/main/java/duke/Todo.java @@ -5,8 +5,16 @@ public class Todo extends Task{ public Todo(String description) { super(description); } + public Todo(String description, boolean isDone) { + super(description, isDone); + } public void printTask() { System.out.println("[T][" + getStatusIcon() + "] " + getDescription()); } + + @Override + public String saveTask() { + return "T | " + (isDone ? 1 : 0) + " | " + description; + } } diff --git a/src/main/java/duke/TodoException.java b/src/main/java/duke/TodoException.java index 66545fe54..541e2220f 100644 --- a/src/main/java/duke/TodoException.java +++ b/src/main/java/duke/TodoException.java @@ -1,4 +1,4 @@ package duke; -public class TodoException extends Exception{ +public class TodoException extends Exception { } From d17bf947c9c6b72f091728af7888aee2102077df Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Wed, 15 Sep 2021 01:20:58 +0800 Subject: [PATCH 19/25] Additional resolving of merge conflicts --- src/main/java/duke/Duke.java | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 95ffd4df5..2a1d01df2 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -13,8 +13,6 @@ public class Duke { public static ArrayList tasks = new ArrayList<>(); -<<<<<<< HEAD -======= public static File f = new File("data/duke.txt"); // create a File for the given file path public static void loadTasks() throws FileNotFoundException { @@ -36,7 +34,6 @@ public static void loadTasks() throws FileNotFoundException { } } } ->>>>>>> branch-Level-7 public static void printGreeting() { System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); @@ -99,8 +96,7 @@ public static void printList() { for (int i = 0; i < tasks.size(); i += 1) { System.out.print((i + 1) + "."); tasks.get(i).printTask(); -<<<<<<< HEAD -======= + } } @@ -137,7 +133,6 @@ public static void write() { writeToFile(); } catch (IOException e) { System.out.println("There has been an error writing to file."); ->>>>>>> branch-Level-7 } } @@ -156,11 +151,7 @@ public static void addTodo(String line) throws TodoException { if (line.equals("") || line.equals("todo")) { throw new TodoException(); } else { -<<<<<<< HEAD - tasks.add(new Todo (line)); -======= tasks.add(new Todo(line)); ->>>>>>> branch-Level-7 printTaskTypeResponse(); write(); } From 890f18e8137ed45a42066e81d5953e3978372d73 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Sun, 26 Sep 2021 00:01:00 +0800 Subject: [PATCH 20/25] Bug fixes --- data/duke.txt | 0 src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/duke/Duke.java | 78 ++++++++++++++----- src/main/java/duke/TaskCountException.java | 4 - .../{ => exceptions}/DeadlineException.java | 2 +- .../{ => exceptions}/DeleteException.java | 2 +- .../duke/exceptions/DeleteRangeException.java | 4 + .../duke/{ => exceptions}/DoneException.java | 2 +- .../duke/exceptions/DoneRangeException.java | 4 + .../duke/exceptions/EmptyListException.java | 4 + .../duke/{ => exceptions}/EventException.java | 2 +- .../duke/{ => exceptions}/TodoException.java | 2 +- src/main/java/duke/{ => tasks}/Deadline.java | 4 +- src/main/java/duke/{ => tasks}/Event.java | 2 +- src/main/java/duke/{ => tasks}/Task.java | 2 +- src/main/java/duke/{ => tasks}/Todo.java | 2 +- 16 files changed, 83 insertions(+), 34 deletions(-) create mode 100644 data/duke.txt create mode 100644 src/main/java/META-INF/MANIFEST.MF delete mode 100644 src/main/java/duke/TaskCountException.java rename src/main/java/duke/{ => exceptions}/DeadlineException.java (68%) rename src/main/java/duke/{ => exceptions}/DeleteException.java (67%) create mode 100644 src/main/java/duke/exceptions/DeleteRangeException.java rename src/main/java/duke/{ => exceptions}/DoneException.java (66%) create mode 100644 src/main/java/duke/exceptions/DoneRangeException.java create mode 100644 src/main/java/duke/exceptions/EmptyListException.java rename src/main/java/duke/{ => exceptions}/EventException.java (67%) rename src/main/java/duke/{ => exceptions}/TodoException.java (66%) rename src/main/java/duke/{ => tasks}/Deadline.java (92%) rename src/main/java/duke/{ => tasks}/Event.java (97%) rename src/main/java/duke/{ => tasks}/Task.java (97%) rename src/main/java/duke/{ => tasks}/Todo.java (95%) diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..2c9a9745c --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.Duke + diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 2a1d01df2..49404d5cb 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,5 +1,18 @@ package duke; +import duke.exceptions.DeleteRangeException; +import duke.exceptions.DoneRangeException; +import duke.exceptions.EmptyListException; +import duke.exceptions.DoneException; +import duke.exceptions.DeleteException; +import duke.exceptions.DeadlineException; +import duke.exceptions.EventException; +import duke.exceptions.TodoException; +import duke.tasks.Deadline; +import duke.tasks.Event; +import duke.tasks.Task; +import duke.tasks.Todo; + import java.util.ArrayList; import java.util.Scanner; import java.io.File; @@ -72,15 +85,23 @@ public static void printEventException() { } public static void printDoneException() { - System.out.println("The description of done cannot be empty."); + System.out.println("The description of done must be a number from the task list."); + } + + public static void printRangeException() { + System.out.println("That task does not exist."); } public static void printDeleteException() { - System.out.println("The description of delete cannot be empty."); + System.out.println("The description of delete must be a number from the task list."); + } + + public static void printEmptyListException() { + System.out.println("The list is empty."); } public static void printTaskTypeResponse() { - //printing different responses depending if its duke.Todo/duke.Deadline/duke.Event + //printing different responses depending if its duke.tasks.Todo/duke.tasks.Deadline/duke.tasks.Event printGotIt(); tasks.get(tasks.size() - 1).printTask(); printTaskCount(); @@ -91,22 +112,25 @@ public static void printDeleteResponse() { tasks.get(tasks.size() - 1).printTask(); } - public static void printList() { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < tasks.size(); i += 1) { - System.out.print((i + 1) + "."); - tasks.get(i).printTask(); - + public static void printList() throws EmptyListException { + if (tasks.size() == 0) { + throw new EmptyListException(); + } else { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i += 1) { + System.out.print((i + 1) + "."); + tasks.get(i).printTask(); + } } } - public static void createFile() throws IOException{ + public static void createFile() throws IOException { Path pathToFile = Paths.get("data/duke.txt"); Files.createDirectories(pathToFile.getParent()); f.createNewFile(); } - public static void initialiseTasks(){ + public static void initialiseTasks() { try { loadTasks(); } catch (FileNotFoundException e0){ @@ -136,11 +160,14 @@ public static void write() { } } - public static void taskDone(String line) throws DoneException { - if (line.equals("") || line.equals("done")) { + public static void taskDone(String line) throws DoneException, DoneRangeException { + if (!line.matches("\\d+")) { throw new DoneException(); + } + int index = Integer.parseInt(line) - 1; + if (index >= tasks.size()) { + throw new DoneRangeException(); } else { - int index = Integer.parseInt(line) - 1; tasks.get(index).setDone(); System.out.println("Nice! I've marked this task as done:"); tasks.get(index).printTask(); @@ -164,7 +191,7 @@ public static void addDeadline(String line) throws DeadlineException { //extracting the description and date String description = line.replaceAll("/.+", ""); String by = line.replaceAll(".+/by", ""); - tasks.add(new Deadline (description, by)); + tasks.add(new Deadline(description, by)); printTaskTypeResponse(); write(); } @@ -177,17 +204,20 @@ public static void addEvent(String line) throws EventException { //extracting the description and date String description = line.replaceAll("/.+", ""); String at = line.replaceAll(".+/at", ""); - tasks.add(new Event (description, at)); + tasks.add(new Event(description, at)); printTaskTypeResponse(); write(); } } - public static void deleteTask(String line) throws DeleteException { - if (line.equals("") || line.equals("delete")) { + public static void deleteTask(String line) throws DeleteException, DeleteRangeException { + if (!line.matches("\\d+")) { throw new DeleteException(); + } + int index = Integer.parseInt(line) - 1; + if (index >= tasks.size()) { + throw new DeleteRangeException(); } else { - int index = Integer.parseInt(line) - 1; printDeleteResponse(); tasks.remove(index); printTaskCount(); @@ -199,13 +229,19 @@ public static void readInput(String line) { String command = splitLine[0]; line = line.replaceAll("^" + command + " ", ""); if (command.equals("list")) { - printList(); + try { + printList(); + } catch (EmptyListException e) { + printEmptyListException(); + } } else if (command.equals("done")) { try { //find which task user is done with taskDone(line); } catch (DoneException e) { printDoneException(); + } catch (DoneRangeException e) { + printRangeException(); } } else if (command.equals("todo")) { try { @@ -230,6 +266,8 @@ public static void readInput(String line) { deleteTask(line); } catch (DeleteException e) { printDeleteException(); + } catch (DeleteRangeException e) { + printRangeException(); } } else { //error with input diff --git a/src/main/java/duke/TaskCountException.java b/src/main/java/duke/TaskCountException.java deleted file mode 100644 index 3662e528a..000000000 --- a/src/main/java/duke/TaskCountException.java +++ /dev/null @@ -1,4 +0,0 @@ -package duke; - -public class TaskCountException extends Exception { -} diff --git a/src/main/java/duke/DeadlineException.java b/src/main/java/duke/exceptions/DeadlineException.java similarity index 68% rename from src/main/java/duke/DeadlineException.java rename to src/main/java/duke/exceptions/DeadlineException.java index b6c92aff2..bee2449a1 100644 --- a/src/main/java/duke/DeadlineException.java +++ b/src/main/java/duke/exceptions/DeadlineException.java @@ -1,4 +1,4 @@ -package duke; +package duke.exceptions; public class DeadlineException extends Exception { } diff --git a/src/main/java/duke/DeleteException.java b/src/main/java/duke/exceptions/DeleteException.java similarity index 67% rename from src/main/java/duke/DeleteException.java rename to src/main/java/duke/exceptions/DeleteException.java index baea749ed..f03a5e128 100644 --- a/src/main/java/duke/DeleteException.java +++ b/src/main/java/duke/exceptions/DeleteException.java @@ -1,4 +1,4 @@ -package duke; +package duke.exceptions; public class DeleteException extends Exception { } diff --git a/src/main/java/duke/exceptions/DeleteRangeException.java b/src/main/java/duke/exceptions/DeleteRangeException.java new file mode 100644 index 000000000..09eab86a7 --- /dev/null +++ b/src/main/java/duke/exceptions/DeleteRangeException.java @@ -0,0 +1,4 @@ +package duke.exceptions; + +public class DeleteRangeException extends Exception { +} diff --git a/src/main/java/duke/DoneException.java b/src/main/java/duke/exceptions/DoneException.java similarity index 66% rename from src/main/java/duke/DoneException.java rename to src/main/java/duke/exceptions/DoneException.java index e0512ebec..c5f747d57 100644 --- a/src/main/java/duke/DoneException.java +++ b/src/main/java/duke/exceptions/DoneException.java @@ -1,4 +1,4 @@ -package duke; +package duke.exceptions; public class DoneException extends Exception { } diff --git a/src/main/java/duke/exceptions/DoneRangeException.java b/src/main/java/duke/exceptions/DoneRangeException.java new file mode 100644 index 000000000..1c9f4b5a1 --- /dev/null +++ b/src/main/java/duke/exceptions/DoneRangeException.java @@ -0,0 +1,4 @@ +package duke.exceptions; + +public class DoneRangeException extends Exception { +} diff --git a/src/main/java/duke/exceptions/EmptyListException.java b/src/main/java/duke/exceptions/EmptyListException.java new file mode 100644 index 000000000..f9e6a4248 --- /dev/null +++ b/src/main/java/duke/exceptions/EmptyListException.java @@ -0,0 +1,4 @@ +package duke.exceptions; + +public class EmptyListException extends Exception { +} diff --git a/src/main/java/duke/EventException.java b/src/main/java/duke/exceptions/EventException.java similarity index 67% rename from src/main/java/duke/EventException.java rename to src/main/java/duke/exceptions/EventException.java index 1241ce337..7dde535d8 100644 --- a/src/main/java/duke/EventException.java +++ b/src/main/java/duke/exceptions/EventException.java @@ -1,4 +1,4 @@ -package duke; +package duke.exceptions; public class EventException extends Exception { } diff --git a/src/main/java/duke/TodoException.java b/src/main/java/duke/exceptions/TodoException.java similarity index 66% rename from src/main/java/duke/TodoException.java rename to src/main/java/duke/exceptions/TodoException.java index 541e2220f..0ab691e07 100644 --- a/src/main/java/duke/TodoException.java +++ b/src/main/java/duke/exceptions/TodoException.java @@ -1,4 +1,4 @@ -package duke; +package duke.exceptions; public class TodoException extends Exception { } diff --git a/src/main/java/duke/Deadline.java b/src/main/java/duke/tasks/Deadline.java similarity index 92% rename from src/main/java/duke/Deadline.java rename to src/main/java/duke/tasks/Deadline.java index 6e3fa5fa7..4ce536a06 100644 --- a/src/main/java/duke/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -1,6 +1,6 @@ -package duke; +package duke.tasks; -public class Deadline extends Todo{ +public class Deadline extends Todo { protected String by; public Deadline(String description, String by) { diff --git a/src/main/java/duke/Event.java b/src/main/java/duke/tasks/Event.java similarity index 97% rename from src/main/java/duke/Event.java rename to src/main/java/duke/tasks/Event.java index 720852d68..c92dc384d 100644 --- a/src/main/java/duke/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -1,4 +1,4 @@ -package duke; +package duke.tasks; public class Event extends Todo{ protected String at; diff --git a/src/main/java/duke/Task.java b/src/main/java/duke/tasks/Task.java similarity index 97% rename from src/main/java/duke/Task.java rename to src/main/java/duke/tasks/Task.java index 061f21fa8..4a11cf0e9 100644 --- a/src/main/java/duke/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -1,4 +1,4 @@ -package duke; +package duke.tasks; public class Task { protected String description; diff --git a/src/main/java/duke/Todo.java b/src/main/java/duke/tasks/Todo.java similarity index 95% rename from src/main/java/duke/Todo.java rename to src/main/java/duke/tasks/Todo.java index 3001483b5..b47b86345 100644 --- a/src/main/java/duke/Todo.java +++ b/src/main/java/duke/tasks/Todo.java @@ -1,4 +1,4 @@ -package duke; +package duke.tasks; public class Todo extends Task{ From 43da2b2937d7746ec137cb1fc973e2c6e116aed2 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Mon, 27 Sep 2021 04:19:18 +0800 Subject: [PATCH 21/25] A-MoreOOP --- src/main/java/duke/Duke.java | 306 +++--------------- src/main/java/duke/Parser.java | 41 +++ src/main/java/duke/Storage.java | 95 ++++++ src/main/java/duke/Ui.java | 23 ++ src/main/java/duke/commands/AddCommand.java | 29 ++ src/main/java/duke/commands/Command.java | 15 + .../java/duke/commands/CommandOutput.java | 33 ++ .../java/duke/commands/DeleteCommand.java | 15 + src/main/java/duke/commands/DoneCommand.java | 15 + src/main/java/duke/commands/ListCommand.java | 15 + src/main/java/duke/commands/NoCommand.java | 15 + .../duke/exceptions/AlreadyDoneException.java | 10 + .../duke/exceptions/DeadlineException.java | 6 + .../java/duke/exceptions/DeleteException.java | 4 - .../exceptions/DeleteFormatException.java | 10 + .../duke/exceptions/DeleteRangeException.java | 6 + .../java/duke/exceptions/DoneException.java | 4 - .../duke/exceptions/DoneFormatException.java | 10 + .../duke/exceptions/DoneRangeException.java | 6 + .../java/duke/exceptions/EmptyException.java | 10 + .../duke/exceptions/EmptyListException.java | 6 + .../java/duke/exceptions/EventException.java | 6 + .../duke/exceptions/FileFormatException.java | 4 + .../java/duke/exceptions/TodoException.java | 6 + src/main/java/duke/tasks/Deadline.java | 9 +- src/main/java/duke/tasks/Event.java | 9 +- src/main/java/duke/tasks/Task.java | 8 +- src/main/java/duke/tasks/TaskList.java | 107 ++++++ .../java/duke/tasks/TaskListResponse.java | 13 + src/main/java/duke/tasks/TaskType.java | 5 + src/main/java/duke/tasks/Todo.java | 6 +- text-ui-test/EXPECTED.TXT | 32 +- text-ui-test/input.txt | 1 + 33 files changed, 580 insertions(+), 300 deletions(-) create mode 100644 src/main/java/duke/Parser.java create mode 100644 src/main/java/duke/Storage.java create mode 100644 src/main/java/duke/Ui.java create mode 100644 src/main/java/duke/commands/AddCommand.java create mode 100644 src/main/java/duke/commands/Command.java create mode 100644 src/main/java/duke/commands/CommandOutput.java create mode 100644 src/main/java/duke/commands/DeleteCommand.java create mode 100644 src/main/java/duke/commands/DoneCommand.java create mode 100644 src/main/java/duke/commands/ListCommand.java create mode 100644 src/main/java/duke/commands/NoCommand.java create mode 100644 src/main/java/duke/exceptions/AlreadyDoneException.java delete mode 100644 src/main/java/duke/exceptions/DeleteException.java create mode 100644 src/main/java/duke/exceptions/DeleteFormatException.java delete mode 100644 src/main/java/duke/exceptions/DoneException.java create mode 100644 src/main/java/duke/exceptions/DoneFormatException.java create mode 100644 src/main/java/duke/exceptions/EmptyException.java create mode 100644 src/main/java/duke/exceptions/FileFormatException.java create mode 100644 src/main/java/duke/tasks/TaskList.java create mode 100644 src/main/java/duke/tasks/TaskListResponse.java create mode 100644 src/main/java/duke/tasks/TaskType.java diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 49404d5cb..1df3de12b 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,291 +1,57 @@ package duke; -import duke.exceptions.DeleteRangeException; -import duke.exceptions.DoneRangeException; -import duke.exceptions.EmptyListException; -import duke.exceptions.DoneException; -import duke.exceptions.DeleteException; -import duke.exceptions.DeadlineException; -import duke.exceptions.EventException; -import duke.exceptions.TodoException; -import duke.tasks.Deadline; -import duke.tasks.Event; -import duke.tasks.Task; -import duke.tasks.Todo; +import duke.commands.Command; +import duke.commands.CommandOutput; +import duke.tasks.*; -import java.util.ArrayList; import java.util.Scanner; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - public class Duke { - public static ArrayList tasks = new ArrayList<>(); - public static File f = new File("data/duke.txt"); // create a File for the given file path - - public static void loadTasks() throws FileNotFoundException { - Scanner s = new Scanner(f); // create a Scanner using the File as the source - while (s.hasNext()) { - String task = s.nextLine(); - String[] taskSplit= task.split(" \\| "); - String taskType = taskSplit[0]; - boolean isDone = taskSplit[1].equals("1"); - String description = taskSplit[2]; - String preposition = taskSplit.length >= 4 ? taskSplit[3] : ""; - if (taskType.equals("E")){ - tasks.add(new Event(description, isDone, preposition)); - } else if (taskType.equals("D")){ - tasks.add(new Deadline(description, isDone, preposition)); - - } else { - tasks.add(new Todo(description, isDone)); - } - } - } - - public static void printGreeting() { - System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); - } - - public static void printConfused() { - System.out.println("Could you say that again?"); - } - - public static void printExit() { - System.out.println("Bye. Hope to see you again soon!"); - } - - public static void printGotIt() { - System.out.println("Got it. I've added this task:"); - } - - public static void printRemoveIt() { - System.out.println("Noted. I've removed this task:"); - } - - public static void printTaskCount() { - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - - public static void printTodoException() { - System.out.println("The description of todo cannot be empty."); - } - public static void printDeadlineException() { - System.out.println("The description of deadline cannot be empty."); - } - - public static void printEventException() { - System.out.println("The description of event cannot be empty."); - } - - public static void printDoneException() { - System.out.println("The description of done must be a number from the task list."); - } - - public static void printRangeException() { - System.out.println("That task does not exist."); - } - - public static void printDeleteException() { - System.out.println("The description of delete must be a number from the task list."); - } - - public static void printEmptyListException() { - System.out.println("The list is empty."); - } - - public static void printTaskTypeResponse() { - //printing different responses depending if its duke.tasks.Todo/duke.tasks.Deadline/duke.tasks.Event - printGotIt(); - tasks.get(tasks.size() - 1).printTask(); - printTaskCount(); - } - - public static void printDeleteResponse() { - printRemoveIt(); - tasks.get(tasks.size() - 1).printTask(); - } - - public static void printList() throws EmptyListException { - if (tasks.size() == 0) { - throw new EmptyListException(); - } else { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < tasks.size(); i += 1) { - System.out.print((i + 1) + "."); - tasks.get(i).printTask(); - } - } - } + private Storage storage; + private TaskList tasks; + private Ui ui; + private Parser parser; - public static void createFile() throws IOException { - Path pathToFile = Paths.get("data/duke.txt"); - Files.createDirectories(pathToFile.getParent()); - f.createNewFile(); + public Duke(String filePath) { + ui = new Ui(); + storage = new Storage(filePath); + parser = new Parser(); + TaskListResponse response = storage.initialiseTasks(); + tasks = response.taskList; + Ui.printMessage(response.response); } - public static void initialiseTasks() { + public CommandOutput executeCommand(Command command) { try { - loadTasks(); - } catch (FileNotFoundException e0){ - System.out.println("The file data/duke.txt is not found."); - try { - createFile(); - } catch (IOException e1){ - System.out.println("There has been an error creating a new file."); - } - System.out.println("A new file data/duke.txt has been created."); - } - } - - private static void writeToFile() throws IOException { - FileWriter fw = new FileWriter("data/duke.txt"); - for (Task list : tasks) { - fw.write(list.saveTask() + "\n"); - } - fw.close(); - } - - public static void write() { - try { - writeToFile(); - } catch (IOException e) { - System.out.println("There has been an error writing to file."); - } - } - - public static void taskDone(String line) throws DoneException, DoneRangeException { - if (!line.matches("\\d+")) { - throw new DoneException(); - } - int index = Integer.parseInt(line) - 1; - if (index >= tasks.size()) { - throw new DoneRangeException(); - } else { - tasks.get(index).setDone(); - System.out.println("Nice! I've marked this task as done:"); - tasks.get(index).printTask(); - } - } - - public static void addTodo(String line) throws TodoException { - if (line.equals("") || line.equals("todo")) { - throw new TodoException(); - } else { - tasks.add(new Todo(line)); - printTaskTypeResponse(); - write(); - } - } - - public static void addDeadline(String line) throws DeadlineException { - if (!line.matches("(.*)/by(.*)")) { - throw new DeadlineException(); - } else { - //extracting the description and date - String description = line.replaceAll("/.+", ""); - String by = line.replaceAll(".+/by", ""); - tasks.add(new Deadline(description, by)); - printTaskTypeResponse(); - write(); - } - } - - public static void addEvent(String line) throws EventException { - if (!line.matches("(.*)/at(.*)")) { - throw new EventException(); - } else { - //extracting the description and date - String description = line.replaceAll("/.+", ""); - String at = line.replaceAll(".+/at", ""); - tasks.add(new Event(description, at)); - printTaskTypeResponse(); - write(); - } - } - - public static void deleteTask(String line) throws DeleteException, DeleteRangeException { - if (!line.matches("\\d+")) { - throw new DeleteException(); - } - int index = Integer.parseInt(line) - 1; - if (index >= tasks.size()) { - throw new DeleteRangeException(); - } else { - printDeleteResponse(); - tasks.remove(index); - printTaskCount(); + return command.execute(); + } catch (Exception e){ + return new CommandOutput(e.getMessage(), false, null); } } - public static void readInput(String line) { - String[] splitLine = line.split(" ", 2); - String command = splitLine[0]; - line = line.replaceAll("^" + command + " ", ""); - if (command.equals("list")) { - try { - printList(); - } catch (EmptyListException e) { - printEmptyListException(); - } - } else if (command.equals("done")) { - try { - //find which task user is done with - taskDone(line); - } catch (DoneException e) { - printDoneException(); - } catch (DoneRangeException e) { - printRangeException(); - } - } else if (command.equals("todo")) { - try { - addTodo(line); - } catch (TodoException e) { - printTodoException(); - } - } else if (command.equals("deadline")) { - try { - addDeadline(line); - } catch (DeadlineException e) { - printDeadlineException(); - } - } else if (command.equals("event")) { - try { - addEvent(line); - } catch (EventException e) { - printEventException(); - } - } else if (command.equals("delete")) { - try { - deleteTask(line); - } catch (DeleteException e) { - printDeleteException(); - } catch (DeleteRangeException e) { - printRangeException(); - } - } else { - //error with input - printConfused(); - } - } - - public static void main(String[] args) { - initialiseTasks(); - printGreeting(); + public void run() { + ui.printGreeting(); String line; Scanner in = new Scanner(System.in); + ui.printPrompt(); line = in.nextLine(); - //continue to run program unless types "bye" to exit program - while(!line.equals("bye")) { - readInput(line); + while(!line.equals(parser.BYE)) { + Command command = parser.parseCommand(tasks, line); + CommandOutput commandOutput = executeCommand(command); + if (commandOutput.isUpdated()) { + tasks = commandOutput.getTaskList(); + String errorMessage = storage.write(tasks); + ui.printMessage(errorMessage); + } + ui.printMessage(commandOutput.getResponse()); + ui.printPrompt(); line = in.nextLine(); } - printExit(); + ui.printExit(); + } + + public static void main(String[] args) { + new Duke("data/duke.txt").run(); } } diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 000000000..ebad1c37f --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,41 @@ +package duke; + +import duke.commands.Command; +import duke.commands.ListCommand; +import duke.commands.AddCommand; +import duke.commands.NoCommand; +import duke.commands.DoneCommand; +import duke.commands.DeleteCommand; +import duke.tasks.TaskList; +import duke.tasks.TaskType; + +public class Parser { + + public final String LIST = "list"; + public final String TODO = "todo"; + public final String DEADLINE = "deadline"; + public final String EVENT = "event"; + public final String DONE = "done"; + public final String DELETE = "delete"; + public final String BYE = "bye"; + + public Command parseCommand(TaskList taskList, String line) { + final String[] splitLine = line.split(" ", 2); + final String command = splitLine[0]; + final String userCommand = line.replaceAll("^" + command + " ", ""); + if (command.equals(LIST)) { + return new ListCommand(taskList); + } else if (command.equals(TODO)) { + return new AddCommand(taskList, userCommand, TaskType.TODO); + } else if (command.equals(DEADLINE)) { + return new AddCommand(taskList, userCommand, TaskType.DEADLINE); + } else if (command.equals(EVENT)) { + return new AddCommand(taskList, userCommand, TaskType.EVENT); + } else if (command.equals(DONE)) { + return new DoneCommand(taskList, userCommand); + } else if (command.equals(DELETE)) { + return new DeleteCommand(taskList, userCommand); + } + return new NoCommand(taskList); + } +} diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java new file mode 100644 index 000000000..3fc7da82a --- /dev/null +++ b/src/main/java/duke/Storage.java @@ -0,0 +1,95 @@ +package duke; + +import duke.exceptions.FileFormatException; +import duke.tasks.Deadline; +import duke.tasks.Event; +import duke.tasks.Task; +import duke.tasks.Todo; +import duke.tasks.TaskList; +import duke.tasks.TaskListResponse; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; + +public class Storage { + + private final String filePath; + private final File f; + + public Storage(String filePath) { + this.filePath = filePath; + this.f = new File(filePath); + } + + public void loadTasks(TaskList taskList) throws FileNotFoundException, FileFormatException { + Scanner s = new Scanner(f); // create a Scanner using the File as the source + while (s.hasNext()) { + String task = s.nextLine(); + if (!task.matches("T\\s\\|\\s[01]\\s\\|.+") + && !task.matches("[ED]\\\\s\\\\|\\\\s[01]\\\\s\\\\|.+\\\\|.+")) { + throw new FileFormatException(); + } + String[] taskSplit= task.split(" \\| "); + String taskType = taskSplit[0]; + boolean isDone = taskSplit[1].equals("1"); + String description = taskSplit[2]; + String preposition = taskSplit.length >= 4 ? taskSplit[3] : ""; + if (taskType.equals("E")){ + taskList.tasks.add(new Event(description, isDone, preposition)); + } else if (taskType.equals("D")){ + taskList.tasks.add(new Deadline(description, isDone, preposition)); + } else { + taskList.tasks.add(new Todo(description, isDone)); + } + } + } + + public void createFile() throws IOException { + Path pathToFile = Paths.get("data/duke.txt"); + Files.createDirectories(pathToFile.getParent()); + f.createNewFile(); + } + + public TaskListResponse initialiseTasks() { + TaskList taskList = new TaskList(); + String response = ""; + try { + loadTasks(taskList); + } catch (FileNotFoundException e0){ + response = response + "The file data/duke.txt is not found."; + try { + createFile(); + } catch (IOException e1) { + response = response + "There has been an error creating a new file. "; + } + response = response + "A new file data/duke.txt has been created."; + } catch (FileFormatException e2) { + response = response + "There is a formatting error with the save file."; + taskList.clearList(); + } + return new TaskListResponse(taskList, response); + } + + private void writeToFile(TaskList taskList) throws IOException { + FileWriter fw = new FileWriter("data/duke.txt"); + for (Task list : taskList.tasks) { + fw.write(list.saveTask() + "\n"); + } + fw.close(); + } + + public String write(TaskList taskList) { + try { + writeToFile(taskList); + } catch (IOException e) { + return "There has been an error writing to file."; + } + return ""; + } +} diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java new file mode 100644 index 000000000..fdb9b556a --- /dev/null +++ b/src/main/java/duke/Ui.java @@ -0,0 +1,23 @@ +package duke; + +public class Ui { + + public static void printMessage(String line) { + if (line.isEmpty()) { + return; + } + System.out.println(line); + } + + public static void printGreeting() { + printMessage("Hello! I'm Duke."); + } + + public static void printPrompt() { + printMessage("What can I do for you?"); + } + + public static void printExit() { + printMessage("Bye. Hope to see you again soon!"); + } +} diff --git a/src/main/java/duke/commands/AddCommand.java b/src/main/java/duke/commands/AddCommand.java new file mode 100644 index 000000000..d413e805b --- /dev/null +++ b/src/main/java/duke/commands/AddCommand.java @@ -0,0 +1,29 @@ +package duke.commands; + +import duke.tasks.TaskList; +import duke.tasks.TaskType; + +public class AddCommand extends Command { + + public TaskType type; + + public AddCommand(TaskList taskList, String userCommand, TaskType type) { + super(taskList, userCommand); + this.type = type; + } + + @Override + public CommandOutput execute() throws Exception { + String response = ""; + + if (type == TaskType.TODO) { + response = taskList.addTodo(userCommand); + } else if (type == TaskType.DEADLINE) { + response = taskList.addDeadline(userCommand); + } else if (type == TaskType.EVENT){ + response = taskList.addEvent(userCommand); + } + + return new CommandOutput(response, true, taskList); + } +} diff --git a/src/main/java/duke/commands/Command.java b/src/main/java/duke/commands/Command.java new file mode 100644 index 000000000..70fbb6a86 --- /dev/null +++ b/src/main/java/duke/commands/Command.java @@ -0,0 +1,15 @@ +package duke.commands; + +import duke.tasks.TaskList; + +public abstract class Command { + protected TaskList taskList; + protected String userCommand; + + protected Command(TaskList taskList, String userCommand) { + this.taskList = taskList; + this.userCommand = userCommand; + } + + public abstract CommandOutput execute() throws Exception; +} diff --git a/src/main/java/duke/commands/CommandOutput.java b/src/main/java/duke/commands/CommandOutput.java new file mode 100644 index 000000000..1c29bbe99 --- /dev/null +++ b/src/main/java/duke/commands/CommandOutput.java @@ -0,0 +1,33 @@ +package duke.commands; + +import duke.tasks.TaskList; + +public class CommandOutput { + private String response; + private boolean updated; + private TaskList taskList; + + public CommandOutput(String response, boolean updated) { + this.response = response; + this.updated = updated; + taskList = null; + } + + public CommandOutput(String response, boolean updated, TaskList taskList) { + this.response = response; + this.updated = updated; + this.taskList = taskList; + } + + public String getResponse() { + return response; + } + + public boolean isUpdated() { + return updated; + } + + public TaskList getTaskList() { + return taskList; + } +} diff --git a/src/main/java/duke/commands/DeleteCommand.java b/src/main/java/duke/commands/DeleteCommand.java new file mode 100644 index 000000000..424a81b64 --- /dev/null +++ b/src/main/java/duke/commands/DeleteCommand.java @@ -0,0 +1,15 @@ +package duke.commands; + +import duke.tasks.TaskList; + +public class DeleteCommand extends Command { + public DeleteCommand(TaskList taskList, String userCommand) { + super(taskList, userCommand); + } + + @Override + public CommandOutput execute() throws Exception { + String response = taskList.deleteTask(userCommand); + return new CommandOutput(response, true, taskList); + } +} diff --git a/src/main/java/duke/commands/DoneCommand.java b/src/main/java/duke/commands/DoneCommand.java new file mode 100644 index 000000000..c55a37320 --- /dev/null +++ b/src/main/java/duke/commands/DoneCommand.java @@ -0,0 +1,15 @@ +package duke.commands; + +import duke.tasks.TaskList; + +public class DoneCommand extends Command { + public DoneCommand(TaskList taskList, String userCommand) { + super(taskList, userCommand); + } + + @Override + public CommandOutput execute() throws Exception { + String response = taskList.taskDone(userCommand); + return new CommandOutput(response, true, taskList); + } +} diff --git a/src/main/java/duke/commands/ListCommand.java b/src/main/java/duke/commands/ListCommand.java new file mode 100644 index 000000000..e6c64e6fb --- /dev/null +++ b/src/main/java/duke/commands/ListCommand.java @@ -0,0 +1,15 @@ +package duke.commands; + +import duke.tasks.TaskList; + +public class ListCommand extends Command { + public ListCommand(TaskList taskList) { + super(taskList, ""); + } + + @Override + public CommandOutput execute() throws Exception { + String response = taskList.printList(); + return new CommandOutput(response, false, taskList); + } +} diff --git a/src/main/java/duke/commands/NoCommand.java b/src/main/java/duke/commands/NoCommand.java new file mode 100644 index 000000000..ca31ca4fa --- /dev/null +++ b/src/main/java/duke/commands/NoCommand.java @@ -0,0 +1,15 @@ +package duke.commands; + +import duke.tasks.TaskList; + +public class NoCommand extends Command { + public NoCommand(TaskList taskList) { + super(taskList, ""); + } + + @Override + public CommandOutput execute() throws Exception { + taskList.noResponse(); + return new CommandOutput("", false, taskList); + } +} diff --git a/src/main/java/duke/exceptions/AlreadyDoneException.java b/src/main/java/duke/exceptions/AlreadyDoneException.java new file mode 100644 index 000000000..d88c37d9e --- /dev/null +++ b/src/main/java/duke/exceptions/AlreadyDoneException.java @@ -0,0 +1,10 @@ +package duke.exceptions; + +public class AlreadyDoneException extends Exception { + public final String DONE_RESPONSE = "This task is already completed."; + + @Override + public String getMessage(){ + return DONE_RESPONSE; + } +} diff --git a/src/main/java/duke/exceptions/DeadlineException.java b/src/main/java/duke/exceptions/DeadlineException.java index bee2449a1..4912c46bb 100644 --- a/src/main/java/duke/exceptions/DeadlineException.java +++ b/src/main/java/duke/exceptions/DeadlineException.java @@ -1,4 +1,10 @@ package duke.exceptions; public class DeadlineException extends Exception { + public final String DEADLINE_RESPONSE = "The description of deadline cannot be empty."; + + @Override + public String getMessage() { + return DEADLINE_RESPONSE; + } } diff --git a/src/main/java/duke/exceptions/DeleteException.java b/src/main/java/duke/exceptions/DeleteException.java deleted file mode 100644 index f03a5e128..000000000 --- a/src/main/java/duke/exceptions/DeleteException.java +++ /dev/null @@ -1,4 +0,0 @@ -package duke.exceptions; - -public class DeleteException extends Exception { -} diff --git a/src/main/java/duke/exceptions/DeleteFormatException.java b/src/main/java/duke/exceptions/DeleteFormatException.java new file mode 100644 index 000000000..afdd15bca --- /dev/null +++ b/src/main/java/duke/exceptions/DeleteFormatException.java @@ -0,0 +1,10 @@ +package duke.exceptions; + +public class DeleteFormatException extends Exception { + public final String DELETE_FORMAT_RESPONSE = "The description of delete cannot be empty."; + + @Override + public String getMessage(){ + return DELETE_FORMAT_RESPONSE; + } +} diff --git a/src/main/java/duke/exceptions/DeleteRangeException.java b/src/main/java/duke/exceptions/DeleteRangeException.java index 09eab86a7..3aa75610b 100644 --- a/src/main/java/duke/exceptions/DeleteRangeException.java +++ b/src/main/java/duke/exceptions/DeleteRangeException.java @@ -1,4 +1,10 @@ package duke.exceptions; public class DeleteRangeException extends Exception { + public final String DELETE_RANGE_RESPONSE = "That task does not exist."; + + @Override + public String getMessage(){ + return DELETE_RANGE_RESPONSE; + } } diff --git a/src/main/java/duke/exceptions/DoneException.java b/src/main/java/duke/exceptions/DoneException.java deleted file mode 100644 index c5f747d57..000000000 --- a/src/main/java/duke/exceptions/DoneException.java +++ /dev/null @@ -1,4 +0,0 @@ -package duke.exceptions; - -public class DoneException extends Exception { -} diff --git a/src/main/java/duke/exceptions/DoneFormatException.java b/src/main/java/duke/exceptions/DoneFormatException.java new file mode 100644 index 000000000..ef382d44e --- /dev/null +++ b/src/main/java/duke/exceptions/DoneFormatException.java @@ -0,0 +1,10 @@ +package duke.exceptions; + +public class DoneFormatException extends Exception { + public final String DONE_FORMAT_RESPONSE = "The description of done cannot be empty."; + + @Override + public String getMessage(){ + return DONE_FORMAT_RESPONSE; + } +} diff --git a/src/main/java/duke/exceptions/DoneRangeException.java b/src/main/java/duke/exceptions/DoneRangeException.java index 1c9f4b5a1..6d8627db3 100644 --- a/src/main/java/duke/exceptions/DoneRangeException.java +++ b/src/main/java/duke/exceptions/DoneRangeException.java @@ -1,4 +1,10 @@ package duke.exceptions; public class DoneRangeException extends Exception { + public final String DONE_RANGE_RESPONSE = "That task does not exist."; + + @Override + public String getMessage(){ + return DONE_RANGE_RESPONSE; + } } diff --git a/src/main/java/duke/exceptions/EmptyException.java b/src/main/java/duke/exceptions/EmptyException.java new file mode 100644 index 000000000..459054af9 --- /dev/null +++ b/src/main/java/duke/exceptions/EmptyException.java @@ -0,0 +1,10 @@ +package duke.exceptions; + +public class EmptyException extends Exception { + public final String EMPTY_RESPONSE = "Command is empty."; + + @Override + public String getMessage(){ + return EMPTY_RESPONSE; + } +} diff --git a/src/main/java/duke/exceptions/EmptyListException.java b/src/main/java/duke/exceptions/EmptyListException.java index f9e6a4248..a72c34663 100644 --- a/src/main/java/duke/exceptions/EmptyListException.java +++ b/src/main/java/duke/exceptions/EmptyListException.java @@ -1,4 +1,10 @@ package duke.exceptions; public class EmptyListException extends Exception { + public final String EMPTY_LIST_RESPONSE = "The list is empty."; + + @Override + public String getMessage(){ + return EMPTY_LIST_RESPONSE; + } } diff --git a/src/main/java/duke/exceptions/EventException.java b/src/main/java/duke/exceptions/EventException.java index 7dde535d8..d064169ad 100644 --- a/src/main/java/duke/exceptions/EventException.java +++ b/src/main/java/duke/exceptions/EventException.java @@ -1,4 +1,10 @@ package duke.exceptions; public class EventException extends Exception { + public final String EVENT_RESPONSE = "The description of event cannot be empty."; + + @Override + public String getMessage(){ + return EVENT_RESPONSE; + } } diff --git a/src/main/java/duke/exceptions/FileFormatException.java b/src/main/java/duke/exceptions/FileFormatException.java new file mode 100644 index 000000000..4eaf6d9af --- /dev/null +++ b/src/main/java/duke/exceptions/FileFormatException.java @@ -0,0 +1,4 @@ +package duke.exceptions; + +public class FileFormatException extends Exception { +} diff --git a/src/main/java/duke/exceptions/TodoException.java b/src/main/java/duke/exceptions/TodoException.java index 0ab691e07..9fcc93f53 100644 --- a/src/main/java/duke/exceptions/TodoException.java +++ b/src/main/java/duke/exceptions/TodoException.java @@ -1,4 +1,10 @@ package duke.exceptions; public class TodoException extends Exception { + public final String TODO_RESPONSE = "The description of todo cannot be empty."; + + @Override + public String getMessage(){ + return TODO_RESPONSE; + } } diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java index 4ce536a06..6de943f67 100644 --- a/src/main/java/duke/tasks/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -1,6 +1,7 @@ package duke.tasks; -public class Deadline extends Todo { +public class Deadline extends Task { + private TaskType type = TaskType.DEADLINE; protected String by; public Deadline(String description, String by) { @@ -20,9 +21,9 @@ public void setBy(String by) { this.by = by; } - public void printTask() { - System.out.print("[D][" + getStatusIcon() + "] " + getDescription()); - System.out.println("(by:" + by + ")"); + @Override + public String printTask() { + return "[D][" + getStatusIcon() + "] " + getDescription() + " (by:" + by + ")"; } @Override diff --git a/src/main/java/duke/tasks/Event.java b/src/main/java/duke/tasks/Event.java index c92dc384d..314245efc 100644 --- a/src/main/java/duke/tasks/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -1,6 +1,7 @@ package duke.tasks; -public class Event extends Todo{ +public class Event extends Task { + private TaskType type = TaskType.EVENT; protected String at; public Event(String description, String at) { @@ -19,9 +20,9 @@ public void setAt(String at) { this.at = at; } - public void printTask() { - System.out.print("[E][" + getStatusIcon() + "] " + getDescription()); - System.out.println("(at:" + at + ")"); + @Override + public String printTask() { + return "[E][" + getStatusIcon() + "] " + getDescription() + "(at:" + at + ")"; } @Override diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java index 4a11cf0e9..069167535 100644 --- a/src/main/java/duke/tasks/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -23,12 +23,16 @@ public String getDescription() { return description; } + public boolean isDone() { + return isDone; + } + public void setDone() { isDone = true; } - public void printTask() { - System.out.println(description); + public String printTask() { + return ""; } public String saveTask() { diff --git a/src/main/java/duke/tasks/TaskList.java b/src/main/java/duke/tasks/TaskList.java new file mode 100644 index 000000000..cbc728175 --- /dev/null +++ b/src/main/java/duke/tasks/TaskList.java @@ -0,0 +1,107 @@ +package duke.tasks; + +import duke.exceptions.*; + +import java.util.ArrayList; + +public class TaskList { + public ArrayList tasks = new ArrayList<>(); + + public String ADDED = "Got it. I've added this task:"; + public String REMOVED = "Noted. I've removed this task:"; + public String ACKNOWLEDGED = "Nice! I've marked this task as done:"; + public String LIST_START = "Here are the tasks in your list:"; + + public String TaskListResponse(String line, int index){ + return line + "\n" + tasks.get(index).getDescription(); + } + + public void noResponse () throws EmptyException { + throw new EmptyException(); + } + + public String printTaskCount() { + return "Now you have " + tasks.size() + " tasks in the list."; + } + + public String printList() throws EmptyListException { + if (tasks.size() == 0) { + throw new EmptyListException(); + } + String listResponse = LIST_START; + for (int i = 1; i <= tasks.size(); i += 1) { + listResponse = listResponse + "\n" + i + ". " + tasks.get(i - 1).printTask(); + } + return listResponse; + } + + public TaskList() { + } + + public String taskDone(String line) + throws DoneFormatException, AlreadyDoneException, DoneRangeException { + if (!line.matches("\\d+")) { + throw new DoneFormatException(); + } + int index = Integer.parseInt(line) - 1; + if (index >= tasks.size()) { + throw new DoneRangeException(); + } else if (tasks.get(index).isDone()) { + throw new AlreadyDoneException(); + } else { + tasks.get(index).setDone(); + return TaskListResponse(ACKNOWLEDGED, index); + } + } + + public String addTodo(String line) throws TodoException { + if (line.equals("") || line.equals("todo")) { + throw new TodoException(); + } else { + tasks.add(new Todo(line)); + return TaskListResponse(ADDED, tasks.size()-1) +"\n" + printTaskCount(); + } + } + + public String addDeadline(String line) throws DeadlineException { + if (!line.matches("(.*)/by(.*)")) { + throw new DeadlineException(); + } else { + //extracting the description and date + String description = line.replaceAll("/.+", ""); + String by = line.replaceAll(".+/by", ""); + tasks.add(new Deadline(description, by)); + return TaskListResponse(ADDED, tasks.size()-1) +"\n" + printTaskCount(); + } + } + + public String addEvent(String line) throws EventException { + if (!line.matches("(.*)/at(.*)")) { + throw new EventException(); + } else { + //extracting the description and date + String description = line.replaceAll("/.+", ""); + String at = line.replaceAll(".+/at", ""); + tasks.add(new Event(description, at)); + return TaskListResponse(ADDED, tasks.size()-1) +"\n" + printTaskCount(); + } + } + + public String deleteTask(String line) throws DeleteFormatException, DeleteRangeException { + if (!line.matches("\\d+")) { + throw new DeleteFormatException(); + } + int index = Integer.parseInt(line) - 1; + if (index >= tasks.size()) { + throw new DeleteRangeException(); + } else { + String response = TaskListResponse(REMOVED, index); + tasks.remove(index); + return response +"\n" + printTaskCount(); + } + } + + public void clearList(){ + tasks.clear(); + } +} diff --git a/src/main/java/duke/tasks/TaskListResponse.java b/src/main/java/duke/tasks/TaskListResponse.java new file mode 100644 index 000000000..b49e46785 --- /dev/null +++ b/src/main/java/duke/tasks/TaskListResponse.java @@ -0,0 +1,13 @@ +package duke.tasks; + +import duke.tasks.TaskList; + +public class TaskListResponse { + public TaskList taskList; + public String response; + + public TaskListResponse(TaskList taskList, String response) { + this.taskList = taskList; + this.response = response; + } +} diff --git a/src/main/java/duke/tasks/TaskType.java b/src/main/java/duke/tasks/TaskType.java new file mode 100644 index 000000000..e641c880a --- /dev/null +++ b/src/main/java/duke/tasks/TaskType.java @@ -0,0 +1,5 @@ +package duke.tasks; + +public enum TaskType { + TODO, EVENT, DEADLINE +} diff --git a/src/main/java/duke/tasks/Todo.java b/src/main/java/duke/tasks/Todo.java index b47b86345..30f8c2c3c 100644 --- a/src/main/java/duke/tasks/Todo.java +++ b/src/main/java/duke/tasks/Todo.java @@ -1,6 +1,7 @@ package duke.tasks; public class Todo extends Task{ + private TaskType type = TaskType.TODO; public Todo(String description) { super(description); @@ -9,8 +10,9 @@ public Todo(String description, boolean isDone) { super(description, isDone); } - public void printTask() { - System.out.println("[T][" + getStatusIcon() + "] " + getDescription()); + @Override + public String printTask() { + return "[T][" + getStatusIcon() + "] " + getDescription(); } @Override diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 0cdc00662..791744bdc 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,20 +1,32 @@ -Hello! I'm Duke +The file data/duke.txt is not found.A new file data/duke.txt has been created. +Hello! I'm Duke. +What can I do for you? +Command is empty. What can I do for you? -Could you say that again? Got it. I've added this task: -[T][ ] eat cake +eat cake Now you have 1 tasks in the list. +What can I do for you? Got it. I've added this task: -[D][ ] eat chocolate cake (by: Friday 2pm) +eat chocolate cake Now you have 2 tasks in the list. +What can I do for you? Got it. I've added this task: -[E][ ] attend cake buffet (at: Saturday 5pm) +attend cake buffet Now you have 3 tasks in the list. +What can I do for you? Nice! I've marked this task as done: -[T][X] eat cake +eat cake +What can I do for you? +The description of done cannot be empty. +What can I do for you? The description of todo cannot be empty. +What can I do for you? Here are the tasks in your list: -1.[T][X] eat cake -2.[D][ ] eat chocolate cake (by: Friday 2pm) -3.[E][ ] attend cake buffet (at: Saturday 5pm) -Could you say that again? +1. [T][X] eat cake +2. [D][ ] eat chocolate cake (by: Friday 2pm) +3. [E][ ] attend cake buffet (at: Saturday 5pm) +What can I do for you? +Command is empty. +What can I do for you? +Bye. Hope to see you again soon! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index bec17eb26..375c6cf2c 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -3,6 +3,7 @@ todo eat cake deadline eat chocolate cake /by Friday 2pm event attend cake buffet /at Saturday 5pm done 1 +done a todo list cake From e39676bfab450f631a631b4bd0b93ca19501821b Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Mon, 27 Sep 2021 11:02:48 +0800 Subject: [PATCH 22/25] Level-9 --- data/duke.txt | 0 src/main/java/duke/Parser.java | 4 ++++ src/main/java/duke/commands/FindCommand.java | 16 ++++++++++++++++ src/main/java/duke/commands/ListCommand.java | 2 +- .../duke/exceptions/NotFoundException.java | 10 ++++++++++ src/main/java/duke/tasks/TaskList.java | 19 +++++++++++++++++++ 6 files changed, 50 insertions(+), 1 deletion(-) delete mode 100644 data/duke.txt create mode 100644 src/main/java/duke/commands/FindCommand.java create mode 100644 src/main/java/duke/exceptions/NotFoundException.java diff --git a/data/duke.txt b/data/duke.txt deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index ebad1c37f..afbd87e51 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -3,6 +3,7 @@ import duke.commands.Command; import duke.commands.ListCommand; import duke.commands.AddCommand; +import duke.commands.FindCommand; import duke.commands.NoCommand; import duke.commands.DoneCommand; import duke.commands.DeleteCommand; @@ -15,6 +16,7 @@ public class Parser { public final String TODO = "todo"; public final String DEADLINE = "deadline"; public final String EVENT = "event"; + public final String FIND = "find"; public final String DONE = "done"; public final String DELETE = "delete"; public final String BYE = "bye"; @@ -31,6 +33,8 @@ public Command parseCommand(TaskList taskList, String line) { return new AddCommand(taskList, userCommand, TaskType.DEADLINE); } else if (command.equals(EVENT)) { return new AddCommand(taskList, userCommand, TaskType.EVENT); + } else if (command.equals(FIND)) { + return new FindCommand(taskList, userCommand); } else if (command.equals(DONE)) { return new DoneCommand(taskList, userCommand); } else if (command.equals(DELETE)) { diff --git a/src/main/java/duke/commands/FindCommand.java b/src/main/java/duke/commands/FindCommand.java new file mode 100644 index 000000000..f2347ec8b --- /dev/null +++ b/src/main/java/duke/commands/FindCommand.java @@ -0,0 +1,16 @@ +package duke.commands; + +import duke.tasks.TaskList; + +public class FindCommand extends Command { + public FindCommand(TaskList taskList, String userCommand){ + super(taskList, userCommand); + } + + @Override + public CommandOutput execute() throws Exception{ + String response = taskList.findTask(userCommand); + + return new CommandOutput(response, false); + } +} diff --git a/src/main/java/duke/commands/ListCommand.java b/src/main/java/duke/commands/ListCommand.java index e6c64e6fb..4556e8138 100644 --- a/src/main/java/duke/commands/ListCommand.java +++ b/src/main/java/duke/commands/ListCommand.java @@ -10,6 +10,6 @@ public ListCommand(TaskList taskList) { @Override public CommandOutput execute() throws Exception { String response = taskList.printList(); - return new CommandOutput(response, false, taskList); + return new CommandOutput(response, false); } } diff --git a/src/main/java/duke/exceptions/NotFoundException.java b/src/main/java/duke/exceptions/NotFoundException.java new file mode 100644 index 000000000..9fff3744f --- /dev/null +++ b/src/main/java/duke/exceptions/NotFoundException.java @@ -0,0 +1,10 @@ +package duke.exceptions; + +public class NotFoundException extends Exception { + public final String NOT_FOUND_RESPONSE = "There are no matching tasks."; + + @Override + public String getMessage(){ + return NOT_FOUND_RESPONSE; + } +} diff --git a/src/main/java/duke/tasks/TaskList.java b/src/main/java/duke/tasks/TaskList.java index cbc728175..fd70c1dda 100644 --- a/src/main/java/duke/tasks/TaskList.java +++ b/src/main/java/duke/tasks/TaskList.java @@ -11,6 +11,7 @@ public class TaskList { public String REMOVED = "Noted. I've removed this task:"; public String ACKNOWLEDGED = "Nice! I've marked this task as done:"; public String LIST_START = "Here are the tasks in your list:"; + public String FIND_START = "Here are the matching tasks in your list:"; public String TaskListResponse(String line, int index){ return line + "\n" + tasks.get(index).getDescription(); @@ -101,6 +102,24 @@ public String deleteTask(String line) throws DeleteFormatException, DeleteRangeE } } + public String findTask(String line) throws NotFoundException { + String foundList = FIND_START; + + int tasksFound = 0; + + for (int i = 1; i <= tasks.size(); ++i) { + if (tasks.get(i - 1).getDescription().toLowerCase().contains(line.toLowerCase())) { + tasksFound += 1; + foundList = foundList + "\n" + i + ". " + tasks.get(i - 1).printTask(); + } + } + + if (tasksFound == 0){ + throw new NotFoundException(); + } + return foundList; + } + public void clearList(){ tasks.clear(); } From 76199bd713985a491b297afe999bc0d0b73966c5 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Mon, 27 Sep 2021 11:46:45 +0800 Subject: [PATCH 23/25] A-JavaDoc --- src/main/java/duke/Duke.java | 2 ++ src/main/java/duke/Parser.java | 1 + src/main/java/duke/Storage.java | 5 +++++ src/main/java/duke/Ui.java | 1 + src/main/java/duke/commands/Command.java | 2 ++ src/main/java/duke/commands/CommandOutput.java | 1 + src/main/java/duke/commands/DeleteCommand.java | 1 + src/main/java/duke/commands/DoneCommand.java | 1 + src/main/java/duke/commands/FindCommand.java | 1 + src/main/java/duke/commands/ListCommand.java | 1 + src/main/java/duke/commands/NoCommand.java | 1 + .../java/duke/exceptions/AlreadyDoneException.java | 2 ++ src/main/java/duke/exceptions/DeadlineException.java | 4 +++- .../java/duke/exceptions/DeleteFormatException.java | 4 +++- .../java/duke/exceptions/DeleteRangeException.java | 2 ++ .../java/duke/exceptions/DoneFormatException.java | 4 +++- src/main/java/duke/exceptions/DoneRangeException.java | 2 ++ src/main/java/duke/exceptions/EmptyException.java | 2 ++ src/main/java/duke/exceptions/EmptyListException.java | 2 ++ src/main/java/duke/exceptions/EventException.java | 4 +++- .../java/duke/exceptions/FileFormatException.java | 1 + src/main/java/duke/exceptions/NotFoundException.java | 2 ++ src/main/java/duke/exceptions/TodoException.java | 4 +++- src/main/java/duke/tasks/Deadline.java | 1 + src/main/java/duke/tasks/Event.java | 1 + src/main/java/duke/tasks/Task.java | 1 + src/main/java/duke/tasks/TaskList.java | 11 +++++++++-- src/main/java/duke/tasks/TaskListResponse.java | 3 +-- src/main/java/duke/tasks/Todo.java | 1 + text-ui-test/EXPECTED.TXT | 8 +++----- text-ui-test/input.txt | 4 +--- 31 files changed, 63 insertions(+), 17 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 1df3de12b..e0704183c 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -22,6 +22,7 @@ public Duke(String filePath) { Ui.printMessage(response.response); } + //attempts to execute the user command public CommandOutput executeCommand(Command command) { try { return command.execute(); @@ -30,6 +31,7 @@ public CommandOutput executeCommand(Command command) { } } + //runs the program public void run() { ui.printGreeting(); String line; diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index afbd87e51..c9faa8efe 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -21,6 +21,7 @@ public class Parser { public final String DELETE = "delete"; public final String BYE = "bye"; + //executes program according to user command public Command parseCommand(TaskList taskList, String line) { final String[] splitLine = line.split(" ", 2); final String command = splitLine[0]; diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 3fc7da82a..9ba5b3ee6 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -27,6 +27,7 @@ public Storage(String filePath) { this.f = new File(filePath); } + //load tasks from save file if save file exists public void loadTasks(TaskList taskList) throws FileNotFoundException, FileFormatException { Scanner s = new Scanner(f); // create a Scanner using the File as the source while (s.hasNext()) { @@ -50,12 +51,14 @@ public void loadTasks(TaskList taskList) throws FileNotFoundException, FileForma } } + //creates a save file public void createFile() throws IOException { Path pathToFile = Paths.get("data/duke.txt"); Files.createDirectories(pathToFile.getParent()); f.createNewFile(); } + //program output for save file public TaskListResponse initialiseTasks() { TaskList taskList = new TaskList(); String response = ""; @@ -76,6 +79,7 @@ public TaskListResponse initialiseTasks() { return new TaskListResponse(taskList, response); } + //writing to save file private void writeToFile(TaskList taskList) throws IOException { FileWriter fw = new FileWriter("data/duke.txt"); for (Task list : taskList.tasks) { @@ -84,6 +88,7 @@ private void writeToFile(TaskList taskList) throws IOException { fw.close(); } + //program output for writing to save file public String write(TaskList taskList) { try { writeToFile(taskList); diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index fdb9b556a..46bab224b 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -2,6 +2,7 @@ public class Ui { + //prints program output public static void printMessage(String line) { if (line.isEmpty()) { return; diff --git a/src/main/java/duke/commands/Command.java b/src/main/java/duke/commands/Command.java index 70fbb6a86..f9b109ab2 100644 --- a/src/main/java/duke/commands/Command.java +++ b/src/main/java/duke/commands/Command.java @@ -3,6 +3,7 @@ import duke.tasks.TaskList; public abstract class Command { + protected TaskList taskList; protected String userCommand; @@ -11,5 +12,6 @@ protected Command(TaskList taskList, String userCommand) { this.userCommand = userCommand; } + //attempts to execute program according to user command public abstract CommandOutput execute() throws Exception; } diff --git a/src/main/java/duke/commands/CommandOutput.java b/src/main/java/duke/commands/CommandOutput.java index 1c29bbe99..fa2463fb9 100644 --- a/src/main/java/duke/commands/CommandOutput.java +++ b/src/main/java/duke/commands/CommandOutput.java @@ -3,6 +3,7 @@ import duke.tasks.TaskList; public class CommandOutput { + private String response; private boolean updated; private TaskList taskList; diff --git a/src/main/java/duke/commands/DeleteCommand.java b/src/main/java/duke/commands/DeleteCommand.java index 424a81b64..c6ddb5a7b 100644 --- a/src/main/java/duke/commands/DeleteCommand.java +++ b/src/main/java/duke/commands/DeleteCommand.java @@ -3,6 +3,7 @@ import duke.tasks.TaskList; public class DeleteCommand extends Command { + public DeleteCommand(TaskList taskList, String userCommand) { super(taskList, userCommand); } diff --git a/src/main/java/duke/commands/DoneCommand.java b/src/main/java/duke/commands/DoneCommand.java index c55a37320..838112870 100644 --- a/src/main/java/duke/commands/DoneCommand.java +++ b/src/main/java/duke/commands/DoneCommand.java @@ -3,6 +3,7 @@ import duke.tasks.TaskList; public class DoneCommand extends Command { + public DoneCommand(TaskList taskList, String userCommand) { super(taskList, userCommand); } diff --git a/src/main/java/duke/commands/FindCommand.java b/src/main/java/duke/commands/FindCommand.java index f2347ec8b..1a3219345 100644 --- a/src/main/java/duke/commands/FindCommand.java +++ b/src/main/java/duke/commands/FindCommand.java @@ -3,6 +3,7 @@ import duke.tasks.TaskList; public class FindCommand extends Command { + public FindCommand(TaskList taskList, String userCommand){ super(taskList, userCommand); } diff --git a/src/main/java/duke/commands/ListCommand.java b/src/main/java/duke/commands/ListCommand.java index 4556e8138..b361140f6 100644 --- a/src/main/java/duke/commands/ListCommand.java +++ b/src/main/java/duke/commands/ListCommand.java @@ -3,6 +3,7 @@ import duke.tasks.TaskList; public class ListCommand extends Command { + public ListCommand(TaskList taskList) { super(taskList, ""); } diff --git a/src/main/java/duke/commands/NoCommand.java b/src/main/java/duke/commands/NoCommand.java index ca31ca4fa..a18a38831 100644 --- a/src/main/java/duke/commands/NoCommand.java +++ b/src/main/java/duke/commands/NoCommand.java @@ -3,6 +3,7 @@ import duke.tasks.TaskList; public class NoCommand extends Command { + public NoCommand(TaskList taskList) { super(taskList, ""); } diff --git a/src/main/java/duke/exceptions/AlreadyDoneException.java b/src/main/java/duke/exceptions/AlreadyDoneException.java index d88c37d9e..e3b0caa02 100644 --- a/src/main/java/duke/exceptions/AlreadyDoneException.java +++ b/src/main/java/duke/exceptions/AlreadyDoneException.java @@ -1,6 +1,8 @@ package duke.exceptions; +//used when user tries to complete an already completed task public class AlreadyDoneException extends Exception { + public final String DONE_RESPONSE = "This task is already completed."; @Override diff --git a/src/main/java/duke/exceptions/DeadlineException.java b/src/main/java/duke/exceptions/DeadlineException.java index 4912c46bb..c4616055d 100644 --- a/src/main/java/duke/exceptions/DeadlineException.java +++ b/src/main/java/duke/exceptions/DeadlineException.java @@ -1,7 +1,9 @@ package duke.exceptions; +//used when deadline description is in the wrong format public class DeadlineException extends Exception { - public final String DEADLINE_RESPONSE = "The description of deadline cannot be empty."; + + public final String DEADLINE_RESPONSE = "The following format must be used: deadline [description] /by [date and time]"; @Override public String getMessage() { diff --git a/src/main/java/duke/exceptions/DeleteFormatException.java b/src/main/java/duke/exceptions/DeleteFormatException.java index afdd15bca..375f11c0d 100644 --- a/src/main/java/duke/exceptions/DeleteFormatException.java +++ b/src/main/java/duke/exceptions/DeleteFormatException.java @@ -1,7 +1,9 @@ package duke.exceptions; +//used when delete description is in the wrong format public class DeleteFormatException extends Exception { - public final String DELETE_FORMAT_RESPONSE = "The description of delete cannot be empty."; + + public final String DELETE_FORMAT_RESPONSE = "The following format must be used: delete [index]"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/DeleteRangeException.java b/src/main/java/duke/exceptions/DeleteRangeException.java index 3aa75610b..bfac4906a 100644 --- a/src/main/java/duke/exceptions/DeleteRangeException.java +++ b/src/main/java/duke/exceptions/DeleteRangeException.java @@ -1,6 +1,8 @@ package duke.exceptions; +//used when task user wants to delete does not exist public class DeleteRangeException extends Exception { + public final String DELETE_RANGE_RESPONSE = "That task does not exist."; @Override diff --git a/src/main/java/duke/exceptions/DoneFormatException.java b/src/main/java/duke/exceptions/DoneFormatException.java index ef382d44e..ba3e8e370 100644 --- a/src/main/java/duke/exceptions/DoneFormatException.java +++ b/src/main/java/duke/exceptions/DoneFormatException.java @@ -1,7 +1,9 @@ package duke.exceptions; +//used when done description is in the wrong format public class DoneFormatException extends Exception { - public final String DONE_FORMAT_RESPONSE = "The description of done cannot be empty."; + + public final String DONE_FORMAT_RESPONSE = "The following format must be used: done [index]"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/DoneRangeException.java b/src/main/java/duke/exceptions/DoneRangeException.java index 6d8627db3..fdafaff6f 100644 --- a/src/main/java/duke/exceptions/DoneRangeException.java +++ b/src/main/java/duke/exceptions/DoneRangeException.java @@ -1,6 +1,8 @@ package duke.exceptions; +//used when task user wants to complete does not exist public class DoneRangeException extends Exception { + public final String DONE_RANGE_RESPONSE = "That task does not exist."; @Override diff --git a/src/main/java/duke/exceptions/EmptyException.java b/src/main/java/duke/exceptions/EmptyException.java index 459054af9..8dca54a82 100644 --- a/src/main/java/duke/exceptions/EmptyException.java +++ b/src/main/java/duke/exceptions/EmptyException.java @@ -1,6 +1,8 @@ package duke.exceptions; +//used when user did not input a command public class EmptyException extends Exception { + public final String EMPTY_RESPONSE = "Command is empty."; @Override diff --git a/src/main/java/duke/exceptions/EmptyListException.java b/src/main/java/duke/exceptions/EmptyListException.java index a72c34663..af62c5c6d 100644 --- a/src/main/java/duke/exceptions/EmptyListException.java +++ b/src/main/java/duke/exceptions/EmptyListException.java @@ -1,6 +1,8 @@ package duke.exceptions; +//used when user tries the list command even though list is empty public class EmptyListException extends Exception { + public final String EMPTY_LIST_RESPONSE = "The list is empty."; @Override diff --git a/src/main/java/duke/exceptions/EventException.java b/src/main/java/duke/exceptions/EventException.java index d064169ad..f2789fad9 100644 --- a/src/main/java/duke/exceptions/EventException.java +++ b/src/main/java/duke/exceptions/EventException.java @@ -1,7 +1,9 @@ package duke.exceptions; +//used when event description is in the wrong format public class EventException extends Exception { - public final String EVENT_RESPONSE = "The description of event cannot be empty."; + + public final String EVENT_RESPONSE = "The following format must be used: event [description] /at [date and time]"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/FileFormatException.java b/src/main/java/duke/exceptions/FileFormatException.java index 4eaf6d9af..6396dc754 100644 --- a/src/main/java/duke/exceptions/FileFormatException.java +++ b/src/main/java/duke/exceptions/FileFormatException.java @@ -1,4 +1,5 @@ package duke.exceptions; +//used when save file is in the wrong format public class FileFormatException extends Exception { } diff --git a/src/main/java/duke/exceptions/NotFoundException.java b/src/main/java/duke/exceptions/NotFoundException.java index 9fff3744f..9868dd2af 100644 --- a/src/main/java/duke/exceptions/NotFoundException.java +++ b/src/main/java/duke/exceptions/NotFoundException.java @@ -1,6 +1,8 @@ package duke.exceptions; +//used when no matching tasks are found from the find command public class NotFoundException extends Exception { + public final String NOT_FOUND_RESPONSE = "There are no matching tasks."; @Override diff --git a/src/main/java/duke/exceptions/TodoException.java b/src/main/java/duke/exceptions/TodoException.java index 9fcc93f53..49589aa23 100644 --- a/src/main/java/duke/exceptions/TodoException.java +++ b/src/main/java/duke/exceptions/TodoException.java @@ -1,7 +1,9 @@ package duke.exceptions; +//used when todo description is in the wrong format public class TodoException extends Exception { - public final String TODO_RESPONSE = "The description of todo cannot be empty."; + + public final String TODO_RESPONSE = "The following format must be used: todo [description]"; @Override public String getMessage(){ diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java index 6de943f67..678c8b03a 100644 --- a/src/main/java/duke/tasks/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -1,6 +1,7 @@ package duke.tasks; public class Deadline extends Task { + private TaskType type = TaskType.DEADLINE; protected String by; diff --git a/src/main/java/duke/tasks/Event.java b/src/main/java/duke/tasks/Event.java index 314245efc..f85cf61bb 100644 --- a/src/main/java/duke/tasks/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -1,6 +1,7 @@ package duke.tasks; public class Event extends Task { + private TaskType type = TaskType.EVENT; protected String at; diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java index 069167535..84458bb4a 100644 --- a/src/main/java/duke/tasks/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -1,6 +1,7 @@ package duke.tasks; public class Task { + protected String description; protected boolean isDone; diff --git a/src/main/java/duke/tasks/TaskList.java b/src/main/java/duke/tasks/TaskList.java index fd70c1dda..eab67fb1a 100644 --- a/src/main/java/duke/tasks/TaskList.java +++ b/src/main/java/duke/tasks/TaskList.java @@ -5,6 +5,7 @@ import java.util.ArrayList; public class TaskList { + public ArrayList tasks = new ArrayList<>(); public String ADDED = "Got it. I've added this task:"; @@ -25,6 +26,7 @@ public String printTaskCount() { return "Now you have " + tasks.size() + " tasks in the list."; } + //displays the list to the user public String printList() throws EmptyListException { if (tasks.size() == 0) { throw new EmptyListException(); @@ -39,6 +41,7 @@ public String printList() throws EmptyListException { public TaskList() { } + //executes program to mark a task as done public String taskDone(String line) throws DoneFormatException, AlreadyDoneException, DoneRangeException { if (!line.matches("\\d+")) { @@ -55,6 +58,7 @@ public String taskDone(String line) } } + //adds a todo task to the list public String addTodo(String line) throws TodoException { if (line.equals("") || line.equals("todo")) { throw new TodoException(); @@ -64,6 +68,7 @@ public String addTodo(String line) throws TodoException { } } + //adds a deadline task to the list public String addDeadline(String line) throws DeadlineException { if (!line.matches("(.*)/by(.*)")) { throw new DeadlineException(); @@ -76,6 +81,7 @@ public String addDeadline(String line) throws DeadlineException { } } + //adds an event task to the list public String addEvent(String line) throws EventException { if (!line.matches("(.*)/at(.*)")) { throw new EventException(); @@ -88,6 +94,7 @@ public String addEvent(String line) throws EventException { } } + //deletes task from the list public String deleteTask(String line) throws DeleteFormatException, DeleteRangeException { if (!line.matches("\\d+")) { throw new DeleteFormatException(); @@ -102,11 +109,10 @@ public String deleteTask(String line) throws DeleteFormatException, DeleteRangeE } } + //finds matching tasks in the list public String findTask(String line) throws NotFoundException { String foundList = FIND_START; - int tasksFound = 0; - for (int i = 1; i <= tasks.size(); ++i) { if (tasks.get(i - 1).getDescription().toLowerCase().contains(line.toLowerCase())) { tasksFound += 1; @@ -120,6 +126,7 @@ public String findTask(String line) throws NotFoundException { return foundList; } + //clears existing list public void clearList(){ tasks.clear(); } diff --git a/src/main/java/duke/tasks/TaskListResponse.java b/src/main/java/duke/tasks/TaskListResponse.java index b49e46785..14a7a3634 100644 --- a/src/main/java/duke/tasks/TaskListResponse.java +++ b/src/main/java/duke/tasks/TaskListResponse.java @@ -1,8 +1,7 @@ package duke.tasks; -import duke.tasks.TaskList; - public class TaskListResponse { + public TaskList taskList; public String response; diff --git a/src/main/java/duke/tasks/Todo.java b/src/main/java/duke/tasks/Todo.java index 30f8c2c3c..15e0c8da8 100644 --- a/src/main/java/duke/tasks/Todo.java +++ b/src/main/java/duke/tasks/Todo.java @@ -1,6 +1,7 @@ package duke.tasks; public class Todo extends Task{ + private TaskType type = TaskType.TODO; public Todo(String description) { diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 791744bdc..59f92e79b 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,8 +1,6 @@ The file data/duke.txt is not found.A new file data/duke.txt has been created. Hello! I'm Duke. What can I do for you? -Command is empty. -What can I do for you? Got it. I've added this task: eat cake Now you have 1 tasks in the list. @@ -18,9 +16,9 @@ What can I do for you? Nice! I've marked this task as done: eat cake What can I do for you? -The description of done cannot be empty. -What can I do for you? -The description of todo cannot be empty. +Here are the matching tasks in your list: +1. [T][X] eat cake +2. [D][ ] eat chocolate cake (by: Friday 2pm) What can I do for you? Here are the tasks in your list: 1. [T][X] eat cake diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 375c6cf2c..322fda1f7 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -1,10 +1,8 @@ -eat cake todo eat cake deadline eat chocolate cake /by Friday 2pm event attend cake buffet /at Saturday 5pm done 1 -done a -todo +find eat list cake bye \ No newline at end of file From a87decbfd970b39066434cf09f6f5063a5e6a540 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Tue, 28 Sep 2021 22:24:53 +0800 Subject: [PATCH 24/25] Customisation and User guide --- docs/README.md | 153 ++++++++++++++++-- src/main/java/duke/Parser.java | 2 +- src/main/java/duke/Storage.java | 10 +- src/main/java/duke/Ui.java | 6 +- .../duke/exceptions/AlreadyDoneException.java | 2 +- .../duke/exceptions/DeadlineException.java | 3 +- .../exceptions/DeleteFormatException.java | 3 +- .../duke/exceptions/DeleteRangeException.java | 2 +- .../duke/exceptions/DoneFormatException.java | 3 +- .../duke/exceptions/DoneRangeException.java | 2 +- .../java/duke/exceptions/EmptyException.java | 2 +- .../duke/exceptions/EmptyListException.java | 2 +- .../java/duke/exceptions/EventException.java | 3 +- .../duke/exceptions/NotFoundException.java | 2 +- .../java/duke/exceptions/TodoException.java | 3 +- src/main/java/duke/tasks/TaskList.java | 10 +- 16 files changed, 171 insertions(+), 37 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..bb32003a0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,158 @@ # User Guide -## Features +## Command list -### Feature-ABC +### `todo` - adds todo task +### `deadline` - adds deadline task +### `event` - adds event task +### `list` - lists all task +### `find` - find matching tasks +### `done` - completes task +### `delete` - deletes task +### `bye` - exits program -Description of the feature. +## Usage -### Feature-XYZ +### `todo` - adds todo task -Description of the feature. +Adds todo task to the list -## Usage +Example of usage: -### `Keyword` - Describe action +`todo Complete CS2113 lecture quiz` -Describe the action and its outcome. +Expected outcome: -Example of usage: +Displays successfully addition of todo task to the list and the total number of tasks in the list. + +``` +Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task: +Complete CS2113 lecture quiz +Now you have 1 tasks in the list. +``` + +### `deadline` - adds deadline task + +Adds deadline task to the list + +Example of usage: + +`deadline Complete iP increment /by Thursday 2359` + +Expected outcome: + +Displays successfully addition of deadline task to the list and the total number of tasks in the list. + +``` +Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task: +Complete iP increment +Now you have 2 tasks in the list. +``` + +### `event` - adds event task + +Adds event task to the list + +Example of usage: + +`event Attend CS2113 lecture /at Friday 1400` + +Expected outcome: + +Displays successfully addition of event task to the list and the total number of tasks in the list. + +``` +Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task: +Attend CS2113 lecture +Now you have 3 tasks in the list. +``` + +### `list` - lists all task + +Lists the entire task list + +Example of usage: + +`list` + +Expected outcome: + +Displays the entire task list -`keyword (optional arguments)` +``` +Right away! ε=ε=┌( >_<)┘ +Kao retrieved the tasks in your list: +1. [T][ ] Complete CS2113 lecture quiz +2. [D][ ] Complete iP increment (by: Thursday 2359) +3. [E][ ] Attend CS2113 lecture (at: Friday 1400) +``` + +### `find` - find matching tasks + +Finds matching tasks in the list + +Example of usage: + +`find CS2113` + +Expected outcome: + +Display tasks in the task list with the matching keyword + +``` +Kao got it! ε=ε=┌( `ー´)┘ +Kao retrieved the matching tasks in your list: +1. [T][ ] Complete CS2113 lecture quiz +3. [E][ ] Attend CS2113 lecture (at: Friday 1400) +``` + +### `done` - completes task + +Marks task as complete + +Example of usage: + +`done 1` Expected outcome: -Description of the outcome. +Displays successful completion of the corresponding task ``` -expected output +Nice job! ( >ω<)☆ Kao marked this task as done: +Complete CS2113 lecture quiz +``` + +### `delete` - deletes task + +Deletes task from the list + +Example of usage: + +`delete 1` + +Expected outcome: + +Displays successful deletion of the corresponding task + +``` +Alright! ( ・∀・ )ノ Kao removed this task: +Complete CS2113 lecture quiz +Now you have 2 tasks in the list. +``` + +### `bye` - exits program + +Exits and closes the program + +Example of usage: + +`bye` + +Expected outcome: + +Shows successful termination of the program + ``` +Bye bye! Kao will be waiting for you 。:゚( ´°ω°` )゚:。 +``` \ No newline at end of file diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index c9faa8efe..166d3874d 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -21,7 +21,7 @@ public class Parser { public final String DELETE = "delete"; public final String BYE = "bye"; - //executes program according to user command + //executes program and updates task list according to user command public Command parseCommand(TaskList taskList, String line) { final String[] splitLine = line.split(" ", 2); final String command = splitLine[0]; diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 9ba5b3ee6..c526b6427 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -65,15 +65,15 @@ public TaskListResponse initialiseTasks() { try { loadTasks(taskList); } catch (FileNotFoundException e0){ - response = response + "The file data/duke.txt is not found."; + response = response + "Kao cannot find the file data/duke.txt ( ;ŏ﹏ŏ ) "; try { createFile(); } catch (IOException e1) { - response = response + "There has been an error creating a new file. "; + response = response + "Kao has encountered an error creating a new file ( °ㅂ°╬ )"; } - response = response + "A new file data/duke.txt has been created."; + response = response + "Kao will create a new file data/duke.txt for you!"; } catch (FileFormatException e2) { - response = response + "There is a formatting error with the save file."; + response = response + "Kao is facing a formatting error with the save file ヽ( `д´ )ノ"; taskList.clearList(); } return new TaskListResponse(taskList, response); @@ -93,7 +93,7 @@ public String write(TaskList taskList) { try { writeToFile(taskList); } catch (IOException e) { - return "There has been an error writing to file."; + return "Kao has faced an error writing to file ( ; ω ; )"; } return ""; } diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index 46bab224b..40a5f5795 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -11,14 +11,14 @@ public static void printMessage(String line) { } public static void printGreeting() { - printMessage("Hello! I'm Duke."); + printMessage("Hello there! Kao here! ⸜(˃ ᵕ ˂ )⸝"); } public static void printPrompt() { - printMessage("What can I do for you?"); + printMessage( "How can Kao be of assistance? ( •̀ ᗜ •́ )"); } public static void printExit() { - printMessage("Bye. Hope to see you again soon!"); + printMessage("Bye bye! Kao will be waiting for you 。:゚( ´°ω°` )゚:。"); } } diff --git a/src/main/java/duke/exceptions/AlreadyDoneException.java b/src/main/java/duke/exceptions/AlreadyDoneException.java index e3b0caa02..df7eef2ec 100644 --- a/src/main/java/duke/exceptions/AlreadyDoneException.java +++ b/src/main/java/duke/exceptions/AlreadyDoneException.java @@ -3,7 +3,7 @@ //used when user tries to complete an already completed task public class AlreadyDoneException extends Exception { - public final String DONE_RESPONSE = "This task is already completed."; + public final String DONE_RESPONSE = "Hmm? You already completed that task (/  ̄O ̄)/"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/DeadlineException.java b/src/main/java/duke/exceptions/DeadlineException.java index c4616055d..767876d12 100644 --- a/src/main/java/duke/exceptions/DeadlineException.java +++ b/src/main/java/duke/exceptions/DeadlineException.java @@ -3,7 +3,8 @@ //used when deadline description is in the wrong format public class DeadlineException extends Exception { - public final String DEADLINE_RESPONSE = "The following format must be used: deadline [description] /by [date and time]"; + public final String DEADLINE_RESPONSE = "Kao needs you to repeat that (。_ 。 )?" + + "\nThe following format must be used: deadline [description] /by [date and time]"; @Override public String getMessage() { diff --git a/src/main/java/duke/exceptions/DeleteFormatException.java b/src/main/java/duke/exceptions/DeleteFormatException.java index 375f11c0d..e2160851d 100644 --- a/src/main/java/duke/exceptions/DeleteFormatException.java +++ b/src/main/java/duke/exceptions/DeleteFormatException.java @@ -3,7 +3,8 @@ //used when delete description is in the wrong format public class DeleteFormatException extends Exception { - public final String DELETE_FORMAT_RESPONSE = "The following format must be used: delete [index]"; + public final String DELETE_FORMAT_RESPONSE = "Could you rephrase that for Kao ( ̄▽ ̄*)?" + + "\nThe following format must be used: delete [index]"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/DeleteRangeException.java b/src/main/java/duke/exceptions/DeleteRangeException.java index bfac4906a..63f4f49b3 100644 --- a/src/main/java/duke/exceptions/DeleteRangeException.java +++ b/src/main/java/duke/exceptions/DeleteRangeException.java @@ -3,7 +3,7 @@ //used when task user wants to delete does not exist public class DeleteRangeException extends Exception { - public final String DELETE_RANGE_RESPONSE = "That task does not exist."; + public final String DELETE_RANGE_RESPONSE = "Kao doesn't see that task anywhere ┐( ・∀・ )┌"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/DoneFormatException.java b/src/main/java/duke/exceptions/DoneFormatException.java index ba3e8e370..eaf9f45fa 100644 --- a/src/main/java/duke/exceptions/DoneFormatException.java +++ b/src/main/java/duke/exceptions/DoneFormatException.java @@ -3,7 +3,8 @@ //used when done description is in the wrong format public class DoneFormatException extends Exception { - public final String DONE_FORMAT_RESPONSE = "The following format must be used: done [index]"; + public final String DONE_FORMAT_RESPONSE = "Kao doesn't get what you said ( ・ω・)?" + + "\nThe following format must be used: done [index]"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/DoneRangeException.java b/src/main/java/duke/exceptions/DoneRangeException.java index fdafaff6f..a8b0414c9 100644 --- a/src/main/java/duke/exceptions/DoneRangeException.java +++ b/src/main/java/duke/exceptions/DoneRangeException.java @@ -3,7 +3,7 @@ //used when task user wants to complete does not exist public class DoneRangeException extends Exception { - public final String DONE_RANGE_RESPONSE = "That task does not exist."; + public final String DONE_RANGE_RESPONSE = "Kao doesn't see that task ( ・д・)?"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/EmptyException.java b/src/main/java/duke/exceptions/EmptyException.java index 8dca54a82..d3ca7c4eb 100644 --- a/src/main/java/duke/exceptions/EmptyException.java +++ b/src/main/java/duke/exceptions/EmptyException.java @@ -3,7 +3,7 @@ //used when user did not input a command public class EmptyException extends Exception { - public final String EMPTY_RESPONSE = "Command is empty."; + public final String EMPTY_RESPONSE = "Did you call Kao? (・_・ )"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/EmptyListException.java b/src/main/java/duke/exceptions/EmptyListException.java index af62c5c6d..a5dfaed2e 100644 --- a/src/main/java/duke/exceptions/EmptyListException.java +++ b/src/main/java/duke/exceptions/EmptyListException.java @@ -3,7 +3,7 @@ //used when user tries the list command even though list is empty public class EmptyListException extends Exception { - public final String EMPTY_LIST_RESPONSE = "The list is empty."; + public final String EMPTY_LIST_RESPONSE = "The list is empty ( ̄ω ̄;)"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/EventException.java b/src/main/java/duke/exceptions/EventException.java index f2789fad9..36ea829ee 100644 --- a/src/main/java/duke/exceptions/EventException.java +++ b/src/main/java/duke/exceptions/EventException.java @@ -3,7 +3,8 @@ //used when event description is in the wrong format public class EventException extends Exception { - public final String EVENT_RESPONSE = "The following format must be used: event [description] /at [date and time]"; + public final String EVENT_RESPONSE = "Kao doesn't understand you ( ⊙_⊙)?" + + "\nThe following format must be used: event [description] /at [date and time]"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/NotFoundException.java b/src/main/java/duke/exceptions/NotFoundException.java index 9868dd2af..675f79a73 100644 --- a/src/main/java/duke/exceptions/NotFoundException.java +++ b/src/main/java/duke/exceptions/NotFoundException.java @@ -3,7 +3,7 @@ //used when no matching tasks are found from the find command public class NotFoundException extends Exception { - public final String NOT_FOUND_RESPONSE = "There are no matching tasks."; + public final String NOT_FOUND_RESPONSE = "Kao couldn't find any matching tasks ╮(・ω・;)╭"; @Override public String getMessage(){ diff --git a/src/main/java/duke/exceptions/TodoException.java b/src/main/java/duke/exceptions/TodoException.java index 49589aa23..ee69226b7 100644 --- a/src/main/java/duke/exceptions/TodoException.java +++ b/src/main/java/duke/exceptions/TodoException.java @@ -3,7 +3,8 @@ //used when todo description is in the wrong format public class TodoException extends Exception { - public final String TODO_RESPONSE = "The following format must be used: todo [description]"; + public final String TODO_RESPONSE = "Kao is confused (・~・ )?" + + "\nThe following format must be used: todo [description]"; @Override public String getMessage(){ diff --git a/src/main/java/duke/tasks/TaskList.java b/src/main/java/duke/tasks/TaskList.java index eab67fb1a..9a863c4a6 100644 --- a/src/main/java/duke/tasks/TaskList.java +++ b/src/main/java/duke/tasks/TaskList.java @@ -8,11 +8,11 @@ public class TaskList { public ArrayList tasks = new ArrayList<>(); - public String ADDED = "Got it. I've added this task:"; - public String REMOVED = "Noted. I've removed this task:"; - public String ACKNOWLEDGED = "Nice! I've marked this task as done:"; - public String LIST_START = "Here are the tasks in your list:"; - public String FIND_START = "Here are the matching tasks in your list:"; + public String ADDED = "Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task:"; + public String REMOVED = "Alright! ( ・∀・ )ノ Kao removed this task:"; + public String ACKNOWLEDGED = "Nice job! ( >ω<)☆ Kao marked this task as done:"; + public String LIST_START = "Right away! ε=ε=┌( >_<)┘ \nKao retrieved the tasks in your list:"; + public String FIND_START = "Kao got it! ε=ε=┌( `ー´)┘ \nKao retrieved the matching tasks in your list:"; public String TaskListResponse(String line, int index){ return line + "\n" + tasks.get(index).getDescription(); From a5be71421dd59ce5e563ddc7cdb6817ef29ed2e5 Mon Sep 17 00:00:00 2001 From: teoziyiivy Date: Wed, 29 Sep 2021 22:00:48 +0800 Subject: [PATCH 25/25] Edit txt files --- text-ui-test/EXPECTED.TXT | 61 ++++++++++++++++++++++----------------- text-ui-test/input.txt | 7 ++--- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 59f92e79b..d09585156 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,30 +1,37 @@ -The file data/duke.txt is not found.A new file data/duke.txt has been created. -Hello! I'm Duke. -What can I do for you? -Got it. I've added this task: -eat cake +Kao cannot find the file data/duke.txt ( ;ŏ﹏ŏ ) Kao will create a new file data/duke.txt for you! +Hello there! Kao here! ⸜(˃ ᵕ ˂ )⸝ +How can Kao be of assistance? ( •̀ ᗜ •́ ) +todo Eat cake +Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task: +Eat cake Now you have 1 tasks in the list. -What can I do for you? -Got it. I've added this task: -eat chocolate cake +How can Kao be of assistance? ( •̀ ᗜ •́ ) +deadline Eat chocolate cake /by Friday 2pm +Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task: +Eat chocolate cake Now you have 2 tasks in the list. -What can I do for you? -Got it. I've added this task: -attend cake buffet +How can Kao be of assistance? ( •̀ ᗜ •́ ) +event Attend cake buffet /at Saturday 5pm +Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task: +Attend cake buffet Now you have 3 tasks in the list. -What can I do for you? -Nice! I've marked this task as done: -eat cake -What can I do for you? -Here are the matching tasks in your list: -1. [T][X] eat cake -2. [D][ ] eat chocolate cake (by: Friday 2pm) -What can I do for you? -Here are the tasks in your list: -1. [T][X] eat cake -2. [D][ ] eat chocolate cake (by: Friday 2pm) -3. [E][ ] attend cake buffet (at: Saturday 5pm) -What can I do for you? -Command is empty. -What can I do for you? -Bye. Hope to see you again soon! +How can Kao be of assistance? ( •̀ ᗜ •́ ) +done 1 +Nice job! ( >ω<)☆ Kao marked this task as done: +Eat cake +How can Kao be of assistance? ( •̀ ᗜ •́ ) +find eat +Kao got it! ε=ε=┌( `ー´)┘ +Kao retrieved the matching tasks in your list: +1. [T][X] Eat cake +2. [D][ ] Eat chocolate cake (by: Friday 2pm) +How can Kao be of assistance? ( •̀ ᗜ •́ ) +list +Right away! ε=ε=┌( >_<)┘ +Kao retrieved the tasks in your list: +1. [T][X] Eat cake +2. [D][ ] Eat chocolate cake (by: Friday 2pm) +3. [E][ ] Attend cake buffet (at: Saturday 5pm) +How can Kao be of assistance? ( •̀ ᗜ •́ ) +bye +Bye bye! Kao will be waiting for you 。:゚( ´°ω°` )゚:。 diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 322fda1f7..567a1aa4e 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -1,8 +1,7 @@ -todo eat cake -deadline eat chocolate cake /by Friday 2pm -event attend cake buffet /at Saturday 5pm +todo Eat cake +deadline Eat chocolate cake /by Friday 2pm +event Attend cake buffet /at Saturday 5pm done 1 find eat list -cake bye \ No newline at end of file