From aed8d755f513e1624b55b1d04a2a74a375327c60 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Fri, 24 Jan 2025 11:33:42 +0800 Subject: [PATCH 01/11] Add greeting --- src/main/java/Clarawr.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/Clarawr.java diff --git a/src/main/java/Clarawr.java b/src/main/java/Clarawr.java new file mode 100644 index 000000000..c007615c4 --- /dev/null +++ b/src/main/java/Clarawr.java @@ -0,0 +1,9 @@ +public class Clarawr { + public static void main(String[] args) { + String name = "Clarawr"; + System.out.println("Hello! I'm " + name); + System.out.println("What can I do for you?"); + System.out.println("Bye. Hope to see you again soon!"); + } +} + From ccb2c96402e9d012edc02cff84095fcab4d71453 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Fri, 24 Jan 2025 19:30:39 +0800 Subject: [PATCH 02/11] Rename main class --- src/main/java/BigBoy.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/BigBoy.java diff --git a/src/main/java/BigBoy.java b/src/main/java/BigBoy.java new file mode 100644 index 000000000..2ea9de81e --- /dev/null +++ b/src/main/java/BigBoy.java @@ -0,0 +1,10 @@ +public class BigBoy { + public static void main(String[] args) { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); + } +} From 04009a69f696fd43aded8cf99ef8333858693d7a Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Fri, 24 Jan 2025 19:46:20 +0800 Subject: [PATCH 03/11] Add support for echo --- src/main/java/Level_1/Clarawr.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/Level_1/Clarawr.java diff --git a/src/main/java/Level_1/Clarawr.java b/src/main/java/Level_1/Clarawr.java new file mode 100644 index 000000000..846b3da4c --- /dev/null +++ b/src/main/java/Level_1/Clarawr.java @@ -0,0 +1,20 @@ +package Level_1; +import java.util.Scanner; + +public class Clarawr { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("Hello! I'm Clarawr\n" + + "What can I do for you?"); + + String instruction = scanner.nextLine(); + + while (!instruction.equalsIgnoreCase("bye")) { // Check for "bye" (case-insensitive) + System.out.println(instruction); // Repeat the input + instruction = scanner.nextLine(); // Read the next input + } + + System.out.println("Bye. Hope to see you again soon!"); + scanner.close(); // Close the scanner + } +} From 1abd7d09557afac52ddd48d34ab5c23701614a59 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Fri, 24 Jan 2025 20:25:53 +0800 Subject: [PATCH 04/11] Added Add, List --- src/main/java/Clarawr.java | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/Clarawr.java b/src/main/java/Clarawr.java index c007615c4..4735b79c8 100644 --- a/src/main/java/Clarawr.java +++ b/src/main/java/Clarawr.java @@ -1,9 +1,38 @@ +import java.util.Scanner; + public class Clarawr { + static int pointer = 0; + public static void main(String[] args) { - String name = "Clarawr"; - System.out.println("Hello! I'm " + name); - System.out.println("What can I do for you?"); + Scanner scanner = new Scanner(System.in); + String[] instructions = new String[100]; // Array to store instructions + + System.out.println("Hello! I'm Clarawr\n" + + "What can I do for you?"); + + String instruction = scanner.nextLine(); + + while (!instruction.equalsIgnoreCase("bye")) { // Check for "bye" (case-insensitive) + if (instruction.equalsIgnoreCase("list")) { + // Print the instructions in the array + if (pointer == 0) { + System.out.println("No instructions added yet."); + } else { + System.out.println("Instructions list:"); + for (int i = 0; i < pointer; i++) { + System.out.println((i + 1) + ". " + instructions[i]); + } + } + } else { + System.out.println("added: " + instruction); // Repeat the input + instructions[pointer] = instruction; // Add the instruction to the array + pointer++; // Move the pointer to the next index + } + + instruction = scanner.nextLine(); // Read the next input + } + System.out.println("Bye. Hope to see you again soon!"); + scanner.close(); // Close the scanner } } - From 95a81027cea7c8e63ca5f36c81897dcd2adf76a3 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Fri, 24 Jan 2025 20:27:18 +0800 Subject: [PATCH 05/11] Added Add, List --- src/main/java/Clarawr.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/Clarawr.java b/src/main/java/Clarawr.java index 4735b79c8..5389f3689 100644 --- a/src/main/java/Clarawr.java +++ b/src/main/java/Clarawr.java @@ -18,7 +18,6 @@ public static void main(String[] args) { if (pointer == 0) { System.out.println("No instructions added yet."); } else { - System.out.println("Instructions list:"); for (int i = 0; i < pointer; i++) { System.out.println((i + 1) + ". " + instructions[i]); } From 37c96078f2a3e6cda3f9337315258e31fc50fbf0 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Fri, 24 Jan 2025 22:56:59 +0800 Subject: [PATCH 06/11] Added Task Class and mark/unmark feature --- src/main/java/Clarawr.java | 29 ++++++++++++++++++++++++----- src/main/java/Task.java | 13 +++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Clarawr.java b/src/main/java/Clarawr.java index 5389f3689..faed23179 100644 --- a/src/main/java/Clarawr.java +++ b/src/main/java/Clarawr.java @@ -2,35 +2,54 @@ public class Clarawr { static int pointer = 0; + static Task[] tasks = new Task[100]; //Array to store Task objects public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - String[] instructions = new String[100]; // Array to store instructions System.out.println("Hello! I'm Clarawr\n" + "What can I do for you?"); String instruction = scanner.nextLine(); - while (!instruction.equalsIgnoreCase("bye")) { // Check for "bye" (case-insensitive) + while (!instruction.equalsIgnoreCase("bye")) { if (instruction.equalsIgnoreCase("list")) { // Print the instructions in the array if (pointer == 0) { System.out.println("No instructions added yet."); } else { + System.out.println("Here are the tasks in your list: "); for (int i = 0; i < pointer; i++) { - System.out.println((i + 1) + ". " + instructions[i]); + System.out.println((i + 1) + ".[" + tasks[i].getStatusIcon() + "] " + tasks[i].description); } } + } else if (instruction.split(" ")[0].equals("mark")) { + int selecter = Integer.parseInt(instruction.split(" ")[1]) - 1; + if (selecter >= 0 && selecter < pointer) { + tasks[selecter].isDone = true; + System.out.println("Nice! I've marked this task as done: \n" + + "[" + tasks[selecter].getStatusIcon() + "] " + tasks[selecter].description); + } else { + System.out.println("Task does not exist! Please try again."); + } + + } else if (instruction.split(" ")[0].equals("unmark")) { + int selecter = Integer.parseInt(instruction.split(" ")[1]) - 1; + if (selecter >= 0 && selecter < pointer) { + tasks[selecter].isDone = false; + System.out.println("OK, I've marked this task as not done yet: \n" + + "[" + tasks[selecter].getStatusIcon() + "] " + tasks[selecter].description); + } else { + System.out.println("Task does not exist! Please try again."); + } } else { System.out.println("added: " + instruction); // Repeat the input - instructions[pointer] = instruction; // Add the instruction to the array + tasks[pointer] = new Task(instruction); // Add the instruction to the array pointer++; // Move the pointer to the next index } instruction = scanner.nextLine(); // Read the next input } - System.out.println("Bye. Hope to see you again soon!"); scanner.close(); // Close the scanner } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..ac9e0d9c1 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,13 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } +} From 35f61c56e0e156f0fc08fa99fdc2bd8881f2bb06 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Fri, 24 Jan 2025 23:50:26 +0800 Subject: [PATCH 07/11] Usage of inheritance for Level 4 --- src/main/java/Clarawr.java | 70 +++++++++++++++++++++++-------------- src/main/java/Deadline.java | 14 ++++++++ src/main/java/Event.java | 16 +++++++++ src/main/java/Task.java | 12 +++++++ src/main/java/Todo.java | 13 +++++++ 5 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Clarawr.java b/src/main/java/Clarawr.java index faed23179..280cb8bf5 100644 --- a/src/main/java/Clarawr.java +++ b/src/main/java/Clarawr.java @@ -2,7 +2,7 @@ public class Clarawr { static int pointer = 0; - static Task[] tasks = new Task[100]; //Array to store Task objects + static Task[] tasks = new Task[100]; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); @@ -14,43 +14,59 @@ public static void main(String[] args) { while (!instruction.equalsIgnoreCase("bye")) { if (instruction.equalsIgnoreCase("list")) { - // Print the instructions in the array if (pointer == 0) { - System.out.println("No instructions added yet."); + System.out.println("No tasks in your list."); } else { - System.out.println("Here are the tasks in your list: "); + System.out.println("Here are the tasks in your list:"); for (int i = 0; i < pointer; i++) { - System.out.println((i + 1) + ".[" + tasks[i].getStatusIcon() + "] " + tasks[i].description); + System.out.println((i + 1) + "." + tasks[i]); } } - } else if (instruction.split(" ")[0].equals("mark")) { - int selecter = Integer.parseInt(instruction.split(" ")[1]) - 1; - if (selecter >= 0 && selecter < pointer) { - tasks[selecter].isDone = true; - System.out.println("Nice! I've marked this task as done: \n" - + "[" + tasks[selecter].getStatusIcon() + "] " + tasks[selecter].description); + } else if (instruction.startsWith("todo ")) { + String description = instruction.substring(5); + tasks[pointer] = new Todo(description); + pointer++; + System.out.println("Got it, I've added this task: "); + System.out.println(" " + tasks[pointer - 1]); + System.out.println("Now you have " + pointer + " tasks in the list."); + } else if (instruction.startsWith("deadline ")) { + String[] parts = instruction.substring(9).split(" /by "); + tasks[pointer] = new Deadline(parts[0], parts[1]); + pointer++; + System.out.println("Got it, I've added this task: "); + System.out.println(" " + tasks[pointer - 1]); + System.out.println("Now you have " + pointer + " tasks in the list."); + } else if (instruction.startsWith("event ")) { + String[] parts = instruction.substring(6).split(" /from "); + String[] times = parts[1].split(" /to "); + tasks[pointer] = new Event(parts[0], times[0], times[1]); + pointer++; + System.out.println("Got it, I've added this task: "); + System.out.println(" " + tasks[pointer - 1]); + System.out.println("Now you have " + pointer + " tasks in the list."); + } else if (instruction.startsWith("mark ")) { + int index = Integer.parseInt(instruction.split(" ")[1]) - 1; + if (index >= 0 && index < pointer) { + tasks[index].markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(" " + tasks[index]); } else { - System.out.println("Task does not exist! Please try again."); + System.out.println("Invalid task number! Please try again."); } - - } else if (instruction.split(" ")[0].equals("unmark")) { - int selecter = Integer.parseInt(instruction.split(" ")[1]) - 1; - if (selecter >= 0 && selecter < pointer) { - tasks[selecter].isDone = false; - System.out.println("OK, I've marked this task as not done yet: \n" - + "[" + tasks[selecter].getStatusIcon() + "] " + tasks[selecter].description); + } else if (instruction.startsWith("unmark ")) { + int index = Integer.parseInt(instruction.split(" ")[1]) - 1; + if (index >= 0 && index < pointer) { + tasks[index].markUndone(); + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(" " + tasks[index]); } else { - System.out.println("Task does not exist! Please try again."); + System.out.println("Invalid task number! Please try again."); } - } else { - System.out.println("added: " + instruction); // Repeat the input - tasks[pointer] = new Task(instruction); // Add the instruction to the array - pointer++; // Move the pointer to the next index } - - instruction = scanner.nextLine(); // Read the next input + instruction = scanner.nextLine(); } + System.out.println("Bye. Hope to see you again soon!"); - scanner.close(); // Close the scanner + scanner.close(); } } diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..1840c63be --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,14 @@ +public class Deadline extends Task { + + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return " [D]" + super.toString() + " (by: " + by + ")"; + } +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..1ce792772 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,16 @@ +public class Event extends Task { + + protected String from; + protected String to; + + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + } + + @Override + public String toString() { + return " [E]" + super.toString() + " (from: " + from + " to: " + ")"; + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index ac9e0d9c1..6efaf3ab8 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -10,4 +10,16 @@ public Task(String description) { public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + + public void markAsDone() { + this.isDone = true; + } + + public void markUndone() { + this.isDone = false; + } + + public String toString() { + return "[" + getStatusIcon() + "] " + description; + } } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..0ac50094e --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,13 @@ +public class Todo extends Task { + + protected String by; + + public Todo (String description) { + super(description); + } + + @Override + public String toString() { + return " [T]" + super.toString(); + } +} From 4fb615c7e8b383eafd2e91696e90c59fb8534617 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Fri, 24 Jan 2025 23:51:05 +0800 Subject: [PATCH 08/11] Usage of inheritance for Level 4 --- src/main/java/Clarawr.java | 3 +++ src/main/java/Todo.java | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/Clarawr.java b/src/main/java/Clarawr.java index 280cb8bf5..6df47e147 100644 --- a/src/main/java/Clarawr.java +++ b/src/main/java/Clarawr.java @@ -63,6 +63,9 @@ public static void main(String[] args) { System.out.println("Invalid task number! Please try again."); } } + else { + System.out.println("Sorry I do not understand your instruction :("); + } instruction = scanner.nextLine(); } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index 0ac50094e..6ab1af9d9 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -1,7 +1,5 @@ public class Todo extends Task { - protected String by; - public Todo (String description) { super(description); } From 506c3410e6c0225f5b6b1be75ec58a28a905d869 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Sat, 25 Jan 2025 14:26:08 +0800 Subject: [PATCH 09/11] Usage of inheritance for Level 4 --- src/main/java/Event.java | 2 +- text-ui-test/EXPECTED.TXT | 57 ++++++++++++++++++++++++++++++++++----- text-ui-test/input.txt | 16 +++++++++++ text-ui-test/runtest.bat | 18 +++++++++---- 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 1ce792772..6c6656d87 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -11,6 +11,6 @@ public Event(String description, String from, String to) { @Override public String toString() { - return " [E]" + super.toString() + " (from: " + from + " to: " + ")"; + return " [E]" + super.toString() + " (from: " + from + " to: " + to + ")"; } } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..cdccccbf8 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,50 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| - +Hello! I'm Clarawr +What can I do for you? +Got it, I've added this task: + [T][ ] revise CS2106 +Now you have 1 tasks in the list. +Got it, I've added this task: + [D][ ] CS2103 (by: Fri 4pm) +Now you have 2 tasks in the list. +Got it, I've added this task: + [E][ ] internship interview (from: Mon 4pm to: Mon 5pm) +Now you have 3 tasks in the list. +Here are the tasks in your list: +1. [T][ ] revise CS2106 +2. [D][ ] CS2103 (by: Fri 4pm) +3. [E][ ] internship interview (from: Mon 4pm to: Mon 5pm) +Got it, I've added this task: + [T][ ] dinner with parents +Now you have 4 tasks in the list. +Here are the tasks in your list: +1. [T][ ] revise CS2106 +2. [D][ ] CS2103 (by: Fri 4pm) +3. [E][ ] internship interview (from: Mon 4pm to: Mon 5pm) +4. [T][ ] dinner with parents +Nice! I've marked this task as done: + [T][X] dinner with parents +Nice! I've marked this task as done: + [E][X] internship interview (from: Mon 4pm to: Mon 5pm) +Here are the tasks in your list: +1. [T][ ] revise CS2106 +2. [D][ ] CS2103 (by: Fri 4pm) +3. [E][X] internship interview (from: Mon 4pm to: Mon 5pm) +4. [T][X] dinner with parents +Invalid task number! Please try again. +Nice! I've marked this task as done: + [T][X] revise CS2106 +Nice! I've marked this task as done: + [D][X] CS2103 (by: Fri 4pm) +Here are the tasks in your list: +1. [T][X] revise CS2106 +2. [D][X] CS2103 (by: Fri 4pm) +3. [E][X] internship interview (from: Mon 4pm to: Mon 5pm) +4. [T][X] dinner with parents +OK, I've marked this task as not done yet: + [D][ ] CS2103 (by: Fri 4pm) +Here are the tasks in your list: +1. [T][X] revise CS2106 +2. [D][ ] CS2103 (by: Fri 4pm) +3. [E][X] internship interview (from: Mon 4pm to: Mon 5pm) +4. [T][X] dinner with parents +Bye. Hope to see you again soon! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..1ea858eb8 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,16 @@ +todo revise CS2106 +deadline CS2103 /by Fri 4pm +event internship interview /from Mon 4pm /to Mon 5pm +list +todo dinner with parents +list +mark 4 +mark 3 +list +mark 5 +mark 1 +mark 2 +list +unmark 2 +list +bye diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..37e7b526a 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -4,18 +4,26 @@ REM create bin directory if it doesn't exist if not exist ..\bin mkdir ..\bin REM delete output from previous run -if exist ACTUAL.TXT del ACTUAL.TXT + REM compile the code into the bin folder javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 +) ELSE ( + echo Build OK! ) -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 Clarawr < input.txt > ACTUAL.TXT + +REM compare the output to the expected output, ignoring white space differences +FC /L /W ACTUAL.TXT EXPECTED.TXT -REM compare the output to the expected output -FC ACTUAL.TXT EXPECTED.TXT +REM Check if FC found a difference +IF ERRORLEVEL 1 ( + echo Something is wrong! Files do not match. +) ELSE ( + echo Yes! All the files are the same. Test passed! :D +) From 9e108be3265a40e63645758f24f2700e37f713d6 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Sat, 25 Jan 2025 14:49:02 +0800 Subject: [PATCH 10/11] Handling Exceptions where user types in incomplete information --- src/main/java/Clarawr.java | 113 ++++++++++++++++------------ src/main/java/ClarawrException.java | 6 ++ 2 files changed, 70 insertions(+), 49 deletions(-) create mode 100644 src/main/java/ClarawrException.java diff --git a/src/main/java/Clarawr.java b/src/main/java/Clarawr.java index 6df47e147..f50973b46 100644 --- a/src/main/java/Clarawr.java +++ b/src/main/java/Clarawr.java @@ -13,58 +13,73 @@ public static void main(String[] args) { String instruction = scanner.nextLine(); while (!instruction.equalsIgnoreCase("bye")) { - if (instruction.equalsIgnoreCase("list")) { - if (pointer == 0) { - System.out.println("No tasks in your list."); - } else { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < pointer; i++) { - System.out.println((i + 1) + "." + tasks[i]); + try { + if (instruction.equalsIgnoreCase("list")) { + if (pointer == 0) { + System.out.println("No tasks in your list."); + } else { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < pointer; i++) { + System.out.println((i + 1) + "." + tasks[i]); + } + } + } else if (instruction.startsWith("todo")) { + String description = instruction.substring(4); + if (description.isEmpty()) { + throw new ClarawrException("The description of a todo task cannot be empty."); + } + tasks[pointer] = new Todo(description); + pointer++; + System.out.println("Got it, I've added this task: "); + System.out.println(" " + tasks[pointer - 1]); + System.out.println("Now you have " + pointer + " tasks in the list."); + } else if (instruction.startsWith("deadline")) { + String[] parts = instruction.substring(8).split(" /by "); + if (parts.length < 2 || parts[0].isEmpty() || parts[1].isEmpty()) { + throw new ClarawrException("The description and deadline of a task cannot be empty."); + } + tasks[pointer] = new Deadline(parts[0], parts[1]); + pointer++; + System.out.println("Got it, I've added this task: "); + System.out.println(" " + tasks[pointer - 1]); + System.out.println("Now you have " + pointer + " tasks in the list."); + } else if (instruction.startsWith("event")) { + String[] parts = instruction.substring(5).split(" /from "); + if (parts.length < 2) { + throw new ClarawrException("The description and timing of an event cannot be empty."); + } + String[] times = parts[1].split(" /to "); + if (times.length < 2 || parts[0].isEmpty() || times[0].isEmpty() || times[1].isEmpty()) { + throw new ClarawrException("The description and both event times cannot be empty."); + } + tasks[pointer] = new Event(parts[0], times[0], times[1]); + pointer++; + System.out.println("Got it, I've added this task: "); + System.out.println(" " + tasks[pointer - 1]); + System.out.println("Now you have " + pointer + " tasks in the list."); + } else if (instruction.startsWith("mark ")) { + int index = Integer.parseInt(instruction.split(" ")[1]) - 1; + if (index >= 0 && index < pointer) { + tasks[index].markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(" " + tasks[index]); + } else { + System.out.println("Invalid task number! Please try again."); + } + } else if (instruction.startsWith("unmark ")) { + int index = Integer.parseInt(instruction.split(" ")[1]) - 1; + if (index >= 0 && index < pointer) { + tasks[index].markUndone(); + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(" " + tasks[index]); + } else { + System.out.println("Invalid task number! Please try again."); } - } - } else if (instruction.startsWith("todo ")) { - String description = instruction.substring(5); - tasks[pointer] = new Todo(description); - pointer++; - System.out.println("Got it, I've added this task: "); - System.out.println(" " + tasks[pointer - 1]); - System.out.println("Now you have " + pointer + " tasks in the list."); - } else if (instruction.startsWith("deadline ")) { - String[] parts = instruction.substring(9).split(" /by "); - tasks[pointer] = new Deadline(parts[0], parts[1]); - pointer++; - System.out.println("Got it, I've added this task: "); - System.out.println(" " + tasks[pointer - 1]); - System.out.println("Now you have " + pointer + " tasks in the list."); - } else if (instruction.startsWith("event ")) { - String[] parts = instruction.substring(6).split(" /from "); - String[] times = parts[1].split(" /to "); - tasks[pointer] = new Event(parts[0], times[0], times[1]); - pointer++; - System.out.println("Got it, I've added this task: "); - System.out.println(" " + tasks[pointer - 1]); - System.out.println("Now you have " + pointer + " tasks in the list."); - } else if (instruction.startsWith("mark ")) { - int index = Integer.parseInt(instruction.split(" ")[1]) - 1; - if (index >= 0 && index < pointer) { - tasks[index].markAsDone(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(" " + tasks[index]); - } else { - System.out.println("Invalid task number! Please try again."); - } - } else if (instruction.startsWith("unmark ")) { - int index = Integer.parseInt(instruction.split(" ")[1]) - 1; - if (index >= 0 && index < pointer) { - tasks[index].markUndone(); - System.out.println("OK, I've marked this task as not done yet:"); - System.out.println(" " + tasks[index]); } else { - System.out.println("Invalid task number! Please try again."); + System.out.println("Sorry I do not understand your instruction :("); } - } - else { - System.out.println("Sorry I do not understand your instruction :("); + } catch (ClarawrException e) { + System.out.println(e.getMessage()); } instruction = scanner.nextLine(); } diff --git a/src/main/java/ClarawrException.java b/src/main/java/ClarawrException.java new file mode 100644 index 000000000..0a94b1bcd --- /dev/null +++ b/src/main/java/ClarawrException.java @@ -0,0 +1,6 @@ +public class ClarawrException extends Exception { + + public ClarawrException(String message) { + super(message); + } +} From 78b94dbf5d09713adac977e0fd5381a6c27c7b41 Mon Sep 17 00:00:00 2001 From: brvndonkoh Date: Sat, 25 Jan 2025 15:42:50 +0800 Subject: [PATCH 11/11] Now we can delete/remove tasks with Clarawr :D --- src/main/java/Clarawr.java | 55 +++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/main/java/Clarawr.java b/src/main/java/Clarawr.java index f50973b46..7678705f5 100644 --- a/src/main/java/Clarawr.java +++ b/src/main/java/Clarawr.java @@ -1,8 +1,8 @@ import java.util.Scanner; +import java.util.ArrayList; public class Clarawr { - static int pointer = 0; - static Task[] tasks = new Task[100]; + static ArrayList tasks = new ArrayList<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); @@ -15,34 +15,32 @@ public static void main(String[] args) { while (!instruction.equalsIgnoreCase("bye")) { try { if (instruction.equalsIgnoreCase("list")) { - if (pointer == 0) { + if (tasks.isEmpty()) { System.out.println("No tasks in your list."); } else { System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < pointer; i++) { - System.out.println((i + 1) + "." + tasks[i]); + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + "." + tasks.get(i)); } } } else if (instruction.startsWith("todo")) { - String description = instruction.substring(4); + String description = instruction.substring(4).trim(); if (description.isEmpty()) { throw new ClarawrException("The description of a todo task cannot be empty."); } - tasks[pointer] = new Todo(description); - pointer++; + tasks.add(new Todo(description)); System.out.println("Got it, I've added this task: "); - System.out.println(" " + tasks[pointer - 1]); - System.out.println("Now you have " + pointer + " tasks in the list."); + System.out.println(" " + tasks.get(tasks.size() - 1)); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } else if (instruction.startsWith("deadline")) { String[] parts = instruction.substring(8).split(" /by "); if (parts.length < 2 || parts[0].isEmpty() || parts[1].isEmpty()) { throw new ClarawrException("The description and deadline of a task cannot be empty."); } - tasks[pointer] = new Deadline(parts[0], parts[1]); - pointer++; + tasks.add(new Deadline(parts[0], parts[1])); System.out.println("Got it, I've added this task: "); - System.out.println(" " + tasks[pointer - 1]); - System.out.println("Now you have " + pointer + " tasks in the list."); + System.out.println(" " + tasks.get(tasks.size() - 1)); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } else if (instruction.startsWith("event")) { String[] parts = instruction.substring(5).split(" /from "); if (parts.length < 2) { @@ -52,26 +50,35 @@ public static void main(String[] args) { if (times.length < 2 || parts[0].isEmpty() || times[0].isEmpty() || times[1].isEmpty()) { throw new ClarawrException("The description and both event times cannot be empty."); } - tasks[pointer] = new Event(parts[0], times[0], times[1]); - pointer++; + tasks.add(new Event(parts[0], times[0], times[1])); System.out.println("Got it, I've added this task: "); - System.out.println(" " + tasks[pointer - 1]); - System.out.println("Now you have " + pointer + " tasks in the list."); + System.out.println(" " + tasks.get(tasks.size() - 1)); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } else if (instruction.startsWith("mark ")) { int index = Integer.parseInt(instruction.split(" ")[1]) - 1; - if (index >= 0 && index < pointer) { - tasks[index].markAsDone(); + if (index >= 0 && index < tasks.size()) { + tasks.get(index).markAsDone(); System.out.println("Nice! I've marked this task as done:"); - System.out.println(" " + tasks[index]); + System.out.println(" " + tasks.get(index)); } else { System.out.println("Invalid task number! Please try again."); } } else if (instruction.startsWith("unmark ")) { int index = Integer.parseInt(instruction.split(" ")[1]) - 1; - if (index >= 0 && index < pointer) { - tasks[index].markUndone(); + if (index >= 0 && index < tasks.size()) { + tasks.get(index).markUndone(); System.out.println("OK, I've marked this task as not done yet:"); - System.out.println(" " + tasks[index]); + System.out.println(" " + tasks.get(index)); + } else { + System.out.println("Invalid task number! Please try again."); + } + } else if (instruction.startsWith("delete ") || instruction.startsWith("remove ")) { + int index = Integer.parseInt(instruction.split(" ")[1]) - 1; + if (index >= 0 && index < tasks.size()) { + Task taskToDelete = tasks.remove(index); + System.out.println("Noted. I've removed this task: "); + System.out.println(" " + taskToDelete); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } else { System.out.println("Invalid task number! Please try again."); }