From d1c9b903141beb78277ee6335be61982d2361f31 Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sat, 9 Mar 2024 22:25:46 +0800 Subject: [PATCH 01/15] Level-0 --- README.md | 4 ++-- src/main/java/Duke.java | 10 ---------- src/main/java/Hailey.java | 23 +++++++++++++++++++++++ text-ui-test/runtest.bat | 2 +- 4 files changed, 26 insertions(+), 13 deletions(-) delete mode 100644 src/main/java/Duke.java create mode 100644 src/main/java/Hailey.java diff --git a/README.md b/README.md index 8715d4d91..b299c9619 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# Hailey 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/Hailey.java` file, right-click it, and choose `Run Hailey.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/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); - } -} diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java new file mode 100644 index 000000000..1ef5d0ac3 --- /dev/null +++ b/src/main/java/Hailey.java @@ -0,0 +1,23 @@ +import java.util.Scanner; + +public class Hailey { + public static void main(String[] args) { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + + System.out.println("Hello from\n" + logo + + "____________________________________________________________\n" + + "Hello! I'm Hailey\nWhat can I do for you?\n" + + "____________________________________________________________"); + + new Scanner(System.in).nextLine(); + + System.out.println("____________________________________________________________\n" + + "Bye. Hope to see you again soon!\n" + + "____________________________________________________________"); + } +} + diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..b8a99cad4 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 Hailey < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From c77f7e76d4ee4b56cedf66af020890b8820ce2de Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sat, 9 Mar 2024 22:44:19 +0800 Subject: [PATCH 02/15] Level-1 --- src/main/java/Hailey.java | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index 1ef5d0ac3..6c93e53ff 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -1,23 +1,43 @@ import java.util.Scanner; public class Hailey { + private static final String LINE_SEPARATOR = "____________________________________________________________"; + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; + + "|____/ \\__,_|_|\\_\\___|\n" + + "What can I help you with?"; + + System.out.println("Hello from\n" + logo); + printLine(); + + String userInput; + do { + System.out.println("Hello! I'm Hailey\nWhat can I do for you?"); + printLine(); - System.out.println("Hello from\n" + logo + - "____________________________________________________________\n" + - "Hello! I'm Hailey\nWhat can I do for you?\n" + - "____________________________________________________________"); + userInput = scanner.nextLine(); + printLine(); - new Scanner(System.in).nextLine(); + if (!"bye".equalsIgnoreCase(userInput)) { + System.out.println(userInput); + printLine(); + } + } while (!"bye".equalsIgnoreCase(userInput)); + + System.out.println("Bye. Hope to see you again soon!"); + printLine(); + + scanner.close(); + } - System.out.println("____________________________________________________________\n" + - "Bye. Hope to see you again soon!\n" + - "____________________________________________________________"); + private static void printLine() { + System.out.println(LINE_SEPARATOR); } } From c025fdda1a2210123c03af75f2e5657d9ce26924 Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sat, 9 Mar 2024 23:37:16 +0800 Subject: [PATCH 03/15] Level-2 --- src/main/java/Hailey.java | 76 +++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index 6c93e53ff..6b8ac0641 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -2,42 +2,74 @@ public class Hailey { private static final String LINE_SEPARATOR = "____________________________________________________________"; + private static final int MAX_TASKS = 100; + private static String[] tasks = new String[MAX_TASKS]; + private static int taskCount = 0; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n" - + "What can I help you with?"; + printWelcomeMessage(); - System.out.println("Hello from\n" + logo); - printLine(); - - String userInput; - do { - System.out.println("Hello! I'm Hailey\nWhat can I do for you?"); - printLine(); - - userInput = scanner.nextLine(); + while (true) { + String userInput = scanner.nextLine().trim(); printLine(); - if (!"bye".equalsIgnoreCase(userInput)) { - System.out.println(userInput); - printLine(); + if (userInput.equalsIgnoreCase("bye")) { + printGoodbyeMessage(); + break; + } else if (userInput.equalsIgnoreCase("list")) { + printTasks(); + } else { + addTask(userInput); } - } while (!"bye".equalsIgnoreCase(userInput)); - - System.out.println("Bye. Hope to see you again soon!"); - printLine(); + } scanner.close(); } + private static void printWelcomeMessage() { + System.out.println("Hello from\n" + + " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n" + + "What can I help you with?"); + printLine(); + System.out.println("Hello! I'm Hailey\nWhat can I do for you?"); + printLine(); + } + private static void printLine() { System.out.println(LINE_SEPARATOR); } + + private static void addTask(String task) { + if (taskCount < MAX_TASKS) { + tasks[taskCount++] = task; + System.out.println("added: " + task); + } else { + System.out.println("Sorry, task list is full!"); + } + printLine(); + } + + private static void printTasks() { + if (taskCount == 0) { + System.out.println("Task list is empty!"); + } else { + System.out.println("Tasks:"); + for (int i = 0; i < taskCount; i++) { + System.out.println((i + 1) + ". " + tasks[i]); + } + } + printLine(); + } + + private static void printGoodbyeMessage() { + System.out.println("Bye. Hope to see you again soon!"); + printLine(); + } } From d1f9362a34817a3a25844b5b182033f5d34a80fd Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sat, 9 Mar 2024 23:55:54 +0800 Subject: [PATCH 04/15] Level-3 --- src/main/java/Hailey.java | 62 ++++++++++++++++++++++++++++++++------- src/main/java/Task.java | 26 ++++++++++++++++ 2 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index 6b8ac0641..8e74cd8f1 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -3,7 +3,7 @@ public class Hailey { private static final String LINE_SEPARATOR = "____________________________________________________________"; private static final int MAX_TASKS = 100; - private static String[] tasks = new String[MAX_TASKS]; + private static Task[] tasks = new Task[MAX_TASKS]; private static int taskCount = 0; public static void main(String[] args) { @@ -12,19 +12,25 @@ public static void main(String[] args) { printWelcomeMessage(); while (true) { - String userInput = scanner.nextLine().trim(); + String userInput = scanner.nextLine().trim().toLowerCase(); printLine(); - if (userInput.equalsIgnoreCase("bye")) { - printGoodbyeMessage(); + if (userInput.equals("bye")) { break; - } else if (userInput.equalsIgnoreCase("list")) { + } + + if (userInput.equals("list")) { printTasks(); + } else if (userInput.startsWith("mark ")) { + markTask(userInput); + } else if (userInput.startsWith("unmark ")) { + unmarkTask(userInput); } else { addTask(userInput); } } + printGoodbyeMessage(); scanner.close(); } @@ -45,28 +51,64 @@ private static void printLine() { System.out.println(LINE_SEPARATOR); } - private static void addTask(String task) { + private static void addTask(String taskDescription) { if (taskCount < MAX_TASKS) { - tasks[taskCount++] = task; - System.out.println("added: " + task); + tasks[taskCount++] = new Task(taskDescription); + System.out.println("added: " + taskDescription); } else { System.out.println("Sorry, task list is full!"); } printLine(); } + private static void markTask(String userInput) { + try { + int taskIndex = Integer.parseInt(userInput.split(" ")[1]) - 1; + if (isValidTaskIndex(taskIndex)) { + tasks[taskIndex].markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(tasks[taskIndex]); + } else { + System.out.println("Invalid task number!"); + } + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + System.out.println("Invalid command format!"); + } + printLine(); + } + + private static void unmarkTask(String userInput) { + try { + int taskIndex = Integer.parseInt(userInput.split(" ")[1]) - 1; + if (isValidTaskIndex(taskIndex)) { + tasks[taskIndex].markAsNotDone(); + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(tasks[taskIndex]); + } else { + System.out.println("Invalid task number!"); + } + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + System.out.println("Invalid command format!"); + } + printLine(); + } + private static void printTasks() { if (taskCount == 0) { System.out.println("Task list is empty!"); } else { - System.out.println("Tasks:"); + System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskCount; i++) { - System.out.println((i + 1) + ". " + tasks[i]); + System.out.println((i + 1) + "." + tasks[i]); } } printLine(); } + private static boolean isValidTaskIndex(int index) { + return index >= 0 && index < taskCount; + } + private static void printGoodbyeMessage() { System.out.println("Bye. Hope to see you again soon!"); printLine(); diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..cb4138953 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,26 @@ +public class Task { + private String description; + private boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public void markAsDone() { + this.isDone = true; + } + + public void markAsNotDone() { + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "[X]" : "[ ]"); + } + + @Override + public String toString() { + return getStatusIcon() + " " + description; + } +} From b56907d7ea44b9866d7b8d43874b9f9f8f342566 Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 00:04:53 +0800 Subject: [PATCH 05/15] Level-3 --- src/main/java/Hailey.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index 8e74cd8f1..12eb521e2 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -113,5 +113,4 @@ private static void printGoodbyeMessage() { System.out.println("Bye. Hope to see you again soon!"); printLine(); } -} - +} \ No newline at end of file From 38683e3f2451b4f144ce792607e5879fc97d0e58 Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 16:18:47 +0800 Subject: [PATCH 06/15] A-CodingStandard --- src/main/java/Hailey.java | 10 +++++----- src/main/java/Task.java | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index 12eb521e2..a45346a8b 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -3,7 +3,7 @@ public class Hailey { private static final String LINE_SEPARATOR = "____________________________________________________________"; private static final int MAX_TASKS = 100; - private static Task[] tasks = new Task[MAX_TASKS]; + private static final Task[] tasks = new Task[MAX_TASKS]; private static int taskCount = 0; public static void main(String[] args) { @@ -41,9 +41,9 @@ private static void printWelcomeMessage() { "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n" + - "What can I help you with?"); + "What can I help you with?"); printLine(); - System.out.println("Hello! I'm Hailey\nWhat can I do for you?"); + System.out.println("Hello! I'm Hailey.\nWhat can I do for you?"); printLine(); } @@ -54,9 +54,9 @@ private static void printLine() { private static void addTask(String taskDescription) { if (taskCount < MAX_TASKS) { tasks[taskCount++] = new Task(taskDescription); - System.out.println("added: " + taskDescription); + System.out.println("Task added: " + taskDescription); } else { - System.out.println("Sorry, task list is full!"); + System.out.println("Sorry, the task list is full!"); } printLine(); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index cb4138953..96aa892d1 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -23,4 +23,4 @@ public String getStatusIcon() { public String toString() { return getStatusIcon() + " " + description; } -} +} \ No newline at end of file From 015fb8da509253cde33ea42c06d5ec40b9db2846 Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 16:41:34 +0800 Subject: [PATCH 07/15] Level-4 --- src/main/java/Deadline.java | 18 +++++++ src/main/java/Event.java | 20 +++++++ src/main/java/Hailey.java | 97 +++++++++------------------------- src/main/java/Task.java | 12 ++--- src/main/java/TaskManager.java | 34 ++++++++++++ src/main/java/Todo.java | 10 ++++ 6 files changed, 112 insertions(+), 79 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/TaskManager.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..84be6426f --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,18 @@ +public class Deadline extends Task { + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String getType() { + return "D"; + } + + @Override + public String toString() { + return super.toString() + " (by: " + by + ")"; + } +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..8a5414240 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,20 @@ +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 getType() { + return "E"; + } + + @Override + public String toString() { + return super.toString() + " (from: " + from + " to: " + to + ")"; + } +} diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index a45346a8b..1d1e0c73c 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -1,13 +1,9 @@ import java.util.Scanner; public class Hailey { - private static final String LINE_SEPARATOR = "____________________________________________________________"; - private static final int MAX_TASKS = 100; - private static final Task[] tasks = new Task[MAX_TASKS]; - private static int taskCount = 0; - public static void main(String[] args) { Scanner scanner = new Scanner(System.in); + TaskManager taskManager = new TaskManager(); printWelcomeMessage(); @@ -19,14 +15,29 @@ public static void main(String[] args) { break; } - if (userInput.equals("list")) { - printTasks(); - } else if (userInput.startsWith("mark ")) { - markTask(userInput); - } else if (userInput.startsWith("unmark ")) { - unmarkTask(userInput); - } else { - addTask(userInput); + String[] inputParts = userInput.split("\\s+", 2); + String command = inputParts[0]; + String description = inputParts.length > 1 ? inputParts[1] : ""; + + switch (command) { + case "list": + taskManager.listTasks(); + break; + case "todo": + taskManager.addTask(new Todo(description)); + break; + case "deadline": + String[] deadlineParts = description.split("/by", 2); + taskManager.addTask(new Deadline(deadlineParts[0].trim(), deadlineParts[1].trim())); + break; + case "event": + String[] eventParts = description.split("/from", 2); + String[] eventDateTime = eventParts[1].split("/to", 2); + taskManager.addTask(new Event(eventParts[0].trim(), eventDateTime[0].trim(), eventDateTime[1].trim())); + break; + default: + System.out.println("Invalid command. Please try again."); + printLine(); } } @@ -48,65 +59,7 @@ private static void printWelcomeMessage() { } private static void printLine() { - System.out.println(LINE_SEPARATOR); - } - - private static void addTask(String taskDescription) { - if (taskCount < MAX_TASKS) { - tasks[taskCount++] = new Task(taskDescription); - System.out.println("Task added: " + taskDescription); - } else { - System.out.println("Sorry, the task list is full!"); - } - printLine(); - } - - private static void markTask(String userInput) { - try { - int taskIndex = Integer.parseInt(userInput.split(" ")[1]) - 1; - if (isValidTaskIndex(taskIndex)) { - tasks[taskIndex].markAsDone(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(tasks[taskIndex]); - } else { - System.out.println("Invalid task number!"); - } - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - System.out.println("Invalid command format!"); - } - printLine(); - } - - private static void unmarkTask(String userInput) { - try { - int taskIndex = Integer.parseInt(userInput.split(" ")[1]) - 1; - if (isValidTaskIndex(taskIndex)) { - tasks[taskIndex].markAsNotDone(); - System.out.println("OK, I've marked this task as not done yet:"); - System.out.println(tasks[taskIndex]); - } else { - System.out.println("Invalid task number!"); - } - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - System.out.println("Invalid command format!"); - } - printLine(); - } - - private static void printTasks() { - if (taskCount == 0) { - System.out.println("Task list is empty!"); - } else { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskCount; i++) { - System.out.println((i + 1) + "." + tasks[i]); - } - } - printLine(); - } - - private static boolean isValidTaskIndex(int index) { - return index >= 0 && index < taskCount; + System.out.println("____________________________________________________________"); } private static void printGoodbyeMessage() { diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 96aa892d1..6b99a8148 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,6 +1,6 @@ -public class Task { - private String description; - private boolean isDone; +public abstract class Task { + protected String description; + protected boolean isDone; public Task(String description) { this.description = description; @@ -15,12 +15,10 @@ public void markAsNotDone() { this.isDone = false; } - public String getStatusIcon() { - return (isDone ? "[X]" : "[ ]"); - } + public abstract String getType(); @Override public String toString() { - return getStatusIcon() + " " + description; + return "[" + (isDone ? "X" : " ") + "] " + description; } } \ No newline at end of file diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java new file mode 100644 index 000000000..5caa44565 --- /dev/null +++ b/src/main/java/TaskManager.java @@ -0,0 +1,34 @@ +import java.util.ArrayList; +import java.util.List; + +public class TaskManager { + private List tasks; + + public TaskManager() { + tasks = new ArrayList<>(); + } + + public void addTask(Task task) { + 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."); + printLine(); + } + + public void listTasks() { + if (tasks.isEmpty()) { + System.out.println("Task list is empty!"); + } else { + 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)); + } + } + printLine(); + } + + private void printLine() { + System.out.println("____________________________________________________________"); + } +} diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..9be96271b --- /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 getType() { + return "T"; + } +} From e6a831a8b1dcb0a7dfb128a211bc1dc3ffa7d6b4 Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 16:56:47 +0800 Subject: [PATCH 08/15] A-CodeQuality --- src/main/java/Hailey.java | 60 ++++++++++++++++++++-------------- src/main/java/TaskManager.java | 24 +++++++++++--- 2 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index 1d1e0c73c..347c97ce6 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -1,6 +1,12 @@ import java.util.Scanner; public class Hailey { + private static final String COMMAND_BYE = "bye"; + private static final String COMMAND_LIST = "list"; + private static final String COMMAND_TODO = "todo"; + private static final String COMMAND_DEADLINE = "deadline"; + private static final String COMMAND_EVENT = "event"; + public static void main(String[] args) { Scanner scanner = new Scanner(System.in); TaskManager taskManager = new TaskManager(); @@ -11,40 +17,44 @@ public static void main(String[] args) { String userInput = scanner.nextLine().trim().toLowerCase(); printLine(); - if (userInput.equals("bye")) { + if (userInput.equals(COMMAND_BYE)) { break; } - String[] inputParts = userInput.split("\\s+", 2); - String command = inputParts[0]; - String description = inputParts.length > 1 ? inputParts[1] : ""; - - switch (command) { - case "list": - taskManager.listTasks(); - break; - case "todo": - taskManager.addTask(new Todo(description)); - break; - case "deadline": - String[] deadlineParts = description.split("/by", 2); - taskManager.addTask(new Deadline(deadlineParts[0].trim(), deadlineParts[1].trim())); - break; - case "event": - String[] eventParts = description.split("/from", 2); - String[] eventDateTime = eventParts[1].split("/to", 2); - taskManager.addTask(new Event(eventParts[0].trim(), eventDateTime[0].trim(), eventDateTime[1].trim())); - break; - default: - System.out.println("Invalid command. Please try again."); - printLine(); - } + processUserInput(userInput, taskManager); } printGoodbyeMessage(); scanner.close(); } + private static void processUserInput(String userInput, TaskManager taskManager) { + String[] inputParts = userInput.split("\\s+", 2); + String command = inputParts[0]; + String description = inputParts.length > 1 ? inputParts[1] : ""; + + switch (command) { + case COMMAND_LIST: + taskManager.listTasks(); + break; + case COMMAND_TODO: + taskManager.addTodoTask(description); + break; + case COMMAND_DEADLINE: + String[] deadlineParts = description.split("/by", 2); + taskManager.addDeadlineTask(deadlineParts[0].trim(), deadlineParts[1].trim()); + break; + case COMMAND_EVENT: + String[] eventParts = description.split("/from", 2); + String[] eventDateTime = eventParts[1].split("/to", 2); + taskManager.addEventTask(eventParts[0].trim(), eventDateTime[0].trim(), eventDateTime[1].trim()); + break; + default: + System.out.println("Invalid command. Please try again."); + printLine(); + } + } + private static void printWelcomeMessage() { System.out.println("Hello from\n" + " ____ _ \n" + diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 5caa44565..ce9c365a2 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -2,16 +2,32 @@ import java.util.List; public class TaskManager { - private List tasks; + private final List tasks; public TaskManager() { tasks = new ArrayList<>(); } - public void addTask(Task task) { - tasks.add(task); + public void addTodoTask(String description) { + tasks.add(new Todo(description)); System.out.println("Got it. I've added this task:"); - System.out.println(" " + task); + System.out.println(" " + tasks.get(tasks.size() - 1)); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + printLine(); + } + + public void addDeadlineTask(String description, String by) { + tasks.add(new Deadline(description, by)); + System.out.println("Got it. I've added this task:"); + System.out.println(" " + tasks.get(tasks.size() - 1)); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + printLine(); + } + + public void addEventTask(String description, String from, String to) { + tasks.add(new Event(description, from, to)); + System.out.println("Got it. I've added this task:"); + System.out.println(" " + tasks.get(tasks.size() - 1)); System.out.println("Now you have " + tasks.size() + " tasks in the list."); printLine(); } From bc6bd763d5671b257796d6768222226b2ded85b6 Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 17:36:54 +0800 Subject: [PATCH 09/15] Level-5 --- src/main/java/Deadline.java | 10 +-- src/main/java/DukeException.java | 5 ++ src/main/java/Event.java | 11 +--- src/main/java/Hailey.java | 107 ++++++++++++++++++------------- src/main/java/Task.java | 15 ++--- src/main/java/TaskManager.java | 53 +++++++++++---- src/main/java/Todo.java | 6 +- 7 files changed, 126 insertions(+), 81 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 84be6426f..56c1f1627 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,18 +1,14 @@ public class Deadline extends Task { - protected String by; + private String by; public Deadline(String description, String by) { super(description); this.by = by; } - @Override - public String getType() { - return "D"; - } - @Override public String toString() { - return super.toString() + " (by: " + by + ")"; + return "[D][" + getStatusIcon() + "] " + description + " (by: " + by + ")"; } } + diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..d2789a1f8 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,5 @@ +public class DukeException extends Exception { + public DukeException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 8a5414240..dbe8f8467 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,6 +1,6 @@ public class Event extends Task { - protected String from; - protected String to; + private String from; + private String to; public Event(String description, String from, String to) { super(description); @@ -8,13 +8,8 @@ public Event(String description, String from, String to) { this.to = to; } - @Override - public String getType() { - return "E"; - } - @Override public String toString() { - return super.toString() + " (from: " + from + " to: " + to + ")"; + return "[E][" + getStatusIcon() + "] " + description + " (from: " + from + " to: " + to + ")"; } } diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index 347c97ce6..8ffd52756 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -1,79 +1,100 @@ import java.util.Scanner; public class Hailey { - private static final String COMMAND_BYE = "bye"; - private static final String COMMAND_LIST = "list"; - private static final String COMMAND_TODO = "todo"; - private static final String COMMAND_DEADLINE = "deadline"; - private static final String COMMAND_EVENT = "event"; + private final TaskManager taskManager; - public static void main(String[] args) { + public Hailey() { + taskManager = new TaskManager(); + } + + public void run() { Scanner scanner = new Scanner(System.in); - TaskManager taskManager = new TaskManager(); printWelcomeMessage(); while (true) { - String userInput = scanner.nextLine().trim().toLowerCase(); + String userInput = scanner.nextLine().trim(); printLine(); - if (userInput.equals(COMMAND_BYE)) { - break; + try { + processUserInput(userInput); + } catch (DukeException e) { + System.out.println(e.getMessage()); } - processUserInput(userInput, taskManager); + printLine(); + + if (userInput.equalsIgnoreCase("bye")) { + break; + } } - printGoodbyeMessage(); scanner.close(); } - private static void processUserInput(String userInput, TaskManager taskManager) { - String[] inputParts = userInput.split("\\s+", 2); - String command = inputParts[0]; - String description = inputParts.length > 1 ? inputParts[1] : ""; + private void processUserInput(String userInput) throws DukeException { + String[] inputParts = userInput.split(" ", 2); + String command = inputParts[0].toLowerCase(); + String argument = inputParts.length > 1 ? inputParts[1] : ""; switch (command) { - case COMMAND_LIST: + case "todo": + taskManager.addTodoTask(argument); + break; + case "deadline": + String[] deadlineArgs = argument.split("/by", 2); + if (deadlineArgs.length != 2) { + throw new DukeException("OOPS!!! Please provide a description and deadline for the task."); + } + taskManager.addDeadlineTask(deadlineArgs[0].trim(), deadlineArgs[1].trim()); + break; + case "event": + String[] eventArgs = argument.split("/from", 2); + if (eventArgs.length != 2) { + throw new DukeException("OOPS!!! Please provide a description and time period for the event."); + } + String[] eventTimeArgs = eventArgs[1].split("/to", 2); + if (eventTimeArgs.length != 2) { + throw new DukeException("OOPS!!! Please provide a valid time period for the event."); + } + taskManager.addEventTask(eventArgs[0].trim(), eventTimeArgs[0].trim(), eventTimeArgs[1].trim()); + break; + case "list": taskManager.listTasks(); break; - case COMMAND_TODO: - taskManager.addTodoTask(description); + case "done": + int taskIndex = Integer.parseInt(argument) - 1; + taskManager.markTaskAsDone(taskIndex); break; - case COMMAND_DEADLINE: - String[] deadlineParts = description.split("/by", 2); - taskManager.addDeadlineTask(deadlineParts[0].trim(), deadlineParts[1].trim()); + case "delete": + int taskIndexToDelete = Integer.parseInt(argument) - 1; + taskManager.deleteTask(taskIndexToDelete); break; - case COMMAND_EVENT: - String[] eventParts = description.split("/from", 2); - String[] eventDateTime = eventParts[1].split("/to", 2); - taskManager.addEventTask(eventParts[0].trim(), eventDateTime[0].trim(), eventDateTime[1].trim()); + case "find": + taskManager.findTasks(argument); break; + case "bye": + printGoodbyeMessage(); + return; default: - System.out.println("Invalid command. Please try again."); - printLine(); + throw new DukeException("OOPS!!! I'm sorry, but I don't know what that means :-("); } } - private static void printWelcomeMessage() { - System.out.println("Hello from\n" + - " ____ _ \n" + - "| _ \\ _ _| | _____ \n" + - "| | | | | | | |/ / _ \\\n" + - "| |_| | |_| | < __/\n" + - "|____/ \\__,_|_|\\_\\___|\n" + - "What can I help you with?"); - printLine(); - System.out.println("Hello! I'm Hailey.\nWhat can I do for you?"); - printLine(); + private void printWelcomeMessage() { + System.out.println("Hello! I'm Hailey"); + System.out.println("What can I do for you?"); + } + + private void printGoodbyeMessage() { + System.out.println("Bye. Hope to see you again soon!"); } - private static void printLine() { + private void printLine() { System.out.println("____________________________________________________________"); } - private static void printGoodbyeMessage() { - System.out.println("Bye. Hope to see you again soon!"); - printLine(); + public static void main(String[] args) { + new Hailey().run(); } } \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 6b99a8148..2c945f693 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -8,17 +8,16 @@ public Task(String description) { } public void markAsDone() { - this.isDone = true; + isDone = true; } - public void markAsNotDone() { - this.isDone = false; + public String getDescription() { + return description; } - public abstract String getType(); - - @Override - public String toString() { - return "[" + (isDone ? "X" : " ") + "] " + description; + public String getStatusIcon() { + return (isDone ? "X" : " "); } + + public abstract String toString(); } \ No newline at end of file diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index ce9c365a2..3d644da36 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -13,7 +13,6 @@ public void addTodoTask(String description) { System.out.println("Got it. I've added this task:"); System.out.println(" " + tasks.get(tasks.size() - 1)); System.out.println("Now you have " + tasks.size() + " tasks in the list."); - printLine(); } public void addDeadlineTask(String description, String by) { @@ -21,7 +20,6 @@ public void addDeadlineTask(String description, String by) { System.out.println("Got it. I've added this task:"); System.out.println(" " + tasks.get(tasks.size() - 1)); System.out.println("Now you have " + tasks.size() + " tasks in the list."); - printLine(); } public void addEventTask(String description, String from, String to) { @@ -29,22 +27,53 @@ public void addEventTask(String description, String from, String to) { System.out.println("Got it. I've added this task:"); System.out.println(" " + tasks.get(tasks.size() - 1)); System.out.println("Now you have " + tasks.size() + " tasks in the list."); - printLine(); } public void listTasks() { if (tasks.isEmpty()) { - System.out.println("Task list is empty!"); - } else { - 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)); - } + System.out.println("You have no tasks in the list."); + return; + } + 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)); + } + } + + public void markTaskAsDone(int index) { + if (index < 0 || index >= tasks.size()) { + System.out.println("Invalid task number."); + return; + } + tasks.get(index).markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(" " + tasks.get(index)); + } + + public void deleteTask(int index) { + if (index < 0 || index >= tasks.size()) { + System.out.println("Invalid task number."); + return; } - printLine(); + System.out.println("Noted. I've removed this task:"); + System.out.println(" " + tasks.get(index)); + tasks.remove(index); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } - private void printLine() { - System.out.println("____________________________________________________________"); + public void findTasks(String keyword) { + int count = 0; + for (int i = 0; i < tasks.size(); i++) { + if (tasks.get(i).getDescription().contains(keyword)) { + if (count == 0) { + System.out.println("Here are the matching tasks in your list:"); + } + System.out.println((count + 1) + ". " + tasks.get(i)); + count++; + } + } + if (count == 0) { + System.out.println("No matching tasks found."); + } } } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index 9be96271b..abb0a4dd7 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -4,7 +4,7 @@ public Todo(String description) { } @Override - public String getType() { - return "T"; + public String toString() { + return "[T][" + getStatusIcon() + "] " + description; } -} +} \ No newline at end of file From 130dc78129dc60da90cbe24e81f96897beedccdd Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 22:32:12 +0800 Subject: [PATCH 10/15] Level-6 --- src/main/java/Hailey.java | 3 +-- src/main/java/TaskManager.java | 34 +++++++++------------------------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index 8ffd52756..fced0ab7a 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -96,5 +96,4 @@ private void printLine() { public static void main(String[] args) { new Hailey().run(); - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 3d644da36..b0a9902a3 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -8,24 +8,10 @@ public TaskManager() { tasks = new ArrayList<>(); } - public void addTodoTask(String description) { - tasks.add(new Todo(description)); + public void addTask(Task task) { + tasks.add(task); System.out.println("Got it. I've added this task:"); - System.out.println(" " + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - - public void addDeadlineTask(String description, String by) { - tasks.add(new Deadline(description, by)); - System.out.println("Got it. I've added this task:"); - System.out.println(" " + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - - public void addEventTask(String description, String from, String to) { - tasks.add(new Event(description, from, to)); - System.out.println("Got it. I've added this task:"); - System.out.println(" " + tasks.get(tasks.size() - 1)); + System.out.println(" " + task); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } @@ -40,24 +26,22 @@ public void listTasks() { } } - public void markTaskAsDone(int index) { + public void markTaskAsDone(int index) throws DukeException { if (index < 0 || index >= tasks.size()) { - System.out.println("Invalid task number."); - return; + throw new DukeException("Invalid task number."); } tasks.get(index).markAsDone(); System.out.println("Nice! I've marked this task as done:"); System.out.println(" " + tasks.get(index)); } - public void deleteTask(int index) { + public void deleteTask(int index) throws DukeException { if (index < 0 || index >= tasks.size()) { - System.out.println("Invalid task number."); - return; + throw new DukeException("Invalid task number."); } + Task removedTask = tasks.remove(index); System.out.println("Noted. I've removed this task:"); - System.out.println(" " + tasks.get(index)); - tasks.remove(index); + System.out.println(" " + removedTask); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } From 70212424c03bd6e035359a5cb884b47189eba21a Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 22:35:45 +0800 Subject: [PATCH 11/15] Level-7 --- src/main/java/TaskManager.java | 84 +++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index b0a9902a3..aeb889040 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -1,63 +1,71 @@ +import java.io.*; import java.util.ArrayList; import java.util.List; public class TaskManager { private final List tasks; + private final String filePath; - public TaskManager() { + public TaskManager(String filePath) { + this.filePath = filePath; tasks = new ArrayList<>(); + loadTasks(); // Load tasks from file when TaskManager is instantiated } public void addTask(Task task) { tasks.add(task); + saveTasksToFile(); // Save tasks 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 listTasks() { - if (tasks.isEmpty()) { - System.out.println("You have no tasks in the list."); - return; - } - 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)); - } - } + // Add other methods for managing tasks... - public void markTaskAsDone(int index) throws DukeException { - if (index < 0 || index >= tasks.size()) { - throw new DukeException("Invalid task number."); - } - tasks.get(index).markAsDone(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(" " + tasks.get(index)); - } + private void loadTasks() { + try { + File file = new File(filePath); + if (!file.exists()) { + return; // If file doesn't exist, do nothing + } - public void deleteTask(int index) throws DukeException { - if (index < 0 || index >= tasks.size()) { - throw new DukeException("Invalid task number."); + BufferedReader reader = new BufferedReader(new FileReader(file)); + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(" \\| "); + String type = parts[0]; + boolean isDone = parts[1].equals("1"); + String description = parts[2]; + switch (type) { + case "T": + tasks.add(new Todo(description, isDone)); + break; + case "D": + String by = parts[3]; + tasks.add(new Deadline(description, by, isDone)); + break; + case "E": + String from = parts[3]; + String to = parts[4]; + tasks.add(new Event(description, from, to, isDone)); + break; + } + } + reader.close(); + } catch (IOException e) { + System.out.println("Error loading tasks: " + e.getMessage()); } - Task removedTask = tasks.remove(index); - 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 findTasks(String keyword) { - int count = 0; - for (int i = 0; i < tasks.size(); i++) { - if (tasks.get(i).getDescription().contains(keyword)) { - if (count == 0) { - System.out.println("Here are the matching tasks in your list:"); - } - System.out.println((count + 1) + ". " + tasks.get(i)); - count++; + private void saveTasksToFile() { + try { + PrintWriter writer = new PrintWriter(filePath); + for (Task task : tasks) { + writer.println(task.toFileString()); } - } - if (count == 0) { - System.out.println("No matching tasks found."); + writer.close(); + } catch (FileNotFoundException e) { + System.out.println("Error saving tasks: " + e.getMessage()); } } } From edca2d7c281b20dba8ca26dac0cf59be5759349a Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 22:47:14 +0800 Subject: [PATCH 12/15] Level-7 --- src/main/java/Hailey.java | 117 ++++++++++++++++----------------- src/main/java/TaskManager.java | 44 +++++++++---- 2 files changed, 86 insertions(+), 75 deletions(-) diff --git a/src/main/java/Hailey.java b/src/main/java/Hailey.java index fced0ab7a..4b0ef744b 100644 --- a/src/main/java/Hailey.java +++ b/src/main/java/Hailey.java @@ -1,10 +1,10 @@ import java.util.Scanner; public class Hailey { - private final TaskManager taskManager; + private TaskManager taskManager; public Hailey() { - taskManager = new TaskManager(); + this.taskManager = new TaskManager("./data/tasks.txt"); } public void run() { @@ -16,84 +16,77 @@ public void run() { String userInput = scanner.nextLine().trim(); printLine(); - try { - processUserInput(userInput); - } catch (DukeException e) { - System.out.println(e.getMessage()); - } - - printLine(); - if (userInput.equalsIgnoreCase("bye")) { + printGoodbyeMessage(); break; - } - } - - scanner.close(); - } - - private void processUserInput(String userInput) throws DukeException { - String[] inputParts = userInput.split(" ", 2); - String command = inputParts[0].toLowerCase(); - String argument = inputParts.length > 1 ? inputParts[1] : ""; - - switch (command) { - case "todo": - taskManager.addTodoTask(argument); - break; - case "deadline": - String[] deadlineArgs = argument.split("/by", 2); - if (deadlineArgs.length != 2) { - throw new DukeException("OOPS!!! Please provide a description and deadline for the task."); + } else if (userInput.equalsIgnoreCase("list")) { + taskManager.printTasks(); + } else if (userInput.startsWith("todo")) { + taskManager.addTodoTask(userInput.substring("todo".length()).trim()); + } else if (userInput.startsWith("deadline")) { + // Parse deadline command and add deadline task + String[] parts = userInput.split("/by", 2); + if (parts.length != 2) { + printErrorMessage("Invalid deadline format. Please use: deadline /by "); + } else { + String description = parts[0].substring("deadline".length()).trim(); + String by = parts[1].trim(); + taskManager.addDeadlineTask(description, by); } - taskManager.addDeadlineTask(deadlineArgs[0].trim(), deadlineArgs[1].trim()); - break; - case "event": - String[] eventArgs = argument.split("/from", 2); - if (eventArgs.length != 2) { - throw new DukeException("OOPS!!! Please provide a description and time period for the event."); + } else if (userInput.startsWith("event")) { + // Parse event command and add event task + String[] parts = userInput.split("/from", 2); + if (parts.length != 2) { + printErrorMessage("Invalid event format. Please use: event /from /to "); + } else { + String description = parts[0].substring("event".length()).trim(); + String[] timeParts = parts[1].split("/to", 2); + if (timeParts.length != 2) { + printErrorMessage("Invalid event format. Please use: event /from /to "); + } else { + String from = timeParts[0].trim(); + String to = timeParts[1].trim(); + taskManager.addEventTask(description, from, to); + } } - String[] eventTimeArgs = eventArgs[1].split("/to", 2); - if (eventTimeArgs.length != 2) { - throw new DukeException("OOPS!!! Please provide a valid time period for the event."); + } else if (userInput.startsWith("delete")) { + // Parse delete command and delete task + String indexStr = userInput.substring("delete".length()).trim(); + try { + int index = Integer.parseInt(indexStr); + taskManager.deleteTask(index - 1); // Adjust index to 0-based + } catch (NumberFormatException e) { + printErrorMessage("Invalid index for delete command. Please enter a valid task number."); } - taskManager.addEventTask(eventArgs[0].trim(), eventTimeArgs[0].trim(), eventTimeArgs[1].trim()); - break; - case "list": - taskManager.listTasks(); - break; - case "done": - int taskIndex = Integer.parseInt(argument) - 1; - taskManager.markTaskAsDone(taskIndex); - break; - case "delete": - int taskIndexToDelete = Integer.parseInt(argument) - 1; - taskManager.deleteTask(taskIndexToDelete); - break; - case "find": - taskManager.findTasks(argument); - break; - case "bye": - printGoodbyeMessage(); - return; - default: - throw new DukeException("OOPS!!! I'm sorry, but I don't know what that means :-("); + } else { + printErrorMessage("I'm sorry, but I don't know what that means :-("); + } } + + scanner.close(); } private void printWelcomeMessage() { System.out.println("Hello! I'm Hailey"); System.out.println("What can I do for you?"); + printLine(); + } + + private void printLine() { + System.out.println("____________________________________________________________"); } private void printGoodbyeMessage() { System.out.println("Bye. Hope to see you again soon!"); + printLine(); } - private void printLine() { - System.out.println("____________________________________________________________"); + private void printErrorMessage(String message) { + System.out.println("OOPS!!! " + message); + printLine(); } public static void main(String[] args) { new Hailey().run(); - } \ No newline at end of file + } +} \ No newline at end of file diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index aeb889040..9f3d76ca1 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -1,6 +1,6 @@ -import java.io.*; import java.util.ArrayList; import java.util.List; +import java.io.*; public class TaskManager { private final List tasks; @@ -9,24 +9,42 @@ public class TaskManager { public TaskManager(String filePath) { this.filePath = filePath; tasks = new ArrayList<>(); - loadTasks(); // Load tasks from file when TaskManager is instantiated + loadTasks(); + } + + public void addTodoTask(String description) { + tasks.add(new TodoTask(description)); + saveTasksToFile(); + } + + public void addDeadlineTask(String description, String by) { + tasks.add(new DeadlineTask(description, by)); + saveTasksToFile(); } - public void addTask(Task task) { - tasks.add(task); - saveTasksToFile(); // Save tasks 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 addEventTask(String description, String from, String to) { + tasks.add(new EventTask(description, from, to)); + saveTasksToFile(); } - // Add other methods for managing tasks... + public void deleteTask(int index) { + if (index >= 0 && index < tasks.size()) { + tasks.remove(index); + saveTasksToFile(); + } + } + + public void printTasks() { + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + ". " + tasks.get(i)); + } + } private void loadTasks() { try { File file = new File(filePath); if (!file.exists()) { - return; // If file doesn't exist, do nothing + return; } BufferedReader reader = new BufferedReader(new FileReader(file)); @@ -38,16 +56,16 @@ private void loadTasks() { String description = parts[2]; switch (type) { case "T": - tasks.add(new Todo(description, isDone)); + tasks.add(new TodoTask(description, isDone)); break; case "D": String by = parts[3]; - tasks.add(new Deadline(description, by, isDone)); + tasks.add(new DeadlineTask(description, by, isDone)); break; case "E": String from = parts[3]; String to = parts[4]; - tasks.add(new Event(description, from, to, isDone)); + tasks.add(new EventTask(description, from, to, isDone)); break; } } From 8addca472a0cfd1cc1db598a8976bf33ea1b4db7 Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 23:29:47 +0800 Subject: [PATCH 13/15] A-MoreOOP --- src/main/MANIFEST.MF | 2 + src/main/java/META-INF/MANIFEST.MF | 3 ++ src/main/java/Parser.java | 5 +++ src/main/java/Storage.java | 60 ++++++++++++++++++++++++++++++ src/main/java/Ui.java | 24 ++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 src/main/MANIFEST.MF create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/Parser.java create mode 100644 src/main/java/Storage.java create mode 100644 src/main/java/Ui.java diff --git a/src/main/MANIFEST.MF b/src/main/MANIFEST.MF new file mode 100644 index 000000000..d9670b91e --- /dev/null +++ b/src/main/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 +Main-Class: Hailey diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..80f38673f --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Hailey + diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 000000000..2cca951e8 --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,5 @@ +public class Parser { + public static Command parse(String fullCommand) throws DukeException { + // Your parsing logic here + } +} diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 000000000..cfd8f09ce --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,60 @@ +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +public class Storage { + private final String filePath; + + public Storage(String filePath) { + this.filePath = filePath; + } + + public List load() throws DukeException { + List tasks = new ArrayList<>(); + try { + File file = new File(filePath); + if (!file.exists()) { + return tasks; + } + + BufferedReader reader = new BufferedReader(new FileReader(file)); + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(" \\| "); + String type = parts[0]; + boolean isDone = parts[1].equals("1"); + String description = parts[2]; + switch (type) { + case "T": + tasks.add(new TodoTask(description, isDone)); + break; + case "D": + String by = parts[3]; + tasks.add(new DeadlineTask(description, by, isDone)); + break; + case "E": + String from = parts[3]; + String to = parts[4]; + tasks.add(new EventTask(description, from, to, isDone)); + break; + } + } + reader.close(); + } catch (IOException e) { + throw new DukeException("Error loading tasks: " + e.getMessage()); + } + return tasks; + } + + public void save(List tasks) throws DukeException { + try { + PrintWriter writer = new PrintWriter(filePath); + for (Task task : tasks) { + writer.println(task.toFileString()); + } + writer.close(); + } catch (FileNotFoundException e) { + throw new DukeException("Error saving tasks: " + e.getMessage()); + } + } +} diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 000000000..5d1d3fd0f --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,24 @@ +public class Ui { + public void showWelcome() { + System.out.println("Hello! I'm Hailey"); + System.out.println("What can I do for you?"); + printLine(); + } + + public void showLine() { + System.out.println("____________________________________________________________"); + } + + public void showError(String message) { + System.out.println("OOPS!!! " + message); + printLine(); + } + + public void showLoadingError() { + System.out.println("Error loading tasks. Starting with an empty task list."); + } + + private void printLine() { + System.out.println("____________________________________________________________"); + } +} \ No newline at end of file From 10b4ebd3b1529d190bbf16d6963215ce9008c9d6 Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Sun, 10 Mar 2024 23:44:36 +0800 Subject: [PATCH 14/15] A-UserGuide --- docs/README.md | 61 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..6adf15930 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,58 @@ -# User Guide +# Hailey User Guide -## Features +Hailey is a command-line chatbot application designed to help you manage your tasks efficiently. -### Feature-ABC +## Getting Started -Description of the feature. +1. Make sure you have Java installed on your computer. +2. Clone or download the Hailey repository to your local machine. +3. Open a terminal in the directory where you saved the Hailey files. +4. Compile the Java files using the following command: + ``` + javac *.java + ``` +5. Run the application using the following command: + ``` + java Hailey + ``` -### Feature-XYZ +## Quick Reference -Description of the feature. +| Command | Parameters | Explanation | +|------------|------------------------------------|----------------------------------------| +| `list` | None | Displays the list of tasks. | +| `todo` | | Adds a todo task to the list. | +| `deadline` | /by | Adds a deadline task to the list. | +| `event` | /from /to | Adds an event task to the list. | +| `delete` | | Deletes the task at the specified index. | +| `find` | | Searches for tasks containing the specified keyword. | +| `bye` | None | Exits the application. | -## Usage +## Features + +### Add Tasks + +Hailey allows you to add different types of tasks: +- Todo tasks +- Deadline tasks +- Event tasks + +### List Tasks -### `Keyword` - Describe action +You can view all your tasks by typing `list`. Hailey will display the tasks along with their descriptions and statuses. -Describe the action and its outcome. +### Delete Tasks -Example of usage: +To remove a task from your list, use the `delete` command followed by the index of the task you wish to delete. -`keyword (optional arguments)` +### Find Tasks -Expected outcome: +If you need to find a specific task, use the `find` command followed by a keyword. Hailey will display all tasks containing the keyword. -Description of the outcome. +### Exiting Hailey + +To exit Hailey, simply type `bye`. Hailey will bid you farewell and close the application. + +## Usage -``` -expected output -``` +### Adding a Todo Task From 1641ec0e824fa995784ecd1d6b1e136433240d2a Mon Sep 17 00:00:00 2001 From: hailey-jung Date: Fri, 15 Mar 2024 16:35:10 +0800 Subject: [PATCH 15/15] A-UserGuide --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 6adf15930..5d31c5723 100644 --- a/docs/README.md +++ b/docs/README.md @@ -55,4 +55,4 @@ To exit Hailey, simply type `bye`. Hailey will bid you farewell and close the ap ## Usage -### Adding a Todo Task +### Adding a Todo Task \ No newline at end of file