From bb2d95a6039d0962d433fc49de91af6efd86ed3b Mon Sep 17 00:00:00 2001 From: Gyurim Date: Fri, 7 Feb 2025 01:36:38 +0800 Subject: [PATCH 01/26] Level-0. Rename, Greet, Exit --- src/main/java/David.java | 18 ++++++++++++++++++ src/main/java/Duke.java | 10 ---------- 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 src/main/java/David.java delete mode 100644 src/main/java/Duke.java diff --git a/src/main/java/David.java b/src/main/java/David.java new file mode 100644 index 000000000..5b9c45c8e --- /dev/null +++ b/src/main/java/David.java @@ -0,0 +1,18 @@ +public class David { + public static void main(String[] args) { +// String logo = " ____ _ \n" +// + "| _ \\ _ _| | _____ \n" +// + "| | | | | | | |/ / _ \\\n" +// + "| |_| | |_| | < __/\n" +// + "|____/ \\__,_|_|\\_\\___|\n"; +// System.out.println("Hello from\n" + logo); + + String greet = "____________________________________________________________\n" + + " Hello! I'm David\n" + + " What can I do for you?\n" + + "____________________________________________________________\n" + + " Bye. Hope to see you again soon!\n" + + "____________________________________________________________"; + System.out.println(greet); + } +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334c..000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} From 8a51432f6eeff00baa9514ad7f62e4c352e928cb Mon Sep 17 00:00:00 2001 From: Gyurim Date: Fri, 7 Feb 2025 01:43:56 +0800 Subject: [PATCH 02/26] Level-1: Echo --- src/main/java/David.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index 5b9c45c8e..572d597bf 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -1,3 +1,4 @@ +import java.util.Scanner; public class David { public static void main(String[] args) { // String logo = " ____ _ \n" @@ -7,12 +8,20 @@ public static void main(String[] args) { // + "|____/ \\__,_|_|\\_\\___|\n"; // System.out.println("Hello from\n" + logo); - String greet = "____________________________________________________________\n" + + System.out.println("____________________________________________________________\n" + " Hello! I'm David\n" + " What can I do for you?\n" + - "____________________________________________________________\n" + - " Bye. Hope to see you again soon!\n" + - "____________________________________________________________"; - System.out.println(greet); + "____________________________________________________________\n"); + Scanner scanner = new Scanner(System.in); + String input = scanner.nextLine(); + while(!input.equals("bye")) { + + System.out.println("____________________________________________________________\n" + + input + "!!\n" + "____________________________________________________________"); + input = scanner.nextLine(); + } + + System.out.println(" Bye. Hope to see you again soon!\n" + + "____________________________________________________________"); } } \ No newline at end of file From 1579809798b65d490d1ddae784e0aca736723c84 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Fri, 7 Feb 2025 01:46:06 +0800 Subject: [PATCH 03/26] Level 2: Add, List --- src/main/java/David.java | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index 572d597bf..12e5be2f8 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -7,21 +7,31 @@ public static void main(String[] args) { // + "| |_| | |_| | < __/\n" // + "|____/ \\__,_|_|\\_\\___|\n"; // System.out.println("Hello from\n" + logo); - + String[] values = new String[100]; System.out.println("____________________________________________________________\n" + " Hello! I'm David\n" + " What can I do for you?\n" + "____________________________________________________________\n"); Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); - while(!input.equals("bye")) { - - System.out.println("____________________________________________________________\n" - + input + "!!\n" + "____________________________________________________________"); + int i = 0; + while (!input.equals("bye")) { + if(input.equals("list")) { + for (int j = 0; j < i ; j++) { + System.out.println("____________________________________________________________\n" + (j + 1) + "." + values[j]); + } + } + else { + System.out.println("____________________________________________________________\n" + + "added: " + input + "\n" + "____________________________________________________________"); + values[i] = input; // store user input in array + i++; + } input = scanner.nextLine(); - } + + } System.out.println(" Bye. Hope to see you again soon!\n" + "____________________________________________________________"); } -} \ No newline at end of file +} From 5c1f7d53a8a7af9c79a293f89bfd8f94eee999b9 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Fri, 7 Feb 2025 01:47:43 +0800 Subject: [PATCH 04/26] Level-3: Mark as Done --- src/main/java/David.java | 79 +++++++++++++++++++++++++++++++--------- src/main/java/Task.java | 18 +++++++++ 2 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/David.java b/src/main/java/David.java index 12e5be2f8..af73ad9af 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -1,37 +1,82 @@ import java.util.Scanner; public class David { + public static final String LINE_SEPERATOR = "____________________________________________________________"; public static void main(String[] args) { -// String logo = " ____ _ \n" -// + "| _ \\ _ _| | _____ \n" -// + "| | | | | | | |/ / _ \\\n" -// + "| |_| | |_| | < __/\n" -// + "|____/ \\__,_|_|\\_\\___|\n"; -// System.out.println("Hello from\n" + logo); + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); String[] values = new String[100]; - System.out.println("____________________________________________________________\n" + - " Hello! I'm David\n" + - " What can I do for you?\n" + - "____________________________________________________________\n"); + Task[] task = new Task[100]; + + printHello(); Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); int i = 0; while (!input.equals("bye")) { - if(input.equals("list")) { - for (int j = 0; j < i ; j++) { - System.out.println("____________________________________________________________\n" + (j + 1) + "." + values[j]); + if (input.equals("list")) { + System.out.println(LINE_SEPERATOR + "\n" + "Here are the tasks in your list:\n"); + for (int j = 0; j < i; j++) { + System.out.println((j + 1) + "." + "[" + task[j].getStatusIcon() + "] " + values[j] + "\n" + LINE_SEPERATOR); + int index = input.indexOf(" "); } } + + else if(input.startsWith("mark")) { + markTask(task, getIndex(input)); + printMark(task,getIndex(input)); + + } + else if(input.startsWith("unmark")) { + unmarkTask(task, getIndex(input)); + printUnmark(task,getIndex(input)); + } else { - System.out.println("____________________________________________________________\n" - + "added: " + input + "\n" + "____________________________________________________________"); + System.out.println(LINE_SEPERATOR + "\n" + + "added: " + input + "\n" + LINE_SEPERATOR); values[i] = input; // store user input in array + task[i] = new Task(input); i++; } input = scanner.nextLine(); - } + printBye(); + } + + private static void printHello() { + System.out.println(LINE_SEPERATOR + "\n" + + " Hello! I'm David\n" + + " What can I do for you?\n" + + LINE_SEPERATOR + "\n"); + } + + private static void printBye() { System.out.println(" Bye. Hope to see you again soon!\n" + - "____________________________________________________________"); + LINE_SEPERATOR); + } + + public static void markTask(Task[] task, int index){ + task[index].isDone = true;; + } + + public static void unmarkTask(Task[] task, int index){ + task[index].isDone = false; + } + + public static void printMark(Task[] task, int index){ + System.out.println(LINE_SEPERATOR + "\n" + "Nice! I've marked this task as done:\n" + "[" + task[index].getStatusIcon() + "] " + task[index].getDescription() + "\n" + LINE_SEPERATOR); + } + + public static void printUnmark(Task[] task, int index){ + System.out.println(LINE_SEPERATOR + "\n" + "[" + task[index].getStatusIcon() + "] " + task[index].getDescription() + "\n" + LINE_SEPERATOR); + } + + public static int getIndex(String marking){ + int index = marking.indexOf(" "); + return (Integer.parseInt(marking.substring(index + 1)) - 1); } } + diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..84ef5a2e5 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,18 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + + public String getDescription() { + return description; + } + +} \ No newline at end of file From adca4dd49a14dc68e37cd63a9f5a40eb666331c3 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Fri, 7 Feb 2025 01:49:33 +0800 Subject: [PATCH 05/26] A-CodeStandard --- src/main/java/David.java | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index af73ad9af..f810fbeb6 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -1,6 +1,8 @@ import java.util.Scanner; + public class David { public static final String LINE_SEPERATOR = "____________________________________________________________"; + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -22,18 +24,14 @@ public static void main(String[] args) { System.out.println((j + 1) + "." + "[" + task[j].getStatusIcon() + "] " + values[j] + "\n" + LINE_SEPERATOR); int index = input.indexOf(" "); } - } - - else if(input.startsWith("mark")) { + } else if (input.startsWith("mark")) { markTask(task, getIndex(input)); - printMark(task,getIndex(input)); + printMark(task, getIndex(input)); - } - else if(input.startsWith("unmark")) { + } else if (input.startsWith("unmark")) { unmarkTask(task, getIndex(input)); - printUnmark(task,getIndex(input)); - } - else { + printUnmark(task, getIndex(input)); + } else { System.out.println(LINE_SEPERATOR + "\n" + "added: " + input + "\n" + LINE_SEPERATOR); values[i] = input; // store user input in array @@ -58,23 +56,24 @@ private static void printBye() { LINE_SEPERATOR); } - public static void markTask(Task[] task, int index){ - task[index].isDone = true;; + public static void markTask(Task[] task, int index) { + task[index].isDone = true; + ; } - public static void unmarkTask(Task[] task, int index){ + public static void unmarkTask(Task[] task, int index) { task[index].isDone = false; } - public static void printMark(Task[] task, int index){ + public static void printMark(Task[] task, int index) { System.out.println(LINE_SEPERATOR + "\n" + "Nice! I've marked this task as done:\n" + "[" + task[index].getStatusIcon() + "] " + task[index].getDescription() + "\n" + LINE_SEPERATOR); } - public static void printUnmark(Task[] task, int index){ + public static void printUnmark(Task[] task, int index) { System.out.println(LINE_SEPERATOR + "\n" + "[" + task[index].getStatusIcon() + "] " + task[index].getDescription() + "\n" + LINE_SEPERATOR); } - public static int getIndex(String marking){ + public static int getIndex(String marking) { int index = marking.indexOf(" "); return (Integer.parseInt(marking.substring(index + 1)) - 1); } From 303d7cb17a8d36a528d62c6e41bbb9dff5db62b3 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Fri, 7 Feb 2025 01:52:06 +0800 Subject: [PATCH 06/26] Level 4: ToDo, Events, Deadlines --- src/main/java/David.java | 43 ++++++++++++++++++++++++++----------- src/main/java/Deadline.java | 13 +++++++++++ src/main/java/Event.java | 16 ++++++++++++++ src/main/java/Task.java | 5 +++++ src/main/java/Todo.java | 11 ++++++++++ 5 files changed, 76 insertions(+), 12 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/David.java b/src/main/java/David.java index f810fbeb6..25884cdc0 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -10,31 +10,42 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - String[] values = new String[100]; Task[] task = new Task[100]; printHello(); Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); int i = 0; + char taskType = input.charAt(input.indexOf("[") + 1); while (!input.equals("bye")) { if (input.equals("list")) { System.out.println(LINE_SEPERATOR + "\n" + "Here are the tasks in your list:\n"); for (int j = 0; j < i; j++) { - System.out.println((j + 1) + "." + "[" + task[j].getStatusIcon() + "] " + values[j] + "\n" + LINE_SEPERATOR); - int index = input.indexOf(" "); + System.out.println((j + 1) + "." + task[j].toString() + "\n" + LINE_SEPERATOR); } } else if (input.startsWith("mark")) { - markTask(task, getIndex(input)); - printMark(task, getIndex(input)); + markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); + printMark(task, Integer.parseInt(input.split(" ")[1]) - 1); } else if (input.startsWith("unmark")) { - unmarkTask(task, getIndex(input)); - printUnmark(task, getIndex(input)); + unmarkTask(task, Integer.parseInt(input.split(" ")[1]) - 1); + printUnmark(task, Integer.parseInt(input.split(" ")[1]) - 1); + } else if (input.startsWith("todo")) { + task[i] = new Todo(input.substring(getIndex(input, " ") + 1)); + System.out.println(printTaskType(task, i)); + i++; + } else if (input.startsWith("deadline")) { + task[i] = new Deadline((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "by") + 3)); + System.out.println(printTaskType(task, i)); + i++; + } else if (input.startsWith("event")) { + task[i] = new Event((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "from") + 5, getIndex(input, "to") - 2), input.substring(getIndex(input, "to") + 3)); + System.out.println(printTaskType(task, i)); + i++; + } else { System.out.println(LINE_SEPERATOR + "\n" + "added: " + input + "\n" + LINE_SEPERATOR); - values[i] = input; // store user input in array task[i] = new Task(input); i++; } @@ -44,6 +55,10 @@ public static void main(String[] args) { printBye(); } + private static String printTaskType(Task[] task, int i) { + return LINE_SEPERATOR + "\n" + " Got it. I've added this task:\n" + task[i].toString() + "\n" + "Now you have " + (i + 1) + " tasks in the list.\n" + LINE_SEPERATOR; + } + private static void printHello() { System.out.println(LINE_SEPERATOR + "\n" + " Hello! I'm David\n" + @@ -58,7 +73,6 @@ private static void printBye() { public static void markTask(Task[] task, int index) { task[index].isDone = true; - ; } public static void unmarkTask(Task[] task, int index) { @@ -73,9 +87,14 @@ public static void printUnmark(Task[] task, int index) { System.out.println(LINE_SEPERATOR + "\n" + "[" + task[index].getStatusIcon() + "] " + task[index].getDescription() + "\n" + LINE_SEPERATOR); } - public static int getIndex(String marking) { - int index = marking.indexOf(" "); - return (Integer.parseInt(marking.substring(index + 1)) - 1); + public static int getIndex(String marking, String find) { + int index = marking.indexOf(find); + if (index == -1) { + System.out.println("Error: Could not find space in the input."); + return -1; + } + return index; } + } diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..5158549a8 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,13 @@ +public class Deadline extends Task { + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + super.toString() + " " + description + " (by: " + by + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..6d59d2b3c --- /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() + " " + description + " (from: " + from + " to: " + to + ")"; + } + +} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 84ef5a2e5..5e60414c1 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -15,4 +15,9 @@ public String getDescription() { return description; } + + @Override + public String toString() { + return "[" + getStatusIcon() + "]"; // You can customize this string as needed + } } \ No newline at end of file diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..0fd99f276 --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,11 @@ +public class Todo extends Task { + + public Todo(String description) { + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString() + " " + description; + } +} \ No newline at end of file From 7d56a7db1988a82c4efcc9888016609cb1cf2892 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Fri, 7 Feb 2025 17:26:54 +0800 Subject: [PATCH 07/26] A-TextUiTesting --- text-ui-test/EXPECTED.TXT | 5 +++++ text-ui-test/runtest.sh | 8 ++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..21735c16e 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -5,3 +5,8 @@ Hello from | |_| | |_| | < __/ |____/ \__,_|_|\_\___| +____________________________________________________________ + Hello! I'm David + What can I do for you? +____________________________________________________________ + diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index c9ec87003..a13b19dff 100644 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -20,14 +20,10 @@ then fi # 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 - -# convert to UNIX format -cp EXPECTED.TXT EXPECTED-UNIX.TXT -dos2unix ACTUAL.TXT EXPECTED-UNIX.TXT +java -classpath ../bin David < input.txt > ACTUAL.TXT # compare the output to the expected output -diff ACTUAL.TXT EXPECTED-UNIX.TXT +diff ACTUAL.TXT EXPECTED.TXT if [ $? -eq 0 ] then echo "Test result: PASSED" From f9fc435ecbea995f8052c2adf434fb18fd60e629 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Sat, 8 Feb 2025 21:41:39 +0800 Subject: [PATCH 08/26] A-CodeQuality --- src/main/java/David.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index 25884cdc0..fbecbffcd 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -16,7 +16,7 @@ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); int i = 0; - char taskType = input.charAt(input.indexOf("[") + 1); + char input_type = input.charAt(input.indexOf("[") + 1); while (!input.equals("bye")) { if (input.equals("list")) { System.out.println(LINE_SEPERATOR + "\n" + "Here are the tasks in your list:\n"); @@ -56,7 +56,9 @@ public static void main(String[] args) { } private static String printTaskType(Task[] task, int i) { - return LINE_SEPERATOR + "\n" + " Got it. I've added this task:\n" + task[i].toString() + "\n" + "Now you have " + (i + 1) + " tasks in the list.\n" + LINE_SEPERATOR; + return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() + + task[i].toString() + System.lineSeparator() + + "Now you have " + (i + 1) + " tasks in the list." + System.lineSeparator() + LINE_SEPERATOR; } private static void printHello() { From 4f9f8676999735d7ba1924d99f983355020e7cc7 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Sat, 8 Feb 2025 21:41:52 +0800 Subject: [PATCH 09/26] Revert "A-CodeQuality" This reverts commit f9fc435ecbea995f8052c2adf434fb18fd60e629. --- src/main/java/David.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index fbecbffcd..25884cdc0 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -16,7 +16,7 @@ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); int i = 0; - char input_type = input.charAt(input.indexOf("[") + 1); + char taskType = input.charAt(input.indexOf("[") + 1); while (!input.equals("bye")) { if (input.equals("list")) { System.out.println(LINE_SEPERATOR + "\n" + "Here are the tasks in your list:\n"); @@ -56,9 +56,7 @@ public static void main(String[] args) { } private static String printTaskType(Task[] task, int i) { - return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() - + task[i].toString() + System.lineSeparator() - + "Now you have " + (i + 1) + " tasks in the list." + System.lineSeparator() + LINE_SEPERATOR; + return LINE_SEPERATOR + "\n" + " Got it. I've added this task:\n" + task[i].toString() + "\n" + "Now you have " + (i + 1) + " tasks in the list.\n" + LINE_SEPERATOR; } private static void printHello() { From 317a6ea22ddeccbede5a2c940ecacd57e973f171 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Sat, 8 Feb 2025 21:48:07 +0800 Subject: [PATCH 10/26] Committed A-Code-Quality code in Level 5 Branch --- src/main/java/David.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index 25884cdc0..fbecbffcd 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -16,7 +16,7 @@ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); int i = 0; - char taskType = input.charAt(input.indexOf("[") + 1); + char input_type = input.charAt(input.indexOf("[") + 1); while (!input.equals("bye")) { if (input.equals("list")) { System.out.println(LINE_SEPERATOR + "\n" + "Here are the tasks in your list:\n"); @@ -56,7 +56,9 @@ public static void main(String[] args) { } private static String printTaskType(Task[] task, int i) { - return LINE_SEPERATOR + "\n" + " Got it. I've added this task:\n" + task[i].toString() + "\n" + "Now you have " + (i + 1) + " tasks in the list.\n" + LINE_SEPERATOR; + return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() + + task[i].toString() + System.lineSeparator() + + "Now you have " + (i + 1) + " tasks in the list." + System.lineSeparator() + LINE_SEPERATOR; } private static void printHello() { From 1b298107983d65a8a0b10c75465b4c0334de42dc Mon Sep 17 00:00:00 2001 From: Gyurim Date: Sat, 8 Feb 2025 22:04:22 +0800 Subject: [PATCH 11/26] Level 5. Handle Errors --- src/main/java/David.java | 68 ++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index fbecbffcd..ab211f593 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -18,37 +18,44 @@ public static void main(String[] args) { int i = 0; char input_type = input.charAt(input.indexOf("[") + 1); while (!input.equals("bye")) { - if (input.equals("list")) { - System.out.println(LINE_SEPERATOR + "\n" + "Here are the tasks in your list:\n"); - for (int j = 0; j < i; j++) { - System.out.println((j + 1) + "." + task[j].toString() + "\n" + LINE_SEPERATOR); + try { + if (input.equals("list")) { + System.out.println(LINE_SEPERATOR + "\n" + "Here are the tasks in your list:\n"); + for (int j = 0; j < i; j++) { + System.out.println((j + 1) + "." + task[j].toString() + "\n" + LINE_SEPERATOR); + } + } else if (input.startsWith("mark")) { + markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); + printMark(task, Integer.parseInt(input.split(" ")[1]) - 1); + + } else if (input.startsWith("unmark")) { + unmarkTask(task, Integer.parseInt(input.split(" ")[1]) - 1); + printUnmark(task, Integer.parseInt(input.split(" ")[1]) - 1); + } else if (input.startsWith("todo")) { + task[i] = new Todo(input.substring(getIndex(input, " ") + 1)); + System.out.println(printTaskType(task, i)); + i++; + } else if (input.startsWith("deadline")) { + task[i] = new Deadline((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "by") + 3)); + System.out.println(printTaskType(task, i)); + i++; + } else if (input.startsWith("event")) { + task[i] = new Event((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "from") + 5, getIndex(input, "to") - 2), input.substring(getIndex(input, "to") + 3)); + System.out.println(printTaskType(task, i)); + i++; + + } else { +// System.out.println(LINE_SEPERATOR + "\n" +// + "added: " + input + "\n" + LINE_SEPERATOR); +// task[i] = new Task(input); +// i++; + throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR); } - } else if (input.startsWith("mark")) { - markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); - printMark(task, Integer.parseInt(input.split(" ")[1]) - 1); - - } else if (input.startsWith("unmark")) { - unmarkTask(task, Integer.parseInt(input.split(" ")[1]) - 1); - printUnmark(task, Integer.parseInt(input.split(" ")[1]) - 1); - } else if (input.startsWith("todo")) { - task[i] = new Todo(input.substring(getIndex(input, " ") + 1)); - System.out.println(printTaskType(task, i)); - i++; - } else if (input.startsWith("deadline")) { - task[i] = new Deadline((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "by") + 3)); - System.out.println(printTaskType(task, i)); - i++; - } else if (input.startsWith("event")) { - task[i] = new Event((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "from") + 5, getIndex(input, "to") - 2), input.substring(getIndex(input, "to") + 3)); - System.out.println(printTaskType(task, i)); - i++; - - } else { - System.out.println(LINE_SEPERATOR + "\n" - + "added: " + input + "\n" + LINE_SEPERATOR); - task[i] = new Task(input); - i++; } + catch (IllegalArgumentException e) { + // Print the custom error message without stack trace + System.out.println(e.getMessage()); + } input = scanner.nextLine(); } @@ -92,8 +99,7 @@ public static void printUnmark(Task[] task, int index) { public static int getIndex(String marking, String find) { int index = marking.indexOf(find); if (index == -1) { - System.out.println("Error: Could not find space in the input."); - return -1; + throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Oops! The description of a task should NOT be blank! Please try again :)" + System.lineSeparator() + LINE_SEPERATOR); } return index; } From 8d425850a917b547eed9237f9f5fad080f29239a Mon Sep 17 00:00:00 2001 From: Gyurim Date: Sat, 8 Feb 2025 22:10:19 +0800 Subject: [PATCH 12/26] Created David Exceptions to handle errors --- src/main/java/David.java | 7 +++++-- src/main/java/DavidException.java | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 src/main/java/DavidException.java diff --git a/src/main/java/David.java b/src/main/java/David.java index ab211f593..6d8d0ce08 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -56,6 +56,9 @@ public static void main(String[] args) { // Print the custom error message without stack trace System.out.println(e.getMessage()); } + catch (DavidException e) { + System.out.println(LINE_SEPERATOR + System.lineSeparator() + " Oops! The description of a task should NOT be blank! Please try again :)" + System.lineSeparator() + LINE_SEPERATOR); + } input = scanner.nextLine(); } @@ -96,10 +99,10 @@ public static void printUnmark(Task[] task, int index) { System.out.println(LINE_SEPERATOR + "\n" + "[" + task[index].getStatusIcon() + "] " + task[index].getDescription() + "\n" + LINE_SEPERATOR); } - public static int getIndex(String marking, String find) { + public static int getIndex(String marking, String find) throws DavidException { int index = marking.indexOf(find); if (index == -1) { - throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Oops! The description of a task should NOT be blank! Please try again :)" + System.lineSeparator() + LINE_SEPERATOR); + throw new DavidException(); } return index; } diff --git a/src/main/java/DavidException.java b/src/main/java/DavidException.java new file mode 100644 index 000000000..60f87c541 --- /dev/null +++ b/src/main/java/DavidException.java @@ -0,0 +1,2 @@ +public class DavidException extends Exception{ +} From 5f0320fb179862d6900ad45c005917c3036fb56e Mon Sep 17 00:00:00 2001 From: Gyurim Date: Fri, 14 Feb 2025 15:04:14 +0800 Subject: [PATCH 13/26] A-Packages : Organize into packages --- src/main/java/David.java | 14 ++++++++------ src/main/java/{ => David/deadline}/Deadline.java | 3 +++ src/main/java/{ => David/event}/Event.java | 3 +++ src/main/java/{ => David/task}/Task.java | 5 +++++ src/main/java/{ => David/todo}/Todo.java | 3 +++ src/main/java/{ => David/ui}/DavidException.java | 2 ++ 6 files changed, 24 insertions(+), 6 deletions(-) rename src/main/java/{ => David/deadline}/Deadline.java (86%) rename src/main/java/{ => David/event}/Event.java (89%) rename src/main/java/{ => David/task}/Task.java (83%) rename src/main/java/{ => David/todo}/Todo.java (83%) rename src/main/java/{ => David/ui}/DavidException.java (72%) diff --git a/src/main/java/David.java b/src/main/java/David.java index 6d8d0ce08..85feb7f5e 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -1,5 +1,11 @@ import java.util.Scanner; +import David.task.Task; +import David.ui.DavidException; +import David.deadline.Deadline; +import David.event.Event; +import David.todo.Todo; + public class David { public static final String LINE_SEPERATOR = "____________________________________________________________"; @@ -45,10 +51,6 @@ public static void main(String[] args) { i++; } else { -// System.out.println(LINE_SEPERATOR + "\n" -// + "added: " + input + "\n" + LINE_SEPERATOR); -// task[i] = new Task(input); -// i++; throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR); } } @@ -84,11 +86,11 @@ private static void printBye() { } public static void markTask(Task[] task, int index) { - task[index].isDone = true; + task[index].setDone(true); } public static void unmarkTask(Task[] task, int index) { - task[index].isDone = false; + task[index].setDone(false); } public static void printMark(Task[] task, int index) { diff --git a/src/main/java/Deadline.java b/src/main/java/David/deadline/Deadline.java similarity index 86% rename from src/main/java/Deadline.java rename to src/main/java/David/deadline/Deadline.java index 5158549a8..4732090d3 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/David/deadline/Deadline.java @@ -1,3 +1,6 @@ +package David.deadline; +import David.task.Task; + public class Deadline extends Task { protected String by; diff --git a/src/main/java/Event.java b/src/main/java/David/event/Event.java similarity index 89% rename from src/main/java/Event.java rename to src/main/java/David/event/Event.java index 6d59d2b3c..2036fa221 100644 --- a/src/main/java/Event.java +++ b/src/main/java/David/event/Event.java @@ -1,3 +1,6 @@ +package David.event; +import David.task.Task; + public class Event extends Task { protected String from; protected String to; diff --git a/src/main/java/Task.java b/src/main/java/David/task/Task.java similarity index 83% rename from src/main/java/Task.java rename to src/main/java/David/task/Task.java index 5e60414c1..5cfeba924 100644 --- a/src/main/java/Task.java +++ b/src/main/java/David/task/Task.java @@ -1,3 +1,5 @@ +package David.task; + public class Task { protected String description; protected boolean isDone; @@ -15,6 +17,9 @@ public String getDescription() { return description; } + public boolean setDone(boolean status) { + return isDone = status; + } @Override public String toString() { diff --git a/src/main/java/Todo.java b/src/main/java/David/todo/Todo.java similarity index 83% rename from src/main/java/Todo.java rename to src/main/java/David/todo/Todo.java index 0fd99f276..376b4d07f 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/David/todo/Todo.java @@ -1,3 +1,6 @@ +package David.todo; +import David.task.Task; + public class Todo extends Task { public Todo(String description) { diff --git a/src/main/java/DavidException.java b/src/main/java/David/ui/DavidException.java similarity index 72% rename from src/main/java/DavidException.java rename to src/main/java/David/ui/DavidException.java index 60f87c541..b6b7456ad 100644 --- a/src/main/java/DavidException.java +++ b/src/main/java/David/ui/DavidException.java @@ -1,2 +1,4 @@ +package David.ui; + public class DavidException extends Exception{ } From ec1266d88379f1d1ba27920955c2d8c0561c6613 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 20 Feb 2025 01:40:00 +0800 Subject: [PATCH 14/26] Level-6 Delete --- src/main/java/David.java | 47 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index 85feb7f5e..2ce8ca0e4 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -1,5 +1,5 @@ import java.util.Scanner; - +import java.util.ArrayList; import David.task.Task; import David.ui.DavidException; import David.deadline.Deadline; @@ -16,8 +16,7 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - Task[] task = new Task[100]; - + ArrayList task = new ArrayList<>(); printHello(); Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); @@ -28,7 +27,7 @@ public static void main(String[] args) { if (input.equals("list")) { System.out.println(LINE_SEPERATOR + "\n" + "Here are the tasks in your list:\n"); for (int j = 0; j < i; j++) { - System.out.println((j + 1) + "." + task[j].toString() + "\n" + LINE_SEPERATOR); + System.out.println((j + 1) + "." + task.get(j).toString() + "\n" + LINE_SEPERATOR); } } else if (input.startsWith("mark")) { markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); @@ -38,19 +37,26 @@ public static void main(String[] args) { unmarkTask(task, Integer.parseInt(input.split(" ")[1]) - 1); printUnmark(task, Integer.parseInt(input.split(" ")[1]) - 1); } else if (input.startsWith("todo")) { - task[i] = new Todo(input.substring(getIndex(input, " ") + 1)); + task.add(new Todo(input.substring(getIndex(input, " ") + 1))); System.out.println(printTaskType(task, i)); i++; } else if (input.startsWith("deadline")) { - task[i] = new Deadline((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "by") + 3)); + task.add(new Deadline((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "by") + 3))); System.out.println(printTaskType(task, i)); i++; } else if (input.startsWith("event")) { - task[i] = new Event((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "from") + 5, getIndex(input, "to") - 2), input.substring(getIndex(input, "to") + 3)); + task.add(new Event((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "from") + 5, getIndex(input, "to") - 2), input.substring(getIndex(input, "to") + 3))); System.out.println(printTaskType(task, i)); i++; - } else { + } + else if (input.startsWith("delete")) { + deleteTask(task, Integer.parseInt(input.substring(input.indexOf(" ") + 1))); + i--; + + } + + else { throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR); } } @@ -67,9 +73,9 @@ public static void main(String[] args) { printBye(); } - private static String printTaskType(Task[] task, int i) { + private static String printTaskType(ArrayList task, int i) { return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() - + task[i].toString() + System.lineSeparator() + + task.get(i).toString() + System.lineSeparator() + "Now you have " + (i + 1) + " tasks in the list." + System.lineSeparator() + LINE_SEPERATOR; } @@ -85,20 +91,25 @@ private static void printBye() { LINE_SEPERATOR); } - public static void markTask(Task[] task, int index) { - task[index].setDone(true); + public static void markTask(ArrayList task, int index) { + task.get(index).setDone(true); + } + + public static void unmarkTask(ArrayList task, int index) { + task.get(index).setDone(false); } - public static void unmarkTask(Task[] task, int index) { - task[index].setDone(false); + public static void deleteTask(ArrayList task, int index) { + System.out.println(LINE_SEPERATOR + "\n" + "Noted. I've removed this task:\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); + task.remove(index); } - public static void printMark(Task[] task, int index) { - System.out.println(LINE_SEPERATOR + "\n" + "Nice! I've marked this task as done:\n" + "[" + task[index].getStatusIcon() + "] " + task[index].getDescription() + "\n" + LINE_SEPERATOR); + public static void printMark(ArrayList task, int index) { + System.out.println(LINE_SEPERATOR + "\n" + "Nice! I've marked this task as done:\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); } - public static void printUnmark(Task[] task, int index) { - System.out.println(LINE_SEPERATOR + "\n" + "[" + task[index].getStatusIcon() + "] " + task[index].getDescription() + "\n" + LINE_SEPERATOR); + public static void printUnmark(ArrayList task, int index) { + System.out.println(LINE_SEPERATOR + "\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); } public static int getIndex(String marking, String find) throws DavidException { From 64114254e6a8fa042c48f7b393f1106744e5d044 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 20 Feb 2025 23:38:04 +0800 Subject: [PATCH 15/26] Level-7 Save --- src/main/java/David.java | 50 +++++++++++++++++++++- src/main/java/David/deadline/Deadline.java | 4 ++ src/main/java/David/event/Event.java | 4 ++ src/main/java/David/task/Task.java | 6 ++- src/main/java/David/todo/Todo.java | 4 ++ 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index 2ce8ca0e4..c444d9f0e 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -1,3 +1,6 @@ +import java.io.*; +import java.nio.file.*; +import java.util.*; import java.util.Scanner; import java.util.ArrayList; import David.task.Task; @@ -8,7 +11,7 @@ public class David { public static final String LINE_SEPERATOR = "____________________________________________________________"; - + public static final String FILE_PATH = "./data/tasks.txt"; public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -18,6 +21,7 @@ public static void main(String[] args) { System.out.println("Hello from\n" + logo); ArrayList task = new ArrayList<>(); printHello(); + loadTasks(task); Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); int i = 0; @@ -61,7 +65,6 @@ else if (input.startsWith("delete")) { } } catch (IllegalArgumentException e) { - // Print the custom error message without stack trace System.out.println(e.getMessage()); } catch (DavidException e) { @@ -70,9 +73,52 @@ else if (input.startsWith("delete")) { input = scanner.nextLine(); } + saveTasks(task); printBye(); } +// +private static void saveTasks(ArrayList task) { + try { + Files.createDirectories(Paths.get("./data")); // Ensure directory exists + BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); + for (Task t : task) { + writer.write(t.toFileFormat()); + writer.newLine(); + } + writer.close(); + System.out.println("Tasks saved!"); + } catch (IOException e) { + System.out.println("Error saving tasks: " + e.getMessage()); + } +} + + private static void loadTasks(ArrayList task) { + try { + Path path = Paths.get(FILE_PATH); + if (Files.exists(path)) { + BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH)); + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(" \\| "); + if (parts[0].equals("T")) { + task.add(new Todo(parts[2])); + } else if (parts[0].equals("D")) { + task.add(new Deadline(parts[2], parts[3])); + } else if (parts[0].equals("E")) { + task.add(new Event(parts[2], parts[3], parts[4])); + } + } + reader.close(); + System.out.println("Tasks loaded from file!"); + } else { + System.out.println("No previous tasks found. Starting fresh!"); + } + } catch (IOException | ArrayIndexOutOfBoundsException e) { + System.out.println("Error loading tasks or corrupted file. Starting fresh!"); + } + } + // private static String printTaskType(ArrayList task, int i) { return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() + task.get(i).toString() + System.lineSeparator() diff --git a/src/main/java/David/deadline/Deadline.java b/src/main/java/David/deadline/Deadline.java index 4732090d3..e037f8d8f 100644 --- a/src/main/java/David/deadline/Deadline.java +++ b/src/main/java/David/deadline/Deadline.java @@ -13,4 +13,8 @@ public Deadline(String description, String by) { public String toString() { return "[D]" + super.toString() + " " + description + " (by: " + by + ")"; } + + public String toFileFormat() { + return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + by; + } } \ No newline at end of file diff --git a/src/main/java/David/event/Event.java b/src/main/java/David/event/Event.java index 2036fa221..04aa72fb0 100644 --- a/src/main/java/David/event/Event.java +++ b/src/main/java/David/event/Event.java @@ -16,4 +16,8 @@ public String toString() { return "[E]" + super.toString() + " " + description + " (from: " + from + " to: " + to + ")"; } + public String toFileFormat() { + return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + from + " | " + to; + } + } \ No newline at end of file diff --git a/src/main/java/David/task/Task.java b/src/main/java/David/task/Task.java index 5cfeba924..f422dcfc6 100644 --- a/src/main/java/David/task/Task.java +++ b/src/main/java/David/task/Task.java @@ -23,6 +23,10 @@ public boolean setDone(boolean status) { @Override public String toString() { - return "[" + getStatusIcon() + "]"; // You can customize this string as needed + return "[" + getStatusIcon() + "]"; + } + + public String toFileFormat() { + return ""; } } \ No newline at end of file diff --git a/src/main/java/David/todo/Todo.java b/src/main/java/David/todo/Todo.java index 376b4d07f..7817fcbcb 100644 --- a/src/main/java/David/todo/Todo.java +++ b/src/main/java/David/todo/Todo.java @@ -11,4 +11,8 @@ public Todo(String description) { public String toString() { return "[T]" + super.toString() + " " + description; } + + public String toFileFormat() { + return "T | " + (isDone ? "1" : "0") + " | " + description; + } } \ No newline at end of file From 0abe3f1af742a8a9e23bed4a393c271dad50d624 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 20 Feb 2025 23:52:42 +0800 Subject: [PATCH 16/26] A-Jar --- src/main/java/META-INF/MANIFEST.MF | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..5dc8eba8a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: David + From 08a7e42afd86aff0a46f963cb788ffb127693de6 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Sat, 22 Feb 2025 19:50:47 +0700 Subject: [PATCH 17/26] Deleted Comments --- src/main/java/David.java | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index c444d9f0e..5f21b203d 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -24,16 +24,18 @@ public static void main(String[] args) { loadTasks(task); Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); - int i = 0; + int i = task.size(); +// int i = 0; char input_type = input.charAt(input.indexOf("[") + 1); while (!input.equals("bye")) { try { if (input.equals("list")) { - System.out.println(LINE_SEPERATOR + "\n" + "Here are the tasks in your list:\n"); - for (int j = 0; j < i; j++) { + System.out.println(LINE_SEPERATOR + "\nHere are the tasks in your list:\n"); + for (int j = 0; j < task.size(); j++) { System.out.println((j + 1) + "." + task.get(j).toString() + "\n" + LINE_SEPERATOR); } - } else if (input.startsWith("mark")) { + } + else if (input.startsWith("mark")) { markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); printMark(task, Integer.parseInt(input.split(" ")[1]) - 1); @@ -55,9 +57,8 @@ public static void main(String[] args) { } else if (input.startsWith("delete")) { - deleteTask(task, Integer.parseInt(input.substring(input.indexOf(" ") + 1))); + deleteTask(task, Integer.parseInt(input.split(" ")[1]) - 1); i--; - } else { @@ -98,27 +99,42 @@ private static void loadTasks(ArrayList task) { if (Files.exists(path)) { BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH)); String line; + System.out.println("Loading tasks from file..."); + while ((line = reader.readLine()) != null) { String[] parts = line.split(" \\| "); + + Task t = null; if (parts[0].equals("T")) { - task.add(new Todo(parts[2])); + t = new Todo(parts[2]); } else if (parts[0].equals("D")) { - task.add(new Deadline(parts[2], parts[3])); + t = new Deadline(parts[2], parts[3]); } else if (parts[0].equals("E")) { - task.add(new Event(parts[2], parts[3], parts[4])); + t = new Event(parts[2], parts[3], parts[4]); + } + + if (t != null) { + if (parts[1].equals("1")) { + t.setDone(true); + } + task.add(t); } } reader.close(); - System.out.println("Tasks loaded from file!"); + System.out.println("Tasks loaded successfully!"); } else { System.out.println("No previous tasks found. Starting fresh!"); } } catch (IOException | ArrayIndexOutOfBoundsException e) { System.out.println("Error loading tasks or corrupted file. Starting fresh!"); } + + System.out.println("Current tasks after loading:"); + for (Task t : task) { + System.out.println(t); + } } - // private static String printTaskType(ArrayList task, int i) { return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() + task.get(i).toString() + System.lineSeparator() From d46c87b9c4ad9bbc48ab66a0b8c9da137107186e Mon Sep 17 00:00:00 2001 From: Gyurim Date: Tue, 4 Mar 2025 21:43:25 +0800 Subject: [PATCH 18/26] Fixed Load Data Delete Issues --- src/main/java/David.java | 76 +++------------------ src/main/java/David/StorageManager.java | 87 +++++++++++++++++++++++++ src/main/java/David/task/Task.java | 1 + 3 files changed, 98 insertions(+), 66 deletions(-) create mode 100644 src/main/java/David/StorageManager.java diff --git a/src/main/java/David.java b/src/main/java/David.java index 5f21b203d..f499c9f53 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -1,6 +1,3 @@ -import java.io.*; -import java.nio.file.*; -import java.util.*; import java.util.Scanner; import java.util.ArrayList; import David.task.Task; @@ -8,10 +5,11 @@ import David.deadline.Deadline; import David.event.Event; import David.todo.Todo; +import David.StorageManager; public class David { public static final String LINE_SEPERATOR = "____________________________________________________________"; - public static final String FILE_PATH = "./data/tasks.txt"; + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -20,12 +18,12 @@ public static void main(String[] args) { + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); ArrayList task = new ArrayList<>(); + task = StorageManager.loadTasks(); printHello(); - loadTasks(task); + Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); int i = task.size(); -// int i = 0; char input_type = input.charAt(input.indexOf("[") + 1); while (!input.equals("bye")) { try { @@ -38,26 +36,31 @@ public static void main(String[] args) { else if (input.startsWith("mark")) { markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); printMark(task, Integer.parseInt(input.split(" ")[1]) - 1); - + StorageManager.saveTasks(task); } else if (input.startsWith("unmark")) { unmarkTask(task, Integer.parseInt(input.split(" ")[1]) - 1); printUnmark(task, Integer.parseInt(input.split(" ")[1]) - 1); + StorageManager.saveTasks(task); } else if (input.startsWith("todo")) { task.add(new Todo(input.substring(getIndex(input, " ") + 1))); System.out.println(printTaskType(task, i)); + StorageManager.saveTasks(task); i++; } else if (input.startsWith("deadline")) { task.add(new Deadline((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "by") + 3))); System.out.println(printTaskType(task, i)); + StorageManager.saveTasks(task); i++; } else if (input.startsWith("event")) { task.add(new Event((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "from") + 5, getIndex(input, "to") - 2), input.substring(getIndex(input, "to") + 3))); System.out.println(printTaskType(task, i)); + StorageManager.saveTasks(task); i++; } else if (input.startsWith("delete")) { deleteTask(task, Integer.parseInt(input.split(" ")[1]) - 1); + StorageManager.saveTasks(task); i--; } @@ -74,67 +77,8 @@ else if (input.startsWith("delete")) { input = scanner.nextLine(); } - saveTasks(task); printBye(); } -// -private static void saveTasks(ArrayList task) { - try { - Files.createDirectories(Paths.get("./data")); // Ensure directory exists - BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); - for (Task t : task) { - writer.write(t.toFileFormat()); - writer.newLine(); - } - writer.close(); - System.out.println("Tasks saved!"); - } catch (IOException e) { - System.out.println("Error saving tasks: " + e.getMessage()); - } -} - - private static void loadTasks(ArrayList task) { - try { - Path path = Paths.get(FILE_PATH); - if (Files.exists(path)) { - BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH)); - String line; - System.out.println("Loading tasks from file..."); - - while ((line = reader.readLine()) != null) { - String[] parts = line.split(" \\| "); - - Task t = null; - if (parts[0].equals("T")) { - t = new Todo(parts[2]); - } else if (parts[0].equals("D")) { - t = new Deadline(parts[2], parts[3]); - } else if (parts[0].equals("E")) { - t = new Event(parts[2], parts[3], parts[4]); - } - - if (t != null) { - if (parts[1].equals("1")) { - t.setDone(true); - } - task.add(t); - } - } - reader.close(); - System.out.println("Tasks loaded successfully!"); - } else { - System.out.println("No previous tasks found. Starting fresh!"); - } - } catch (IOException | ArrayIndexOutOfBoundsException e) { - System.out.println("Error loading tasks or corrupted file. Starting fresh!"); - } - - System.out.println("Current tasks after loading:"); - for (Task t : task) { - System.out.println(t); - } - } - private static String printTaskType(ArrayList task, int i) { return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() + task.get(i).toString() + System.lineSeparator() diff --git a/src/main/java/David/StorageManager.java b/src/main/java/David/StorageManager.java new file mode 100644 index 000000000..43bb31253 --- /dev/null +++ b/src/main/java/David/StorageManager.java @@ -0,0 +1,87 @@ +package David; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; + + +import David.deadline.Deadline; +import David.event.Event; +import David.task.Task; +import David.todo.Todo; + +public class StorageManager { + private static final String FILE_PATH = "./data/tasks.txt"; + + static { + File folder = new File("data"); + if (!folder.isDirectory() && !folder.mkdirs()) { + System.err.println("Failed to create directory: " + folder.getAbsolutePath()); + } + } + + + public static void saveTasks(ArrayList task) { + try { + Files.createDirectories(Paths.get("./data")); // Ensure directory exists + BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); + for (Task t : task) { + writer.write(t.toFileFormat()); + writer.newLine(); + } + writer.close(); + System.out.println("Tasks saved!"); + } catch (IOException e) { + System.out.println("Error saving tasks: " + e.getMessage()); + } + } + + public static ArrayList loadTasks() { + File file = new File(FILE_PATH); // create a File for the given file path + ArrayList tasks = new ArrayList(); + if (!file.exists()) { + System.out.println("File not found. Returning an empty task list."); + return tasks; + } + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(" \\| "); + if (parts.length < 3) { + System.out.println("Skipping invalid task entry: " + line); + continue; + } + + String taskType = parts[0]; + boolean isDone = "1".equals(parts[1]); + String description = parts[2]; + Task task; + + // Construct tasks based on type + if ("T".equals(taskType)) { + task = new Todo(description); + } else if ("D".equals(taskType) && parts.length > 3) { + task = new Deadline(description, parts[3]); + } else if ("E".equals(taskType) && parts.length > 4) { + task = new Event(description, parts[3], parts[4]); + } else { + System.out.println("Unknown task type: " + taskType); + continue; + } + tasks.add(task); + + } + } catch (IOException e) { + System.err.println("Error while reading tasks: " + e.getMessage()); + } + + return tasks; + } + +} diff --git a/src/main/java/David/task/Task.java b/src/main/java/David/task/Task.java index f422dcfc6..088b09456 100644 --- a/src/main/java/David/task/Task.java +++ b/src/main/java/David/task/Task.java @@ -29,4 +29,5 @@ public String toString() { public String toFileFormat() { return ""; } + } \ No newline at end of file From c4fd83f4d1f65d9d5b5c45bb3ea6631947f99594 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 13 Mar 2025 00:53:06 +0800 Subject: [PATCH 19/26] A-MoreOOP : Use More OOP --- src/main/java/David.java | 146 +++++------------------- src/main/java/David/Parser.java | 93 +++++++++++++++ src/main/java/David/Storage.java | 21 ++++ src/main/java/David/StorageManager.java | 105 ++++++++--------- src/main/java/David/TaskList.java | 53 +++++++++ src/main/java/David/Ui.java | 72 ++++++++++++ 6 files changed, 316 insertions(+), 174 deletions(-) create mode 100644 src/main/java/David/Parser.java create mode 100644 src/main/java/David/Storage.java create mode 100644 src/main/java/David/TaskList.java create mode 100644 src/main/java/David/Ui.java diff --git a/src/main/java/David.java b/src/main/java/David.java index f499c9f53..79e57147b 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -1,130 +1,46 @@ import java.util.Scanner; -import java.util.ArrayList; -import David.task.Task; +import java.io.FileNotFoundException; +import David.Storage; +import David.Ui; +import David.TaskList; +import David.Parser; import David.ui.DavidException; -import David.deadline.Deadline; -import David.event.Event; -import David.todo.Todo; -import David.StorageManager; public class David { - public static final String LINE_SEPERATOR = "____________________________________________________________"; - - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - ArrayList task = new ArrayList<>(); - task = StorageManager.loadTasks(); - printHello(); - - Scanner scanner = new Scanner(System.in); - String input = scanner.nextLine(); - int i = task.size(); - char input_type = input.charAt(input.indexOf("[") + 1); - while (!input.equals("bye")) { - try { - if (input.equals("list")) { - System.out.println(LINE_SEPERATOR + "\nHere are the tasks in your list:\n"); - for (int j = 0; j < task.size(); j++) { - System.out.println((j + 1) + "." + task.get(j).toString() + "\n" + LINE_SEPERATOR); - } - } - else if (input.startsWith("mark")) { - markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); - printMark(task, Integer.parseInt(input.split(" ")[1]) - 1); - StorageManager.saveTasks(task); - } else if (input.startsWith("unmark")) { - unmarkTask(task, Integer.parseInt(input.split(" ")[1]) - 1); - printUnmark(task, Integer.parseInt(input.split(" ")[1]) - 1); - StorageManager.saveTasks(task); - } else if (input.startsWith("todo")) { - task.add(new Todo(input.substring(getIndex(input, " ") + 1))); - System.out.println(printTaskType(task, i)); - StorageManager.saveTasks(task); - i++; - } else if (input.startsWith("deadline")) { - task.add(new Deadline((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "by") + 3))); - System.out.println(printTaskType(task, i)); - StorageManager.saveTasks(task); - i++; - } else if (input.startsWith("event")) { - task.add(new Event((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "from") + 5, getIndex(input, "to") - 2), input.substring(getIndex(input, "to") + 3))); - System.out.println(printTaskType(task, i)); - StorageManager.saveTasks(task); - i++; - - } - else if (input.startsWith("delete")) { - deleteTask(task, Integer.parseInt(input.split(" ")[1]) - 1); - StorageManager.saveTasks(task); - i--; - } - - else { - throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR); - } - } - catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); + private Storage storage; + private TaskList tasks; + private Ui ui; + private Parser parser; + + public David(String filePath) { + ui = new Ui(); + storage = new Storage(filePath); + try { + tasks = new TaskList(storage.loadTasks()); } - catch (DavidException e) { - System.out.println(LINE_SEPERATOR + System.lineSeparator() + " Oops! The description of a task should NOT be blank! Please try again :)" + System.lineSeparator() + LINE_SEPERATOR); - } - input = scanner.nextLine(); - + catch (DavidException e) { + ui.showLoadingError(); + tasks = new TaskList(); } - printBye(); - } - private static String printTaskType(ArrayList task, int i) { - return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() - + task.get(i).toString() + System.lineSeparator() - + "Now you have " + (i + 1) + " tasks in the list." + System.lineSeparator() + LINE_SEPERATOR; - } - - private static void printHello() { - System.out.println(LINE_SEPERATOR + "\n" + - " Hello! I'm David\n" + - " What can I do for you?\n" + - LINE_SEPERATOR + "\n"); - } - - private static void printBye() { - System.out.println(" Bye. Hope to see you again soon!\n" + - LINE_SEPERATOR); + parser = new Parser(ui, tasks, storage); } - public static void markTask(ArrayList task, int index) { - task.get(index).setDone(true); - } - - public static void unmarkTask(ArrayList task, int index) { - task.get(index).setDone(false); - } - - public static void deleteTask(ArrayList task, int index) { - System.out.println(LINE_SEPERATOR + "\n" + "Noted. I've removed this task:\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); - task.remove(index); + public void run() { + ui.printGreeting(); + ui.printHello(); + boolean isExit = false; + while (!isExit) { + String input = ui.readLine(); + isExit = parser.readCommand(input); + } + ui.printBye(); } - public static void printMark(ArrayList task, int index) { - System.out.println(LINE_SEPERATOR + "\n" + "Nice! I've marked this task as done:\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); + public static void main(String[] args) { + new David("./data/tasks.txt").run(); } +} - public static void printUnmark(ArrayList task, int index) { - System.out.println(LINE_SEPERATOR + "\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); - } - public static int getIndex(String marking, String find) throws DavidException { - int index = marking.indexOf(find); - if (index == -1) { - throw new DavidException(); - } - return index; - } -} diff --git a/src/main/java/David/Parser.java b/src/main/java/David/Parser.java new file mode 100644 index 000000000..3e020cecd --- /dev/null +++ b/src/main/java/David/Parser.java @@ -0,0 +1,93 @@ +package David; +import java.util.ArrayList; +import java.util.Scanner; +import David.deadline.Deadline; +import David.event.Event; +import David.task.Task; +import David.todo.Todo; +import David.ui.DavidException; +import David.Ui; + + +public class Parser { + public static final String LINE_SEPERATOR = "____________________________________________________________"; + private final Ui ui; + private final TaskList task; + private final Storage storage; + + public Parser(Ui ui, TaskList tasks, Storage storage) { + this.ui = ui; + this.task = tasks; + this.storage = storage; + } + + public boolean readCommand(String input) { + int i = task.getTasks().size(); + char input_type = input.charAt(input.indexOf("[") + 1); + try { + if (input.equals("list")) { + System.out.println(LINE_SEPERATOR + "\nHere are the tasks in your list:\n"); + for (int j = 0; j < task.getTasks().size(); j++) { + System.out.println((j + 1) + "." + task.getTask(j).toString() + "\n" + LINE_SEPERATOR); + } + } + else if(input.equals("bye")) { + return true; + + } else if (input.startsWith("mark")) { + task.markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); + ui.printMark(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + storage.saveTasks(task.getTasks()); + } else if (input.startsWith("unmark")) { + task.unmarkTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + ui.printUnmark(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + storage.saveTasks(task.getTasks()); + } else if (input.startsWith("todo")) { + parseTodo(input, task.getTasks()); + System.out.println(Ui.printTaskType(task.getTasks(), i)); + storage.saveTasks(task.getTasks()); + i++; + } else if (input.startsWith("deadline")) { + parseDeadline(input, task.getTasks()); + System.out.println(Ui.printTaskType(task.getTasks(), i)); + storage.saveTasks(task.getTasks()); + i++; + } else if (input.startsWith("event")) { + parseEvent(input, task.getTasks()); + System.out.println(Ui.printTaskType(task.getTasks(), i)); + storage.saveTasks(task.getTasks()); + i++; + + } else if (input.startsWith("delete")) { + TaskList.deleteTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + storage.saveTasks(task.getTasks()); + i--; + } else { + throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR); + } + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } catch (DavidException e) { + System.out.println(LINE_SEPERATOR + System.lineSeparator() + " Oops! The description of a task should NOT be blank! Please try again :)" + System.lineSeparator() + LINE_SEPERATOR); + } + + return false; + } + + private static void parseTodo(String input, ArrayList task) throws DavidException { + task.add(new Todo(input.substring(TaskList.getIndex(input, " ") + 1))); + } + + private static void parseEvent(String input, ArrayList task) throws DavidException { + task.add(new Event((input.substring(TaskList.getIndex(input, " ") + 1, TaskList.getIndex(input, "/") - 1)), input.substring(TaskList.getIndex(input, "from") + 5, TaskList.getIndex(input, "to") - 2), input.substring(TaskList.getIndex(input, "to") + 3))); + } + + private static void parseDeadline(String input, ArrayList task) throws DavidException { + task.add(new Deadline((input.substring(TaskList.getIndex(input, " ") + 1, TaskList.getIndex(input, "/") - 1)), input.substring(TaskList.getIndex(input, "by") + 3))); + } + + +} + + + diff --git a/src/main/java/David/Storage.java b/src/main/java/David/Storage.java new file mode 100644 index 000000000..20e9d2b4d --- /dev/null +++ b/src/main/java/David/Storage.java @@ -0,0 +1,21 @@ +package David; +import java.util.ArrayList; +import David.task.Task; +import David.ui.DavidException; + + +public class Storage { + private final String filePath; + + public Storage(String filePath) { + this.filePath = filePath; + } + + public ArrayList loadTasks() throws DavidException { + return StorageManager.loadTasks(filePath); + } + + public void saveTasks(ArrayList tasks) { + StorageManager.saveTasks(tasks, filePath); + } +} diff --git a/src/main/java/David/StorageManager.java b/src/main/java/David/StorageManager.java index 43bb31253..d88fdbc98 100644 --- a/src/main/java/David/StorageManager.java +++ b/src/main/java/David/StorageManager.java @@ -15,73 +15,60 @@ import David.event.Event; import David.task.Task; import David.todo.Todo; - public class StorageManager { - private static final String FILE_PATH = "./data/tasks.txt"; - - static { - File folder = new File("data"); - if (!folder.isDirectory() && !folder.mkdirs()) { - System.err.println("Failed to create directory: " + folder.getAbsolutePath()); + public static void saveTasks(ArrayList task, String filePath) { + try { + Files.createDirectories(Paths.get("./data")); // Ensure directory exists + BufferedWriter writer = new BufferedWriter(new FileWriter(filePath)); + for (Task t : task) { + writer.write(t.toFileFormat()); + writer.newLine(); + } + writer.close(); + System.out.println("Tasks saved to " + filePath); + } catch (IOException e) { + System.err.println("Error saving tasks: " + e.getMessage()); + } } - } - - public static void saveTasks(ArrayList task) { - try { - Files.createDirectories(Paths.get("./data")); // Ensure directory exists - BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); - for (Task t : task) { - writer.write(t.toFileFormat()); - writer.newLine(); + public static ArrayList loadTasks(String filePath) { + File file = new File(filePath); + ArrayList tasks = new ArrayList<>(); + if (!file.exists()) { + System.out.println("File not found. Returning an empty task list."); + return tasks; } - writer.close(); - System.out.println("Tasks saved!"); - } catch (IOException e) { - System.out.println("Error saving tasks: " + e.getMessage()); - } - } - public static ArrayList loadTasks() { - File file = new File(FILE_PATH); // create a File for the given file path - ArrayList tasks = new ArrayList(); - if (!file.exists()) { - System.out.println("File not found. Returning an empty task list."); - return tasks; - } - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - String line; - while ((line = reader.readLine()) != null) { - String[] parts = line.split(" \\| "); - if (parts.length < 3) { - System.out.println("Skipping invalid task entry: " + line); - continue; - } + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(" \\| "); + if (parts.length < 3) { + System.out.println("Skipping invalid task entry: " + line); + continue; + } - String taskType = parts[0]; - boolean isDone = "1".equals(parts[1]); - String description = parts[2]; - Task task; + String taskType = parts[0]; + boolean isDone = "1".equals(parts[1]); + String description = parts[2]; + Task task; - // Construct tasks based on type - if ("T".equals(taskType)) { - task = new Todo(description); - } else if ("D".equals(taskType) && parts.length > 3) { - task = new Deadline(description, parts[3]); - } else if ("E".equals(taskType) && parts.length > 4) { - task = new Event(description, parts[3], parts[4]); - } else { - System.out.println("Unknown task type: " + taskType); - continue; - } + if ("T".equals(taskType)) { + task = new Todo(description); + } else if ("D".equals(taskType) && parts.length > 3) { + task = new Deadline(description, parts[3]); + } else if ("E".equals(taskType) && parts.length > 4) { + task = new Event(description, parts[3], parts[4]); + } else { + System.out.println("Unknown task type: " + taskType); + continue; + } tasks.add(task); - + } + } catch (IOException e) { + System.err.println("Error while reading tasks: " + e.getMessage()); } - } catch (IOException e) { - System.err.println("Error while reading tasks: " + e.getMessage()); - } - - return tasks; - } + return tasks; + } } diff --git a/src/main/java/David/TaskList.java b/src/main/java/David/TaskList.java new file mode 100644 index 000000000..0250378bd --- /dev/null +++ b/src/main/java/David/TaskList.java @@ -0,0 +1,53 @@ +package David; + +import java.util.ArrayList; + +import David.task.Task; +import David.ui.DavidException; + +public class TaskList { + public static final String LINE_SEPERATOR = "____________________________________________________________"; + private final ArrayList tasks; + + public TaskList() { + tasks = new ArrayList<>(); + } + + public TaskList(ArrayList tasks) { + this.tasks = tasks; + } + + public static void markTask(TaskList task, int index) { + task.getTask(index).setDone(true); + } + + public static void unmarkTask(ArrayList task, int index) { + task.get(index).setDone(false); + } + + public static void deleteTask(ArrayList task, int index) { + System.out.println(LINE_SEPERATOR + "\n" + "Noted. I've removed this task:\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); + task.remove(index); + } + + public ArrayList getTasks() { + return tasks; + } + + public Task getTask(int index) { + return tasks.get(index); + } + + public boolean isEmpty() { + return tasks.isEmpty(); + } + + public static int getIndex(String marking, String find) throws DavidException { + int index = marking.indexOf(find); + if (index == -1) { + throw new DavidException(); + } + return index; + } + +} diff --git a/src/main/java/David/Ui.java b/src/main/java/David/Ui.java new file mode 100644 index 000000000..8a7ee867c --- /dev/null +++ b/src/main/java/David/Ui.java @@ -0,0 +1,72 @@ +package David; + +import java.util.ArrayList; +import java.util.Scanner; +import David.task.Task; + +public class Ui { + public static final String LINE_SEPERATOR = "____________________________________________________________"; + private Scanner scanner; + + public Ui() { + scanner = new Scanner(System.in); + } + + public static String printTaskType(ArrayList task, int i) { + return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() + + task.get(i).toString() + System.lineSeparator() + + "Now you have " + (i + 1) + " tasks in the list." + System.lineSeparator() + LINE_SEPERATOR; + } + public void printGreeting() { + String logo = " ______ ____ ,---. ,---..-./`) ______ .---. \n" + + "| _ `''. .' __ `. | / | |\\ .-.')| _ `''. \\ / \n" + + "| _ | ) _ \\/ ' \\ \\| | | .'/ `-' \\| _ | ) _ \\ | | \n" + + "|( ''_' ) ||___| / || | _ | | `-'`\"`|( ''_' ) | \\ / \n" + + "| . (_) `. | _.-` || _( )_ | .---. | . (_) `. | v \n" + + "|(_ ._) '.' _ |\\ (_ o._) / | | |(_ ._) ' _ _ \n" + + "| (_.\\.' / | _( )_ | \\ (_,_) / | | | (_.\\.' / (_I_) \n" + + "| .' \\ (_ o _) / \\ / | | | .' (_(=)_) \n" + + "'-----'` '.(_,_).' `---` '---' '-----'` (_I_) \n" + + " "; + System.out.println("Hello from\n" + logo); + } + + public String readLine() { + return scanner.nextLine().trim(); + } + + public void printHello() { + System.out.println(LINE_SEPERATOR + "\n" + + " Hello! I'm David\n" + + " What can I do for you?\n" + + LINE_SEPERATOR + "\n"); + } + + public static void printBye() { + System.out.println(" Bye. Hope to see you again soon!\n" + + LINE_SEPERATOR); + } + + public static void printMark(ArrayList task, int index) { + System.out.println(LINE_SEPERATOR + "\n" + "Nice! I've marked this task as done:\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); + } + + public static void printUnmark(ArrayList task, int index) { + System.out.println(LINE_SEPERATOR + "\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); + } + + public static void showLoadingError() { + System.out.println(LINE_SEPERATOR); + System.out.println("The file could not be loaded."); + System.out.println(LINE_SEPERATOR); + } + + public static void printError(String message) { + System.out.println(LINE_SEPERATOR); + System.out.println("OOPS! Your input is invalid."); + System.out.println("[ Please refer to the error message ] \n- [error]: "+ message); + System.out.println(LINE_SEPERATOR); + } +} + + From 5c2c1e41f5725bb9812426ed8b25341cc49a1b2e Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 13 Mar 2025 03:24:07 +0800 Subject: [PATCH 20/26] Level 9. Find --- src/main/java/David/Parser.java | 19 +++++++++++++++++-- src/main/java/David/TaskList.java | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/David/Parser.java b/src/main/java/David/Parser.java index 3e020cecd..6e9f1164b 100644 --- a/src/main/java/David/Parser.java +++ b/src/main/java/David/Parser.java @@ -59,10 +59,25 @@ else if(input.equals("bye")) { i++; } else if (input.startsWith("delete")) { - TaskList.deleteTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + task.deleteTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); storage.saveTasks(task.getTasks()); i--; - } else { + } else if(input.startsWith("find")) { + boolean found = false; + for(i = 0; i < task.getTasks().size(); i++) { + if(task.getTask(i).getDescription().contains(input.substring(input.indexOf(" ") + 1))) { + System.out.println((i + 1) + "." + task.getTask(i).toString() + "\n" + LINE_SEPERATOR); + found = true; + } + } + if(!found){ + System.out.println(LINE_SEPERATOR + System.lineSeparator() + "Could not be found" + System.lineSeparator() + LINE_SEPERATOR); + } + else{ + System.out.println(System.lineSeparator() + "These tasks were found" + System.lineSeparator() + LINE_SEPERATOR); + } + } + else { throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR); } } catch (IllegalArgumentException e) { diff --git a/src/main/java/David/TaskList.java b/src/main/java/David/TaskList.java index 0250378bd..9e3b9ac5c 100644 --- a/src/main/java/David/TaskList.java +++ b/src/main/java/David/TaskList.java @@ -51,3 +51,5 @@ public static int getIndex(String marking, String find) throws DavidException { } } + + From 35fcfcf31cd5d9da544b72a7e18c574696f7fac4 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 13 Mar 2025 13:31:23 +0800 Subject: [PATCH 21/26] A-JavaDoc : Add JavaDoc Comments --- src/main/java/David.java | 34 ++++- src/main/java/David/Parser.java | 153 ++++++++++++++------- src/main/java/David/Storage.java | 28 ++++ src/main/java/David/StorageManager.java | 107 +++++++------- src/main/java/David/TaskList.java | 64 ++++++++- src/main/java/David/Ui.java | 63 ++++++++- src/main/java/David/deadline/Deadline.java | 20 ++- src/main/java/David/event/Event.java | 27 +++- src/main/java/David/task/Task.java | 44 +++++- src/main/java/David/todo/Todo.java | 26 +++- 10 files changed, 451 insertions(+), 115 deletions(-) diff --git a/src/main/java/David.java b/src/main/java/David.java index 79e57147b..b78044377 100644 --- a/src/main/java/David.java +++ b/src/main/java/David.java @@ -1,30 +1,44 @@ -import java.util.Scanner; -import java.io.FileNotFoundException; import David.Storage; import David.Ui; import David.TaskList; import David.Parser; import David.ui.DavidException; + +/** + * The main class of the David application, responsible for initializing + * the core components and handling user interactions. + */ public class David { private Storage storage; private TaskList tasks; private Ui ui; private Parser parser; + + /** + * Constructor for the David application. + * Initializes the UI, storage, task list, and command parser. + * + * @param filePath The file path where tasks are stored. + */ public David(String filePath) { ui = new Ui(); storage = new Storage(filePath); try { tasks = new TaskList(storage.loadTasks()); - } - catch (DavidException e) { + } catch (DavidException e) { ui.showLoadingError(); tasks = new TaskList(); } parser = new Parser(ui, tasks, storage); } + + /** + * Runs the main event loop of the application, handling user input. + * The loop continues until the user enters the exit command. + */ public void run() { ui.printGreeting(); ui.printHello(); @@ -36,6 +50,13 @@ public void run() { ui.printBye(); } + + /** + * The main entry point of the program. + * Initializes the application and starts the event loop. + * + * @param args Command-line arguments (not used). + */ public static void main(String[] args) { new David("./data/tasks.txt").run(); } @@ -44,3 +65,8 @@ public static void main(String[] args) { + + + + + diff --git a/src/main/java/David/Parser.java b/src/main/java/David/Parser.java index 3e020cecd..507d7aa23 100644 --- a/src/main/java/David/Parser.java +++ b/src/main/java/David/Parser.java @@ -1,93 +1,154 @@ package David; + + import java.util.ArrayList; -import java.util.Scanner; + + import David.deadline.Deadline; import David.event.Event; import David.task.Task; import David.todo.Todo; import David.ui.DavidException; -import David.Ui; +/** + * The Parser class handles user input, processes commands, and executes appropriate actions + * on the task list, including adding, deleting, and marking tasks. + */ public class Parser { public static final String LINE_SEPERATOR = "____________________________________________________________"; private final Ui ui; private final TaskList task; private final Storage storage; + + /** + * Constructs a Parser object. + * + * @param ui User interface handler. + * @param tasks Task list manager. + * @param storage Storage handler for saving and loading tasks. + */ public Parser(Ui ui, TaskList tasks, Storage storage) { this.ui = ui; this.task = tasks; this.storage = storage; } + + /** + * Reads and processes a command input by the user. + * + * @param input The command entered by the user. + * @return true if the command is "bye" to exit the program, false otherwise. + */ public boolean readCommand(String input) { int i = task.getTasks().size(); - char input_type = input.charAt(input.indexOf("[") + 1); try { - if (input.equals("list")) { - System.out.println(LINE_SEPERATOR + "\nHere are the tasks in your list:\n"); - for (int j = 0; j < task.getTasks().size(); j++) { - System.out.println((j + 1) + "." + task.getTask(j).toString() + "\n" + LINE_SEPERATOR); - } + if (input.equals("list")) { + System.out.println(LINE_SEPERATOR + "\nHere are the tasks in your list:\n"); + for (int j = 0; j < task.getTasks().size(); j++) { + System.out.println((j + 1) + "." + task.getTask(j).toString() + "\n" + LINE_SEPERATOR); } - else if(input.equals("bye")) { - return true; - - } else if (input.startsWith("mark")) { - task.markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); - ui.printMark(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); - storage.saveTasks(task.getTasks()); - } else if (input.startsWith("unmark")) { - task.unmarkTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); - ui.printUnmark(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); - storage.saveTasks(task.getTasks()); - } else if (input.startsWith("todo")) { - parseTodo(input, task.getTasks()); - System.out.println(Ui.printTaskType(task.getTasks(), i)); - storage.saveTasks(task.getTasks()); - i++; - } else if (input.startsWith("deadline")) { - parseDeadline(input, task.getTasks()); - System.out.println(Ui.printTaskType(task.getTasks(), i)); - storage.saveTasks(task.getTasks()); - i++; - } else if (input.startsWith("event")) { - parseEvent(input, task.getTasks()); - System.out.println(Ui.printTaskType(task.getTasks(), i)); - storage.saveTasks(task.getTasks()); - i++; - - } else if (input.startsWith("delete")) { - TaskList.deleteTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); - storage.saveTasks(task.getTasks()); - i--; - } else { - throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR); - } - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } catch (DavidException e) { - System.out.println(LINE_SEPERATOR + System.lineSeparator() + " Oops! The description of a task should NOT be blank! Please try again :)" + System.lineSeparator() + LINE_SEPERATOR); + } else if (input.equals("bye")) { + return true; + + + } else if (input.startsWith("mark")) { + task.markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); + ui.printMark(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + storage.saveTasks(task.getTasks()); + } else if (input.startsWith("unmark")) { + task.unmarkTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + ui.printUnmark(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + storage.saveTasks(task.getTasks()); + } else if (input.startsWith("todo")) { + parseTodo(input, task.getTasks()); + System.out.println(Ui.printTaskType(task.getTasks(), i)); + storage.saveTasks(task.getTasks()); + i++; + } else if (input.startsWith("deadline")) { + parseDeadline(input, task.getTasks()); + System.out.println(Ui.printTaskType(task.getTasks(), i)); + storage.saveTasks(task.getTasks()); + i++; + } else if (input.startsWith("event")) { + parseEvent(input, task.getTasks()); + System.out.println(Ui.printTaskType(task.getTasks(), i)); + storage.saveTasks(task.getTasks()); + i++; + + + } else if (input.startsWith("delete")) { + TaskList.deleteTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + storage.saveTasks(task.getTasks()); + i--; + } else { + throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR); } + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } catch (DavidException e) { + System.out.println(LINE_SEPERATOR + System.lineSeparator() + " Oops! The description of a task should NOT be blank! Please try again :)" + System.lineSeparator() + LINE_SEPERATOR); + } + return false; } + + /** + * Marks a task as done or not done. + * + * @param index The index of the task. + * @param isDone The status to mark the task. + * @param ui The UI instance to show all messages. + */ + + + /** + * Parses and adds a Todo task from the user input. + * + * @param input The user command string. + * @param task The task list where the new task will be added. + * @throws DavidException If the description of the task is missing. + */ private static void parseTodo(String input, ArrayList task) throws DavidException { task.add(new Todo(input.substring(TaskList.getIndex(input, " ") + 1))); } + + /** + * Parses and adds an Event task from the user input. + * + * @param input The user command string. + * @param task The task list where the new task will be added. + * @throws DavidException If the description of the task is missing. + */ private static void parseEvent(String input, ArrayList task) throws DavidException { task.add(new Event((input.substring(TaskList.getIndex(input, " ") + 1, TaskList.getIndex(input, "/") - 1)), input.substring(TaskList.getIndex(input, "from") + 5, TaskList.getIndex(input, "to") - 2), input.substring(TaskList.getIndex(input, "to") + 3))); } + + /** + * Parses and adds a Deadline task from the user input. + * + * @param input The user command string. + * @param task The task list where the new task will be added. + * @throws DavidException If the description of the task is missing. + */ private static void parseDeadline(String input, ArrayList task) throws DavidException { task.add(new Deadline((input.substring(TaskList.getIndex(input, " ") + 1, TaskList.getIndex(input, "/") - 1)), input.substring(TaskList.getIndex(input, "by") + 3))); } + + } + + + + diff --git a/src/main/java/David/Storage.java b/src/main/java/David/Storage.java index 20e9d2b4d..dac055f35 100644 --- a/src/main/java/David/Storage.java +++ b/src/main/java/David/Storage.java @@ -1,21 +1,49 @@ package David; + + import java.util.ArrayList; + + import David.task.Task; import David.ui.DavidException; +/** + * Handles the storage of tasks by reading from and writing to a file. + * Uses StorageManager to perform actual file operations. + */ public class Storage { private final String filePath; + + /** + * Constructor for Storage. + * + * @param filePath The file path where tasks will be loaded from and saved to. + */ public Storage(String filePath) { this.filePath = filePath; } + + /** + * Loads tasks from the specified file. + * + * @return An ArrayList of Task objects loaded from the file. + * @throws DavidException If an error occurs during file reading. + */ public ArrayList loadTasks() throws DavidException { return StorageManager.loadTasks(filePath); } + + /** + * Saves the list of tasks to the specified file. + * + * @param tasks The list of tasks to be saved. + */ public void saveTasks(ArrayList tasks) { StorageManager.saveTasks(tasks, filePath); } } + diff --git a/src/main/java/David/StorageManager.java b/src/main/java/David/StorageManager.java index d88fdbc98..221bd35d4 100644 --- a/src/main/java/David/StorageManager.java +++ b/src/main/java/David/StorageManager.java @@ -1,8 +1,9 @@ package David; + + import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; @@ -15,60 +16,72 @@ import David.event.Event; import David.task.Task; import David.todo.Todo; + + public class StorageManager { - public static void saveTasks(ArrayList task, String filePath) { - try { - Files.createDirectories(Paths.get("./data")); // Ensure directory exists - BufferedWriter writer = new BufferedWriter(new FileWriter(filePath)); - for (Task t : task) { - writer.write(t.toFileFormat()); - writer.newLine(); - } - writer.close(); - System.out.println("Tasks saved to " + filePath); - } catch (IOException e) { - System.err.println("Error saving tasks: " + e.getMessage()); + public static void saveTasks(ArrayList task, String filePath) { + try { + Files.createDirectories(Paths.get("./data")); // Ensure directory exists + BufferedWriter writer = new BufferedWriter(new FileWriter(filePath)); + for (Task t : task) { + writer.write(t.toFileFormat()); + writer.newLine(); } + writer.close(); + System.out.println("Tasks saved to " + filePath); + } catch (IOException e) { + System.err.println("Error saving tasks: " + e.getMessage()); } + } - public static ArrayList loadTasks(String filePath) { - File file = new File(filePath); - ArrayList tasks = new ArrayList<>(); - if (!file.exists()) { - System.out.println("File not found. Returning an empty task list."); - return tasks; - } - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - String line; - while ((line = reader.readLine()) != null) { - String[] parts = line.split(" \\| "); - if (parts.length < 3) { - System.out.println("Skipping invalid task entry: " + line); - continue; - } + /** + * Loads the list of tasks from the specified file. + * + * @param filePath The path of the file to load tasks from. + * @return The list of tasks loaded from the file. + */ + public static ArrayList loadTasks(String filePath) { + File file = new File(filePath); + ArrayList tasks = new ArrayList<>(); + if (!file.exists()) { + System.out.println("File not found. Returning an empty task list."); + return tasks; + } + + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(" \\| "); + if (parts.length < 3) { + System.out.println("Skipping invalid task entry: " + line); + continue; + } - String taskType = parts[0]; - boolean isDone = "1".equals(parts[1]); - String description = parts[2]; - Task task; - if ("T".equals(taskType)) { - task = new Todo(description); - } else if ("D".equals(taskType) && parts.length > 3) { - task = new Deadline(description, parts[3]); - } else if ("E".equals(taskType) && parts.length > 4) { - task = new Event(description, parts[3], parts[4]); - } else { - System.out.println("Unknown task type: " + taskType); - continue; - } - tasks.add(task); + String taskType = parts[0]; + boolean isDone = "1".equals(parts[1]); + String description = parts[2]; + Task task; + // Create tasks based on the task type + if ("T".equals(taskType)) { + task = new Todo(description); + } else if ("D".equals(taskType) && parts.length > 3) { + task = new Deadline(description, parts[3]); + } else if ("E".equals(taskType) && parts.length > 4) { + task = new Event(description, parts[3], parts[4]); + } else { + System.out.println("Unknown task type: " + taskType); + continue; } - } catch (IOException e) { - System.err.println("Error while reading tasks: " + e.getMessage()); + tasks.add(task); } - - return tasks; + } catch (IOException e) { + // Print an error message if there's an issue while reading the file + System.err.println("Error while reading tasks: " + e.getMessage()); } + return tasks; + } } + diff --git a/src/main/java/David/TaskList.java b/src/main/java/David/TaskList.java index 0250378bd..1d6e1186a 100644 --- a/src/main/java/David/TaskList.java +++ b/src/main/java/David/TaskList.java @@ -1,47 +1,103 @@ package David; + import java.util.ArrayList; + import David.task.Task; import David.ui.DavidException; + +/** + * Represents a list of tasks. Provides methods to manipulate tasks, + * such as marking, unmarking, and deleting tasks. + */ public class TaskList { public static final String LINE_SEPERATOR = "____________________________________________________________"; private final ArrayList tasks; + + /** + * Default constructor that initializes an empty task list. + */ public TaskList() { tasks = new ArrayList<>(); } + + /** + * Constructor that initializes the task list with an existing list of tasks. + * + * @param tasks The list of tasks to initialize with. + */ public TaskList(ArrayList tasks) { this.tasks = tasks; } + + /** + * Marks a task as completed by setting its done status to true. + * + * @param task The TaskList object containing the task. + * @param index The index of the task to mark. + */ public static void markTask(TaskList task, int index) { task.getTask(index).setDone(true); } + + /** + * Unmarks a task as incomplete by setting its done status to false. + * + * @param task The list of tasks. + * @param index The index of the task to unmark. + */ public static void unmarkTask(ArrayList task, int index) { task.get(index).setDone(false); } + + /** + * Deletes a task from the task list. + * + * @param task The list of tasks. + * @param index The index of the task to delete. + */ public static void deleteTask(ArrayList task, int index) { System.out.println(LINE_SEPERATOR + "\n" + "Noted. I've removed this task:\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); task.remove(index); } + + /** + * Returns the list of tasks. + * + * @return The list of tasks. + */ public ArrayList getTasks() { return tasks; } + + /** + * Retrieves a task at a specific index. + * + * @param index The index of the task to retrieve. + * @return The task at the given index. + */ public Task getTask(int index) { return tasks.get(index); } - public boolean isEmpty() { - return tasks.isEmpty(); - } + /** + * Finds the index of a given substring within a string. + * + * @param marking The string to search within. + * @param find The substring to find. + * @return The index of the substring if found. + * @throws DavidException if the substring is not found. + */ public static int getIndex(String marking, String find) throws DavidException { int index = marking.indexOf(find); if (index == -1) { @@ -50,4 +106,6 @@ public static int getIndex(String marking, String find) throws DavidException { return index; } + } + diff --git a/src/main/java/David/Ui.java b/src/main/java/David/Ui.java index 8a7ee867c..ffb127bf0 100644 --- a/src/main/java/David/Ui.java +++ b/src/main/java/David/Ui.java @@ -1,22 +1,43 @@ package David; + import java.util.ArrayList; import java.util.Scanner; + + import David.task.Task; + public class Ui { public static final String LINE_SEPERATOR = "____________________________________________________________"; private Scanner scanner; + + /** + * Constructs a Ui instance and initializes the scanner for user input. + */ public Ui() { scanner = new Scanner(System.in); } + + /** + * Prints the task type and details when a task is added. + * + * @param task The list of tasks. + * @param i The index of the task in the list. + * @return A string containing the message about the added task. + */ public static String printTaskType(ArrayList task, int i) { return LINE_SEPERATOR + System.lineSeparator() + " Got it. I've added this task:" + System.lineSeparator() + task.get(i).toString() + System.lineSeparator() + "Now you have " + (i + 1) + " tasks in the list." + System.lineSeparator() + LINE_SEPERATOR; } + + + /** + * Prints the greeting message and logo when the program starts. + */ public void printGreeting() { String logo = " ______ ____ ,---. ,---..-./`) ______ .---. \n" + "| _ `''. .' __ `. | / | |\\ .-.')| _ `''. \\ / \n" + @@ -31,10 +52,20 @@ public void printGreeting() { System.out.println("Hello from\n" + logo); } + + /** + * Reads a line of input from the user, trimming leading and trailing spaces. + * + * @return The trimmed input from the user. + */ public String readLine() { return scanner.nextLine().trim(); } + + /** + * Prints a welcome message when the program starts. + */ public void printHello() { System.out.println(LINE_SEPERATOR + "\n" + " Hello! I'm David\n" + @@ -42,31 +73,51 @@ public void printHello() { LINE_SEPERATOR + "\n"); } + + /** + * Prints a goodbye message when the program ends. + */ public static void printBye() { System.out.println(" Bye. Hope to see you again soon!\n" + LINE_SEPERATOR); } + + /** + * Prints a message when a task is marked as done. + * + * @param task The list of tasks. + * @param index The index of the task in the list. + */ public static void printMark(ArrayList task, int index) { System.out.println(LINE_SEPERATOR + "\n" + "Nice! I've marked this task as done:\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); } + + /** + * Prints a message when a task is unmarked. + * + * @param task The list of tasks. + * @param index The index of the task in the list. + */ public static void printUnmark(ArrayList task, int index) { System.out.println(LINE_SEPERATOR + "\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); } + + /** + * Prints an error message when there is an issue loading a file. + */ public static void showLoadingError() { System.out.println(LINE_SEPERATOR); System.out.println("The file could not be loaded."); System.out.println(LINE_SEPERATOR); } - public static void printError(String message) { - System.out.println(LINE_SEPERATOR); - System.out.println("OOPS! Your input is invalid."); - System.out.println("[ Please refer to the error message ] \n- [error]: "+ message); - System.out.println(LINE_SEPERATOR); - } + } + + + diff --git a/src/main/java/David/deadline/Deadline.java b/src/main/java/David/deadline/Deadline.java index e037f8d8f..6c46f9855 100644 --- a/src/main/java/David/deadline/Deadline.java +++ b/src/main/java/David/deadline/Deadline.java @@ -1,20 +1,32 @@ package David.deadline; import David.task.Task; + public class Deadline extends Task { protected String by; - + /** + * Constructs a Deadline task with the given description and deadline. + * @param description The description of the task. + * @param by The deadline date for the task. + */ public Deadline(String description, String by) { super(description); this.by = by; } - + /** + * Returns a string representation of the Deadline task. + * @return A string showing the task's type, description, and deadline. + */ @Override public String toString() { return "[D]" + super.toString() + " " + description + " (by: " + by + ")"; } - + /** + * Returns a file format string representation of the Deadline task. + * @return A string formatted for saving to a file, including the task type, completion status, description, and deadline. + */ public String toFileFormat() { return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + by; } -} \ No newline at end of file +} + diff --git a/src/main/java/David/event/Event.java b/src/main/java/David/event/Event.java index 04aa72fb0..057bb2ce6 100644 --- a/src/main/java/David/event/Event.java +++ b/src/main/java/David/event/Event.java @@ -1,23 +1,48 @@ package David.event; + + import David.task.Task; + public class Event extends Task { protected String from; protected String to; + + /** + * Constructs an Event task with the given description, start time, and end time. + * + * @param description The description of the Event task. + * @param from The start time of the event. + * @param to The end time of the event. + */ public Event(String description, String from, String to) { super(description); this.from = from; this.to = to; } + + /** + * Returns a string representation of the Event task. + * + * @return A string showing the task's type, description, start time, and end time. + */ @Override public String toString() { return "[E]" + super.toString() + " " + description + " (from: " + from + " to: " + to + ")"; } + + /** + * Returns a file format string representation of the Event task. + * + * @return A string formatted for saving to a file, including the task type, completion status, description, start time, and end time. + */ public String toFileFormat() { return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + from + " | " + to; } -} \ No newline at end of file + +} + diff --git a/src/main/java/David/task/Task.java b/src/main/java/David/task/Task.java index 088b09456..311ccadc6 100644 --- a/src/main/java/David/task/Task.java +++ b/src/main/java/David/task/Task.java @@ -1,33 +1,75 @@ package David.task; + public class Task { protected String description; protected boolean isDone; + + /** + * Constructs a Task with the given description, initially marked as not done. + * + * @param description The description of the Task. + */ public Task(String description) { this.description = description; this.isDone = false; } + + /** + * Returns the status icon of the task, marking it as done (X) or not done (space). + * + * @return A string "X" if the task is done, otherwise a space. + */ public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + + /** + * Returns the description of the task. + * + * @return The description of the Task. + */ public String getDescription() { return description; } + + /** + * Sets the task's completion status. + * + * @param status The new status of the task (true for done, false for not done). + * @return The new completion status of the task. + */ public boolean setDone(boolean status) { return isDone = status; } + + /** + * Returns a string representation of the task, showing its completion status. + * + * @return A string representation of the Task, including the status icon. + */ @Override public String toString() { return "[" + getStatusIcon() + "]"; } + + /** + * Returns a file format string representation of the task. + * + * @return An empty string, to be overridden in subclasses. + */ public String toFileFormat() { return ""; } -} \ No newline at end of file + +} + + + diff --git a/src/main/java/David/todo/Todo.java b/src/main/java/David/todo/Todo.java index 7817fcbcb..089e3cac1 100644 --- a/src/main/java/David/todo/Todo.java +++ b/src/main/java/David/todo/Todo.java @@ -1,18 +1,38 @@ package David.todo; + + import David.task.Task; -public class Todo extends Task { +public class Todo extends Task { + /** + * Constructs a Todo task with the given description. + * + * @param description The description of the Todo task. + */ public Todo(String description) { super(description); } + + /** + * Returns a string representation of the Todo task. + * + * @return A string showing the task's type and description. + */ @Override public String toString() { - return "[T]" + super.toString() + " " + description; + return "[T]" + super.toString() + " " + description; } + + /** + * Returns a file format string representation of the Todo task. + * + * @return A string formatted for saving to a file, including the task type, completion status, and description. + */ public String toFileFormat() { return "T | " + (isDone ? "1" : "0") + " | " + description; } -} \ No newline at end of file +} + From e7b24da42cd04408b68239d48b0455fd8e435451 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 13 Mar 2025 17:49:16 +0800 Subject: [PATCH 22/26] A-UserGuide: User Guide --- docs/README.md | 161 +++++++++++++++++++-- src/main/java/David/Parser.java | 16 +- src/main/java/David/Storage.java | 2 - src/main/java/David/StorageManager.java | 2 - src/main/java/David/TaskList.java | 2 - src/main/java/David/Ui.java | 2 +- src/main/java/David/deadline/Deadline.java | 9 +- src/main/java/David/event/Event.java | 1 - src/main/java/David/task/Task.java | 1 - src/main/java/David/todo/Todo.java | 1 - 10 files changed, 170 insertions(+), 27 deletions(-) diff --git a/docs/README.md b/docs/README.md index 47b9f984f..727fa860d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,30 +1,165 @@ -# Duke User Guide +# David User Guide -// Update the title above to match the actual product name // Product screenshot goes here -// Product intro goes here +David is a chatbot and task manager designed to help users stay organized effortlessly. Using David, users can add, find, track, and manage different types of tasks, such as to-dos, deadlines, and events with ease. It features a simple command-line interface and supports automatic task saving, ensuring that users never lose track of what needs to be done. -## Adding deadlines +---------------------------------------------------------------------------------------------------- -// Describe the action and its outcome. +## Adding a Todo: `todo` +Adds a todo task to the task list.
+Description: tasks without any date/time attached to it
+Format: todo `DESCRIPTION`
+Example: `todo CS2113`
+Expected output: +---------------------------------------------------------------------------------------------------- -// Give examples of usage +```Got it. I've added this task: +[T][ ] CS2113 +Now you have 6 tasks in the list. +Tasks saved to ./data/tasks.txt +``` +---------------------------------------------------------------------------------------------------- + +## Adding a Deadline: `deadline` +Adds a deadline task to the task list.
+Description: tasks that need to be done before a specific date/time
+Format: deadline `DESCRIPTION` /by `SPECIFIC_DATE` `SPECIFIC_TIME`
+`SPECIFIC_DATE` should be in the format of year/month/day and `SPECIFIC_TIME` should be in `Hour:Minutes`
+Example: `deadline hw /by 2020/03/04 3`
+Expected output: +---------------------------------------------------------------------------------------------------- + +``` Got it. I've added this task: +[D][ ] hw (by: 2020/03/04 3) +Now you have 8 tasks in the list. +____________________________________________________________ +Tasks saved to ./data/tasks.txt +---------------------------------------------------------------------------------------------------- +``` +---------------------------------------------------------------------------------------------------- + + +## Adding an Event: event +Adds an event to the task list.
+Description: tasks that start at a specific date/time and ends at a specific date/time
+Format: event `DESCRIPTION` /from `START` /to `END`
+`START` and `END` should follow the format of `Year/Month/Day` and/or `Hour:Minutes`
+Example: `event project meeting /from Mon 2pm /to 4pm`
+Expected output: +---------------------------------------------------------------------------------------------------- +Got it. I've added this task: +[E][ ] project meeting (from: Mon 2pm to: 4pm) +Now you have 9 tasks in the list. +____________________________________________________________ +Tasks saved to ./data/tasks.txt + +---------------------------------------------------------------------------------------------------- -Example: `keyword (optional arguments)` -// A description of the expected outcome goes here +## Listing all tasks: list
+Shows a list of all tasks in the task list.
+Format: list
+Example: list
+Expected output:
+---------------------------------------------------------------------------------------------------- ``` -expected output +____________________________________________________________ +Here are the tasks in your list: + +1.[D][ ] hw (by: 2020/03/04 3) +____________________________________________________________ +2.[E][ ] project meeting (from: Mon 2pm to: 4pm) +____________________________________________________________ +3.[T][ ] CS2113 +____________________________________________________________ +``` +---------------------------------------------------------------------------------------------------- + + +## Marking Task as Done: mark +Marks a task as done in the task list.
+Format: mark `INDEX_NUMBER`
+Example: `mark 1`
+Expected output of mark 1: +``` +-----------------------------------------------------------------------------____________________________________________________________ +Nice! I've marked this task as done: +[X] project meeting +____________________________________________________________ +---------------------------------------------------------------------------------------------------- +``` + +## Marking Task as Not Done: unmark +Marks a task as not done in the task list.
+Format: unmark `INDEX_NUMBER`
+Example: `unmark 1`
+Expected output of unmark 1:
``` +---------------------------------------------------------------------------------------------------- +____________________________________________________________ +Okay! I've marked this task as not done:_______________________________ +[ ] hw +____________________________________________________________ +Tasks saved to ./data/tasks.txt -## Feature ABC +``` +---------------------------------------------------------------------------------------------------- + + +## Deleting a task: delete +Delete a specified task from the task list.
+Format: delete `INDEX_NUMBER` +Example: `delete 1`
+Expected output of delete 1:
+``` +---------------------------------------------------------------------------------------------------- +There you go. I've removed this task for you: +[T][ ] revise CS2113 OOP +Now you have 2 tasks in the list. +---------------------------------------------------------------------------------------------------- +``` + +## Finding tasks by keyword: find +Finds tasks which descriptions contain the given keyword. If task not found then, print out not found.
+Format: find `KEYWORD`
+Example: `find hw`
+Expected output:
+``` +---------------------------------------------------------------------------------------------------- +1.[D][ ] hw (by: 2020/03/04 3) +____________________________________________________________ +These tasks were found +____________________________________________________________ +``` +---------------------------------------------------------------------------------------------------- +Example: find CS1231 +Expected output: +``` +____________________________________________________________ +Could not be found +____________________________________________________________ + +``` +## Leaving the program: bye +Leaves the program.
+Format: bye
+Example: `bye`
+Expected output: +``` +____________________________________________________________ +Bye. Hope to see you again soon! +____________________________________________________________ +``` -// Feature details +## Saving the Data +Data is saved automatically after any command that results in the data being changed.
+If the program is closed unexpectedly or improperly, any unsaved changes will be lost, and the last saved version of the task list will be used when the program is restarted. +## Editing the Data +All tasks are stored in a file named tasks.txt, located in the current directory.
+Users can manually edit this file if needed, but modifications outside the program may lead to unexpected behavior. -## Feature XYZ -// Feature details \ No newline at end of file diff --git a/src/main/java/David/Parser.java b/src/main/java/David/Parser.java index 507d7aa23..12dc91249 100644 --- a/src/main/java/David/Parser.java +++ b/src/main/java/David/Parser.java @@ -53,7 +53,6 @@ public boolean readCommand(String input) { } else if (input.equals("bye")) { return true; - } else if (input.startsWith("mark")) { task.markTask(task, Integer.parseInt(input.split(" ")[1]) - 1); ui.printMark(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); @@ -83,6 +82,19 @@ public boolean readCommand(String input) { TaskList.deleteTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); storage.saveTasks(task.getTasks()); i--; + } else if (input.startsWith("find")) { + boolean found = false; + for (i = 0; i < task.getTasks().size(); i++) { + if (task.getTask(i).getDescription().contains(input.substring(input.indexOf(" ") + 1))) { + System.out.println((i + 1) + "." + task.getTask(i).toString() + "\n" + LINE_SEPERATOR); + found = true; + } + } + if (!found) { + System.out.println(LINE_SEPERATOR + System.lineSeparator() + "Could not be found" + System.lineSeparator() + LINE_SEPERATOR); + } else { + System.out.println(System.lineSeparator() + "These tasks were found" + System.lineSeparator() + LINE_SEPERATOR); + } } else { throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR); } @@ -142,8 +154,6 @@ private static void parseDeadline(String input, ArrayList task) throws Dav } - - } diff --git a/src/main/java/David/Storage.java b/src/main/java/David/Storage.java index dac055f35..5a7be4441 100644 --- a/src/main/java/David/Storage.java +++ b/src/main/java/David/Storage.java @@ -1,9 +1,7 @@ package David; - import java.util.ArrayList; - import David.task.Task; import David.ui.DavidException; diff --git a/src/main/java/David/StorageManager.java b/src/main/java/David/StorageManager.java index 221bd35d4..4db9085b0 100644 --- a/src/main/java/David/StorageManager.java +++ b/src/main/java/David/StorageManager.java @@ -10,8 +10,6 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; - - import David.deadline.Deadline; import David.event.Event; import David.task.Task; diff --git a/src/main/java/David/TaskList.java b/src/main/java/David/TaskList.java index 1d6e1186a..9d942ab4f 100644 --- a/src/main/java/David/TaskList.java +++ b/src/main/java/David/TaskList.java @@ -1,9 +1,7 @@ package David; - import java.util.ArrayList; - import David.task.Task; import David.ui.DavidException; diff --git a/src/main/java/David/Ui.java b/src/main/java/David/Ui.java index ffb127bf0..cb0f43ebb 100644 --- a/src/main/java/David/Ui.java +++ b/src/main/java/David/Ui.java @@ -101,7 +101,7 @@ public static void printMark(ArrayList task, int index) { * @param index The index of the task in the list. */ public static void printUnmark(ArrayList task, int index) { - System.out.println(LINE_SEPERATOR + "\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); + System.out.println(LINE_SEPERATOR + "\n" + "Okay! I've marked this task as not done:\n" + "[" + task.get(index).getStatusIcon() + "] " + task.get(index).getDescription() + "\n" + LINE_SEPERATOR); } diff --git a/src/main/java/David/deadline/Deadline.java b/src/main/java/David/deadline/Deadline.java index 6c46f9855..35c5327b5 100644 --- a/src/main/java/David/deadline/Deadline.java +++ b/src/main/java/David/deadline/Deadline.java @@ -1,28 +1,35 @@ package David.deadline; + import David.task.Task; public class Deadline extends Task { protected String by; + /** * Constructs a Deadline task with the given description and deadline. + * * @param description The description of the task. - * @param by The deadline date for the task. + * @param by The deadline date for the task. */ public Deadline(String description, String by) { super(description); this.by = by; } + /** * Returns a string representation of the Deadline task. + * * @return A string showing the task's type, description, and deadline. */ @Override public String toString() { return "[D]" + super.toString() + " " + description + " (by: " + by + ")"; } + /** * Returns a file format string representation of the Deadline task. + * * @return A string formatted for saving to a file, including the task type, completion status, description, and deadline. */ public String toFileFormat() { diff --git a/src/main/java/David/event/Event.java b/src/main/java/David/event/Event.java index 057bb2ce6..63128c24f 100644 --- a/src/main/java/David/event/Event.java +++ b/src/main/java/David/event/Event.java @@ -1,6 +1,5 @@ package David.event; - import David.task.Task; diff --git a/src/main/java/David/task/Task.java b/src/main/java/David/task/Task.java index 311ccadc6..7ec0f6166 100644 --- a/src/main/java/David/task/Task.java +++ b/src/main/java/David/task/Task.java @@ -1,6 +1,5 @@ package David.task; - public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/David/todo/Todo.java b/src/main/java/David/todo/Todo.java index 089e3cac1..15ec11bfb 100644 --- a/src/main/java/David/todo/Todo.java +++ b/src/main/java/David/todo/Todo.java @@ -1,6 +1,5 @@ package David.todo; - import David.task.Task; From a4b0571f5d5b69e95091d258bbcf9e5e26f03e7b Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 13 Mar 2025 18:17:41 +0800 Subject: [PATCH 23/26] Added Image to David ReadMe --- docs/README.md | 48 ++++++++++++++++++++++++--------- src/main/java/David/Parser.java | 3 +++ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/docs/README.md b/docs/README.md index 727fa860d..3d864ba60 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,18 +1,20 @@ # David User Guide - -// Product screenshot goes here - -David is a chatbot and task manager designed to help users stay organized effortlessly. Using David, users can add, find, track, and manage different types of tasks, such as to-dos, deadlines, and events with ease. It features a simple command-line interface and supports automatic task saving, ensuring that users never lose track of what needs to be done. +![davidwelcome.png](../out/image/davidwelcome.png) +David is a chatbot and task manager designed to help users stay organized effortlessly. Using David, users can add, +find, track, and manage different types of tasks, such as to-dos, deadlines, and events with ease. It features a simple +command-line interface and supports automatic task saving, ensuring that users never lose track of what needs to be +done. ---------------------------------------------------------------------------------------------------- ## Adding a Todo: `todo` + Adds a todo task to the task list.
Description: tasks without any date/time attached to it
Format: todo `DESCRIPTION`
Example: `todo CS2113`
-Expected output: +Expected output: ---------------------------------------------------------------------------------------------------- ```Got it. I've added this task: @@ -20,15 +22,17 @@ Expected output: Now you have 6 tasks in the list. Tasks saved to ./data/tasks.txt ``` + ---------------------------------------------------------------------------------------------------- ## Adding a Deadline: `deadline` + Adds a deadline task to the task list.
Description: tasks that need to be done before a specific date/time
Format: deadline `DESCRIPTION` /by `SPECIFIC_DATE` `SPECIFIC_TIME`
`SPECIFIC_DATE` should be in the format of year/month/day and `SPECIFIC_TIME` should be in `Hour:Minutes`
Example: `deadline hw /by 2020/03/04 3`
-Expected output: +Expected output: ---------------------------------------------------------------------------------------------------- ``` Got it. I've added this task: @@ -38,16 +42,17 @@ ____________________________________________________________ Tasks saved to ./data/tasks.txt ---------------------------------------------------------------------------------------------------- ``` ----------------------------------------------------------------------------------------------------- +---------------------------------------------------------------------------------------------------- ## Adding an Event: event + Adds an event to the task list.
Description: tasks that start at a specific date/time and ends at a specific date/time
Format: event `DESCRIPTION` /from `START` /to `END`
`START` and `END` should follow the format of `Year/Month/Day` and/or `Hour:Minutes`
Example: `event project meeting /from Mon 2pm /to 4pm`
-Expected output: +Expected output: ---------------------------------------------------------------------------------------------------- Got it. I've added this task: [E][ ] project meeting (from: Mon 2pm to: 4pm) @@ -57,14 +62,15 @@ Tasks saved to ./data/tasks.txt ---------------------------------------------------------------------------------------------------- - ## Listing all tasks: list
+ Shows a list of all tasks in the task list.
Format: list
Example: list
Expected output:
---------------------------------------------------------------------------------------------------- + ``` ____________________________________________________________ Here are the tasks in your list: @@ -76,14 +82,16 @@ ____________________________________________________________ 3.[T][ ] CS2113 ____________________________________________________________ ``` ----------------------------------------------------------------------------------------------------- +---------------------------------------------------------------------------------------------------- ## Marking Task as Done: mark + Marks a task as done in the task list.
Format: mark `INDEX_NUMBER`
Example: `mark 1`
-Expected output of mark 1: +Expected output of mark 1: + ``` -----------------------------------------------------------------------------____________________________________________________________ Nice! I've marked this task as done: @@ -93,10 +101,12 @@ ____________________________________________________________ ``` ## Marking Task as Not Done: unmark + Marks a task as not done in the task list.
Format: unmark `INDEX_NUMBER`
Example: `unmark 1`
Expected output of unmark 1:
+ ``` ---------------------------------------------------------------------------------------------------- ____________________________________________________________ @@ -106,14 +116,16 @@ ____________________________________________________________ Tasks saved to ./data/tasks.txt ``` ----------------------------------------------------------------------------------------------------- +---------------------------------------------------------------------------------------------------- ## Deleting a task: delete + Delete a specified task from the task list.
Format: delete `INDEX_NUMBER` Example: `delete 1`
Expected output of delete 1:
+ ``` ---------------------------------------------------------------------------------------------------- There you go. I've removed this task for you: @@ -123,10 +135,12 @@ Now you have 2 tasks in the list. ``` ## Finding tasks by keyword: find + Finds tasks which descriptions contain the given keyword. If task not found then, print out not found.
Format: find `KEYWORD`
Example: `find hw`
Expected output:
+ ``` ---------------------------------------------------------------------------------------------------- 1.[D][ ] hw (by: 2020/03/04 3) @@ -134,20 +148,25 @@ ____________________________________________________________ These tasks were found ____________________________________________________________ ``` + ---------------------------------------------------------------------------------------------------- Example: find CS1231 Expected output: + ``` ____________________________________________________________ Could not be found ____________________________________________________________ ``` + ## Leaving the program: bye + Leaves the program.
Format: bye
Example: `bye`
Expected output: + ``` ____________________________________________________________ Bye. Hope to see you again soon! @@ -155,10 +174,13 @@ ____________________________________________________________ ``` ## Saving the Data + Data is saved automatically after any command that results in the data being changed.
-If the program is closed unexpectedly or improperly, any unsaved changes will be lost, and the last saved version of the task list will be used when the program is restarted. +If the program is closed unexpectedly or improperly, any unsaved changes will be lost, and the last saved version of the +task list will be used when the program is restarted. ## Editing the Data + All tasks are stored in a file named tasks.txt, located in the current directory.
Users can manually edit this file if needed, but modifications outside the program may lead to unexpected behavior. diff --git a/src/main/java/David/Parser.java b/src/main/java/David/Parser.java index 12dc91249..80c785a20 100644 --- a/src/main/java/David/Parser.java +++ b/src/main/java/David/Parser.java @@ -103,6 +103,9 @@ public boolean readCommand(String input) { } catch (DavidException e) { System.out.println(LINE_SEPERATOR + System.lineSeparator() + " Oops! The description of a task should NOT be blank! Please try again :)" + System.lineSeparator() + LINE_SEPERATOR); } + catch (IndexOutOfBoundsException e) { + System.out.println("Does not exist, try again. " + System.lineSeparator() + LINE_SEPERATOR); + } return false; From fe8fe3394b8c4f4882f3b914b451924e87f4469a Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 13 Mar 2025 18:26:04 +0800 Subject: [PATCH 24/26] Fixed ReadMe Format --- docs/README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/docs/README.md b/docs/README.md index 3d864ba60..bcf14f0f2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -15,15 +15,15 @@ Description: tasks without any date/time attached to it
Format: todo `DESCRIPTION`
Example: `todo CS2113`
Expected output: ----------------------------------------------------------------------------------------------------- -```Got it. I've added this task: +``` +---------------------------------------------------------------------------------------------------- + Got it. I've added this task: [T][ ] CS2113 Now you have 6 tasks in the list. Tasks saved to ./data/tasks.txt -``` - ---------------------------------------------------------------------------------------------------- +``` ## Adding a Deadline: `deadline` @@ -33,9 +33,9 @@ Format: deadline `DESCRIPTION` /by `SPECIFIC_DATE` `SPECIFIC_TIME`
`SPECIFIC_DATE` should be in the format of year/month/day and `SPECIFIC_TIME` should be in `Hour:Minutes`
Example: `deadline hw /by 2020/03/04 3`
Expected output: +``` ---------------------------------------------------------------------------------------------------- - -``` Got it. I've added this task: +Got it. I've added this task: [D][ ] hw (by: 2020/03/04 3) Now you have 8 tasks in the list. ____________________________________________________________ @@ -53,14 +53,15 @@ Format: event `DESCRIPTION` /from `START` /to `END`
`START` and `END` should follow the format of `Year/Month/Day` and/or `Hour:Minutes`
Example: `event project meeting /from Mon 2pm /to 4pm`
Expected output: +``` ---------------------------------------------------------------------------------------------------- Got it. I've added this task: [E][ ] project meeting (from: Mon 2pm to: 4pm) Now you have 9 tasks in the list. ____________________________________________________________ Tasks saved to ./data/tasks.txt - ---------------------------------------------------------------------------------------------------- +``` ## Listing all tasks: list
@@ -93,11 +94,10 @@ Example: `mark 1`
Expected output of mark 1: ``` ------------------------------------------------------------------------------____________________________________________________________ +____________________________________________________________ Nice! I've marked this task as done: [X] project meeting ____________________________________________________________ ----------------------------------------------------------------------------------------------------- ``` ## Marking Task as Not Done: unmark @@ -108,13 +108,11 @@ Example: `unmark 1`
Expected output of unmark 1:
``` ----------------------------------------------------------------------------------------------------- ____________________________________________________________ -Okay! I've marked this task as not done:_______________________________ +Okay! I've marked this task as not done: [ ] hw ____________________________________________________________ Tasks saved to ./data/tasks.txt - ``` ---------------------------------------------------------------------------------------------------- @@ -134,6 +132,8 @@ Now you have 2 tasks in the list. ---------------------------------------------------------------------------------------------------- ``` +---------------------------------------------------------------------------------------------------- + ## Finding tasks by keyword: find Finds tasks which descriptions contain the given keyword. If task not found then, print out not found.
@@ -142,14 +142,13 @@ Example: `find hw`
Expected output:
``` ----------------------------------------------------------------------------------------------------- +____________________________________________________________ 1.[D][ ] hw (by: 2020/03/04 3) ____________________________________________________________ These tasks were found ____________________________________________________________ ``` ----------------------------------------------------------------------------------------------------- Example: find CS1231 Expected output: @@ -159,6 +158,7 @@ Could not be found ____________________________________________________________ ``` +---------------------------------------------------------------------------------------------------- ## Leaving the program: bye @@ -172,7 +172,7 @@ ____________________________________________________________ Bye. Hope to see you again soon! ____________________________________________________________ ``` - +---------------------------------------------------------------------------------------------------- ## Saving the Data Data is saved automatically after any command that results in the data being changed.
From f1b149ab740240bf61a3523f407074734f506647 Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 13 Mar 2025 19:02:41 +0800 Subject: [PATCH 25/26] Fixed Minor Unmark/Mark Tasks Bug --- src/main/java/David/Parser.java | 10 +++++----- src/main/java/David/StorageManager.java | 13 +++++++------ src/main/java/David/deadline/Deadline.java | 5 ++--- src/main/java/David/event/Event.java | 6 +++--- src/main/java/David/task/Task.java | 5 ++--- src/main/java/David/todo/Todo.java | 5 ++--- 6 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/main/java/David/Parser.java b/src/main/java/David/Parser.java index 80c785a20..14ee0e85c 100644 --- a/src/main/java/David/Parser.java +++ b/src/main/java/David/Parser.java @@ -20,6 +20,7 @@ public class Parser { private final Ui ui; private final TaskList task; private final Storage storage; + private static boolean isDone; /** @@ -77,9 +78,8 @@ public boolean readCommand(String input) { storage.saveTasks(task.getTasks()); i++; - } else if (input.startsWith("delete")) { - TaskList.deleteTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); + task.deleteTask(task.getTasks(), Integer.parseInt(input.split(" ")[1]) - 1); storage.saveTasks(task.getTasks()); i--; } else if (input.startsWith("find")) { @@ -129,7 +129,7 @@ public boolean readCommand(String input) { * @throws DavidException If the description of the task is missing. */ private static void parseTodo(String input, ArrayList task) throws DavidException { - task.add(new Todo(input.substring(TaskList.getIndex(input, " ") + 1))); + task.add(new Todo(input.substring(TaskList.getIndex(input, " ") + 1), isDone)); } @@ -141,7 +141,7 @@ private static void parseTodo(String input, ArrayList task) throws DavidEx * @throws DavidException If the description of the task is missing. */ private static void parseEvent(String input, ArrayList task) throws DavidException { - task.add(new Event((input.substring(TaskList.getIndex(input, " ") + 1, TaskList.getIndex(input, "/") - 1)), input.substring(TaskList.getIndex(input, "from") + 5, TaskList.getIndex(input, "to") - 2), input.substring(TaskList.getIndex(input, "to") + 3))); + task.add(new Event((input.substring(TaskList.getIndex(input, " ") + 1, TaskList.getIndex(input, "/") - 1)), input.substring(TaskList.getIndex(input, "from") + 5, TaskList.getIndex(input, "to") - 2), input.substring(TaskList.getIndex(input, "to") + 3), isDone)); } @@ -153,7 +153,7 @@ private static void parseEvent(String input, ArrayList task) throws DavidE * @throws DavidException If the description of the task is missing. */ private static void parseDeadline(String input, ArrayList task) throws DavidException { - task.add(new Deadline((input.substring(TaskList.getIndex(input, " ") + 1, TaskList.getIndex(input, "/") - 1)), input.substring(TaskList.getIndex(input, "by") + 3))); + task.add(new Deadline((input.substring(TaskList.getIndex(input, " ") + 1, TaskList.getIndex(input, "/") - 1)), input.substring(TaskList.getIndex(input, "by") + 3), isDone)); } diff --git a/src/main/java/David/StorageManager.java b/src/main/java/David/StorageManager.java index 4db9085b0..e7de3c5fb 100644 --- a/src/main/java/David/StorageManager.java +++ b/src/main/java/David/StorageManager.java @@ -47,7 +47,6 @@ public static ArrayList loadTasks(String filePath) { return tasks; } - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; while ((line = reader.readLine()) != null) { @@ -57,22 +56,23 @@ public static ArrayList loadTasks(String filePath) { continue; } - String taskType = parts[0]; - boolean isDone = "1".equals(parts[1]); + boolean isDone = "1".equals(parts[1]); // Ensure the task is marked done if "1" String description = parts[2]; Task task; + // Create tasks based on the task type if ("T".equals(taskType)) { - task = new Todo(description); + task = new Todo(description, isDone); } else if ("D".equals(taskType) && parts.length > 3) { - task = new Deadline(description, parts[3]); + task = new Deadline(description, parts[3], isDone); } else if ("E".equals(taskType) && parts.length > 4) { - task = new Event(description, parts[3], parts[4]); + task = new Event(description, parts[3], parts[4], isDone); } else { System.out.println("Unknown task type: " + taskType); continue; } + tasks.add(task); } } catch (IOException e) { @@ -81,5 +81,6 @@ public static ArrayList loadTasks(String filePath) { } return tasks; } + } diff --git a/src/main/java/David/deadline/Deadline.java b/src/main/java/David/deadline/Deadline.java index 35c5327b5..0f95b095e 100644 --- a/src/main/java/David/deadline/Deadline.java +++ b/src/main/java/David/deadline/Deadline.java @@ -12,11 +12,10 @@ public class Deadline extends Task { * @param description The description of the task. * @param by The deadline date for the task. */ - public Deadline(String description, String by) { - super(description); + public Deadline(String description, String by, boolean isDone) { + super(description, isDone); this.by = by; } - /** * Returns a string representation of the Deadline task. * diff --git a/src/main/java/David/event/Event.java b/src/main/java/David/event/Event.java index 63128c24f..ff4b339ef 100644 --- a/src/main/java/David/event/Event.java +++ b/src/main/java/David/event/Event.java @@ -15,13 +15,13 @@ public class Event extends Task { * @param from The start time of the event. * @param to The end time of the event. */ - public Event(String description, String from, String to) { - super(description); + + public Event(String description, String from, String to, boolean isDone) { + super(description, isDone); this.from = from; this.to = to; } - /** * Returns a string representation of the Event task. * diff --git a/src/main/java/David/task/Task.java b/src/main/java/David/task/Task.java index 7ec0f6166..1593243fc 100644 --- a/src/main/java/David/task/Task.java +++ b/src/main/java/David/task/Task.java @@ -10,12 +10,11 @@ public class Task { * * @param description The description of the Task. */ - public Task(String description) { + public Task(String description, boolean isDone) { this.description = description; - this.isDone = false; + this.isDone = isDone; } - /** * Returns the status icon of the task, marking it as done (X) or not done (space). * diff --git a/src/main/java/David/todo/Todo.java b/src/main/java/David/todo/Todo.java index 15ec11bfb..dc8da683d 100644 --- a/src/main/java/David/todo/Todo.java +++ b/src/main/java/David/todo/Todo.java @@ -9,11 +9,10 @@ public class Todo extends Task { * * @param description The description of the Todo task. */ - public Todo(String description) { - super(description); + public Todo(String description, boolean isDone) { + super(description, isDone); // Store isDone status } - /** * Returns a string representation of the Todo task. * From 68630aedae98cff613c5f129c26f9b47320d91da Mon Sep 17 00:00:00 2001 From: Gyurim Date: Thu, 13 Mar 2025 23:26:38 +0800 Subject: [PATCH 26/26] Fixed Image Issue in Readme --- docs/README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index bcf14f0f2..d607e0bef 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,24 @@ # David User Guide -![davidwelcome.png](../out/image/davidwelcome.png) +``` +Hello from + ______ ____ ,---. ,---..-./`) ______ .---. +| _ `''. .' __ `. | / | |\ .-.')| _ `''. \ / +| _ | ) _ \/ ' \ \| | | .'/ `-' \| _ | ) _ \ | | +|( ''_' ) ||___| / || | _ | | `-'`"`|( ''_' ) | \ / +| . (_) `. | _.-` || _( )_ | .---. | . (_) `. | v +|(_ ._) '.' _ |\ (_ o._) / | | |(_ ._) ' _ _ +| (_.\.' / | _( )_ | \ (_,_) / | | | (_.\.' / (_I_) +| .' \ (_ o _) / \ / | | | .' (_(=)_) +'-----'` '.(_,_).' `---` '---' '-----'` (_I_) + +____________________________________________________________ + Hello! I'm David + What can I do for you? +____________________________________________________________ + + +``` David is a chatbot and task manager designed to help users stay organized effortlessly. Using David, users can add, find, track, and manage different types of tasks, such as to-dos, deadlines, and events with ease. It features a simple command-line interface and supports automatic task saving, ensuring that users never lose track of what needs to be