From 9b2170b860515150d59cd7b2dbec73d57f8427a4 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Thu, 10 Oct 2024 10:19:58 +0800 Subject: [PATCH 01/26] Level-0 --- src/main/java/Duke.java | 10 ---------- src/main/java/KenChat.java | 13 +++++++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) delete mode 100644 src/main/java/Duke.java create mode 100644 src/main/java/KenChat.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334c..000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java new file mode 100644 index 000000000..6f2ef3504 --- /dev/null +++ b/src/main/java/KenChat.java @@ -0,0 +1,13 @@ +public class KenChat { + + public static void main(String[] args) { + String chatBotName = "KenChat"; + String line = "____________________________________" ; + System.out.println(line); + System.out.println("Hello! I'm " + chatBotName); + System.out.println("What can I do for you?"); + System.out.println(line); + System.out.println("Bye. Hope to see you again soon!"); + System.out.println(line); + } +} \ No newline at end of file From 2fdeff946645d6bbec1e1a7b8e60a023158acc26 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 12:48:41 +0800 Subject: [PATCH 02/26] Level-0 --- src/main/java/KenChat.java | 39 +++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 6f2ef3504..6effb3199 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,13 +1,42 @@ -public class KenChat { +import java.util.*; - public static void main(String[] args) { - String chatBotName = "KenChat"; +public class KenChat { + public static void printline() { String line = "____________________________________" ; System.out.println(line); + } + + public static void startPrograme() { + String chatBotName = "KenChat"; + printline(); System.out.println("Hello! I'm " + chatBotName); System.out.println("What can I do for you?"); - System.out.println(line); + printline(); + } + + public static void endPrograme() { + printline(); System.out.println("Bye. Hope to see you again soon!"); - System.out.println(line); + printline(); + } + + public static void displayOutput(String display){ + printline(); + System.out.println(display); + printline(); + } + + public static void main(String[] args) { + Scanner sc= new Scanner(System.in); + boolean running = true; + startPrograme(); + while (running){ + System.out.println(); + String str= sc.nextLine(); + if (str.equalsIgnoreCase("bye") ) + running = false; + displayOutput(str); + } + endPrograme(); } } \ No newline at end of file From 6aa72b343630e0e404f6ae80ca35c1f00b0e3c0d Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 12:50:13 +0800 Subject: [PATCH 03/26] Level-0 --- src/main/java/KenChat.java | 39 +++++--------------------------------- 1 file changed, 5 insertions(+), 34 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 6effb3199..6f2ef3504 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,42 +1,13 @@ -import java.util.*; - public class KenChat { - public static void printline() { - String line = "____________________________________" ; - System.out.println(line); - } - public static void startPrograme() { + public static void main(String[] args) { String chatBotName = "KenChat"; - printline(); + String line = "____________________________________" ; + System.out.println(line); System.out.println("Hello! I'm " + chatBotName); System.out.println("What can I do for you?"); - printline(); - } - - public static void endPrograme() { - printline(); + System.out.println(line); System.out.println("Bye. Hope to see you again soon!"); - printline(); - } - - public static void displayOutput(String display){ - printline(); - System.out.println(display); - printline(); - } - - public static void main(String[] args) { - Scanner sc= new Scanner(System.in); - boolean running = true; - startPrograme(); - while (running){ - System.out.println(); - String str= sc.nextLine(); - if (str.equalsIgnoreCase("bye") ) - running = false; - displayOutput(str); - } - endPrograme(); + System.out.println(line); } } \ No newline at end of file From 1372eaacf45ae3a602b6945f2bbefff729892dcd Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 12:50:35 +0800 Subject: [PATCH 04/26] Level-1 --- src/main/java/KenChat.java | 42 ++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 6f2ef3504..a40f40dce 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,13 +1,43 @@ -public class KenChat { +import java.util.*; - public static void main(String[] args) { - String chatBotName = "KenChat"; +public class KenChat { + public static void printline() { String line = "____________________________________" ; System.out.println(line); + } + + public static void startPrograme() { + String chatBotName = "KenChat"; + printline(); System.out.println("Hello! I'm " + chatBotName); System.out.println("What can I do for you?"); - System.out.println(line); + printline(); + } + + public static void endPrograme() { + printline(); System.out.println("Bye. Hope to see you again soon!"); - System.out.println(line); + printline(); + } + + public static void displayOutput(String display){ + printline(); + System.out.println(display); + printline(); } -} \ No newline at end of file + + public static void main(String[] args) { + Scanner sc= new Scanner(System.in); + boolean running = true; + startPrograme(); + while (running){ + System.out.println(); + String str= sc.nextLine(); + if (str.equalsIgnoreCase("bye") ) + running = false; + displayOutput(str); + } + endPrograme(); + } +} + From bf43ec2435ee5681132a1da1620614d2551f3d2f Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 13:38:58 +0800 Subject: [PATCH 05/26] Level-1 --- src/main/java/KenChat.java | 43 -------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 src/main/java/KenChat.java diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java deleted file mode 100644 index a40f40dce..000000000 --- a/src/main/java/KenChat.java +++ /dev/null @@ -1,43 +0,0 @@ -import java.util.*; - -public class KenChat { - public static void printline() { - String line = "____________________________________" ; - System.out.println(line); - } - - public static void startPrograme() { - String chatBotName = "KenChat"; - printline(); - System.out.println("Hello! I'm " + chatBotName); - System.out.println("What can I do for you?"); - printline(); - } - - public static void endPrograme() { - printline(); - System.out.println("Bye. Hope to see you again soon!"); - printline(); - } - - public static void displayOutput(String display){ - printline(); - System.out.println(display); - printline(); - } - - public static void main(String[] args) { - Scanner sc= new Scanner(System.in); - boolean running = true; - startPrograme(); - while (running){ - System.out.println(); - String str= sc.nextLine(); - if (str.equalsIgnoreCase("bye") ) - running = false; - displayOutput(str); - } - endPrograme(); - } -} - From 3d84e1c2c22969ea10a32aca09763e77bfa0f62e Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 13:52:19 +0800 Subject: [PATCH 06/26] Level-2 --- src/main/java/KenChat.java | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/main/java/KenChat.java diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java new file mode 100644 index 000000000..de8d32d67 --- /dev/null +++ b/src/main/java/KenChat.java @@ -0,0 +1,64 @@ +import java.util.*; + +public class KenChat { + public static void printLine() { + String line = "____________________________________" ; + System.out.println(line); + } + + public static void startProgramme() { + String chatBotName = "KenChat"; + printLine(); + System.out.println("Hello! I'm " + chatBotName); + System.out.println("What can I do for you?"); + printLine(); + } + + public static void endProgramme() { + printLine(); + System.out.println("Bye. Hope to see you again soon!"); + printLine(); + } + + public static void displayList(String[] doList){ + printLine(); + for (int i=0; i< doList.length; i++){ + if(doList[i] != null){ + System.out.println((i+1)+". "+doList[i]); + } + } + printLine(); + } + + public static void addList(String[] doList, String item){ + printLine(); + System.out.println("added: "+item); + printLine(); + for (int i=0; i< doList.length; i++){ + if(doList[i] == null){ + doList[i] = item; + break; + } + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + boolean running = true; + int arraySize = 100; + String[] doList = new String[arraySize]; + startProgramme(); + while (running) { + System.out.println(); + String str = sc.nextLine(); + if (str.equalsIgnoreCase("bye")) + running = false; + else if (str.equalsIgnoreCase("list")) + displayList(doList); + else + addList(doList, str); + } + endProgramme(); + } +} + From 02fccb16e496cd22bb106db1979ab815b8ed4fda Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 13:52:19 +0800 Subject: [PATCH 07/26] Level-0 --- src/main/java/KenChat.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/java/KenChat.java diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java new file mode 100644 index 000000000..4024a080a --- /dev/null +++ b/src/main/java/KenChat.java @@ -0,0 +1,13 @@ +public class KenChat { + + public static void main(String[] args) { + String chatBotName = "KenChat"; + String line = "____________________________________" ; + System.out.println(line); + System.out.println("Hello! I'm " + chatBotName); + System.out.println("What can I do for you?"); + System.out.println(line); + System.out.println("Bye. Hope to see you again soon!"); + System.out.println(line); + } +} From da7a3730a84558ede9f623c02bb503da8641bf38 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 14:03:17 +0800 Subject: [PATCH 08/26] Level-1 --- src/main/java/KenChat.java | 40 ++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 0f2e70356..95c3003b9 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,13 +1,41 @@ -public class KenChat { +import java.util.Scanner; - public static void main(String[] args) { - String chatBotName = "KenChat"; +public class KenChat { + public static void printLine() { String line = "____________________________________" ; System.out.println(line); + } + + public static void startProgramme() { + String chatBotName = "KenChat"; + printLine(); System.out.println("Hello! I'm " + chatBotName); - System.out.println("What can I do for u?"); - System.out.println(line); + System.out.println("What can I do for you?"); + printLine(); + } + + public static void endProgramme() { + printLine(); System.out.println("Bye. Hope to see you again soon!"); - System.out.println(line); + printLine(); + } + + public static void displayOutput(String display){ + printLine(); + System.out.println(display); + printLine(); + } + public static void main(String[] args) { + Scanner sc= new Scanner(System.in); + boolean running = true; + startProgramme(); + while (running){ + System.out.println(); + String str= sc.nextLine(); + if (str.equalsIgnoreCase("bye") ) + running = false; + displayOutput(str); + } + endProgramme(); } } From 98d29ab5b04f21628eb15b371f3e648cf9879cbd Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 14:09:37 +0800 Subject: [PATCH 09/26] Level-2 --- src/main/java/KenChat.java | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 95c3003b9..d4e82ebfd 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -20,21 +20,43 @@ public static void endProgramme() { printLine(); } - public static void displayOutput(String display){ + public static void displayList(String[] doList){ printLine(); - System.out.println(display); + for (int i=0; i< doList.length; i++){ + if(doList[i] != null){ + System.out.println((i+1)+". "+doList[i]); + } + } printLine(); } + + public static void addList(String[] doList, String item){ + printLine(); + System.out.println("added: "+item); + printLine(); + for (int i=0; i< doList.length; i++){ + if(doList[i] == null){ + doList[i] = item; + break; + } + } + } + public static void main(String[] args) { Scanner sc= new Scanner(System.in); boolean running = true; + int arraySize = 100; + String[] doList = new String[arraySize]; startProgramme(); while (running){ System.out.println(); String str= sc.nextLine(); - if (str.equalsIgnoreCase("bye") ) + if (str.equalsIgnoreCase("bye")) running = false; - displayOutput(str); + else if(str.equalsIgnoreCase("list")) + displayList(doList); + else + addList(doList, str); } endProgramme(); } From 22bc111c54ff2084ed13a417dbc35958122e3ee8 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 16:13:48 +0800 Subject: [PATCH 10/26] Level-3 --- src/main/java/KenChat.java | 39 ++++++++++++++++++++++++++++++++------ src/main/java/Task.java | 30 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index d4e82ebfd..82de002aa 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,3 +1,5 @@ +package main.java; + import java.util.Scanner; public class KenChat { @@ -20,7 +22,7 @@ public static void endProgramme() { printLine(); } - public static void displayList(String[] doList){ + public static void displayList(Task[] doList){ printLine(); for (int i=0; i< doList.length; i++){ if(doList[i] != null){ @@ -30,31 +32,56 @@ public static void displayList(String[] doList){ printLine(); } - public static void addList(String[] doList, String item){ + public static void addList(Task[] doList, String item){ printLine(); System.out.println("added: "+item); printLine(); + for (int i=0; i< doList.length; i++){ if(doList[i] == null){ - doList[i] = item; + Task doItem = new Task(item); + doList[i] = doItem; break; } } } + public static void setTaskStatus(boolean isMark, Task item){ + printLine(); + if(isMark){ + item.markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + } + else { + item.markAsUndone(); + System.out.println("OK, I've marked this task as not done yet:"); + } + System.out.println(" "+item); + printLine(); + } + public static void main(String[] args) { Scanner sc= new Scanner(System.in); boolean running = true; int arraySize = 100; - String[] doList = new String[arraySize]; + Task[] doList = new Task[arraySize]; startProgramme(); while (running){ System.out.println(); String str= sc.nextLine(); - if (str.equalsIgnoreCase("bye")) + String[] command = str.split(" "); + if (command[0].equalsIgnoreCase("bye")) running = false; - else if(str.equalsIgnoreCase("list")) + else if(command[0].equalsIgnoreCase("list")) displayList(doList); + else if(command[0].equalsIgnoreCase("mark")){ + int itemNumber = Integer.parseInt(command[1]) - 1; + setTaskStatus(true, doList[itemNumber]); + } + else if(command[0].equalsIgnoreCase("unmark")){ + int itemNumber = Integer.parseInt(command[1]) - 1; + setTaskStatus(false, doList[itemNumber]); + } else addList(doList, str); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..fa86f6adc --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,30 @@ +package main.java; + +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 getName() { + return description; + } + + public void markAsDone(){ + this.isDone = true; + } + + public void markAsUndone(){ + this.isDone = false; + } + + public String toString() { return "["+ getStatusIcon() +"] "+getName(); } + +} \ No newline at end of file From 21a6c09a55165b936f9e76502763a256a9b09524 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 19:43:05 +0800 Subject: [PATCH 11/26] Level-4 --- src/main/java/KenChat.java | 47 +++++++++++++++++++++++++++++++++----- src/main/java/Task.java | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 82de002aa..77ad33460 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -32,15 +32,50 @@ public static void displayList(Task[] doList){ printLine(); } - public static void addList(Task[] doList, String item){ - printLine(); - System.out.println("added: "+item); + public static void addList(Task[] doList, String item) { printLine(); + String[] parts = item.split(" ", 2); // Split on first space to get command + String command = parts[0]; + String description = parts.length > 1 ? parts[1] : ""; - for (int i=0; i< doList.length; i++){ - if(doList[i] == null){ - Task doItem = new Task(item); + if (command.equalsIgnoreCase("todo")) { + Task doItem = new Task.ToDo(description); + addTask(doList, doItem); + } else if (command.equalsIgnoreCase("deadline")) { + String[] deadlineParts = description.split(" /by ", 2); + if (deadlineParts.length == 2) { + Task doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); + addTask(doList, doItem); + } else { + System.out.println("Invalid deadline format. Use: deadline /by "); + } + } else if (command.equalsIgnoreCase("event")) { + String[] eventParts = description.split(" /from | /to ", 3); + if (eventParts.length == 3) { + Task doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); + addTask(doList, doItem); + } else { + System.out.println("Invalid event format. Use: event /from /to "); + } + } else { + Task doItem = new Task.ToDo(item); // Default to ToDo if no specific type + addTask(doList, doItem); + } + } + + private static void addTask(Task[] doList, Task doItem) { + for (int i = 0; i < doList.length; i++) { + if (doList[i] == null) { doList[i] = doItem; + int taskCount = 0; + for (Task task : doList) { + if (task != null) { + taskCount++; + } + } + System.out.println("Got it. I've added this task:"); + System.out.println(" " + doItem); + System.out.println("Now you have " + taskCount + " tasks in the list."); break; } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index fa86f6adc..6b729a121 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -27,4 +27,45 @@ public void markAsUndone(){ public String toString() { return "["+ getStatusIcon() +"] "+getName(); } + // ToDo class + public static class ToDo extends Task { + public ToDo(String description) { + super(description); + } + + public String toString() { + return "[T]" + super.toString(); + } + } + + // Deadline class + public static class Deadline extends Task { + public String dueDate; + + public Deadline(String description, String dueDate) { + super(description); + this.dueDate = dueDate; + } + + public String toString() { + return "[D]" + super.toString() + " (by: " + dueDate + ")"; + } + } + + // Event class + static class Event extends Task { + public String startDate; + public String endDate; + + public Event(String description, String startDate, String endDate) { + super(description); + this.startDate = startDate; + this.endDate = endDate; + } + + public String toString() { + return "[E]" + super.toString() + " (from: " + startDate + " to: " + endDate + ")"; + } + } + } \ No newline at end of file From 4ddff06f20285cdff7147fe6830c7a6c14651f2a Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 19:53:51 +0800 Subject: [PATCH 12/26] Level-4 --- src/main/java/Task.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 6b729a121..3de69ddd7 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -53,7 +53,7 @@ public String toString() { } // Event class - static class Event extends Task { + public static class Event extends Task { public String startDate; public String endDate; From 4686943743f233018729053437ab60bfcf28a7cb Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 23:10:09 +0800 Subject: [PATCH 13/26] Level-4 --- src/main/java/KenChat.java | 131 ++++++++++++++++++++-------- src/main/java/KenChatException.java | 42 +++++++++ 2 files changed, 139 insertions(+), 34 deletions(-) create mode 100644 src/main/java/KenChatException.java diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 77ad33460..1e2bcb5e5 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -4,7 +4,7 @@ public class KenChat { public static void printLine() { - String line = "____________________________________" ; + String line = "____________________________________"; System.out.println(line); } @@ -22,22 +22,32 @@ public static void endProgramme() { printLine(); } - public static void displayList(Task[] doList){ + public static void displayList(Task[] doList) throws KenChatException { printLine(); - for (int i=0; i< doList.length; i++){ - if(doList[i] != null){ - System.out.println((i+1)+". "+doList[i]); + boolean hasTasks = false; + for (int i = 0; i < doList.length; i++) { + if (doList[i] != null) { + System.out.println((i + 1) + ". " + doList[i]); + hasTasks = true; } } + if (!hasTasks) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } printLine(); } - public static void addList(Task[] doList, String item) { + public static void addList(Task[] doList, String item) throws KenChatException { printLine(); String[] parts = item.split(" ", 2); // Split on first space to get command String command = parts[0]; String description = parts.length > 1 ? parts[1] : ""; + + if (description.isEmpty() && (command.equals("todo") || command.equals("deadline") || command.equals("event"))) { + throw new KenChatException(KenChatException.getEmptyDescriptionMessage(command)); + } + if (command.equalsIgnoreCase("todo")) { Task doItem = new Task.ToDo(description); addTask(doList, doItem); @@ -47,7 +57,7 @@ public static void addList(Task[] doList, String item) { Task doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); addTask(doList, doItem); } else { - System.out.println("Invalid deadline format. Use: deadline /by "); + throw new KenChatException(KenChatException.getInvalidDeadlineFormatMessage()); } } else if (command.equalsIgnoreCase("event")) { String[] eventParts = description.split(" /from | /to ", 3); @@ -55,11 +65,10 @@ public static void addList(Task[] doList, String item) { Task doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); addTask(doList, doItem); } else { - System.out.println("Invalid event format. Use: event /from /to "); + throw new KenChatException(KenChatException.getInvalidEventFormatMessage()); } } else { - Task doItem = new Task.ToDo(item); // Default to ToDo if no specific type - addTask(doList, doItem); + throw new KenChatException(KenChatException.getUnknownCommandMessage()); } } @@ -81,45 +90,99 @@ private static void addTask(Task[] doList, Task doItem) { } } - public static void setTaskStatus(boolean isMark, Task item){ + public static void setTaskStatus(boolean isMark, Task item) throws KenChatException { printLine(); - if(isMark){ + if (item == null) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + if (isMark) { item.markAsDone(); System.out.println("Nice! I've marked this task as done:"); - } - else { + } else { item.markAsUndone(); System.out.println("OK, I've marked this task as not done yet:"); } - System.out.println(" "+item); + System.out.println(" " + item); + printLine(); + } + + public static void showHelp() { + printLine(); + System.out.println("Valid commands:"); + System.out.println("1. todo - Adds a todo task."); + System.out.println("2. deadline /by - Adds a deadline task. can be date and/or time."); + System.out.println("3. event /from /to - Adds an event task. can be date and/or time."); + System.out.println("4. list - Displays all tasks in the list."); + System.out.println("5. mark - Marks the specified task as done."); + System.out.println("6. unmark - Marks the specified task as not done."); + System.out.println("7. bye - Exits the program."); + System.out.println("8. help - Shows this help message."); printLine(); } public static void main(String[] args) { - Scanner sc= new Scanner(System.in); + Scanner sc = new Scanner(System.in); boolean running = true; int arraySize = 100; Task[] doList = new Task[arraySize]; startProgramme(); - while (running){ - System.out.println(); - String str= sc.nextLine(); - String[] command = str.split(" "); - if (command[0].equalsIgnoreCase("bye")) - running = false; - else if(command[0].equalsIgnoreCase("list")) - displayList(doList); - else if(command[0].equalsIgnoreCase("mark")){ - int itemNumber = Integer.parseInt(command[1]) - 1; - setTaskStatus(true, doList[itemNumber]); - } - else if(command[0].equalsIgnoreCase("unmark")){ - int itemNumber = Integer.parseInt(command[1]) - 1; - setTaskStatus(false, doList[itemNumber]); + + while (running) { + try { + System.out.println(); + String str = sc.nextLine(); + String[] command = str.split(" "); + + if (str.trim().isEmpty()) { + throw new KenChatException(KenChatException.emptyCommand()); + } + + if (command[0].equalsIgnoreCase("bye")) { + running = false; + } else if (command[0].equalsIgnoreCase("help")) { + showHelp(); + }else if (command[0].equalsIgnoreCase("list")) { + displayList(doList); + } else if (command[0].equalsIgnoreCase("mark")) { + if (doList[0] == null) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } else if (command.length < 2) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); + } + try { + int itemNumber = Integer.parseInt(command[1]) - 1; + if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } else { + setTaskStatus(true, doList[itemNumber]); + } + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + } else if (command[0].equalsIgnoreCase("unmark")) { + if (doList[0] == null) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } if (command.length < 2) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("unmark")); + } + try { + int itemNumber = Integer.parseInt(command[1]) - 1; + if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } else { + setTaskStatus(false, doList[itemNumber]); + } + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + } else { + addList(doList, str); + } + } catch (KenChatException e) { + System.out.println(e.getMessage()); + printLine(); } - else - addList(doList, str); } endProgramme(); } -} +} \ No newline at end of file diff --git a/src/main/java/KenChatException.java b/src/main/java/KenChatException.java new file mode 100644 index 000000000..3307b6bf6 --- /dev/null +++ b/src/main/java/KenChatException.java @@ -0,0 +1,42 @@ +package main.java; + +public class KenChatException extends Throwable { + public KenChatException(String message) { + super(message); + } + public static String getEmptyDescriptionMessage(String command) { + return "ERROR!!! Enter a description for the command. Use: " + command + " "; + } + + public static String getUnknownCommandMessage() { + return "Wrong command/Please do not have space(s) before a command! Use for full list of commands. "; + } + + public static String getInvalidDeadlineFormatMessage() { + return "Wrong deadline format. Use: deadline /by "; + } + + public static String getInvalidEventFormatMessage() { + return "Wrong event format. Use: event /from /to "; + } + + public static String getTaskNotExistMessage() { + return "No task exist."; + } + + public static String getInvalidTaskNumberMessage() { + return "Please enter a valid task number. Use: mark/unmark "; + } + + public static String getTaskNumberDoesNotExistMessage() { + return "That task number does not exist. Use to see full task list."; + } + + public static String getEmptyTaskNumberMessage(String action) { + return "Task number is missing!! Please specify the task number to " + action + "."; + } + + public static String emptyCommand() { + return "Empty command! Use for full list of commands."; + } +} From 24c44256e684105cd55d56aa67795a516fd4ae6b Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 23:13:10 +0800 Subject: [PATCH 14/26] Level-4 --- src/main/java/KenChat.java | 129 ++++++++++--------------------------- 1 file changed, 33 insertions(+), 96 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 1e2bcb5e5..e59deb544 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -4,7 +4,7 @@ public class KenChat { public static void printLine() { - String line = "____________________________________"; + String line = "____________________________________" ; System.out.println(line); } @@ -22,32 +22,22 @@ public static void endProgramme() { printLine(); } - public static void displayList(Task[] doList) throws KenChatException { + public static void displayList(Task[] doList){ printLine(); - boolean hasTasks = false; - for (int i = 0; i < doList.length; i++) { - if (doList[i] != null) { - System.out.println((i + 1) + ". " + doList[i]); - hasTasks = true; + for (int i=0; i< doList.length; i++){ + if(doList[i] != null){ + System.out.println((i+1)+". "+doList[i]); } } - if (!hasTasks) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } printLine(); } - public static void addList(Task[] doList, String item) throws KenChatException { + public static void addList(Task[] doList, String item) { printLine(); String[] parts = item.split(" ", 2); // Split on first space to get command String command = parts[0]; String description = parts.length > 1 ? parts[1] : ""; - - if (description.isEmpty() && (command.equals("todo") || command.equals("deadline") || command.equals("event"))) { - throw new KenChatException(KenChatException.getEmptyDescriptionMessage(command)); - } - if (command.equalsIgnoreCase("todo")) { Task doItem = new Task.ToDo(description); addTask(doList, doItem); @@ -57,7 +47,7 @@ public static void addList(Task[] doList, String item) throws KenChatException { Task doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); addTask(doList, doItem); } else { - throw new KenChatException(KenChatException.getInvalidDeadlineFormatMessage()); + System.out.println("Invalid deadline format. Use: deadline /by "); } } else if (command.equalsIgnoreCase("event")) { String[] eventParts = description.split(" /from | /to ", 3); @@ -65,10 +55,11 @@ public static void addList(Task[] doList, String item) throws KenChatException { Task doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); addTask(doList, doItem); } else { - throw new KenChatException(KenChatException.getInvalidEventFormatMessage()); + System.out.println("Invalid event format. Use: event /from /to "); } } else { - throw new KenChatException(KenChatException.getUnknownCommandMessage()); + Task doItem = new Task.ToDo(item); // Default to ToDo if no specific type + addTask(doList, doItem); } } @@ -90,98 +81,44 @@ private static void addTask(Task[] doList, Task doItem) { } } - public static void setTaskStatus(boolean isMark, Task item) throws KenChatException { + public static void setTaskStatus(boolean isMark, Task item){ printLine(); - if (item == null) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } - if (isMark) { + if(isMark){ item.markAsDone(); System.out.println("Nice! I've marked this task as done:"); - } else { + } + else { item.markAsUndone(); System.out.println("OK, I've marked this task as not done yet:"); } - System.out.println(" " + item); - printLine(); - } - - public static void showHelp() { - printLine(); - System.out.println("Valid commands:"); - System.out.println("1. todo - Adds a todo task."); - System.out.println("2. deadline /by - Adds a deadline task. can be date and/or time."); - System.out.println("3. event /from /to - Adds an event task. can be date and/or time."); - System.out.println("4. list - Displays all tasks in the list."); - System.out.println("5. mark - Marks the specified task as done."); - System.out.println("6. unmark - Marks the specified task as not done."); - System.out.println("7. bye - Exits the program."); - System.out.println("8. help - Shows this help message."); + System.out.println(" "+item); printLine(); } public static void main(String[] args) { - Scanner sc = new Scanner(System.in); + Scanner sc= new Scanner(System.in); boolean running = true; int arraySize = 100; Task[] doList = new Task[arraySize]; startProgramme(); - - while (running) { - try { - System.out.println(); - String str = sc.nextLine(); - String[] command = str.split(" "); - - if (str.trim().isEmpty()) { - throw new KenChatException(KenChatException.emptyCommand()); - } - - if (command[0].equalsIgnoreCase("bye")) { - running = false; - } else if (command[0].equalsIgnoreCase("help")) { - showHelp(); - }else if (command[0].equalsIgnoreCase("list")) { - displayList(doList); - } else if (command[0].equalsIgnoreCase("mark")) { - if (doList[0] == null) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } else if (command.length < 2) { - throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); - } - try { - int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { - throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); - } else { - setTaskStatus(true, doList[itemNumber]); - } - } catch (NumberFormatException e) { - throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); - } - } else if (command[0].equalsIgnoreCase("unmark")) { - if (doList[0] == null) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } if (command.length < 2) { - throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("unmark")); - } - try { - int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { - throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); - } else { - setTaskStatus(false, doList[itemNumber]); - } - } catch (NumberFormatException e) { - throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); - } - } else { - addList(doList, str); - } - } catch (KenChatException e) { - System.out.println(e.getMessage()); - printLine(); + while (running){ + System.out.println(); + String str= sc.nextLine(); + String[] command = str.split(" "); + if (command[0].equalsIgnoreCase("bye")) + running = false; + else if(command[0].equalsIgnoreCase("list")) + displayList(doList); + else if(command[0].equalsIgnoreCase("mark")){ + int itemNumber = Integer.parseInt(command[1]) - 1; + setTaskStatus(true, doList[itemNumber]); + } + else if(command[0].equalsIgnoreCase("unmark")){ + int itemNumber = Integer.parseInt(command[1]) - 1; + setTaskStatus(false, doList[itemNumber]); } + else + addList(doList, str); } endProgramme(); } From 08036ede1d93d72aae6b0904aae1abe78f50a883 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Sun, 13 Oct 2024 23:13:52 +0800 Subject: [PATCH 15/26] Level-5 --- src/main/java/KenChat.java | 129 +++++++++++++++++++++------- src/main/java/KenChatException.java | 2 +- 2 files changed, 97 insertions(+), 34 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index e59deb544..1e2bcb5e5 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -4,7 +4,7 @@ public class KenChat { public static void printLine() { - String line = "____________________________________" ; + String line = "____________________________________"; System.out.println(line); } @@ -22,22 +22,32 @@ public static void endProgramme() { printLine(); } - public static void displayList(Task[] doList){ + public static void displayList(Task[] doList) throws KenChatException { printLine(); - for (int i=0; i< doList.length; i++){ - if(doList[i] != null){ - System.out.println((i+1)+". "+doList[i]); + boolean hasTasks = false; + for (int i = 0; i < doList.length; i++) { + if (doList[i] != null) { + System.out.println((i + 1) + ". " + doList[i]); + hasTasks = true; } } + if (!hasTasks) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } printLine(); } - public static void addList(Task[] doList, String item) { + public static void addList(Task[] doList, String item) throws KenChatException { printLine(); String[] parts = item.split(" ", 2); // Split on first space to get command String command = parts[0]; String description = parts.length > 1 ? parts[1] : ""; + + if (description.isEmpty() && (command.equals("todo") || command.equals("deadline") || command.equals("event"))) { + throw new KenChatException(KenChatException.getEmptyDescriptionMessage(command)); + } + if (command.equalsIgnoreCase("todo")) { Task doItem = new Task.ToDo(description); addTask(doList, doItem); @@ -47,7 +57,7 @@ public static void addList(Task[] doList, String item) { Task doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); addTask(doList, doItem); } else { - System.out.println("Invalid deadline format. Use: deadline /by "); + throw new KenChatException(KenChatException.getInvalidDeadlineFormatMessage()); } } else if (command.equalsIgnoreCase("event")) { String[] eventParts = description.split(" /from | /to ", 3); @@ -55,11 +65,10 @@ public static void addList(Task[] doList, String item) { Task doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); addTask(doList, doItem); } else { - System.out.println("Invalid event format. Use: event /from /to "); + throw new KenChatException(KenChatException.getInvalidEventFormatMessage()); } } else { - Task doItem = new Task.ToDo(item); // Default to ToDo if no specific type - addTask(doList, doItem); + throw new KenChatException(KenChatException.getUnknownCommandMessage()); } } @@ -81,44 +90,98 @@ private static void addTask(Task[] doList, Task doItem) { } } - public static void setTaskStatus(boolean isMark, Task item){ + public static void setTaskStatus(boolean isMark, Task item) throws KenChatException { printLine(); - if(isMark){ + if (item == null) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + if (isMark) { item.markAsDone(); System.out.println("Nice! I've marked this task as done:"); - } - else { + } else { item.markAsUndone(); System.out.println("OK, I've marked this task as not done yet:"); } - System.out.println(" "+item); + System.out.println(" " + item); + printLine(); + } + + public static void showHelp() { + printLine(); + System.out.println("Valid commands:"); + System.out.println("1. todo - Adds a todo task."); + System.out.println("2. deadline /by - Adds a deadline task. can be date and/or time."); + System.out.println("3. event /from /to - Adds an event task. can be date and/or time."); + System.out.println("4. list - Displays all tasks in the list."); + System.out.println("5. mark - Marks the specified task as done."); + System.out.println("6. unmark - Marks the specified task as not done."); + System.out.println("7. bye - Exits the program."); + System.out.println("8. help - Shows this help message."); printLine(); } public static void main(String[] args) { - Scanner sc= new Scanner(System.in); + Scanner sc = new Scanner(System.in); boolean running = true; int arraySize = 100; Task[] doList = new Task[arraySize]; startProgramme(); - while (running){ - System.out.println(); - String str= sc.nextLine(); - String[] command = str.split(" "); - if (command[0].equalsIgnoreCase("bye")) - running = false; - else if(command[0].equalsIgnoreCase("list")) - displayList(doList); - else if(command[0].equalsIgnoreCase("mark")){ - int itemNumber = Integer.parseInt(command[1]) - 1; - setTaskStatus(true, doList[itemNumber]); - } - else if(command[0].equalsIgnoreCase("unmark")){ - int itemNumber = Integer.parseInt(command[1]) - 1; - setTaskStatus(false, doList[itemNumber]); + + while (running) { + try { + System.out.println(); + String str = sc.nextLine(); + String[] command = str.split(" "); + + if (str.trim().isEmpty()) { + throw new KenChatException(KenChatException.emptyCommand()); + } + + if (command[0].equalsIgnoreCase("bye")) { + running = false; + } else if (command[0].equalsIgnoreCase("help")) { + showHelp(); + }else if (command[0].equalsIgnoreCase("list")) { + displayList(doList); + } else if (command[0].equalsIgnoreCase("mark")) { + if (doList[0] == null) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } else if (command.length < 2) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); + } + try { + int itemNumber = Integer.parseInt(command[1]) - 1; + if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } else { + setTaskStatus(true, doList[itemNumber]); + } + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + } else if (command[0].equalsIgnoreCase("unmark")) { + if (doList[0] == null) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } if (command.length < 2) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("unmark")); + } + try { + int itemNumber = Integer.parseInt(command[1]) - 1; + if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } else { + setTaskStatus(false, doList[itemNumber]); + } + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + } else { + addList(doList, str); + } + } catch (KenChatException e) { + System.out.println(e.getMessage()); + printLine(); } - else - addList(doList, str); } endProgramme(); } diff --git a/src/main/java/KenChatException.java b/src/main/java/KenChatException.java index 3307b6bf6..db3c61076 100644 --- a/src/main/java/KenChatException.java +++ b/src/main/java/KenChatException.java @@ -9,7 +9,7 @@ public static String getEmptyDescriptionMessage(String command) { } public static String getUnknownCommandMessage() { - return "Wrong command/Please do not have space(s) before a command! Use for full list of commands. "; + return "Wrong Command/Please do not have space(s) before a command! Use for full list of commands. "; } public static String getInvalidDeadlineFormatMessage() { From f2ffef38356c254d440f51789d6f5c752630f51b Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Mon, 14 Oct 2024 02:13:36 +0800 Subject: [PATCH 16/26] Level-6 --- src/main/java/KenChat.java | 150 +++++++++++++++++----------- src/main/java/KenChatException.java | 14 ++- 2 files changed, 99 insertions(+), 65 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 1e2bcb5e5..fcfda70b8 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,6 +1,7 @@ package main.java; import java.util.Scanner; +import java.util.ArrayList; public class KenChat { public static void printLine() { @@ -22,23 +23,20 @@ public static void endProgramme() { printLine(); } - public static void displayList(Task[] doList) throws KenChatException { - printLine(); - boolean hasTasks = false; - for (int i = 0; i < doList.length; i++) { - if (doList[i] != null) { - System.out.println((i + 1) + ". " + doList[i]); - hasTasks = true; - } - } - if (!hasTasks) { + public static void displayList(ArrayList doList) throws KenChatException { + if (doList.isEmpty()) { throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } else { + printLine(); + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < doList.size(); i++) { + System.out.println((i + 1) + ". " + doList.get(i)); + } } printLine(); } - public static void addList(Task[] doList, String item) throws KenChatException { - printLine(); + public static void addList(ArrayList doList, String item) throws KenChatException { String[] parts = item.split(" ", 2); // Split on first space to get command String command = parts[0]; String description = parts.length > 1 ? parts[1] : ""; @@ -48,46 +46,47 @@ public static void addList(Task[] doList, String item) throws KenChatException { throw new KenChatException(KenChatException.getEmptyDescriptionMessage(command)); } - if (command.equalsIgnoreCase("todo")) { - Task doItem = new Task.ToDo(description); - addTask(doList, doItem); - } else if (command.equalsIgnoreCase("deadline")) { + Task doItem; + + switch (command.toLowerCase()) { + case "todo": + doItem = new Task.ToDo(description); + break; + case "deadline": String[] deadlineParts = description.split(" /by ", 2); if (deadlineParts.length == 2) { - Task doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); - addTask(doList, doItem); + doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); } else { throw new KenChatException(KenChatException.getInvalidDeadlineFormatMessage()); } - } else if (command.equalsIgnoreCase("event")) { + break; + case "event": String[] eventParts = description.split(" /from | /to ", 3); if (eventParts.length == 3) { - Task doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); - addTask(doList, doItem); + doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); } else { throw new KenChatException(KenChatException.getInvalidEventFormatMessage()); } - } else { + break; + default: throw new KenChatException(KenChatException.getUnknownCommandMessage()); } + + doList.add(doItem); // Add the task to the list + printLine(); + System.out.println("Got it. I've added this task:"); + System.out.println(" " + doItem); + System.out.println("Now you have " + doList.size() + " tasks in the list."); + printLine(); } - private static void addTask(Task[] doList, Task doItem) { - for (int i = 0; i < doList.length; i++) { - if (doList[i] == null) { - doList[i] = doItem; - int taskCount = 0; - for (Task task : doList) { - if (task != null) { - taskCount++; - } - } - System.out.println("Got it. I've added this task:"); - System.out.println(" " + doItem); - System.out.println("Now you have " + taskCount + " tasks in the list."); - break; - } - } + public static void deleteTask(ArrayList doList, int index) { + printLine(); + Task removedTask = doList.remove(index); + System.out.println("Noted. I've removed this task:"); + System.out.println(" " + removedTask); + System.out.println("Now you have " + doList.size() + " tasks in the list."); + printLine(); } public static void setTaskStatus(boolean isMark, Task item) throws KenChatException { @@ -115,74 +114,105 @@ public static void showHelp() { System.out.println("4. list - Displays all tasks in the list."); System.out.println("5. mark - Marks the specified task as done."); System.out.println("6. unmark - Marks the specified task as not done."); - System.out.println("7. bye - Exits the program."); - System.out.println("8. help - Shows this help message."); + System.out.println("7. delete - Deletes the specified task from the list."); + System.out.println("8. bye - Exits the program."); + System.out.println("9. help - Shows this help message."); printLine(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean running = true; - int arraySize = 100; - Task[] doList = new Task[arraySize]; + ArrayList doList = new ArrayList<>(); startProgramme(); while (running) { try { System.out.println(); - String str = sc.nextLine(); - String[] command = str.split(" "); + String str = sc.nextLine().trim(); + if (str.contains(" ")) { // Check if there are multiple spaces + throw new KenChatException(KenChatException.multipleSpaces()); + } + String[] command = str.split(" ", 2); if (str.trim().isEmpty()) { throw new KenChatException(KenChatException.emptyCommand()); } - if (command[0].equalsIgnoreCase("bye")) { + switch (command[0].toLowerCase()) { + case "bye": running = false; - } else if (command[0].equalsIgnoreCase("help")) { + break; + case "help": showHelp(); - }else if (command[0].equalsIgnoreCase("list")) { + break; + case "list": displayList(doList); - } else if (command[0].equalsIgnoreCase("mark")) { - if (doList[0] == null) { + break; + case "mark": + if (doList.isEmpty()) { throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } else if (command.length < 2) { + } + if (command.length < 2) { throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); } try { int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { + if (itemNumber < 0 || itemNumber >= doList.size()) { throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); } else { - setTaskStatus(true, doList[itemNumber]); + setTaskStatus(true, doList.get(itemNumber)); } } catch (NumberFormatException e) { throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); } - } else if (command[0].equalsIgnoreCase("unmark")) { - if (doList[0] == null) { + break; + case "unmark": + if (doList.isEmpty()) { throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } if (command.length < 2) { + } + if (command.length < 2) { throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("unmark")); } try { int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { + if (itemNumber < 0 || itemNumber >= doList.size()) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } else { + setTaskStatus(false, doList.get(itemNumber)); + } + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + break; + case "delete": + if (doList.isEmpty()) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + if (command.length < 2) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("delete")); + } + try { + int itemNumber = Integer.parseInt(command[1]) - 1; + if (itemNumber < 0 || itemNumber >= doList.size()) { throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); } else { - setTaskStatus(false, doList[itemNumber]); + deleteTask(doList, itemNumber); } } catch (NumberFormatException e) { throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); } - } else { + break; + default: addList(doList, str); + break; } } catch (KenChatException e) { + printLine(); System.out.println(e.getMessage()); printLine(); + } } + endProgramme(); } - endProgramme(); } -} \ No newline at end of file diff --git a/src/main/java/KenChatException.java b/src/main/java/KenChatException.java index db3c61076..6af8cf8d2 100644 --- a/src/main/java/KenChatException.java +++ b/src/main/java/KenChatException.java @@ -5,19 +5,19 @@ public KenChatException(String message) { super(message); } public static String getEmptyDescriptionMessage(String command) { - return "ERROR!!! Enter a description for the command. Use: " + command + " "; + return "ERROR!!! Enter a description for the command. Use: " + command + " "; } public static String getUnknownCommandMessage() { - return "Wrong Command/Please do not have space(s) before a command! Use for full list of commands. "; + return "Wrong Command! Use for full list of commands. "; } public static String getInvalidDeadlineFormatMessage() { - return "Wrong deadline format. Use: deadline /by "; + return "Wrong deadline format. Use: deadline /by Note: can be date and/or time."; } public static String getInvalidEventFormatMessage() { - return "Wrong event format. Use: event /from /to "; + return "Wrong event format. Use: event /from /to Note: and can be date and/or time."; } public static String getTaskNotExistMessage() { @@ -25,7 +25,7 @@ public static String getTaskNotExistMessage() { } public static String getInvalidTaskNumberMessage() { - return "Please enter a valid task number. Use: mark/unmark "; + return "Please enter a valid task number. Use: mark/unmark/delete "; } public static String getTaskNumberDoesNotExistMessage() { @@ -39,4 +39,8 @@ public static String getEmptyTaskNumberMessage(String action) { public static String emptyCommand() { return "Empty command! Use for full list of commands."; } + + public static String multipleSpaces() { + return "Too many spaces between words! Ensure each word is separated by only a space."; + } } From 2e81f9ee1636c2e934f71a6329c0193587c1c2c3 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Mon, 14 Oct 2024 09:46:58 +0800 Subject: [PATCH 17/26] Implemented Level-7 --- src/main/java/KenChat.java | 85 ++++++++++++++--------------- src/main/java/KenChatException.java | 9 ++- src/main/java/Storage.java | 80 +++++++++++++++++++++++++++ src/main/java/Task.java | 15 ++++- 4 files changed, 143 insertions(+), 46 deletions(-) create mode 100644 src/main/java/Storage.java diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 1e2bcb5e5..1cc1c4655 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,5 +1,6 @@ package main.java; +import java.util.ArrayList; import java.util.Scanner; public class KenChat { @@ -22,22 +23,18 @@ public static void endProgramme() { printLine(); } - public static void displayList(Task[] doList) throws KenChatException { + public static void displayList(ArrayList doList) throws KenChatException { printLine(); - boolean hasTasks = false; - for (int i = 0; i < doList.length; i++) { - if (doList[i] != null) { - System.out.println((i + 1) + ". " + doList[i]); - hasTasks = true; - } - } - if (!hasTasks) { + if (doList.isEmpty()) { throw new KenChatException(KenChatException.getTaskNotExistMessage()); } + for (int i = 0; i < doList.size(); i++) { + System.out.println((i + 1) + ". " + doList.get(i)); + } printLine(); } - public static void addList(Task[] doList, String item) throws KenChatException { + public static void addList(ArrayList doList, String item, Storage storage) throws KenChatException { printLine(); String[] parts = item.split(" ", 2); // Split on first space to get command String command = parts[0]; @@ -48,49 +45,41 @@ public static void addList(Task[] doList, String item) throws KenChatException { throw new KenChatException(KenChatException.getEmptyDescriptionMessage(command)); } + Task doItem; + if (command.equalsIgnoreCase("todo")) { - Task doItem = new Task.ToDo(description); - addTask(doList, doItem); + doItem = new Task.ToDo(description); + doList.add(doItem); } else if (command.equalsIgnoreCase("deadline")) { String[] deadlineParts = description.split(" /by ", 2); if (deadlineParts.length == 2) { - Task doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); - addTask(doList, doItem); + doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); + doList.add(doItem); } else { throw new KenChatException(KenChatException.getInvalidDeadlineFormatMessage()); } } else if (command.equalsIgnoreCase("event")) { String[] eventParts = description.split(" /from | /to ", 3); if (eventParts.length == 3) { - Task doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); - addTask(doList, doItem); + doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); + doList.add(doItem); } else { throw new KenChatException(KenChatException.getInvalidEventFormatMessage()); } } else { throw new KenChatException(KenChatException.getUnknownCommandMessage()); } - } - private static void addTask(Task[] doList, Task doItem) { - for (int i = 0; i < doList.length; i++) { - if (doList[i] == null) { - doList[i] = doItem; - int taskCount = 0; - for (Task task : doList) { - if (task != null) { - taskCount++; - } - } - System.out.println("Got it. I've added this task:"); - System.out.println(" " + doItem); - System.out.println("Now you have " + taskCount + " tasks in the list."); - break; - } - } + System.out.println("Got it. I've added this task:"); + System.out.println(" " + doList.get(doList.size() - 1)); + System.out.println("Now you have " + doList.size() + " tasks in the list."); + printLine(); + + // Save the updated list to the file + storage.save(doList); } - public static void setTaskStatus(boolean isMark, Task item) throws KenChatException { + public static void setTaskStatus(boolean isMark, Task item, Storage storage, ArrayList doList) throws KenChatException { printLine(); if (item == null) { throw new KenChatException(KenChatException.getTaskNotExistMessage()); @@ -104,6 +93,9 @@ public static void setTaskStatus(boolean isMark, Task item) throws KenChatExcept } System.out.println(" " + item); printLine(); + + // Save the updated list to the file + storage.save(doList); } public static void showHelp() { @@ -123,8 +115,15 @@ public static void showHelp() { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean running = true; - int arraySize = 100; - Task[] doList = new Task[arraySize]; + ArrayList doList = new ArrayList<>(); + Storage storage = new Storage("./data/KenChat.txt"); + + try { + doList = storage.load(); + } catch (KenChatException e) { + System.out.println(e.getMessage()); + } + startProgramme(); while (running) { @@ -144,39 +143,39 @@ public static void main(String[] args) { }else if (command[0].equalsIgnoreCase("list")) { displayList(doList); } else if (command[0].equalsIgnoreCase("mark")) { - if (doList[0] == null) { + if (doList.isEmpty()) { throw new KenChatException(KenChatException.getTaskNotExistMessage()); } else if (command.length < 2) { throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); } try { int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { + if (itemNumber < 0 || itemNumber >= doList.size()) { throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); } else { - setTaskStatus(true, doList[itemNumber]); + setTaskStatus(true, doList.get(itemNumber), storage, doList); } } catch (NumberFormatException e) { throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); } } else if (command[0].equalsIgnoreCase("unmark")) { - if (doList[0] == null) { + if (doList.isEmpty()) { throw new KenChatException(KenChatException.getTaskNotExistMessage()); } if (command.length < 2) { throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("unmark")); } try { int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.length || doList[itemNumber] == null) { + if (itemNumber < 0 || itemNumber >= doList.size()) { throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); } else { - setTaskStatus(false, doList[itemNumber]); + setTaskStatus(false, doList.get(itemNumber), storage, doList); } } catch (NumberFormatException e) { throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); } } else { - addList(doList, str); + addList(doList, str, storage); } } catch (KenChatException e) { System.out.println(e.getMessage()); diff --git a/src/main/java/KenChatException.java b/src/main/java/KenChatException.java index db3c61076..91196b7c6 100644 --- a/src/main/java/KenChatException.java +++ b/src/main/java/KenChatException.java @@ -1,15 +1,16 @@ package main.java; public class KenChatException extends Throwable { + public KenChatException(String message) { super(message); } public static String getEmptyDescriptionMessage(String command) { - return "ERROR!!! Enter a description for the command. Use: " + command + " "; + return "ERROR!!! Enter a description for the command. Use: " + command + " "; } public static String getUnknownCommandMessage() { - return "Wrong Command/Please do not have space(s) before a command! Use for full list of commands. "; + return "Wrong Command! Use for full list of commands. "; } public static String getInvalidDeadlineFormatMessage() { @@ -39,4 +40,8 @@ public static String getEmptyTaskNumberMessage(String action) { public static String emptyCommand() { return "Empty command! Use for full list of commands."; } + + public static String directoryCreationFailure() { + return "Failed to create directory."; + } } diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 000000000..32764ce0a --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,80 @@ +package main.java; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.ArrayList; +public class Storage { + private final String filePath; + + public Storage(String filePath) { + this.filePath = filePath; + } + + public ArrayList load() throws KenChatException { + ArrayList doList = new ArrayList<>(); + File file = new File(filePath); + + if (!file.exists()) { + System.out.println("Data file no found. Beginning with a new empty task list."); + return doList; + } + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + Task task = getTask(line); + doList.add(task); + } + } catch (IOException e) { + throw new KenChatException("Error loading data from file: " + e.getMessage()); + } + return doList; + } + + private static Task getTask(String line) throws KenChatException { + String[] parts = line.split(" \\| "); + Task task; + switch (parts[0]) { + case "T": + task = new Task.ToDo(parts[2]); + break; + case "D": + task = new Task.Deadline(parts[2], parts[3]); + break; + case "E": + task = new Task.Event(parts[2], parts[3], parts[4]); + break; + default: + throw new KenChatException("Corrupted data file."); + } + if (parts[1].equals("1")) { + task.markAsDone(); + } + return task; + } + + public void save(ArrayList doList) throws KenChatException { + File file = new File(filePath); + + // Ensure the directory exists + File directory = new File(file.getParent()); + if (!directory.exists()) { + if (directory.mkdirs()) { + System.out.println("Directory created successfully."); + } else { + throw new KenChatException(KenChatException.directoryCreationFailure()); + } + } + + try (FileWriter writer = new FileWriter(file)) { + for (Task task : doList) { + writer.write(task.formatForStorage() + System.lineSeparator()); + } + } catch (IOException e) { + throw new KenChatException("Error saving data to file: " + e.getMessage()); + } + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 3de69ddd7..c8e5642c5 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,9 +1,11 @@ package main.java; -public class Task { +public abstract class Task { protected String description; protected boolean isDone; + public abstract String formatForStorage(); + public Task(String description) { this.description = description; this.isDone = false; @@ -36,6 +38,10 @@ public ToDo(String description) { public String toString() { return "[T]" + super.toString(); } + + public String formatForStorage() { + return "T | " + (isDone ? "1" : "0") + " | " + description; + } } // Deadline class @@ -50,6 +56,9 @@ public Deadline(String description, String dueDate) { public String toString() { return "[D]" + super.toString() + " (by: " + dueDate + ")"; } + public String formatForStorage() { + return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + dueDate; + } } // Event class @@ -66,6 +75,10 @@ public Event(String description, String startDate, String endDate) { public String toString() { return "[E]" + super.toString() + " (from: " + startDate + " to: " + endDate + ")"; } + + public String formatForStorage() { + return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + startDate + " | " + endDate; + } } } \ No newline at end of file From a337c2df6788f7c13398fc9ef90563c5eac73027 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Tue, 15 Oct 2024 12:54:41 +0800 Subject: [PATCH 18/26] A-MoreOOP --- src/main/java/AddCommand.java | 16 ++ src/main/java/Command.java | 10 ++ src/main/java/DeleteCommand.java | 23 +++ src/main/java/ExitCommand.java | 14 ++ src/main/java/HelpCommand.java | 9 + src/main/java/KenChat.java | 246 +++------------------------- src/main/java/KenChatException.java | 8 +- src/main/java/ListCommand.java | 12 ++ src/main/java/MarkCommand.java | 23 +++ src/main/java/Parser.java | 82 ++++++++++ src/main/java/TaskList.java | 37 +++++ src/main/java/Ui.java | 108 ++++++++++++ src/main/java/UnmarkCommand.java | 23 +++ 13 files changed, 386 insertions(+), 225 deletions(-) create mode 100644 src/main/java/AddCommand.java create mode 100644 src/main/java/Command.java create mode 100644 src/main/java/DeleteCommand.java create mode 100644 src/main/java/ExitCommand.java create mode 100644 src/main/java/HelpCommand.java create mode 100644 src/main/java/ListCommand.java create mode 100644 src/main/java/MarkCommand.java create mode 100644 src/main/java/Parser.java create mode 100644 src/main/java/TaskList.java create mode 100644 src/main/java/Ui.java create mode 100644 src/main/java/UnmarkCommand.java diff --git a/src/main/java/AddCommand.java b/src/main/java/AddCommand.java new file mode 100644 index 000000000..e05629884 --- /dev/null +++ b/src/main/java/AddCommand.java @@ -0,0 +1,16 @@ +package main.java; + +public class AddCommand extends Command { + private final Task task; + + public AddCommand(Task task) { + this.task = task; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + tasks.addTask(task); + storage.save(tasks.getTasks()); + ui.showTaskAdded(task, tasks.getSize()); + } +} diff --git a/src/main/java/Command.java b/src/main/java/Command.java new file mode 100644 index 000000000..4be7365cb --- /dev/null +++ b/src/main/java/Command.java @@ -0,0 +1,10 @@ +package main.java; + +public abstract class Command { + + public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException; + + public boolean isRunning() { + return true; + } +} diff --git a/src/main/java/DeleteCommand.java b/src/main/java/DeleteCommand.java new file mode 100644 index 000000000..43eb2e804 --- /dev/null +++ b/src/main/java/DeleteCommand.java @@ -0,0 +1,23 @@ +package main.java; + +public class DeleteCommand extends Command { + private final int index; + + public DeleteCommand(int index) { + this.index = index - 1; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + if (tasks.getSize() == 0) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + if (index < 0 || index >= tasks.getSize()) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } + Task removedTask = tasks.getTask(index); + tasks.deleteTask(index); + storage.save(tasks.getTasks()); + ui.showTaskRemoved(removedTask, tasks.getSize()); + } +} diff --git a/src/main/java/ExitCommand.java b/src/main/java/ExitCommand.java new file mode 100644 index 000000000..cc4937cd4 --- /dev/null +++ b/src/main/java/ExitCommand.java @@ -0,0 +1,14 @@ +package main.java; + +public class ExitCommand extends Command { + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) { + + } + + @Override + public boolean isRunning() { + return false; + } +} diff --git a/src/main/java/HelpCommand.java b/src/main/java/HelpCommand.java new file mode 100644 index 000000000..5c2a193e9 --- /dev/null +++ b/src/main/java/HelpCommand.java @@ -0,0 +1,9 @@ +package main.java; + +public class HelpCommand extends Command { + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) { + ui.showHelp(); + } +} diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 8fbd26f3d..03a5fa42d 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,234 +1,38 @@ package main.java; -import java.util.ArrayList; -import java.util.Scanner; -import java.util.ArrayList; - public class KenChat { - public static void printLine() { - String line = "____________________________________"; - System.out.println(line); - } - - public static void startProgramme() { - String chatBotName = "KenChat"; - printLine(); - System.out.println("Hello! I'm " + chatBotName); - System.out.println("What can I do for you?"); - printLine(); - } - - public static void endProgramme() { - printLine(); - System.out.println("Bye. Hope to see you again soon!"); - printLine(); - } - - public static void displayList(ArrayList doList) throws KenChatException { - if (doList.isEmpty()) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } else { - printLine(); - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < doList.size(); i++) { - System.out.println((i + 1) + ". " + doList.get(i)); - } - } - printLine(); - } - - public static void addList(ArrayList doList, String item, Storage storage) throws KenChatException { - String[] parts = item.split(" ", 2); // Split on first space to get command - String command = parts[0]; - String description = parts.length > 1 ? parts[1] : ""; - - - if (description.isEmpty() && (command.equals("todo") || command.equals("deadline") || command.equals("event"))) { - throw new KenChatException(KenChatException.getEmptyDescriptionMessage(command)); - } - - Task doItem; - - switch (command.toLowerCase()) { - case "todo": - doItem = new Task.ToDo(description); - break; - case "deadline": - String[] deadlineParts = description.split(" /by ", 2); - if (deadlineParts.length == 2) { - doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); - } else { - throw new KenChatException(KenChatException.getInvalidDeadlineFormatMessage()); - } - break; - case "event": - String[] eventParts = description.split(" /from | /to ", 3); - if (eventParts.length == 3) { - doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); - } else { - throw new KenChatException(KenChatException.getInvalidEventFormatMessage()); - } - break; - default: - throw new KenChatException(KenChatException.getUnknownCommandMessage()); - } - - doList.add(doItem); // Add the task to the list - storage.save(doList); - printLine(); - System.out.println("Got it. I've added this task:"); - System.out.println(" " + doItem); - System.out.println("Now you have " + doList.size() + " tasks in the list."); - printLine(); - } - - public static void deleteTask(ArrayList doList, int index, Storage storage) throws KenChatException { - printLine(); - Task removedTask = doList.remove(index); - System.out.println("Noted. I've removed this task:"); - System.out.println(" " + removedTask); - System.out.println("Now you have " + doList.size() + " tasks in the list."); - printLine(); - - storage.save(doList); - } - - public static void setTaskStatus(boolean isMark, Task item, Storage storage, ArrayList doList) throws KenChatException { - printLine(); - if (item == null) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } - if (isMark) { - item.markAsDone(); - System.out.println("Nice! I've marked this task as done:"); - } else { - item.markAsUndone(); - System.out.println("OK, I've marked this task as not done yet:"); - } - System.out.println(" " + item); - printLine(); - - // Save the updated list to the file - storage.save(doList); - } - - public static void showHelp() { - printLine(); - System.out.println("Valid commands:"); - System.out.println("1. todo - Adds a todo task."); - System.out.println("2. deadline /by - Adds a deadline task. can be date and/or time."); - System.out.println("3. event /from /to - Adds an event task. can be date and/or time."); - System.out.println("4. list - Displays all tasks in the list."); - System.out.println("5. mark - Marks the specified task as done."); - System.out.println("6. unmark - Marks the specified task as not done."); - System.out.println("7. delete - Deletes the specified task from the list."); - System.out.println("8. bye - Exits the program."); - System.out.println("9. help - Shows this help message."); - printLine(); - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - boolean running = true; - ArrayList doList = new ArrayList<>(); - - Storage storage = new Storage("./data/KenChat.txt"); + private final Storage storage; + private TaskList tasks; + private final Ui ui; + public KenChat(String filePath) { + ui = new Ui(); + storage = new Storage(filePath); try { - doList = storage.load(); + tasks = new TaskList(storage.load()); } catch (KenChatException e) { - System.out.println(e.getMessage()); + tasks = new TaskList(); + ui.showLoadingError(); } + } - startProgramme(); - - while (running) { + public void run() { + ui.start(); + boolean isRunning = true; + while (isRunning) { try { - System.out.println(); - String str = sc.nextLine().trim(); - if (str.contains(" ")) { // Check if there are multiple spaces - throw new KenChatException(KenChatException.multipleSpaces()); - } - String[] command = str.split(" ", 2); - - if (str.trim().isEmpty()) { - throw new KenChatException(KenChatException.emptyCommand()); - } - - switch (command[0].toLowerCase()) { - case "bye": - running = false; - break; - case "help": - showHelp(); - break; - case "list": - displayList(doList); - break; - case "mark": - if (doList.isEmpty()) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } - if (command.length < 2) { - throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); - } - try { - int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.size()) { - throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); - } else { - setTaskStatus(true, doList.get(itemNumber), storage, doList); - } - } catch (NumberFormatException e) { - throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); - } - break; - case "unmark": - if (doList.isEmpty()) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } - if (command.length < 2) { - throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("unmark")); - } - try { - int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.size()) { - throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); - } else { - setTaskStatus(false, doList.get(itemNumber), storage, doList); - } - } catch (NumberFormatException e) { - throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); - } - break; - case "delete": - if (doList.isEmpty()) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } - if (command.length < 2) { - throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("delete")); - } - try { - int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.size()) { - throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); - } else { - deleteTask(doList, itemNumber, storage); - } - } catch (NumberFormatException e) { - throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); - } - break; - default: - addList(doList, str, storage); - break; - } + String fullCommand = ui.readCommand(); + Command command = Parser.parse(fullCommand); + command.execute(tasks, ui, storage); + isRunning = command.isRunning(); } catch (KenChatException e) { - printLine(); - System.out.println(e.getMessage()); - printLine(); - } + ui.showError(e.getMessage()); } - endProgramme(); } + ui.end(); + } + + public static void main(String[] args) throws KenChatException { + new KenChat("data/DukeBot.txt").run(); } +} diff --git a/src/main/java/KenChatException.java b/src/main/java/KenChatException.java index fbe797798..5c52fca58 100644 --- a/src/main/java/KenChatException.java +++ b/src/main/java/KenChatException.java @@ -1,12 +1,12 @@ package main.java; -public class KenChatException extends Throwable { +public class KenChatException extends Exception { public KenChatException(String message) { super(message); } public static String getEmptyDescriptionMessage(String command) { - return "ERROR!!! Enter a description for the command. Use: " + command + " "; + return "Description is missing! Enter a description for the command. Use: " + command + " "; } public static String getUnknownCommandMessage() { @@ -22,7 +22,7 @@ public static String getInvalidEventFormatMessage() { } public static String getTaskNotExistMessage() { - return "No task exist."; + return "No task exists."; } public static String getInvalidTaskNumberMessage() { @@ -38,7 +38,7 @@ public static String getEmptyTaskNumberMessage(String action) { } public static String emptyCommand() { - return "Empty command! Use for full list of commands."; + return "Empty command!! Use for full list of commands."; } public static String multipleSpaces() { diff --git a/src/main/java/ListCommand.java b/src/main/java/ListCommand.java new file mode 100644 index 000000000..9430bbc04 --- /dev/null +++ b/src/main/java/ListCommand.java @@ -0,0 +1,12 @@ +package main.java; + +public class ListCommand extends Command { + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + if (tasks.getSize() == 0) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + ui.showTasksList(tasks.getTasks()); + } +} diff --git a/src/main/java/MarkCommand.java b/src/main/java/MarkCommand.java new file mode 100644 index 000000000..5bbcfe7ae --- /dev/null +++ b/src/main/java/MarkCommand.java @@ -0,0 +1,23 @@ +package main.java; + +public class MarkCommand extends Command { + private final int index; + + public MarkCommand(int index) { + this.index = index - 1; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + if (tasks.getSize() == 0) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + if (index < 0 || index >= tasks.getSize()) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } + Task task = tasks.getTask(index); + task.markAsDone(); + storage.save(tasks.getTasks()); + ui.showTaskMarked(task); + } +} diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 000000000..97fb2bbdc --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,82 @@ +package main.java; + +public class Parser { + public static Command parse(String userInput) throws KenChatException { + userInput = userInput.trim(); + + if (userInput.isEmpty()) { // Check for null command + throw new KenChatException(KenChatException.emptyCommand()); + } + + if (userInput.contains(" ")) { // Check for multiple spaces + throw new KenChatException(KenChatException.multipleSpaces()); + } + + String[] command = userInput.split(" ", 2); + String action = command[0].toLowerCase(); + String arguments = command.length > 1 ? command[1] : ""; + + switch (action) { + case "bye": + return new ExitCommand(); + case "list": + return new ListCommand(); + case "mark": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); + } + try { + return new MarkCommand(Integer.parseInt(arguments)); + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + case "unmark": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); + } + try { + return new UnmarkCommand(Integer.parseInt(arguments)); + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + case "delete": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); + } + try { + return new DeleteCommand(Integer.parseInt(arguments)); + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + case "todo": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyDescriptionMessage(action)); + } + return new AddCommand(new Task.ToDo(arguments)); + case "deadline": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyDescriptionMessage(action)); + } + String[] deadlineParts = arguments.split(" /by ", 2); + if (deadlineParts.length == 2) { + return new AddCommand(new Task.Deadline(deadlineParts[0], deadlineParts[1])); + } else { + throw new KenChatException(KenChatException.getInvalidDeadlineFormatMessage()); + } + case "event": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyDescriptionMessage(action)); + } + String[] eventParts = arguments.split(" /from | /to ", 3); + if (eventParts.length == 3) { + return new AddCommand(new Task.Event(eventParts[0], eventParts[1], eventParts[2])); + } else { + throw new KenChatException(KenChatException.getInvalidEventFormatMessage()); + } + case "help": + return new HelpCommand(); + default: + throw new KenChatException(KenChatException.getUnknownCommandMessage()); + } + } +} diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 000000000..d9afa7f2c --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,37 @@ +package main.java; + +import java.util.ArrayList; +import java.util.List; + +public class TaskList { + private final List tasks; + + public TaskList() { + tasks = new ArrayList<>(); + } + + public TaskList(List tasks) { + this.tasks = tasks; + } + + public void addTask(Task task) { + tasks.add(task); + } + + public void deleteTask(int index) { + tasks.remove(index); + } + + public Task getTask(int index) { + return tasks.get(index); + } + + public int getSize() { + return tasks.size(); + } + + public ArrayList getTasks() { + return (ArrayList) tasks; + } + +} diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 000000000..4313bed28 --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,108 @@ +package main.java; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Ui { + private final Scanner sc; + + public Ui() { + this.sc = new Scanner(System.in); + } + + public void start() { + printLine(); + System.out.println("Hello! I'm DukeBot"); + System.out.println("What can I do for you?"); + printLine(); + System.out.println(); + } + + public void end() { + printLine(); + System.out.println("Bye. Hope to see you again soon!"); + printLine(); + System.out.println(); + } + + public void printLine() { + System.out.println("____________________________________"); + } + + public String readCommand() { + return sc.nextLine(); + } + + public void showError(String message) { + printLine(); + System.out.println(message); + printLine(); + System.out.println(); + } + + public void showLoadingError() { + showError("Error loading data from file."); + } + + public void showTaskAdded(Task task, int size) { + printLine(); + System.out.println("Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println("Now you have " + size + " tasks in the list."); + printLine(); + System.out.println(); + } + + public void showTaskRemoved(Task task, int size) { + printLine(); + System.out.println("Noted. I've removed this task:"); + System.out.println(" " + task); + System.out.println("Now you have " + size + " tasks in the list."); + printLine(); + System.out.println(); + } + + public void showTaskMarked(Task task) { + printLine(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(" " + task); + printLine(); + System.out.println(); + } + + public void showTaskUnmarked(Task task) { + printLine(); + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(" " + task); + printLine(); + System.out.println(); + } + + public void showTasksList(ArrayList tasks) { + printLine(); + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + ". " + tasks.get(i)); + } + printLine(); + System.out.println(); + } + + public void showHelp() { + printLine(); + System.out.println("Valid commands:"); + System.out.println("1. todo - Adds a todo task."); + System.out.println("2. deadline /by - Adds a deadline task. can be date and/or time."); + System.out.println("3. event /from /to - Adds an event task. can be date and/or time."); + System.out.println("4. list - Displays all tasks in the list."); + System.out.println("5. mark - Marks the specified task as done."); + System.out.println("6. unmark - Marks the specified task as not done."); + System.out.println("7. delete - Deletes the specified task from the list."); + System.out.println("8. bye - Exits the program."); + System.out.println("9. help - Shows this help message."); + printLine(); + System.out.println(); + } + +} + diff --git a/src/main/java/UnmarkCommand.java b/src/main/java/UnmarkCommand.java new file mode 100644 index 000000000..57207fdac --- /dev/null +++ b/src/main/java/UnmarkCommand.java @@ -0,0 +1,23 @@ +package main.java; + +public class UnmarkCommand extends Command { + private final int index; + + public UnmarkCommand(int index) { + this.index = index - 1; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + if (tasks.getSize() == 0) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + if (index < 0 || index >= tasks.getSize()) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } + Task task = tasks.getTask(index); + task.markAsUndone(); + storage.save(tasks.getTasks()); + ui.showTaskUnmarked(task); + } +} From b526ab832876a08f22f0044195a443bf78de906a Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Tue, 15 Oct 2024 12:54:41 +0800 Subject: [PATCH 19/26] Level-9 --- src/main/java/AddCommand.java | 16 ++ src/main/java/Command.java | 10 ++ src/main/java/DeleteCommand.java | 23 +++ src/main/java/ExitCommand.java | 14 ++ src/main/java/FindCommand.java | 34 ++++ src/main/java/HelpCommand.java | 9 + src/main/java/KenChat.java | 246 +++------------------------- src/main/java/KenChatException.java | 12 +- src/main/java/ListCommand.java | 12 ++ src/main/java/MarkCommand.java | 23 +++ src/main/java/Parser.java | 87 ++++++++++ src/main/java/Task.java | 10 +- src/main/java/TaskList.java | 62 +++++++ src/main/java/Ui.java | 114 +++++++++++++ src/main/java/UnmarkCommand.java | 23 +++ 15 files changed, 469 insertions(+), 226 deletions(-) create mode 100644 src/main/java/AddCommand.java create mode 100644 src/main/java/Command.java create mode 100644 src/main/java/DeleteCommand.java create mode 100644 src/main/java/ExitCommand.java create mode 100644 src/main/java/FindCommand.java create mode 100644 src/main/java/HelpCommand.java create mode 100644 src/main/java/ListCommand.java create mode 100644 src/main/java/MarkCommand.java create mode 100644 src/main/java/Parser.java create mode 100644 src/main/java/TaskList.java create mode 100644 src/main/java/Ui.java create mode 100644 src/main/java/UnmarkCommand.java diff --git a/src/main/java/AddCommand.java b/src/main/java/AddCommand.java new file mode 100644 index 000000000..e05629884 --- /dev/null +++ b/src/main/java/AddCommand.java @@ -0,0 +1,16 @@ +package main.java; + +public class AddCommand extends Command { + private final Task task; + + public AddCommand(Task task) { + this.task = task; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + tasks.addTask(task); + storage.save(tasks.getTasks()); + ui.showTaskAdded(task, tasks.getSize()); + } +} diff --git a/src/main/java/Command.java b/src/main/java/Command.java new file mode 100644 index 000000000..4be7365cb --- /dev/null +++ b/src/main/java/Command.java @@ -0,0 +1,10 @@ +package main.java; + +public abstract class Command { + + public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException; + + public boolean isRunning() { + return true; + } +} diff --git a/src/main/java/DeleteCommand.java b/src/main/java/DeleteCommand.java new file mode 100644 index 000000000..43eb2e804 --- /dev/null +++ b/src/main/java/DeleteCommand.java @@ -0,0 +1,23 @@ +package main.java; + +public class DeleteCommand extends Command { + private final int index; + + public DeleteCommand(int index) { + this.index = index - 1; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + if (tasks.getSize() == 0) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + if (index < 0 || index >= tasks.getSize()) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } + Task removedTask = tasks.getTask(index); + tasks.deleteTask(index); + storage.save(tasks.getTasks()); + ui.showTaskRemoved(removedTask, tasks.getSize()); + } +} diff --git a/src/main/java/ExitCommand.java b/src/main/java/ExitCommand.java new file mode 100644 index 000000000..cc4937cd4 --- /dev/null +++ b/src/main/java/ExitCommand.java @@ -0,0 +1,14 @@ +package main.java; + +public class ExitCommand extends Command { + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) { + + } + + @Override + public boolean isRunning() { + return false; + } +} diff --git a/src/main/java/FindCommand.java b/src/main/java/FindCommand.java new file mode 100644 index 000000000..687aee711 --- /dev/null +++ b/src/main/java/FindCommand.java @@ -0,0 +1,34 @@ +package main.java; + +import java.util.ArrayList; +import java.util.Map; + +public class FindCommand extends Command { + private final String keyword; + + public FindCommand(String keyword) { + this.keyword = keyword; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + //ArrayList matchingTasks = tasks.findTasksByKeywordWithIndex(keyword); + + // Use the findTasksByKeywordWithIndex method that returns a Map of + Map matchingTasks = tasks.findTasksByKeywordWithIndex(keyword); + if (matchingTasks.isEmpty()) { + ui.showError("No matching tasks found for: " + keyword); + } else { + ui.printLine(); + ui.showMessage("Here are the matching tasks in your list:"); + // Display the matching tasks with the correct indices + for (Map.Entry entry : matchingTasks.entrySet()) { + int index = entry.getKey(); // The original index of the task in the task list + Task task = entry.getValue(); // The task itself + ui.showMessage(index + ". " + task); // Output task with its original index + } + ui.printLine(); + System.out.println(); + } + } +} diff --git a/src/main/java/HelpCommand.java b/src/main/java/HelpCommand.java new file mode 100644 index 000000000..5c2a193e9 --- /dev/null +++ b/src/main/java/HelpCommand.java @@ -0,0 +1,9 @@ +package main.java; + +public class HelpCommand extends Command { + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) { + ui.showHelp(); + } +} diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 8fbd26f3d..03a5fa42d 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,234 +1,38 @@ package main.java; -import java.util.ArrayList; -import java.util.Scanner; -import java.util.ArrayList; - public class KenChat { - public static void printLine() { - String line = "____________________________________"; - System.out.println(line); - } - - public static void startProgramme() { - String chatBotName = "KenChat"; - printLine(); - System.out.println("Hello! I'm " + chatBotName); - System.out.println("What can I do for you?"); - printLine(); - } - - public static void endProgramme() { - printLine(); - System.out.println("Bye. Hope to see you again soon!"); - printLine(); - } - - public static void displayList(ArrayList doList) throws KenChatException { - if (doList.isEmpty()) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } else { - printLine(); - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < doList.size(); i++) { - System.out.println((i + 1) + ". " + doList.get(i)); - } - } - printLine(); - } - - public static void addList(ArrayList doList, String item, Storage storage) throws KenChatException { - String[] parts = item.split(" ", 2); // Split on first space to get command - String command = parts[0]; - String description = parts.length > 1 ? parts[1] : ""; - - - if (description.isEmpty() && (command.equals("todo") || command.equals("deadline") || command.equals("event"))) { - throw new KenChatException(KenChatException.getEmptyDescriptionMessage(command)); - } - - Task doItem; - - switch (command.toLowerCase()) { - case "todo": - doItem = new Task.ToDo(description); - break; - case "deadline": - String[] deadlineParts = description.split(" /by ", 2); - if (deadlineParts.length == 2) { - doItem = new Task.Deadline(deadlineParts[0], deadlineParts[1]); - } else { - throw new KenChatException(KenChatException.getInvalidDeadlineFormatMessage()); - } - break; - case "event": - String[] eventParts = description.split(" /from | /to ", 3); - if (eventParts.length == 3) { - doItem = new Task.Event(eventParts[0], eventParts[1], eventParts[2]); - } else { - throw new KenChatException(KenChatException.getInvalidEventFormatMessage()); - } - break; - default: - throw new KenChatException(KenChatException.getUnknownCommandMessage()); - } - - doList.add(doItem); // Add the task to the list - storage.save(doList); - printLine(); - System.out.println("Got it. I've added this task:"); - System.out.println(" " + doItem); - System.out.println("Now you have " + doList.size() + " tasks in the list."); - printLine(); - } - - public static void deleteTask(ArrayList doList, int index, Storage storage) throws KenChatException { - printLine(); - Task removedTask = doList.remove(index); - System.out.println("Noted. I've removed this task:"); - System.out.println(" " + removedTask); - System.out.println("Now you have " + doList.size() + " tasks in the list."); - printLine(); - - storage.save(doList); - } - - public static void setTaskStatus(boolean isMark, Task item, Storage storage, ArrayList doList) throws KenChatException { - printLine(); - if (item == null) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } - if (isMark) { - item.markAsDone(); - System.out.println("Nice! I've marked this task as done:"); - } else { - item.markAsUndone(); - System.out.println("OK, I've marked this task as not done yet:"); - } - System.out.println(" " + item); - printLine(); - - // Save the updated list to the file - storage.save(doList); - } - - public static void showHelp() { - printLine(); - System.out.println("Valid commands:"); - System.out.println("1. todo - Adds a todo task."); - System.out.println("2. deadline /by - Adds a deadline task. can be date and/or time."); - System.out.println("3. event /from /to - Adds an event task. can be date and/or time."); - System.out.println("4. list - Displays all tasks in the list."); - System.out.println("5. mark - Marks the specified task as done."); - System.out.println("6. unmark - Marks the specified task as not done."); - System.out.println("7. delete - Deletes the specified task from the list."); - System.out.println("8. bye - Exits the program."); - System.out.println("9. help - Shows this help message."); - printLine(); - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - boolean running = true; - ArrayList doList = new ArrayList<>(); - - Storage storage = new Storage("./data/KenChat.txt"); + private final Storage storage; + private TaskList tasks; + private final Ui ui; + public KenChat(String filePath) { + ui = new Ui(); + storage = new Storage(filePath); try { - doList = storage.load(); + tasks = new TaskList(storage.load()); } catch (KenChatException e) { - System.out.println(e.getMessage()); + tasks = new TaskList(); + ui.showLoadingError(); } + } - startProgramme(); - - while (running) { + public void run() { + ui.start(); + boolean isRunning = true; + while (isRunning) { try { - System.out.println(); - String str = sc.nextLine().trim(); - if (str.contains(" ")) { // Check if there are multiple spaces - throw new KenChatException(KenChatException.multipleSpaces()); - } - String[] command = str.split(" ", 2); - - if (str.trim().isEmpty()) { - throw new KenChatException(KenChatException.emptyCommand()); - } - - switch (command[0].toLowerCase()) { - case "bye": - running = false; - break; - case "help": - showHelp(); - break; - case "list": - displayList(doList); - break; - case "mark": - if (doList.isEmpty()) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } - if (command.length < 2) { - throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); - } - try { - int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.size()) { - throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); - } else { - setTaskStatus(true, doList.get(itemNumber), storage, doList); - } - } catch (NumberFormatException e) { - throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); - } - break; - case "unmark": - if (doList.isEmpty()) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } - if (command.length < 2) { - throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("unmark")); - } - try { - int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.size()) { - throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); - } else { - setTaskStatus(false, doList.get(itemNumber), storage, doList); - } - } catch (NumberFormatException e) { - throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); - } - break; - case "delete": - if (doList.isEmpty()) { - throw new KenChatException(KenChatException.getTaskNotExistMessage()); - } - if (command.length < 2) { - throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("delete")); - } - try { - int itemNumber = Integer.parseInt(command[1]) - 1; - if (itemNumber < 0 || itemNumber >= doList.size()) { - throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); - } else { - deleteTask(doList, itemNumber, storage); - } - } catch (NumberFormatException e) { - throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); - } - break; - default: - addList(doList, str, storage); - break; - } + String fullCommand = ui.readCommand(); + Command command = Parser.parse(fullCommand); + command.execute(tasks, ui, storage); + isRunning = command.isRunning(); } catch (KenChatException e) { - printLine(); - System.out.println(e.getMessage()); - printLine(); - } + ui.showError(e.getMessage()); } - endProgramme(); } + ui.end(); + } + + public static void main(String[] args) throws KenChatException { + new KenChat("data/DukeBot.txt").run(); } +} diff --git a/src/main/java/KenChatException.java b/src/main/java/KenChatException.java index fbe797798..be6d87d93 100644 --- a/src/main/java/KenChatException.java +++ b/src/main/java/KenChatException.java @@ -1,12 +1,12 @@ package main.java; -public class KenChatException extends Throwable { +public class KenChatException extends Exception { public KenChatException(String message) { super(message); } public static String getEmptyDescriptionMessage(String command) { - return "ERROR!!! Enter a description for the command. Use: " + command + " "; + return "Description is missing! Enter a description for the command. Use: " + command + " "; } public static String getUnknownCommandMessage() { @@ -22,7 +22,7 @@ public static String getInvalidEventFormatMessage() { } public static String getTaskNotExistMessage() { - return "No task exist."; + return "No task exists."; } public static String getInvalidTaskNumberMessage() { @@ -38,7 +38,7 @@ public static String getEmptyTaskNumberMessage(String action) { } public static String emptyCommand() { - return "Empty command! Use for full list of commands."; + return "Empty command!! Use for full list of commands."; } public static String multipleSpaces() { @@ -47,4 +47,8 @@ public static String multipleSpaces() { public static String directoryCreationFailure() { return "Failed to create directory."; } + + public static String getKeyWordMissing() { + return "Keyword is missing. Use: find "; + } } diff --git a/src/main/java/ListCommand.java b/src/main/java/ListCommand.java new file mode 100644 index 000000000..9430bbc04 --- /dev/null +++ b/src/main/java/ListCommand.java @@ -0,0 +1,12 @@ +package main.java; + +public class ListCommand extends Command { + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + if (tasks.getSize() == 0) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + ui.showTasksList(tasks.getTasks()); + } +} diff --git a/src/main/java/MarkCommand.java b/src/main/java/MarkCommand.java new file mode 100644 index 000000000..5bbcfe7ae --- /dev/null +++ b/src/main/java/MarkCommand.java @@ -0,0 +1,23 @@ +package main.java; + +public class MarkCommand extends Command { + private final int index; + + public MarkCommand(int index) { + this.index = index - 1; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + if (tasks.getSize() == 0) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + if (index < 0 || index >= tasks.getSize()) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } + Task task = tasks.getTask(index); + task.markAsDone(); + storage.save(tasks.getTasks()); + ui.showTaskMarked(task); + } +} diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 000000000..be4fb5a57 --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,87 @@ +package main.java; + +public class Parser { + public static Command parse(String userInput) throws KenChatException { + userInput = userInput.trim(); + + if (userInput.isEmpty()) { // Check for null command + throw new KenChatException(KenChatException.emptyCommand()); + } + + if (userInput.contains(" ")) { // Check for multiple spaces + throw new KenChatException(KenChatException.multipleSpaces()); + } + + String[] command = userInput.split(" ", 2); + String action = command[0].toLowerCase(); + String arguments = command.length > 1 ? command[1] : ""; + + switch (action) { + case "bye": + return new ExitCommand(); + case "list": + return new ListCommand(); + case "mark": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); + } + try { + return new MarkCommand(Integer.parseInt(arguments)); + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + case "unmark": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); + } + try { + return new UnmarkCommand(Integer.parseInt(arguments)); + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + case "delete": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyTaskNumberMessage("mark")); + } + try { + return new DeleteCommand(Integer.parseInt(arguments)); + } catch (NumberFormatException e) { + throw new KenChatException(KenChatException.getInvalidTaskNumberMessage()); + } + case "todo": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyDescriptionMessage(action)); + } + return new AddCommand(new Task.ToDo(arguments)); + case "deadline": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyDescriptionMessage(action)); + } + String[] deadlineParts = arguments.split(" /by ", 2); + if (deadlineParts.length == 2) { + return new AddCommand(new Task.Deadline(deadlineParts[0], deadlineParts[1])); + } else { + throw new KenChatException(KenChatException.getInvalidDeadlineFormatMessage()); + } + case "event": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getEmptyDescriptionMessage(action)); + } + String[] eventParts = arguments.split(" /from | /to ", 3); + if (eventParts.length == 3) { + return new AddCommand(new Task.Event(eventParts[0], eventParts[1], eventParts[2])); + } else { + throw new KenChatException(KenChatException.getInvalidEventFormatMessage()); + } + case "help": + return new HelpCommand(); + case "find": + if (arguments.isEmpty()) { + throw new KenChatException(KenChatException.getKeyWordMissing()); + } + return new FindCommand(arguments); + default: + throw new KenChatException(KenChatException.getUnknownCommandMessage()); + } + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index c8e5642c5..4d46d4f88 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,5 +1,8 @@ package main.java; +import java.util.ArrayList; +import java.util.List; + public abstract class Task { protected String description; protected boolean isDone; @@ -11,7 +14,12 @@ public Task(String description) { this.isDone = false; } - public String getStatusIcon() { + // Check if task description contains a keyword + public boolean containsKeyword(String keyword) { + return description.toLowerCase().contains(keyword.toLowerCase()); + } + + public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 000000000..76fb30ac0 --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,62 @@ +package main.java; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.HashMap; + +public class TaskList { + private final List tasks; + + public TaskList() { + tasks = new ArrayList<>(); + } + + public TaskList(List tasks) { + this.tasks = tasks; + } + + public void addTask(Task task) { + tasks.add(task); + } + + public void deleteTask(int index) { + tasks.remove(index); + } + + public Task getTask(int index) { + return tasks.get(index); + } + + public int getSize() { + return tasks.size(); + } + + public ArrayList getTasks() { + return (ArrayList) tasks; + } + + // New method to find tasks by keyword + /*public ArrayList findTasksByKeywordWithIndex(String keyword) { + ArrayList matchingTasks = new ArrayList<>(); + for (Task task : tasks) { + if (task.getName().contains(keyword)) { + matchingTasks.add(task); // Keep task number + } + } + return matchingTasks; + }**/ + + public Map findTasksByKeywordWithIndex(String keyword) { + Map matchingTasks = new HashMap<>(); // Store index-task pairs + for (int i = 0; i < tasks.size(); i++) { + Task task = tasks.get(i); + if (task.getName().contains(keyword)) { + matchingTasks.put(i + 1, task); // Store the 1-based index + } + } + return matchingTasks; + } + + +} diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 000000000..f4bb12b80 --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,114 @@ +package main.java; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Ui { + private final Scanner sc; + + public Ui() { + this.sc = new Scanner(System.in); + } + + public void start() { + printLine(); + System.out.println("Hello! I'm KenChat"); + System.out.println("What can I do for you?"); + System.out.println("Use to show full list of commands."); + printLine(); + System.out.println(); + } + + public void end() { + printLine(); + System.out.println("Bye. Hope to see you again soon!"); + printLine(); + System.out.println(); + } + + public void printLine() { + System.out.println("____________________________________"); + } + + public String readCommand() { + return sc.nextLine(); + } + + public void showError(String message) { + printLine(); + System.out.println(message); + printLine(); + System.out.println(); + } + + public void showLoadingError() { + showError("Error loading data from file."); + } + + public void showTaskAdded(Task task, int size) { + printLine(); + System.out.println("Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println("Now you have " + size + " tasks in the list."); + printLine(); + System.out.println(); + } + + public void showTaskRemoved(Task task, int size) { + printLine(); + System.out.println("Noted. I've removed this task:"); + System.out.println(" " + task); + System.out.println("Now you have " + size + " tasks in the list."); + printLine(); + System.out.println(); + } + + public void showTaskMarked(Task task) { + printLine(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(" " + task); + printLine(); + System.out.println(); + } + + public void showTaskUnmarked(Task task) { + printLine(); + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(" " + task); + printLine(); + System.out.println(); + } + + public void showTasksList(ArrayList tasks) { + printLine(); + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + ". " + tasks.get(i)); + } + printLine(); + System.out.println(); + } + + public void showHelp() { + printLine(); + System.out.println("Valid commands:"); + System.out.println("1. todo - Adds a todo task."); + System.out.println("2. deadline /by - Adds a deadline task. can be date and/or time."); + System.out.println("3. event /from /to - Adds an event task. can be date and/or time."); + System.out.println("4. list - Displays all tasks in the list."); + System.out.println("5. mark - Marks the specified task as done."); + System.out.println("6. unmark - Marks the specified task as not done."); + System.out.println("7. delete - Deletes the specified task from the list."); + System.out.println("8. find - Find task(s) in the list by searching with keyword(s)."); + System.out.println("9. bye - Exits the program."); + System.out.println("10. help - Shows this help message."); + printLine(); + System.out.println(); + } + + public void showMessage(String message) { + System.out.println(message); + } + +} + diff --git a/src/main/java/UnmarkCommand.java b/src/main/java/UnmarkCommand.java new file mode 100644 index 000000000..57207fdac --- /dev/null +++ b/src/main/java/UnmarkCommand.java @@ -0,0 +1,23 @@ +package main.java; + +public class UnmarkCommand extends Command { + private final int index; + + public UnmarkCommand(int index) { + this.index = index - 1; + } + + @Override + public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { + if (tasks.getSize() == 0) { + throw new KenChatException(KenChatException.getTaskNotExistMessage()); + } + if (index < 0 || index >= tasks.getSize()) { + throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage()); + } + Task task = tasks.getTask(index); + task.markAsUndone(); + storage.save(tasks.getTasks()); + ui.showTaskUnmarked(task); + } +} From 4cc1ab28ecfab583b114695d2d9f1db88d73dd86 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Tue, 15 Oct 2024 14:36:32 +0800 Subject: [PATCH 20/26] Level-9 --- src/main/java/FindCommand.java | 3 --- src/main/java/Task.java | 8 -------- src/main/java/Ui.java | 2 +- 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/src/main/java/FindCommand.java b/src/main/java/FindCommand.java index 687aee711..1894b1b33 100644 --- a/src/main/java/FindCommand.java +++ b/src/main/java/FindCommand.java @@ -1,6 +1,5 @@ package main.java; -import java.util.ArrayList; import java.util.Map; public class FindCommand extends Command { @@ -12,8 +11,6 @@ public FindCommand(String keyword) { @Override public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { - //ArrayList matchingTasks = tasks.findTasksByKeywordWithIndex(keyword); - // Use the findTasksByKeywordWithIndex method that returns a Map of Map matchingTasks = tasks.findTasksByKeywordWithIndex(keyword); if (matchingTasks.isEmpty()) { diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 4d46d4f88..cdeca5e66 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,8 +1,5 @@ package main.java; -import java.util.ArrayList; -import java.util.List; - public abstract class Task { protected String description; protected boolean isDone; @@ -14,11 +11,6 @@ public Task(String description) { this.isDone = false; } - // Check if task description contains a keyword - public boolean containsKeyword(String keyword) { - return description.toLowerCase().contains(keyword.toLowerCase()); - } - public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index f4bb12b80..db198f115 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -105,7 +105,7 @@ public void showHelp() { printLine(); System.out.println(); } - + public void showMessage(String message) { System.out.println(message); } From 7211d209af2d79b127122d621c61db00c4ba33e5 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Tue, 15 Oct 2024 14:38:14 +0800 Subject: [PATCH 21/26] Level-9 --- src/main/java/KenChat.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 03a5fa42d..b96b70eb7 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -35,4 +35,4 @@ public void run() { public static void main(String[] args) throws KenChatException { new KenChat("data/DukeBot.txt").run(); } -} +} \ No newline at end of file From 48f5dab2419e126f38d95d8d538b769884f13333 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Tue, 15 Oct 2024 19:39:57 +0800 Subject: [PATCH 22/26] A-JavaDoc --- src/main/java/AddCommand.java | 12 ++++++++++++ src/main/java/Command.java | 18 ++++++++++++++++++ src/main/java/DeleteCommand.java | 12 ++++++++++++ 3 files changed, 42 insertions(+) diff --git a/src/main/java/AddCommand.java b/src/main/java/AddCommand.java index e05629884..42519afe5 100644 --- a/src/main/java/AddCommand.java +++ b/src/main/java/AddCommand.java @@ -1,5 +1,8 @@ package main.java; +/** + * Represents the command to add a task to the task list. + */ public class AddCommand extends Command { private final Task task; @@ -7,6 +10,15 @@ public AddCommand(Task task) { this.task = task; } + /** + * Perform the add command by adding the task to the task list, + * saving the task list and notifying the user. + * + * @param tasks The task list. + * @param ui The UI component which the user will see. + * @param storage The storage component to save the updated task list. + * @throws KenChatException If there is an error when saving the task list. + */ @Override public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { tasks.addTask(task); diff --git a/src/main/java/Command.java b/src/main/java/Command.java index 4be7365cb..2059dd536 100644 --- a/src/main/java/Command.java +++ b/src/main/java/Command.java @@ -1,9 +1,27 @@ package main.java; +/** + * Represents an abstract command that can be performed with the task management chat bot. + * Specific commands like add or delete will extend this class. + */ public abstract class Command { + /** + * Performs the command using the given task list, UI and storage components. + * + * @param tasks The task list that the command will work on. + * @param ui The UI component of the chat bot. + * @param storage The storage component of the chat bot. + * @throws KenChatException If there is an error when performing the command. + */ public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException; + /** + * Notifying the chat bat on its running status after performing the command. + * All commands will return true, except only when ExitCommand is called. + * + * @return true If the chat bot should continue running. + */ public boolean isRunning() { return true; } diff --git a/src/main/java/DeleteCommand.java b/src/main/java/DeleteCommand.java index 43eb2e804..378f4fe89 100644 --- a/src/main/java/DeleteCommand.java +++ b/src/main/java/DeleteCommand.java @@ -1,5 +1,8 @@ package main.java; +/** + * Represents a command to delete a task from the task list. + */ public class DeleteCommand extends Command { private final int index; @@ -7,6 +10,15 @@ public DeleteCommand(int index) { this.index = index - 1; } + /** + * Performs the delete command, removing the specific task from the task list, using the task index. + * This is followed by saving to the storage and notifying the user through a display from the UI. + * + * @param tasks The task list that the command will work on. + * @param ui The UI component of the chat bot. + * @param storage The storage component of the chat bot. + * @throws KenChatException If the task list is empty, or if the task number is invalid. + */ @Override public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { if (tasks.getSize() == 0) { From 165b854b01b6a79a06aca244084d5d8be83edd3d Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Tue, 15 Oct 2024 19:40:24 +0800 Subject: [PATCH 23/26] A-JavaDoc --- src/main/java/ExitCommand.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/ExitCommand.java b/src/main/java/ExitCommand.java index cc4937cd4..268102230 100644 --- a/src/main/java/ExitCommand.java +++ b/src/main/java/ExitCommand.java @@ -1,12 +1,25 @@ package main.java; +/** + * Represents a command to exit the chat bot. + */ public class ExitCommand extends Command { + /** + * Performs the exit command. No other action is required. + * + * @param tasks The task list that the command will work on. + * @param ui The UI component of the chat bot. + * @param storage The storage component of the chat bot. + */ @Override - public void execute(TaskList tasks, Ui ui, Storage storage) { - - } + public void execute(TaskList tasks, Ui ui, Storage storage) {} + /** + * Notify the chat bot to terminate. + * + * @return false so that the chat bot will stop running. + */ @Override public boolean isRunning() { return false; From b1675c01dc4040b5e39ecb5cb375d1ec5f927c24 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Tue, 15 Oct 2024 19:41:01 +0800 Subject: [PATCH 24/26] A-JavaDoc --- src/main/java/HelpCommand.java | 10 ++++++++++ src/main/java/ListCommand.java | 12 ++++++++++++ src/main/java/MarkCommand.java | 12 ++++++++++++ src/main/java/UnmarkCommand.java | 11 +++++++++++ 4 files changed, 45 insertions(+) diff --git a/src/main/java/HelpCommand.java b/src/main/java/HelpCommand.java index 5c2a193e9..b63708968 100644 --- a/src/main/java/HelpCommand.java +++ b/src/main/java/HelpCommand.java @@ -1,7 +1,17 @@ package main.java; +/** + * Represents a command to display help information to the user. + */ public class HelpCommand extends Command { + /** + * Performs the help command by displaying all the valid commands the user can enter. + * + * @param tasks The task list that the command will work on. + * @param ui The UI component of the chat bot. + * @param storage The storage component of the chat bot. + */ @Override public void execute(TaskList tasks, Ui ui, Storage storage) { ui.showHelp(); diff --git a/src/main/java/ListCommand.java b/src/main/java/ListCommand.java index 9430bbc04..221dfef0c 100644 --- a/src/main/java/ListCommand.java +++ b/src/main/java/ListCommand.java @@ -1,7 +1,19 @@ package main.java; +/** + * Represents a command to list all the tasks in the task list. + */ public class ListCommand extends Command { + /** + * Performs the list command, displaying all the tasks in the task list via the UI. + * If the task list is empty, an exception is thrown. + * + * @param tasks The task list that the command will work on. + * @param ui The UI component of the chat bot. + * @param storage The storage component of the chat bot. + * @throws KenChatException If the task list is empty, which will display that there is not task in the tasklist. + */ @Override public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { if (tasks.getSize() == 0) { diff --git a/src/main/java/MarkCommand.java b/src/main/java/MarkCommand.java index 5bbcfe7ae..3576c632a 100644 --- a/src/main/java/MarkCommand.java +++ b/src/main/java/MarkCommand.java @@ -1,5 +1,8 @@ package main.java; +/** + * Performs a command to mark a task as completed in the task list. + */ public class MarkCommand extends Command { private final int index; @@ -7,6 +10,15 @@ public MarkCommand(int index) { this.index = index - 1; } + /** + * Performs the mark command, marking the specific task as done. + * Saves the updated task list to the storage and notifying the user through display via the UI + * + * @param tasks The task list that the command will work on. + * @param ui The UI component of the chat bot. + * @param storage The storage component of the chat bot. + * @throws KenChatException If the task list is empty or the index is invalid. + */ @Override public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { if (tasks.getSize() == 0) { diff --git a/src/main/java/UnmarkCommand.java b/src/main/java/UnmarkCommand.java index 57207fdac..08c164766 100644 --- a/src/main/java/UnmarkCommand.java +++ b/src/main/java/UnmarkCommand.java @@ -1,5 +1,8 @@ package main.java; +/** + * Represents a command to unmark a task as not completed. + */ public class UnmarkCommand extends Command { private final int index; @@ -7,6 +10,14 @@ public UnmarkCommand(int index) { this.index = index - 1; } + /** + * Performs the unmark command indicating that the task is not yet completed.\ + * + * @param tasks The task list that the command will work on. + * @param ui The UI component of the chat bot. + * @param storage The storage component of the chat bot. + * @throws KenChatException If the task list is empty or the index is invalid. + */ @Override public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { if (tasks.getSize() == 0) { From ddcb0a25f792555e63052dadc7a90a37c1acc5b1 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Tue, 15 Oct 2024 19:42:14 +0800 Subject: [PATCH 25/26] A-JavaDoc --- src/main/java/KenChat.java | 23 ++++++++++-- src/main/java/KenChatException.java | 15 ++++++++ src/main/java/Parser.java | 12 +++++- src/main/java/Storage.java | 23 ++++++++++++ src/main/java/Task.java | 37 ++++++++++++++++++- src/main/java/TaskList.java | 20 +++++++++- src/main/java/Ui.java | 57 +++++++++++++++++++++++++++-- 7 files changed, 178 insertions(+), 9 deletions(-) diff --git a/src/main/java/KenChat.java b/src/main/java/KenChat.java index 03a5fa42d..f85a9f07a 100644 --- a/src/main/java/KenChat.java +++ b/src/main/java/KenChat.java @@ -1,10 +1,18 @@ package main.java; +/** + * The main class for KenChat application, managing the UI, task storage and command execution. + */ public class KenChat { private final Storage storage; private TaskList tasks; private final Ui ui; + /** + * Initialises the KenChat application with the specific file path for storage. + * + * @param filePath The file path where task list is stored. + */ public KenChat(String filePath) { ui = new Ui(); storage = new Storage(filePath); @@ -16,8 +24,11 @@ public KenChat(String filePath) { } } + /** + * Runs the KenChat application, processing user commands until exit is prompted. + */ public void run() { - ui.start(); + ui.showStart(); boolean isRunning = true; while (isRunning) { try { @@ -29,10 +40,16 @@ public void run() { ui.showError(e.getMessage()); } } - ui.end(); + ui.showEnd(); } + /** + * The main entry point of the KenChat application. + * + * @param args Command line arguments (not used). + * @throws KenChatException If there is an error during initialization. + */ public static void main(String[] args) throws KenChatException { - new KenChat("data/DukeBot.txt").run(); + new KenChat("data/KenChat.txt").run(); } } diff --git a/src/main/java/KenChatException.java b/src/main/java/KenChatException.java index 5c52fca58..ae72e22d4 100644 --- a/src/main/java/KenChatException.java +++ b/src/main/java/KenChatException.java @@ -1,10 +1,19 @@ package main.java; +/** + * Exception class for the KenChat application + */ public class KenChatException extends Exception { public KenChatException(String message) { super(message); } + + /** + * Returns an error message because of an empty description for a specific command + * @param command The command that has the missing description. + * @return The error message. + */ public static String getEmptyDescriptionMessage(String command) { return "Description is missing! Enter a description for the command. Use: " + command + " "; } @@ -33,6 +42,12 @@ public static String getTaskNumberDoesNotExistMessage() { return "That task number does not exist. Use to see full task list."; } + /** + * Returns an error message because of a missing task number for Mark/Unmark/Delete command. + * + * @param action The command that is supposed to carry out the action of Mark/Unmark/Delete. + * @return The error message. + */ public static String getEmptyTaskNumberMessage(String action) { return "Task number is missing!! Please specify the task number to " + action + "."; } diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 97fb2bbdc..faf73b2df 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -1,10 +1,20 @@ package main.java; +/** + * Parses user input and creates the matching command objects. + */ public class Parser { + /** + * Parses the user input and returns the corresponding Command. + * + * @param userInput The input command string from the user. + * @return The Command object corresponding to the user input. + * @throws KenChatException If the input is invalid or commands are wrong. + */ public static Command parse(String userInput) throws KenChatException { userInput = userInput.trim(); - if (userInput.isEmpty()) { // Check for null command + if (userInput.isEmpty()) { // Check for empty command throw new KenChatException(KenChatException.emptyCommand()); } diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index 32764ce0a..366491cfd 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -6,6 +6,10 @@ import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; + +/** + * Handles load and save to a file + */ public class Storage { private final String filePath; @@ -13,6 +17,12 @@ public Storage(String filePath) { this.filePath = filePath; } + /** + * Loads tasks from the file into an ArrayList + * + * @return An ArrayList of tasks loaded from the file + * @throws KenChatException If there is an error when loading tasks. + */ public ArrayList load() throws KenChatException { ArrayList doList = new ArrayList<>(); File file = new File(filePath); @@ -34,6 +44,13 @@ public ArrayList load() throws KenChatException { return doList; } + /** + * Parses a line from the file and returns a matching Task object + * + * @param line The line of text from the file which represents a task. + * @return The matching task object. + * @throws KenChatException If the data format is wrong/corrupted. + */ private static Task getTask(String line) throws KenChatException { String[] parts = line.split(" \\| "); Task task; @@ -56,6 +73,12 @@ private static Task getTask(String line) throws KenChatException { return task; } + /** + * Saves the current list of tasks to the file. + * + * @param doList The task list to save. + * @throws KenChatException If there is an error when saving tasks. + */ public void save(ArrayList doList) throws KenChatException { File file = new File(filePath); diff --git a/src/main/java/Task.java b/src/main/java/Task.java index c8e5642c5..95a22e56b 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,5 +1,8 @@ package main.java; +/** + * Represents a task with a description/name and its completion status. + */ public abstract class Task { protected String description; protected boolean isDone; @@ -11,10 +14,20 @@ public Task(String description) { this.isDone = false; } + /** + * Returns the task's completion status icon. + * + * @return "X" if task is completed. + */ public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + /** + * Returns the description/name of the task. + * + * @return The task description. + */ public String getName() { return description; } @@ -27,10 +40,20 @@ public void markAsUndone(){ this.isDone = false; } - public String toString() { return "["+ getStatusIcon() +"] "+getName(); } + /** + * Returns a formatted string of the task. + * + * @return The string of the task. + */ + public String toString() {return "["+ getStatusIcon() +"] "+getName();} // ToDo class public static class ToDo extends Task { + /** + * Create a ToDo task with the specific description. + * + * @param description The description of the ToDo task. + */ public ToDo(String description) { super(description); } @@ -48,6 +71,12 @@ public String formatForStorage() { public static class Deadline extends Task { public String dueDate; + /** + * Creates a Deadline task with the specific description and due date. + * + * @param description The description of the Deadline task. + * @param dueDate The due date of the Deadline task. + */ public Deadline(String description, String dueDate) { super(description); this.dueDate = dueDate; @@ -66,6 +95,12 @@ public static class Event extends Task { public String startDate; public String endDate; + /** + * Creates an Event task with the specific description, start date, and end date. + * @param description The description of the Event task. + * @param startDate The start date of the Event task. + * @param endDate The end date of the Event task. + */ public Event(String description, String startDate, String endDate) { super(description); this.startDate = startDate; diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index d9afa7f2c..4d8f51b10 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -3,6 +3,9 @@ import java.util.ArrayList; import java.util.List; +/** + * Represents a list of tasks. + */ public class TaskList { private final List tasks; @@ -18,14 +21,30 @@ public void addTask(Task task) { tasks.add(task); } + /** + * Deletes a task from the task list at the specific index. + * + * @param index The index of the task to be deleted. + */ public void deleteTask(int index) { tasks.remove(index); } + /** + * Retrieves the task at the specific index in the task list. + * + * @param index The index of the task to retrieve. + * @return The task at the specific index. + */ public Task getTask(int index) { return tasks.get(index); } + /** + * Returns the number of tasks in the task list. + * + * @return The size of the task list. + */ public int getSize() { return tasks.size(); } @@ -33,5 +52,4 @@ public int getSize() { public ArrayList getTasks() { return (ArrayList) tasks; } - } diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 4313bed28..eb5a88629 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -3,6 +3,9 @@ import java.util.ArrayList; import java.util.Scanner; +/** + * Handles the user interface and interactions with the user. + */ public class Ui { private final Scanner sc; @@ -10,7 +13,10 @@ public Ui() { this.sc = new Scanner(System.in); } - public void start() { + /** + * Displays the start message to the user. + */ + public void showStart() { printLine(); System.out.println("Hello! I'm DukeBot"); System.out.println("What can I do for you?"); @@ -18,7 +24,10 @@ public void start() { System.out.println(); } - public void end() { + /** + * Displays the end message to the user. + */ + public void showEnd() { printLine(); System.out.println("Bye. Hope to see you again soon!"); printLine(); @@ -29,10 +38,20 @@ public void printLine() { System.out.println("____________________________________"); } + /** + * Reads and returns the command entered by the user. + * + * @return The user input. + */ public String readCommand() { return sc.nextLine(); } + /** + * Displays the given error message to the user. + * + * @param message The error message to display. + */ public void showError(String message) { printLine(); System.out.println(message); @@ -40,10 +59,19 @@ public void showError(String message) { System.out.println(); } + /** + * Displays an error message for failure to load data. + */ public void showLoadingError() { showError("Error loading data from file."); } + /** + * Displays a message indicating that a task has been added to the list. + * + * @param task The task that was added. + * @param size The current number of tasks in the list. + */ public void showTaskAdded(Task task, int size) { printLine(); System.out.println("Got it. I've added this task:"); @@ -53,6 +81,12 @@ public void showTaskAdded(Task task, int size) { System.out.println(); } + /** + * Displays a message indicating that a task has been removed from the list. + * + * @param task The task that was removed. + * @param size The current number of tasks in the list. + */ public void showTaskRemoved(Task task, int size) { printLine(); System.out.println("Noted. I've removed this task:"); @@ -62,6 +96,11 @@ public void showTaskRemoved(Task task, int size) { System.out.println(); } + /** + * Displays a message indicating that a task has been marked as completed. + * + * @param task The task that was marked as done. + */ public void showTaskMarked(Task task) { printLine(); System.out.println("Nice! I've marked this task as done:"); @@ -70,6 +109,11 @@ public void showTaskMarked(Task task) { System.out.println(); } + /** + * Displays a message indicating that a task has been marked as not completed. + * + * @param task The task that was unmarked. + */ public void showTaskUnmarked(Task task) { printLine(); System.out.println("OK, I've marked this task as not done yet:"); @@ -78,6 +122,11 @@ public void showTaskUnmarked(Task task) { System.out.println(); } + /** + * Displays the list of tasks. + * + * @param tasks The list of tasks to display. + */ public void showTasksList(ArrayList tasks) { printLine(); System.out.println("Here are the tasks in your list:"); @@ -88,6 +137,9 @@ public void showTasksList(ArrayList tasks) { System.out.println(); } + /** + * Displays the list of valid commands for the user. + */ public void showHelp() { printLine(); System.out.println("Valid commands:"); @@ -103,6 +155,5 @@ public void showHelp() { printLine(); System.out.println(); } - } From e193e2e2d1ce526c445c664e174c1e19aa2a7a80 Mon Sep 17 00:00:00 2001 From: SemiColonKen Date: Tue, 15 Oct 2024 21:17:19 +0800 Subject: [PATCH 26/26] Added JavaDoc for FindCommand --- src/main/java/FindCommand.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/FindCommand.java b/src/main/java/FindCommand.java index 1894b1b33..f77f49dcf 100644 --- a/src/main/java/FindCommand.java +++ b/src/main/java/FindCommand.java @@ -2,6 +2,9 @@ import java.util.Map; +/** + * Represents a command to find tasks in the task list according to keyword(s). + */ public class FindCommand extends Command { private final String keyword; @@ -9,6 +12,15 @@ public FindCommand(String keyword) { this.keyword = keyword; } + /** + * Performs the find command to search for tasks containing the specific keyword(s). + * + * + * @param tasks The task list that the command will work on. + * @param ui The UI component of the chat bot. + * @param storage The storage component of the chat bot. + * @throws KenChatException If an error occurs while performing the command. + */ @Override public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException { // Use the findTasksByKeywordWithIndex method that returns a Map of