From 4245a0d516d2c6b58de969262a973be1f3e56514 Mon Sep 17 00:00:00 2001 From: niha81 Date: Thu, 23 Jan 2025 17:23:12 +0800 Subject: [PATCH 01/11] level 0 done --- README.md | 4 ++-- docs/README.md | 2 +- src/main/java/Duke.java | 10 ---------- src/main/java/Woogie.java | 9 +++++++++ text-ui-test/runtest.bat | 2 +- 5 files changed, 13 insertions(+), 14 deletions(-) delete mode 100644 src/main/java/Duke.java create mode 100644 src/main/java/Woogie.java diff --git a/README.md b/README.md index af0309a9e..26fb87d2e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# Woogie 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 17, update Intellij to the most recent version. 1. If there are any further prompts, accept the defaults. 1. Configure the project to use **JDK 17** (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. -1. 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: +1. After that, locate the `src/main/java/Woogie.java` file, right-click it, and choose `Run Woogie.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/docs/README.md b/docs/README.md index 47b9f984f..5a283dac3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,4 @@ -# Duke User Guide +# Woogie User Guide // Update the title above to match the actual product name 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/Woogie.java b/src/main/java/Woogie.java new file mode 100644 index 000000000..c8c73e9d3 --- /dev/null +++ b/src/main/java/Woogie.java @@ -0,0 +1,9 @@ +public class Woogie { + public static void main(String[] args) { + String line = "\n----------------------------------------\n"; + String printContent = line + "Hello! I'm Woogie\n" + + "What can I do for you?" + line + "Bye. Hope to see you again soon!" + line; + System.out.println(printContent); + + } +} diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..a8f8679f5 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 Woogie < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From 917a4a62ca127ae4f22d16b7296ee01dc4da907a Mon Sep 17 00:00:00 2001 From: niha81 Date: Thu, 23 Jan 2025 17:53:27 +0800 Subject: [PATCH 02/11] level 1 done --- src/main/java/Woogie.java | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index c8c73e9d3..e42fab890 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -1,9 +1,26 @@ +import java.util.Scanner; + public class Woogie { public static void main(String[] args) { String line = "\n----------------------------------------\n"; - String printContent = line + "Hello! I'm Woogie\n" + - "What can I do for you?" + line + "Bye. Hope to see you again soon!" + line; - System.out.println(printContent); + String greeting = line + "Hello! I'm Woogie\n" + + "What can I do for you?" + line; + + System.out.println(greeting); + + Scanner scanner = new Scanner(System.in); + + while (true) { + String input = scanner.nextLine(); + + if (input.equalsIgnoreCase("bye")) { + System.out.println(line + "Bye. Hope to see you again soon!" + line); + break; + } + + System.out.println(line + input + line); + } + scanner.close(); } } From ae93e79d25726a4bd94bde9c2c0c047faa4bb82c Mon Sep 17 00:00:00 2001 From: niha81 Date: Thu, 23 Jan 2025 18:32:36 +0800 Subject: [PATCH 03/11] add, list --- src/main/java/Woogie.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index e42fab890..9c45f2b4b 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -8,6 +8,9 @@ public static void main(String[] args) { System.out.println(greeting); + String[] tasks = new String[100]; + int taskCount = 0; + Scanner scanner = new Scanner(System.in); while (true) { @@ -18,7 +21,25 @@ public static void main(String[] args) { break; } - System.out.println(line + input + line); + if (input.equalsIgnoreCase("list")) { + if (taskCount == 0) { + System.out.println(line + "nothing here yet TT" + line); + } else { + String taskList = ""; + for (int i = 0; i < taskCount; i++) { + taskList = taskList + (i+1) + ". " + tasks[i] + "\n"; + } + System.out.println(line + taskList + line); + } + } else { + if (taskCount < 100) { + tasks[taskCount] = input; + taskCount++; + System.out.println(line + "added: " + input + line); + } else { + System.out.println(line + "no more space <3" + line); + } + } } scanner.close(); From 68da7d61ca90a2ef37ba2cfcc87f73356b7b63e6 Mon Sep 17 00:00:00 2001 From: niha81 Date: Thu, 23 Jan 2025 23:00:47 +0800 Subject: [PATCH 04/11] mark done --- src/main/java/Woogie.java | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index 9c45f2b4b..23127dc12 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -10,6 +10,11 @@ public static void main(String[] args) { String[] tasks = new String[100]; int taskCount = 0; + Boolean statuses[] = new Boolean[100]; + + for (int i = 0; i < 100; i++) { + statuses[i] = false; + } Scanner scanner = new Scanner(System.in); @@ -26,11 +31,43 @@ public static void main(String[] args) { System.out.println(line + "nothing here yet TT" + line); } else { String taskList = ""; + for (int i = 0; i < taskCount; i++) { - taskList = taskList + (i+1) + ". " + tasks[i] + "\n"; + String state = statuses[i] ? "X" : " "; + taskList = taskList + (i+1) + ".[" + state + "] " + tasks[i] + "\n"; } System.out.println(line + taskList + line); } + } else if (input.startsWith("mark")) { + String[] parts = input.split(" "); + if (parts.length < 2) { + System.out.println(line + "INVALID! Pls specify task number :)" + line); + continue; + } + int taskIndex = Integer.parseInt(parts[1]) - 1; + + if (taskIndex < 0 || taskIndex >= taskCount) { + System.out.println(line + "pls choose a task between 1 and " + taskCount + "." + line); + continue; + } + + statuses[taskIndex] = true; + System.out.println(line + "Nice! I've marked this task as done: \n [X] " + tasks[taskIndex] + line); + } else if (input.startsWith("unmark")) { + String[] parts = input.split(" "); + if (parts.length < 2) { + System.out.println(line + "INVALID! Pls specify task number :)" + line); + continue; + } + int taskIndex = Integer.parseInt(parts[1]) - 1; + + if (taskIndex < 0 || taskIndex >= taskCount) { + System.out.println(line + "pls choose a task between 1 and " + taskCount + "." + line); + continue; + } + + statuses[taskIndex] = false; + System.out.println(line + "Ok, I've marked this task as not done yet: \n [ ] " + tasks[taskIndex] + line); } else { if (taskCount < 100) { tasks[taskCount] = input; From ce47d5d008da6a2c57bebbdec21fc29371601b03 Mon Sep 17 00:00:00 2001 From: niha81 Date: Fri, 24 Jan 2025 01:13:10 +0800 Subject: [PATCH 05/11] todo, event, deadline --- src/main/java/Deadline.java | 13 ++++++++ src/main/java/Event.java | 15 +++++++++ src/main/java/Task.java | 23 +++++++++++++ src/main/java/ToDo.java | 10 ++++++ src/main/java/Woogie.java | 65 +++++++++++++++++++++++-------------- 5 files changed, 102 insertions(+), 24 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Task.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..b7d4bd808 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,13 @@ +public class Deadline extends Task { + private 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..92b959b9f --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,15 @@ +public class Event extends Task { + private String from; + private 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/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..84adae413 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,23 @@ +public class Task { + private String description; + private boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public void markDone() { + this.isDone = true; + } + + public void markUndone() { + this.isDone = false; + } + + @Override + public String toString() { + String status = isDone ? "X" : " "; + return "[" + status + "] " + description; + } +} diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java new file mode 100644 index 000000000..ee72ef43c --- /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(); + } +} diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index 23127dc12..ffebb3a72 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -3,18 +3,11 @@ public class Woogie { public static void main(String[] args) { String line = "\n----------------------------------------\n"; - String greeting = line + "Hello! I'm Woogie\n" + - "What can I do for you?" + line; - + String greeting = line + "Hello! I'm Woogie\n" + "What can I do for you?" + line; System.out.println(greeting); - String[] tasks = new String[100]; + Task[] tasks = new Task[100]; int taskCount = 0; - Boolean statuses[] = new Boolean[100]; - - for (int i = 0; i < 100; i++) { - statuses[i] = false; - } Scanner scanner = new Scanner(System.in); @@ -30,13 +23,11 @@ public static void main(String[] args) { if (taskCount == 0) { System.out.println(line + "nothing here yet TT" + line); } else { - String taskList = ""; - + System.out.println(line); for (int i = 0; i < taskCount; i++) { - String state = statuses[i] ? "X" : " "; - taskList = taskList + (i+1) + ".[" + state + "] " + tasks[i] + "\n"; + System.out.println((i + 1) + "." + tasks[i]); } - System.out.println(line + taskList + line); + System.out.println(line); } } else if (input.startsWith("mark")) { String[] parts = input.split(" "); @@ -51,8 +42,8 @@ public static void main(String[] args) { continue; } - statuses[taskIndex] = true; - System.out.println(line + "Nice! I've marked this task as done: \n [X] " + tasks[taskIndex] + line); + tasks[taskIndex].markDone(); + System.out.println(line + "Nice! I've marked this task as done: \n " + tasks[taskIndex] + line); } else if (input.startsWith("unmark")) { String[] parts = input.split(" "); if (parts.length < 2) { @@ -66,16 +57,42 @@ public static void main(String[] args) { continue; } - statuses[taskIndex] = false; - System.out.println(line + "Ok, I've marked this task as not done yet: \n [ ] " + tasks[taskIndex] + line); - } else { - if (taskCount < 100) { - tasks[taskCount] = input; - taskCount++; - System.out.println(line + "added: " + input + line); - } else { + tasks[taskIndex].markUndone(); + System.out.println(line + "Ok, I've marked this task as not done yet: \n " + tasks[taskIndex] + line); + } else if (input.startsWith("todo")) { + if (taskCount >= 100) { System.out.println(line + "no more space <3" + line); } + String description = input.substring(5); + tasks[taskCount] = new ToDo(description); + taskCount++; + System.out.println(line + "Got it. I've added this task:\n " + tasks[taskCount - 1] + "\nNow you have " + + taskCount + " tasks in the list." + line); + } else if (input.startsWith("deadline")) { + if (taskCount >= 100) { + System.out.println(line + "no more space <3" + line); + } + String[] parts = input.split(" /by "); + String description = parts[0].substring(9); + String by = parts[1]; + tasks[taskCount] = new Deadline(description, by); + taskCount++; + System.out.println(line + "Got it. I've added this task:\n " + tasks[taskCount - 1] + "\nNow you have " + + taskCount + " tasks in the list." + line); + } else if (input.startsWith("event")) { + if (taskCount >= 100) { + System.out.println(line + "no more space <3" + line); + } + String[] parts = input.split(" /from | /to"); + String description = parts[0].substring(6); + String from = parts[1]; + String to = parts[2]; + tasks[taskCount] = new Event(description, from, to); + taskCount++; + System.out.println(line + "Got it. I've added this task:\n " + tasks[taskCount - 1] + "\nNow you have " + + taskCount + " tasks in the list." + line); + } else { + System.out.println(line + "idk this command ;-;" + line); } } From 9e63b63b99009474c8ea4f0914a0cd7f428afa71 Mon Sep 17 00:00:00 2001 From: niha81 Date: Fri, 24 Jan 2025 10:52:30 +0800 Subject: [PATCH 06/11] handle errors --- src/main/java/Woogie.java | 51 ++++++++++++++++++++-- text-ui-test/EXPECTED.TXT | 91 ++++++++++++++++++++++++++++++++++++--- text-ui-test/input.txt | 12 ++++++ 3 files changed, 144 insertions(+), 10 deletions(-) diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index ffebb3a72..737b696b4 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -2,7 +2,7 @@ public class Woogie { public static void main(String[] args) { - String line = "\n----------------------------------------\n"; + String line = "\n------------------------------------------------\n"; String greeting = line + "Hello! I'm Woogie\n" + "What can I do for you?" + line; System.out.println(greeting); @@ -63,6 +63,10 @@ public static void main(String[] args) { if (taskCount >= 100) { System.out.println(line + "no more space <3" + line); } + if (input.length() <= 5 || input.substring(5).trim().isEmpty()) { + System.out.println(line + "todo's description cannot be empty, pls add one >:(" + line); + continue; + } String description = input.substring(5); tasks[taskCount] = new ToDo(description); taskCount++; @@ -72,7 +76,22 @@ public static void main(String[] args) { if (taskCount >= 100) { System.out.println(line + "no more space <3" + line); } - String[] parts = input.split(" /by "); + + if (!input.contains(" /by ")) { + System.out.println(line + "you can't have a deadline without a deadline, add a /by :)" + line); + continue; + } + + String[] parts = input.split(" /by ", 2); + if (parts[0].length() <= 9 || parts[0].substring(9).trim().isEmpty()) { + System.out.println(line + "deadline's description cannot be empty, pls add one >:(" + line); + continue; + } + if (parts[1].trim().isEmpty()) { + System.out.println(line + "deadline's /by time cannot be empty!" + line); + continue; + } + String description = parts[0].substring(9); String by = parts[1]; tasks[taskCount] = new Deadline(description, by); @@ -83,7 +102,30 @@ public static void main(String[] args) { if (taskCount >= 100) { System.out.println(line + "no more space <3" + line); } - String[] parts = input.split(" /from | /to"); + + if (!input.contains(" /from ")) { + System.out.println(line + "i need to know when you event starts, pls add a /from :<" + line); + continue; + } + if (!input.contains(" /to ")) { + System.out.println(line + "i need to know when you event ends, pls add a /to :<" + line); + continue; + } + + String[] parts = input.split(" /from | /to ", 3); + if (parts[0].length() <= 6 || parts[0].substring(6).trim().isEmpty()) { + System.out.println(line + "event's description cannot be empty, pls add one >:(" + line); + continue; + } + if (parts[1].trim().isEmpty()) { + System.out.println(line + "event's /from time cannot be empty!" + line); + continue; + } + if (parts[2].trim().isEmpty()) { + System.out.println(line + "event's /to time cannot be empty!" + line); + continue; + } + String description = parts[0].substring(6); String from = parts[1]; String to = parts[2]; @@ -92,10 +134,11 @@ public static void main(String[] args) { System.out.println(line + "Got it. I've added this task:\n " + tasks[taskCount - 1] + "\nNow you have " + taskCount + " tasks in the list." + line); } else { - System.out.println(line + "idk this command ;-;" + line); + System.out.println(line + "sorry idk this command ;-;" + line); } } scanner.close(); } + } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..b85e51f01 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,86 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| + +---------------------------------------- +Hello! I'm Woogie +What can I do for you? +---------------------------------------- + + +---------------------------------------- +nothing here yet TT +---------------------------------------- + + +---------------------------------------- +Got it. I've added this task: + [T][ ] one +Now you have 1 tasks in the list. +---------------------------------------- + + +---------------------------------------- +Got it. I've added this task: + [D][ ] two (by: sun) +Now you have 2 tasks in the list. +---------------------------------------- + + +---------------------------------------- +Got it. I've added this task: + [E][ ] three (from: mon 2pm to: 4pm) +Now you have 3 tasks in the list. +---------------------------------------- + + +---------------------------------------- + +1.[T][ ] one +2.[D][ ] two (by: sun) +3.[E][ ] three (from: mon 2pm to: 4pm) + +---------------------------------------- + + +---------------------------------------- +INVALID! Pls specify task number :) +---------------------------------------- + + +---------------------------------------- +pls choose a task between 1 and 3. +---------------------------------------- + + +---------------------------------------- +Nice! I've marked this task as done: + [T][X] one +---------------------------------------- + + +---------------------------------------- + +1.[T][X] one +2.[D][ ] two (by: sun) +3.[E][ ] three (from: mon 2pm to: 4pm) + +---------------------------------------- + + +---------------------------------------- +Ok, I've marked this task as not done yet: + [T][ ] one +---------------------------------------- + + +---------------------------------------- + +1.[T][ ] one +2.[D][ ] two (by: sun) +3.[E][ ] three (from: mon 2pm to: 4pm) + +---------------------------------------- + + +---------------------------------------- +Bye. Hope to see you again soon! +---------------------------------------- diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..533d4beb2 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,12 @@ +list +todo one +deadline two /by sun +event three /from mon 2pm /to 4pm +list +mark +mark 8 +mark 1 +list +unmark 1 +list +bye \ No newline at end of file From 25b22393305495749a59fe5e9f41af90657a3988 Mon Sep 17 00:00:00 2001 From: niha81 Date: Fri, 24 Jan 2025 11:40:40 +0800 Subject: [PATCH 07/11] delete --- src/main/java/Woogie.java | 45 +++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index 737b696b4..da28cd396 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -68,10 +68,9 @@ public static void main(String[] args) { continue; } String description = input.substring(5); - tasks[taskCount] = new ToDo(description); + Task newTask = new ToDo(description); + addTask(tasks, taskCount, newTask, line); taskCount++; - System.out.println(line + "Got it. I've added this task:\n " + tasks[taskCount - 1] + "\nNow you have " - + taskCount + " tasks in the list." + line); } else if (input.startsWith("deadline")) { if (taskCount >= 100) { System.out.println(line + "no more space <3" + line); @@ -94,10 +93,9 @@ public static void main(String[] args) { String description = parts[0].substring(9); String by = parts[1]; - tasks[taskCount] = new Deadline(description, by); + Task newTask = new Deadline(description, by); + addTask(tasks, taskCount, newTask, line); taskCount++; - System.out.println(line + "Got it. I've added this task:\n " + tasks[taskCount - 1] + "\nNow you have " - + taskCount + " tasks in the list." + line); } else if (input.startsWith("event")) { if (taskCount >= 100) { System.out.println(line + "no more space <3" + line); @@ -129,10 +127,23 @@ public static void main(String[] args) { String description = parts[0].substring(6); String from = parts[1]; String to = parts[2]; - tasks[taskCount] = new Event(description, from, to); + Task newTask = new Event(description, from, to); + addTask(tasks, taskCount, newTask, line); taskCount++; - System.out.println(line + "Got it. I've added this task:\n " + tasks[taskCount - 1] + "\nNow you have " - + taskCount + " tasks in the list." + line); + } else if (input.startsWith("delete")) { + String[] parts = input.split(" "); + if (parts.length < 2) { + System.out.println(line + "INVALID! Pls specify task number :)" + line); + continue; + } + int taskIndex = Integer.parseInt(parts[1]) - 1; + + if (taskIndex < 0 || taskIndex >= taskCount) { + System.out.println(line + "pls choose a task between 1 and " + taskCount + "." + line); + continue; + } + taskCount--; + deleteTask(tasks, taskCount, taskIndex, line); } else { System.out.println(line + "sorry idk this command ;-;" + line); } @@ -141,4 +152,20 @@ public static void main(String[] args) { scanner.close(); } + private static void addTask(Task[] tasks, int taskCount, Task newTask, String line) { + tasks[taskCount] = newTask; + System.out.println(line + "Got it. I've added this task:\n " + tasks[taskCount] + + "\nNow you have " + (taskCount + 1) + " tasks in the list." + line); + } + + private static void deleteTask(Task[] tasks, int taskCount, int taskIndex, String line) { + Task rem = tasks[taskIndex]; + for(int i = taskIndex; i < taskCount; i++) { + tasks[i] = tasks[i + 1]; + } + tasks[taskCount] = null; + System.out.println(line + "Noted. I've removed this task:\n " + rem + + "\nNow you have " + taskCount + " tasks in the list." + line); + } + } From 7a39c5ecf505cb0f63586bbe05c3342a22af806f Mon Sep 17 00:00:00 2001 From: niha81 Date: Fri, 24 Jan 2025 12:50:06 +0800 Subject: [PATCH 08/11] automated text ui testing --- src/main/java/Woogie.java | 4 +-- text-ui-test/EXPECTED.TXT | 62 ++++++++++++++++----------------------- text-ui-test/input.txt | 2 -- 3 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index da28cd396..10a984757 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -43,7 +43,7 @@ public static void main(String[] args) { } tasks[taskIndex].markDone(); - System.out.println(line + "Nice! I've marked this task as done: \n " + tasks[taskIndex] + line); + System.out.println(line + "Nice! I've marked this task as done:\n " + tasks[taskIndex] + line); } else if (input.startsWith("unmark")) { String[] parts = input.split(" "); if (parts.length < 2) { @@ -58,7 +58,7 @@ public static void main(String[] args) { } tasks[taskIndex].markUndone(); - System.out.println(line + "Ok, I've marked this task as not done yet: \n " + tasks[taskIndex] + line); + System.out.println(line + "Ok, I've marked this task as not done yet:\n " + tasks[taskIndex] + line); } else if (input.startsWith("todo")) { if (taskCount >= 100) { System.out.println(line + "no more space <3" + line); diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index b85e51f01..4f48bd341 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,86 +1,76 @@ ----------------------------------------- +------------------------------------------------ Hello! I'm Woogie What can I do for you? ----------------------------------------- +------------------------------------------------ ----------------------------------------- +------------------------------------------------ nothing here yet TT ----------------------------------------- +------------------------------------------------ ----------------------------------------- +------------------------------------------------ Got it. I've added this task: [T][ ] one Now you have 1 tasks in the list. ----------------------------------------- +------------------------------------------------ ----------------------------------------- +------------------------------------------------ Got it. I've added this task: [D][ ] two (by: sun) Now you have 2 tasks in the list. ----------------------------------------- +------------------------------------------------ ----------------------------------------- +------------------------------------------------ Got it. I've added this task: - [E][ ] three (from: mon 2pm to: 4pm) + [E][ ] three (from: mon 2pm to:4pm) Now you have 3 tasks in the list. ----------------------------------------- +------------------------------------------------ ----------------------------------------- +------------------------------------------------ 1.[T][ ] one 2.[D][ ] two (by: sun) -3.[E][ ] three (from: mon 2pm to: 4pm) +3.[E][ ] three (from: mon 2pm to:4pm) ----------------------------------------- +------------------------------------------------ ----------------------------------------- -INVALID! Pls specify task number :) ----------------------------------------- - - ----------------------------------------- -pls choose a task between 1 and 3. ----------------------------------------- - - ----------------------------------------- +------------------------------------------------ Nice! I've marked this task as done: [T][X] one ----------------------------------------- +------------------------------------------------ ----------------------------------------- +------------------------------------------------ 1.[T][X] one 2.[D][ ] two (by: sun) -3.[E][ ] three (from: mon 2pm to: 4pm) +3.[E][ ] three (from: mon 2pm to:4pm) ----------------------------------------- +------------------------------------------------ ----------------------------------------- +------------------------------------------------ Ok, I've marked this task as not done yet: [T][ ] one ----------------------------------------- +------------------------------------------------ ----------------------------------------- +------------------------------------------------ 1.[T][ ] one 2.[D][ ] two (by: sun) -3.[E][ ] three (from: mon 2pm to: 4pm) +3.[E][ ] three (from: mon 2pm to:4pm) ----------------------------------------- +------------------------------------------------ ----------------------------------------- +------------------------------------------------ Bye. Hope to see you again soon! ----------------------------------------- +------------------------------------------------ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 533d4beb2..fa07dd616 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -3,8 +3,6 @@ todo one deadline two /by sun event three /from mon 2pm /to 4pm list -mark -mark 8 mark 1 list unmark 1 From 32c828644da973fcfb5d011ac58e27569ffd22e2 Mon Sep 17 00:00:00 2001 From: niha81 Date: Fri, 31 Jan 2025 03:14:58 +0800 Subject: [PATCH 09/11] added exceptions, fixed syntax --- src/main/java/Woogie.java | 284 ++++++++++++++++++++------------------ text-ui-test/EXPECTED.TXT | 52 ++++++- text-ui-test/input.txt | 10 +- 3 files changed, 206 insertions(+), 140 deletions(-) diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index 10a984757..cb876eecb 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -1,171 +1,183 @@ +import java.util.ArrayList; import java.util.Scanner; public class Woogie { + private static final String LINE = "\n----------------------------------------------------\n"; public static void main(String[] args) { - String line = "\n------------------------------------------------\n"; - String greeting = line + "Hello! I'm Woogie\n" + "What can I do for you?" + line; + String greeting = LINE + "Hello! I'm Woogie\n" + "What can I do for you?" + LINE; System.out.println(greeting); - Task[] tasks = new Task[100]; - int taskCount = 0; - + ArrayList tasks = new ArrayList<>(); Scanner scanner = new Scanner(System.in); while (true) { String input = scanner.nextLine(); if (input.equalsIgnoreCase("bye")) { - System.out.println(line + "Bye. Hope to see you again soon!" + line); + System.out.println(LINE + "Bye. Hope to see you again soon!" + LINE); break; } if (input.equalsIgnoreCase("list")) { - if (taskCount == 0) { - System.out.println(line + "nothing here yet TT" + line); - } else { - System.out.println(line); - for (int i = 0; i < taskCount; i++) { - System.out.println((i + 1) + "." + tasks[i]); - } - System.out.println(line); - } + listTasks(tasks); } else if (input.startsWith("mark")) { - String[] parts = input.split(" "); - if (parts.length < 2) { - System.out.println(line + "INVALID! Pls specify task number :)" + line); - continue; - } - int taskIndex = Integer.parseInt(parts[1]) - 1; - - if (taskIndex < 0 || taskIndex >= taskCount) { - System.out.println(line + "pls choose a task between 1 and " + taskCount + "." + line); - continue; - } - - tasks[taskIndex].markDone(); - System.out.println(line + "Nice! I've marked this task as done:\n " + tasks[taskIndex] + line); + markAsDone(tasks, input); } else if (input.startsWith("unmark")) { - String[] parts = input.split(" "); - if (parts.length < 2) { - System.out.println(line + "INVALID! Pls specify task number :)" + line); - continue; - } - int taskIndex = Integer.parseInt(parts[1]) - 1; - - if (taskIndex < 0 || taskIndex >= taskCount) { - System.out.println(line + "pls choose a task between 1 and " + taskCount + "." + line); - continue; - } - - tasks[taskIndex].markUndone(); - System.out.println(line + "Ok, I've marked this task as not done yet:\n " + tasks[taskIndex] + line); + markAsNotDone(tasks, input); } else if (input.startsWith("todo")) { - if (taskCount >= 100) { - System.out.println(line + "no more space <3" + line); - } - if (input.length() <= 5 || input.substring(5).trim().isEmpty()) { - System.out.println(line + "todo's description cannot be empty, pls add one >:(" + line); - continue; - } - String description = input.substring(5); - Task newTask = new ToDo(description); - addTask(tasks, taskCount, newTask, line); - taskCount++; + addTodo(tasks, input); } else if (input.startsWith("deadline")) { - if (taskCount >= 100) { - System.out.println(line + "no more space <3" + line); - } - - if (!input.contains(" /by ")) { - System.out.println(line + "you can't have a deadline without a deadline, add a /by :)" + line); - continue; - } - - String[] parts = input.split(" /by ", 2); - if (parts[0].length() <= 9 || parts[0].substring(9).trim().isEmpty()) { - System.out.println(line + "deadline's description cannot be empty, pls add one >:(" + line); - continue; - } - if (parts[1].trim().isEmpty()) { - System.out.println(line + "deadline's /by time cannot be empty!" + line); - continue; - } - - String description = parts[0].substring(9); - String by = parts[1]; - Task newTask = new Deadline(description, by); - addTask(tasks, taskCount, newTask, line); - taskCount++; + addDeadline(tasks, input); } else if (input.startsWith("event")) { - if (taskCount >= 100) { - System.out.println(line + "no more space <3" + line); - } - - if (!input.contains(" /from ")) { - System.out.println(line + "i need to know when you event starts, pls add a /from :<" + line); - continue; - } - if (!input.contains(" /to ")) { - System.out.println(line + "i need to know when you event ends, pls add a /to :<" + line); - continue; - } - - String[] parts = input.split(" /from | /to ", 3); - if (parts[0].length() <= 6 || parts[0].substring(6).trim().isEmpty()) { - System.out.println(line + "event's description cannot be empty, pls add one >:(" + line); - continue; - } - if (parts[1].trim().isEmpty()) { - System.out.println(line + "event's /from time cannot be empty!" + line); - continue; - } - if (parts[2].trim().isEmpty()) { - System.out.println(line + "event's /to time cannot be empty!" + line); - continue; - } - - String description = parts[0].substring(6); - String from = parts[1]; - String to = parts[2]; - Task newTask = new Event(description, from, to); - addTask(tasks, taskCount, newTask, line); - taskCount++; + addEvent(tasks, input); } else if (input.startsWith("delete")) { - String[] parts = input.split(" "); - if (parts.length < 2) { - System.out.println(line + "INVALID! Pls specify task number :)" + line); - continue; - } - int taskIndex = Integer.parseInt(parts[1]) - 1; - - if (taskIndex < 0 || taskIndex >= taskCount) { - System.out.println(line + "pls choose a task between 1 and " + taskCount + "." + line); - continue; - } - taskCount--; - deleteTask(tasks, taskCount, taskIndex, line); + deleteTask(tasks, input); } else { - System.out.println(line + "sorry idk this command ;-;" + line); + System.out.println(LINE + "sorry idk this command ;-;" + LINE); } + } scanner.close(); } - private static void addTask(Task[] tasks, int taskCount, Task newTask, String line) { - tasks[taskCount] = newTask; - System.out.println(line + "Got it. I've added this task:\n " + tasks[taskCount] - + "\nNow you have " + (taskCount + 1) + " tasks in the list." + line); + private static void listTasks(ArrayList tasks) { + if (tasks.isEmpty()) { + System.out.println(LINE + "nothing here yet TT" + LINE); + } else { + System.out.println(LINE); + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + "." + tasks.get(i)); + } + System.out.println(LINE); + } + } + + private static void markAsDone(ArrayList tasks, String input) { + String[] parts = input.split(" "); + if (parts.length < 2) { + System.out.println(LINE + "INVALID! Pls specify task number :)" + LINE); + return; + } + try { + int taskIndex = Integer.parseInt(parts[1]) - 1; + tasks.get(taskIndex).markDone(); + System.out.println(LINE + "Nice! I've marked this task as done:\n " + tasks.get(taskIndex) + LINE); + } catch (NumberFormatException e) { + System.out.println(LINE + "INVALID! Task number must be a valid number :)" + LINE); + } catch (IndexOutOfBoundsException e) { + System.out.println(LINE + "pls choose a task between 1 and " + tasks.size() + "." + LINE); + } } - private static void deleteTask(Task[] tasks, int taskCount, int taskIndex, String line) { - Task rem = tasks[taskIndex]; - for(int i = taskIndex; i < taskCount; i++) { - tasks[i] = tasks[i + 1]; + private static void markAsNotDone(ArrayList tasks, String input) { + String[] parts = input.split(" "); + if (parts.length < 2) { + System.out.println(LINE + "INVALID! Pls specify task number :)" + LINE); + return; + } + + try { + int taskIndex = Integer.parseInt(parts[1]) - 1; + tasks.get(taskIndex).markUndone(); + System.out.println(LINE + "Ok, I've marked this task as not done yet:\n " + tasks.get(taskIndex) + LINE); + } catch (NumberFormatException e) { + System.out.println(LINE + "INVALID! Task number must be a valid number :)" + LINE); + } catch (IndexOutOfBoundsException e) { + System.out.println(LINE + "pls choose a task between 1 and " + tasks.size() + "." + LINE); + } + } + + private static void addTodo(ArrayList tasks, String input) { + if (input.length() <= 5 || input.substring(5).trim().isEmpty()) { + System.out.println(LINE + "todo's description cannot be empty, pls add one >:(" + LINE); + return; + } + String description = input.substring(5).trim(); + Task newTask = new ToDo(description); + addTask(tasks, newTask); + } + + private static void addDeadline(ArrayList tasks, String input) { + if (!input.contains(" /by ")) { + System.out.println(LINE + "you can't have a deadline without a deadline, add a /by :)" + LINE); + return; + } + + try { + String[] parts = input.split(" /by ", 2); + if (parts[0].length() <= 9 || parts[0].substring(9).trim().isEmpty()) { + throw new IllegalArgumentException("deadline's description cannot be empty, pls add one >:("); + } + if (parts[1].trim().isEmpty()) { + throw new IllegalArgumentException("deadline's /by time cannot be empty!"); + } + + String description = parts[0].substring(9); + String by = parts[1]; + Task newTask = new Deadline(description, by); + addTask(tasks, newTask); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println(LINE + "oop, smt went wrong with your deadline input." + LINE); + } catch (IllegalArgumentException e) { + System.out.println(LINE + e.getMessage() + LINE); + } + } + + private static void addEvent(ArrayList tasks, String input) { + if (!input.contains(" /from ") || !input.contains(" /to ")) { + System.out.println(LINE + "i need to know when your event starts and ends,\n" + + "pls add both a /from and /to :<" + LINE); + return; + } + + try { + String[] parts = input.split(" /from | /to ", 3); + if (parts.length < 3 || parts[0].length() <= 6 || parts[0].substring(6).trim().isEmpty()) { + throw new IllegalArgumentException("event's description cannot be empty, pls add one >:("); + } + if (parts[1].trim().isEmpty()) { + throw new IllegalArgumentException("event's /from time cannot be empty!"); + } + if (parts[2].trim().isEmpty()) { + throw new IllegalArgumentException("event's /to time cannot be empty!"); + } + + String description = parts[0].substring(6); + String from = parts[1]; + String to = parts[2]; + Task newTask = new Event(description, from, to); + addTask(tasks, newTask); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println(LINE + "oop, smt went wrong with your event input." + LINE); + } catch (IllegalArgumentException e) { + System.out.println(LINE + e.getMessage() + LINE); + } + } + + private static void addTask(ArrayList tasks, Task newTask) { + tasks.add(newTask); + System.out.println(LINE + "Got it. I've added this task:\n " + newTask + + "\nNow you have " + tasks.size() + " tasks in the list." + LINE); + } + + private static void deleteTask(ArrayList tasks, String input) { + String[] parts = input.split(" "); + if (parts.length < 2) { + System.out.println(LINE + "INVALID! Pls specify task number :)" + LINE); + return; + } + + try { + int taskIndex = Integer.parseInt(parts[1]) - 1; + Task rem = tasks.remove(taskIndex); + System.out.println(LINE + "Noted. I've removed this task:\n " + rem + + "\nNow you have " + tasks.size() + " tasks in the list." + LINE); + } catch (NumberFormatException e) { + System.out.println(LINE + "INVALID! Task number must be a valid number :)" + LINE); + } catch (IndexOutOfBoundsException e) { + System.out.println(LINE + "pls choose a task between 1 and " + tasks.size() + "." + LINE); } - tasks[taskCount] = null; - System.out.println(line + "Noted. I've removed this task:\n " + rem - + "\nNow you have " + taskCount + " tasks in the list." + line); } } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 4f48bd341..bfafa1c8d 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -10,6 +10,11 @@ nothing here yet TT ------------------------------------------------ +------------------------------------------------ +todo's description cannot be empty, pls add one >:( +------------------------------------------------ + + ------------------------------------------------ Got it. I've added this task: [T][ ] one @@ -17,6 +22,16 @@ Now you have 1 tasks in the list. ------------------------------------------------ +------------------------------------------------ +you can't have a deadline without a deadline, add a /by :) +------------------------------------------------ + + +------------------------------------------------ +deadline's description cannot be empty, pls add one >:( +------------------------------------------------ + + ------------------------------------------------ Got it. I've added this task: [D][ ] two (by: sun) @@ -40,16 +55,32 @@ Now you have 3 tasks in the list. ------------------------------------------------ +------------------------------------------------ +INVALID! Pls specify task number :) +------------------------------------------------ + + +------------------------------------------------ +pls choose a task between 1 and 3. +------------------------------------------------ + + ------------------------------------------------ Nice! I've marked this task as done: [T][X] one ------------------------------------------------ +------------------------------------------------ +Nice! I've marked this task as done: + [D][X] two (by: sun) +------------------------------------------------ + + ------------------------------------------------ 1.[T][X] one -2.[D][ ] two (by: sun) +2.[D][X] two (by: sun) 3.[E][ ] three (from: mon 2pm to:4pm) ------------------------------------------------ @@ -57,19 +88,34 @@ Nice! I've marked this task as done: ------------------------------------------------ Ok, I've marked this task as not done yet: - [T][ ] one + [D][ ] two (by: sun) ------------------------------------------------ ------------------------------------------------ -1.[T][ ] one +1.[T][X] one 2.[D][ ] two (by: sun) 3.[E][ ] three (from: mon 2pm to:4pm) ------------------------------------------------ +------------------------------------------------ +Noted. I've removed this task: + [T][X] one +Now you have 2 tasks in the list. +------------------------------------------------ + + +------------------------------------------------ + +1.[D][ ] two (by: sun) +2.[E][ ] three (from: mon 2pm to:4pm) + +------------------------------------------------ + + ------------------------------------------------ Bye. Hope to see you again soon! ------------------------------------------------ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index fa07dd616..d727d7b44 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -1,10 +1,18 @@ list +todo todo one +deadline +deadline /by sun deadline two /by sun event three /from mon 2pm /to 4pm list +mark +mark 8 mark 1 +mark 2 list -unmark 1 +unmark 2 +list +delete 1 list bye \ No newline at end of file From 4402a1c82fec07d20da62802cb757ab24d104163 Mon Sep 17 00:00:00 2001 From: niha81 Date: Fri, 31 Jan 2025 04:17:28 +0800 Subject: [PATCH 10/11] Implement file save and load --- data/woogie.txt | 0 src/main/java/Deadline.java | 5 ++++ src/main/java/Event.java | 5 ++++ src/main/java/Storage.java | 53 +++++++++++++++++++++++++++++++++++++ src/main/java/Task.java | 36 ++++++++++++++++++++++--- src/main/java/ToDo.java | 5 ++++ src/main/java/Woogie.java | 49 +++++++++++++++++++--------------- 7 files changed, 129 insertions(+), 24 deletions(-) create mode 100644 data/woogie.txt create mode 100644 src/main/java/Storage.java diff --git a/data/woogie.txt b/data/woogie.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index b7d4bd808..d490f2a8f 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -6,6 +6,11 @@ public Deadline(String description, String by) { this.by = by; } + @Override + public String toFileFormat() { + return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + 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 index 92b959b9f..71e8a73df 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -8,6 +8,11 @@ public Event(String description, String from, String to) { this.to = to; } + @Override + public String toFileFormat() { + return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + from + " | " + to; + } + @Override public String toString() { return "[E]" + super.toString() + " (from: " + this.from + " to:" + this.to +")"; diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 000000000..da89fe90d --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,53 @@ +import java.io.File; +import java.io.FileWriter; +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.IOException; +import java.util.ArrayList; + + +public class Storage { + private final String filePath; + + public Storage(String filePath) { + this.filePath = filePath; + } + + public void saveTasks(ArrayList tasks) { + try { + File file = new File(filePath); + file.getParentFile().mkdirs(); + FileWriter writer = new FileWriter(file); + + for (Task task : tasks) { + writer.write(task.toFileFormat() + "\n"); + } + writer.close(); + } catch (IOException e) { + System.out.println("Error saving tasks: " + e.getMessage()); + } + } + + public ArrayList loadTasks() { + ArrayList tasks = new ArrayList<>(); + File file = new File(filePath); + + if (!file.exists()) { + return tasks; // Return empty list if file does not exist + } + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + Task task = Task.fromFileFormat(line); + if (task != null) { + tasks.add(task); + } + } + } catch (IOException e) { + System.out.println("Error loading tasks: " + e.getMessage()); + } + + return tasks; + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 84adae413..7a3aa5858 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,6 +15,36 @@ public void markUndone() { this.isDone = false; } + public abstract String toFileFormat(); + + public static Task fromFileFormat(String line) { + String[] parts = line.split(" \\| "); + if (parts.length < 3) return null; + + String type = parts[0]; + boolean isDone = parts[1].equals("1"); + String description = parts[2]; + + switch (type) { + case "T": + ToDo todo = new ToDo(description); + if (isDone) todo.markDone(); + return todo; + case "D": + if (parts.length < 4) return null; + Deadline deadline = new Deadline(description, parts[3]); + if (isDone) deadline.markDone(); + return deadline; + case "E": + if (parts.length < 5) return null; + Event event = new Event(description, parts[3], parts[4]); + if (isDone) event.markDone(); + return event; + default: + return null; + } + } + @Override public String toString() { String status = isDone ? "X" : " "; diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java index ee72ef43c..e754105d0 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/ToDo.java @@ -3,6 +3,11 @@ public ToDo(String description) { super(description); } + @Override + public String toFileFormat() { + return "T | " + (isDone ? "1" : "0") + " | " + description; + } + @Override public String toString() { return "[T]" + super.toString(); diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index cb876eecb..036d501fd 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -3,44 +3,51 @@ public class Woogie { private static final String LINE = "\n----------------------------------------------------\n"; + private static final String FILE_PATH = "./data/woogie.txt"; + private static Storage storage = new Storage(FILE_PATH); + public static void main(String[] args) { + ArrayList tasks = storage.loadTasks(); + String greeting = LINE + "Hello! I'm Woogie\n" + "What can I do for you?" + LINE; System.out.println(greeting); - ArrayList tasks = new ArrayList<>(); Scanner scanner = new Scanner(System.in); while (true) { - String input = scanner.nextLine(); + String input = scanner.nextLine().trim(); if (input.equalsIgnoreCase("bye")) { System.out.println(LINE + "Bye. Hope to see you again soon!" + LINE); break; } - if (input.equalsIgnoreCase("list")) { - listTasks(tasks); - } else if (input.startsWith("mark")) { - markAsDone(tasks, input); - } else if (input.startsWith("unmark")) { - markAsNotDone(tasks, input); - } else if (input.startsWith("todo")) { - addTodo(tasks, input); - } else if (input.startsWith("deadline")) { - addDeadline(tasks, input); - } else if (input.startsWith("event")) { - addEvent(tasks, input); - } else if (input.startsWith("delete")) { - deleteTask(tasks, input); - } else { - System.out.println(LINE + "sorry idk this command ;-;" + LINE); - } - + processCommand(tasks, input); + storage.saveTasks(tasks); } - scanner.close(); } + private static void processCommand(ArrayList tasks, String input) { + if (input.equalsIgnoreCase("list")) { + listTasks(tasks); + } else if (input.startsWith("mark")) { + markAsDone(tasks, input); + } else if (input.startsWith("unmark")) { + markAsNotDone(tasks, input); + } else if (input.startsWith("todo")) { + addTodo(tasks, input); + } else if (input.startsWith("deadline")) { + addDeadline(tasks, input); + } else if (input.startsWith("event")) { + addEvent(tasks, input); + } else if (input.startsWith("delete")) { + deleteTask(tasks, input); + } else { + System.out.println(LINE + "sorry idk this command ;-;" + LINE); + } + } + private static void listTasks(ArrayList tasks) { if (tasks.isEmpty()) { System.out.println(LINE + "nothing here yet TT" + LINE); From 0006b0dd189bd1db662a7217f928f987781cc24b Mon Sep 17 00:00:00 2001 From: niha81 Date: Fri, 31 Jan 2025 05:55:50 +0800 Subject: [PATCH 11/11] convert dates and times --- data/woogie.txt | 3 +++ src/main/java/Deadline.java | 13 +++++++++---- src/main/java/Event.java | 19 +++++++++++++------ src/main/java/Woogie.java | 25 ++++++++++++++++--------- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/data/woogie.txt b/data/woogie.txt index e69de29bb..b00ded394 100644 --- a/data/woogie.txt +++ b/data/woogie.txt @@ -0,0 +1,3 @@ +T | 0 | a +D | 1 | b | 2025-02-28 1400 +E | 0 | c | 2024-12-03 1300 | 2025-02-03 1500 diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index d490f2a8f..c50831ada 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,18 +1,23 @@ +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + public class Deadline extends Task { - private String by; + private LocalDateTime by; + private static final DateTimeFormatter INPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); + private static final DateTimeFormatter OUTPUT_FORMATTER = DateTimeFormatter.ofPattern("MMM d yyyy, h:mm a"); public Deadline(String description, String by) { super(description); - this.by = by; + this.by = LocalDateTime.parse(by, INPUT_FORMATTER); } @Override public String toFileFormat() { - return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + by; + return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + by.format(INPUT_FORMATTER); } @Override public String toString() { - return "[D]" + super.toString() + " (by: " + this.by + ")"; + return "[D]" + super.toString() + " (by: " + by.format(OUTPUT_FORMATTER) + ")"; } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 71e8a73df..677e7560e 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,20 +1,27 @@ +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + public class Event extends Task { - private String from; - private String to; + private LocalDateTime from; + private LocalDateTime to; + private static final DateTimeFormatter INPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); + private static final DateTimeFormatter OUTPUT_FORMATTER = DateTimeFormatter.ofPattern("MMM d yyyy, h:mm a"); public Event(String description, String from, String to) { super(description); - this.from = from; - this.to = to; + this.from = LocalDateTime.parse(from, INPUT_FORMATTER); + this.to = LocalDateTime.parse(to, INPUT_FORMATTER); } @Override public String toFileFormat() { - return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + from + " | " + to; + return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + + from.format(INPUT_FORMATTER) + " | " + to.format(INPUT_FORMATTER); } @Override public String toString() { - return "[E]" + super.toString() + " (from: " + this.from + " to:" + this.to +")"; + return "[E]" + super.toString() + " (from: " + from.format(OUTPUT_FORMATTER) + + " to: " + to.format(OUTPUT_FORMATTER) + ")"; } } diff --git a/src/main/java/Woogie.java b/src/main/java/Woogie.java index 036d501fd..bfa16e380 100644 --- a/src/main/java/Woogie.java +++ b/src/main/java/Woogie.java @@ -119,6 +119,9 @@ private static void addDeadline(ArrayList tasks, String input) { if (parts[1].trim().isEmpty()) { throw new IllegalArgumentException("deadline's /by time cannot be empty!"); } + if (!parts[1].matches("\\d{4}-\\d{2}-\\d{2} \\d{4}")) { // Ensure correct format + throw new IllegalArgumentException("deadline must be in yyyy-MM-dd HHmm format!"); + } String description = parts[0].substring(9); String by = parts[1]; @@ -139,20 +142,24 @@ private static void addEvent(ArrayList tasks, String input) { } try { - String[] parts = input.split(" /from | /to ", 3); - if (parts.length < 3 || parts[0].length() <= 6 || parts[0].substring(6).trim().isEmpty()) { + String[] firstSplit = input.split(" /from ", 2); + if (firstSplit.length < 2 || firstSplit[0].substring(6).trim().isEmpty()) { throw new IllegalArgumentException("event's description cannot be empty, pls add one >:("); } - if (parts[1].trim().isEmpty()) { - throw new IllegalArgumentException("event's /from time cannot be empty!"); + + String description = firstSplit[0].substring(6).trim(); + String[] secondSplit = firstSplit[1].split(" /to ", 2); + if (secondSplit.length < 2 || secondSplit[0].trim().isEmpty() || secondSplit[1].trim().isEmpty()) { + throw new IllegalArgumentException("event's /from and /to times cannot be empty!"); } - if (parts[2].trim().isEmpty()) { - throw new IllegalArgumentException("event's /to time cannot be empty!"); + + String from = secondSplit[0].trim(); + String to = secondSplit[1].trim(); + + if (!from.matches("\\d{4}-\\d{2}-\\d{2} \\d{4}") || !to.matches("\\d{4}-\\d{2}-\\d{2} \\d{4}")) { + throw new IllegalArgumentException("event times must be in yyyy-MM-dd HHmm format!"); } - String description = parts[0].substring(6); - String from = parts[1]; - String to = parts[2]; Task newTask = new Event(description, from, to); addTask(tasks, newTask); } catch (ArrayIndexOutOfBoundsException e) {