From 599e1de7ba812cbf0b0e7aa6204ac632d1cde7e8 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Wed, 6 Sep 2023 12:33:19 +0800 Subject: [PATCH 01/24] Add Level-0 --- src/main/java/Duke.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..07d6e530a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,17 @@ public class Duke { + public static String BOT_NAME = "Amy"; public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + //System.out.println("Hello from\n" + logo); + System.out.println("____________________________________________________________"); + System.out.println("Hello! I'm " + BOT_NAME); + System.out.println("What can I do for you?"); + System.out.println("____________________________________________________________"); + System.out.println("Bye. Hope to see you again soon!"); + System.out.println("____________________________________________________________"); } } From 98eeed0e23401bf536d991dc0306a8bc08ae07ab Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Wed, 6 Sep 2023 15:32:07 +0800 Subject: [PATCH 02/24] Add echo() method --- src/main/java/Duke.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 07d6e530a..52a56ff6b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,5 +1,19 @@ +import java.util.*; public class Duke { - public static String BOT_NAME = "Amy"; + public static String BOT_NAME = "Amy"; // a constant bot name + public static void echo() { // Level-1 Echo + Scanner scanner = new Scanner(System.in); + while (true) { + String userInput = scanner.nextLine(); // Read user input + System.out.println("____________________________________________________________"); + System.out.println(userInput); // Echo the user's input + if (userInput.equals("bye")) { + break; // Exit the loop if the user enters "bye" + } + System.out.println("____________________________________________________________"); + } + scanner.close(); + } public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -11,7 +25,9 @@ public static void main(String[] args) { System.out.println("Hello! I'm " + BOT_NAME); System.out.println("What can I do for you?"); System.out.println("____________________________________________________________"); + echo(); System.out.println("Bye. Hope to see you again soon!"); System.out.println("____________________________________________________________"); + } } From 4895c529675f64e3b6432311828da350e29dbb02 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Wed, 6 Sep 2023 19:48:40 +0800 Subject: [PATCH 03/24] Add add_List() and conditionals for Level-2 --- src/main/java/Duke.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 52a56ff6b..d20bfcf5b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,14 +1,23 @@ import java.util.*; public class Duke { public static String BOT_NAME = "Amy"; // a constant bot name - public static void echo() { // Level-1 Echo + public static String[] taskList = new String[100]; // contains tasks + public static int numberOfTasks = 0; // number of tasks + public static void add_List() { // Level-2 add,list Scanner scanner = new Scanner(System.in); while (true) { String userInput = scanner.nextLine(); // Read user input System.out.println("____________________________________________________________"); - System.out.println(userInput); // Echo the user's input if (userInput.equals("bye")) { break; // Exit the loop if the user enters "bye" + } else if (userInput.equals("list")) { + for (int i = 0; i < numberOfTasks; i++) { + System.out.println((i + 1) + ". " + taskList[i]); // list if the user enters "list' + } + } else { + System.out.println("added: " + userInput); // print add + taskList[numberOfTasks] = userInput; + numberOfTasks++; } System.out.println("____________________________________________________________"); } @@ -25,9 +34,8 @@ public static void main(String[] args) { System.out.println("Hello! I'm " + BOT_NAME); System.out.println("What can I do for you?"); System.out.println("____________________________________________________________"); - echo(); + add_List(); System.out.println("Bye. Hope to see you again soon!"); System.out.println("____________________________________________________________"); - } } From 345505e24337b83f875cdb0579731cac8d4874ca Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Thu, 7 Sep 2023 09:01:06 +0800 Subject: [PATCH 04/24] Add markTaskAsDone & unmarkTask Add Task.java --- src/main/java/Duke.java | 73 ++++++++++++++++++++++++++++++----------- src/main/java/Task.java | 26 +++++++++++++++ 2 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index d20bfcf5b..5ddf0c47f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,27 +1,38 @@ import java.util.*; public class Duke { public static String BOT_NAME = "Amy"; // a constant bot name - public static String[] taskList = new String[100]; // contains tasks + public static Task[] taskList = new Task[100]; // contains tasks public static int numberOfTasks = 0; // number of tasks - public static void add_List() { // Level-2 add,list - Scanner scanner = new Scanner(System.in); - while (true) { - String userInput = scanner.nextLine(); // Read user input - System.out.println("____________________________________________________________"); - if (userInput.equals("bye")) { - break; // Exit the loop if the user enters "bye" - } else if (userInput.equals("list")) { - for (int i = 0; i < numberOfTasks; i++) { - System.out.println((i + 1) + ". " + taskList[i]); // list if the user enters "list' - } - } else { - System.out.println("added: " + userInput); // print add - taskList[numberOfTasks] = userInput; - numberOfTasks++; - } - System.out.println("____________________________________________________________"); + public static void addToList(String task) { + taskList[numberOfTasks] = new Task(task); + numberOfTasks++; + System.out.println("added: " + task); + } + public static void listTasks() { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < numberOfTasks; i++) { + System.out.println((i + 1) + ". " + taskList[i]); // list if the user enters list + } + } + public static void markTaskAsDone(int taskIndex) { + if (taskIndex >= 1 && taskIndex <= numberOfTasks) { + Task task = taskList[taskIndex - 1]; + task.markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(" " + task); + } else { + System.out.println("Invalid task index. Please try again."); + } + } + public static void unmarkTask(int taskIndex) { + if (taskIndex >= 1 && taskIndex <= numberOfTasks) { + Task task = taskList[taskIndex - 1]; + task.markAsNotDone(); + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(" " + task); + } else { + System.out.println("Invalid task index. Please try again."); } - scanner.close(); } public static void main(String[] args) { String logo = " ____ _ \n" @@ -34,7 +45,29 @@ public static void main(String[] args) { System.out.println("Hello! I'm " + BOT_NAME); System.out.println("What can I do for you?"); System.out.println("____________________________________________________________"); - add_List(); + Scanner scanner = new Scanner(System.in); + while (true) { + String userInput = scanner.nextLine(); // Read user input + System.out.println("____________________________________________________________"); + if (userInput.equals("bye")) { + break; // Exit the loop if the user enters "bye" + } else if (userInput.equals("list")) { + listTasks(); // list tasks + } else if (userInput.startsWith("mark")) { + // Extract the task index from the user input + int taskIndex = Integer.parseInt(userInput.substring(5).trim()); + markTaskAsDone(taskIndex); + } else if (userInput.startsWith("unmark")) { + // Extract the task index from the user input + int taskIndex = Integer.parseInt(userInput.substring(7).trim()); + unmarkTask(taskIndex); + } else { + addToList(userInput); + } + System.out.println("____________________________________________________________"); + } + scanner.close(); + System.out.println("Bye. Hope to see you again soon!"); System.out.println("____________________________________________________________"); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..d081b7b69 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,26 @@ +import java.util.*; +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + public void markAsDone() { + isDone = true; + } + + public void markAsNotDone() { + isDone = false; + } + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + public String toString() { + return "[" + this.getStatusIcon() + "] " + description; + } + + //... +} + From e55ef274bb6516b7718ae56f2613259facc1f1fb Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Thu, 7 Sep 2023 09:33:29 +0800 Subject: [PATCH 05/24] Changed Coding Standards --- src/main/java/Duke.java | 5 ++++- src/main/java/Task.java | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5ddf0c47f..a77aa07cf 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,4 @@ -import java.util.*; +import java.util.Scanner; public class Duke { public static String BOT_NAME = "Amy"; // a constant bot name public static Task[] taskList = new Task[100]; // contains tasks @@ -41,10 +41,12 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; //System.out.println("Hello from\n" + logo); + System.out.println("____________________________________________________________"); System.out.println("Hello! I'm " + BOT_NAME); System.out.println("What can I do for you?"); System.out.println("____________________________________________________________"); + Scanner scanner = new Scanner(System.in); while (true) { String userInput = scanner.nextLine(); // Read user input @@ -66,6 +68,7 @@ public static void main(String[] args) { } System.out.println("____________________________________________________________"); } + scanner.close(); System.out.println("Bye. Hope to see you again soon!"); diff --git a/src/main/java/Task.java b/src/main/java/Task.java index d081b7b69..7d4ef08ad 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,4 +1,3 @@ -import java.util.*; public class Task { protected String description; protected boolean isDone; @@ -10,7 +9,6 @@ public Task(String description) { public void markAsDone() { isDone = true; } - public void markAsNotDone() { isDone = false; } @@ -20,7 +18,5 @@ public String getStatusIcon() { public String toString() { return "[" + this.getStatusIcon() + "] " + description; } - - //... } From 7b661902a860fb109123c17e09870014e69d1d3d Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Fri, 8 Sep 2023 15:31:45 +0800 Subject: [PATCH 06/24] Add Deadline, Todo, and Event classes --- src/main/java/Deadline.java | 15 +++++++++++++++ src/main/java/Duke.java | 29 ++++++++++++++++++++++++----- src/main/java/Event.java | 13 +++++++++++++ src/main/java/Todo.java | 10 ++++++++++ 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..9cb0cc769 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,15 @@ +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 + ")"; + } +} + diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a77aa07cf..c2034318d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,10 +3,12 @@ public class Duke { public static String BOT_NAME = "Amy"; // a constant bot name public static Task[] taskList = new Task[100]; // contains tasks public static int numberOfTasks = 0; // number of tasks - public static void addToList(String task) { - taskList[numberOfTasks] = new Task(task); + public static void addToList(Task task) { + taskList[numberOfTasks] = task; numberOfTasks++; - System.out.println("added: " + task); + System.out.println("Got it. I've added this task: "); + System.out.println(" " + task); + System.out.println("Now you have " + numberOfTasks + " tasks in the list"); } public static void listTasks() { System.out.println("Here are the tasks in your list:"); @@ -63,8 +65,25 @@ public static void main(String[] args) { // Extract the task index from the user input int taskIndex = Integer.parseInt(userInput.substring(7).trim()); unmarkTask(taskIndex); - } else { - addToList(userInput); + } else if (userInput.startsWith("todo")) { + String description = userInput.substring(5).trim(); + Todo todo = new Todo(description); + addToList(todo); + }else if (userInput.startsWith("deadline")) { + int inputIndex = userInput.indexOf(" /by "); + String description = userInput.substring("deadline ".length(), inputIndex); + String by = userInput.substring(inputIndex + " /by ".length()); + addToList(new Deadline(description, by)); + } else if (userInput.startsWith("event")) { + String[] parts = userInput.substring(5).split("/from"); + String description = parts[0].trim(); + String[] dateTimeParts = parts[1].split("/to"); + String from = dateTimeParts[0].trim(); + String to = dateTimeParts[1].trim(); + Event event = new Event(description, from, to); + addToList(event); + } else { + System.out.println("Invalid command. Please try again."); } System.out.println("____________________________________________________________"); } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..e54954104 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,13 @@ +public class Event extends Task { + protected String from; + protected String to; + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + } + @Override + public String toString() { + return "[E]" + super.toString() + " (from: " + from + ", to: " + to + ")"; + } +} diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..eabef3ab8 --- /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(); + } +} From 3411af4a7bc9c3cbe11b1ada747a1efe341f7787 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Tue, 12 Sep 2023 02:51:23 +0800 Subject: [PATCH 07/24] Modified Code Quality --- src/main/java/Duke.java | 73 ++++++++++++++++++++++++++-------------- src/main/java/Event.java | 2 ++ src/main/java/Task.java | 7 +++- 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index c2034318d..9663ae13c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,8 +1,38 @@ import java.util.Scanner; public class Duke { - public static String BOT_NAME = "Amy"; // a constant bot name - public static Task[] taskList = new Task[100]; // contains tasks - public static int numberOfTasks = 0; // number of tasks + public static final String BOT_NAME = "AMY"; + public static final String LINE = "____________________________________________________________"; + public static Task[] taskList = new Task[100]; + public static int numberOfTasks = 0; + + // Draws a line + public static void drawLine() { + System.out.println(LINE); + } + + // Writes a welcome message + public static void welcomeMessage() { + drawLine(); + String logo = " \n" + + " /\\ |\\ /| \\ / \n" + + " / \\ | \\/ | \\ / \n" + + " /----\\ | | | \n" + + " / \\ | | | \n"; + System.out.println("Hello from\n" + logo); + + drawLine(); + System.out.println("Hello! I'm " + BOT_NAME); + System.out.println("What can I do for you?"); + drawLine(); + } + + // Writes a bye message + public static void byMessage() { + System.out.println("Bye. Hope to see you again soon!"); + drawLine(); + } + + // Add a task to the list public static void addToList(Task task) { taskList[numberOfTasks] = task; numberOfTasks++; @@ -10,12 +40,16 @@ public static void addToList(Task task) { System.out.println(" " + task); System.out.println("Now you have " + numberOfTasks + " tasks in the list"); } + + // List all tasks if the user enters "list" public static void listTasks() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < numberOfTasks; i++) { - System.out.println((i + 1) + ". " + taskList[i]); // list if the user enters list + System.out.println((i + 1) + ". " + taskList[i]); } } + + // Mark task as done if the user enters "mark" public static void markTaskAsDone(int taskIndex) { if (taskIndex >= 1 && taskIndex <= numberOfTasks) { Task task = taskList[taskIndex - 1]; @@ -26,6 +60,8 @@ public static void markTaskAsDone(int taskIndex) { System.out.println("Invalid task index. Please try again."); } } + + // Unmark task as done if the user enters "unmark" public static void unmarkTask(int taskIndex) { if (taskIndex >= 1 && taskIndex <= numberOfTasks) { Task task = taskList[taskIndex - 1]; @@ -36,23 +72,14 @@ public static void unmarkTask(int taskIndex) { System.out.println("Invalid task index. Please try again."); } } - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - //System.out.println("Hello from\n" + logo); - - System.out.println("____________________________________________________________"); - System.out.println("Hello! I'm " + BOT_NAME); - System.out.println("What can I do for you?"); - System.out.println("____________________________________________________________"); + // Main method executes the Chat bot + public static void main(String[] args) { + welcomeMessage(); Scanner scanner = new Scanner(System.in); while (true) { String userInput = scanner.nextLine(); // Read user input - System.out.println("____________________________________________________________"); + System.out.println(LINE); if (userInput.equals("bye")) { break; // Exit the loop if the user enters "bye" } else if (userInput.equals("list")) { @@ -69,7 +96,7 @@ public static void main(String[] args) { String description = userInput.substring(5).trim(); Todo todo = new Todo(description); addToList(todo); - }else if (userInput.startsWith("deadline")) { + } else if (userInput.startsWith("deadline")) { int inputIndex = userInput.indexOf(" /by "); String description = userInput.substring("deadline ".length(), inputIndex); String by = userInput.substring(inputIndex + " /by ".length()); @@ -82,15 +109,11 @@ public static void main(String[] args) { String to = dateTimeParts[1].trim(); Event event = new Event(description, from, to); addToList(event); - } else { + } else { System.out.println("Invalid command. Please try again."); } - System.out.println("____________________________________________________________"); + drawLine(); } - - scanner.close(); - - System.out.println("Bye. Hope to see you again soon!"); - System.out.println("____________________________________________________________"); + byMessage(); } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index e54954104..ad95772db 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,11 +1,13 @@ public class Event extends Task { protected String from; protected String to; + public Event(String description, String from, String to) { super(description); this.from = from; this.to = to; } + @Override public String toString() { return "[E]" + super.toString() + " (from: " + from + ", to: " + to + ")"; diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 7d4ef08ad..d0da7dbc4 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -6,15 +6,20 @@ public Task(String description) { this.description = description; this.isDone = false; } + public void markAsDone() { isDone = true; } + public void markAsNotDone() { isDone = false; } + + // mark done task with X public String getStatusIcon() { - return (isDone ? "X" : " "); // mark done task with X + return (isDone ? "X" : " "); } + public String toString() { return "[" + this.getStatusIcon() + "] " + description; } From 77f0227a45f8a6cba5c770e6b778c27e6f6ac8f4 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Thu, 14 Sep 2023 19:41:11 +0800 Subject: [PATCH 08/24] Change Class and File Name to AMY --- src/main/java/{Duke.java => AMY.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/{Duke.java => AMY.java} (99%) diff --git a/src/main/java/Duke.java b/src/main/java/AMY.java similarity index 99% rename from src/main/java/Duke.java rename to src/main/java/AMY.java index 9663ae13c..500bea85b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/AMY.java @@ -1,5 +1,5 @@ import java.util.Scanner; -public class Duke { +public class AMY { public static final String BOT_NAME = "AMY"; public static final String LINE = "____________________________________________________________"; public static Task[] taskList = new Task[100]; From 748217ff19640878e561c98c91a79f44c70a6545 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Thu, 14 Sep 2023 19:49:33 +0800 Subject: [PATCH 09/24] Add manageInput to organize Main --- src/main/java/AMY.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/AMY.java b/src/main/java/AMY.java index 500bea85b..9a9fbc5e3 100644 --- a/src/main/java/AMY.java +++ b/src/main/java/AMY.java @@ -73,9 +73,7 @@ public static void unmarkTask(int taskIndex) { } } - // Main method executes the Chat bot - public static void main(String[] args) { - welcomeMessage(); + public static void manageInput() { Scanner scanner = new Scanner(System.in); while (true) { String userInput = scanner.nextLine(); // Read user input @@ -114,6 +112,12 @@ public static void main(String[] args) { } drawLine(); } + } + + // Main method executes the Chat bot + public static void main(String[] args) { + welcomeMessage(); + manageInput(); byMessage(); } } From f4b7317c0ac8f27b32dcd60979849a1a2b6ac863 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Thu, 14 Sep 2023 20:42:28 +0800 Subject: [PATCH 10/24] Create exceptions --- src/main/java/EmptyDeadlineException.java | 2 ++ src/main/java/EmptyEventException.java | 2 ++ src/main/java/EmptyMarkException.java | 2 ++ src/main/java/EmptyToDoException.java | 2 ++ src/main/java/EmptyUnmarkException.java | 2 ++ src/main/java/UnacceptableInput.java | 2 ++ 6 files changed, 12 insertions(+) create mode 100644 src/main/java/EmptyDeadlineException.java create mode 100644 src/main/java/EmptyEventException.java create mode 100644 src/main/java/EmptyMarkException.java create mode 100644 src/main/java/EmptyToDoException.java create mode 100644 src/main/java/EmptyUnmarkException.java create mode 100644 src/main/java/UnacceptableInput.java diff --git a/src/main/java/EmptyDeadlineException.java b/src/main/java/EmptyDeadlineException.java new file mode 100644 index 000000000..8d584c5f3 --- /dev/null +++ b/src/main/java/EmptyDeadlineException.java @@ -0,0 +1,2 @@ +public class EmptyDeadlineException extends Exception{ +} diff --git a/src/main/java/EmptyEventException.java b/src/main/java/EmptyEventException.java new file mode 100644 index 000000000..c442d3bca --- /dev/null +++ b/src/main/java/EmptyEventException.java @@ -0,0 +1,2 @@ +public class EmptyEventException extends Exception { +} diff --git a/src/main/java/EmptyMarkException.java b/src/main/java/EmptyMarkException.java new file mode 100644 index 000000000..fa860eca8 --- /dev/null +++ b/src/main/java/EmptyMarkException.java @@ -0,0 +1,2 @@ +public class EmptyMarkException extends Exception{ +} diff --git a/src/main/java/EmptyToDoException.java b/src/main/java/EmptyToDoException.java new file mode 100644 index 000000000..47180bedb --- /dev/null +++ b/src/main/java/EmptyToDoException.java @@ -0,0 +1,2 @@ +public class EmptyToDoException extends Exception { +} diff --git a/src/main/java/EmptyUnmarkException.java b/src/main/java/EmptyUnmarkException.java new file mode 100644 index 000000000..468e21b37 --- /dev/null +++ b/src/main/java/EmptyUnmarkException.java @@ -0,0 +1,2 @@ +public class EmptyUnmarkException extends Exception{ +} diff --git a/src/main/java/UnacceptableInput.java b/src/main/java/UnacceptableInput.java new file mode 100644 index 000000000..e149b6439 --- /dev/null +++ b/src/main/java/UnacceptableInput.java @@ -0,0 +1,2 @@ +public class UnacceptableInput extends Exception { +} From 9dfc94a98b505664a51aedf41a09de03c3dd8638 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Mon, 18 Sep 2023 14:43:16 +0800 Subject: [PATCH 11/24] Add Exception handlers --- src/main/java/AMY.java | 105 +++++++++++++++++++-------- src/main/java/EmptyInput.java | 2 + src/main/java/UnacceptableInput.java | 2 - 3 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 src/main/java/EmptyInput.java delete mode 100644 src/main/java/UnacceptableInput.java diff --git a/src/main/java/AMY.java b/src/main/java/AMY.java index 9a9fbc5e3..9d55d4908 100644 --- a/src/main/java/AMY.java +++ b/src/main/java/AMY.java @@ -4,6 +4,7 @@ public class AMY { public static final String LINE = "____________________________________________________________"; public static Task[] taskList = new Task[100]; public static int numberOfTasks = 0; + public static Scanner scanner = new Scanner(System.in); // Draws a line public static void drawLine() { @@ -73,42 +74,84 @@ public static void unmarkTask(int taskIndex) { } } + public static void manageException(String userInput) throws EmptyInput, EmptyToDoException, + EmptyMarkException, EmptyUnmarkException, EmptyDeadlineException, EmptyEventException { + + Scanner input = new Scanner(userInput); + String command; + if (!input.hasNext()) { + throw new EmptyInput(); + } else { + command = input.next(); + } + if (command.equals("todo") && !input.hasNext()) { + throw new EmptyToDoException(); + } + if (command.equals("mark") && !input.hasNext()) { + throw new EmptyMarkException(); + } + if (command.equals("unmark") && !input.hasNext()) { + throw new EmptyUnmarkException(); + } + if (command.equals("deadline") && !input.hasNext()) { + throw new EmptyDeadlineException(); + } + if (command.equals("event") && !input.hasNext()) { + throw new EmptyEventException(); + } + } + public static void manageInput() { Scanner scanner = new Scanner(System.in); while (true) { String userInput = scanner.nextLine(); // Read user input System.out.println(LINE); - if (userInput.equals("bye")) { - break; // Exit the loop if the user enters "bye" - } else if (userInput.equals("list")) { - listTasks(); // list tasks - } else if (userInput.startsWith("mark")) { - // Extract the task index from the user input - int taskIndex = Integer.parseInt(userInput.substring(5).trim()); - markTaskAsDone(taskIndex); - } else if (userInput.startsWith("unmark")) { - // Extract the task index from the user input - int taskIndex = Integer.parseInt(userInput.substring(7).trim()); - unmarkTask(taskIndex); - } else if (userInput.startsWith("todo")) { - String description = userInput.substring(5).trim(); - Todo todo = new Todo(description); - addToList(todo); - } else if (userInput.startsWith("deadline")) { - int inputIndex = userInput.indexOf(" /by "); - String description = userInput.substring("deadline ".length(), inputIndex); - String by = userInput.substring(inputIndex + " /by ".length()); - addToList(new Deadline(description, by)); - } else if (userInput.startsWith("event")) { - String[] parts = userInput.substring(5).split("/from"); - String description = parts[0].trim(); - String[] dateTimeParts = parts[1].split("/to"); - String from = dateTimeParts[0].trim(); - String to = dateTimeParts[1].trim(); - Event event = new Event(description, from, to); - addToList(event); - } else { - System.out.println("Invalid command. Please try again."); + try { + manageException(userInput); + if (userInput.equals("bye")) { + break; // Exit the loop if the user enters "bye" + } else if (userInput.equals("list")) { + listTasks(); // list tasks + } else if (userInput.startsWith("mark")) { + // Extract the task index from the user input + int taskIndex = Integer.parseInt(userInput.substring(5).trim()); + markTaskAsDone(taskIndex); + } else if (userInput.startsWith("unmark")) { + // Extract the task index from the user input + int taskIndex = Integer.parseInt(userInput.substring(7).trim()); + unmarkTask(taskIndex); + } else if (userInput.startsWith("todo")) { + String description = userInput.substring(5).trim(); + Todo todo = new Todo(description); + addToList(todo); + } else if (userInput.startsWith("deadline")) { + int inputIndex = userInput.indexOf(" /by "); + String description = userInput.substring("deadline ".length(), inputIndex); + String by = userInput.substring(inputIndex + " /by ".length()); + addToList(new Deadline(description, by)); + } else if (userInput.startsWith("event")) { + String[] parts = userInput.substring(5).split("/from"); + String description = parts[0].trim(); + String[] dateTimeParts = parts[1].split("/to"); + String from = dateTimeParts[0].trim(); + String to = dateTimeParts[1].trim(); + Event event = new Event(description, from, to); + addToList(event); + } else { + System.out.println("Invalid command. Please try again."); + } + } catch (EmptyInput exception) { + System.out.println("☹ OOPS!!! The description cannot be empty."); + } catch (EmptyToDoException exception) { + System.out.println("☹ OOPS!!! The description of a todo cannot be empty."); + } catch (EmptyMarkException exception) { + System.out.println("☹ OOPS!!! The description of a mark cannot be empty."); + } catch (EmptyUnmarkException exception) { + System.out.println("☹ OOPS!!! The description of an unmark cannot be empty."); + } catch (EmptyDeadlineException exception) { + System.out.println("☹ OOPS!!! The description of a deadline cannot be empty."); + } catch (EmptyEventException exception) { + System.out.println("☹ OOPS!!! The description of an event cannot be empty."); } drawLine(); } diff --git a/src/main/java/EmptyInput.java b/src/main/java/EmptyInput.java new file mode 100644 index 000000000..6e6253440 --- /dev/null +++ b/src/main/java/EmptyInput.java @@ -0,0 +1,2 @@ +public class EmptyInput extends Exception { +} diff --git a/src/main/java/UnacceptableInput.java b/src/main/java/UnacceptableInput.java deleted file mode 100644 index e149b6439..000000000 --- a/src/main/java/UnacceptableInput.java +++ /dev/null @@ -1,2 +0,0 @@ -public class UnacceptableInput extends Exception { -} From d3f85b1a171fffa2a606b559e461cdaddd99e3b1 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Mon, 18 Sep 2023 15:48:31 +0800 Subject: [PATCH 12/24] Create Packages --- src/main/java/{ => AMY}/AMY.java | 8 ++++++++ .../java/{ => AMY/Exceptions}/EmptyDeadlineException.java | 2 ++ .../java/{ => AMY/Exceptions}/EmptyEventException.java | 2 ++ src/main/java/{ => AMY/Exceptions}/EmptyInput.java | 2 ++ .../java/{ => AMY/Exceptions}/EmptyMarkException.java | 2 ++ .../java/{ => AMY/Exceptions}/EmptyToDoException.java | 2 ++ .../java/{ => AMY/Exceptions}/EmptyUnmarkException.java | 2 ++ src/main/java/{ => AMY/command}/Deadline.java | 2 ++ src/main/java/{ => AMY/command}/Event.java | 2 ++ src/main/java/{ => AMY/command}/Task.java | 2 ++ src/main/java/{ => AMY/command}/Todo.java | 2 ++ 11 files changed, 28 insertions(+) rename src/main/java/{ => AMY}/AMY.java (97%) rename src/main/java/{ => AMY/Exceptions}/EmptyDeadlineException.java (69%) rename src/main/java/{ => AMY/Exceptions}/EmptyEventException.java (68%) rename src/main/java/{ => AMY/Exceptions}/EmptyInput.java (64%) rename src/main/java/{ => AMY/Exceptions}/EmptyMarkException.java (67%) rename src/main/java/{ => AMY/Exceptions}/EmptyToDoException.java (68%) rename src/main/java/{ => AMY/Exceptions}/EmptyUnmarkException.java (68%) rename src/main/java/{ => AMY/command}/Deadline.java (92%) rename src/main/java/{ => AMY/command}/Event.java (94%) rename src/main/java/{ => AMY/command}/Task.java (96%) rename src/main/java/{ => AMY/command}/Todo.java (90%) diff --git a/src/main/java/AMY.java b/src/main/java/AMY/AMY.java similarity index 97% rename from src/main/java/AMY.java rename to src/main/java/AMY/AMY.java index 9d55d4908..44a460028 100644 --- a/src/main/java/AMY.java +++ b/src/main/java/AMY/AMY.java @@ -1,3 +1,11 @@ +package AMY; + +import AMY.Exceptions.*; +import AMY.command.Deadline; +import AMY.command.Event; +import AMY.command.Task; +import AMY.command.Todo; + import java.util.Scanner; public class AMY { public static final String BOT_NAME = "AMY"; diff --git a/src/main/java/EmptyDeadlineException.java b/src/main/java/AMY/Exceptions/EmptyDeadlineException.java similarity index 69% rename from src/main/java/EmptyDeadlineException.java rename to src/main/java/AMY/Exceptions/EmptyDeadlineException.java index 8d584c5f3..b8f8242b9 100644 --- a/src/main/java/EmptyDeadlineException.java +++ b/src/main/java/AMY/Exceptions/EmptyDeadlineException.java @@ -1,2 +1,4 @@ +package AMY.Exceptions; + public class EmptyDeadlineException extends Exception{ } diff --git a/src/main/java/EmptyEventException.java b/src/main/java/AMY/Exceptions/EmptyEventException.java similarity index 68% rename from src/main/java/EmptyEventException.java rename to src/main/java/AMY/Exceptions/EmptyEventException.java index c442d3bca..c5f52f758 100644 --- a/src/main/java/EmptyEventException.java +++ b/src/main/java/AMY/Exceptions/EmptyEventException.java @@ -1,2 +1,4 @@ +package AMY.Exceptions; + public class EmptyEventException extends Exception { } diff --git a/src/main/java/EmptyInput.java b/src/main/java/AMY/Exceptions/EmptyInput.java similarity index 64% rename from src/main/java/EmptyInput.java rename to src/main/java/AMY/Exceptions/EmptyInput.java index 6e6253440..e6e76f7ff 100644 --- a/src/main/java/EmptyInput.java +++ b/src/main/java/AMY/Exceptions/EmptyInput.java @@ -1,2 +1,4 @@ +package AMY.Exceptions; + public class EmptyInput extends Exception { } diff --git a/src/main/java/EmptyMarkException.java b/src/main/java/AMY/Exceptions/EmptyMarkException.java similarity index 67% rename from src/main/java/EmptyMarkException.java rename to src/main/java/AMY/Exceptions/EmptyMarkException.java index fa860eca8..e136a4d70 100644 --- a/src/main/java/EmptyMarkException.java +++ b/src/main/java/AMY/Exceptions/EmptyMarkException.java @@ -1,2 +1,4 @@ +package AMY.Exceptions; + public class EmptyMarkException extends Exception{ } diff --git a/src/main/java/EmptyToDoException.java b/src/main/java/AMY/Exceptions/EmptyToDoException.java similarity index 68% rename from src/main/java/EmptyToDoException.java rename to src/main/java/AMY/Exceptions/EmptyToDoException.java index 47180bedb..5b537b141 100644 --- a/src/main/java/EmptyToDoException.java +++ b/src/main/java/AMY/Exceptions/EmptyToDoException.java @@ -1,2 +1,4 @@ +package AMY.Exceptions; + public class EmptyToDoException extends Exception { } diff --git a/src/main/java/EmptyUnmarkException.java b/src/main/java/AMY/Exceptions/EmptyUnmarkException.java similarity index 68% rename from src/main/java/EmptyUnmarkException.java rename to src/main/java/AMY/Exceptions/EmptyUnmarkException.java index 468e21b37..ee03dd945 100644 --- a/src/main/java/EmptyUnmarkException.java +++ b/src/main/java/AMY/Exceptions/EmptyUnmarkException.java @@ -1,2 +1,4 @@ +package AMY.Exceptions; + public class EmptyUnmarkException extends Exception{ } diff --git a/src/main/java/Deadline.java b/src/main/java/AMY/command/Deadline.java similarity index 92% rename from src/main/java/Deadline.java rename to src/main/java/AMY/command/Deadline.java index 9cb0cc769..9a4798075 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/AMY/command/Deadline.java @@ -1,3 +1,5 @@ +package AMY.command; + public class Deadline extends Task { protected String by; diff --git a/src/main/java/Event.java b/src/main/java/AMY/command/Event.java similarity index 94% rename from src/main/java/Event.java rename to src/main/java/AMY/command/Event.java index ad95772db..7b59b7c2b 100644 --- a/src/main/java/Event.java +++ b/src/main/java/AMY/command/Event.java @@ -1,3 +1,5 @@ +package AMY.command; + public class Event extends Task { protected String from; protected String to; diff --git a/src/main/java/Task.java b/src/main/java/AMY/command/Task.java similarity index 96% rename from src/main/java/Task.java rename to src/main/java/AMY/command/Task.java index d0da7dbc4..da350dc57 100644 --- a/src/main/java/Task.java +++ b/src/main/java/AMY/command/Task.java @@ -1,3 +1,5 @@ +package AMY.command; + public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/Todo.java b/src/main/java/AMY/command/Todo.java similarity index 90% rename from src/main/java/Todo.java rename to src/main/java/AMY/command/Todo.java index eabef3ab8..5ba1dd07f 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/AMY/command/Todo.java @@ -1,3 +1,5 @@ +package AMY.command; + public class Todo extends Task { public Todo(String description) { super(description); From 4c13dd00ee5b647401607a6a232a9232976635fa Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Fri, 22 Sep 2023 04:17:07 +0800 Subject: [PATCH 13/24] Add Collection --- src/main/java/AMY/AMY.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/AMY/AMY.java b/src/main/java/AMY/AMY.java index 44a460028..0afadc9e0 100644 --- a/src/main/java/AMY/AMY.java +++ b/src/main/java/AMY/AMY.java @@ -7,11 +7,11 @@ import AMY.command.Todo; import java.util.Scanner; +import java.util.ArrayList; public class AMY { public static final String BOT_NAME = "AMY"; public static final String LINE = "____________________________________________________________"; - public static Task[] taskList = new Task[100]; - public static int numberOfTasks = 0; + public static ArrayList taskList = new ArrayList<>(); public static Scanner scanner = new Scanner(System.in); // Draws a line @@ -43,25 +43,25 @@ public static void byMessage() { // Add a task to the list public static void addToList(Task task) { - taskList[numberOfTasks] = task; - numberOfTasks++; + //taskList[numberOfTasks] = task; + taskList.add(task); System.out.println("Got it. I've added this task: "); System.out.println(" " + task); - System.out.println("Now you have " + numberOfTasks + " tasks in the list"); + System.out.println("Now you have " + taskList.size() + " tasks in the list"); } // List all tasks if the user enters "list" public static void listTasks() { System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < numberOfTasks; i++) { - System.out.println((i + 1) + ". " + taskList[i]); + for (int i = 0; i < taskList.size(); i++) { + System.out.println((i + 1) + ". " + taskList.get(i)); } } // Mark task as done if the user enters "mark" public static void markTaskAsDone(int taskIndex) { - if (taskIndex >= 1 && taskIndex <= numberOfTasks) { - Task task = taskList[taskIndex - 1]; + if (taskIndex >= 1 && taskIndex <= taskList.size()) { + Task task = taskList.get(taskIndex - 1); task.markAsDone(); System.out.println("Nice! I've marked this task as done:"); System.out.println(" " + task); @@ -72,8 +72,8 @@ public static void markTaskAsDone(int taskIndex) { // Unmark task as done if the user enters "unmark" public static void unmarkTask(int taskIndex) { - if (taskIndex >= 1 && taskIndex <= numberOfTasks) { - Task task = taskList[taskIndex - 1]; + if (taskIndex >= 1 && taskIndex <= taskList.size()) { + Task task = taskList.get(taskIndex - 1); task.markAsNotDone(); System.out.println("OK, I've marked this task as not done yet:"); System.out.println(" " + task); From ccb1ed633383d3cebb53582848c9ff9d19eec9c6 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Wed, 4 Oct 2023 09:32:16 +0800 Subject: [PATCH 14/24] Add delete --- src/main/java/AMY/AMY.java | 24 ++++++++++++++++++- .../AMY/Exceptions/EmptyDeleteException.java | 4 ++++ src/main/java/AMY/command/Delete.java | 16 +++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/main/java/AMY/Exceptions/EmptyDeleteException.java create mode 100644 src/main/java/AMY/command/Delete.java diff --git a/src/main/java/AMY/AMY.java b/src/main/java/AMY/AMY.java index 0afadc9e0..a9ea04118 100644 --- a/src/main/java/AMY/AMY.java +++ b/src/main/java/AMY/AMY.java @@ -5,6 +5,7 @@ import AMY.command.Event; import AMY.command.Task; import AMY.command.Todo; +import AMY.command.Delete; import java.util.Scanner; import java.util.ArrayList; @@ -82,8 +83,21 @@ public static void unmarkTask(int taskIndex) { } } + // Delete a task from the list + public static void deleteTask(int taskIndex) { + if (taskIndex >= 1 && taskIndex <= taskList.size()) { + Task removedTask = taskList.remove(taskIndex - 1); + System.out.println("Noted. I've removed this task:"); + System.out.println(" " + removedTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } else { + System.out.println("Invalid task index. Please try again."); + } + } + public static void manageException(String userInput) throws EmptyInput, EmptyToDoException, - EmptyMarkException, EmptyUnmarkException, EmptyDeadlineException, EmptyEventException { + EmptyMarkException, EmptyUnmarkException, EmptyDeadlineException, EmptyEventException, + EmptyDeleteException { Scanner input = new Scanner(userInput); String command; @@ -107,6 +121,9 @@ public static void manageException(String userInput) throws EmptyInput, EmptyToD if (command.equals("event") && !input.hasNext()) { throw new EmptyEventException(); } + if (command.equals("delete") && !input.hasNext()) { + throw new EmptyDeleteException(); + } } public static void manageInput() { @@ -145,6 +162,9 @@ public static void manageInput() { String to = dateTimeParts[1].trim(); Event event = new Event(description, from, to); addToList(event); + } else if (userInput.startsWith("delete")) { + int taskIndex = Integer.parseInt(userInput.substring(7).trim()); + deleteTask(taskIndex); } else { System.out.println("Invalid command. Please try again."); } @@ -160,6 +180,8 @@ public static void manageInput() { System.out.println("☹ OOPS!!! The description of a deadline cannot be empty."); } catch (EmptyEventException exception) { System.out.println("☹ OOPS!!! The description of an event cannot be empty."); + } catch (EmptyDeleteException exception) { + System.out.println("☹ OOPS!!! The description of an delete cannot be empty."); } drawLine(); } diff --git a/src/main/java/AMY/Exceptions/EmptyDeleteException.java b/src/main/java/AMY/Exceptions/EmptyDeleteException.java new file mode 100644 index 000000000..3eb599006 --- /dev/null +++ b/src/main/java/AMY/Exceptions/EmptyDeleteException.java @@ -0,0 +1,4 @@ +package AMY.Exceptions; + +public class EmptyDeleteException extends Exception{ +} diff --git a/src/main/java/AMY/command/Delete.java b/src/main/java/AMY/command/Delete.java new file mode 100644 index 000000000..939cbe59f --- /dev/null +++ b/src/main/java/AMY/command/Delete.java @@ -0,0 +1,16 @@ +package AMY.command; + +public class Delete extends Task { + + protected String by; + + public Delete(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + by + ")"; + } +} From 8855133032ad864d7b0b3c39fb770a96f81dd053 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Thu, 5 Oct 2023 05:59:54 +0800 Subject: [PATCH 15/24] Add Save & Load --- src/main/java/AMY/AMY.java | 63 +++++++++++++++++++++++++++-- src/main/java/AMY/command/Task.java | 52 ++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 4 deletions(-) diff --git a/src/main/java/AMY/AMY.java b/src/main/java/AMY/AMY.java index 44a460028..2b65dcb64 100644 --- a/src/main/java/AMY/AMY.java +++ b/src/main/java/AMY/AMY.java @@ -1,18 +1,27 @@ package AMY; -import AMY.Exceptions.*; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.util.Scanner; + +import AMY.Exceptions.EmptyDeadlineException; +import AMY.Exceptions.EmptyEventException; +import AMY.Exceptions.EmptyInput; +import AMY.Exceptions.EmptyMarkException; +import AMY.Exceptions.EmptyToDoException; +import AMY.Exceptions.EmptyUnmarkException; + import AMY.command.Deadline; import AMY.command.Event; import AMY.command.Task; import AMY.command.Todo; - -import java.util.Scanner; public class AMY { public static final String BOT_NAME = "AMY"; public static final String LINE = "____________________________________________________________"; public static Task[] taskList = new Task[100]; public static int numberOfTasks = 0; - public static Scanner scanner = new Scanner(System.in); // Draws a line public static void drawLine() { @@ -41,6 +50,50 @@ public static void byMessage() { drawLine(); } + // File path for storing tasks + private static final String FILE_PATH = "./data/AMY.txt"; + + // Save tasks to a file + public static void saveTasksToFile() { + try { + FileWriter writer = new FileWriter(FILE_PATH); + for (int i = 0; i < numberOfTasks; i++) { + Task task = taskList[i]; + writer.write(task.toFileString() + "\n"); + } + writer.close(); + } catch (IOException e) { + System.out.println("☹ OOPS!!! Error saving tasks to the file."); + } + } + + // Load tasks from a file + public static void loadTasksFromFile() { + try { + File file = new File(FILE_PATH); + if (!file.exists()) { + file.getParentFile().mkdirs(); // Create parent directories if they don't exist + file.createNewFile(); + } + Scanner scanner = new Scanner(file); + int numberOfTasks = 0; // Initialize numberOfTasks + while (scanner.hasNextLine()) { + String taskData = scanner.nextLine(); + Task task = Task.parseFromFileString(taskData); + if (task != null) { + AMY.addToList(task); + numberOfTasks++; // Increment numberOfTasks + } + } + scanner.close(); + AMY.numberOfTasks = numberOfTasks; // Update numberOfTasks in AMY class + } catch (FileNotFoundException e) { + System.out.println("Data file not found. Creating a new file."); + } catch (IOException e) { + System.out.println("☹ OOPS!!! Error loading tasks from the file."); + } + } + // Add a task to the list public static void addToList(Task task) { taskList[numberOfTasks] = task; @@ -168,7 +221,9 @@ public static void manageInput() { // Main method executes the Chat bot public static void main(String[] args) { welcomeMessage(); + loadTasksFromFile(); manageInput(); + saveTasksToFile(); byMessage(); } } diff --git a/src/main/java/AMY/command/Task.java b/src/main/java/AMY/command/Task.java index da350dc57..c6590c1b0 100644 --- a/src/main/java/AMY/command/Task.java +++ b/src/main/java/AMY/command/Task.java @@ -22,8 +22,60 @@ public String getStatusIcon() { return (isDone ? "X" : " "); } + public String getDescription() { + return description; + } + + // Convert the task to a file-readable string format + public String toFileString() { + String status = isDone ? "1" : "0"; + return String.format("%s | %s | %s", getTaskType(), status, description); + } + + // Parse a task from a file string + public static Task parseFromFileString(String fileString) { + String[] parts = fileString.split("\\|"); + String taskType = parts[0].trim(); + String status = parts[1].trim(); + String description = parts[2].trim(); + + Task task; + + if (taskType.equals("T")) { + task = new Todo(description); + } else if (taskType.equals("D")) { + // Extract the deadline information (e.g., "June 6th") + String by = parts[3].trim(); + task = new Deadline(description, by); + } else if (taskType.equals("E")) { + // Extract the event information (e.g., "Aug 6th 2-4pm") + String fromTo = parts[3].trim(); + String[] dateTimeParts = fromTo.split("\\|"); + String from = dateTimeParts[0].trim(); + String to = dateTimeParts[1].trim(); + task = new Event(description, from, to); + } else { + // Handle unknown task types (optional) + task = null; + } + + if (task != null) { + if (status.equals("1")) { + task.markAsDone(); + } + } + + return task; + } + + protected String getTaskType() { + return "T"; + } + + @Override public String toString() { return "[" + this.getStatusIcon() + "] " + description; } } + From d6c28543afe15e37447531623869b396f80fece3 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Thu, 5 Oct 2023 06:00:23 +0800 Subject: [PATCH 16/24] Create txt file --- data/AMY.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 data/AMY.txt diff --git a/data/AMY.txt b/data/AMY.txt new file mode 100644 index 000000000..e9ddefa2e --- /dev/null +++ b/data/AMY.txt @@ -0,0 +1,2 @@ +T | 0 | list +T | 0 | hw From 4dc78dbd78386fe09e983fd2cc27e96dcc05efa9 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Thu, 5 Oct 2023 06:57:34 +0800 Subject: [PATCH 17/24] Add jar --- src/main/java/META-INF/MANIFEST.MF | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..f2985ff8c --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: AMY.AMY + From 6aca928ebffec59066405ebb4592d553dfe30958 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Fri, 6 Oct 2023 17:40:28 +0800 Subject: [PATCH 18/24] Improve Coding-Standard --- src/main/java/AMY/AMY.java | 31 ++++++++++++--------------- src/main/java/AMY/command/Delete.java | 16 -------------- src/main/java/AMY/command/Task.java | 5 ----- 3 files changed, 14 insertions(+), 38 deletions(-) delete mode 100644 src/main/java/AMY/command/Delete.java diff --git a/src/main/java/AMY/AMY.java b/src/main/java/AMY/AMY.java index 91117b4a7..04c2840cd 100644 --- a/src/main/java/AMY/AMY.java +++ b/src/main/java/AMY/AMY.java @@ -17,24 +17,23 @@ import AMY.command.Event; import AMY.command.Task; import AMY.command.Todo; -import AMY.command.Delete; import java.util.Scanner; import java.util.ArrayList; public class AMY { public static final String BOT_NAME = "AMY"; + private static final String FILE_PATH = "./data/AMY.txt"; public static final String LINE = "____________________________________________________________"; public static ArrayList taskList = new ArrayList<>(); - public static int numberOfTasks; public static Scanner scanner = new Scanner(System.in); + public static int numberOfTasks; - - // Draws a line + // Draw a line to separate commands after given inputs public static void drawLine() { System.out.println(LINE); } - // Writes a welcome message + // Write a welcome message to welcome user when the program starts public static void welcomeMessage() { drawLine(); String logo = " \n" @@ -50,16 +49,13 @@ public static void welcomeMessage() { drawLine(); } - // Writes a bye message + // Write a bye message for the user when the program ends public static void byMessage() { System.out.println("Bye. Hope to see you again soon!"); drawLine(); } - // File path for storing tasks - private static final String FILE_PATH = "./data/AMY.txt"; - - // Save tasks to a file + // Save tasks to a file to load later when command is entered public static void saveTasksToFile() { try { FileWriter writer = new FileWriter(FILE_PATH); @@ -72,7 +68,7 @@ public static void saveTasksToFile() { } } - // Load tasks from a file + // Load tasks from a file to show saved tasks when the program starts public static void loadTasksFromFile() { try { File file = new File(FILE_PATH); @@ -99,16 +95,15 @@ public static void loadTasksFromFile() { } } - // Add a task to the list + // Add a task to the list when an todo command is entered public static void addToList(Task task) { - //taskList[numberOfTasks] = task; taskList.add(task); System.out.println("Got it. I've added this task: "); System.out.println(" " + task); System.out.println("Now you have " + taskList.size() + " tasks in the list"); } - // List all tasks if the user enters "list" + // List all tasks when "list" command is entered public static void listTasks() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskList.size(); i++) { @@ -116,7 +111,7 @@ public static void listTasks() { } } - // Mark task as done if the user enters "mark" + // Mark task as done when "mark" command is entered public static void markTaskAsDone(int taskIndex) { if (taskIndex >= 1 && taskIndex <= taskList.size()) { Task task = taskList.get(taskIndex - 1); @@ -128,7 +123,7 @@ public static void markTaskAsDone(int taskIndex) { } } - // Unmark task as done if the user enters "unmark" + // Unmark task as done when "unmark" command is entered public static void unmarkTask(int taskIndex) { if (taskIndex >= 1 && taskIndex <= taskList.size()) { Task task = taskList.get(taskIndex - 1); @@ -140,7 +135,7 @@ public static void unmarkTask(int taskIndex) { } } - // Delete a task from the list + // Delete a task from the list when "delete" command is entered public static void deleteTask(int taskIndex) { if (taskIndex >= 1 && taskIndex <= taskList.size()) { Task removedTask = taskList.remove(taskIndex - 1); @@ -152,6 +147,7 @@ public static void deleteTask(int taskIndex) { } } + // Throw exceptions when exceptions are encountered to avoid an error public static void manageException(String userInput) throws EmptyInput, EmptyToDoException, EmptyMarkException, EmptyUnmarkException, EmptyDeadlineException, EmptyEventException, EmptyDeleteException { @@ -183,6 +179,7 @@ public static void manageException(String userInput) throws EmptyInput, EmptyToD } } + // Manage input when input is given to follow order public static void manageInput() { Scanner scanner = new Scanner(System.in); while (true) { diff --git a/src/main/java/AMY/command/Delete.java b/src/main/java/AMY/command/Delete.java deleted file mode 100644 index 939cbe59f..000000000 --- a/src/main/java/AMY/command/Delete.java +++ /dev/null @@ -1,16 +0,0 @@ -package AMY.command; - -public class Delete extends Task { - - protected String by; - - public Delete(String description, String by) { - super(description); - this.by = by; - } - - @Override - public String toString() { - return "[D]" + super.toString() + " (by: " + by + ")"; - } -} diff --git a/src/main/java/AMY/command/Task.java b/src/main/java/AMY/command/Task.java index c6590c1b0..106e2648a 100644 --- a/src/main/java/AMY/command/Task.java +++ b/src/main/java/AMY/command/Task.java @@ -44,27 +44,22 @@ public static Task parseFromFileString(String fileString) { if (taskType.equals("T")) { task = new Todo(description); } else if (taskType.equals("D")) { - // Extract the deadline information (e.g., "June 6th") String by = parts[3].trim(); task = new Deadline(description, by); } else if (taskType.equals("E")) { - // Extract the event information (e.g., "Aug 6th 2-4pm") String fromTo = parts[3].trim(); String[] dateTimeParts = fromTo.split("\\|"); String from = dateTimeParts[0].trim(); String to = dateTimeParts[1].trim(); task = new Event(description, from, to); } else { - // Handle unknown task types (optional) task = null; } - if (task != null) { if (status.equals("1")) { task.markAsDone(); } } - return task; } From f2429fa3f900255656231dc212108f9473633332 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Tue, 10 Oct 2023 22:06:51 +0800 Subject: [PATCH 19/24] Add More-OOP --- data/AMY.txt | 2 - src/main/java/AMY/AMY.java | 250 +------------------------------- src/main/java/AMY/Parser.java | 118 +++++++++++++++ src/main/java/AMY/Storage.java | 58 ++++++++ src/main/java/AMY/TaskList.java | 61 ++++++++ src/main/java/AMY/Ui.java | 36 +++++ 6 files changed, 280 insertions(+), 245 deletions(-) create mode 100644 src/main/java/AMY/Parser.java create mode 100644 src/main/java/AMY/Storage.java create mode 100644 src/main/java/AMY/TaskList.java create mode 100644 src/main/java/AMY/Ui.java diff --git a/data/AMY.txt b/data/AMY.txt index 65286a423..596b740da 100644 --- a/data/AMY.txt +++ b/data/AMY.txt @@ -1,3 +1 @@ T | 0 | hello -T | 0 | bye -T | 0 | close diff --git a/src/main/java/AMY/AMY.java b/src/main/java/AMY/AMY.java index 04c2840cd..34d3f622b 100644 --- a/src/main/java/AMY/AMY.java +++ b/src/main/java/AMY/AMY.java @@ -1,252 +1,16 @@ package AMY; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.FileNotFoundException; - -import AMY.Exceptions.EmptyDeadlineException; -import AMY.Exceptions.EmptyDeleteException; -import AMY.Exceptions.EmptyEventException; -import AMY.Exceptions.EmptyInput; -import AMY.Exceptions.EmptyMarkException; -import AMY.Exceptions.EmptyToDoException; -import AMY.Exceptions.EmptyUnmarkException; - -import AMY.command.Deadline; -import AMY.command.Event; -import AMY.command.Task; -import AMY.command.Todo; - -import java.util.Scanner; -import java.util.ArrayList; public class AMY { - public static final String BOT_NAME = "AMY"; - private static final String FILE_PATH = "./data/AMY.txt"; - public static final String LINE = "____________________________________________________________"; - public static ArrayList taskList = new ArrayList<>(); - public static Scanner scanner = new Scanner(System.in); - public static int numberOfTasks; - - // Draw a line to separate commands after given inputs - public static void drawLine() { - System.out.println(LINE); - } - - // Write a welcome message to welcome user when the program starts - public static void welcomeMessage() { - drawLine(); - String logo = " \n" - + " /\\ |\\ /| \\ / \n" - + " / \\ | \\/ | \\ / \n" - + " /----\\ | | | \n" - + " / \\ | | | \n"; - System.out.println("Hello from\n" + logo); - - drawLine(); - System.out.println("Hello! I'm " + BOT_NAME); - System.out.println("What can I do for you?"); - drawLine(); - } - - // Write a bye message for the user when the program ends - public static void byMessage() { - System.out.println("Bye. Hope to see you again soon!"); - drawLine(); - } - - // Save tasks to a file to load later when command is entered - public static void saveTasksToFile() { - try { - FileWriter writer = new FileWriter(FILE_PATH); - for (Task task : taskList) { - writer.write(task.toFileString() + "\n"); - } - writer.close(); - } catch (IOException e) { - System.out.println("☹ OOPS!!! Error saving tasks to the file."); - } - } - - // Load tasks from a file to show saved tasks when the program starts - public static void loadTasksFromFile() { - try { - File file = new File(FILE_PATH); - if (!file.exists()) { - file.getParentFile().mkdirs(); // Create parent directories if they don't exist - file.createNewFile(); - } - Scanner scanner = new Scanner(file); - int numberOfTasks = 0; // Initialize numberOfTasks - while (scanner.hasNextLine()) { - String taskData = scanner.nextLine(); - Task task = Task.parseFromFileString(taskData); - if (task != null) { - AMY.addToList(task); - numberOfTasks++; // Increment numberOfTasks - } - } - scanner.close(); - AMY.numberOfTasks = numberOfTasks; // Update numberOfTasks in AMY class - } catch (FileNotFoundException e) { - System.out.println("Data file not found. Creating a new file."); - } catch (IOException e) { - System.out.println("☹ OOPS!!! Error loading tasks from the file."); - } - } - - // Add a task to the list when an todo command is entered - public static void addToList(Task task) { - taskList.add(task); - System.out.println("Got it. I've added this task: "); - System.out.println(" " + task); - System.out.println("Now you have " + taskList.size() + " tasks in the list"); - } - - // List all tasks when "list" command is entered - public static void listTasks() { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskList.size(); i++) { - System.out.println((i + 1) + ". " + taskList.get(i)); - } - } - - // Mark task as done when "mark" command is entered - public static void markTaskAsDone(int taskIndex) { - if (taskIndex >= 1 && taskIndex <= taskList.size()) { - Task task = taskList.get(taskIndex - 1); - task.markAsDone(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(" " + task); - } else { - System.out.println("Invalid task index. Please try again."); - } - } - - // Unmark task as done when "unmark" command is entered - public static void unmarkTask(int taskIndex) { - if (taskIndex >= 1 && taskIndex <= taskList.size()) { - Task task = taskList.get(taskIndex - 1); - task.markAsNotDone(); - System.out.println("OK, I've marked this task as not done yet:"); - System.out.println(" " + task); - } else { - System.out.println("Invalid task index. Please try again."); - } - } - - // Delete a task from the list when "delete" command is entered - public static void deleteTask(int taskIndex) { - if (taskIndex >= 1 && taskIndex <= taskList.size()) { - Task removedTask = taskList.remove(taskIndex - 1); - System.out.println("Noted. I've removed this task:"); - System.out.println(" " + removedTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - } else { - System.out.println("Invalid task index. Please try again."); - } - } - - // Throw exceptions when exceptions are encountered to avoid an error - public static void manageException(String userInput) throws EmptyInput, EmptyToDoException, - EmptyMarkException, EmptyUnmarkException, EmptyDeadlineException, EmptyEventException, - EmptyDeleteException { - - Scanner input = new Scanner(userInput); - String command; - if (!input.hasNext()) { - throw new EmptyInput(); - } else { - command = input.next(); - } - if (command.equals("todo") && !input.hasNext()) { - throw new EmptyToDoException(); - } - if (command.equals("mark") && !input.hasNext()) { - throw new EmptyMarkException(); - } - if (command.equals("unmark") && !input.hasNext()) { - throw new EmptyUnmarkException(); - } - if (command.equals("deadline") && !input.hasNext()) { - throw new EmptyDeadlineException(); - } - if (command.equals("event") && !input.hasNext()) { - throw new EmptyEventException(); - } - if (command.equals("delete") && !input.hasNext()) { - throw new EmptyDeleteException(); - } - } - // Manage input when input is given to follow order - public static void manageInput() { - Scanner scanner = new Scanner(System.in); - while (true) { - String userInput = scanner.nextLine(); // Read user input - System.out.println(LINE); - try { - manageException(userInput); - if (userInput.equals("bye")) { - break; // Exit the loop if the user enters "bye" - } else if (userInput.equals("list")) { - listTasks(); // list tasks - } else if (userInput.startsWith("mark")) { - // Extract the task index from the user input - int taskIndex = Integer.parseInt(userInput.substring(5).trim()); - markTaskAsDone(taskIndex); - } else if (userInput.startsWith("unmark")) { - // Extract the task index from the user input - int taskIndex = Integer.parseInt(userInput.substring(7).trim()); - unmarkTask(taskIndex); - } else if (userInput.startsWith("todo")) { - String description = userInput.substring(5).trim(); - Todo todo = new Todo(description); - addToList(todo); - } else if (userInput.startsWith("deadline")) { - int inputIndex = userInput.indexOf(" /by "); - String description = userInput.substring("deadline ".length(), inputIndex); - String by = userInput.substring(inputIndex + " /by ".length()); - addToList(new Deadline(description, by)); - } else if (userInput.startsWith("event")) { - String[] parts = userInput.substring(5).split("/from"); - String description = parts[0].trim(); - String[] dateTimeParts = parts[1].split("/to"); - String from = dateTimeParts[0].trim(); - String to = dateTimeParts[1].trim(); - Event event = new Event(description, from, to); - addToList(event); - } else if (userInput.startsWith("delete")) { - int taskIndex = Integer.parseInt(userInput.substring(7).trim()); - deleteTask(taskIndex); - } else { - System.out.println("Invalid command. Please try again."); - } - } catch (EmptyInput exception) { - System.out.println("☹ OOPS!!! The description cannot be empty."); - } catch (EmptyToDoException exception) { - System.out.println("☹ OOPS!!! The description of a todo cannot be empty."); - } catch (EmptyMarkException exception) { - System.out.println("☹ OOPS!!! The description of a mark cannot be empty."); - } catch (EmptyUnmarkException exception) { - System.out.println("☹ OOPS!!! The description of an unmark cannot be empty."); - } catch (EmptyDeadlineException exception) { - System.out.println("☹ OOPS!!! The description of a deadline cannot be empty."); - } catch (EmptyEventException exception) { - System.out.println("☹ OOPS!!! The description of an event cannot be empty."); - } catch (EmptyDeleteException exception) { - System.out.println("☹ OOPS!!! The description of an delete cannot be empty."); - } - drawLine(); - } + public static void run() { + Ui.welcomeMessage(); + Storage.loadTasksFromFile(); + Parser.manageInput(); + Storage.saveTasksToFile(); + Ui.byMessage(); } - // Main method executes the Chat bot public static void main(String[] args) { - welcomeMessage(); - loadTasksFromFile(); - manageInput(); - saveTasksToFile(); - byMessage(); + run(); } } diff --git a/src/main/java/AMY/Parser.java b/src/main/java/AMY/Parser.java new file mode 100644 index 000000000..230bd017c --- /dev/null +++ b/src/main/java/AMY/Parser.java @@ -0,0 +1,118 @@ +package AMY; + +import AMY.Exceptions.EmptyDeadlineException; +import AMY.Exceptions.EmptyDeleteException; +import AMY.Exceptions.EmptyEventException; +import AMY.Exceptions.EmptyInput; +import AMY.Exceptions.EmptyMarkException; +import AMY.Exceptions.EmptyToDoException; +import AMY.Exceptions.EmptyUnmarkException; + +import AMY.command.Deadline; +import AMY.command.Event; +import AMY.command.Todo; + +import java.util.Scanner; + +public class Parser { + + public static final String LINE = "____________________________________________________________"; + + public static void drawLine() { + System.out.println(LINE); + } + + // Throw exceptions when exceptions are encountered to avoid an error + public static void manageException(String userInput) throws EmptyInput, EmptyToDoException, + EmptyMarkException, EmptyUnmarkException, EmptyDeadlineException, EmptyEventException, + EmptyDeleteException { + + Scanner input = new Scanner(userInput); + String command; + if (!input.hasNext()) { + throw new EmptyInput(); + } else { + command = input.next(); + } + if (command.equals("todo") && !input.hasNext()) { + throw new EmptyToDoException(); + } + if (command.equals("mark") && !input.hasNext()) { + throw new EmptyMarkException(); + } + if (command.equals("unmark") && !input.hasNext()) { + throw new EmptyUnmarkException(); + } + if (command.equals("deadline") && !input.hasNext()) { + throw new EmptyDeadlineException(); + } + if (command.equals("event") && !input.hasNext()) { + throw new EmptyEventException(); + } + if (command.equals("delete") && !input.hasNext()) { + throw new EmptyDeleteException(); + } + } + + // Manage input when input is given to follow order + public static void manageInput() { + Scanner scanner = new Scanner(System.in); + while (true) { + String userInput = scanner.nextLine(); // Read user input + System.out.println(LINE); + try { + manageException(userInput); + if (userInput.equals("bye")) { + break; // Exit the loop if the user enters "bye" + } else if (userInput.equals("list")) { + TaskList.listTasks(); // list tasks + } else if (userInput.startsWith("mark")) { + // Extract the task index from the user input + int taskIndex = Integer.parseInt(userInput.substring(5).trim()); + TaskList.markTaskAsDone(taskIndex); + } else if (userInput.startsWith("unmark")) { + // Extract the task index from the user input + int taskIndex = Integer.parseInt(userInput.substring(7).trim()); + TaskList.unmarkTask(taskIndex); + } else if (userInput.startsWith("todo")) { + String description = userInput.substring(5).trim(); + Todo todo = new Todo(description); + TaskList.addToList(todo); + } else if (userInput.startsWith("deadline")) { + int inputIndex = userInput.indexOf(" /by "); + String description = userInput.substring("deadline ".length(), inputIndex); + String by = userInput.substring(inputIndex + " /by ".length()); + TaskList.addToList(new Deadline(description, by)); + } else if (userInput.startsWith("event")) { + String[] parts = userInput.substring(5).split("/from"); + String description = parts[0].trim(); + String[] dateTimeParts = parts[1].split("/to"); + String from = dateTimeParts[0].trim(); + String to = dateTimeParts[1].trim(); + Event event = new Event(description, from, to); + TaskList.addToList(event); + } else if (userInput.startsWith("delete")) { + int taskIndex = Integer.parseInt(userInput.substring(7).trim()); + TaskList.deleteTask(taskIndex); + } else { + System.out.println("Invalid command. Please try again."); + } + } catch (EmptyInput exception) { + System.out.println("☹ OOPS!!! The description cannot be empty."); + } catch (EmptyToDoException exception) { + System.out.println("☹ OOPS!!! The description of a todo cannot be empty."); + } catch (EmptyMarkException exception) { + System.out.println("☹ OOPS!!! The description of a mark cannot be empty."); + } catch (EmptyUnmarkException exception) { + System.out.println("☹ OOPS!!! The description of an unmark cannot be empty."); + } catch (EmptyDeadlineException exception) { + System.out.println("☹ OOPS!!! The description of a deadline cannot be empty."); + } catch (EmptyEventException exception) { + System.out.println("☹ OOPS!!! The description of an event cannot be empty."); + } catch (EmptyDeleteException exception) { + System.out.println("☹ OOPS!!! The description of an delete cannot be empty."); + } + drawLine(); + } + } +} diff --git a/src/main/java/AMY/Storage.java b/src/main/java/AMY/Storage.java new file mode 100644 index 000000000..2f4c9ef35 --- /dev/null +++ b/src/main/java/AMY/Storage.java @@ -0,0 +1,58 @@ +package AMY; + +import AMY.command.Task; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.FileNotFoundException; + +import java.util.Scanner; + +public class Storage { + public static String FILE_PATH = "./data/AMY.txt"; + public static int totalTasks; + public Storage(String filePath) { + FILE_PATH = filePath; + } + + // Load tasks from a file to show saved tasks when the program starts + public static void loadTasksFromFile() { + try { + File file = new File(FILE_PATH); + if (!file.exists()) { + file.getParentFile().mkdirs(); // Create parent directories if they don't exist + file.createNewFile(); + } + Scanner scanner = new Scanner(file); + int numberOfTasks = 0; // Initialize numberOfTasks + while (scanner.hasNextLine()) { + String taskData = scanner.nextLine(); + Task task = Task.parseFromFileString(taskData); + if (task != null) { + TaskList.addToList(task); + numberOfTasks++; // Increment numberOfTasks + } + } + scanner.close(); + totalTasks = numberOfTasks; // Update numberOfTasks in AMY class + } catch (FileNotFoundException e) { + System.out.println("Data file not found. Creating a new file."); + } catch (IOException e) { + System.out.println("☹ OOPS!!! Error loading tasks from the file."); + } + } + + // Save tasks to a file to load later when command is entered + public static void saveTasksToFile() { + try { + FileWriter writer = new FileWriter(FILE_PATH); + for (Task task : TaskList.taskList) { + writer.write(task.toFileString() + "\n"); + } + writer.close(); + } catch (IOException e) { + System.out.println("☹ OOPS!!! Error saving tasks to the file."); + } + } +} \ No newline at end of file diff --git a/src/main/java/AMY/TaskList.java b/src/main/java/AMY/TaskList.java new file mode 100644 index 000000000..e5f41edda --- /dev/null +++ b/src/main/java/AMY/TaskList.java @@ -0,0 +1,61 @@ +package AMY; + +import AMY.command.Task; + +import java.util.ArrayList; + +public class TaskList { + public static ArrayList taskList = new ArrayList<>(); + + // Add a task to the list when an todo command is entered + public static void addToList(Task task) { + taskList.add(task); + System.out.println("Got it. I've added this task: "); + System.out.println(" " + task); + System.out.println("Now you have " + taskList.size() + " tasks in the list"); + } + + // List all tasks when "list" command is entered + public static void listTasks() { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskList.size(); i++) { + System.out.println((i + 1) + ". " + taskList.get(i)); + } + } + + // Delete a task from the list when "delete" command is entered + public static void deleteTask(int taskIndex) { + if (taskIndex >= 1 && taskIndex <= taskList.size()) { + Task removedTask = taskList.remove(taskIndex - 1); + System.out.println("Noted. I've removed this task:"); + System.out.println(" " + removedTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } else { + System.out.println("Invalid task index. Please try again."); + } + } + + // Unmark task as done when "unmark" command is entered + public static void unmarkTask(int taskIndex) { + if (taskIndex >= 1 && taskIndex <= taskList.size()) { + Task task = taskList.get(taskIndex - 1); + task.markAsNotDone(); + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(" " + task); + } else { + System.out.println("Invalid task index. Please try again."); + } + } + + // Mark task as done when "mark" command is entered + public static void markTaskAsDone(int taskIndex) { + if (taskIndex >= 1 && taskIndex <= taskList.size()) { + Task task = taskList.get(taskIndex - 1); + task.markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(" " + task); + } else { + System.out.println("Invalid task index. Please try again."); + } + } +} diff --git a/src/main/java/AMY/Ui.java b/src/main/java/AMY/Ui.java new file mode 100644 index 000000000..0ee003670 --- /dev/null +++ b/src/main/java/AMY/Ui.java @@ -0,0 +1,36 @@ +package AMY; + +import java.util.Scanner; + +public class Ui { + public static final String BOT_NAME = "AMY"; + public static final String LINE = "____________________________________________________________"; + + // Draw a line to separate commands after given inputs + public static void drawLine() { + System.out.println(LINE); + } + + // Write a welcome message to welcome user when the program starts + public static void welcomeMessage() { + drawLine(); + String logo = " \n" + + " /\\ |\\ /| \\ / \n" + + " / \\ | \\/ | \\ / \n" + + " /----\\ | | | \n" + + " / \\ | | | \n"; + System.out.println("Hello from\n" + logo); + + drawLine(); + System.out.println("Hello! I'm " + BOT_NAME); + System.out.println("What can I do for you?"); + drawLine(); + } + + // Write a bye message for the user when the program ends + public static void byMessage() { + System.out.println("Bye. Hope to see you again soon!"); + drawLine(); + } + +} From e4a4b948338612be44952def7f12acf57bf7e674 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Tue, 10 Oct 2023 22:14:05 +0800 Subject: [PATCH 20/24] Add find --- data/AMY.txt | 2 ++ src/main/java/AMY/Parser.java | 5 ++++- src/main/java/AMY/TaskList.java | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/data/AMY.txt b/data/AMY.txt index 596b740da..d7c4db5f5 100644 --- a/data/AMY.txt +++ b/data/AMY.txt @@ -1 +1,3 @@ T | 0 | hello +T | 0 | bye +T | 0 | helloo diff --git a/src/main/java/AMY/Parser.java b/src/main/java/AMY/Parser.java index 230bd017c..ff0f8594b 100644 --- a/src/main/java/AMY/Parser.java +++ b/src/main/java/AMY/Parser.java @@ -94,7 +94,10 @@ public static void manageInput() { } else if (userInput.startsWith("delete")) { int taskIndex = Integer.parseInt(userInput.substring(7).trim()); TaskList.deleteTask(taskIndex); - } else { + } else if (userInput.startsWith("find")) { + String keyword = userInput.substring(5).trim(); + TaskList.findTasks(keyword); + }else { System.out.println("Invalid command. Please try again."); } } catch (EmptyInput exception) { diff --git a/src/main/java/AMY/TaskList.java b/src/main/java/AMY/TaskList.java index e5f41edda..40be5140d 100644 --- a/src/main/java/AMY/TaskList.java +++ b/src/main/java/AMY/TaskList.java @@ -58,4 +58,19 @@ public static void markTaskAsDone(int taskIndex) { System.out.println("Invalid task index. Please try again."); } } + + // Find tasks containing a specific keyword + public static void findTasks(String keyword) { + System.out.println("Here are the matching tasks in your list:"); + int count = 0; + for (Task task : taskList) { + if (task.getDescription().contains(keyword)) { + System.out.println((++count) + ". " + task); + } + } + + if (count == 0) { + System.out.println("No matching tasks found."); + } + } } From 9681ffcdd9011dfdb426b8153d5d36f8abd4edf4 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Wed, 11 Oct 2023 07:02:55 +0800 Subject: [PATCH 21/24] Add A-JavaDoc --- src/main/java/AMY/AMY.java | 3 +++ src/main/java/AMY/Parser.java | 16 ++++++++++++++-- src/main/java/AMY/Storage.java | 11 +++++++++-- src/main/java/AMY/TaskList.java | 28 ++++++++++++++++++++++------ src/main/java/AMY/Ui.java | 5 ++++- 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/main/java/AMY/AMY.java b/src/main/java/AMY/AMY.java index 34d3f622b..4950803da 100644 --- a/src/main/java/AMY/AMY.java +++ b/src/main/java/AMY/AMY.java @@ -2,6 +2,9 @@ public class AMY { + /** + * Runs the whole program when prompted + */ public static void run() { Ui.welcomeMessage(); Storage.loadTasksFromFile(); diff --git a/src/main/java/AMY/Parser.java b/src/main/java/AMY/Parser.java index ff0f8594b..fd01bc380 100644 --- a/src/main/java/AMY/Parser.java +++ b/src/main/java/AMY/Parser.java @@ -22,7 +22,17 @@ public static void drawLine() { System.out.println(LINE); } - // Throw exceptions when exceptions are encountered to avoid an error + /** + * Throw exceptions when exceptions are encountered to avoid an error + * @param userInput the user input + * @throws EmptyInput when the input is empty, the exception is thrown + * @throws EmptyToDoException when the todo command is empty, the exception is thrown + * @throws EmptyMarkException when the mark is command empty, the exception is thrown + * @throws EmptyUnmarkException when the unmark command is empty, the exception is thrown + * @throws EmptyDeadlineException when the deadline command is empty, the exception is thrown + * @throws EmptyEventException when the event command is empty, the exception is thrown + * @throws EmptyDeleteException when the delete command is empty, the exception is thrown + */ public static void manageException(String userInput) throws EmptyInput, EmptyToDoException, EmptyMarkException, EmptyUnmarkException, EmptyDeadlineException, EmptyEventException, EmptyDeleteException { @@ -54,7 +64,9 @@ public static void manageException(String userInput) throws EmptyInput, EmptyToD } } - // Manage input when input is given to follow order + /** + * Manage input when input is given to follow order + */ public static void manageInput() { Scanner scanner = new Scanner(System.in); while (true) { diff --git a/src/main/java/AMY/Storage.java b/src/main/java/AMY/Storage.java index 2f4c9ef35..7d811ba04 100644 --- a/src/main/java/AMY/Storage.java +++ b/src/main/java/AMY/Storage.java @@ -16,7 +16,11 @@ public Storage(String filePath) { FILE_PATH = filePath; } - // Load tasks from a file to show saved tasks when the program starts + /** + * Load tasks from a file to show saved tasks when the program starts + * Take no parameter + * Load from given file path + */ public static void loadTasksFromFile() { try { File file = new File(FILE_PATH); @@ -43,7 +47,10 @@ public static void loadTasksFromFile() { } } - // Save tasks to a file to load later when command is entered + /** + * Save tasks to a file to load later when command is entered + * Save to given file path + */ public static void saveTasksToFile() { try { FileWriter writer = new FileWriter(FILE_PATH); diff --git a/src/main/java/AMY/TaskList.java b/src/main/java/AMY/TaskList.java index 40be5140d..263f6b0c1 100644 --- a/src/main/java/AMY/TaskList.java +++ b/src/main/java/AMY/TaskList.java @@ -7,7 +7,9 @@ public class TaskList { public static ArrayList taskList = new ArrayList<>(); - // Add a task to the list when an todo command is entered + /** + * Add a task to the list when an todo command is entered + */ public static void addToList(Task task) { taskList.add(task); System.out.println("Got it. I've added this task: "); @@ -15,7 +17,9 @@ public static void addToList(Task task) { System.out.println("Now you have " + taskList.size() + " tasks in the list"); } - // List all tasks when "list" command is entered + /** + * List all tasks when "list" command is entered + */ public static void listTasks() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskList.size(); i++) { @@ -23,7 +27,10 @@ public static void listTasks() { } } - // Delete a task from the list when "delete" command is entered + /** + * Delete a task from the list when "delete" command is entered + * @param taskIndex the position of task to be deleted + */ public static void deleteTask(int taskIndex) { if (taskIndex >= 1 && taskIndex <= taskList.size()) { Task removedTask = taskList.remove(taskIndex - 1); @@ -35,7 +42,10 @@ public static void deleteTask(int taskIndex) { } } - // Unmark task as done when "unmark" command is entered + /** + * Unmark task as done when "unmark" command is entered + * @param taskIndex the position of task to be unmarked + */ public static void unmarkTask(int taskIndex) { if (taskIndex >= 1 && taskIndex <= taskList.size()) { Task task = taskList.get(taskIndex - 1); @@ -47,7 +57,10 @@ public static void unmarkTask(int taskIndex) { } } - // Mark task as done when "mark" command is entered + /** + * Mark task as done when "mark" command is entered + * @param taskIndex the position of task to be marked + */ public static void markTaskAsDone(int taskIndex) { if (taskIndex >= 1 && taskIndex <= taskList.size()) { Task task = taskList.get(taskIndex - 1); @@ -59,7 +72,10 @@ public static void markTaskAsDone(int taskIndex) { } } - // Find tasks containing a specific keyword + /** + * Find tasks containing a specific keyword + * @param keyword the keyword to locate + */ public static void findTasks(String keyword) { System.out.println("Here are the matching tasks in your list:"); int count = 0; diff --git a/src/main/java/AMY/Ui.java b/src/main/java/AMY/Ui.java index 0ee003670..1f561e98b 100644 --- a/src/main/java/AMY/Ui.java +++ b/src/main/java/AMY/Ui.java @@ -11,7 +11,10 @@ public static void drawLine() { System.out.println(LINE); } - // Write a welcome message to welcome user when the program starts + /** + * Write a welcome message to welcome user when the program starts + * Display bot name at the top + */ public static void welcomeMessage() { drawLine(); String logo = " \n" From f3f690f7bdbb7f320ee057f81b41a29fd4275444 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Wed, 11 Oct 2023 07:09:00 +0800 Subject: [PATCH 22/24] Add More Java-Doc --- src/main/java/AMY/AMY.java | 2 +- src/main/java/AMY/Storage.java | 2 +- src/main/java/AMY/Ui.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/AMY/AMY.java b/src/main/java/AMY/AMY.java index 4950803da..87079e7ed 100644 --- a/src/main/java/AMY/AMY.java +++ b/src/main/java/AMY/AMY.java @@ -3,7 +3,7 @@ public class AMY { /** - * Runs the whole program when prompted + * Runs the whole program when prompted by the main */ public static void run() { Ui.welcomeMessage(); diff --git a/src/main/java/AMY/Storage.java b/src/main/java/AMY/Storage.java index 7d811ba04..a1ce87fc1 100644 --- a/src/main/java/AMY/Storage.java +++ b/src/main/java/AMY/Storage.java @@ -49,7 +49,7 @@ public static void loadTasksFromFile() { /** * Save tasks to a file to load later when command is entered - * Save to given file path + * Save the tasks to the given file path */ public static void saveTasksToFile() { try { diff --git a/src/main/java/AMY/Ui.java b/src/main/java/AMY/Ui.java index 1f561e98b..d9a5739b4 100644 --- a/src/main/java/AMY/Ui.java +++ b/src/main/java/AMY/Ui.java @@ -12,8 +12,8 @@ public static void drawLine() { } /** + * Display the bot name at the top * Write a welcome message to welcome user when the program starts - * Display bot name at the top */ public static void welcomeMessage() { drawLine(); From 4f7e82d385edb4f9daa7f957fc0d6ae91a3396b8 Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Wed, 11 Oct 2023 07:14:11 +0800 Subject: [PATCH 23/24] Add EmptyFindException --- data/AMY.txt | 3 +-- src/main/java/AMY/Exceptions/EmptyFindException.java | 4 ++++ src/main/java/AMY/Exceptions/EmptyUnmarkException.java | 2 +- src/main/java/AMY/Parser.java | 8 +++++++- 4 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 src/main/java/AMY/Exceptions/EmptyFindException.java diff --git a/data/AMY.txt b/data/AMY.txt index d7c4db5f5..c49f32ad1 100644 --- a/data/AMY.txt +++ b/data/AMY.txt @@ -1,3 +1,2 @@ -T | 0 | hello T | 0 | bye -T | 0 | helloo +T | 1 | helloo diff --git a/src/main/java/AMY/Exceptions/EmptyFindException.java b/src/main/java/AMY/Exceptions/EmptyFindException.java new file mode 100644 index 000000000..f0df8c57b --- /dev/null +++ b/src/main/java/AMY/Exceptions/EmptyFindException.java @@ -0,0 +1,4 @@ +package AMY.Exceptions; + +public class EmptyFindException extends Exception { +} diff --git a/src/main/java/AMY/Exceptions/EmptyUnmarkException.java b/src/main/java/AMY/Exceptions/EmptyUnmarkException.java index ee03dd945..13bd5bf19 100644 --- a/src/main/java/AMY/Exceptions/EmptyUnmarkException.java +++ b/src/main/java/AMY/Exceptions/EmptyUnmarkException.java @@ -1,4 +1,4 @@ package AMY.Exceptions; -public class EmptyUnmarkException extends Exception{ +public class EmptyUnmarkException extends Exception { } diff --git a/src/main/java/AMY/Parser.java b/src/main/java/AMY/Parser.java index fd01bc380..826f52dee 100644 --- a/src/main/java/AMY/Parser.java +++ b/src/main/java/AMY/Parser.java @@ -7,6 +7,7 @@ import AMY.Exceptions.EmptyMarkException; import AMY.Exceptions.EmptyToDoException; import AMY.Exceptions.EmptyUnmarkException; +import AMY.Exceptions.EmptyFindException; import AMY.command.Deadline; import AMY.command.Event; @@ -35,7 +36,7 @@ public static void drawLine() { */ public static void manageException(String userInput) throws EmptyInput, EmptyToDoException, EmptyMarkException, EmptyUnmarkException, EmptyDeadlineException, EmptyEventException, - EmptyDeleteException { + EmptyDeleteException, EmptyFindException { Scanner input = new Scanner(userInput); String command; @@ -62,6 +63,9 @@ public static void manageException(String userInput) throws EmptyInput, EmptyToD if (command.equals("delete") && !input.hasNext()) { throw new EmptyDeleteException(); } + if (command.equals("find") && !input.hasNext()) { + throw new EmptyFindException(); + } } /** @@ -126,6 +130,8 @@ public static void manageInput() { System.out.println("☹ OOPS!!! The description of an event cannot be empty."); } catch (EmptyDeleteException exception) { System.out.println("☹ OOPS!!! The description of an delete cannot be empty."); + } catch (EmptyFindException exception){ + System.out.println("☹ OOPS!!! The description of an find cannot be empty."); } drawLine(); } From d8eecc7598ab133f36ad721b1ffa4e5651273eef Mon Sep 17 00:00:00 2001 From: Bayasgalan Kherlen Date: Wed, 11 Oct 2023 08:16:33 +0800 Subject: [PATCH 24/24] Add User-Guide --- docs/README.md | 180 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 167 insertions(+), 13 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..471e76b11 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,183 @@ -# User Guide +# AMY User Guide + +AMY is a text based Java app for managing your daily +tasks. Given below is the features list and your user guide. ## Features -### Feature-ABC +Here is your features list: + ++ **todo**, **event**, ++ **deadline**, **mark**, ++ **unmark**, **delete**, ++ **find**, **list**, **bye** + +Users can use these features to handle their tasks + +### ToDo + +Adds a new task to do in the task list. + +#### Usage + +**Format**: todo [task] + +**Examples** + ++ `todo finish my homework` + +**Expected Output**: -Description of the feature. +``` ++ Got it. I've added this task: ++ [T][ ] [finish my homework] +``` + +### Deadline + +Adds a new task with deadline to do in the task list. + +#### Usage -### Feature-XYZ +**Format**: deadline [task] /by [deadline] + +**Example** + ++ `deadline finish my homework /by Sunday` + +**Expected Output**: + +``` ++ Got it. I've added this task:
++ [D][ ] [finish my homework] (by: [Sunday]) +``` + +### Event + +Adds a new Event to the task list. + +#### Usage + +**Format**: event [task] /from [start] /to [end] + +**Example** + ++`finish homework /from 12:00 /to 18:00` + +**Expected Output**: + +``` ++ Got it. I've added this task: ++ [E][ ] [finish homework] (from: [12:00] to: [18:00]) +``` + +### List + +Lists all tasks in the task list. + +#### Usage + +**Format**: list + +**Expected Output**: + +``` +1. [D][ ] [task] (by: [Deadline]) +2. [T][ ] [task] +``` -Description of the feature. +### Mark Task + +Mark the task as done in the task list. + +#### Usage + +**Format**: mark [TASK INDEX] + +**Example** + ++ `mark 1` + +**Expected Output**: + +``` ++ Nice! I've marked this task as done: ++ [T][X] [finish homework] +``` +### Unmark + +Mark the task as not done in the task list. + +#### Usage + +**Format**: unmark [index] + +**Example** + ++ ```unmark 1``` + +**Expected Output**: + +``` ++ Nice! I've marked this task as not done. ++ [T][ ] [task] +``` + +### Delete Task + +Delete a task from the task list. + +#### Usage + +**Format**: delete [index] + +**Example** + ++ ```delete 2``` + +**Expected Output**: + +``` +Noted. I've removed this task: +[task] +Now you have [#] tasks in your list. +``` + +### Find Task + +Find tasks from the task list using either description or timing. + +#### Usage + +**Format**: find [keyword] + +**Example** + ++ ```find /description exam``` ++ ```find /time 21-04-2021 14:30``` + +**Expected Output**: + +``` +Here are the matching tasks in your list: ++ [index]. [task] ++ [index]. [task] +``` -## Usage +### bye -### `Keyword` - Describe action +Exit from the application. -Describe the action and its outcome. +#### Usage -Example of usage: +**Format**: bye -`keyword (optional arguments)` +**Example** -Expected outcome: ++ ```bye``` -Description of the outcome. +**Expected Output**: ``` -expected output ++ Bye. Hope to see you again soon! ```