From 7e3eb8d783f397e778c48ce414e878ba84e25082 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Wed, 31 Jan 2024 22:17:14 +0800 Subject: [PATCH 01/18] Level-0 --- README.md | 4 ++-- src/main/java/Ma.java | 17 +++++++++++++++++ text-ui-test/runtest.bat | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/main/java/Ma.java diff --git a/README.md b/README.md index 8715d4d91..fd6c6a930 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# Ma project template This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. @@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version. 1. If there are any further prompts, accept the defaults. 1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: +3. After that, locate the `src/main/java/Ma.java` file, right-click it, and choose `Run Ma.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from ____ _ diff --git a/src/main/java/Ma.java b/src/main/java/Ma.java new file mode 100644 index 000000000..9ab710210 --- /dev/null +++ b/src/main/java/Ma.java @@ -0,0 +1,17 @@ +public class Ma { + private static void printLine() { + System.out.println("____________________________________________________________"); + } + + public static void main(String[] args) { + String logo = "๐ŸŽ __ __ _ ๐ŸŽ\n" + + " | \\/ | / \\ \n" + + " | |\\/| | / _ \\ \n" + + " | | | |/ ___ \\ \n" + + "๐ŸŽ |_| |_/_/ \\_\\๐ŸŽ\n" + + "What can I help you with๏ผŸ"; + System.out.println("Hello from\n" + logo); + printLine(); + System.out.println("See you again! Goodbye!\n"); + } +} diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..cdc0b90b2 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Duke < input.txt > ACTUAL.TXT +java -classpath ..\bin Ma < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From b246b6187f03d1821df7752543fcb38811d5c10c Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Wed, 31 Jan 2024 22:18:08 +0800 Subject: [PATCH 02/18] Level-0, deleting Duke --- src/main/java/Duke.java | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/main/java/Duke.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334c..000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} From 4e8d3b28ce92fbc40090111ebfff11254f65016e Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Wed, 31 Jan 2024 22:45:55 +0800 Subject: [PATCH 03/18] level1 --- src/main/java/Ma.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/Ma.java b/src/main/java/Ma.java index 9ab710210..c88b47e38 100644 --- a/src/main/java/Ma.java +++ b/src/main/java/Ma.java @@ -1,17 +1,37 @@ +import java.util.Scanner; + public class Ma { private static void printLine() { System.out.println("____________________________________________________________"); } public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); // Create a Scanner object + String logo = "๐ŸŽ __ __ _ ๐ŸŽ\n" + " | \\/ | / \\ \n" + " | |\\/| | / _ \\ \n" + " | | | |/ ___ \\ \n" + "๐ŸŽ |_| |_/_/ \\_\\๐ŸŽ\n" + "What can I help you with๏ผŸ"; + System.out.println("Hello from\n" + logo); printLine(); - System.out.println("See you again! Goodbye!\n"); + System.out.println("Hello! I'm Ma\nWhat can I do for you?"); + printLine(); + + while (true) { + String userInput = scanner.nextLine(); + printLine(); + if ("bye".equalsIgnoreCase(userInput)) { + System.out.println("Bye. Hope to see you again soon!"); + printLine(); + break; + } else { + System.out.println(userInput); + printLine(); + } + } + scanner.close(); } } From 774fee3f38c2537c05bb00f07d398499f1e9937d Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Wed, 31 Jan 2024 23:45:08 +0800 Subject: [PATCH 04/18] level2 --- .gitignore | 1 + src/main/java/Ma.java | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2873e189e..c396857e1 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT + diff --git a/src/main/java/Ma.java b/src/main/java/Ma.java index c88b47e38..434dff400 100644 --- a/src/main/java/Ma.java +++ b/src/main/java/Ma.java @@ -1,4 +1,5 @@ import java.util.Scanner; +import java.util.ArrayList; public class Ma { private static void printLine() { @@ -8,6 +9,8 @@ private static void printLine() { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object + ArrayList list = new ArrayList<>(); + String logo = "๐ŸŽ __ __ _ ๐ŸŽ\n" + " | \\/ | / \\ \n" + " | |\\/| | / _ \\ \n" @@ -27,8 +30,16 @@ public static void main(String[] args) { System.out.println("Bye. Hope to see you again soon!"); printLine(); break; + } else if ("list".equalsIgnoreCase(userInput)) { + printLine(); + for (int i = 0; i < list.size(); i++) { + System.out.println(" " + (i + 1) + ". " + list.get(i)); + } + printLine(); } else { - System.out.println(userInput); + list.add(userInput); + printLine(); + System.out.println("added: " + userInput); printLine(); } } From 583d93724632eb4f69724027839d5476bbc93871 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Thu, 1 Feb 2024 00:51:23 +0800 Subject: [PATCH 05/18] Level3 --- src/main/java/Ma.java | 24 +++++++++++++++++++----- src/main/java/Task.java | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Ma.java b/src/main/java/Ma.java index 434dff400..15b9c72a4 100644 --- a/src/main/java/Ma.java +++ b/src/main/java/Ma.java @@ -9,7 +9,7 @@ private static void printLine() { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object - ArrayList list = new ArrayList<>(); + ArrayList list = new ArrayList<>(); String logo = "๐ŸŽ __ __ _ ๐ŸŽ\n" + " | \\/ | / \\ \n" @@ -31,13 +31,27 @@ public static void main(String[] args) { printLine(); break; } else if ("list".equalsIgnoreCase(userInput)) { - printLine(); + System.out.println("Here are the tasks in your list:"); for (int i = 0; i < list.size(); i++) { - System.out.println(" " + (i + 1) + ". " + list.get(i)); + System.out.println(" " + (i + 1) + ". " + list.get(i).toString()); } printLine(); - } else { - list.add(userInput); + } else if (userInput.startsWith("mark")) { + int index = Integer.parseInt(userInput.split(" ")[1]) - 1; // ่Žทๅ–ไปปๅŠก็ผ–ๅท + Task task = list.get(index); + task.markAsDone(); + System.out.println("Nice! I've marked this task as done:\n " + task); + printLine(); + }else if (userInput.startsWith("unmark")) { + int index = Integer.parseInt(userInput.split(" ")[1]) - 1; // ่Žทๅ–ไปปๅŠก็ผ–ๅท + Task task = list.get(index); + task.markAsUnDone(); + System.out.println("OK, I've marked this task as not done yet:\n " + task); + printLine(); + } + else { + Task newTask = new Task(userInput); + list.add(newTask); printLine(); System.out.println("added: " + userInput); printLine(); diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..ca28ea372 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,27 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + + public void markAsDone() { + this.isDone = true; + } + + public void markAsUnDone() { + this.isDone = false; + } + + @Override + public String toString() { + return "[" + this.getStatusIcon() + "] " + this.description; + } +} + From 776bbad4338dd01a3e76e97e1a4b89a518b45e55 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Mon, 12 Feb 2024 00:56:30 +0800 Subject: [PATCH 06/18] =?UTF-8?q?restructure=20the=20code=20into=20OOP=20s?= =?UTF-8?q?tyle=EF=BC=8Cseparate=20the=20functionality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/CommandParser.java | 16 +++++++++ src/main/java/DeadLine.java | 13 +++++++ src/main/java/Event.java | 15 ++++++++ src/main/java/Ma.java | 62 -------------------------------- src/main/java/Task.java | 9 ----- src/main/java/TaskFactory.java | 20 +++++++++++ src/main/java/TaskList.java | 26 ++++++++++++++ src/main/java/TaskManager.java | 23 ++++++++++++ src/main/java/Todo.java | 10 ++++++ 9 files changed, 123 insertions(+), 71 deletions(-) create mode 100644 src/main/java/CommandParser.java create mode 100644 src/main/java/DeadLine.java create mode 100644 src/main/java/Event.java delete mode 100644 src/main/java/Ma.java create mode 100644 src/main/java/TaskFactory.java create mode 100644 src/main/java/TaskList.java create mode 100644 src/main/java/TaskManager.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/CommandParser.java b/src/main/java/CommandParser.java new file mode 100644 index 000000000..c2a4788b3 --- /dev/null +++ b/src/main/java/CommandParser.java @@ -0,0 +1,16 @@ +public class CommandParser { + private final TaskList taskList; + + public CommandParser(TaskList taskList) { + this.taskList = taskList; + } + + public void parseCommand(String userInput) { + if (userInput.equalsIgnoreCase("list")) { + taskList.listTasks(); + } else { + taskList.addTask(userInput); + } + } +} + diff --git a/src/main/java/DeadLine.java b/src/main/java/DeadLine.java new file mode 100644 index 000000000..66356c29e --- /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: " + this.by + ")"; + } +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..618cb5f06 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,15 @@ +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: " + this.from + " to: " + this.to + ")"; + } +} diff --git a/src/main/java/Ma.java b/src/main/java/Ma.java deleted file mode 100644 index 15b9c72a4..000000000 --- a/src/main/java/Ma.java +++ /dev/null @@ -1,62 +0,0 @@ -import java.util.Scanner; -import java.util.ArrayList; - -public class Ma { - private static void printLine() { - System.out.println("____________________________________________________________"); - } - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); // Create a Scanner object - - ArrayList list = new ArrayList<>(); - - String logo = "๐ŸŽ __ __ _ ๐ŸŽ\n" - + " | \\/ | / \\ \n" - + " | |\\/| | / _ \\ \n" - + " | | | |/ ___ \\ \n" - + "๐ŸŽ |_| |_/_/ \\_\\๐ŸŽ\n" - + "What can I help you with๏ผŸ"; - - System.out.println("Hello from\n" + logo); - printLine(); - System.out.println("Hello! I'm Ma\nWhat can I do for you?"); - printLine(); - - while (true) { - String userInput = scanner.nextLine(); - printLine(); - if ("bye".equalsIgnoreCase(userInput)) { - System.out.println("Bye. Hope to see you again soon!"); - printLine(); - break; - } else if ("list".equalsIgnoreCase(userInput)) { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < list.size(); i++) { - System.out.println(" " + (i + 1) + ". " + list.get(i).toString()); - } - printLine(); - } else if (userInput.startsWith("mark")) { - int index = Integer.parseInt(userInput.split(" ")[1]) - 1; // ่Žทๅ–ไปปๅŠก็ผ–ๅท - Task task = list.get(index); - task.markAsDone(); - System.out.println("Nice! I've marked this task as done:\n " + task); - printLine(); - }else if (userInput.startsWith("unmark")) { - int index = Integer.parseInt(userInput.split(" ")[1]) - 1; // ่Žทๅ–ไปปๅŠก็ผ–ๅท - Task task = list.get(index); - task.markAsUnDone(); - System.out.println("OK, I've marked this task as not done yet:\n " + task); - printLine(); - } - else { - Task newTask = new Task(userInput); - list.add(newTask); - printLine(); - System.out.println("added: " + userInput); - printLine(); - } - } - scanner.close(); - } -} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index ca28ea372..acc824bb7 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -11,17 +11,8 @@ public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } - public void markAsDone() { - this.isDone = true; - } - public void markAsUnDone() { - this.isDone = false; - } - - @Override public String toString() { return "[" + this.getStatusIcon() + "] " + this.description; } } - diff --git a/src/main/java/TaskFactory.java b/src/main/java/TaskFactory.java new file mode 100644 index 000000000..f5e6354f5 --- /dev/null +++ b/src/main/java/TaskFactory.java @@ -0,0 +1,20 @@ +public class TaskFactory { + public static Task createTask(String userInput) { + String[] parts = userInput.split(" ", 2); + String type = parts[0]; + Task task = null; + + if (type.equals("todo")) { + task = new Todo(parts[1]); + } else if (type.startsWith("deadline")) { + String[] details = parts[1].split(" /by ", 2); + task = new DeadLine(details[0], details[1]); + } else if (type.startsWith("event")) { + String[] details = parts[1].split(" /from ", 2); + String[] times = details[1].split(" /to ", 2); + task = new Event(details[0], times[0], times[1]); + } + + return task; + } +} diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 000000000..74807ace6 --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,26 @@ +import java.util.ArrayList; +public class TaskList { + private final ArrayList tasks; + + public TaskList() { + this.tasks = new ArrayList<>(); + } + + public void addTask(String userInput) { + // Similar to addTask method in TaskManager, but refactored for this class + Task task = TaskFactory.createTask(userInput); + if (task != null) { + tasks.add(task); + System.out.println("Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + } + } + + public void listTasks() { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + "." + tasks.get(i)); + } + } +} diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java new file mode 100644 index 000000000..5d0d9a6e0 --- /dev/null +++ b/src/main/java/TaskManager.java @@ -0,0 +1,23 @@ +import java.util.Scanner; + +public class TaskManager { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + TaskList taskList = new TaskList(); + CommandParser commandParser = new CommandParser(taskList); + + System.out.println("Hello! I'm TaskManager Jamarcus \nWhat can I do for you?"); + + while (true) { + String userInput = scanner.nextLine().trim(); + if (userInput.equalsIgnoreCase("bye")) { + System.out.println("Bye. Hope to see you again soon!"); + break; + } else { + commandParser.parseCommand(userInput); + } + } + + scanner.close(); + } +} 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 4ff88d1672b19056a18a4511386b40d22b95ee57 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Thu, 15 Feb 2024 09:58:01 +0800 Subject: [PATCH 07/18] checked coding standard and quality --- src/main/java/CommandParser.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/CommandParser.java b/src/main/java/CommandParser.java index c2a4788b3..f6c95c8db 100644 --- a/src/main/java/CommandParser.java +++ b/src/main/java/CommandParser.java @@ -1,6 +1,7 @@ public class CommandParser { private final TaskList taskList; + public CommandParser(TaskList taskList) { this.taskList = taskList; } From a59c96e2062e3ee0e1567cd59a76b788ef8aa709 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Fri, 16 Feb 2024 22:13:14 +0800 Subject: [PATCH 08/18] add function to handle exception --- src/main/java/CommandParser.java | 8 ++++++-- src/main/java/HandleException.java | 6 ++++++ src/main/java/TaskFactory.java | 30 +++++++++++++++++++++++++----- src/main/java/TaskList.java | 2 +- src/main/java/TaskManager.java | 23 +++++++++++++++++------ 5 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 src/main/java/HandleException.java diff --git a/src/main/java/CommandParser.java b/src/main/java/CommandParser.java index f6c95c8db..dbbdf49bc 100644 --- a/src/main/java/CommandParser.java +++ b/src/main/java/CommandParser.java @@ -6,9 +6,13 @@ public CommandParser(TaskList taskList) { this.taskList = taskList; } - public void parseCommand(String userInput) { - if (userInput.equalsIgnoreCase("list")) { + public void parseCommand(String userInput) throws HandleException { + if (userInput.trim().isEmpty()) { + throw new HandleException("The input cannot be empty!"); + } else if (userInput.equalsIgnoreCase("list")) { taskList.listTasks(); + } else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") && !userInput.startsWith("event")) { + throw new HandleException("OOPS!!! I'm sorry, but I don't know what that means :-("); } else { taskList.addTask(userInput); } diff --git a/src/main/java/HandleException.java b/src/main/java/HandleException.java new file mode 100644 index 000000000..45e3ea951 --- /dev/null +++ b/src/main/java/HandleException.java @@ -0,0 +1,6 @@ +public class HandleException extends Exception { + public HandleException(String message) { + super(message); + } +} + diff --git a/src/main/java/TaskFactory.java b/src/main/java/TaskFactory.java index f5e6354f5..c6f5a0ee8 100644 --- a/src/main/java/TaskFactory.java +++ b/src/main/java/TaskFactory.java @@ -1,20 +1,40 @@ public class TaskFactory { - public static Task createTask(String userInput) { + public static Task createTask(String userInput) throws HandleException { String[] parts = userInput.split(" ", 2); String type = parts[0]; Task task = null; - if (type.equals("todo")) { + if (parts.length < 2 || parts[1].isEmpty()) { + throw new HandleException("OOPS!!! The description of a task cannot be empty."); + } + + switch (type) { + case "todo": task = new Todo(parts[1]); - } else if (type.startsWith("deadline")) { + break; + case "deadline": String[] details = parts[1].split(" /by ", 2); + if (details.length < 2 || details[1].isEmpty()) { + throw new HandleException("OOPS!!! The date of a deadline cannot be empty."); + } task = new DeadLine(details[0], details[1]); - } else if (type.startsWith("event")) { - String[] details = parts[1].split(" /from ", 2); + break; + case "event": + details = parts[1].split(" /from ", 2); + if (details.length < 2 || details[1].isEmpty()) { + throw new HandleException("OOPS!!! The start time of an event cannot be empty."); + } String[] times = details[1].split(" /to ", 2); + if (times.length < 2 || times[1].isEmpty()) { + throw new HandleException("OOPS!!! The end time of an event cannot be empty."); + } task = new Event(details[0], times[0], times[1]); + break; + default: + throw new HandleException("OOPS!!! I'm sorry, but I don't know what that means :-("); } return task; } } + diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index 74807ace6..e21323f11 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -6,7 +6,7 @@ public TaskList() { this.tasks = new ArrayList<>(); } - public void addTask(String userInput) { + public void addTask(String userInput) throws HandleException { // Similar to addTask method in TaskManager, but refactored for this class Task task = TaskFactory.createTask(userInput); if (task != null) { diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 5d0d9a6e0..d73ca3a7d 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -9,15 +9,26 @@ public static void main(String[] args) { System.out.println("Hello! I'm TaskManager Jamarcus \nWhat can I do for you?"); while (true) { - String userInput = scanner.nextLine().trim(); - if (userInput.equalsIgnoreCase("bye")) { - System.out.println("Bye. Hope to see you again soon!"); - break; - } else { - commandParser.parseCommand(userInput); + try { + String userInput = scanner.nextLine().trim(); + if (userInput.equalsIgnoreCase("bye")) { + System.out.println("Bye. Hope to see you again soon!"); + break; + } else { + commandParser.parseCommand(userInput); + } + } catch (HandleException e) { + // Print out the custom error message from the DukeException + System.out.println(e.getMessage()); + } catch (Exception e) { + // This catches any other exceptions that are not DukeExceptions + System.out.println("An unexpected error occurred: " + e.getMessage()); + // Optionally, for debugging purposes, you might want to print the stack trace + // e.printStackTrace(); } } scanner.close(); } } + From 78c3ec69253a4bfc042f357fa31a81216ebb3e3f Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Fri, 16 Feb 2024 22:26:15 +0800 Subject: [PATCH 09/18] Modifed exception handlers. --- src/main/java/TaskManager.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index d73ca3a7d..972526914 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -8,6 +8,7 @@ public static void main(String[] args) { System.out.println("Hello! I'm TaskManager Jamarcus \nWhat can I do for you?"); + while (true) { try { String userInput = scanner.nextLine().trim(); From bf630f3b06eae657816631a170279987836023e2 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Fri, 16 Feb 2024 22:35:09 +0800 Subject: [PATCH 10/18] Cleaned some unnecessary comments --- src/main/java/TaskManager.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 972526914..9de19462b 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -19,13 +19,9 @@ public static void main(String[] args) { commandParser.parseCommand(userInput); } } catch (HandleException e) { - // Print out the custom error message from the DukeException System.out.println(e.getMessage()); } catch (Exception e) { - // This catches any other exceptions that are not DukeExceptions System.out.println("An unexpected error occurred: " + e.getMessage()); - // Optionally, for debugging purposes, you might want to print the stack trace - // e.printStackTrace(); } } From 6f480edf3f6ebbbe4d4a92f0aafe65a855d93ba0 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Fri, 23 Feb 2024 12:51:47 +0800 Subject: [PATCH 11/18] Implement Level-6: Add support for deleting tasks --- src/main/java/CommandParser.java | 13 +++++++++++++ src/main/java/TaskList.java | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/CommandParser.java b/src/main/java/CommandParser.java index dbbdf49bc..459ca28a2 100644 --- a/src/main/java/CommandParser.java +++ b/src/main/java/CommandParser.java @@ -11,6 +11,18 @@ public void parseCommand(String userInput) throws HandleException { throw new HandleException("The input cannot be empty!"); } else if (userInput.equalsIgnoreCase("list")) { taskList.listTasks(); + } else if (userInput.startsWith("delete")) { + String[] parts = userInput.split(" ", 2); + if (parts.length < 2 || parts[1].isEmpty()) { + throw new HandleException("OOPS!!! The task number to delete cannot be empty."); + } + int taskIndex; + try { + taskIndex = Integer.parseInt(parts[1]); + } catch (NumberFormatException e) { + throw new HandleException("OOPS!!! The task number is invalid."); + } + taskList.deleteTask(taskIndex); } else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") && !userInput.startsWith("event")) { throw new HandleException("OOPS!!! I'm sorry, but I don't know what that means :-("); } else { @@ -19,3 +31,4 @@ public void parseCommand(String userInput) throws HandleException { } } + diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index e21323f11..85ead580f 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -17,6 +17,16 @@ public void addTask(String userInput) throws HandleException { } } + public void deleteTask(int taskIndex) throws HandleException { + if (taskIndex < 1 || taskIndex > tasks.size()) { + throw new HandleException("OOPS!!! The task number is invalid."); + } + Task removedTask = tasks.remove(taskIndex - 1); + System.out.println("Noted. I've removed this task:"); + System.out.println(" " + removedTask); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + } + public void listTasks() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < tasks.size(); i++) { From f575da97ff70500fbc951754d32d3cdc5b5627f6 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Fri, 23 Feb 2024 17:09:23 +0800 Subject: [PATCH 12/18] Implement Level-7 --- data/duke.txt | 2 + src/main/java/DeadLine.java | 3 ++ src/main/java/Event.java | 3 ++ src/main/java/Task.java | 3 ++ src/main/java/TaskList.java | 76 ++++++++++++++++++++++++++++++++++++- src/main/java/Todo.java | 4 ++ 6 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 data/duke.txt diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 000000000..f9d50bb61 --- /dev/null +++ b/data/duke.txt @@ -0,0 +1,2 @@ +T | 0 | Read a book +T | 0 | Sleep an hour diff --git a/src/main/java/DeadLine.java b/src/main/java/DeadLine.java index 66356c29e..4119d76c9 100644 --- a/src/main/java/DeadLine.java +++ b/src/main/java/DeadLine.java @@ -1,6 +1,9 @@ public class DeadLine extends Task { protected String by; + public String toFileFormat() { + return String.format("D | %d | %s | %s", isDone ? 1 : 0, description, by); + } public DeadLine(String description, String by) { super(description); this.by = by; diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 618cb5f06..c64053991 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -2,6 +2,9 @@ public class Event extends Task { protected String from; protected String to; + public String toFileFormat() { + return String.format("E | %d | %s | %s to %s", isDone ? 1 : 0, description, from, to); + } public Event(String description, String from, String to) { super(description); this.from = from; diff --git a/src/main/java/Task.java b/src/main/java/Task.java index acc824bb7..24a1c4d70 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -2,6 +2,9 @@ public class Task { protected String description; protected boolean isDone; + public String toFileFormat() { + return String.format("T | %d | %s", isDone ? 1 : 0, description); + } public Task(String description) { this.description = description; this.isDone = false; diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index e21323f11..661a364ac 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,22 +1,95 @@ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; +import java.util.Scanner; + public class TaskList { private final ArrayList tasks; + private static final String FILE_PATH = "./data/duke.txt"; public TaskList() { this.tasks = new ArrayList<>(); + loadTasks(); // Ensure tasks are loaded when an instance is created + } + + private void loadTasks() { + File file = new File(FILE_PATH); + if (!file.exists()) { + return; // Skip loading if file does not exist + } + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + Task task = parseLineToTask(line); + if (task != null) { + tasks.add(task); + } + } + } catch (IOException e) { // Catching IOException which covers FileNotFoundException + System.out.println("Unable to read tasks from file: " + e.getMessage()); + } + } + + private Task parseLineToTask(String line) { + String[] parts = line.split(" \\| "); + String type = parts[0]; + boolean isDone = parts[1].equals("1"); + String description = parts[2]; + Task task; + + switch (type) { + case "T": + task = new Todo(description); + break; + case "D": + String by = parts.length > 3 ? parts[3] : ""; + task = new DeadLine(description, by); + break; + case "E": + String from = parts.length > 3 ? parts[3] : ""; + String to = parts.length > 4 ? parts[4] : ""; + task = new Event(description, from, to); + break; + default: + System.out.println("Unknown task type in file: " + type); + return null; // Return null for unknown task types + } + task.isDone = isDone; + return task; } + + + public void addTask(String userInput) throws HandleException { - // Similar to addTask method in TaskManager, but refactored for this class Task task = TaskFactory.createTask(userInput); if (task != null) { tasks.add(task); + saveTasks(); // Save the task list to file after adding a new task System.out.println("Got it. I've added this task:"); System.out.println(" " + task); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } } + public void saveTasks() { + try { + File file = new File(FILE_PATH); + if (!file.getParentFile().mkdirs() && !file.getParentFile().exists()) { + System.out.println("Failed to create directories for the task file."); + return; + } + try (FileWriter writer = new FileWriter(file, false)) { + for (Task task : tasks) { + writer.write(task.toFileFormat() + System.lineSeparator()); + } + } + } catch (IOException e) { + System.out.println("Unable to save tasks to file: " + e.getMessage()); + } + } + public void listTasks() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < tasks.size(); i++) { @@ -24,3 +97,4 @@ public void listTasks() { } } } + diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index eabef3ab8..45ae4c66e 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -3,6 +3,10 @@ public Todo(String description) { super(description); } + public String toFileFormat() { + return String.format("T | %d | %s", isDone ? 1 : 0, description); + } + @Override public String toString() { return "[T]" + super.toString(); From d5c4f4f9a759c73ccbef89e36228e79ec6b2927e Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Mon, 4 Mar 2024 14:34:33 +0800 Subject: [PATCH 13/18] more-oop --- data/duke.txt | 7 ++- src/main/java/CommandParser.java | 52 ++++++++-------- src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/Storage.java | 71 ++++++++++++++++++++++ src/main/java/Task.java | 11 ++++ src/main/java/TaskFactory.java | 9 ++- src/main/java/TaskList.java | 95 +++++++----------------------- src/main/java/TaskManager.java | 41 +++++++------ src/main/java/Todo.java | 2 +- src/main/java/Ui.java | 30 ++++++++++ 10 files changed, 197 insertions(+), 124 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/Storage.java create mode 100644 src/main/java/Ui.java diff --git a/data/duke.txt b/data/duke.txt index f9d50bb61..06adbe7f1 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,2 +1,5 @@ -T | 0 | Read a book -T | 0 | Sleep an hour +T | 1 | 1 +T | 0 | 2 +T | 1 | read book +D | 0 | askdfsoief as kjsa lf | SFJ +T | 1 | readbook diff --git a/src/main/java/CommandParser.java b/src/main/java/CommandParser.java index 459ca28a2..7e93b6127 100644 --- a/src/main/java/CommandParser.java +++ b/src/main/java/CommandParser.java @@ -1,34 +1,38 @@ public class CommandParser { private final TaskList taskList; + private final Ui ui; - - public CommandParser(TaskList taskList) { + public CommandParser(TaskList taskList, Ui ui) { this.taskList = taskList; + this.ui = ui; } - public void parseCommand(String userInput) throws HandleException { - if (userInput.trim().isEmpty()) { - throw new HandleException("The input cannot be empty!"); - } else if (userInput.equalsIgnoreCase("list")) { - taskList.listTasks(); - } else if (userInput.startsWith("delete")) { - String[] parts = userInput.split(" ", 2); - if (parts.length < 2 || parts[1].isEmpty()) { - throw new HandleException("OOPS!!! The task number to delete cannot be empty."); - } - int taskIndex; - try { - taskIndex = Integer.parseInt(parts[1]); - } catch (NumberFormatException e) { - throw new HandleException("OOPS!!! The task number is invalid."); + public void parseCommand(String userInput) { + try { + if (userInput.trim().isEmpty()) { + ui.showError("The input cannot be empty!"); + } else if (userInput.equalsIgnoreCase("list")) { + taskList.listTasks(); + } else if (userInput.startsWith("delete")) { + int taskIndex = Integer.parseInt(userInput.split(" ")[1]) - 1; // Get task index + taskList.deleteTask(taskIndex); + } else if (userInput.startsWith("mark")) { + int taskIndex = Integer.parseInt(userInput.split(" ")[1]) - 1; // Get task index + taskList.markTaskAsDone(taskIndex); + } else if (userInput.startsWith("unmark")) { + int taskIndex = Integer.parseInt(userInput.split(" ")[1]) - 1; // Get task index + taskList.markTaskAsNotDone(taskIndex); + } else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") && !userInput.startsWith("event")) { + ui.showError("OOPS!!! I'm sorry, but I don't know what that means :-("); + } else { + taskList.addTask(userInput); } - taskList.deleteTask(taskIndex); - } else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") && !userInput.startsWith("event")) { - throw new HandleException("OOPS!!! I'm sorry, but I don't know what that means :-("); - } else { - taskList.addTask(userInput); + } catch (HandleException he) { + ui.showError(he.getMessage()); + } catch (NumberFormatException nfe) { + ui.showError("OOPS!!! The task number is invalid."); + } catch (ArrayIndexOutOfBoundsException aioobe) { + ui.showError("OOPS!!! It seems like the command is not complete."); } } } - - diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..fda7ec6fa --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: TaskManager + diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 000000000..06fe6f101 --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,71 @@ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + private final String filePath; + + public Storage(String filePath) { + this.filePath = filePath; + } + + public ArrayList load() { + ArrayList loadedTasks = new ArrayList<>(); + File file = new File(filePath); + if (file.exists()) { + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + Task task = parseLineToTask(line); // Use this class's method + if (task != null) { + loadedTasks.add(task); + } + } + } catch (IOException e) { + System.out.println("Unable to read tasks from file: " + e.getMessage()); + } + } + return loadedTasks; + } + + public void save(ArrayList tasks) throws IOException { + try (FileWriter writer = new FileWriter(filePath, false)) { + for (Task task : tasks) { + writer.write(task.toFileFormat() + System.lineSeparator()); + } + } + } + + // Moved to Storage class from TaskList + private Task parseLineToTask(String line) { + String[] parts = line.split(" \\| "); + String type = parts[0]; + Task task = null; + + try { + switch (type) { + case "T": + task = new Todo(parts[2].trim()); + break; + case "D": + task = new DeadLine(parts[2].trim(), parts[3].trim()); + break; + case "E": + task = new Event(parts[2].trim(), parts[3].trim(), parts[4].trim()); + break; + } + + if (task != null) { + boolean isDone = parts[1].trim().equals("1"); + task.setDone(isDone); + } + } catch (Exception e) { + System.err.println("Failed to parse line into task: " + line); + } + + return task; + } +} + diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 24a1c4d70..bf5ed1aba 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -9,6 +9,17 @@ public Task(String description) { this.description = description; this.isDone = false; } + public void markAsDone() { + this.isDone = true; + } + + public void markAsNotDone() { + this.isDone = false; + } + + public void setDone(boolean isDone) { + this.isDone = isDone; + } public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X diff --git a/src/main/java/TaskFactory.java b/src/main/java/TaskFactory.java index c6f5a0ee8..1b4f2760f 100644 --- a/src/main/java/TaskFactory.java +++ b/src/main/java/TaskFactory.java @@ -2,25 +2,28 @@ public class TaskFactory { public static Task createTask(String userInput) throws HandleException { String[] parts = userInput.split(" ", 2); String type = parts[0]; - Task task = null; + Task task; if (parts.length < 2 || parts[1].isEmpty()) { throw new HandleException("OOPS!!! The description of a task cannot be empty."); } + // Declare details outside the switch to use across multiple cases + String[] details = parts[1].split(" /by ", 2); + switch (type) { case "todo": task = new Todo(parts[1]); break; case "deadline": - String[] details = parts[1].split(" /by ", 2); if (details.length < 2 || details[1].isEmpty()) { throw new HandleException("OOPS!!! The date of a deadline cannot be empty."); } task = new DeadLine(details[0], details[1]); break; case "event": - details = parts[1].split(" /from ", 2); + // Assuming event command format was intended to be "event description /from start /to end" + details = parts[1].split(" /from ", 2); // Re-split for event if (details.length < 2 || details[1].isEmpty()) { throw new HandleException("OOPS!!! The start time of an event cannot be empty."); } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index afafce764..aef7b5128 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,78 +1,44 @@ -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; import java.util.ArrayList; -import java.util.Scanner; public class TaskList { private final ArrayList tasks; - private static final String FILE_PATH = "./data/duke.txt"; - public TaskList() { - this.tasks = new ArrayList<>(); - loadTasks(); // Ensure tasks are loaded when an instance is created + public TaskList(ArrayList tasks) { + this.tasks = tasks; } - private void loadTasks() { - File file = new File(FILE_PATH); - if (!file.exists()) { - return; // Skip loading if file does not exist - } - try (Scanner scanner = new Scanner(file)) { - while (scanner.hasNextLine()) { - String line = scanner.nextLine(); - Task task = parseLineToTask(line); - if (task != null) { - tasks.add(task); - } - } - } catch (IOException e) { // Catching IOException which covers FileNotFoundException - System.out.println("Unable to read tasks from file: " + e.getMessage()); - } - } - - private Task parseLineToTask(String line) { - String[] parts = line.split(" \\| "); - String type = parts[0]; - boolean isDone = parts[1].equals("1"); - String description = parts[2]; - Task task; - - switch (type) { - case "T": - task = new Todo(description); - break; - case "D": - String by = parts.length > 3 ? parts[3] : ""; - task = new DeadLine(description, by); - break; - case "E": - String from = parts.length > 3 ? parts[3] : ""; - String to = parts.length > 4 ? parts[4] : ""; - task = new Event(description, from, to); - break; - default: - System.out.println("Unknown task type in file: " + type); - return null; // Return null for unknown task types - } - task.isDone = isDone; - return task; + public ArrayList getTasks() { + return tasks; } - - - public void addTask(String userInput) throws HandleException { Task task = TaskFactory.createTask(userInput); if (task != null) { tasks.add(task); - saveTasks(); // Save the task list to file after adding a new task System.out.println("Got it. I've added this task:"); System.out.println(" " + task); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } } + public void markTaskAsDone(int taskIndex) { + if (taskIndex >= 0 && taskIndex < tasks.size()) { + Task task = tasks.get(taskIndex); + task.markAsDone(); + System.out.println("Nice! I've marked this task as done:\n " + task); + } else { + System.out.println("Task with given index does not exist."); + } + } + public void markTaskAsNotDone(int taskIndex) { + if (taskIndex >= 0 && taskIndex < tasks.size()) { + Task task = tasks.get(taskIndex); + task.markAsNotDone(); + System.out.println("OK, I've marked this task as not done yet:\n " + task); + } else { + System.out.println("Task with given index does not exist."); + } + } public void deleteTask(int taskIndex) throws HandleException { if (taskIndex < 1 || taskIndex > tasks.size()) { @@ -84,23 +50,6 @@ public void deleteTask(int taskIndex) throws HandleException { System.out.println("Now you have " + tasks.size() + " tasks in the list."); } - public void saveTasks() { - try { - File file = new File(FILE_PATH); - if (!file.getParentFile().mkdirs() && !file.getParentFile().exists()) { - System.out.println("Failed to create directories for the task file."); - return; - } - try (FileWriter writer = new FileWriter(file, false)) { - for (Task task : tasks) { - writer.write(task.toFileFormat() + System.lineSeparator()); - } - } - } catch (IOException e) { - System.out.println("Unable to save tasks to file: " + e.getMessage()); - } - } - public void listTasks() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < tasks.size(); i++) { diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 9de19462b..106ea9f92 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -1,31 +1,30 @@ -import java.util.Scanner; +import java.io.IOException; // Make sure to import IOException +import java.util.ArrayList; public class TaskManager { public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - TaskList taskList = new TaskList(); - CommandParser commandParser = new CommandParser(taskList); + Ui ui = new Ui(); + Storage storage = new Storage("./data/duke.txt"); + ArrayList loadedTasks = storage.load(); // Load tasks from storage + TaskList taskList = new TaskList(loadedTasks); // Initialize TaskList with loaded tasks + CommandParser commandParser = new CommandParser(taskList, ui); // Initialize CommandParser with TaskList and Ui - System.out.println("Hello! I'm TaskManager Jamarcus \nWhat can I do for you?"); + ui.showWelcome(); - - while (true) { - try { - String userInput = scanner.nextLine().trim(); - if (userInput.equalsIgnoreCase("bye")) { - System.out.println("Bye. Hope to see you again soon!"); - break; - } else { - commandParser.parseCommand(userInput); + boolean isExit = false; + while (!isExit) { + String userInput = ui.readCommand(); + if (userInput.equalsIgnoreCase("bye")) { + ui.showGoodbye(); + try { + storage.save(taskList.getTasks()); // Attempt to save tasks before exiting + } catch (IOException e) { // Correctly handle the IOException + ui.showError("An error occurred while saving tasks: " + e.getMessage()); } - } catch (HandleException e) { - System.out.println(e.getMessage()); - } catch (Exception e) { - System.out.println("An unexpected error occurred: " + e.getMessage()); + isExit = true; + } else { + commandParser.parseCommand(userInput); } } - - scanner.close(); } } - diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index 45ae4c66e..d977cff12 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -4,7 +4,7 @@ public Todo(String description) { } public String toFileFormat() { - return String.format("T | %d | %s", isDone ? 1 : 0, description); + return super.toFileFormat(); } @Override diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 000000000..7cc86a62c --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,30 @@ +import java.util.Scanner; + +public class Ui { + private final Scanner scanner; + + public Ui() { + this.scanner = new Scanner(System.in); + } + + public String readCommand() { + System.out.println("\nEnter your command:"); + return scanner.nextLine().trim(); + } + + public void showWelcome() { + System.out.println("Hello! I'm TaskManager\nWhat can I do for you?"); + } + + public void showError(String message) { + System.out.println(message); + } + + public void showLine(String message) { + System.out.println(message); + } + + public void showGoodbye() { + System.out.println("Bye. Hope to see you again soon!"); + } +} From 8dfd01e404fe5389bc8a43c0bd9e7bc4e287ff57 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Mon, 4 Mar 2024 22:47:32 +0800 Subject: [PATCH 14/18] checked coding standard and quality --- src/main/java/CommandParser.java | 6 +++++- src/main/java/Task.java | 4 ++++ src/main/java/TaskList.java | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/CommandParser.java b/src/main/java/CommandParser.java index 7e93b6127..d2ed783f1 100644 --- a/src/main/java/CommandParser.java +++ b/src/main/java/CommandParser.java @@ -22,7 +22,11 @@ public void parseCommand(String userInput) { } else if (userInput.startsWith("unmark")) { int taskIndex = Integer.parseInt(userInput.split(" ")[1]) - 1; // Get task index taskList.markTaskAsNotDone(taskIndex); - } else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") && !userInput.startsWith("event")) { + } else if (userInput.startsWith("find")) { + String keyword = userInput.substring(5); // ๅ‡่ฎพ "find " ๅŽ้ข่ทŸ็€ๅ…ณ้”ฎ่ฏ + taskList.findTask(keyword); + }else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") + && !userInput.startsWith("event") && !userInput.startsWith("find")) { ui.showError("OOPS!!! I'm sorry, but I don't know what that means :-("); } else { taskList.addTask(userInput); diff --git a/src/main/java/Task.java b/src/main/java/Task.java index bf5ed1aba..f4072527c 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -29,4 +29,8 @@ public String getStatusIcon() { public String toString() { return "[" + this.getStatusIcon() + "] " + this.description; } + + public String getDescription() { + return this.description; + } } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index aef7b5128..78dcbf03d 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -56,5 +56,20 @@ public void listTasks() { System.out.println((i + 1) + "." + tasks.get(i)); } } + + public void findTask(String keyword) { + System.out.println("Here are the matching tasks in your list:"); + int count = 0; + for (int i = 0; i < tasks.size(); i++) { + Task task = tasks.get(i); + if (task.getDescription().contains(keyword)) { + System.out.println((i + 1) + "." + task); + count++; + } + } + if (count == 0) { + System.out.println("No matching tasks found."); + } + } } From da4cb5e836315a586d35ad6aa7f106132aff0d10 Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Mon, 4 Mar 2024 23:26:15 +0800 Subject: [PATCH 15/18] checked coding standard and quality --- src/main/java/CommandParser.java | 22 ++++++++++- src/main/java/DeadLine.java | 34 ++++++++++++++--- src/main/java/Event.java | 40 ++++++++++++++++---- src/main/java/HandleException.java | 14 +++++++ src/main/java/Storage.java | 37 ++++++++++++++++-- src/main/java/Task.java | 55 ++++++++++++++++++++++++--- src/main/java/TaskFactory.java | 27 ++++++++++--- src/main/java/TaskList.java | 61 +++++++++++++++++++++++++----- src/main/java/TaskManager.java | 38 ++++++++++++------- src/main/java/Todo.java | 30 +++++++++++++-- src/main/java/Ui.java | 31 ++++++++++++++- 11 files changed, 333 insertions(+), 56 deletions(-) diff --git a/src/main/java/CommandParser.java b/src/main/java/CommandParser.java index d2ed783f1..eef2d2ae4 100644 --- a/src/main/java/CommandParser.java +++ b/src/main/java/CommandParser.java @@ -1,12 +1,30 @@ +/** + * CommandParser is responsible for interpreting and executing commands + * input by the user. It acts as a controller that directs user commands + * to appropriate actions on the task list. + */ public class CommandParser { private final TaskList taskList; private final Ui ui; + /** + * Constructs a CommandParser with the specified task list and UI. + * + * @param taskList the task list to be manipulated based on user commands. + * @param ui the UI object for interacting with the user. + */ public CommandParser(TaskList taskList, Ui ui) { this.taskList = taskList; this.ui = ui; } + /** + * Parses and executes a user input command. Supported commands include + * adding, deleting, marking, unmarking tasks, and finding tasks by keywords. + * It also handles listing all tasks and exiting the application. + * + * @param userInput the full user input command to be parsed and executed. + */ public void parseCommand(String userInput) { try { if (userInput.trim().isEmpty()) { @@ -23,9 +41,9 @@ public void parseCommand(String userInput) { int taskIndex = Integer.parseInt(userInput.split(" ")[1]) - 1; // Get task index taskList.markTaskAsNotDone(taskIndex); } else if (userInput.startsWith("find")) { - String keyword = userInput.substring(5); // ๅ‡่ฎพ "find " ๅŽ้ข่ทŸ็€ๅ…ณ้”ฎ่ฏ + String keyword = userInput.substring(5); // Assume "find " is followed by a keyword taskList.findTask(keyword); - }else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") + } else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") && !userInput.startsWith("event") && !userInput.startsWith("find")) { ui.showError("OOPS!!! I'm sorry, but I don't know what that means :-("); } else { diff --git a/src/main/java/DeadLine.java b/src/main/java/DeadLine.java index 4119d76c9..ddbc36201 100644 --- a/src/main/java/DeadLine.java +++ b/src/main/java/DeadLine.java @@ -1,14 +1,38 @@ +/** + * Represents a deadline task with a specific due date. + * A {@code DeadLine} object encapsulates the details of a task that needs to be done before a specific date. + */ public class DeadLine extends Task { - protected String by; + protected String by; // The due date of the deadline task. + /** + * Constructs a {@code DeadLine} with the specified task description and due date. + * + * @param description The description of the deadline task. + * @param by The due date of the task. + */ + public DeadLine(String description, String by) { + super(description); // Call the superclass constructor to set the task description. + this.by = by; // Set the due date of the deadline task. + } + + /** + * Returns the string representation of the deadline task in a format suitable for file storage. + * The format is "D | isDone | description | by", where "D" indicates a deadline task. + * + * @return A string representation of the deadline task for file storage. + */ + @Override public String toFileFormat() { return String.format("D | %d | %s | %s", isDone ? 1 : 0, description, by); } - public DeadLine(String description, String by) { - super(description); - this.by = by; - } + /** + * Returns the string representation of the deadline task, including its status (done or not done), + * description, and due date. + * + * @return A string representation of the deadline task, including status, description, and due date. + */ @Override public String toString() { return "[D]" + super.toString() + " (by: " + this.by + ")"; diff --git a/src/main/java/Event.java b/src/main/java/Event.java index c64053991..b94f9a61c 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,18 +1,44 @@ +/** + * Represents an event task with a start and end time. + * An {@code Event} object encapsulates the details of a task that occurs over a period defined by a start and end time. + */ public class Event extends Task { - protected String from; - protected String to; + protected String from; // The start time of the event. + protected String to; // The end time of the event. + /** + * Constructs an {@code Event} with the specified task description, start time, and end time. + * + * @param description The description of the event task. + * @param from The start time of the event. + * @param to The end time of the event. + */ + public Event(String description, String from, String to) { + super(description); // Call the superclass constructor to set the task description. + this.from = from; // Set the start time of the event. + this.to = to; // Set the end time of the event. + } + + /** + * Returns the string representation of the event task in a format suitable for file storage. + * The format is "E | isDone | description | from to to", where "E" indicates an event task. + * + * @return A string representation of the event task for file storage. + */ + @Override public String toFileFormat() { return String.format("E | %d | %s | %s to %s", isDone ? 1 : 0, description, from, to); } - public Event(String description, String from, String to) { - super(description); - this.from = from; - this.to = to; - } + /** + * Returns the string representation of the event task, including its status (done or not done), + * description, and the period over which it occurs (from start time to end time). + * + * @return A string representation of the event task, including status, description, and period. + */ @Override public String toString() { return "[E]" + super.toString() + " (from: " + this.from + " to: " + this.to + ")"; } } + diff --git a/src/main/java/HandleException.java b/src/main/java/HandleException.java index 45e3ea951..325306b2c 100644 --- a/src/main/java/HandleException.java +++ b/src/main/java/HandleException.java @@ -1,6 +1,20 @@ +/** + * Represents a custom exception class for handling errors specific to the application. + * {@code HandleException} is used to encapsulate user-related errors, providing + * more context and control over error handling within the application. + */ public class HandleException extends Exception { + + /** + * Constructs a new {@code HandleException} with the specified detail message. + * The detail message is saved for later retrieval by the {@link #getMessage()} method. + * + * @param message the detail message. The detail message is saved for later retrieval + * by the {@link #getMessage()} method. + */ public HandleException(String message) { super(message); } } + diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index 06fe6f101..9d7033cc2 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -4,13 +4,30 @@ import java.util.ArrayList; import java.util.Scanner; +/** + * Handles loading and saving tasks to a file. + * This class is responsible for reading from and writing to the task storage file, + * converting between the file format and Task objects. + */ public class Storage { - private final String filePath; + private final String filePath; // The file path where tasks are stored. + /** + * Constructs a Storage object associated with a specific file path. + * + * @param filePath the path to the file used for task storage. + */ public Storage(String filePath) { this.filePath = filePath; } + /** + * Loads tasks from the storage file. + * Reads the task file line by line, parsing each line into a Task object, + * and returns a list of all tasks loaded from the file. + * + * @return an ArrayList of Task objects loaded from the file. + */ public ArrayList load() { ArrayList loadedTasks = new ArrayList<>(); File file = new File(filePath); @@ -18,7 +35,7 @@ public ArrayList load() { try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); - Task task = parseLineToTask(line); // Use this class's method + Task task = parseLineToTask(line); if (task != null) { loadedTasks.add(task); } @@ -30,6 +47,13 @@ public ArrayList load() { return loadedTasks; } + /** + * Saves the current list of tasks to the storage file. + * Writes each task to the file in a format suitable for later loading. + * + * @param tasks the list of Task objects to be saved to the file. + * @throws IOException if an I/O error occurs writing to the file. + */ public void save(ArrayList tasks) throws IOException { try (FileWriter writer = new FileWriter(filePath, false)) { for (Task task : tasks) { @@ -38,7 +62,13 @@ public void save(ArrayList tasks) throws IOException { } } - // Moved to Storage class from TaskList + /** + * Parses a single line from the task file into a Task object. + * This method is used internally when loading tasks from the file. + * + * @param line the line from the file to parse into a Task object. + * @return the Task object parsed from the line, or null if the line could not be parsed. + */ private Task parseLineToTask(String line) { String[] parts = line.split(" \\| "); String type = parts[0]; @@ -68,4 +98,3 @@ private Task parseLineToTask(String line) { return task; } } - diff --git a/src/main/java/Task.java b/src/main/java/Task.java index f4072527c..d5b9de8f6 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,35 +1,78 @@ +/** + * Represents a generic task in the task list. + * This class serves as a base class for different types of tasks + * such as todos, deadlines, and events. + */ public class Task { - protected String description; - protected boolean isDone; + protected String description; // The task's description + protected boolean isDone; // The task's completion status - public String toFileFormat() { - return String.format("T | %d | %s", isDone ? 1 : 0, description); - } + /** + * Constructs a new Task with the given description. By default, the task is not done. + * + * @param description The text description of the task. + */ public Task(String description) { this.description = description; this.isDone = false; } + + /** + * Formats the task for file storage, including its done status and description. + * + * @return A string representation of the task suitable for file storage. + */ + public String toFileFormat() { + return String.format("T | %d | %s", isDone ? 1 : 0, description); + } + + /** + * Marks the task as done. + */ public void markAsDone() { this.isDone = true; } + /** + * Marks the task as not done. + */ public void markAsNotDone() { this.isDone = false; } + /** + * Sets the done status of the task. + * + * @param isDone True if the task is done, false otherwise. + */ public void setDone(boolean isDone) { this.isDone = isDone; } + /** + * Returns a status icon indicating whether the task is done. + * + * @return A string representing a check mark if done, or a space if not done. + */ public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } - + /** + * Returns a string representation of the task, including its status icon and description. + * + * @return A string representation of the task. + */ + @Override public String toString() { return "[" + this.getStatusIcon() + "] " + this.description; } + /** + * Gets the description of the task. + * + * @return The description of the task. + */ public String getDescription() { return this.description; } diff --git a/src/main/java/TaskFactory.java b/src/main/java/TaskFactory.java index 1b4f2760f..595ddab3b 100644 --- a/src/main/java/TaskFactory.java +++ b/src/main/java/TaskFactory.java @@ -1,4 +1,21 @@ +/** + * TaskFactory is responsible for creating Task objects based on user input. + * It interprets the command and parameters provided by the user to instantiate + * the appropriate Task subclass (Todo, DeadLine, or Event). + */ public class TaskFactory { + + /** + * Creates a Task object based on the user's input. + * This method parses the user input to determine the type of task to create + * and extracts any necessary parameters (e.g., description, due date). + * + * @param userInput The complete command input from the user, including the task type + * and any parameters. + * @return A Task object corresponding to the user's command. + * @throws HandleException If the input format is incorrect or insufficient information + * is provided to create a task. + */ public static Task createTask(String userInput) throws HandleException { String[] parts = userInput.split(" ", 2); String type = parts[0]; @@ -8,22 +25,22 @@ public static Task createTask(String userInput) throws HandleException { throw new HandleException("OOPS!!! The description of a task cannot be empty."); } - // Declare details outside the switch to use across multiple cases - String[] details = parts[1].split(" /by ", 2); + // Details are declared outside the switch to use across multiple cases. + String[] details; - switch (type) { + switch (type.toLowerCase()) { case "todo": task = new Todo(parts[1]); break; case "deadline": + details = parts[1].split(" /by ", 2); if (details.length < 2 || details[1].isEmpty()) { throw new HandleException("OOPS!!! The date of a deadline cannot be empty."); } task = new DeadLine(details[0], details[1]); break; case "event": - // Assuming event command format was intended to be "event description /from start /to end" - details = parts[1].split(" /from ", 2); // Re-split for event + details = parts[1].split(" /from ", 2); if (details.length < 2 || details[1].isEmpty()) { throw new HandleException("OOPS!!! The start time of an event cannot be empty."); } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index 78dcbf03d..718a53fa9 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,25 +1,49 @@ import java.util.ArrayList; +/** + * Represents a list of tasks in the application. This class manages the tasks, + * including adding, deleting, marking tasks as done or not done, listing all tasks, + * and finding tasks by keywords. + */ public class TaskList { - private final ArrayList tasks; + private final ArrayList tasks; // A list of tasks. + /** + * Constructs a TaskList with a specified list of tasks. + * + * @param tasks An ArrayList of Task objects. + */ public TaskList(ArrayList tasks) { this.tasks = tasks; } + /** + * Returns the list of tasks. + * + * @return An ArrayList of Task objects. + */ public ArrayList getTasks() { return tasks; } + /** + * Adds a task to the task list based on the user input. + * + * @param userInput The full command input from the user for creating a task. + * @throws HandleException If the task cannot be created due to invalid input. + */ public void addTask(String userInput) throws HandleException { Task task = TaskFactory.createTask(userInput); - if (task != null) { - tasks.add(task); - System.out.println("Got it. I've added this task:"); - System.out.println(" " + task); - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } + tasks.add(task); + System.out.println("Got it. I've added this task:\n " + task); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } + + /** + * Marks a task as done based on its index in the task list. + * + * @param taskIndex The index of the task to be marked as done. + */ public void markTaskAsDone(int taskIndex) { if (taskIndex >= 0 && taskIndex < tasks.size()) { Task task = tasks.get(taskIndex); @@ -30,6 +54,11 @@ public void markTaskAsDone(int taskIndex) { } } + /** + * Marks a task as not done based on its index in the task list. + * + * @param taskIndex The index of the task to be marked as not done. + */ public void markTaskAsNotDone(int taskIndex) { if (taskIndex >= 0 && taskIndex < tasks.size()) { Task task = tasks.get(taskIndex); @@ -40,16 +69,24 @@ public void markTaskAsNotDone(int taskIndex) { } } + /** + * Deletes a task from the task list based on its index. + * + * @param taskIndex The index of the task to be deleted. + * @throws HandleException If the specified index is invalid. + */ public void deleteTask(int taskIndex) throws HandleException { if (taskIndex < 1 || taskIndex > tasks.size()) { throw new HandleException("OOPS!!! The task number is invalid."); } Task removedTask = tasks.remove(taskIndex - 1); - System.out.println("Noted. I've removed this task:"); - System.out.println(" " + removedTask); + System.out.println("Noted. I've removed this task:\n " + removedTask); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } + /** + * Lists all tasks in the task list. + */ public void listTasks() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < tasks.size(); i++) { @@ -57,6 +94,11 @@ public void listTasks() { } } + /** + * Finds and lists all tasks that contain the specified keyword in their description. + * + * @param keyword The keyword to search for in task descriptions. + */ public void findTask(String keyword) { System.out.println("Here are the matching tasks in your list:"); int count = 0; @@ -72,4 +114,3 @@ public void findTask(String keyword) { } } } - diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 106ea9f92..a6374e302 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -1,29 +1,41 @@ import java.io.IOException; // Make sure to import IOException import java.util.ArrayList; +/** + * The entry point of the application. This class orchestrates the flow of the application, + * including initializing components, loading tasks from storage, and processing user commands. + */ public class TaskManager { + + /** + * The main method that drives the application. It initializes the necessary components + * such as Ui, Storage, and TaskList, and enters a loop to accept and process user commands + * until the "bye" command is entered. + * + * @param args Command line arguments (not used in this application). + */ public static void main(String[] args) { - Ui ui = new Ui(); - Storage storage = new Storage("./data/duke.txt"); - ArrayList loadedTasks = storage.load(); // Load tasks from storage - TaskList taskList = new TaskList(loadedTasks); // Initialize TaskList with loaded tasks - CommandParser commandParser = new CommandParser(taskList, ui); // Initialize CommandParser with TaskList and Ui + Ui ui = new Ui(); // Component for user interactions. + Storage storage = new Storage("./data/duke.txt"); // Component for handling task storage. + ArrayList loadedTasks = storage.load(); // Load tasks from the specified storage. + TaskList taskList = new TaskList(loadedTasks); // Initialize the task list with loaded tasks. + CommandParser commandParser = new CommandParser(taskList, ui); // Initialize the command parser with the task list and UI. - ui.showWelcome(); + ui.showWelcome(); // Display a welcome message to the user. - boolean isExit = false; + boolean isExit = false; // Flag to control the main application loop. while (!isExit) { - String userInput = ui.readCommand(); + String userInput = ui.readCommand(); // Read user input. if (userInput.equalsIgnoreCase("bye")) { - ui.showGoodbye(); + ui.showGoodbye(); // Display a goodbye message. try { - storage.save(taskList.getTasks()); // Attempt to save tasks before exiting - } catch (IOException e) { // Correctly handle the IOException + storage.save(taskList.getTasks()); // Attempt to save the current state of tasks. + } catch (IOException e) { // Handle potential IO exceptions from saving tasks. ui.showError("An error occurred while saving tasks: " + e.getMessage()); } - isExit = true; + isExit = true; // Set flag to exit the application loop. } else { - commandParser.parseCommand(userInput); + commandParser.parseCommand(userInput); // Parse and execute the user command. } } } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index d977cff12..aaf307d16 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -1,14 +1,38 @@ +/** + * Represents a to-do task without a specific deadline. + * A {@code Todo} object encapsulates the details of a task that needs to be done. + */ public class Todo extends Task { + + /** + * Constructs a {@code Todo} with the specified task description. + * Initializes a new to-do task with the provided description. + * + * @param description The text description of the to-do task. + */ public Todo(String description) { - super(description); + super(description); // Calls the superclass Task constructor with the description. } + /** + * Returns the string representation of the to-do task in a format suitable for file storage. + * This method leverages the {@code toFileFormat} method from the superclass {@code Task}. + * + * @return A string representation of the to-do task for file storage, maintaining the format defined in {@code Task}. + */ + @Override public String toFileFormat() { - return super.toFileFormat(); + return super.toFileFormat(); // Uses the Task class's method for file formatting. } + /** + * Returns the string representation of the to-do task, including its type, status (done or not done), + * and description. Overrides the {@code toString} method from the superclass {@code Task}. + * + * @return A string representation of the to-do task, formatted with its type and status. + */ @Override public String toString() { - return "[T]" + super.toString(); + return "[T]" + super.toString(); // Adds the to-do type identifier "[T]" before the standard task representation. } } diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 7cc86a62c..423970c13 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,29 +1,58 @@ import java.util.Scanner; +/** + * Handles all user interactions for the application. This class is responsible for + * displaying messages to the user and reading user input from the command line. + */ public class Ui { - private final Scanner scanner; + private final Scanner scanner; // Scanner object to read user input. + /** + * Constructs a Ui object, initializing the scanner to read input from the command line. + */ public Ui() { this.scanner = new Scanner(System.in); } + /** + * Prompts the user for a command and returns the trimmed input. + * + * @return The user's input as a trimmed string. + */ public String readCommand() { System.out.println("\nEnter your command:"); return scanner.nextLine().trim(); } + /** + * Displays a welcome message to the user at the start of the application. + */ public void showWelcome() { System.out.println("Hello! I'm TaskManager\nWhat can I do for you?"); } + /** + * Displays an error message to the user. + * + * @param message The error message to be displayed. + */ public void showError(String message) { System.out.println(message); } + /** + * Displays a line of text to the user. Can be used for displaying + * informational messages or command results. + * + * @param message The message to be displayed. + */ public void showLine(String message) { System.out.println(message); } + /** + * Displays a goodbye message to the user when exiting the application. + */ public void showGoodbye() { System.out.println("Bye. Hope to see you again soon!"); } From fb94eb630ea54ef61e0a6490abe45f414999d45b Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Mon, 4 Mar 2024 23:53:00 +0800 Subject: [PATCH 16/18] checked coding standard and quality --- docs/README.md | 142 ++++++++++++++++++++++++++++++++---- src/main/java/TaskList.java | 4 +- 2 files changed, 128 insertions(+), 18 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..e27844093 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,139 @@ -# User Guide +# TaskManager -## Features +TaskManager is a simple yet powerful Java application designed to help users manage their tasks efficiently. It allows users to add, delete, mark tasks as done or not done, and find tasks by searching for keywords. -### Feature-ABC +## Features -Description of the feature. +- **Add Tasks**: Users can add three types of tasks - todos, deadlines, and events. +- **Delete Tasks**: Users can delete tasks they no longer need. +- **Mark Tasks**: Users can mark tasks as done or not done. +- **Find Tasks**: Users can search for tasks by keywords. +- **List Tasks**: Users can view all their tasks at any time. -### Feature-XYZ +## Getting Started +## Adding a task +### Type of task +- Todo +- Deadline +- Event -Description of the feature. +### Prompt +- Todo: todo {task description} -## Usage + _e.g. adding task todo `read book`_ + ``` + todo read book + ``` -### `Keyword` - Describe action +- Deadline: deadline {task description} /by {date} -Describe the action and its outcome. + _e.g. adding task deadline `return book` before `2021-09-17`_ + ``` + deadline return book /by 2021-09-17 + ``` -Example of usage: +- Event: event {task description} from {date} to {date} -`keyword (optional arguments)` + _e.g. adding task event `project meeting` from `2021-09-17` to `2021-09-18`_ + ``` + event project meeting from 2021-09-17 to 2021-09-18 + ``` +### Expected output for all adding tasks + Got it. I've added this task: + [D][ ] return book (by: 2021-09-17) +> Note: the following e.g. for each command will be using the output of the above prompt -Expected outcome: +## Listing all tasks +### Prompt +- list -Description of the outcome. + _e.g. listing all tasks_ + ``` + list + ``` +### Expected output + ``` + Here are the tasks in your list: + 1. [T][ ] read book + 2. [D][ ] return book (by: Sep 17 2021) + 3. [E][ ] project meeting (from: Sep 17 2021 to: Sep 18 2021) + ``` -``` -expected output -``` +## Deleting a task +### prompt +- delete {task number} + + _e.g. deleting task 3_ + ``` + delete 3 + ``` +### Expected output + ``` + Noted. I've removed this task: {task number} + ``` + this can be verified by listing all tasks: + ``` + list + Here are the tasks in your list: + 1. [T][ ] read book + 2. [D][ ] return book (by: Sep 17 2021) + ``` + +## Marking a task as done +### Prompt +- mark {task name} + + _e.g. marking task 1 as done_ + ``` + mark read book + ``` +### Expected output + ``` + Nice! I've marked this task as done: {task name} + ``` + this can be verified by listing or finding tasks: + ``` + list + Here are the tasks in your list: + 1. [T][X] read book + 2. [D][ ] return book (by: Sep 17 2021) + 3. [E][ ] project meeting (from: Sep 17 2021 to: Sep 18 2021) + ``` + +- unmark {task name} + + _e.g. unmarking read book_ + ``` + unmark read book + ``` +### Expected output + ``` + unmarked + I've unmarked this task as done: {task name} + ``` +this can be verified by listing all tasks: + ``` + list + Here are the tasks in your list: + 1. [T][ ] read book + 2. [D][ ] return book (by: Sep 17 2021) + 3. [E][ ] project meeting (from: Sep 17 2021 to: Sep 18 2021) + ``` + +## Finding a task +### prompt +- find {keyword} + + e.g. finding tasks related to `book` + ``` + find book + ``` +### Expected output + ``` + Here are the matching tasks in your list: + 1. [T][ ] read book + 2. [D][ ] return book (by: Sep 17 2021) + ``` +### Prerequisites + +- Java 11 or above. \ No newline at end of file diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index 718a53fa9..2e16bb2b4 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -76,10 +76,10 @@ public void markTaskAsNotDone(int taskIndex) { * @throws HandleException If the specified index is invalid. */ public void deleteTask(int taskIndex) throws HandleException { - if (taskIndex < 1 || taskIndex > tasks.size()) { + if (taskIndex < 0 || taskIndex > tasks.size()) { throw new HandleException("OOPS!!! The task number is invalid."); } - Task removedTask = tasks.remove(taskIndex - 1); + Task removedTask = tasks.remove(taskIndex); System.out.println("Noted. I've removed this task:\n " + removedTask); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } From 00b0a4c8ae7d7ab124e74723dd41adcc865f7ddc Mon Sep 17 00:00:00 2001 From: MaJiajun Date: Tue, 5 Mar 2024 00:03:03 +0800 Subject: [PATCH 17/18] checked coding standard and quality --- data/duke.txt | 3 +-- docs/README.md | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/data/duke.txt b/data/duke.txt index 06adbe7f1..58561111f 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,5 +1,4 @@ -T | 1 | 1 -T | 0 | 2 +T | 1 | 2 T | 1 | read book D | 0 | askdfsoief as kjsa lf | SFJ T | 1 | readbook diff --git a/docs/README.md b/docs/README.md index e27844093..205e7a5a2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -52,32 +52,31 @@ TaskManager is a simple yet powerful Java application designed to help users man list ``` ### Expected output - ``` + Here are the tasks in your list: 1. [T][ ] read book 2. [D][ ] return book (by: Sep 17 2021) 3. [E][ ] project meeting (from: Sep 17 2021 to: Sep 18 2021) - ``` + ## Deleting a task ### prompt - delete {task number} - _e.g. deleting task 3_ + _e.g. deleting 3_ ``` delete 3 ``` ### Expected output - ``` + Noted. I've removed this task: {task number} - ``` this can be verified by listing all tasks: - ``` + list Here are the tasks in your list: 1. [T][ ] read book 2. [D][ ] return book (by: Sep 17 2021) - ``` + ## Marking a task as done ### Prompt @@ -88,9 +87,8 @@ TaskManager is a simple yet powerful Java application designed to help users man mark read book ``` ### Expected output - ``` + Nice! I've marked this task as done: {task name} - ``` this can be verified by listing or finding tasks: ``` list @@ -129,11 +127,18 @@ this can be verified by listing all tasks: find book ``` ### Expected output - ``` Here are the matching tasks in your list: 1. [T][ ] read book 2. [D][ ] return book (by: Sep 17 2021) - ``` + +## Exit Program +### prompt +- bye + +### Expected output + ``` + Bye. Hope to see you again soon! + ``` ### Prerequisites - Java 11 or above. \ No newline at end of file From e8ab79396a67353323cf30b80718f2c8635f45b0 Mon Sep 17 00:00:00 2001 From: MA JIAJUN <110816787+Jamarcus111@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:27:52 +0800 Subject: [PATCH 18/18] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fd6c6a930..9f37d2ba9 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,4 @@ Prerequisites: JDK 11, update Intellij to the most recent version. | |_| | |_| | < __/ |____/ \__,_|_|\_\___| ``` +The specific instructions for this task manager program is under docs.README.md