From ff10a435e8390ee762c0c03b2d37e4fb8eeb381d Mon Sep 17 00:00:00 2001 From: himethcodes Date: Fri, 7 Feb 2025 21:29:53 +0800 Subject: [PATCH 01/35] Level-0: Rename, Greet, Exit --- ip/src/main/java/BuddyBot.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ip/src/main/java/BuddyBot.java diff --git a/ip/src/main/java/BuddyBot.java b/ip/src/main/java/BuddyBot.java new file mode 100644 index 000000000..bf6741f84 --- /dev/null +++ b/ip/src/main/java/BuddyBot.java @@ -0,0 +1,11 @@ +public class BuddyBot { // Replace chatbot's name + public static void main(String[] args) { + System.out.println("____________________________________________________________"); + System.out.println(" Hello! I'm BuddyBot"); // Change chatbot's name + System.out.println(" What can I do for you?"); + System.out.println("____________________________________________________________"); + + System.out.println(" Bye. Hope to see you again soon!"); + System.out.println("____________________________________________________________"); + } +} From 303dc880c00dfc47c279bf6c8d25d2f637c9d752 Mon Sep 17 00:00:00 2001 From: himethcodes Date: Fri, 7 Feb 2025 22:34:22 +0800 Subject: [PATCH 02/35] Level-1: Echo feature added --- ip/src/main/java/BuddyBot.java | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ip/src/main/java/BuddyBot.java b/ip/src/main/java/BuddyBot.java index bf6741f84..2268bee10 100644 --- a/ip/src/main/java/BuddyBot.java +++ b/ip/src/main/java/BuddyBot.java @@ -1,11 +1,29 @@ -public class BuddyBot { // Replace chatbot's name +import java.util.Scanner; + +public class BuddyBot { public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("____________________________________________________________"); - System.out.println(" Hello! I'm BuddyBot"); // Change chatbot's name + System.out.println(" Hello! I'm BuddyBot"); System.out.println(" What can I do for you?"); System.out.println("____________________________________________________________"); - System.out.println(" Bye. Hope to see you again soon!"); - System.out.println("____________________________________________________________"); + while (true) { + String userInput = scanner.nextLine(); + + if (userInput.equalsIgnoreCase("bye")) { + System.out.println("____________________________________________________________"); + System.out.println(" Bye. Hope to see you again soon!"); + System.out.println("____________________________________________________________"); + break; + } + + System.out.println("____________________________________________________________"); + System.out.println(" " + userInput); + System.out.println("____________________________________________________________"); + } + + scanner.close(); } } From c346b3fbe64c68d0d03cc4e86eb35332ff609395 Mon Sep 17 00:00:00 2001 From: himethcodes Date: Fri, 7 Feb 2025 22:39:36 +0800 Subject: [PATCH 03/35] Level-2: Add and List feature implemented --- ip/src/main/java/BuddyBot.java | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/ip/src/main/java/BuddyBot.java b/ip/src/main/java/BuddyBot.java index 2268bee10..644e377d8 100644 --- a/ip/src/main/java/BuddyBot.java +++ b/ip/src/main/java/BuddyBot.java @@ -3,27 +3,38 @@ public class BuddyBot { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); + String[] tasks = new String[100]; // Array to store tasks + int taskCount = 0; // Number of tasks stored + // Print greeting System.out.println("____________________________________________________________"); System.out.println(" Hello! I'm BuddyBot"); System.out.println(" What can I do for you?"); System.out.println("____________________________________________________________"); while (true) { - String userInput = scanner.nextLine(); + String input = scanner.nextLine(); // Read user input - if (userInput.equalsIgnoreCase("bye")) { + if (input.equals("bye")) { System.out.println("____________________________________________________________"); System.out.println(" Bye. Hope to see you again soon!"); System.out.println("____________________________________________________________"); break; + } else if (input.equals("list")) { + System.out.println("____________________________________________________________"); + for (int i = 0; i < taskCount; i++) { + System.out.println((i + 1) + ". " + tasks[i]); // Display stored tasks + } + System.out.println("____________________________________________________________"); + } else { + tasks[taskCount] = input; // Store the task + taskCount++; + System.out.println("____________________________________________________________"); + System.out.println(" added: " + input); + System.out.println("____________________________________________________________"); } - - System.out.println("____________________________________________________________"); - System.out.println(" " + userInput); - System.out.println("____________________________________________________________"); } - scanner.close(); + scanner.close(); // Close the scanner } } From 3bf3953422551869d7b17731e1f4a78dcf7a7800 Mon Sep 17 00:00:00 2001 From: himethcodes Date: Fri, 7 Feb 2025 22:52:35 +0800 Subject: [PATCH 04/35] Level-3: Mark as Done --- ip/src/main/java/BuddyBot.java | 112 ++++++++++++++++++++++++++------- ip/src/main/java/Task.java | 26 ++++++++ 2 files changed, 115 insertions(+), 23 deletions(-) create mode 100644 ip/src/main/java/Task.java diff --git a/ip/src/main/java/BuddyBot.java b/ip/src/main/java/BuddyBot.java index 644e377d8..d0d5d37e0 100644 --- a/ip/src/main/java/BuddyBot.java +++ b/ip/src/main/java/BuddyBot.java @@ -1,40 +1,106 @@ import java.util.Scanner; public class BuddyBot { + private static final int MAX_TASKS = 100; + private static Task[] tasks = new Task[MAX_TASKS]; + private static int taskCount = 0; + public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - String[] tasks = new String[100]; // Array to store tasks - int taskCount = 0; // Number of tasks stored - // Print greeting + printGreeting(); + + while (true) { + String command = scanner.nextLine().trim(); + + if (command.equals("bye")) { + printExitMessage(); + break; + } else if (command.equals("list")) { + printTaskList(); + } else if (command.startsWith("mark ")) { + markTaskAsDone(command); + } else if (command.startsWith("unmark ")) { + unmarkTask(command); + } else { + addTask(command); + } + } + + scanner.close(); + } + + private static void printGreeting() { System.out.println("____________________________________________________________"); System.out.println(" Hello! I'm BuddyBot"); System.out.println(" What can I do for you?"); System.out.println("____________________________________________________________"); + } - while (true) { - String input = scanner.nextLine(); // Read user input + private static void printExitMessage() { + System.out.println("____________________________________________________________"); + System.out.println(" Bye. Hope to see you again soon!"); + System.out.println("____________________________________________________________"); + } - if (input.equals("bye")) { - System.out.println("____________________________________________________________"); - System.out.println(" Bye. Hope to see you again soon!"); - System.out.println("____________________________________________________________"); - break; - } else if (input.equals("list")) { - System.out.println("____________________________________________________________"); - for (int i = 0; i < taskCount; i++) { - System.out.println((i + 1) + ". " + tasks[i]); // Display stored tasks - } - System.out.println("____________________________________________________________"); - } else { - tasks[taskCount] = input; // Store the task - taskCount++; - System.out.println("____________________________________________________________"); - System.out.println(" added: " + input); - System.out.println("____________________________________________________________"); + private static void addTask(String taskDescription) { + if (taskCount < MAX_TASKS) { + tasks[taskCount] = new Task(taskDescription); + taskCount++; + System.out.println("____________________________________________________________"); + System.out.println(" added: " + taskDescription); + System.out.println("____________________________________________________________"); + } else { + System.out.println("Task list is full!"); + } + } + + private static void printTaskList() { + System.out.println("____________________________________________________________"); + if (taskCount == 0) { + System.out.println(" No tasks in the list!"); + } else { + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < taskCount; i++) { + System.out.println(" " + (i + 1) + ". " + tasks[i]); } } + System.out.println("____________________________________________________________"); + } + + private static void markTaskAsDone(String command) { + int taskIndex = getTaskIndex(command, "mark "); + if (taskIndex != -1) { + tasks[taskIndex].markAsDone(); + System.out.println("____________________________________________________________"); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" " + tasks[taskIndex]); + System.out.println("____________________________________________________________"); + } + } + + private static void unmarkTask(String command) { + int taskIndex = getTaskIndex(command, "unmark "); + if (taskIndex != -1) { + tasks[taskIndex].unmarkAsDone(); + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println(" " + tasks[taskIndex]); + System.out.println("____________________________________________________________"); + } + } - scanner.close(); // Close the scanner + private static int getTaskIndex(String command, String prefix) { + try { + int index = Integer.parseInt(command.substring(prefix.length())) - 1; + if (index >= 0 && index < taskCount) { + return index; + } else { + System.out.println("Invalid task number!"); + } + } catch (NumberFormatException e) { + System.out.println("Please enter a valid number."); + } + return -1; } } diff --git a/ip/src/main/java/Task.java b/ip/src/main/java/Task.java new file mode 100644 index 000000000..c46d22617 --- /dev/null +++ b/ip/src/main/java/Task.java @@ -0,0 +1,26 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public void markAsDone() { + isDone = true; + } + + public void unmarkAsDone() { + isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "[X]" : "[ ]"); // X for done, space for not done + } + + @Override + public String toString() { + return getStatusIcon() + " " + description; + } +} From c7eed9116969afb1377f71e284a74334f23c2f94 Mon Sep 17 00:00:00 2001 From: himethcodes Date: Fri, 7 Feb 2025 23:05:27 +0800 Subject: [PATCH 05/35] Completed Level-3: Mark as Done --- ip/.gitignore | 17 ++++++++++++++++ ip/CONTRIBUTORS.md | 9 +++++++++ ip/README.md | 26 ++++++++++++++++++++++++ ip/docs/README.md | 30 ++++++++++++++++++++++++++++ ip/text-ui-test/EXPECTED.TXT | 7 +++++++ ip/text-ui-test/input.txt | 0 ip/text-ui-test/runtest.bat | 21 ++++++++++++++++++++ ip/text-ui-test/runtest.sh | 38 ++++++++++++++++++++++++++++++++++++ 8 files changed, 148 insertions(+) create mode 100644 ip/.gitignore create mode 100644 ip/CONTRIBUTORS.md create mode 100644 ip/README.md create mode 100644 ip/docs/README.md create mode 100644 ip/text-ui-test/EXPECTED.TXT create mode 100644 ip/text-ui-test/input.txt create mode 100644 ip/text-ui-test/runtest.bat create mode 100644 ip/text-ui-test/runtest.sh diff --git a/ip/.gitignore b/ip/.gitignore new file mode 100644 index 000000000..2873e189e --- /dev/null +++ b/ip/.gitignore @@ -0,0 +1,17 @@ +# IDEA files +/.idea/ +/out/ +/*.iml + +# Gradle build files +/.gradle/ +/build/ +src/main/resources/docs/ + +# MacOS custom attributes files created by Finder +.DS_Store +*.iml +bin/ + +/text-ui-test/ACTUAL.TXT +text-ui-test/EXPECTED-UNIX.TXT diff --git a/ip/CONTRIBUTORS.md b/ip/CONTRIBUTORS.md new file mode 100644 index 000000000..8e359a014 --- /dev/null +++ b/ip/CONTRIBUTORS.md @@ -0,0 +1,9 @@ +# Contributors + +Display | Name | Github Profile | Homepage +---|:---:|:---:|:---: +![](https://avatars0.githubusercontent.com/u/22460123?s=100) | Jeffry Lum | [Github](https://github.com/j-lum/) | [Homepage](https://se.kasugano.moe) +![](https://avatars0.githubusercontent.com/u/1673303?s=100) | Damith C. Rajapakse | [Github](https://github.com/damithc/) | [Homepage](https://www.comp.nus.edu.sg/~damithch/) +# I would like to join this list. How can I help the project + +For more information, please refer to our [contributor's guide](https://oss-generic.github.io/process/). diff --git a/ip/README.md b/ip/README.md new file mode 100644 index 000000000..cb097281e --- /dev/null +++ b/ip/README.md @@ -0,0 +1,26 @@ +# BuddyBot project template + +This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. + +## Setting up in Intellij + +Prerequisites: JDK 17, update Intellij to the most recent version. + +1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) +1. Open the project into Intellij as follows: + 1. Click `Open`. + 1. Select the project directory, and click `OK`. + 1. If there are any further prompts, accept the defaults. +1. Configure the project to use **JDK 17** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
+ In the same dialog, set the **Project language level** field to the `SDK default` option. +1. After that, locate the `src/main/java/BuddyBot.java` file, right-click it, and choose `Run BuddyBot.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: + ``` + Hello from + ____ _ + | _ \ _ _| | _____ + | | | | | | | |/ / _ \ + | |_| | |_| | < __/ + |____/ \__,_|_|\_\___| + ``` + +**Warning:** Keep the `src\main\java` folder as the root folder for Java files (i.e., don't rename those folders or move Java files to another folder outside of this folder path), as this is the default location some tools (e.g., Gradle) expect to find Java files. diff --git a/ip/docs/README.md b/ip/docs/README.md new file mode 100644 index 000000000..ea889d98a --- /dev/null +++ b/ip/docs/README.md @@ -0,0 +1,30 @@ +# BuddyBot User Guide + +// Update the title above to match the actual product name + +// Product screenshot goes here + +// Product intro goes here + +## Adding deadlines + +// Describe the action and its outcome. + +// Give examples of usage + +Example: `keyword (optional arguments)` + +// A description of the expected outcome goes here + +``` +expected output +``` + +## Feature ABC + +// Feature details + + +## Feature XYZ + +// Feature details \ No newline at end of file diff --git a/ip/text-ui-test/EXPECTED.TXT b/ip/text-ui-test/EXPECTED.TXT new file mode 100644 index 000000000..657e74f6e --- /dev/null +++ b/ip/text-ui-test/EXPECTED.TXT @@ -0,0 +1,7 @@ +Hello from + ____ _ +| _ \ _ _| | _____ +| | | | | | | |/ / _ \ +| |_| | |_| | < __/ +|____/ \__,_|_|\_\___| + diff --git a/ip/text-ui-test/input.txt b/ip/text-ui-test/input.txt new file mode 100644 index 000000000..e69de29bb diff --git a/ip/text-ui-test/runtest.bat b/ip/text-ui-test/runtest.bat new file mode 100644 index 000000000..99f72b1e2 --- /dev/null +++ b/ip/text-ui-test/runtest.bat @@ -0,0 +1,21 @@ +@ECHO OFF + +REM create bin directory if it doesn't exist +if not exist ..\bin mkdir ..\bin + +REM delete output from previous run +if exist ACTUAL.TXT del ACTUAL.TXT + +REM compile the code into the bin folder +javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java +IF ERRORLEVEL 1 ( + echo ********** BUILD FAILURE ********** + exit /b 1 +) +REM no error here, errorlevel == 0 + +REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT +java -classpath ..\bin BuddyBot < input.txt > ACTUAL.TXT + +REM compare the output to the expected output +FC ACTUAL.TXT EXPECTED.TXT diff --git a/ip/text-ui-test/runtest.sh b/ip/text-ui-test/runtest.sh new file mode 100644 index 000000000..c9ec87003 --- /dev/null +++ b/ip/text-ui-test/runtest.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +# create bin directory if it doesn't exist +if [ ! -d "../bin" ] +then + mkdir ../bin +fi + +# delete output from previous run +if [ -e "./ACTUAL.TXT" ] +then + rm ACTUAL.TXT +fi + +# compile the code into the bin folder, terminates if error occurred +if ! javac -cp ../src/main/java -Xlint:none -d ../bin ../src/main/java/*.java +then + echo "********** BUILD FAILURE **********" + exit 1 +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 + +# compare the output to the expected output +diff ACTUAL.TXT EXPECTED-UNIX.TXT +if [ $? -eq 0 ] +then + echo "Test result: PASSED" + exit 0 +else + echo "Test result: FAILED" + exit 1 +fi \ No newline at end of file From 939475c2603a21e0aa639fab07c95d875454bc50 Mon Sep 17 00:00:00 2001 From: himethcodes Date: Sun, 9 Feb 2025 00:52:46 +0800 Subject: [PATCH 06/35] Level-4: Added support for ToDos, Deadlines, and Events --- ip/src/main/java/BuddyBot.java | 126 +++++++++++++++------------------ ip/src/main/java/Deadline.java | 15 ++++ ip/src/main/java/Event.java | 15 ++++ ip/src/main/java/Todo.java | 10 +++ 4 files changed, 97 insertions(+), 69 deletions(-) create mode 100644 ip/src/main/java/Deadline.java create mode 100644 ip/src/main/java/Event.java create mode 100644 ip/src/main/java/Todo.java diff --git a/ip/src/main/java/BuddyBot.java b/ip/src/main/java/BuddyBot.java index d0d5d37e0..a64f4d0b6 100644 --- a/ip/src/main/java/BuddyBot.java +++ b/ip/src/main/java/BuddyBot.java @@ -1,106 +1,94 @@ +import java.util.ArrayList; import java.util.Scanner; public class BuddyBot { - private static final int MAX_TASKS = 100; - private static Task[] tasks = new Task[MAX_TASKS]; - private static int taskCount = 0; + private final ArrayList tasks; - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); + public BuddyBot() { + this.tasks = new ArrayList<>(); + } - printGreeting(); + public void run() { + Scanner scanner = new Scanner(System.in); + System.out.println("Hello! I'm BuddyBot\nWhat can I do for you?"); while (true) { - String command = scanner.nextLine().trim(); + String command = scanner.nextLine(); if (command.equals("bye")) { - printExitMessage(); + System.out.println("Bye! Hope to see you again soon!"); break; } else if (command.equals("list")) { - printTaskList(); + listTasks(); + } else if (command.startsWith("todo ")) { + addTodo(command.substring(5)); + } else if (command.startsWith("deadline ")) { + String[] parts = command.substring(9).split(" /by "); + addDeadline(parts[0], parts[1]); + } else if (command.startsWith("event ")) { + String[] parts = command.substring(6).split(" /from | /to "); + addEvent(parts[0], parts[1], parts[2]); } else if (command.startsWith("mark ")) { - markTaskAsDone(command); + markTask(Integer.parseInt(command.substring(5)) - 1); } else if (command.startsWith("unmark ")) { - unmarkTask(command); + unmarkTask(Integer.parseInt(command.substring(7)) - 1); } else { - addTask(command); + System.out.println("Sorry, I don't understand that command."); } } scanner.close(); } - private static void printGreeting() { - System.out.println("____________________________________________________________"); - System.out.println(" Hello! I'm BuddyBot"); - System.out.println(" What can I do for you?"); - System.out.println("____________________________________________________________"); + private void listTasks() { + System.out.println("Here are your tasks:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + "." + tasks.get(i)); + } } - private static void printExitMessage() { - System.out.println("____________________________________________________________"); - System.out.println(" Bye. Hope to see you again soon!"); - System.out.println("____________________________________________________________"); + private void addTodo(String description) { + Task task = new Todo(description); + tasks.add(task); + printTaskAdded(task); } - private static void addTask(String taskDescription) { - if (taskCount < MAX_TASKS) { - tasks[taskCount] = new Task(taskDescription); - taskCount++; - System.out.println("____________________________________________________________"); - System.out.println(" added: " + taskDescription); - System.out.println("____________________________________________________________"); - } else { - System.out.println("Task list is full!"); - } + private void addDeadline(String description, String by) { + Task task = new Deadline(description, by); + tasks.add(task); + printTaskAdded(task); + } + + private void addEvent(String description, String from, String to) { + Task task = new Event(description, from, to); + tasks.add(task); + printTaskAdded(task); } - private static void printTaskList() { - System.out.println("____________________________________________________________"); - if (taskCount == 0) { - System.out.println(" No tasks in the list!"); + private void markTask(int index) { + if (index >= 0 && index < tasks.size()) { + tasks.get(index).markAsDone(); + System.out.println("Nice! I've marked this task as done:\n " + tasks.get(index)); } else { - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < taskCount; i++) { - System.out.println(" " + (i + 1) + ". " + tasks[i]); - } + System.out.println("Invalid task number."); } - System.out.println("____________________________________________________________"); } - private static void markTaskAsDone(String command) { - int taskIndex = getTaskIndex(command, "mark "); - if (taskIndex != -1) { - tasks[taskIndex].markAsDone(); - System.out.println("____________________________________________________________"); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" " + tasks[taskIndex]); - System.out.println("____________________________________________________________"); + private void unmarkTask(int index) { + if (index >= 0 && index < tasks.size()) { + tasks.get(index).unmarkAsDone(); + System.out.println("OK, I've marked this task as not done yet:\n " + tasks.get(index)); + } else { + System.out.println("Invalid task number."); } } - private static void unmarkTask(String command) { - int taskIndex = getTaskIndex(command, "unmark "); - if (taskIndex != -1) { - tasks[taskIndex].unmarkAsDone(); - System.out.println("____________________________________________________________"); - System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println(" " + tasks[taskIndex]); - System.out.println("____________________________________________________________"); - } + private void printTaskAdded(Task task) { + System.out.println("Got it! I've added this task:\n " + task); + System.out.println("Now you have " + tasks.size() + " tasks in your list."); } - private static int getTaskIndex(String command, String prefix) { - try { - int index = Integer.parseInt(command.substring(prefix.length())) - 1; - if (index >= 0 && index < taskCount) { - return index; - } else { - System.out.println("Invalid task number!"); - } - } catch (NumberFormatException e) { - System.out.println("Please enter a valid number."); - } - return -1; + public static void main(String[] args) { + new BuddyBot().run(); } } diff --git a/ip/src/main/java/Deadline.java b/ip/src/main/java/Deadline.java new file mode 100644 index 000000000..77aab05df --- /dev/null +++ b/ip/src/main/java/Deadline.java @@ -0,0 +1,15 @@ +public class Deadline extends Task { + private final String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + by + ")"; + } +} + + diff --git a/ip/src/main/java/Event.java b/ip/src/main/java/Event.java new file mode 100644 index 000000000..33680a07c --- /dev/null +++ b/ip/src/main/java/Event.java @@ -0,0 +1,15 @@ +public class Event extends Task { + private final String from; + private final String to; + + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; + } +} diff --git a/ip/src/main/java/Todo.java b/ip/src/main/java/Todo.java new file mode 100644 index 000000000..eabef3ab8 --- /dev/null +++ b/ip/src/main/java/Todo.java @@ -0,0 +1,10 @@ +public class Todo extends Task { + public Todo(String description) { + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} From 58239b4d05555f5c4d49d6160bf2ef85475e5fd9 Mon Sep 17 00:00:00 2001 From: himethcodes Date: Fri, 14 Feb 2025 09:42:07 +0800 Subject: [PATCH 07/35] Add BuddyException class --- ip/src/main/java/BuddyException.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 ip/src/main/java/BuddyException.java diff --git a/ip/src/main/java/BuddyException.java b/ip/src/main/java/BuddyException.java new file mode 100644 index 000000000..fd2a0c707 --- /dev/null +++ b/ip/src/main/java/BuddyException.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class BuddyException { +} From 64e7da9df5942438961ba884c970f04b2a2b00c9 Mon Sep 17 00:00:00 2001 From: himethcodes Date: Fri, 14 Feb 2025 09:54:25 +0800 Subject: [PATCH 08/35] Add BuddyException class --- ip/src/main/java/BuddyBot.java | 129 +++++++++++++++++++-------- ip/src/main/java/BuddyException.java | 7 +- 2 files changed, 94 insertions(+), 42 deletions(-) diff --git a/ip/src/main/java/BuddyBot.java b/ip/src/main/java/BuddyBot.java index a64f4d0b6..eace5d0f0 100644 --- a/ip/src/main/java/BuddyBot.java +++ b/ip/src/main/java/BuddyBot.java @@ -10,30 +10,66 @@ public BuddyBot() { public void run() { Scanner scanner = new Scanner(System.in); - System.out.println("Hello! I'm BuddyBot\nWhat can I do for you?"); + + printLine(); + System.out.println(" Hello! I'm BuddyBot"); + System.out.println(" What can I do for you?"); + printLine(); while (true) { String command = scanner.nextLine(); - if (command.equals("bye")) { - System.out.println("Bye! Hope to see you again soon!"); - break; - } else if (command.equals("list")) { - listTasks(); - } else if (command.startsWith("todo ")) { - addTodo(command.substring(5)); - } else if (command.startsWith("deadline ")) { - String[] parts = command.substring(9).split(" /by "); - addDeadline(parts[0], parts[1]); - } else if (command.startsWith("event ")) { - String[] parts = command.substring(6).split(" /from | /to "); - addEvent(parts[0], parts[1], parts[2]); - } else if (command.startsWith("mark ")) { - markTask(Integer.parseInt(command.substring(5)) - 1); - } else if (command.startsWith("unmark ")) { - unmarkTask(Integer.parseInt(command.substring(7)) - 1); - } else { - System.out.println("Sorry, I don't understand that command."); + try { + if (command.equals("bye")) { + printLine(); + System.out.println("Bye! Hope to see you again soon!"); + printLine(); + break; + } else if (command.equals("list")) { + printLine(); + listTasks(); + printLine(); + } else if (command.startsWith("todo ")) { + printLine(); + addTodo(command.substring(5)); + printLine(); + } else if (command.startsWith("deadline ")) { + printLine(); + String[] parts = command.substring(9).split(" /by "); + if (parts.length < 2) throw new BuddyException("Oops! A deadline must have a description and a '/by' date."); + addDeadline(parts[0], parts[1]); + printLine(); + } else if (command.startsWith("event ")) { + printLine(); + String[] parts = command.substring(6).split(" /from | /to "); + if (parts.length < 3) throw new BuddyException("Oops! An event must have a description, a '/from' time, and a '/to' time."); + addEvent(parts[0], parts[1], parts[2]); + printLine(); + } else if (command.startsWith("mark ")) { + printLine(); + int index = Integer.parseInt(command.substring(5)) - 1; + markTask(index); + printLine(); + } else if (command.startsWith("unmark ")) { + printLine(); + int index = Integer.parseInt(command.substring(7)) - 1; + unmarkTask(index); + printLine(); + } else { + throw new BuddyException("Sorry, I don't understand that command."); + } + } catch (BuddyException e) { + printLine(); + System.out.println("OOPS!!! " + e.getMessage()); + printLine(); + } catch (NumberFormatException e) { + printLine(); + System.out.println("OOPS!!! Please enter a valid task number."); + printLine(); + } catch (IndexOutOfBoundsException e) { + printLine(); + System.out.println("OOPS!!! Task number does not exist."); + printLine(); } } @@ -41,51 +77,66 @@ public void run() { } private void listTasks() { - System.out.println("Here are your tasks:"); - for (int i = 0; i < tasks.size(); i++) { - System.out.println((i + 1) + "." + tasks.get(i)); + if (tasks.isEmpty()) { + System.out.println("Your task list is empty."); + } else { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println(" " + (i + 1) + "." + tasks.get(i)); + } } } - private void addTodo(String description) { + private void addTodo(String description) throws BuddyException { + if (description.isBlank()) { + throw new BuddyException("The description of a todo cannot be empty."); + } Task task = new Todo(description); tasks.add(task); printTaskAdded(task); } - private void addDeadline(String description, String by) { + private void addDeadline(String description, String by) throws BuddyException { + if (description.isBlank()) { + throw new BuddyException("The description of a deadline cannot be empty."); + } Task task = new Deadline(description, by); tasks.add(task); printTaskAdded(task); } - private void addEvent(String description, String from, String to) { + private void addEvent(String description, String from, String to) throws BuddyException { + if (description.isBlank()) { + throw new BuddyException("The description of an event cannot be empty."); + } Task task = new Event(description, from, to); tasks.add(task); printTaskAdded(task); } - private void markTask(int index) { - if (index >= 0 && index < tasks.size()) { - tasks.get(index).markAsDone(); - System.out.println("Nice! I've marked this task as done:\n " + tasks.get(index)); - } else { - System.out.println("Invalid task number."); + private void markTask(int index) throws BuddyException { + if (index < 0 || index >= tasks.size()) { + throw new BuddyException("Invalid task number."); } + tasks.get(index).markAsDone(); + System.out.println("Nice! I've marked this task as done:\n " + tasks.get(index)); } - private void unmarkTask(int index) { - if (index >= 0 && index < tasks.size()) { - tasks.get(index).unmarkAsDone(); - System.out.println("OK, I've marked this task as not done yet:\n " + tasks.get(index)); - } else { - System.out.println("Invalid task number."); + private void unmarkTask(int index) throws BuddyException { + if (index < 0 || index >= tasks.size()) { + throw new BuddyException("Invalid task number."); } + tasks.get(index).unmarkAsDone(); + System.out.println("OK, I've marked this task as not done yet:\n " + tasks.get(index)); } private void printTaskAdded(Task task) { System.out.println("Got it! I've added this task:\n " + task); - System.out.println("Now you have " + tasks.size() + " tasks in your list."); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + } + + private void printLine() { + System.out.println(" ____________________________________________________________"); } public static void main(String[] args) { diff --git a/ip/src/main/java/BuddyException.java b/ip/src/main/java/BuddyException.java index fd2a0c707..828e8df92 100644 --- a/ip/src/main/java/BuddyException.java +++ b/ip/src/main/java/BuddyException.java @@ -1,4 +1,5 @@ -package PACKAGE_NAME; - -public class BuddyException { +public class BuddyException extends Exception { + public BuddyException(String message) { + super(message); + } } From ee484f51097bc95ed922796f89f534febeb4d04b Mon Sep 17 00:00:00 2001 From: himethcodes Date: Mon, 24 Feb 2025 16:39:27 +0800 Subject: [PATCH 09/35] implement error handling with BuddyException (Level-5) --- .gitignore | 17 ----------------- CONTRIBUTORS.md | 9 --------- README.md | 26 -------------------------- docs/README.md | 30 ------------------------------ src/main/java/Duke.java | 10 ---------- text-ui-test/EXPECTED.TXT | 7 ------- text-ui-test/input.txt | 0 text-ui-test/runtest.bat | 21 --------------------- text-ui-test/runtest.sh | 38 -------------------------------------- tutorial-05 | 1 + 10 files changed, 1 insertion(+), 158 deletions(-) delete mode 100644 .gitignore delete mode 100644 CONTRIBUTORS.md delete mode 100644 README.md delete mode 100644 docs/README.md delete mode 100644 src/main/java/Duke.java delete mode 100644 text-ui-test/EXPECTED.TXT delete mode 100644 text-ui-test/input.txt delete mode 100644 text-ui-test/runtest.bat delete mode 100644 text-ui-test/runtest.sh create mode 160000 tutorial-05 diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2873e189e..000000000 --- a/.gitignore +++ /dev/null @@ -1,17 +0,0 @@ -# IDEA files -/.idea/ -/out/ -/*.iml - -# Gradle build files -/.gradle/ -/build/ -src/main/resources/docs/ - -# MacOS custom attributes files created by Finder -.DS_Store -*.iml -bin/ - -/text-ui-test/ACTUAL.TXT -text-ui-test/EXPECTED-UNIX.TXT diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md deleted file mode 100644 index 8e359a014..000000000 --- a/CONTRIBUTORS.md +++ /dev/null @@ -1,9 +0,0 @@ -# Contributors - -Display | Name | Github Profile | Homepage ----|:---:|:---:|:---: -![](https://avatars0.githubusercontent.com/u/22460123?s=100) | Jeffry Lum | [Github](https://github.com/j-lum/) | [Homepage](https://se.kasugano.moe) -![](https://avatars0.githubusercontent.com/u/1673303?s=100) | Damith C. Rajapakse | [Github](https://github.com/damithc/) | [Homepage](https://www.comp.nus.edu.sg/~damithch/) -# I would like to join this list. How can I help the project - -For more information, please refer to our [contributor's guide](https://oss-generic.github.io/process/). diff --git a/README.md b/README.md deleted file mode 100644 index af0309a9e..000000000 --- a/README.md +++ /dev/null @@ -1,26 +0,0 @@ -# Duke project template - -This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. - -## Setting up in Intellij - -Prerequisites: JDK 17, update Intellij to the most recent version. - -1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) -1. Open the project into Intellij as follows: - 1. Click `Open`. - 1. Select the project directory, and click `OK`. - 1. If there are any further prompts, accept the defaults. -1. Configure the project to use **JDK 17** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
- In the same dialog, set the **Project language level** field to the `SDK default` option. -1. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: - ``` - Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| - ``` - -**Warning:** Keep the `src\main\java` folder as the root folder for Java files (i.e., don't rename those folders or move Java files to another folder outside of this folder path), as this is the default location some tools (e.g., Gradle) expect to find Java files. diff --git a/docs/README.md b/docs/README.md deleted file mode 100644 index 47b9f984f..000000000 --- a/docs/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# Duke User Guide - -// Update the title above to match the actual product name - -// Product screenshot goes here - -// Product intro goes here - -## Adding deadlines - -// Describe the action and its outcome. - -// Give examples of usage - -Example: `keyword (optional arguments)` - -// A description of the expected outcome goes here - -``` -expected output -``` - -## Feature ABC - -// Feature details - - -## Feature XYZ - -// Feature details \ 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); - } -} diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT deleted file mode 100644 index 657e74f6e..000000000 --- a/text-ui-test/EXPECTED.TXT +++ /dev/null @@ -1,7 +0,0 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| - diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt deleted file mode 100644 index e69de29bb..000000000 diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat deleted file mode 100644 index 087374464..000000000 --- a/text-ui-test/runtest.bat +++ /dev/null @@ -1,21 +0,0 @@ -@ECHO OFF - -REM create bin directory if it doesn't exist -if not exist ..\bin mkdir ..\bin - -REM delete output from previous run -if exist ACTUAL.TXT del ACTUAL.TXT - -REM compile the code into the bin folder -javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java -IF ERRORLEVEL 1 ( - echo ********** BUILD FAILURE ********** - exit /b 1 -) -REM no error here, errorlevel == 0 - -REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Duke < input.txt > ACTUAL.TXT - -REM compare the output to the expected output -FC ACTUAL.TXT EXPECTED.TXT diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh deleted file mode 100644 index c9ec87003..000000000 --- a/text-ui-test/runtest.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -# create bin directory if it doesn't exist -if [ ! -d "../bin" ] -then - mkdir ../bin -fi - -# delete output from previous run -if [ -e "./ACTUAL.TXT" ] -then - rm ACTUAL.TXT -fi - -# compile the code into the bin folder, terminates if error occurred -if ! javac -cp ../src/main/java -Xlint:none -d ../bin ../src/main/java/*.java -then - echo "********** BUILD FAILURE **********" - exit 1 -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 - -# compare the output to the expected output -diff ACTUAL.TXT EXPECTED-UNIX.TXT -if [ $? -eq 0 ] -then - echo "Test result: PASSED" - exit 0 -else - echo "Test result: FAILED" - exit 1 -fi \ No newline at end of file diff --git a/tutorial-05 b/tutorial-05 new file mode 160000 index 000000000..2c7df1733 --- /dev/null +++ b/tutorial-05 @@ -0,0 +1 @@ +Subproject commit 2c7df17336080c3d2360c02fd9554768c1b71ed8 From d724241fd5159f2c19a24798398f9fc7e25e512a Mon Sep 17 00:00:00 2001 From: himethcodes Date: Tue, 25 Feb 2025 00:21:23 +0800 Subject: [PATCH 10/35] Implement Level-6: Task Deletion Feature --- ip/src/main/java/BuddyBot.java | 60 +++++++++++++++++++++++++++++----- ip/src/main/java/Task.java | 33 +++++++++++++++++++ 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/ip/src/main/java/BuddyBot.java b/ip/src/main/java/BuddyBot.java index eace5d0f0..72209c3c5 100644 --- a/ip/src/main/java/BuddyBot.java +++ b/ip/src/main/java/BuddyBot.java @@ -1,11 +1,14 @@ +import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class BuddyBot { private final ArrayList tasks; + private static final String FILE_PATH = "data/duke.txt"; public BuddyBot() { this.tasks = new ArrayList<>(); + loadTasksFromFile(); } public void run() { @@ -55,6 +58,11 @@ public void run() { int index = Integer.parseInt(command.substring(7)) - 1; unmarkTask(index); printLine(); + } else if (command.startsWith("delete ")) { + printLine(); + int index = Integer.parseInt(command.substring(7)) - 1; + deleteTask(index); + printLine(); } else { throw new BuddyException("Sorry, I don't understand that command."); } @@ -93,6 +101,7 @@ private void addTodo(String description) throws BuddyException { } Task task = new Todo(description); tasks.add(task); + saveTasksToFile(); printTaskAdded(task); } @@ -102,6 +111,7 @@ private void addDeadline(String description, String by) throws BuddyException { } Task task = new Deadline(description, by); tasks.add(task); + saveTasksToFile(); printTaskAdded(task); } @@ -111,25 +121,59 @@ private void addEvent(String description, String from, String to) throws BuddyEx } Task task = new Event(description, from, to); tasks.add(task); + saveTasksToFile(); printTaskAdded(task); } - private void markTask(int index) throws BuddyException { - if (index < 0 || index >= tasks.size()) { - throw new BuddyException("Invalid task number."); - } + private void markTask(int index) { tasks.get(index).markAsDone(); + saveTasksToFile(); System.out.println("Nice! I've marked this task as done:\n " + tasks.get(index)); } - private void unmarkTask(int index) throws BuddyException { - if (index < 0 || index >= tasks.size()) { - throw new BuddyException("Invalid task number."); - } + private void unmarkTask(int index) { tasks.get(index).unmarkAsDone(); + saveTasksToFile(); System.out.println("OK, I've marked this task as not done yet:\n " + tasks.get(index)); } + private void deleteTask(int index) { + Task removedTask = tasks.remove(index); + saveTasksToFile(); + System.out.println("Noted. I've removed this task:\n " + removedTask); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + } + + private void saveTasksToFile() { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) { + for (Task task : tasks) { + writer.write(task.toSaveFormat()); + writer.newLine(); + } + } catch (IOException e) { + System.out.println("Error saving tasks to file."); + } + } + + private void loadTasksFromFile() { + File file = new File(FILE_PATH); + if (!file.exists()) return; + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + try { + tasks.add(Task.parse(line)); + } catch (BuddyException e) { + System.out.println("Skipping invalid task entry: " + line); + } + } + } catch (IOException e) { + System.out.println("Error loading tasks from file."); + } + } + + private void printTaskAdded(Task task) { System.out.println("Got it! I've added this task:\n " + task); System.out.println("Now you have " + tasks.size() + " tasks in the list."); diff --git a/ip/src/main/java/Task.java b/ip/src/main/java/Task.java index c46d22617..4ce85cc2c 100644 --- a/ip/src/main/java/Task.java +++ b/ip/src/main/java/Task.java @@ -23,4 +23,37 @@ public String getStatusIcon() { public String toString() { return getStatusIcon() + " " + description; } + + public String toSaveFormat() { + return (isDone ? "1" : "0") + " | " + description; + } + + public static Task parse(String line) throws BuddyException { + String[] parts = line.split(" \\| "); + if (parts.length < 2) throw new BuddyException("Invalid task format in file."); + + boolean isDone = parts[0].equals("1"); + String description = parts[1]; + + Task task; + if (line.startsWith("T")) { + task = new Todo(description); + } else if (line.startsWith("D")) { + if (parts.length < 3) throw new BuddyException("Invalid deadline format."); + task = new Deadline(description, parts[2]); + } else if (line.startsWith("E")) { + if (parts.length < 4) throw new BuddyException("Invalid event format."); + task = new Event(description, parts[2], parts[3]); + } else { + throw new BuddyException("Unknown task type in file."); + } + + if (isDone) { + task.markAsDone(); + } + + return task; + } + } + From db6f55b0f8271932e548bf8529f04331afcadded Mon Sep 17 00:00:00 2001 From: himethcodes Date: Sat, 1 Mar 2025 13:19:30 +0800 Subject: [PATCH 11/35] Resolve merge conflicts --- ip/src/main/java/BuddyBot.java | 59 +++++++++++++++++++++++++++------- ip/src/main/java/Task.java | 14 +++----- ip/src/main/java/Todo.java | 4 +-- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/ip/src/main/java/BuddyBot.java b/ip/src/main/java/BuddyBot.java index a64f4d0b6..44f7b7cc0 100644 --- a/ip/src/main/java/BuddyBot.java +++ b/ip/src/main/java/BuddyBot.java @@ -1,11 +1,14 @@ +import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class BuddyBot { private final ArrayList tasks; + private static final String FILE_PATH = "./data/duke.txt"; public BuddyBot() { this.tasks = new ArrayList<>(); + loadTasks(); // Load tasks when the bot starts } public void run() { @@ -32,6 +35,8 @@ public void run() { markTask(Integer.parseInt(command.substring(5)) - 1); } else if (command.startsWith("unmark ")) { unmarkTask(Integer.parseInt(command.substring(7)) - 1); + } else if (command.startsWith("delete ")) { + deleteTask(Integer.parseInt(command.substring(7)) - 1); } else { System.out.println("Sorry, I don't understand that command."); } @@ -51,33 +56,29 @@ private void addTodo(String description) { Task task = new Todo(description); tasks.add(task); printTaskAdded(task); + saveTasks(); } private void addDeadline(String description, String by) { Task task = new Deadline(description, by); tasks.add(task); printTaskAdded(task); + saveTasks(); } private void addEvent(String description, String from, String to) { Task task = new Event(description, from, to); tasks.add(task); printTaskAdded(task); + saveTasks(); } - private void markTask(int index) { + private void deleteTask(int index) { if (index >= 0 && index < tasks.size()) { - tasks.get(index).markAsDone(); - System.out.println("Nice! I've marked this task as done:\n " + tasks.get(index)); - } else { - System.out.println("Invalid task number."); - } - } - - private void unmarkTask(int index) { - if (index >= 0 && index < tasks.size()) { - tasks.get(index).unmarkAsDone(); - System.out.println("OK, I've marked this task as not done yet:\n " + tasks.get(index)); + Task removedTask = tasks.remove(index); + System.out.println("Noted. I've removed this task:\n " + removedTask); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + saveTasks(); } else { System.out.println("Invalid task number."); } @@ -88,6 +89,40 @@ private void printTaskAdded(Task task) { System.out.println("Now you have " + tasks.size() + " tasks in your list."); } + private void saveTasks() { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) { + for (Task task : tasks) { + writer.write(task.toSaveFormat() + "\n"); + } + } catch (IOException e) { + System.out.println("Error saving tasks."); + } + } + + private void loadTasks() { + File file = new File(FILE_PATH); + if (!file.exists()) return; + + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + String[] parts = scanner.nextLine().split(" \\| "); + switch (parts[0]) { + case "T": + tasks.add(new Todo(parts[2])); + break; + case "D": + tasks.add(new Deadline(parts[2], parts[3])); + break; + case "E": + tasks.add(new Event(parts[2], parts[3], parts[4])); + break; + } + } + } catch (FileNotFoundException e) { + System.out.println("Error loading tasks."); + } + } + public static void main(String[] args) { new BuddyBot().run(); } diff --git a/ip/src/main/java/Task.java b/ip/src/main/java/Task.java index c46d22617..725b06320 100644 --- a/ip/src/main/java/Task.java +++ b/ip/src/main/java/Task.java @@ -1,4 +1,4 @@ -public class Task { +public abstract class Task { protected String description; protected boolean isDone; @@ -7,18 +7,12 @@ public Task(String description) { this.isDone = false; } - public void markAsDone() { - isDone = true; - } - - public void unmarkAsDone() { - isDone = false; - } - public String getStatusIcon() { - return (isDone ? "[X]" : "[ ]"); // X for done, space for not done + return (isDone ? "[X]" : "[ ]"); } + public abstract String toSaveFormat(); + @Override public String toString() { return getStatusIcon() + " " + description; diff --git a/ip/src/main/java/Todo.java b/ip/src/main/java/Todo.java index eabef3ab8..18a28e88a 100644 --- a/ip/src/main/java/Todo.java +++ b/ip/src/main/java/Todo.java @@ -4,7 +4,7 @@ public Todo(String description) { } @Override - public String toString() { - return "[T]" + super.toString(); + public String toSaveFormat() { + return "T | " + (isDone ? "1" : "0") + " | " + description; } } From 1dcf58604c46cf72c7ea19d31ebec13d5c52620f Mon Sep 17 00:00:00 2001 From: himethcodes Date: Sat, 1 Mar 2025 13:31:03 +0800 Subject: [PATCH 12/35] Implemented Level-6: Delete task feature --- ip/src/main/java/BuddyBot.java | 84 +++++++++++++++------------------- ip/src/main/java/Deadline.java | 2 - ip/src/main/java/Task.java | 10 +++- ip/src/main/java/Todo.java | 4 +- 4 files changed, 46 insertions(+), 54 deletions(-) diff --git a/ip/src/main/java/BuddyBot.java b/ip/src/main/java/BuddyBot.java index 44f7b7cc0..71a06784b 100644 --- a/ip/src/main/java/BuddyBot.java +++ b/ip/src/main/java/BuddyBot.java @@ -1,14 +1,11 @@ -import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class BuddyBot { private final ArrayList tasks; - private static final String FILE_PATH = "./data/duke.txt"; public BuddyBot() { this.tasks = new ArrayList<>(); - loadTasks(); // Load tasks when the bot starts } public void run() { @@ -27,10 +24,18 @@ public void run() { addTodo(command.substring(5)); } else if (command.startsWith("deadline ")) { String[] parts = command.substring(9).split(" /by "); - addDeadline(parts[0], parts[1]); + if (parts.length == 2) { + addDeadline(parts[0], parts[1]); + } else { + System.out.println("Invalid deadline format. Use: deadline /by