From 08758df0a608555fc0126ac639800afb6d2c997d Mon Sep 17 00:00:00 2001 From: "Jeremy Y." Date: Thu, 31 Aug 2023 12:57:49 +0800 Subject: [PATCH 01/24] Rename Duke to Chatbot --- src/main/java/{Duke.java => Chatbot.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/{Duke.java => Chatbot.java} (93%) diff --git a/src/main/java/Duke.java b/src/main/java/Chatbot.java similarity index 93% rename from src/main/java/Duke.java rename to src/main/java/Chatbot.java index 5d313334c..9f8307a2a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Chatbot.java @@ -1,4 +1,4 @@ -public class Duke { +public class Chatbot { public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" From 70db08d0b7fb011414f0e945f730e2ca32d80e81 Mon Sep 17 00:00:00 2001 From: "Jeremy Y." Date: Thu, 31 Aug 2023 13:01:19 +0800 Subject: [PATCH 02/24] Implemented skeletal chatbot functionality --- src/main/java/Chatbot.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/Chatbot.java b/src/main/java/Chatbot.java index 9f8307a2a..46421446b 100644 --- a/src/main/java/Chatbot.java +++ b/src/main/java/Chatbot.java @@ -1,10 +1,11 @@ public class Chatbot { public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + String msg = "____________________________________________________________\n" + + " Hello! I'm Chatbot\n" + + " What can I do for you?\n" + + "____________________________________________________________\n" + + " Bye. Hope to see you again soon!\n" + + "____________________________________________________________\n"; + System.out.println(msg); } } From f19959271e972178ab8edf16bc509f168440e871 Mon Sep 17 00:00:00 2001 From: "Jeremy Y." Date: Thu, 31 Aug 2023 21:05:18 +0800 Subject: [PATCH 03/24] Implemented Level 1 - echo, bye --- src/main/java/Chatbot.java | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/Chatbot.java b/src/main/java/Chatbot.java index 46421446b..8dcba1301 100644 --- a/src/main/java/Chatbot.java +++ b/src/main/java/Chatbot.java @@ -1,11 +1,26 @@ +import java.util.Scanner; + public class Chatbot { public static void main(String[] args) { - String msg = "____________________________________________________________\n" + + String greetingMsg = "____________________________________________________________\n" + " Hello! I'm Chatbot\n" + " What can I do for you?\n" + - "____________________________________________________________\n" + + "____________________________________________________________\n"; + String byeMsg = "____________________________________________________________\n" + " Bye. Hope to see you again soon!\n" + "____________________________________________________________\n"; - System.out.println(msg); + System.out.println(greetingMsg); + + + Scanner in = new Scanner(System.in); + String input = in.nextLine(); + while(!input.equals("bye")) { + System.out.println("____________________________________________________________"); + System.out.println(input); + System.out.println("____________________________________________________________"); + input = in.nextLine(); + } + System.out.println(byeMsg); + } } From 74cd2693d95508c8d6f6edff3f658dda3b34477a Mon Sep 17 00:00:00 2001 From: "Jeremy Y." Date: Fri, 1 Sep 2023 00:22:50 +0800 Subject: [PATCH 04/24] Implemented Level 2 - Add, list --- src/main/java/Chatbot.java | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/Chatbot.java b/src/main/java/Chatbot.java index 8dcba1301..fd0363d97 100644 --- a/src/main/java/Chatbot.java +++ b/src/main/java/Chatbot.java @@ -2,6 +2,9 @@ public class Chatbot { public static void main(String[] args) { + String[] tasks = new String[100]; + int numOfTasks = 0; + String greetingMsg = "____________________________________________________________\n" + " Hello! I'm Chatbot\n" + " What can I do for you?\n" + @@ -15,9 +18,22 @@ public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); while(!input.equals("bye")) { - System.out.println("____________________________________________________________"); - System.out.println(input); - System.out.println("____________________________________________________________"); + if( input.equals("list") ) { + System.out.println("____________________________________________________________"); + for(int i=0; i Date: Fri, 1 Sep 2023 00:38:35 +0800 Subject: [PATCH 05/24] Implemented Level 3 - Mark as Done --- src/main/java/Chatbot.java | 30 +++++++++++++++++++++++++----- src/main/java/Task.java | 22 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Chatbot.java b/src/main/java/Chatbot.java index fd0363d97..1752dff74 100644 --- a/src/main/java/Chatbot.java +++ b/src/main/java/Chatbot.java @@ -2,7 +2,7 @@ public class Chatbot { public static void main(String[] args) { - String[] tasks = new String[100]; + Task[] tasks = new Task[100]; int numOfTasks = 0; String greetingMsg = "____________________________________________________________\n" + @@ -20,14 +20,34 @@ public static void main(String[] args) { while(!input.equals("bye")) { if( input.equals("list") ) { System.out.println("____________________________________________________________"); + System.out.println(" Here are the tasks in your list:"); for(int i=0; i 0 ) { + tasks[markTaskNo-1].markAsDone(); + } + System.out.println("____________________________________________________________"); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println( " [" + tasks[markTaskNo-1].getStatusIcon() + "] " + tasks[markTaskNo-1].getDescription() ); + System.out.println("____________________________________________________________"); + } else if ( input.startsWith("unmark ") ) { + String number = input.replace("unmark ", "").trim(); + int unmarkTaskNo = Integer.parseInt(number); + if( unmarkTaskNo > 0 ) { + tasks[unmarkTaskNo-1].markAsUndone(); + } + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println( " [" + tasks[unmarkTaskNo-1].getStatusIcon() + "] " + tasks[unmarkTaskNo-1].getDescription() ); + System.out.println("____________________________________________________________"); } else { - // if not bye, list, then the command by default is add - tasks[numOfTasks] = input; + // if not bye, list, mark, then the command by default is add + tasks[numOfTasks] = new Task(input); numOfTasks++; System.out.println("____________________________________________________________"); System.out.println("added: " + input); diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..33392f408 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,22 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + public String getDescription() { + return this.description; + } + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + public void markAsDone() { + this.isDone = true; + } + public void markAsUndone() { + this.isDone = false; + } + +} \ No newline at end of file From c78ca994bd533296663ede5921a99f5a61487157 Mon Sep 17 00:00:00 2001 From: "Jeremy Y." Date: Wed, 6 Sep 2023 11:35:11 +0800 Subject: [PATCH 06/24] Level 4. ToDos, Events, Deadlines implemented --- src/main/java/Chatbot.java | 63 ++++++++++++++++++++++++++++++-------- src/main/java/Task.java | 7 +++-- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/main/java/Chatbot.java b/src/main/java/Chatbot.java index 1752dff74..60e6b2c60 100644 --- a/src/main/java/Chatbot.java +++ b/src/main/java/Chatbot.java @@ -18,36 +18,75 @@ public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); while(!input.equals("bye")) { - if( input.equals("list") ) { + if (input.equals("list")) { System.out.println("____________________________________________________________"); System.out.println(" Here are the tasks in your list:"); - for(int i=0; i 0 ) { - tasks[markTaskNo-1].markAsDone(); + if (markTaskNo > 0) { + tasks[markTaskNo - 1].markAsDone(); } System.out.println("____________________________________________________________"); System.out.println(" Nice! I've marked this task as done:"); - System.out.println( " [" + tasks[markTaskNo-1].getStatusIcon() + "] " + tasks[markTaskNo-1].getDescription() ); + System.out.println(" [" + tasks[markTaskNo - 1].getStatusIcon() + "] " + tasks[markTaskNo - 1].getDescription()); System.out.println("____________________________________________________________"); - } else if ( input.startsWith("unmark ") ) { + } else if (input.startsWith("unmark ")) { String number = input.replace("unmark ", "").trim(); int unmarkTaskNo = Integer.parseInt(number); - if( unmarkTaskNo > 0 ) { - tasks[unmarkTaskNo-1].markAsUndone(); + if (unmarkTaskNo > 0) { + tasks[unmarkTaskNo - 1].markAsUndone(); } System.out.println("____________________________________________________________"); System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println( " [" + tasks[unmarkTaskNo-1].getStatusIcon() + "] " + tasks[unmarkTaskNo-1].getDescription() ); + System.out.println(" [" + tasks[unmarkTaskNo - 1].getStatusIcon() + "] " + tasks[unmarkTaskNo - 1].getDescription()); + System.out.println("____________________________________________________________"); + } else if ( input.startsWith("todo") ) { + + String msg = input.replace("todo ", "").trim(); + msg = msg.replace("deadline", ""); + msg = msg.replace("event", ""); + + Task task = new Task(input, "T"); + tasks[numOfTasks] = task; + numOfTasks++; + + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" [" + task.getTypeIcon() + "][" + task.getStatusIcon() + "] " + task.getDescription()); + System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } else if ( input.startsWith("deadline") ) { + String msg = input.replace("deadline", ""); + + Task task = new Task(input, "D"); + tasks[numOfTasks] = task; + numOfTasks++; + + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" [" + task.getTypeIcon() + "][" + task.getStatusIcon() + "] " + task.getDescription()); + System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } else if ( input.startsWith("event") ) { + String msg = input.replace("event", ""); + + Task task = new Task(input, "E"); + tasks[numOfTasks] = task; + numOfTasks++; + + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" [" + task.getTypeIcon() + "][" + task.getStatusIcon() + "] " + task.getDescription()); + System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); System.out.println("____________________________________________________________"); } else { // if not bye, list, mark, then the command by default is add - tasks[numOfTasks] = new Task(input); + tasks[numOfTasks] = new Task(input, ""); numOfTasks++; System.out.println("____________________________________________________________"); System.out.println("added: " + input); diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 33392f408..044a3dbf8 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,10 +1,11 @@ public class Task { protected String description; protected boolean isDone; - - public Task(String description) { + protected String type; + public Task(String description, String type) { this.description = description; this.isDone = false; + this.type = type; } public String getDescription() { return this.description; @@ -19,4 +20,6 @@ public void markAsUndone() { this.isDone = false; } + public String getTypeIcon() { return type; } + } \ No newline at end of file From fc69e5c6cac2f2667df012ce26571f10525527ac Mon Sep 17 00:00:00 2001 From: "Jeremy Y." Date: Wed, 6 Sep 2023 16:42:33 +0800 Subject: [PATCH 07/24] A-Inheritance Implemented --- src/main/java/Chatbot.java | 31 ++++++++++++++++++------------- src/main/java/Deadline.java | 13 +++++++++++++ src/main/java/Event.java | 14 ++++++++++++++ src/main/java/Task.java | 9 +++++---- src/main/java/Todo.java | 10 ++++++++++ 5 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Chatbot.java b/src/main/java/Chatbot.java index 60e6b2c60..8a6243508 100644 --- a/src/main/java/Chatbot.java +++ b/src/main/java/Chatbot.java @@ -22,7 +22,8 @@ public static void main(String[] args) { System.out.println("____________________________________________________________"); System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < numOfTasks; i++) { - System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); + //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); + System.out.println(" " + (i + 1) + "." + tasks[i]); } System.out.println("____________________________________________________________"); } else if (input.startsWith("mark ")) { @@ -46,47 +47,51 @@ public static void main(String[] args) { System.out.println(" [" + tasks[unmarkTaskNo - 1].getStatusIcon() + "] " + tasks[unmarkTaskNo - 1].getDescription()); System.out.println("____________________________________________________________"); } else if ( input.startsWith("todo") ) { - String msg = input.replace("todo ", "").trim(); - msg = msg.replace("deadline", ""); - msg = msg.replace("event", ""); - Task task = new Task(input, "T"); + Task task = new Todo(msg); tasks[numOfTasks] = task; numOfTasks++; System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); - System.out.println(" [" + task.getTypeIcon() + "][" + task.getStatusIcon() + "] " + task.getDescription()); + System.out.println(" " + task); System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); System.out.println("____________________________________________________________"); } else if ( input.startsWith("deadline") ) { - String msg = input.replace("deadline", ""); + String msg = input.replace("deadline", "").trim(); + String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate + String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description - Task task = new Task(input, "D"); + Task task = new Deadline(desc, byDate); tasks[numOfTasks] = task; numOfTasks++; System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); - System.out.println(" [" + task.getTypeIcon() + "][" + task.getStatusIcon() + "] " + task.getDescription()); + System.out.println(" " + task); System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); System.out.println("____________________________________________________________"); } else if ( input.startsWith("event") ) { - String msg = input.replace("event", ""); + String msg = input.replace("event", "").trim(); + String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); + String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); + String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); + + String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description - Task task = new Task(input, "E"); + Task task = new Event(desc, fromDate, toDate); tasks[numOfTasks] = task; numOfTasks++; System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); - System.out.println(" [" + task.getTypeIcon() + "][" + task.getStatusIcon() + "] " + task.getDescription()); + System.out.println(" " + task); System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); System.out.println("____________________________________________________________"); } else { // if not bye, list, mark, then the command by default is add - tasks[numOfTasks] = new Task(input, ""); + tasks[numOfTasks] = new Task(input); numOfTasks++; System.out.println("____________________________________________________________"); System.out.println("added: " + input); diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..95928fb5f --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,13 @@ +public class Deadline extends Task { + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + by + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..f22c9c557 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,14 @@ +public class Event extends Task { + protected String fromDate; + protected String toDate; + public Event(String description, String fromDate, String toDate) { + super(description); + this.fromDate = fromDate; + this.toDate = toDate; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (from: " + this.fromDate + " to: " + this.toDate + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 044a3dbf8..3e77773bc 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,11 +1,9 @@ public class Task { protected String description; protected boolean isDone; - protected String type; - public Task(String description, String type) { + public Task(String description) { this.description = description; this.isDone = false; - this.type = type; } public String getDescription() { return this.description; @@ -20,6 +18,9 @@ public void markAsUndone() { this.isDone = false; } - public String getTypeIcon() { return type; } + @Override + public String toString() { + return "[" + this.getStatusIcon() + "] " + 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..798752410 --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,10 @@ +public class Todo extends Task { + public Todo(String description) { + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} \ No newline at end of file From 725a68374bfb1677024b1a5c17294c9263a6b63a Mon Sep 17 00:00:00 2001 From: "Jeremy Y." Date: Tue, 12 Sep 2023 21:57:50 +0800 Subject: [PATCH 08/24] Implemented Level-5 with Exceptions --- src/main/java/Chatbot.java | 156 ++++++++++-------- src/main/java/ChatbotEmptyDescException.java | 5 + .../java/ChatbotUnknownCommandException.java | 5 + 3 files changed, 97 insertions(+), 69 deletions(-) create mode 100644 src/main/java/ChatbotEmptyDescException.java create mode 100644 src/main/java/ChatbotUnknownCommandException.java diff --git a/src/main/java/Chatbot.java b/src/main/java/Chatbot.java index 8a6243508..eec01beee 100644 --- a/src/main/java/Chatbot.java +++ b/src/main/java/Chatbot.java @@ -1,7 +1,8 @@ import java.util.Scanner; public class Chatbot { - public static void main(String[] args) { + + public static void main(String[] args) throws Exception { Task[] tasks = new Task[100]; int numOfTasks = 0; @@ -14,88 +15,105 @@ public static void main(String[] args) { "____________________________________________________________\n"; System.out.println(greetingMsg); - Scanner in = new Scanner(System.in); String input = in.nextLine(); while(!input.equals("bye")) { - if (input.equals("list")) { - System.out.println("____________________________________________________________"); - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < numOfTasks; i++) { - //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); - System.out.println(" " + (i + 1) + "." + tasks[i]); - } - System.out.println("____________________________________________________________"); - } else if (input.startsWith("mark ")) { - String number = input.replace("mark ", "").trim(); - int markTaskNo = Integer.parseInt(number); - if (markTaskNo > 0) { - tasks[markTaskNo - 1].markAsDone(); - } - System.out.println("____________________________________________________________"); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" [" + tasks[markTaskNo - 1].getStatusIcon() + "] " + tasks[markTaskNo - 1].getDescription()); - System.out.println("____________________________________________________________"); - } else if (input.startsWith("unmark ")) { - String number = input.replace("unmark ", "").trim(); - int unmarkTaskNo = Integer.parseInt(number); - if (unmarkTaskNo > 0) { - tasks[unmarkTaskNo - 1].markAsUndone(); - } - System.out.println("____________________________________________________________"); - System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println(" [" + tasks[unmarkTaskNo - 1].getStatusIcon() + "] " + tasks[unmarkTaskNo - 1].getDescription()); - System.out.println("____________________________________________________________"); - } else if ( input.startsWith("todo") ) { - String msg = input.replace("todo ", "").trim(); + try { + if (input.equals("list")) { + System.out.println("____________________________________________________________"); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < numOfTasks; i++) { + //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); + System.out.println(" " + (i + 1) + "." + tasks[i]); + } + System.out.println("____________________________________________________________"); + } else if (input.startsWith("mark ")) { + String number = input.replace("mark ", "").trim(); + int markTaskNo = Integer.parseInt(number); + if (markTaskNo > 0) { + tasks[markTaskNo - 1].markAsDone(); + } + System.out.println("____________________________________________________________"); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" [" + tasks[markTaskNo - 1].getStatusIcon() + "] " + tasks[markTaskNo - 1].getDescription()); + System.out.println("____________________________________________________________"); + } else if (input.startsWith("unmark ")) { + String number = input.replace("unmark ", "").trim(); + int unmarkedTaskNo = Integer.parseInt(number); + if (unmarkedTaskNo > 0) { + tasks[unmarkedTaskNo - 1].markAsUndone(); + } + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println(" [" + tasks[unmarkedTaskNo - 1].getStatusIcon() + "] " + tasks[unmarkedTaskNo - 1].getDescription()); + System.out.println("____________________________________________________________"); + } else if (input.startsWith("todo")) { + String msg = input.replace("todo", "").trim(); + if(msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty."); + } - Task task = new Todo(msg); - tasks[numOfTasks] = task; - numOfTasks++; + Task task = new Todo(msg); + tasks[numOfTasks] = task; + numOfTasks++; - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); - System.out.println("____________________________________________________________"); - } else if ( input.startsWith("deadline") ) { - String msg = input.replace("deadline", "").trim(); - String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate - String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } else if (input.startsWith("deadline")) { + String msg = input.replace("deadline", "").trim(); + if(msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); + } - Task task = new Deadline(desc, byDate); - tasks[numOfTasks] = task; - numOfTasks++; + String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate + String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); - System.out.println("____________________________________________________________"); - } else if ( input.startsWith("event") ) { - String msg = input.replace("event", "").trim(); - String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); - String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); - String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); + Task task = new Deadline(desc, byDate); + tasks[numOfTasks] = task; + numOfTasks++; - String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } else if (input.startsWith("event")) { + String msg = input.replace("event", "").trim(); + if(msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty."); + } - Task task = new Event(desc, fromDate, toDate); - tasks[numOfTasks] = task; - numOfTasks++; + String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); + String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); + String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); + String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description + Task task = new Event(desc, fromDate, toDate); + tasks[numOfTasks] = task; + numOfTasks++; + + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } else { + // unknown command + throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + } + } catch(ChatbotUnknownCommandException e) { System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); + System.out.println(e.getMessage()); System.out.println("____________________________________________________________"); - } else { - // if not bye, list, mark, then the command by default is add - tasks[numOfTasks] = new Task(input); - numOfTasks++; + } catch(ChatbotEmptyDescException e) { System.out.println("____________________________________________________________"); - System.out.println("added: " + input); + System.out.println(e.getMessage()); System.out.println("____________________________________________________________"); + } catch (Exception e) { + System.out.println("Unknown exception. Error message: " + e.getMessage()); } input = in.nextLine(); diff --git a/src/main/java/ChatbotEmptyDescException.java b/src/main/java/ChatbotEmptyDescException.java new file mode 100644 index 000000000..0eb928c68 --- /dev/null +++ b/src/main/java/ChatbotEmptyDescException.java @@ -0,0 +1,5 @@ +public class ChatbotEmptyDescException extends Exception { + public ChatbotEmptyDescException(String exceptionMsg) { + super(exceptionMsg); + } +} diff --git a/src/main/java/ChatbotUnknownCommandException.java b/src/main/java/ChatbotUnknownCommandException.java new file mode 100644 index 000000000..3f7ab4acf --- /dev/null +++ b/src/main/java/ChatbotUnknownCommandException.java @@ -0,0 +1,5 @@ +public class ChatbotUnknownCommandException extends Exception { + public ChatbotUnknownCommandException(String exceptionMsg) { + super(exceptionMsg); + } +} From 6d91851575b6abaa4aa942e0e605a645e0a1fd74 Mon Sep 17 00:00:00 2001 From: "Jeremy Y." Date: Tue, 12 Sep 2023 22:53:30 +0800 Subject: [PATCH 09/24] Implemented A-Packages --- src/main/java/{ => chatbot}/Chatbot.java | 4 ++++ src/main/java/{ => chatbot}/ChatbotEmptyDescException.java | 2 ++ .../java/{ => chatbot}/ChatbotUnknownCommandException.java | 2 ++ src/main/java/{ => chatbot}/Deadline.java | 2 ++ src/main/java/{ => chatbot}/Event.java | 2 ++ src/main/java/{ => chatbot}/Task.java | 2 ++ src/main/java/{ => chatbot}/Todo.java | 4 ++++ 7 files changed, 18 insertions(+) rename src/main/java/{ => chatbot}/Chatbot.java (99%) rename src/main/java/{ => chatbot}/ChatbotEmptyDescException.java (89%) rename src/main/java/{ => chatbot}/ChatbotUnknownCommandException.java (90%) rename src/main/java/{ => chatbot}/Deadline.java (94%) rename src/main/java/{ => chatbot}/Event.java (95%) rename src/main/java/{ => chatbot}/Task.java (97%) rename src/main/java/{ => chatbot}/Todo.java (83%) diff --git a/src/main/java/Chatbot.java b/src/main/java/chatbot/Chatbot.java similarity index 99% rename from src/main/java/Chatbot.java rename to src/main/java/chatbot/Chatbot.java index eec01beee..980a0d3b7 100644 --- a/src/main/java/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -1,3 +1,7 @@ +package chatbot; + +import chatbot.*; + import java.util.Scanner; public class Chatbot { diff --git a/src/main/java/ChatbotEmptyDescException.java b/src/main/java/chatbot/ChatbotEmptyDescException.java similarity index 89% rename from src/main/java/ChatbotEmptyDescException.java rename to src/main/java/chatbot/ChatbotEmptyDescException.java index 0eb928c68..f6d977ae5 100644 --- a/src/main/java/ChatbotEmptyDescException.java +++ b/src/main/java/chatbot/ChatbotEmptyDescException.java @@ -1,3 +1,5 @@ +package chatbot; + public class ChatbotEmptyDescException extends Exception { public ChatbotEmptyDescException(String exceptionMsg) { super(exceptionMsg); diff --git a/src/main/java/ChatbotUnknownCommandException.java b/src/main/java/chatbot/ChatbotUnknownCommandException.java similarity index 90% rename from src/main/java/ChatbotUnknownCommandException.java rename to src/main/java/chatbot/ChatbotUnknownCommandException.java index 3f7ab4acf..e91860510 100644 --- a/src/main/java/ChatbotUnknownCommandException.java +++ b/src/main/java/chatbot/ChatbotUnknownCommandException.java @@ -1,3 +1,5 @@ +package chatbot; + public class ChatbotUnknownCommandException extends Exception { public ChatbotUnknownCommandException(String exceptionMsg) { super(exceptionMsg); diff --git a/src/main/java/Deadline.java b/src/main/java/chatbot/Deadline.java similarity index 94% rename from src/main/java/Deadline.java rename to src/main/java/chatbot/Deadline.java index 95928fb5f..37c63ca4e 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/chatbot/Deadline.java @@ -1,3 +1,5 @@ +package chatbot; + public class Deadline extends Task { protected String by; diff --git a/src/main/java/Event.java b/src/main/java/chatbot/Event.java similarity index 95% rename from src/main/java/Event.java rename to src/main/java/chatbot/Event.java index f22c9c557..ff96ae806 100644 --- a/src/main/java/Event.java +++ b/src/main/java/chatbot/Event.java @@ -1,3 +1,5 @@ +package chatbot; + public class Event extends Task { protected String fromDate; protected String toDate; diff --git a/src/main/java/Task.java b/src/main/java/chatbot/Task.java similarity index 97% rename from src/main/java/Task.java rename to src/main/java/chatbot/Task.java index 3e77773bc..f061a5cee 100644 --- a/src/main/java/Task.java +++ b/src/main/java/chatbot/Task.java @@ -1,3 +1,5 @@ +package chatbot; + public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/Todo.java b/src/main/java/chatbot/Todo.java similarity index 83% rename from src/main/java/Todo.java rename to src/main/java/chatbot/Todo.java index 798752410..8d052ade3 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/chatbot/Todo.java @@ -1,3 +1,7 @@ +package chatbot; + +import chatbot.Task; + public class Todo extends Task { public Todo(String description) { super(description); From 05dfeaf1d119efce1364ac1fa40af9d1261400af Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Wed, 20 Sep 2023 11:14:03 +0800 Subject: [PATCH 10/24] Implemented Level-6 with A-Collections using ArrayList --- src/main/java/chatbot/Chatbot.java | 36 ++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java index 980a0d3b7..853a21064 100644 --- a/src/main/java/chatbot/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -2,12 +2,14 @@ import chatbot.*; +import java.util.ArrayList; import java.util.Scanner; public class Chatbot { public static void main(String[] args) throws Exception { - Task[] tasks = new Task[100]; + //Task[] tasks = new Task[100]; + ArrayList tasks = new ArrayList(100); int numOfTasks = 0; String greetingMsg = "____________________________________________________________\n" + @@ -28,28 +30,28 @@ public static void main(String[] args) throws Exception { System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < numOfTasks; i++) { //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); - System.out.println(" " + (i + 1) + "." + tasks[i]); + System.out.println(" " + (i + 1) + "." + tasks.get(i)); } System.out.println("____________________________________________________________"); } else if (input.startsWith("mark ")) { String number = input.replace("mark ", "").trim(); int markTaskNo = Integer.parseInt(number); if (markTaskNo > 0) { - tasks[markTaskNo - 1].markAsDone(); + tasks.get(markTaskNo - 1).markAsDone(); } System.out.println("____________________________________________________________"); System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" [" + tasks[markTaskNo - 1].getStatusIcon() + "] " + tasks[markTaskNo - 1].getDescription()); + System.out.println(" [" + tasks.get(markTaskNo - 1).getStatusIcon() + "] " + tasks.get(markTaskNo - 1).getDescription()); System.out.println("____________________________________________________________"); } else if (input.startsWith("unmark ")) { String number = input.replace("unmark ", "").trim(); int unmarkedTaskNo = Integer.parseInt(number); if (unmarkedTaskNo > 0) { - tasks[unmarkedTaskNo - 1].markAsUndone(); + tasks.get(unmarkedTaskNo - 1).markAsUndone(); } System.out.println("____________________________________________________________"); System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println(" [" + tasks[unmarkedTaskNo - 1].getStatusIcon() + "] " + tasks[unmarkedTaskNo - 1].getDescription()); + System.out.println(" [" + tasks.get(unmarkedTaskNo - 1).getStatusIcon() + "] " + tasks.get(unmarkedTaskNo - 1).getDescription()); System.out.println("____________________________________________________________"); } else if (input.startsWith("todo")) { String msg = input.replace("todo", "").trim(); @@ -58,7 +60,8 @@ public static void main(String[] args) throws Exception { } Task task = new Todo(msg); - tasks[numOfTasks] = task; + //tasks.set(numOfTasks, task); + tasks.add(task); numOfTasks++; System.out.println("____________________________________________________________"); @@ -76,7 +79,8 @@ public static void main(String[] args) throws Exception { String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description Task task = new Deadline(desc, byDate); - tasks[numOfTasks] = task; + //tasks.set(numOfTasks, task); + tasks.add(task); numOfTasks++; System.out.println("____________________________________________________________"); @@ -96,7 +100,8 @@ public static void main(String[] args) throws Exception { String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description Task task = new Event(desc, fromDate, toDate); - tasks[numOfTasks] = task; + //tasks.set(numOfTasks, task); + tasks.add(task); numOfTasks++; System.out.println("____________________________________________________________"); @@ -104,6 +109,19 @@ public static void main(String[] args) throws Exception { System.out.println(" " + task); System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); System.out.println("____________________________________________________________"); + } else if (input.startsWith("delete")) { + String parameter = input.replace("delete", "").trim(); + if(parameter.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of delete cannot be empty."); + } + int indexToRemove = Integer.parseInt(parameter); + Task task = tasks.get(indexToRemove-1); + tasks.remove(indexToRemove-1); + System.out.println("____________________________________________________________"); + System.out.println(" Noted. I've removed this task: "); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); } else { // unknown command throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); From c6f11a25d9ac5decd6350210585935f69f67febc Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Wed, 20 Sep 2023 23:37:36 +0800 Subject: [PATCH 11/24] Implemented Level-7 --- src/main/java/chatbot/Chatbot.java | 235 ++++++++++++++++++----------- tasklist.txt | 2 + 2 files changed, 151 insertions(+), 86 deletions(-) create mode 100644 tasklist.txt diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java index 980a0d3b7..33a86442d 100644 --- a/src/main/java/chatbot/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -2,123 +2,186 @@ import chatbot.*; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.util.Scanner; public class Chatbot { + public static int numOfTasks = 0; + public static void saveToFile(String input) { + String str = input + System.lineSeparator(); + try { + Path path = Paths.get("./tasklist.txt"); + //System.out.println("fullpath: " + path.toAbsolutePath().toString()); + if(Files.exists(path)) { + Files.write(path, str.getBytes(), StandardOpenOption.APPEND); + } else { + Files.write(path, str.getBytes(), StandardOpenOption.CREATE); + } - public static void main(String[] args) throws Exception { - Task[] tasks = new Task[100]; - int numOfTasks = 0; + } catch (IOException e) { + //exception handling left as an exercise for the reader + } + } + public static void deleteFromFile(int index) { - String greetingMsg = "____________________________________________________________\n" + - " Hello! I'm Chatbot\n" + - " What can I do for you?\n" + - "____________________________________________________________\n"; - String byeMsg = "____________________________________________________________\n" + - " Bye. Hope to see you again soon!\n" + - "____________________________________________________________\n"; - System.out.println(greetingMsg); + } - Scanner in = new Scanner(System.in); - String input = in.nextLine(); - while(!input.equals("bye")) { - try { - if (input.equals("list")) { - System.out.println("____________________________________________________________"); - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < numOfTasks; i++) { - //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); - System.out.println(" " + (i + 1) + "." + tasks[i]); - } - System.out.println("____________________________________________________________"); - } else if (input.startsWith("mark ")) { - String number = input.replace("mark ", "").trim(); - int markTaskNo = Integer.parseInt(number); - if (markTaskNo > 0) { - tasks[markTaskNo - 1].markAsDone(); - } - System.out.println("____________________________________________________________"); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" [" + tasks[markTaskNo - 1].getStatusIcon() + "] " + tasks[markTaskNo - 1].getDescription()); - System.out.println("____________________________________________________________"); - } else if (input.startsWith("unmark ")) { - String number = input.replace("unmark ", "").trim(); - int unmarkedTaskNo = Integer.parseInt(number); - if (unmarkedTaskNo > 0) { - tasks[unmarkedTaskNo - 1].markAsUndone(); - } - System.out.println("____________________________________________________________"); - System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println(" [" + tasks[unmarkedTaskNo - 1].getStatusIcon() + "] " + tasks[unmarkedTaskNo - 1].getDescription()); - System.out.println("____________________________________________________________"); - } else if (input.startsWith("todo")) { - String msg = input.replace("todo", "").trim(); - if(msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty."); - } + public static void parseFile(Task[] tasks) throws IOException { + Path path = Paths.get("./tasklist.txt"); + if(!Files.exists(path)) { + return; + } + try (BufferedReader br = new BufferedReader(new FileReader(path.toAbsolutePath().toString()))) { + String line; + while ((line = br.readLine()) != null) { + // process the line. + //System.out.println(line); + if(line.trim().isEmpty()) { + continue; + } + parseCommand(tasks, line, false); - Task task = new Todo(msg); - tasks[numOfTasks] = task; - numOfTasks++; + } + } + } + public static void parseCommand(Task[] tasks, String input, boolean isUserInput) { + try { + if (input.equals("list")) { + System.out.println("____________________________________________________________"); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < numOfTasks; i++) { + //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); + System.out.println(" " + (i + 1) + "." + tasks[i]); + } + System.out.println("____________________________________________________________"); + } else if (input.startsWith("mark ")) { + String number = input.replace("mark ", "").trim(); + int markTaskNo = Integer.parseInt(number); + if (markTaskNo > 0) { + tasks[markTaskNo - 1].markAsDone(); + } + System.out.println("____________________________________________________________"); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" [" + tasks[markTaskNo - 1].getStatusIcon() + "] " + tasks[markTaskNo - 1].getDescription()); + System.out.println("____________________________________________________________"); + } else if (input.startsWith("unmark ")) { + String number = input.replace("unmark ", "").trim(); + int unmarkedTaskNo = Integer.parseInt(number); + if (unmarkedTaskNo > 0) { + tasks[unmarkedTaskNo - 1].markAsUndone(); + } + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println(" [" + tasks[unmarkedTaskNo - 1].getStatusIcon() + "] " + tasks[unmarkedTaskNo - 1].getDescription()); + System.out.println("____________________________________________________________"); + } else if (input.startsWith("todo")) { + String msg = input.replace("todo", "").trim(); + if(msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty."); + } + + Task task = new Todo(msg); + tasks[numOfTasks] = task; + numOfTasks++; + + if(isUserInput) { + saveToFile(input); System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); System.out.println(" " + task); System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); System.out.println("____________________________________________________________"); - } else if (input.startsWith("deadline")) { - String msg = input.replace("deadline", "").trim(); - if(msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); - } + } - String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate - String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description + } else if (input.startsWith("deadline")) { + String msg = input.replace("deadline", "").trim(); + if(msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); + } + + String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate + String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description - Task task = new Deadline(desc, byDate); - tasks[numOfTasks] = task; - numOfTasks++; + Task task = new Deadline(desc, byDate); + tasks[numOfTasks] = task; + numOfTasks++; + if(isUserInput) { + saveToFile(input); System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); System.out.println(" " + task); System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); System.out.println("____________________________________________________________"); - } else if (input.startsWith("event")) { - String msg = input.replace("event", "").trim(); - if(msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty."); - } + } + + } else if (input.startsWith("event")) { + String msg = input.replace("event", "").trim(); + if(msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty."); + } - String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); - String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); - String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); - String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description + String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); + String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); + String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); + String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description - Task task = new Event(desc, fromDate, toDate); - tasks[numOfTasks] = task; - numOfTasks++; + Task task = new Event(desc, fromDate, toDate); + tasks[numOfTasks] = task; + numOfTasks++; + if(isUserInput) { + saveToFile(input); System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); System.out.println(" " + task); System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); System.out.println("____________________________________________________________"); - } else { - // unknown command - throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } - } catch(ChatbotUnknownCommandException e) { - System.out.println("____________________________________________________________"); - System.out.println(e.getMessage()); - System.out.println("____________________________________________________________"); - } catch(ChatbotEmptyDescException e) { - System.out.println("____________________________________________________________"); - System.out.println(e.getMessage()); - System.out.println("____________________________________________________________"); - } catch (Exception e) { - System.out.println("Unknown exception. Error message: " + e.getMessage()); + + } else { + // unknown command + throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } + } catch(ChatbotUnknownCommandException e) { + System.out.println("____________________________________________________________"); + System.out.println(e.getMessage()); + System.out.println("____________________________________________________________"); + } catch(ChatbotEmptyDescException e) { + System.out.println("____________________________________________________________"); + System.out.println(e.getMessage()); + System.out.println("____________________________________________________________"); + } catch (Exception e) { + System.out.println("Unknown exception. Error message: " + e.getMessage()); + } + } + public static void main(String[] args) throws Exception { + Task[] tasks = new Task[100]; + + parseFile(tasks); + + + + String greetingMsg = "____________________________________________________________\n" + + " Hello! I'm Chatbot\n" + + " What can I do for you?\n" + + "____________________________________________________________\n"; + String byeMsg = "____________________________________________________________\n" + + " Bye. Hope to see you again soon!\n" + + "____________________________________________________________\n"; + System.out.println(greetingMsg); + + Scanner in = new Scanner(System.in); + String input = in.nextLine(); + while(!input.equals("bye")) { + parseCommand(tasks, input, true); input = in.nextLine(); } diff --git a/tasklist.txt b/tasklist.txt new file mode 100644 index 000000000..62b8864c1 --- /dev/null +++ b/tasklist.txt @@ -0,0 +1,2 @@ +todo 1111 +deadline assignment /by 23jan From 55262423dfd141d13d90a8632076b2642dd0e891 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Wed, 20 Sep 2023 23:52:49 +0800 Subject: [PATCH 12/24] Fixed bug where Task status was not saved in storage file --- src/main/java/chatbot/Chatbot.java | 55 ++++++++++++++++-------------- tasklist.txt | 3 ++ 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java index 33a86442d..368e5ca6f 100644 --- a/src/main/java/chatbot/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -9,6 +9,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.sql.Array; +import java.util.ArrayList; import java.util.Scanner; public class Chatbot { @@ -28,11 +30,8 @@ public static void saveToFile(String input) { //exception handling left as an exercise for the reader } } - public static void deleteFromFile(int index) { - } - - public static void parseFile(Task[] tasks) throws IOException { + public static void parseFile(ArrayList tasks) throws IOException { Path path = Paths.get("./tasklist.txt"); if(!Files.exists(path)) { return; @@ -51,36 +50,44 @@ public static void parseFile(Task[] tasks) throws IOException { } } - public static void parseCommand(Task[] tasks, String input, boolean isUserInput) { + public static void parseCommand(ArrayList tasks, String input, boolean isUserInput) { try { if (input.equals("list")) { System.out.println("____________________________________________________________"); System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < numOfTasks; i++) { //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); - System.out.println(" " + (i + 1) + "." + tasks[i]); + System.out.println(" " + (i + 1) + "." + tasks.get(i)); } System.out.println("____________________________________________________________"); } else if (input.startsWith("mark ")) { String number = input.replace("mark ", "").trim(); int markTaskNo = Integer.parseInt(number); if (markTaskNo > 0) { - tasks[markTaskNo - 1].markAsDone(); + tasks.get(markTaskNo - 1).markAsDone(); } - System.out.println("____________________________________________________________"); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" [" + tasks[markTaskNo - 1].getStatusIcon() + "] " + tasks[markTaskNo - 1].getDescription()); - System.out.println("____________________________________________________________"); + if(isUserInput) { + saveToFile(input); + System.out.println("____________________________________________________________"); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" [" + tasks.get(markTaskNo - 1).getStatusIcon() + "] " + tasks.get(markTaskNo - 1).getDescription()); + System.out.println("____________________________________________________________"); + } + } else if (input.startsWith("unmark ")) { String number = input.replace("unmark ", "").trim(); int unmarkedTaskNo = Integer.parseInt(number); if (unmarkedTaskNo > 0) { - tasks[unmarkedTaskNo - 1].markAsUndone(); + tasks.get(unmarkedTaskNo - 1).markAsUndone(); } - System.out.println("____________________________________________________________"); - System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println(" [" + tasks[unmarkedTaskNo - 1].getStatusIcon() + "] " + tasks[unmarkedTaskNo - 1].getDescription()); - System.out.println("____________________________________________________________"); + if(isUserInput) { + saveToFile(input); + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println(" [" + tasks.get(unmarkedTaskNo - 1).getStatusIcon() + "] " + tasks.get(unmarkedTaskNo - 1).getDescription()); + System.out.println("____________________________________________________________"); + } + } else if (input.startsWith("todo")) { String msg = input.replace("todo", "").trim(); if(msg.isEmpty()) { @@ -88,7 +95,7 @@ public static void parseCommand(Task[] tasks, String input, boolean isUserInput) } Task task = new Todo(msg); - tasks[numOfTasks] = task; + tasks.add(task); numOfTasks++; if(isUserInput) { @@ -96,7 +103,7 @@ public static void parseCommand(Task[] tasks, String input, boolean isUserInput) System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); System.out.println("____________________________________________________________"); } @@ -110,7 +117,7 @@ public static void parseCommand(Task[] tasks, String input, boolean isUserInput) String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description Task task = new Deadline(desc, byDate); - tasks[numOfTasks] = task; + tasks.add(task); numOfTasks++; if(isUserInput) { @@ -118,7 +125,7 @@ public static void parseCommand(Task[] tasks, String input, boolean isUserInput) System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); System.out.println("____________________________________________________________"); } @@ -134,7 +141,7 @@ public static void parseCommand(Task[] tasks, String input, boolean isUserInput) String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description Task task = new Event(desc, fromDate, toDate); - tasks[numOfTasks] = task; + tasks.add(task); numOfTasks++; if(isUserInput) { @@ -142,7 +149,7 @@ public static void parseCommand(Task[] tasks, String input, boolean isUserInput) System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list."); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); System.out.println("____________________________________________________________"); } @@ -163,12 +170,10 @@ public static void parseCommand(Task[] tasks, String input, boolean isUserInput) } } public static void main(String[] args) throws Exception { - Task[] tasks = new Task[100]; + ArrayList tasks = new ArrayList(100); parseFile(tasks); - - String greetingMsg = "____________________________________________________________\n" + " Hello! I'm Chatbot\n" + " What can I do for you?\n" + diff --git a/tasklist.txt b/tasklist.txt index 62b8864c1..0e5568f50 100644 --- a/tasklist.txt +++ b/tasklist.txt @@ -1,2 +1,5 @@ todo 1111 deadline assignment /by 23jan +todo go jogging +todo go jogging +mark 1 From 5e2946d665d43803b48707a3ecefb2814833f07c Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Thu, 21 Sep 2023 00:29:42 +0800 Subject: [PATCH 13/24] Removed numOfTask variable references --- src/main/java/chatbot/Chatbot.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java index ce2c5fb35..9936089a4 100644 --- a/src/main/java/chatbot/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -95,7 +95,6 @@ public static void parseCommand(ArrayList tasks, String input, boolean isU Task task = new Todo(msg); tasks.add(task); - numOfTasks++; if (isUserInput) { saveToFile(input); @@ -117,7 +116,6 @@ public static void parseCommand(ArrayList tasks, String input, boolean isU Task task = new Deadline(desc, byDate); tasks.add(task); - numOfTasks++; if (isUserInput) { saveToFile(input); @@ -141,7 +139,6 @@ public static void parseCommand(ArrayList tasks, String input, boolean isU Task task = new Event(desc, fromDate, toDate); tasks.add(task); - numOfTasks++; if (isUserInput) { saveToFile(input); From 46504a428b6eea0bcd3848f783e0fc879ff3d5a1 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Fri, 6 Oct 2023 23:07:24 +0800 Subject: [PATCH 14/24] Implemented A-MoreOOP --- src/main/java/chatbot/Chatbot.java | 210 ++++------------------------- src/main/java/chatbot/Command.java | 139 +++++++++++++++++++ src/main/java/chatbot/Parser.java | 26 ++++ src/main/java/chatbot/Storage.java | 53 ++++++++ src/main/java/chatbot/Ui.java | 27 ++++ tasklist.txt | 3 + 6 files changed, 277 insertions(+), 181 deletions(-) create mode 100644 src/main/java/chatbot/Command.java create mode 100644 src/main/java/chatbot/Parser.java create mode 100644 src/main/java/chatbot/Storage.java create mode 100644 src/main/java/chatbot/Ui.java diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java index 9936089a4..359ae3722 100644 --- a/src/main/java/chatbot/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -14,198 +14,46 @@ import java.util.Scanner; public class Chatbot { - public static void saveToFile(String input) { - String str = input + System.lineSeparator(); - try { - Path path = Paths.get("./tasklist.txt"); - //System.out.println("fullpath: " + path.toAbsolutePath().toString()); - if(Files.exists(path)) { - Files.write(path, str.getBytes(), StandardOpenOption.APPEND); - } else { - Files.write(path, str.getBytes(), StandardOpenOption.CREATE); - } - - } catch (IOException e) { - //exception handling left as an exercise for the reader - } + private Storage storage; + private Parser parser; + private Ui ui; + + public Chatbot() { + this.ui = new Ui(); + this.storage = new Storage("./tasklist.txt"); + this.parser = new Parser(); } - public static void parseFile(ArrayList tasks) throws IOException { - Path path = Paths.get("./tasklist.txt"); - if(!Files.exists(path)) { - return; - } - try (BufferedReader br = new BufferedReader(new FileReader(path.toAbsolutePath().toString()))) { - String line; - while ((line = br.readLine()) != null) { - // process the line. - //System.out.println(line); - if(line.trim().isEmpty()) { - continue; - } - parseCommand(tasks, line, false); - - } - } - } + public void run() throws IOException { + ArrayList tasks = new ArrayList(100); + this.ui.showGreetingMessage();; - public static void parseCommand(ArrayList tasks, String input, boolean isUserInput) { try { - if (input.equals("list")) { - System.out.println("____________________________________________________________"); - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < tasks.size(); i++) { - //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); - System.out.println(" " + (i + 1) + "." + tasks.get(i)); - } - System.out.println("____________________________________________________________"); - } else if (input.startsWith("mark ")) { - String number = input.replace("mark ", "").trim(); - int markTaskNo = Integer.parseInt(number); - if (markTaskNo > 0) { - tasks.get(markTaskNo - 1).markAsDone(); - } - if (isUserInput) { - saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" [" + tasks.get(markTaskNo - 1).getStatusIcon() + "] " + tasks.get(markTaskNo - 1).getDescription()); - System.out.println("____________________________________________________________"); - } - - } else if (input.startsWith("unmark ")) { - String number = input.replace("unmark ", "").trim(); - int unmarkedTaskNo = Integer.parseInt(number); - if (unmarkedTaskNo > 0) { - tasks.get(unmarkedTaskNo - 1).markAsUndone(); - } - if (isUserInput) { - saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println(" [" + tasks.get(unmarkedTaskNo - 1).getStatusIcon() + "] " + tasks.get(unmarkedTaskNo - 1).getDescription()); - System.out.println("____________________________________________________________"); - } - - } else if (input.startsWith("todo")) { - String msg = input.replace("todo", "").trim(); - if (msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty."); - } - - Task task = new Todo(msg); - tasks.add(task); - - if (isUserInput) { - saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); - System.out.println("____________________________________________________________"); - } - - } else if (input.startsWith("deadline")) { - String msg = input.replace("deadline", "").trim(); - if (msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); - } - - String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate - String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description - - Task task = new Deadline(desc, byDate); - tasks.add(task); - - if (isUserInput) { - saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); - System.out.println("____________________________________________________________"); - } - - } else if (input.startsWith("event")) { - String msg = input.replace("event", "").trim(); - if (msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty."); - } - - String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); - String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); - String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); - String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description - - Task task = new Event(desc, fromDate, toDate); - tasks.add(task); - - if (isUserInput) { - saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); - System.out.println("____________________________________________________________"); - } + ArrayList lines = storage.parseFile(tasks); + for(String line : lines) { + Command c = this.parser.parseCommand(line); + c.execute(tasks, false); + } - } else if (input.startsWith("delete")) { - String msg = input.replace("delete", "").trim(); - if (msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of delete cannot be empty."); - } - int indexToRemove = Integer.parseInt(msg); - if( indexToRemove > 0 ) { - indexToRemove -= 1; - } - Task task = tasks.get(indexToRemove); - tasks.remove(indexToRemove); - if (isUserInput) { - saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Noted. I've removed this task: "); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); - System.out.println("____________________________________________________________"); - } - } else { - // unknown command - throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + Scanner in = new Scanner(System.in); + String input = in.nextLine(); + while(!input.equals("bye")) { + Command c = this.parser.parseCommand(input); + c.execute(tasks, true); + input = in.nextLine(); } } catch(ChatbotUnknownCommandException e) { - System.out.println("____________________________________________________________"); - System.out.println(e.getMessage()); - System.out.println("____________________________________________________________"); + this.ui.showError(e.getMessage(), true); } catch(ChatbotEmptyDescException e) { - System.out.println("____________________________________________________________"); - System.out.println(e.getMessage()); - System.out.println("____________________________________________________________"); + this.ui.showError(e.getMessage(), true); } catch (Exception e) { - System.out.println("Unknown exception. Error message: " + e.getMessage()); + this.ui.showError(e.getMessage(), false); } - } - public static void main(String[] args) throws Exception { - ArrayList tasks = new ArrayList(100); - - parseFile(tasks); - - String greetingMsg = "____________________________________________________________\n" + - " Hello! I'm Chatbot\n" + - " What can I do for you?\n" + - "____________________________________________________________\n"; - String byeMsg = "____________________________________________________________\n" + - " Bye. Hope to see you again soon!\n" + - "____________________________________________________________\n"; - System.out.println(greetingMsg); - Scanner in = new Scanner(System.in); - String input = in.nextLine(); - while(!input.equals("bye")) { - parseCommand(tasks, input, true); - - input = in.nextLine(); - } - System.out.println(byeMsg); + this.ui.showByeMessage();; + } + public static void main(String[] args) throws Exception { + new Chatbot().run(); } } diff --git a/src/main/java/chatbot/Command.java b/src/main/java/chatbot/Command.java new file mode 100644 index 000000000..6fe0a6f93 --- /dev/null +++ b/src/main/java/chatbot/Command.java @@ -0,0 +1,139 @@ +package chatbot; + +import java.util.ArrayList; + +public class Command { + private Storage storage; + private String commandType; + private String input; + public Command(String commandType, String input) { + this.storage = new Storage("./tasklist.txt"); + this.commandType = commandType; + this.input = input; + } + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUnknownCommandException, ChatbotEmptyDescException { + + if (this.commandType.equals("list")) { + System.out.println("____________________________________________________________"); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); + System.out.println(" " + (i + 1) + "." + tasks.get(i)); + } + System.out.println("____________________________________________________________"); + } else if (this.commandType.equals("mark")) { + String number = input.replace("mark ", "").trim(); + int markTaskNo = Integer.parseInt(number); + if (markTaskNo > 0) { + tasks.get(markTaskNo - 1).markAsDone(); + } + if (isUserInput) { + storage.saveToFile(input); + System.out.println("____________________________________________________________"); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" [" + tasks.get(markTaskNo - 1).getStatusIcon() + "] " + tasks.get(markTaskNo - 1).getDescription()); + System.out.println("____________________________________________________________"); + } + + } else if (this.commandType.equals("unmark")) { + String number = input.replace("unmark ", "").trim(); + int unmarkedTaskNo = Integer.parseInt(number); + if (unmarkedTaskNo > 0) { + tasks.get(unmarkedTaskNo - 1).markAsUndone(); + } + if (isUserInput) { + storage.saveToFile(input); + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println(" [" + tasks.get(unmarkedTaskNo - 1).getStatusIcon() + "] " + tasks.get(unmarkedTaskNo - 1).getDescription()); + System.out.println("____________________________________________________________"); + } + + } else if (this.commandType.equals("todo")) { + String msg = input.replace("todo", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty."); + } + + Task task = new Todo(msg); + tasks.add(task); + + if (isUserInput) { + storage.saveToFile(input); + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + + } else if (this.commandType.equals("deadline")) { + String msg = input.replace("deadline", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); + } + + String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate + String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description + + Task task = new Deadline(desc, byDate); + tasks.add(task); + + if (isUserInput) { + storage.saveToFile(input); + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + + } else if (this.commandType.equals("event")) { + String msg = input.replace("event", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty."); + } + + String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); + String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); + String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); + String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description + + Task task = new Event(desc, fromDate, toDate); + tasks.add(task); + + if (isUserInput) { + storage.saveToFile(input); + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + + } else if (this.commandType.equals("delete")) { + String msg = input.replace("delete", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of delete cannot be empty."); + } + int indexToRemove = Integer.parseInt(msg); + if( indexToRemove > 0 ) { + indexToRemove -= 1; + } + Task task = tasks.get(indexToRemove); + tasks.remove(indexToRemove); + if (isUserInput) { + storage.saveToFile(input); + System.out.println("____________________________________________________________"); + System.out.println(" Noted. I've removed this task: "); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + } else { + // unknown command + throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + } + } + +} diff --git a/src/main/java/chatbot/Parser.java b/src/main/java/chatbot/Parser.java new file mode 100644 index 000000000..cf283d86a --- /dev/null +++ b/src/main/java/chatbot/Parser.java @@ -0,0 +1,26 @@ +package chatbot; + +import java.util.ArrayList; + +public class Parser { + public Command parseCommand(String input) throws ChatbotUnknownCommandException { + if (input.equals("list")) { + return new Command("list", input); + } else if (input.startsWith("mark ")) { + return new Command("mark", input); + } else if (input.startsWith("unmark ")) { + return new Command("unmark", input); + } else if (input.startsWith("todo")) { + return new Command("todo", input); + } else if (input.startsWith("deadline")) { + return new Command("deadline", input); + } else if (input.startsWith("event")) { + return new Command("event", input); + } else if (input.startsWith("delete")) { + return new Command("delete", input); + } else { + // unknown command + throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + } + } +} diff --git a/src/main/java/chatbot/Storage.java b/src/main/java/chatbot/Storage.java new file mode 100644 index 000000000..e5e041205 --- /dev/null +++ b/src/main/java/chatbot/Storage.java @@ -0,0 +1,53 @@ +package chatbot; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; + + +public class Storage { + private String filepath; + public Storage(String filepath) { + this.filepath = filepath; + } + public ArrayList parseFile(ArrayList tasks) throws IOException { + Path path = Paths.get(this.filepath); + ArrayList lines = new ArrayList<>(); + if(!Files.exists(path)) { + return lines; + } + try (BufferedReader br = new BufferedReader(new FileReader(path.toAbsolutePath().toString()))) { + String line; + while ((line = br.readLine()) != null) { + // process the line. + //System.out.println(line); + if(line.trim().isEmpty()) { + continue; + } + lines.add(line); + //parseCommand(tasks, line, false); + } + } + return lines; + } + public void saveToFile(String input) { + String str = input + System.lineSeparator(); + try { + Path path = Paths.get(this.filepath); + //System.out.println("fullpath: " + path.toAbsolutePath().toString()); + if(Files.exists(path)) { + Files.write(path, str.getBytes(), StandardOpenOption.APPEND); + } else { + Files.write(path, str.getBytes(), StandardOpenOption.CREATE); + } + + } catch (IOException e) { + //exception handling left as an exercise for the reader + } + } +} diff --git a/src/main/java/chatbot/Ui.java b/src/main/java/chatbot/Ui.java new file mode 100644 index 000000000..67b8926c6 --- /dev/null +++ b/src/main/java/chatbot/Ui.java @@ -0,0 +1,27 @@ +package chatbot; + +public class Ui { + public void showGreetingMessage() { + String greetingMsg = "____________________________________________________________\n" + + " Hello! I'm Chatbot\n" + + " What can I do for you?\n" + + "____________________________________________________________\n"; + System.out.println(greetingMsg); + } + public void showByeMessage() { + String byeMsg = "____________________________________________________________\n" + + " Bye. Hope to see you again soon!\n" + + "____________________________________________________________\n"; + System.out.println(byeMsg); + } + + public void showError(String message, boolean addLines) { + if( addLines ) { + System.out.println("____________________________________________________________"); + } + System.out.println("Unknown exception. Error message: " + message); + if( addLines ) { + System.out.println("____________________________________________________________"); + } + } +} diff --git a/tasklist.txt b/tasklist.txt index 383652a35..94df5a5d9 100644 --- a/tasklist.txt +++ b/tasklist.txt @@ -1,3 +1,6 @@ event jeremy attend CS2113 lecture Friday 22 Sep 2023 /from 4 /to 6pm deadline jeremy submit UG draft /by today 2359 delete 2 +todo borrow book +todo borrow new book +todo 111 From 5b89da7f3125619e29de497acbe9522bc0683403 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Fri, 6 Oct 2023 23:20:12 +0800 Subject: [PATCH 15/24] Implemented A-MoreOOP --- src/main/java/chatbot/Chatbot.java | 12 +++++++----- src/main/java/chatbot/TaskList.java | 15 +++++++++++++++ tasklist.txt | 1 + 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 src/main/java/chatbot/TaskList.java diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java index 359ae3722..11efcdd86 100644 --- a/src/main/java/chatbot/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -17,29 +17,31 @@ public class Chatbot { private Storage storage; private Parser parser; private Ui ui; + private TaskList taskList; public Chatbot() { this.ui = new Ui(); this.storage = new Storage("./tasklist.txt"); this.parser = new Parser(); + this.taskList = new TaskList(); } public void run() throws IOException { - ArrayList tasks = new ArrayList(100); - this.ui.showGreetingMessage();; + + this.ui.showGreetingMessage(); try { - ArrayList lines = storage.parseFile(tasks); + ArrayList lines = storage.parseFile(this.taskList.getTaskList()); for(String line : lines) { Command c = this.parser.parseCommand(line); - c.execute(tasks, false); + c.execute(this.taskList.getTaskList(), false); } Scanner in = new Scanner(System.in); String input = in.nextLine(); while(!input.equals("bye")) { Command c = this.parser.parseCommand(input); - c.execute(tasks, true); + c.execute(this.taskList.getTaskList(), true); input = in.nextLine(); } } catch(ChatbotUnknownCommandException e) { diff --git a/src/main/java/chatbot/TaskList.java b/src/main/java/chatbot/TaskList.java new file mode 100644 index 000000000..5759bf39f --- /dev/null +++ b/src/main/java/chatbot/TaskList.java @@ -0,0 +1,15 @@ +package chatbot; + +import java.util.ArrayList; + +public class TaskList { + private ArrayList tasks = new ArrayList(100); + + public ArrayList getTaskList() { + return this.tasks; + } + + public void addTask(Task task) { + this.tasks.add(task); + } +} diff --git a/tasklist.txt b/tasklist.txt index 94df5a5d9..a74603adf 100644 --- a/tasklist.txt +++ b/tasklist.txt @@ -4,3 +4,4 @@ delete 2 todo borrow book todo borrow new book todo 111 +todo aasdf From c33523b1c18291a3b02e3afe02c375f5b8589b47 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Fri, 6 Oct 2023 23:41:31 +0800 Subject: [PATCH 16/24] Implemented branch-Level-9 --- src/main/java/chatbot/Chatbot.java | 7 +++++++ src/main/java/chatbot/Command.java | 15 +++++++++++++++ src/main/java/chatbot/Parser.java | 8 ++++++++ src/main/java/chatbot/Task.java | 25 +++++++++++++++++++++++++ src/main/java/chatbot/Ui.java | 19 ++++++++++++++++++- 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java index 11efcdd86..838c47a97 100644 --- a/src/main/java/chatbot/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -26,6 +26,13 @@ public Chatbot() { this.taskList = new TaskList(); } + + /** + * Runs the main routine of the app + * + * @author Jeremy + * @since 2023-10-06 + */ public void run() throws IOException { this.ui.showGreetingMessage(); diff --git a/src/main/java/chatbot/Command.java b/src/main/java/chatbot/Command.java index 6fe0a6f93..430d537b1 100644 --- a/src/main/java/chatbot/Command.java +++ b/src/main/java/chatbot/Command.java @@ -130,6 +130,21 @@ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUn System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); System.out.println("____________________________________________________________"); } + } else if (this.commandType.equals("find")) { + String searchFor = input.replace("find", "").trim(); + if (searchFor.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The find command needs a string to search for."); + } + System.out.println("____________________________________________________________"); + System.out.println(" Here are the matching tasks in your list:"); + + for(Task task : tasks) { + String desc = task.getDescription(); + if(desc.contains(searchFor)) { + System.out.println(" " + task); + } + } + System.out.println("____________________________________________________________"); } else { // unknown command throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); diff --git a/src/main/java/chatbot/Parser.java b/src/main/java/chatbot/Parser.java index cf283d86a..f8d679abf 100644 --- a/src/main/java/chatbot/Parser.java +++ b/src/main/java/chatbot/Parser.java @@ -3,6 +3,12 @@ import java.util.ArrayList; public class Parser { + /** + * Parse the command and returns a Command object + * + * @author Jeremy + * @since 2023-10-06 + */ public Command parseCommand(String input) throws ChatbotUnknownCommandException { if (input.equals("list")) { return new Command("list", input); @@ -18,6 +24,8 @@ public Command parseCommand(String input) throws ChatbotUnknownCommandException return new Command("event", input); } else if (input.startsWith("delete")) { return new Command("delete", input); + } else if (input.startsWith("find")) { + return new Command("find", input); } else { // unknown command throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); diff --git a/src/main/java/chatbot/Task.java b/src/main/java/chatbot/Task.java index f061a5cee..9e9adc8e5 100644 --- a/src/main/java/chatbot/Task.java +++ b/src/main/java/chatbot/Task.java @@ -7,15 +7,40 @@ public Task(String description) { this.description = description; this.isDone = false; } + + /** + * Get the description + * + * @author Jeremy + * @since 2023-10-06 + */ public String getDescription() { return this.description; } + /** + * Get the status icon + * + * @author Jeremy + * @since 2023-10-06 + */ public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + /** + * Mark task as done + * + * @author Jeremy + * @since 2023-10-06 + */ public void markAsDone() { this.isDone = true; } + /** + * Mark task as undone + * + * @author Jeremy + * @since 2023-10-06 + */ public void markAsUndone() { this.isDone = false; } diff --git a/src/main/java/chatbot/Ui.java b/src/main/java/chatbot/Ui.java index 67b8926c6..88990837c 100644 --- a/src/main/java/chatbot/Ui.java +++ b/src/main/java/chatbot/Ui.java @@ -1,6 +1,12 @@ package chatbot; public class Ui { + /** + * Show greeting message when app first launches. + * + * @author Jeremy + * @since 2023-10-06 + */ public void showGreetingMessage() { String greetingMsg = "____________________________________________________________\n" + " Hello! I'm Chatbot\n" + @@ -8,13 +14,24 @@ public void showGreetingMessage() { "____________________________________________________________\n"; System.out.println(greetingMsg); } + /** + * Show bye message when app is closing + * + * @author Jeremy + * @since 2023-10-06 + */ public void showByeMessage() { String byeMsg = "____________________________________________________________\n" + " Bye. Hope to see you again soon!\n" + "____________________________________________________________\n"; System.out.println(byeMsg); } - + /** + * Show an error message, with additional formatting possible + * + * @author Jeremy + * @since 2023-10-06 + */ public void showError(String message, boolean addLines) { if( addLines ) { System.out.println("____________________________________________________________"); From 2026fdf9728ec9a7e7e2b47f00bbeef9686aacb5 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Fri, 6 Oct 2023 23:59:37 +0800 Subject: [PATCH 17/24] A-UserGuide --- docs/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/README.md b/docs/README.md index 8077118eb..349cf7191 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,5 @@ # User Guide +The Chatbot name is very intuitive - Chatbot. ## Features From 0465acec0cea22f2b57ebae84fb259cf4e72df97 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Sat, 7 Oct 2023 00:26:37 +0800 Subject: [PATCH 18/24] Modified user guide --- docs/README.md | 183 ++++++++++++++++++++++++++++++++++++++++++++++--- tasklist.txt | 4 ++ 2 files changed, 176 insertions(+), 11 deletions(-) diff --git a/docs/README.md b/docs/README.md index 349cf7191..18a89646f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,29 +2,190 @@ The Chatbot name is very intuitive - Chatbot. ## Features +1. list +2. mark +3. unmark +4. todo +5. deadline +6. event +7. delete +8. find + +### Feature-list +List all tasks +### Feature-mark +Mark a task as done +### Feature-unmark +Unmark a task as done +### Feature-todo +Add a todo +### Feature-deadline +Add a deadline +### Feature-event +Add an event +### Feature-delete +Delete a task +### Feature-find +Find a task -### Feature-ABC -Description of the feature. -### Feature-XYZ -Description of the feature. ## Usage -### `Keyword` - Describe action +### `list` - list all tasks +List all the tasks recorded + +Command: `list` + +Argument: Nil + +Example: `list` + +Example of outcome: + + Tasks are listed +``` + ____________________________________________________________ + Here are the tasks in your list: + 1.[T][X] read book + 2.[D][ ] return book (by: June 6th) + 3.[E][ ] project meeting (from: Aug 6th 2pm to: 4pm) + 4.[T][X] join sports club + 5.[T][ ] borrow book + ____________________________________________________________ +``` + +### `mark` - mark a task +Command: `mark` + +Argument: ID of the task + +Example: `mark 1` + +Example of outcome: + + Task is marked as done +``` +____________________________________________________________ + Nice! I've marked this task as done: + [X] borrow new book +____________________________________________________________ +``` + +### `unmark` - unmark a task +Mark a task as done + +Command: `unmark` + +Argument: ID of the task + +Example: `unmark 1` + +Example of outcome: + + Task is unmarked +``` +____________________________________________________________ + OK, I've marked this task as not done yet: + [ ] borrow new book +____________________________________________________________ +``` + +### `todo` - add a todo + +Command: `todo` + +Argument: description of the task + +Example: `todo borrow book` + +Example of outcome: -Describe the action and its outcome. + Todo is added +``` + ____________________________________________________________ + Got it. I've added this task: + [T][ ] borrow book + Now you have 5 tasks in the list. + ____________________________________________________________ +``` + +### `deadline` - add a deadline + +Command: `deadline` + +Argument: description of the deadline + +Example: `deadline return book /by Sunday` + +Example of outcome: + + Deadline is added + +``` + ____________________________________________________________ + Got it. I've added this task: + [D][ ] return book (by: Sunday) + Now you have 6 tasks in the list. + ____________________________________________________________ +``` + +### `event` - add an event + +Command: `event` + +Argument: description of the event -Example of usage: +Example: `event project meeting /from Mon 2pm /to 4pm` -`keyword (optional arguments)` +Example of outcome: -Expected outcome: + Task is marked as done -Description of the outcome. +``` + ____________________________________________________________ + Got it. I've added this task: + [E][ ] project meeting (from: Mon 2pm to: 4pm) + Now you have 7 tasks in the list. + ____________________________________________________________ +``` +### `delete` - delete a task + +Command: `delete` + +Argument: ID of the task to delete + +Example: `delete 1` + +Example of outcome: + + Task is deleted ``` -expected output + ____________________________________________________________ + Noted. I've removed this task: + [E][ ] project meeting (from: Aug 6th 2pm to: 4pm) + Now you have 4 tasks in the list. + ____________________________________________________________ +``` + +### `find` - find a task +Command: `find` + +Argument: `String` What to search for + +Example: `find book` + +Example of outcome: + + Book is found + ``` +____________________________________________________________ + Here are the matching tasks in your list: + [T][ ] borrow book + [T][ ] borrow new book +____________________________________________________________ +``` \ No newline at end of file diff --git a/tasklist.txt b/tasklist.txt index a74603adf..51c79a822 100644 --- a/tasklist.txt +++ b/tasklist.txt @@ -5,3 +5,7 @@ todo borrow book todo borrow new book todo 111 todo aasdf +mark 1 +unmark 1 +mark 3 +unmark 3 From 3c63edfb17446a975ea31acc063f5b485ff636e5 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Sun, 8 Oct 2023 20:27:34 +0800 Subject: [PATCH 19/24] update docs --- docs/README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 18a89646f..98bab9dc8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -67,6 +67,7 @@ Example: `mark 1` Example of outcome: Task is marked as done + ``` ____________________________________________________________ Nice! I've marked this task as done: @@ -86,6 +87,7 @@ Example: `unmark 1` Example of outcome: Task is unmarked + ``` ____________________________________________________________ OK, I've marked this task as not done yet: @@ -104,6 +106,7 @@ Example: `todo borrow book` Example of outcome: Todo is added + ``` ____________________________________________________________ Got it. I've added this task: @@ -142,7 +145,7 @@ Example: `event project meeting /from Mon 2pm /to 4pm` Example of outcome: - Task is marked as done + Event is added ``` ____________________________________________________________ @@ -180,7 +183,7 @@ Example: `find book` Example of outcome: - Book is found + book is found ``` ____________________________________________________________ From 8b4c583a588f3ab2e6854bc710de2f0fc04b086e Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Tue, 24 Oct 2023 16:19:28 +0800 Subject: [PATCH 20/24] extracted most .println calls from Command to the Ui class --- src/main/java/chatbot/Command.java | 70 +++++++++++++----------------- src/main/java/chatbot/Ui.java | 58 +++++++++++++++++++++++++ tasklist.txt | 6 +++ 3 files changed, 94 insertions(+), 40 deletions(-) diff --git a/src/main/java/chatbot/Command.java b/src/main/java/chatbot/Command.java index 430d537b1..dd04fcd97 100644 --- a/src/main/java/chatbot/Command.java +++ b/src/main/java/chatbot/Command.java @@ -6,21 +6,23 @@ public class Command { private Storage storage; private String commandType; private String input; + private Ui ui; public Command(String commandType, String input) { this.storage = new Storage("./tasklist.txt"); this.commandType = commandType; this.input = input; + this.ui = new Ui(); } + /** + * Execute the command specified by the user + * + * @author Jeremy + * @since 2023-10-06 + */ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUnknownCommandException, ChatbotEmptyDescException { if (this.commandType.equals("list")) { - System.out.println("____________________________________________________________"); - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < tasks.size(); i++) { - //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); - System.out.println(" " + (i + 1) + "." + tasks.get(i)); - } - System.out.println("____________________________________________________________"); + ui.printList(tasks); } else if (this.commandType.equals("mark")) { String number = input.replace("mark ", "").trim(); int markTaskNo = Integer.parseInt(number); @@ -29,12 +31,8 @@ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUn } if (isUserInput) { storage.saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" [" + tasks.get(markTaskNo - 1).getStatusIcon() + "] " + tasks.get(markTaskNo - 1).getDescription()); - System.out.println("____________________________________________________________"); + ui.printMarkResult(tasks, markTaskNo); } - } else if (this.commandType.equals("unmark")) { String number = input.replace("unmark ", "").trim(); int unmarkedTaskNo = Integer.parseInt(number); @@ -43,10 +41,7 @@ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUn } if (isUserInput) { storage.saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println(" [" + tasks.get(unmarkedTaskNo - 1).getStatusIcon() + "] " + tasks.get(unmarkedTaskNo - 1).getDescription()); - System.out.println("____________________________________________________________"); + ui.printUnmarkResult(tasks, unmarkedTaskNo); } } else if (this.commandType.equals("todo")) { @@ -60,11 +55,7 @@ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUn if (isUserInput) { storage.saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); - System.out.println("____________________________________________________________"); + ui.printTodoResult(tasks, task); } } else if (this.commandType.equals("deadline")) { @@ -72,20 +63,20 @@ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUn if (msg.isEmpty()) { throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); } - String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate + if (byDate.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /by argument of a deadline cannot be empty."); + } String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description + if (desc.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); + } Task task = new Deadline(desc, byDate); tasks.add(task); - if (isUserInput) { storage.saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); - System.out.println("____________________________________________________________"); + ui.printDeadlineResult(tasks, task); } } else if (this.commandType.equals("event")) { @@ -95,20 +86,24 @@ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUn } String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); + if (dateRange.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The date range of a event cannot be empty."); + } String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); + if (fromDate.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /from argument cannot be empty."); + } String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); + if (toDate.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /to argument cannot be empty."); + } String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description Task task = new Event(desc, fromDate, toDate); tasks.add(task); - if (isUserInput) { storage.saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); - System.out.println("____________________________________________________________"); + ui.printEventResult(tasks, task); } } else if (this.commandType.equals("delete")) { @@ -124,11 +119,7 @@ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUn tasks.remove(indexToRemove); if (isUserInput) { storage.saveToFile(input); - System.out.println("____________________________________________________________"); - System.out.println(" Noted. I've removed this task: "); - System.out.println(" " + task); - System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); - System.out.println("____________________________________________________________"); + ui.printDeleteResult(tasks, task); } } else if (this.commandType.equals("find")) { String searchFor = input.replace("find", "").trim(); @@ -137,7 +128,6 @@ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUn } System.out.println("____________________________________________________________"); System.out.println(" Here are the matching tasks in your list:"); - for(Task task : tasks) { String desc = task.getDescription(); if(desc.contains(searchFor)) { diff --git a/src/main/java/chatbot/Ui.java b/src/main/java/chatbot/Ui.java index 88990837c..abe781f18 100644 --- a/src/main/java/chatbot/Ui.java +++ b/src/main/java/chatbot/Ui.java @@ -1,5 +1,7 @@ package chatbot; +import java.util.ArrayList; + public class Ui { /** * Show greeting message when app first launches. @@ -41,4 +43,60 @@ public void showError(String message, boolean addLines) { System.out.println("____________________________________________________________"); } } + + public void printList(ArrayList tasks) { + System.out.println("____________________________________________________________"); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); + System.out.println(" " + (i + 1) + "." + tasks.get(i)); + } + System.out.println("____________________________________________________________"); + } + + public void printMarkResult(ArrayList tasks, int markTaskNo) { + System.out.println("____________________________________________________________"); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" [" + tasks.get(markTaskNo - 1).getStatusIcon() + "] " + tasks.get(markTaskNo - 1).getDescription()); + System.out.println("____________________________________________________________"); + } + + public void printUnmarkResult(ArrayList tasks, int unmarkedTaskNo) { + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println(" [" + tasks.get(unmarkedTaskNo - 1).getStatusIcon() + "] " + tasks.get(unmarkedTaskNo - 1).getDescription()); + System.out.println("____________________________________________________________"); + } + + public void printTodoResult(ArrayList tasks, Task task) { + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + + public void printDeadlineResult(ArrayList tasks, Task task) { + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + + public void printEventResult(ArrayList tasks, Task task) { + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + + public void printDeleteResult(ArrayList tasks, Task task) { + System.out.println("____________________________________________________________"); + System.out.println(" Noted. I've removed this task: "); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } } diff --git a/tasklist.txt b/tasklist.txt index 51c79a822..483668dde 100644 --- a/tasklist.txt +++ b/tasklist.txt @@ -9,3 +9,9 @@ mark 1 unmark 1 mark 3 unmark 3 +mark 5 +delete 1 +todo borrow book +event /from 23oct /to 23oct go to hall +deadline return book /by Sunday +event project meeting /from Mon 2pm /to 4pm From 0838b6a95c6abe31249580a22efb3810e747b333 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Tue, 24 Oct 2023 16:27:09 +0800 Subject: [PATCH 21/24] added command package --- src/main/java/chatbot/Chatbot.java | 11 ++--------- src/main/java/chatbot/Parser.java | 2 +- src/main/java/chatbot/{ => command}/Command.java | 6 +++++- 3 files changed, 8 insertions(+), 11 deletions(-) rename src/main/java/chatbot/{ => command}/Command.java (97%) diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java index 838c47a97..62e72b1a8 100644 --- a/src/main/java/chatbot/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -1,15 +1,8 @@ package chatbot; -import chatbot.*; +import chatbot.command.Command; -import java.io.BufferedReader; -import java.io.FileReader; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.sql.Array; import java.util.ArrayList; import java.util.Scanner; @@ -59,7 +52,7 @@ public void run() throws IOException { this.ui.showError(e.getMessage(), false); } - this.ui.showByeMessage();; + this.ui.showByeMessage(); } public static void main(String[] args) throws Exception { diff --git a/src/main/java/chatbot/Parser.java b/src/main/java/chatbot/Parser.java index f8d679abf..fdb63c15b 100644 --- a/src/main/java/chatbot/Parser.java +++ b/src/main/java/chatbot/Parser.java @@ -1,6 +1,6 @@ package chatbot; -import java.util.ArrayList; +import chatbot.command.Command; public class Parser { /** diff --git a/src/main/java/chatbot/Command.java b/src/main/java/chatbot/command/Command.java similarity index 97% rename from src/main/java/chatbot/Command.java rename to src/main/java/chatbot/command/Command.java index dd04fcd97..b5fe078ca 100644 --- a/src/main/java/chatbot/Command.java +++ b/src/main/java/chatbot/command/Command.java @@ -1,4 +1,6 @@ -package chatbot; +package chatbot.command; + +import chatbot.*; import java.util.ArrayList; @@ -16,6 +18,8 @@ public Command(String commandType, String input) { /** * Execute the command specified by the user * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input * @author Jeremy * @since 2023-10-06 */ From 1e16cf357d863e55dce42bcda52bd7e504c36f1c Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Tue, 24 Oct 2023 16:42:11 +0800 Subject: [PATCH 22/24] Refactored if-else commands to individual classes --- src/main/java/chatbot/Parser.java | 18 +-- .../java/chatbot/command/AbstractCommand.java | 11 ++ src/main/java/chatbot/command/Command.java | 130 +----------------- .../java/chatbot/command/DeadlineCommand.java | 35 +++++ .../java/chatbot/command/DeleteCommand.java | 29 ++++ .../java/chatbot/command/EventCommand.java | 41 ++++++ .../java/chatbot/command/FindCommand.java | 28 ++++ .../java/chatbot/command/ListCommand.java | 14 ++ .../java/chatbot/command/MarkCommand.java | 22 +++ .../java/chatbot/command/TodoCommand.java | 27 ++++ .../java/chatbot/command/UnmarkCommand.java | 22 +++ tasklist.txt | 5 + 12 files changed, 249 insertions(+), 133 deletions(-) create mode 100644 src/main/java/chatbot/command/AbstractCommand.java create mode 100644 src/main/java/chatbot/command/DeadlineCommand.java create mode 100644 src/main/java/chatbot/command/DeleteCommand.java create mode 100644 src/main/java/chatbot/command/EventCommand.java create mode 100644 src/main/java/chatbot/command/FindCommand.java create mode 100644 src/main/java/chatbot/command/ListCommand.java create mode 100644 src/main/java/chatbot/command/MarkCommand.java create mode 100644 src/main/java/chatbot/command/TodoCommand.java create mode 100644 src/main/java/chatbot/command/UnmarkCommand.java diff --git a/src/main/java/chatbot/Parser.java b/src/main/java/chatbot/Parser.java index fdb63c15b..09e6cb51e 100644 --- a/src/main/java/chatbot/Parser.java +++ b/src/main/java/chatbot/Parser.java @@ -1,6 +1,6 @@ package chatbot; -import chatbot.command.Command; +import chatbot.command.*; public class Parser { /** @@ -11,21 +11,21 @@ public class Parser { */ public Command parseCommand(String input) throws ChatbotUnknownCommandException { if (input.equals("list")) { - return new Command("list", input); + return new ListCommand("list", input); } else if (input.startsWith("mark ")) { - return new Command("mark", input); + return new MarkCommand("mark", input); } else if (input.startsWith("unmark ")) { - return new Command("unmark", input); + return new UnmarkCommand("unmark", input); } else if (input.startsWith("todo")) { - return new Command("todo", input); + return new TodoCommand("todo", input); } else if (input.startsWith("deadline")) { - return new Command("deadline", input); + return new DeadlineCommand("deadline", input); } else if (input.startsWith("event")) { - return new Command("event", input); + return new EventCommand("event", input); } else if (input.startsWith("delete")) { - return new Command("delete", input); + return new DeleteCommand("delete", input); } else if (input.startsWith("find")) { - return new Command("find", input); + return new FindCommand("find", input); } else { // unknown command throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); diff --git a/src/main/java/chatbot/command/AbstractCommand.java b/src/main/java/chatbot/command/AbstractCommand.java new file mode 100644 index 000000000..83c89dbe0 --- /dev/null +++ b/src/main/java/chatbot/command/AbstractCommand.java @@ -0,0 +1,11 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.ChatbotUnknownCommandException; +import chatbot.Task; + +import java.util.ArrayList; + +public abstract class AbstractCommand { + public abstract void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException; +} diff --git a/src/main/java/chatbot/command/Command.java b/src/main/java/chatbot/command/Command.java index b5fe078ca..6191b456b 100644 --- a/src/main/java/chatbot/command/Command.java +++ b/src/main/java/chatbot/command/Command.java @@ -4,11 +4,11 @@ import java.util.ArrayList; -public class Command { - private Storage storage; - private String commandType; - private String input; - private Ui ui; +public class Command extends AbstractCommand { + protected Storage storage; + protected String commandType; + protected String input; + protected Ui ui; public Command(String commandType, String input) { this.storage = new Storage("./tasklist.txt"); this.commandType = commandType; @@ -23,126 +23,8 @@ public Command(String commandType, String input) { * @author Jeremy * @since 2023-10-06 */ - public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotUnknownCommandException, ChatbotEmptyDescException { + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { - if (this.commandType.equals("list")) { - ui.printList(tasks); - } else if (this.commandType.equals("mark")) { - String number = input.replace("mark ", "").trim(); - int markTaskNo = Integer.parseInt(number); - if (markTaskNo > 0) { - tasks.get(markTaskNo - 1).markAsDone(); - } - if (isUserInput) { - storage.saveToFile(input); - ui.printMarkResult(tasks, markTaskNo); - } - } else if (this.commandType.equals("unmark")) { - String number = input.replace("unmark ", "").trim(); - int unmarkedTaskNo = Integer.parseInt(number); - if (unmarkedTaskNo > 0) { - tasks.get(unmarkedTaskNo - 1).markAsUndone(); - } - if (isUserInput) { - storage.saveToFile(input); - ui.printUnmarkResult(tasks, unmarkedTaskNo); - } - - } else if (this.commandType.equals("todo")) { - String msg = input.replace("todo", "").trim(); - if (msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty."); - } - - Task task = new Todo(msg); - tasks.add(task); - - if (isUserInput) { - storage.saveToFile(input); - ui.printTodoResult(tasks, task); - } - - } else if (this.commandType.equals("deadline")) { - String msg = input.replace("deadline", "").trim(); - if (msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); - } - String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate - if (byDate.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /by argument of a deadline cannot be empty."); - } - String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description - if (desc.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); - } - - Task task = new Deadline(desc, byDate); - tasks.add(task); - if (isUserInput) { - storage.saveToFile(input); - ui.printDeadlineResult(tasks, task); - } - - } else if (this.commandType.equals("event")) { - String msg = input.replace("event", "").trim(); - if (msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty."); - } - - String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); - if (dateRange.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The date range of a event cannot be empty."); - } - String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); - if (fromDate.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /from argument cannot be empty."); - } - String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); - if (toDate.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /to argument cannot be empty."); - } - String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description - - Task task = new Event(desc, fromDate, toDate); - tasks.add(task); - if (isUserInput) { - storage.saveToFile(input); - ui.printEventResult(tasks, task); - } - - } else if (this.commandType.equals("delete")) { - String msg = input.replace("delete", "").trim(); - if (msg.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of delete cannot be empty."); - } - int indexToRemove = Integer.parseInt(msg); - if( indexToRemove > 0 ) { - indexToRemove -= 1; - } - Task task = tasks.get(indexToRemove); - tasks.remove(indexToRemove); - if (isUserInput) { - storage.saveToFile(input); - ui.printDeleteResult(tasks, task); - } - } else if (this.commandType.equals("find")) { - String searchFor = input.replace("find", "").trim(); - if (searchFor.isEmpty()) { - throw new ChatbotEmptyDescException(" ☹ OOPS!!! The find command needs a string to search for."); - } - System.out.println("____________________________________________________________"); - System.out.println(" Here are the matching tasks in your list:"); - for(Task task : tasks) { - String desc = task.getDescription(); - if(desc.contains(searchFor)) { - System.out.println(" " + task); - } - } - System.out.println("____________________________________________________________"); - } else { - // unknown command - throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); - } } } diff --git a/src/main/java/chatbot/command/DeadlineCommand.java b/src/main/java/chatbot/command/DeadlineCommand.java new file mode 100644 index 000000000..86d92bdea --- /dev/null +++ b/src/main/java/chatbot/command/DeadlineCommand.java @@ -0,0 +1,35 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Deadline; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class DeadlineCommand extends Command { + public DeadlineCommand(String commandType, String input) { + super(commandType, input); + } + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String msg = input.replace("deadline", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); + } + String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate + if (byDate.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /by argument of a deadline cannot be empty."); + } + String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description + if (desc.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); + } + + Task task = new Deadline(desc, byDate); + tasks.add(task); + if (isUserInput) { + storage.saveToFile(input); + ui.printDeadlineResult(tasks, task); + } + } +} diff --git a/src/main/java/chatbot/command/DeleteCommand.java b/src/main/java/chatbot/command/DeleteCommand.java new file mode 100644 index 000000000..07a386c4a --- /dev/null +++ b/src/main/java/chatbot/command/DeleteCommand.java @@ -0,0 +1,29 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class DeleteCommand extends Command { + public DeleteCommand(String commandType, String input) { + super(commandType, input); + } + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String msg = input.replace("delete", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of delete cannot be empty."); + } + int indexToRemove = Integer.parseInt(msg); + if( indexToRemove > 0 ) { + indexToRemove -= 1; + } + Task task = tasks.get(indexToRemove); + tasks.remove(indexToRemove); + if (isUserInput) { + storage.saveToFile(input); + ui.printDeleteResult(tasks, task); + } + } +} diff --git a/src/main/java/chatbot/command/EventCommand.java b/src/main/java/chatbot/command/EventCommand.java new file mode 100644 index 000000000..1ca708f16 --- /dev/null +++ b/src/main/java/chatbot/command/EventCommand.java @@ -0,0 +1,41 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Event; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class EventCommand extends Command { + public EventCommand(String commandType, String input) { + super(commandType, input); + } + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String msg = input.replace("event", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty."); + } + + String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); + if (dateRange.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The date range of a event cannot be empty."); + } + String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); + if (fromDate.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /from argument cannot be empty."); + } + String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); + if (toDate.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /to argument cannot be empty."); + } + String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description + + Task task = new Event(desc, fromDate, toDate); + tasks.add(task); + if (isUserInput) { + storage.saveToFile(input); + ui.printEventResult(tasks, task); + } + } +} diff --git a/src/main/java/chatbot/command/FindCommand.java b/src/main/java/chatbot/command/FindCommand.java new file mode 100644 index 000000000..ea33c3b93 --- /dev/null +++ b/src/main/java/chatbot/command/FindCommand.java @@ -0,0 +1,28 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class FindCommand extends Command { + public FindCommand(String commandType, String input) { + super(commandType, input); + } + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String searchFor = input.replace("find", "").trim(); + if (searchFor.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The find command needs a string to search for."); + } + System.out.println("____________________________________________________________"); + System.out.println(" Here are the matching tasks in your list:"); + for(Task task : tasks) { + String desc = task.getDescription(); + if(desc.contains(searchFor)) { + System.out.println(" " + task); + } + } + System.out.println("____________________________________________________________"); + } +} diff --git a/src/main/java/chatbot/command/ListCommand.java b/src/main/java/chatbot/command/ListCommand.java new file mode 100644 index 000000000..4e960b7b5 --- /dev/null +++ b/src/main/java/chatbot/command/ListCommand.java @@ -0,0 +1,14 @@ +package chatbot.command; + +import chatbot.Task; + +import java.util.ArrayList; + +public class ListCommand extends Command { + public ListCommand(String commandType, String input) { + super(commandType, input); + } + public void execute(ArrayList tasks, boolean isUserInput) { + this.ui.printList(tasks); + } +} diff --git a/src/main/java/chatbot/command/MarkCommand.java b/src/main/java/chatbot/command/MarkCommand.java new file mode 100644 index 000000000..4215b6499 --- /dev/null +++ b/src/main/java/chatbot/command/MarkCommand.java @@ -0,0 +1,22 @@ +package chatbot.command; + +import chatbot.Task; + +import java.util.ArrayList; + +public class MarkCommand extends Command { + public MarkCommand(String commandType, String input) { + super(commandType, input); + } + public void execute(ArrayList tasks, boolean isUserInput) { + String number = input.replace("mark ", "").trim(); + int markTaskNo = Integer.parseInt(number); + if (markTaskNo > 0) { + tasks.get(markTaskNo - 1).markAsDone(); + } + if (isUserInput) { + storage.saveToFile(input); + ui.printMarkResult(tasks, markTaskNo); + } + } +} diff --git a/src/main/java/chatbot/command/TodoCommand.java b/src/main/java/chatbot/command/TodoCommand.java new file mode 100644 index 000000000..a8ba26340 --- /dev/null +++ b/src/main/java/chatbot/command/TodoCommand.java @@ -0,0 +1,27 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class TodoCommand extends Command { + public TodoCommand(String commandType, String input) { + super(commandType, input); + } + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String msg = input.replace("todo", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty."); + } + + Task task = new Todo(msg); + tasks.add(task); + + if (isUserInput) { + storage.saveToFile(input); + ui.printTodoResult(tasks, task); + } + } +} diff --git a/src/main/java/chatbot/command/UnmarkCommand.java b/src/main/java/chatbot/command/UnmarkCommand.java new file mode 100644 index 000000000..3275d30a7 --- /dev/null +++ b/src/main/java/chatbot/command/UnmarkCommand.java @@ -0,0 +1,22 @@ +package chatbot.command; + +import chatbot.Task; + +import java.util.ArrayList; + +public class UnmarkCommand extends Command { + public UnmarkCommand(String commandType, String input) { + super(commandType, input); + } + public void execute(ArrayList tasks, boolean isUserInput) { + String number = input.replace("unmark ", "").trim(); + int unmarkedTaskNo = Integer.parseInt(number); + if (unmarkedTaskNo > 0) { + tasks.get(unmarkedTaskNo - 1).markAsUndone(); + } + if (isUserInput) { + storage.saveToFile(input); + ui.printUnmarkResult(tasks, unmarkedTaskNo); + } + } +} diff --git a/tasklist.txt b/tasklist.txt index 483668dde..4b75b2fb0 100644 --- a/tasklist.txt +++ b/tasklist.txt @@ -15,3 +15,8 @@ todo borrow book event /from 23oct /to 23oct go to hall deadline return book /by Sunday event project meeting /from Mon 2pm /to 4pm +mark 8 +unmark 8 +delete 8 +todo borrow book +deadline aaaa /by Sat From ba940d1c02a7ae938da51c2e4c42451bc8891de2 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Tue, 24 Oct 2023 16:47:57 +0800 Subject: [PATCH 23/24] fixed bug where program exists upon exception --- src/main/java/chatbot/Chatbot.java | 34 +++++++++++++++++------------- tasklist.txt | 1 + 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java index 62e72b1a8..504a1f510 100644 --- a/src/main/java/chatbot/Chatbot.java +++ b/src/main/java/chatbot/Chatbot.java @@ -27,31 +27,35 @@ public Chatbot() { * @since 2023-10-06 */ public void run() throws IOException { - this.ui.showGreetingMessage(); - try { - ArrayList lines = storage.parseFile(this.taskList.getTaskList()); - for(String line : lines) { + ArrayList lines = storage.parseFile(this.taskList.getTaskList()); + for(String line : lines) { + try { Command c = this.parser.parseCommand(line); c.execute(this.taskList.getTaskList(), false); + } catch(ChatbotUnknownCommandException | ChatbotEmptyDescException e) { + this.ui.showError(e.getMessage(), true); + continue; + } catch (Exception e) { + this.ui.showError(e.getMessage(), false); } + } - Scanner in = new Scanner(System.in); - String input = in.nextLine(); - while(!input.equals("bye")) { + Scanner in = new Scanner(System.in); + String input = ""; + while(!input.equals("bye")) { + try{ + input = in.nextLine(); Command c = this.parser.parseCommand(input); c.execute(this.taskList.getTaskList(), true); - input = in.nextLine(); + } catch(ChatbotUnknownCommandException | ChatbotEmptyDescException e) { + this.ui.showError(e.getMessage(), true); + continue; + } catch (Exception e) { + this.ui.showError(e.getMessage(), false); } - } catch(ChatbotUnknownCommandException e) { - this.ui.showError(e.getMessage(), true); - } catch(ChatbotEmptyDescException e) { - this.ui.showError(e.getMessage(), true); - } catch (Exception e) { - this.ui.showError(e.getMessage(), false); } - this.ui.showByeMessage(); } diff --git a/tasklist.txt b/tasklist.txt index 4b75b2fb0..d790807e5 100644 --- a/tasklist.txt +++ b/tasklist.txt @@ -20,3 +20,4 @@ unmark 8 delete 8 todo borrow book deadline aaaa /by Sat +deadline aaa2 /by From 888536f0bc99f3e9bda0075db3aec543a26853d4 Mon Sep 17 00:00:00 2001 From: imaginarys96 Date: Tue, 24 Oct 2023 16:58:56 +0800 Subject: [PATCH 24/24] added function comments --- src/main/java/chatbot/Parser.java | 1 + src/main/java/chatbot/Storage.java | 14 ++++ src/main/java/chatbot/TaskList.java | 14 +++- src/main/java/chatbot/Ui.java | 64 +++++++++++++++++-- .../java/chatbot/command/AbstractCommand.java | 8 +++ src/main/java/chatbot/command/Command.java | 2 +- .../java/chatbot/command/DeadlineCommand.java | 8 +++ .../java/chatbot/command/DeleteCommand.java | 8 +++ .../java/chatbot/command/EventCommand.java | 8 +++ .../java/chatbot/command/FindCommand.java | 8 +++ .../java/chatbot/command/ListCommand.java | 8 +++ .../java/chatbot/command/MarkCommand.java | 8 +++ .../java/chatbot/command/TodoCommand.java | 8 +++ .../java/chatbot/command/UnmarkCommand.java | 8 +++ 14 files changed, 157 insertions(+), 10 deletions(-) diff --git a/src/main/java/chatbot/Parser.java b/src/main/java/chatbot/Parser.java index 09e6cb51e..c329605c0 100644 --- a/src/main/java/chatbot/Parser.java +++ b/src/main/java/chatbot/Parser.java @@ -6,6 +6,7 @@ public class Parser { /** * Parse the command and returns a Command object * + * @param input The input to be parsed * @author Jeremy * @since 2023-10-06 */ diff --git a/src/main/java/chatbot/Storage.java b/src/main/java/chatbot/Storage.java index e5e041205..543adf029 100644 --- a/src/main/java/chatbot/Storage.java +++ b/src/main/java/chatbot/Storage.java @@ -15,6 +15,13 @@ public class Storage { public Storage(String filepath) { this.filepath = filepath; } + /** + * Parse the storage file and load the data + * + * @param tasks The current task list + * @author Jeremy + * @since 2023-10-06 + */ public ArrayList parseFile(ArrayList tasks) throws IOException { Path path = Paths.get(this.filepath); ArrayList lines = new ArrayList<>(); @@ -35,6 +42,13 @@ public ArrayList parseFile(ArrayList tasks) throws IOException { } return lines; } + /** + * Save input to storage file + * + * @param input The input to be saved + * @author Jeremy + * @since 2023-10-06 + */ public void saveToFile(String input) { String str = input + System.lineSeparator(); try { diff --git a/src/main/java/chatbot/TaskList.java b/src/main/java/chatbot/TaskList.java index 5759bf39f..3f3d2fb91 100644 --- a/src/main/java/chatbot/TaskList.java +++ b/src/main/java/chatbot/TaskList.java @@ -4,11 +4,21 @@ public class TaskList { private ArrayList tasks = new ArrayList(100); - + /** + * Get the list of tasks + * + * @author Jeremy + * @since 2023-10-06 + */ public ArrayList getTaskList() { return this.tasks; } - + /** + * Add a task to the list of tasks + * + * @author Jeremy + * @since 2023-10-06 + */ public void addTask(Task task) { this.tasks.add(task); } diff --git a/src/main/java/chatbot/Ui.java b/src/main/java/chatbot/Ui.java index abe781f18..11e4c2d23 100644 --- a/src/main/java/chatbot/Ui.java +++ b/src/main/java/chatbot/Ui.java @@ -31,6 +31,8 @@ public void showByeMessage() { /** * Show an error message, with additional formatting possible * + * @param message the message to be displayed + * @param addLines whether to wrap the message between to horizontal dashed-lines * @author Jeremy * @since 2023-10-06 */ @@ -43,7 +45,13 @@ public void showError(String message, boolean addLines) { System.out.println("____________________________________________________________"); } } - + /** + * Print the list of tasks + * + * @param tasks the list of tasks to be printed + * @author Jeremy + * @since 2023-10-06 + */ public void printList(ArrayList tasks) { System.out.println("____________________________________________________________"); System.out.println(" Here are the tasks in your list:"); @@ -53,21 +61,42 @@ public void printList(ArrayList tasks) { } System.out.println("____________________________________________________________"); } - + /** + * Print the result of the `mark` command + * + * @param tasks the current list of tasks + * @param markTaskNo the task number that was marked by the user + * @author Jeremy + * @since 2023-10-06 + */ public void printMarkResult(ArrayList tasks, int markTaskNo) { System.out.println("____________________________________________________________"); System.out.println(" Nice! I've marked this task as done:"); System.out.println(" [" + tasks.get(markTaskNo - 1).getStatusIcon() + "] " + tasks.get(markTaskNo - 1).getDescription()); System.out.println("____________________________________________________________"); } - + /** + * Print the result of the `unmark` command + * + * @param tasks the current list of tasks + * @param unmarkedTaskNo the task number that was unmarked by the user + * @author Jeremy + * @since 2023-10-06 + */ public void printUnmarkResult(ArrayList tasks, int unmarkedTaskNo) { System.out.println("____________________________________________________________"); System.out.println(" OK, I've marked this task as not done yet:"); System.out.println(" [" + tasks.get(unmarkedTaskNo - 1).getStatusIcon() + "] " + tasks.get(unmarkedTaskNo - 1).getDescription()); System.out.println("____________________________________________________________"); } - + /** + * Print the result of the `todo` command + * + * @param tasks the current list of tasks + * @param task the task that was added + * @author Jeremy + * @since 2023-10-06 + */ public void printTodoResult(ArrayList tasks, Task task) { System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); @@ -75,7 +104,14 @@ public void printTodoResult(ArrayList tasks, Task task) { System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); System.out.println("____________________________________________________________"); } - + /** + * Print the result of the `deadline` command + * + * @param tasks the current list of tasks + * @param task the task that was added + * @author Jeremy + * @since 2023-10-06 + */ public void printDeadlineResult(ArrayList tasks, Task task) { System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); @@ -83,7 +119,14 @@ public void printDeadlineResult(ArrayList tasks, Task task) { System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); System.out.println("____________________________________________________________"); } - + /** + * Print the result of the `event` command + * + * @param tasks the current list of tasks + * @param task the task that was added + * @author Jeremy + * @since 2023-10-06 + */ public void printEventResult(ArrayList tasks, Task task) { System.out.println("____________________________________________________________"); System.out.println(" Got it. I've added this task:"); @@ -91,7 +134,14 @@ public void printEventResult(ArrayList tasks, Task task) { System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); System.out.println("____________________________________________________________"); } - + /** + * Print the result of the `delete` command + * + * @param tasks the current list of tasks + * @param task the task that was added + * @author Jeremy + * @since 2023-10-06 + */ public void printDeleteResult(ArrayList tasks, Task task) { System.out.println("____________________________________________________________"); System.out.println(" Noted. I've removed this task: "); diff --git a/src/main/java/chatbot/command/AbstractCommand.java b/src/main/java/chatbot/command/AbstractCommand.java index 83c89dbe0..c38f5f3a3 100644 --- a/src/main/java/chatbot/command/AbstractCommand.java +++ b/src/main/java/chatbot/command/AbstractCommand.java @@ -7,5 +7,13 @@ import java.util.ArrayList; public abstract class AbstractCommand { + /** + * Abstract method to execute the command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ public abstract void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException; } diff --git a/src/main/java/chatbot/command/Command.java b/src/main/java/chatbot/command/Command.java index 6191b456b..38ae76157 100644 --- a/src/main/java/chatbot/command/Command.java +++ b/src/main/java/chatbot/command/Command.java @@ -24,7 +24,7 @@ public Command(String commandType, String input) { * @since 2023-10-06 */ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { - + return; } } diff --git a/src/main/java/chatbot/command/DeadlineCommand.java b/src/main/java/chatbot/command/DeadlineCommand.java index 86d92bdea..9969af911 100644 --- a/src/main/java/chatbot/command/DeadlineCommand.java +++ b/src/main/java/chatbot/command/DeadlineCommand.java @@ -11,6 +11,14 @@ public class DeadlineCommand extends Command { public DeadlineCommand(String commandType, String input) { super(commandType, input); } + /** + * Execute the `deadline` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { String msg = input.replace("deadline", "").trim(); if (msg.isEmpty()) { diff --git a/src/main/java/chatbot/command/DeleteCommand.java b/src/main/java/chatbot/command/DeleteCommand.java index 07a386c4a..5d965b9e5 100644 --- a/src/main/java/chatbot/command/DeleteCommand.java +++ b/src/main/java/chatbot/command/DeleteCommand.java @@ -10,6 +10,14 @@ public class DeleteCommand extends Command { public DeleteCommand(String commandType, String input) { super(commandType, input); } + /** + * Execute the `delete` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { String msg = input.replace("delete", "").trim(); if (msg.isEmpty()) { diff --git a/src/main/java/chatbot/command/EventCommand.java b/src/main/java/chatbot/command/EventCommand.java index 1ca708f16..18a14b170 100644 --- a/src/main/java/chatbot/command/EventCommand.java +++ b/src/main/java/chatbot/command/EventCommand.java @@ -11,6 +11,14 @@ public class EventCommand extends Command { public EventCommand(String commandType, String input) { super(commandType, input); } + /** + * Execute the `event` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { String msg = input.replace("event", "").trim(); if (msg.isEmpty()) { diff --git a/src/main/java/chatbot/command/FindCommand.java b/src/main/java/chatbot/command/FindCommand.java index ea33c3b93..ba0fad89d 100644 --- a/src/main/java/chatbot/command/FindCommand.java +++ b/src/main/java/chatbot/command/FindCommand.java @@ -10,6 +10,14 @@ public class FindCommand extends Command { public FindCommand(String commandType, String input) { super(commandType, input); } + /** + * Execute the `find` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { String searchFor = input.replace("find", "").trim(); if (searchFor.isEmpty()) { diff --git a/src/main/java/chatbot/command/ListCommand.java b/src/main/java/chatbot/command/ListCommand.java index 4e960b7b5..b77e8a9f9 100644 --- a/src/main/java/chatbot/command/ListCommand.java +++ b/src/main/java/chatbot/command/ListCommand.java @@ -8,6 +8,14 @@ public class ListCommand extends Command { public ListCommand(String commandType, String input) { super(commandType, input); } + /** + * Execute the `list` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ public void execute(ArrayList tasks, boolean isUserInput) { this.ui.printList(tasks); } diff --git a/src/main/java/chatbot/command/MarkCommand.java b/src/main/java/chatbot/command/MarkCommand.java index 4215b6499..ce9829ec7 100644 --- a/src/main/java/chatbot/command/MarkCommand.java +++ b/src/main/java/chatbot/command/MarkCommand.java @@ -8,6 +8,14 @@ public class MarkCommand extends Command { public MarkCommand(String commandType, String input) { super(commandType, input); } + /** + * Execute the `mark` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ public void execute(ArrayList tasks, boolean isUserInput) { String number = input.replace("mark ", "").trim(); int markTaskNo = Integer.parseInt(number); diff --git a/src/main/java/chatbot/command/TodoCommand.java b/src/main/java/chatbot/command/TodoCommand.java index a8ba26340..ca7ea92dc 100644 --- a/src/main/java/chatbot/command/TodoCommand.java +++ b/src/main/java/chatbot/command/TodoCommand.java @@ -10,6 +10,14 @@ public class TodoCommand extends Command { public TodoCommand(String commandType, String input) { super(commandType, input); } + /** + * Execute the `todo` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { String msg = input.replace("todo", "").trim(); if (msg.isEmpty()) { diff --git a/src/main/java/chatbot/command/UnmarkCommand.java b/src/main/java/chatbot/command/UnmarkCommand.java index 3275d30a7..4f4f39831 100644 --- a/src/main/java/chatbot/command/UnmarkCommand.java +++ b/src/main/java/chatbot/command/UnmarkCommand.java @@ -8,6 +8,14 @@ public class UnmarkCommand extends Command { public UnmarkCommand(String commandType, String input) { super(commandType, input); } + /** + * Execute the `unmark` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ public void execute(ArrayList tasks, boolean isUserInput) { String number = input.replace("unmark ", "").trim(); int unmarkedTaskNo = Integer.parseInt(number);