From a1fb5b38852c099c29cf67d489d5280a213c1b54 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Wed, 25 Jan 2023 13:40:16 +0800 Subject: [PATCH 01/27] no message --- src/main/java/Duke.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..fdc052e70 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,7 @@ public class Duke { public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + System.out.println("Hello! I'm Duke"); + System.out.println("What can I do for you?"); + System.out.println("Bye. Hope to see you again soon!"); } } From 50e943829988ec3ada29b2e76d8cff3191a38488 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Mon, 30 Jan 2023 22:06:58 +0800 Subject: [PATCH 02/27] Revert "no message" This reverts commit a1fb5b38852c099c29cf67d489d5280a213c1b54. --- src/main/java/Duke.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index fdc052e70..5d313334c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,10 @@ public class Duke { public static void main(String[] args) { - System.out.println("Hello! I'm Duke"); - System.out.println("What can I do for you?"); - System.out.println("Bye. Hope to see you again soon!"); + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); } } From 8f06dc1c925be8bfef21559ba303c10bd10814fa Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Mon, 30 Jan 2023 22:09:32 +0800 Subject: [PATCH 03/27] no message --- src/main/java/Duke.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..fdc052e70 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,7 @@ public class Duke { public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + System.out.println("Hello! I'm Duke"); + System.out.println("What can I do for you?"); + System.out.println("Bye. Hope to see you again soon!"); } } From 15e6f918468c73d8692d36b324b7fd7807c9a6e9 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Mon, 30 Jan 2023 22:09:48 +0800 Subject: [PATCH 04/27] Revert "no message" This reverts commit 8f06dc1c925be8bfef21559ba303c10bd10814fa. --- src/main/java/Duke.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index fdc052e70..5d313334c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,10 @@ public class Duke { public static void main(String[] args) { - System.out.println("Hello! I'm Duke"); - System.out.println("What can I do for you?"); - System.out.println("Bye. Hope to see you again soon!"); + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); } } From f19569e3265707d1b2cd037cf7cd675ef0298db4 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Mon, 30 Jan 2023 22:11:31 +0800 Subject: [PATCH 05/27] Level 0 --- src/main/java/Duke.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..fdc052e70 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,7 @@ public class Duke { public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + System.out.println("Hello! I'm Duke"); + System.out.println("What can I do for you?"); + System.out.println("Bye. Hope to see you again soon!"); } } From aeb3c02d13e753d7e107a4cd53a3b4baea8e5266 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Tue, 31 Jan 2023 17:21:02 +0800 Subject: [PATCH 06/27] Level 1 --- src/main/java/Duke.java | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index fdc052e70..88fb6eebe 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,22 @@ +import java.util.Scanner; public class Duke { public static void main(String[] args) { - System.out.println("Hello! I'm Duke"); - System.out.println("What can I do for you?"); - System.out.println("Bye. Hope to see you again soon!"); + System.out.println("Hello! I'm Duke \nWhat can I do for you?"); + + boolean isFinished = false; + + String userInput; + Scanner scan = new Scanner(System.in); + + while (!isFinished) { + userInput = scan.nextLine(); + + if (userInput.equals("bye")) { + System.out.println("Bye. Hope to see you again soon!"); + isFinished = true; + } else { + System.out.println(userInput); + } + } } } From 8b22744a7a54f23016118d5a5d2472d275bc95c5 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Tue, 31 Jan 2023 17:39:19 +0800 Subject: [PATCH 07/27] Level 2 --- src/main/java/Duke.java | 6 +++++- src/main/java/List.java | 21 +++++++++++++++++++++ src/main/java/Task.java | 11 +++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/main/java/List.java create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 88fb6eebe..292a491d1 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -4,6 +4,7 @@ public static void main(String[] args) { System.out.println("Hello! I'm Duke \nWhat can I do for you?"); boolean isFinished = false; + List toDoList = new List(); String userInput; Scanner scan = new Scanner(System.in); @@ -14,8 +15,11 @@ public static void main(String[] args) { if (userInput.equals("bye")) { System.out.println("Bye. Hope to see you again soon!"); isFinished = true; + } else if (userInput.equals("list")) { + toDoList.printList(); } else { - System.out.println(userInput); + toDoList.addTask(userInput); + System.out.println("added: " + userInput); } } } diff --git a/src/main/java/List.java b/src/main/java/List.java new file mode 100644 index 000000000..8df19fa1e --- /dev/null +++ b/src/main/java/List.java @@ -0,0 +1,21 @@ +import java.util.ArrayList; + + public class List { + private ArrayList toDoList; + + public List() { + this.toDoList = new ArrayList(); + } + + public void addTask(String userInput) { + toDoList.add(new Task(userInput)); + } + + public void printList() { + int counter = 1; + for (Task task : toDoList) { + System.out.println(counter + ". " + task.printName()); + counter++; + } + } + } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..91ccd0248 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,11 @@ +public class Task { + private String name; + + public Task(String name) { + this.name = name; + } + + public String printName() { + return name; + } +} From 1feb6e5a5caa4f175a23d87099dcad0cd6f6e7e7 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Wed, 1 Feb 2023 14:24:16 +0800 Subject: [PATCH 08/27] Level 3 --- src/main/java/Duke.java | 46 +++++++++++++++++++++++++++-------------- src/main/java/Task.java | 12 ++++++----- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 292a491d1..36209a543 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,26 +1,42 @@ import java.util.Scanner; +import java.util.ArrayList; public class Duke { public static void main(String[] args) { System.out.println("Hello! I'm Duke \nWhat can I do for you?"); - - boolean isFinished = false; - List toDoList = new List(); + Scanner in = new Scanner(System.in); + ArrayList taskList = new ArrayList(); String userInput; - Scanner scan = new Scanner(System.in); - - while (!isFinished) { - userInput = scan.nextLine(); + while(in.hasNext()){ - if (userInput.equals("bye")) { - System.out.println("Bye. Hope to see you again soon!"); - isFinished = true; - } else if (userInput.equals("list")) { - toDoList.printList(); + userInput = in.nextLine(); + if(userInput.equals("bye")){ + break; + } + if(userInput.equals("list")){ + for(int i = 0; i Date: Wed, 1 Feb 2023 20:01:43 +0800 Subject: [PATCH 09/27] Level 4 --- src/main/java/Deadline.java | 14 +++++++++ src/main/java/Duke.java | 59 ++++++++++++++++++++++++++++++++----- src/main/java/Event.java | 15 ++++++++++ src/main/java/List.java | 21 ------------- src/main/java/Task.java | 16 +++++----- src/main/java/Todo.java | 8 +++++ 6 files changed, 97 insertions(+), 36 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java delete mode 100644 src/main/java/List.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..6f5231892 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,14 @@ +public class Deadline extends Task { + + protected String due; + + public Deadline(String info, String due) { + super(info); + this.due = due; + } + + @Override + public String toString(){ + return "[D]" + super.toString() + "(by: " + due + ")"; + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 36209a543..883e3b538 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -13,27 +13,70 @@ public static void main(String[] args) { if(userInput.equals("bye")){ break; } - if(userInput.equals("list")){ - for(int i = 0; i toDoList; - - public List() { - this.toDoList = new ArrayList(); - } - - public void addTask(String userInput) { - toDoList.add(new Task(userInput)); - } - - public void printList() { - int counter = 1; - for (Task task : toDoList) { - System.out.println(counter + ". " + task.printName()); - counter++; - } - } - } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 6882fac33..59d789695 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,13 +1,15 @@ public class Task { protected String name; protected boolean isCompleted; - public Task(String name, boolean isCompleted) { - this.name = name; + + public Task(String info) { + this.name = info; + } + public Task(String info, boolean isCompleted) { + this.name = info; isCompleted = false; } - - public String markTask (){ - return(isCompleted?"[X]":"[ ]"); + public String toString () { + return(isCompleted?"[X]"+this.name:"[ ]"+this.name); } -} - +} \ No newline at end of file diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..c17a7246b --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,8 @@ +public class Todo extends Task { + public Todo (String info) { + super (info); + } + public String toString() { + return "[T]" + super.toString(); + } +} From 1bf5e36b9a69a67b93847300bdba31f74fd77162 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Thu, 9 Feb 2023 21:43:34 +0800 Subject: [PATCH 10/27] A-CodingStandard --- src/main/java/Duke.java | 68 ++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 883e3b538..28070bb7a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,33 +1,34 @@ import java.util.Scanner; import java.util.ArrayList; +import java.security.InvalidAlgorithmParameterException; +import java.util.MissingFormatArgumentException; public class Duke { public static void main(String[] args) { System.out.println("Hello! I'm Duke \nWhat can I do for you?"); Scanner in = new Scanner(System.in); - ArrayList taskList = new ArrayList(); + ArrayList tasks = new ArrayList(); String userInput; while(in.hasNext()){ - userInput = in.nextLine(); if(userInput.equals("bye")){ + System.out.println("Bye. Hope to see you again soon!"); break; - } - if(userInput.equals("list")) { - for (int i = 0; i < taskList.size(); i++) { + } else if(userInput.contains("list")) { + for (int i = 0; i < tasks.size(); i++) { System.out.print(i + 1 + "."); - System.out.println(taskList.get(i).toString()); + System.out.println(tasks.get(i).toString()); } } else if (userInput.contains("todo")) { String info = userInput.substring(5).trim(); - taskList.add(new Todo(info)); - int finalTask = taskList.size() - 1; + tasks.add(new Todo(info)); + int finalTask = tasks.size() - 1; System.out.println("Got it. I've added this task:"); - System.out.println(taskList.get(finalTask).toString()); - if (taskList.size() == 1){ - System.out.println("Now you have " +taskList.size()+" task in the list."); + System.out.println(tasks.get(finalTask).toString()); + if (tasks.size() == 1) { + System.out.println("Now you have " +tasks.size()+" task in the list."); } else { - System.out.println("Now you have " + taskList.size() + " tasks in the list."); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } } else if (userInput.contains("deadline")) { String [] listArray = userInput.split("/",2); @@ -35,14 +36,14 @@ public static void main(String[] args) { String dueDate = listArray[1]; String info = description.substring(8).trim(); String due = dueDate.substring(3).trim(); - taskList.add(new Deadline(info, due)); - int finalTask = taskList.size() - 1; + tasks.add(new Deadline(info, due)); + int finalTask = tasks.size() - 1; System.out.println("Got it. I've added this task:"); - System.out.println(taskList.get(finalTask).toString()); - if (taskList.size() == 1){ - System.out.println("Now you have " +taskList.size()+" task in the list."); + System.out.println(tasks.get(finalTask).toString()); + if (tasks.size() == 1) { + System.out.println("Now you have " + tasks.size() + " task in the list."); } else { - System.out.println("Now you have " + taskList.size() + " tasks in the list."); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } } else if (userInput.contains("event")) { String [] listArray = userInput.split("/",3); @@ -52,34 +53,33 @@ public static void main(String[] args) { String info = description.substring(6).trim(); String start = startTime.substring(5).trim(); String end = endTime.substring(3).trim(); - taskList.add(new Event(info, start, end)); - int finalTask = taskList.size() - 1; + tasks.add(new Event(info, start, end)); + int finalTask = tasks.size() - 1; System.out.println("Got it. I've added this task:"); - System.out.println(taskList.get(finalTask).toString()); - if (taskList.size() == 1){ - System.out.println("Now you have " +taskList.size()+" task in the list."); + System.out.println(tasks.get(finalTask).toString()); + if (tasks.size() == 1){ + System.out.println("Now you have " + tasks.size() + " task in the list."); } else { - System.out.println("Now you have " + taskList.size() + " tasks in the list."); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } - } else if(userInput.contains("unmark") || userInput.contains("mark")){ + } else if(userInput.contains("mark")){ Integer itemNumber; - String [] IndexArr = userInput.split(" ",2); + String [] commandIndex = userInput.split(" ",2); itemNumber = Integer.parseInt(IndexArr[1])-1; - if (userInput.contains("unmark")) { + if (userInput.equals("unmark")) { System.out.println("OK, I've marked this task as not done yet:"); - taskList.get((int) itemNumber).isCompleted = false; - System.out.println(taskList.get(itemNumber).toString()); + tasks.get((int) itemNumber).isCompleted = false; + System.out.println(tasks.get(itemNumber).toString()); } else { System.out.println("Nice! I've marked this task as done:"); - taskList.get(itemNumber).isCompleted = true; - System.out.println(taskList.get(itemNumber).toString()); + tasks.get(itemNumber).isCompleted = true; + System.out.println(tasks.get(itemNumber).toString()); } } else { - taskList.add(new Task(userInput,false)); - System.out.println("added: "+userInput); + tasks.add(new Task(userInput, false)); + System.out.println("added: " + userInput); } } - System.out.println("Bye. Hope to see you again soon!"); } } \ No newline at end of file From 3d6f6e3b3de2475469a8242f3f7ec2f1c99a4b50 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Thu, 9 Feb 2023 21:44:51 +0800 Subject: [PATCH 11/27] A-CodingQuality --- src/main/java/Deadline.java | 3 --- src/main/java/Task.java | 1 - 2 files changed, 4 deletions(-) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 6f5231892..7a1f6ac34 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,12 +1,9 @@ public class Deadline extends Task { - protected String due; - public Deadline(String info, String due) { super(info); this.due = due; } - @Override public String toString(){ return "[D]" + super.toString() + "(by: " + due + ")"; diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 59d789695..68a6b6219 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,7 +1,6 @@ public class Task { protected String name; protected boolean isCompleted; - public Task(String info) { this.name = info; } From 875ec030ea66ff662f89b0d4980cf04ff42fac33 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Thu, 16 Feb 2023 15:21:17 +0800 Subject: [PATCH 12/27] Level-5 --- src/main/java/Duke.java | 191 +++++++++++++++++++------------ src/main/java/DukeException.java | 5 + 2 files changed, 122 insertions(+), 74 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 28070bb7a..ae06cb4ac 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,85 +1,128 @@ import java.util.Scanner; import java.util.ArrayList; -import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidParameterException; import java.util.MissingFormatArgumentException; public class Duke { public static void main(String[] args) { System.out.println("Hello! I'm Duke \nWhat can I do for you?"); Scanner in = new Scanner(System.in); ArrayList tasks = new ArrayList(); - - String userInput; - while(in.hasNext()){ - userInput = in.nextLine(); - if(userInput.equals("bye")){ - System.out.println("Bye. Hope to see you again soon!"); - break; - } else if(userInput.contains("list")) { - for (int i = 0; i < tasks.size(); i++) { - System.out.print(i + 1 + "."); - System.out.println(tasks.get(i).toString()); - } - } else if (userInput.contains("todo")) { - String info = userInput.substring(5).trim(); - tasks.add(new Todo(info)); - int finalTask = tasks.size() - 1; - System.out.println("Got it. I've added this task:"); - System.out.println(tasks.get(finalTask).toString()); - if (tasks.size() == 1) { - System.out.println("Now you have " +tasks.size()+" task in the list."); - } else { - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - } else if (userInput.contains("deadline")) { - String [] listArray = userInput.split("/",2); - String description = listArray[0]; - String dueDate = listArray[1]; - String info = description.substring(8).trim(); - String due = dueDate.substring(3).trim(); - tasks.add(new Deadline(info, due)); - int finalTask = tasks.size() - 1; - System.out.println("Got it. I've added this task:"); - System.out.println(tasks.get(finalTask).toString()); - if (tasks.size() == 1) { - System.out.println("Now you have " + tasks.size() + " task in the list."); - } else { - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - } else if (userInput.contains("event")) { - String [] listArray = userInput.split("/",3); - String description = listArray[0]; - String startTime = listArray[1]; - String endTime = listArray[2]; - String info = description.substring(6).trim(); - String start = startTime.substring(5).trim(); - String end = endTime.substring(3).trim(); - tasks.add(new Event(info, start, end)); - int finalTask = tasks.size() - 1; - System.out.println("Got it. I've added this task:"); - System.out.println(tasks.get(finalTask).toString()); - if (tasks.size() == 1){ - System.out.println("Now you have " + tasks.size() + " task in the list."); - } else { - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - } else if(userInput.contains("mark")){ - Integer itemNumber; - String [] commandIndex = userInput.split(" ",2); - itemNumber = Integer.parseInt(IndexArr[1])-1; - if (userInput.equals("unmark")) { - System.out.println("OK, I've marked this task as not done yet:"); - tasks.get((int) itemNumber).isCompleted = false; - System.out.println(tasks.get(itemNumber).toString()); - } - else { - System.out.println("Nice! I've marked this task as done:"); - tasks.get(itemNumber).isCompleted = true; - System.out.println(tasks.get(itemNumber).toString()); - } - } else { - tasks.add(new Task(userInput, false)); - System.out.println("added: " + userInput); - } + while (true) { + String userInput = in.nextLine(); + processUserInput(tasks, userInput); } } + private static String[] splitActionAndDescription(String userInput) { + String[] actionAndDescription = userInput.split(" ", 2); + return actionAndDescription.length == 2 ? actionAndDescription : new String[]{actionAndDescription[0], ""}; + } + private static void processUserInput(ArrayList tasks, String userInput) { + String[] actionAndDescription = splitActionAndDescription(userInput); + String action = actionAndDescription[0]; + String description = actionAndDescription[1]; + switch (action) { + case "bye": + System.out.println("Bye. Hope to see you again soon!"); + break; + case "list": + executeListAction(tasks); + break; + case "mark": + executeMarkAction(tasks, description); + break; + case "unmark": + executeUnmarkAction(tasks, description); + break; + case "todo": + executeTodoAction(tasks, description); + break; + case "event": + executeEventAction(tasks, description); + break; + case "deadline": + executeDeadlineAction(tasks, description); + break; + default: + System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-("); + } + } + private static void executeListAction(ArrayList tasks) { + for (int i = 0; i < tasks.size(); i++) { + System.out.print(i + 1 + "."); + System.out.println(tasks.get(i).toString()); + } + } + private static void executeMarkAction(ArrayList tasks, String description) { + Integer index = Integer.parseInt(description) - 1; + tasks.get(index).isCompleted = true; + System.out.println("Nice! I've marked this task as done:"); + System.out.println(tasks.get(index).toString()); + } + private static void executeUnmarkAction(ArrayList tasks, String description) { + Integer index = Integer.parseInt(description) - 1; + tasks.get(index).isCompleted = false; + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(tasks.get(index).toString()); + } + private static void printAcknowledgement(ArrayList tasks) { + int lastIndexOfTasks = tasks.size() - 1; + System.out.println("Got it. I've added this task:"); + System.out.println(tasks.get(lastIndexOfTasks).toString()); + if (tasks.size() == 1){ + System.out.println("Now you have " +tasks.size()+" task in the list."); + } else { + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + } + } + private static void executeTodoAction(ArrayList tasks, String description) { + try { + addTodoToList(tasks, description); + printAcknowledgement(tasks); + } catch (Exception e){ + System.out.println("Unable to add todo: No tasks given."); + } + } + private static void addTodoToList(ArrayList tasks, String description) throws DukeException { + if(description==""){ + throw new DukeException(new IllegalArgumentException()); + } + tasks.add(new Todo(description)); + } + private static void executeEventAction(ArrayList tasks, String description) { + try { + String[] indexArr = splitDescriptionEvent(description); + String name = indexArr[0].trim(); + String fromDate = indexArr[1].substring(5).trim(); + String toDate = indexArr[2].substring(3).trim(); + tasks.add(new Event(description, fromDate, toDate)); + printAcknowledgement(tasks); + }catch (DukeException e){ + System.out.println("Not enough commands to execute \"event\""); + } + } + private static String[] splitDescriptionEvent(String description) throws DukeException { + String[] indexArr = description.split("/", 3); + if(indexArr.length < 3){ + throw new DukeException(new IllegalArgumentException()); + } + return indexArr; + } + private static void executeDeadlineAction(ArrayList tasks, String description) { + try { + String[] indexArr = splitDescriptionDeadline(description); + String name = indexArr[0].trim(); + String byDate = indexArr[1].substring(3).trim(); + tasks.add(new Deadline(description, byDate)); + printAcknowledgement(tasks); + }catch(DukeException e){ + System.out.println("Not enough commands to execute \"deadline\""); + } + } + private static String[] splitDescriptionDeadline(String description) throws DukeException { + String[] indexArr = description.split("/", 2); + if(indexArr.length < 2){ + throw new DukeException(new IllegalArgumentException()); + } + return indexArr; + } } \ No newline at end of file diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..4c6b3c98a --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,5 @@ +public class DukeException extends Exception { + public DukeException(Throwable cause) { + super(cause); + } +} From 2f9f975b21cd9fc796ff007b5792716075e5e5ee Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Wed, 22 Feb 2023 19:48:57 +0800 Subject: [PATCH 13/27] Level-6 --- src/main/java/Duke.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ae06cb4ac..1a09a3be5 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -42,6 +42,9 @@ private static void processUserInput(ArrayList tasks, String userInput) { case "deadline": executeDeadlineAction(tasks, description); break; + case "delete": + executeDeleteAction(tasks, description); + break; default: System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-("); } @@ -69,7 +72,7 @@ private static void printAcknowledgement(ArrayList tasks) { System.out.println("Got it. I've added this task:"); System.out.println(tasks.get(lastIndexOfTasks).toString()); if (tasks.size() == 1){ - System.out.println("Now you have " +tasks.size()+" task in the list."); + System.out.println("Now you have " + tasks.size()+ " task in the list."); } else { System.out.println("Now you have " + tasks.size() + " tasks in the list."); } @@ -125,4 +128,16 @@ private static String[] splitDescriptionDeadline(String description) throws Duke } return indexArr; } + private static void executeDeleteAction(ArrayListtasks, String description) { + Integer index = Integer.parseInt(description) - 1; + String deletedTask = tasks.get(index).name; + tasks.remove((int)index); + System.out.println("Noted. I've removed this task:"); + System.out.println(deletedTask); + if (tasks.size() == 1){ + System.out.println("Now you have " + tasks.size()+ " task in the list."); + } else { + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + } + } } \ No newline at end of file From 6bdd653d213ea1c788e2d516f08a34465c2c53ac Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Wed, 22 Feb 2023 20:36:22 +0800 Subject: [PATCH 14/27] Level-7 --- duke.txt | 13 +++ src/main/java/Duke.java | 172 ++++++++++++++++++++--------- src/main/java/META-INF/MANIFEST.MF | 3 + 3 files changed, 134 insertions(+), 54 deletions(-) create mode 100644 duke.txt create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/duke.txt b/duke.txt new file mode 100644 index 000000000..26101aa2d --- /dev/null +++ b/duke.txt @@ -0,0 +1,13 @@ +todo cs2113 +todo cg2023 +list +mark 1 +todo cg2023 +todo cs2113 +list +mark 2 +mark 3 +mark 4 +event project meeting from 2pm to 4pm +event project meeting /from Mon 2pm /to 4pm +deadline return book /by Sunday diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ae06cb4ac..74063b11c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,126 +1,190 @@ import java.util.Scanner; import java.util.ArrayList; -import java.security.InvalidParameterException; -import java.util.MissingFormatArgumentException; +import java.io.File; +import java.io.FileWriter; +import java.io.FileNotFoundException; +import java.io.IOException; public class Duke { public static void main(String[] args) { + File F = new File("src/main/java/duke.txt"); + if (!F.exists()) { + F = new File("duke.txt"); + } System.out.println("Hello! I'm Duke \nWhat can I do for you?"); + boolean fileDoesExist; Scanner in = new Scanner(System.in); - ArrayList tasks = new ArrayList(); + ArrayList tasks = new ArrayList<>(); + ArrayList inputCommands = new ArrayList<>(); + try { + fileDoesExist = false; + Scanner s = new Scanner(F); + while (s.hasNext()) { + processUserInput(tasks,inputCommands,s.nextLine(),F,fileDoesExist); + } + } catch(FileNotFoundException e) { + System.out.println("File does not exist."); + } while (true) { String userInput = in.nextLine(); - processUserInput(tasks, userInput); + fileDoesExist = true; + processUserInput(tasks, inputCommands, userInput, F, fileDoesExist); + } + } + private static void writeToFile (String filePath,ArrayListinputCommands) throws IOException { + FileWriter fw = new FileWriter(filePath); + for (String s:inputCommands) { + fw.write(s); + fw.write('\n'); } + fw.close(); + } + private static void appendToFile(String filePath, String userInput) throws IOException{ + FileWriter fw = new FileWriter(filePath,true); + fw.write(userInput); + fw.write('\n'); + fw.close(); } private static String[] splitActionAndDescription(String userInput) { String[] actionAndDescription = userInput.split(" ", 2); return actionAndDescription.length == 2 ? actionAndDescription : new String[]{actionAndDescription[0], ""}; } - private static void processUserInput(ArrayList tasks, String userInput) { + private static void processUserInput(ArrayList tasks, ArrayList inputCommands, String userInput, File F, boolean fileDoesExist) { + inputCommands.add(userInput); String[] actionAndDescription = splitActionAndDescription(userInput); String action = actionAndDescription[0]; String description = actionAndDescription[1]; - switch (action) { - case "bye": - System.out.println("Bye. Hope to see you again soon!"); - break; - case "list": - executeListAction(tasks); - break; - case "mark": - executeMarkAction(tasks, description); - break; - case "unmark": - executeUnmarkAction(tasks, description); - break; - case "todo": - executeTodoAction(tasks, description); - break; - case "event": - executeEventAction(tasks, description); - break; - case "deadline": - executeDeadlineAction(tasks, description); - break; - default: - System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-("); + try { + switch (action) { + case "bye": + System.out.println("Bye. Hope to see you again soon!"); + break; + case "list": + executeListAction(tasks, fileDoesExist); + break; + case "mark": + executeMarkAction(tasks, description, fileDoesExist); + if (fileDoesExist) { + writeToFile(F.getPath(), inputCommands); + } + break; + case "unmark": + executeUnmarkAction(tasks, description, fileDoesExist); + if (fileDoesExist) { + writeToFile(F.getPath(), inputCommands); + } + break; + case "todo": + executeTodoAction(tasks, description, fileDoesExist); + if (fileDoesExist) { + appendToFile(F.getPath(), userInput); + } + break; + case "event": + executeEventAction(tasks, description, fileDoesExist); + if (fileDoesExist) { + appendToFile(F.getPath(), userInput); + } + break; + case "deadline": + executeDeadlineAction(tasks, description, fileDoesExist); + if (fileDoesExist) { + appendToFile(F.getPath(), userInput); + } + break; + default: + System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-("); + } + } catch (IOException e) { + System.out.println("Unable to write/append to file."); } } - private static void executeListAction(ArrayList tasks) { - for (int i = 0; i < tasks.size(); i++) { - System.out.print(i + 1 + "."); - System.out.println(tasks.get(i).toString()); + private static void executeListAction(ArrayList tasks, boolean fileDoesExist) { + if (fileDoesExist) { + for (int i = 0; i < tasks.size(); i++) { + System.out.print(i + 1 + "."); + System.out.println(tasks.get(i).toString()); + } } } - private static void executeMarkAction(ArrayList tasks, String description) { + private static void executeMarkAction(ArrayList tasks, String description, boolean fileDoesExist) { Integer index = Integer.parseInt(description) - 1; tasks.get(index).isCompleted = true; - System.out.println("Nice! I've marked this task as done:"); - System.out.println(tasks.get(index).toString()); + if (fileDoesExist) { + System.out.println("Nice! I've marked this task as done:"); + System.out.println(tasks.get(index).toString()); + } } - private static void executeUnmarkAction(ArrayList tasks, String description) { + private static void executeUnmarkAction(ArrayList tasks, String description, boolean fileDoesExist) { Integer index = Integer.parseInt(description) - 1; tasks.get(index).isCompleted = false; - System.out.println("OK, I've marked this task as not done yet:"); - System.out.println(tasks.get(index).toString()); + if (fileDoesExist) { + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(tasks.get(index).toString()); + } } private static void printAcknowledgement(ArrayList tasks) { int lastIndexOfTasks = tasks.size() - 1; System.out.println("Got it. I've added this task:"); System.out.println(tasks.get(lastIndexOfTasks).toString()); - if (tasks.size() == 1){ + if (tasks.size() == 1) { System.out.println("Now you have " +tasks.size()+" task in the list."); } else { System.out.println("Now you have " + tasks.size() + " tasks in the list."); } } - private static void executeTodoAction(ArrayList tasks, String description) { + private static void executeTodoAction(ArrayList tasks, String description, boolean fileDoesExist) { try { addTodoToList(tasks, description); - printAcknowledgement(tasks); - } catch (Exception e){ + if (fileDoesExist) { + printAcknowledgement(tasks); + } + } catch (Exception e) { System.out.println("Unable to add todo: No tasks given."); } } private static void addTodoToList(ArrayList tasks, String description) throws DukeException { - if(description==""){ + if (description.equals(" ")) { throw new DukeException(new IllegalArgumentException()); } tasks.add(new Todo(description)); } - private static void executeEventAction(ArrayList tasks, String description) { + private static void executeEventAction(ArrayList tasks, String description, boolean fileDoesExist) { try { String[] indexArr = splitDescriptionEvent(description); String name = indexArr[0].trim(); String fromDate = indexArr[1].substring(5).trim(); String toDate = indexArr[2].substring(3).trim(); - tasks.add(new Event(description, fromDate, toDate)); - printAcknowledgement(tasks); - }catch (DukeException e){ + tasks.add(new Event(name, fromDate, toDate)); + if (fileDoesExist) { + printAcknowledgement(tasks); + } + } catch (DukeException e) { System.out.println("Not enough commands to execute \"event\""); } } private static String[] splitDescriptionEvent(String description) throws DukeException { String[] indexArr = description.split("/", 3); - if(indexArr.length < 3){ + if (indexArr.length < 3){ throw new DukeException(new IllegalArgumentException()); } return indexArr; } - private static void executeDeadlineAction(ArrayList tasks, String description) { + private static void executeDeadlineAction(ArrayList tasks, String description, boolean fileDoesExist) { try { String[] indexArr = splitDescriptionDeadline(description); String name = indexArr[0].trim(); String byDate = indexArr[1].substring(3).trim(); - tasks.add(new Deadline(description, byDate)); - printAcknowledgement(tasks); - }catch(DukeException e){ + tasks.add(new Deadline(name, byDate)); + if (fileDoesExist) { + printAcknowledgement(tasks); + } + } catch(DukeException e) { System.out.println("Not enough commands to execute \"deadline\""); } } private static String[] splitDescriptionDeadline(String description) throws DukeException { String[] indexArr = description.split("/", 2); - if(indexArr.length < 2){ + if (indexArr.length < 2) { throw new DukeException(new IllegalArgumentException()); } return indexArr; diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..9f37e4e0a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + From 8206b33b2661caf4ff1d37d04c4733217f151ae3 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Sat, 25 Feb 2023 23:28:13 +0800 Subject: [PATCH 15/27] Level-9 --- src/main/java/Duke.java | 8 ++++++++ src/main/java/IO.java | 3 +++ src/main/java/TaskList.java | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index cd23cf6f3..797a0515f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -78,6 +78,9 @@ private void processUserInput(String userInput, boolean fileDoesExist) { case "delete": executeDeleteAction(description); break; + case "find": + executeFindAction(description); + break; default: io.showInvalidCommand(); } @@ -162,4 +165,9 @@ private void executeDeleteAction(String description) { String deletedTask = tasks.deleteTaskAtIndex(index); io.printDeleted(tasks.getTasks(), deletedTask); } + private void executeFindAction(String description){ + ArrayList foundTasks = tasks.findTasks(description); + io.printFind(); + io.printList(foundTasks); + } } \ No newline at end of file diff --git a/src/main/java/IO.java b/src/main/java/IO.java index d2b09b919..1a54f450e 100644 --- a/src/main/java/IO.java +++ b/src/main/java/IO.java @@ -42,6 +42,9 @@ public void printDeleted(ArrayList tasks, String deletedTask) { System.out.println("Now you have " + tasks.size() + " tasks in the list."); } } + public void printFind() { + System.out.println("Here are the matching tasks in your list:"); + } public void showCannotEditFile(){ System.out.println("Unable to write/append to file."); } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index 60a381440..f062d5924 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -29,4 +29,14 @@ public String deleteTaskAtIndex(Integer index){ tasks.remove((int)index); return nameOfToBeDeletedTask; } + public ArrayList findTasks(String description){ + ArrayList matchedTasks = new ArrayList<>(); + for(Task s:tasks){ + if (s.name.contains(description)){ + matchedTasks.add(s); + } + } + return matchedTasks; + } } + From 4375e13b9d9b6a7d8b582fccd06d0b1cfb60cb85 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Sun, 26 Feb 2023 00:14:07 +0800 Subject: [PATCH 16/27] A-JavaDoc --- src/main/java/Deadline.java | 9 ++++ src/main/java/Duke.java | 88 +++++++++++++++++++++++++++++++++++++ src/main/java/Event.java | 10 +++++ src/main/java/IO.java | 58 +++++++++++++++++++++++- src/main/java/Memory.java | 22 ++++++++++ src/main/java/Task.java | 7 +++ src/main/java/TaskList.java | 38 ++++++++++++++++ src/main/java/Todo.java | 8 ++++ 8 files changed, 239 insertions(+), 1 deletion(-) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 7a1f6ac34..134175212 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,5 +1,14 @@ +/** + Represents a task with a deadline. + Inherits from the Task class. + */ public class Deadline extends Task { protected String due; + /** + Constructs a Deadline object with the specified information and deadline. + @param info the description of the task. + @param due the deadline of the task. + */ public Deadline(String info, String due) { super(info); this.due = due; diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 797a0515f..487ca73fe 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,6 @@ +/** + * The Duke class represents a chatbot that can respond to user input. + */ import java.util.Scanner; import java.util.ArrayList; import java.io.IOException; @@ -8,6 +11,12 @@ public static void main(String[] args) { private Memory memory; private IO io; private TaskList tasks; + /** + * Constructor for Duke object. + * Initializes a new Duke object and loads tasks from a file. + * Prints a welcome message and loops for user input until the user types "bye". + * @param filepath Path to the file to load tasks from. + */ public Duke(String filepath) { memory = new Memory(filepath); io = new IO(); @@ -24,15 +33,35 @@ public Duke(String filepath) { processUserInput(userInput, true); } } + /** + * Loads a list of input commands to the TaskList. + * Each input command is processed by the processUserInput method. + * + * @param inputCommands A list of input commands to load to the TaskList. + * @throws IOException If there is an error reading the file. + */ public void storeTasksInList(ArrayList inputCommands) throws IOException { for (String s : inputCommands) { processUserInput(s, false); } } + /** + * Splits user input into an action and a description array. + * + * @param userInput The user's input. + * @return An array containing the action and description of the input. + */ public String[] splitActionAndDescription(String userInput) { String[] actionAndDescription = userInput.split(" ", 2); return actionAndDescription.length == 2 ? actionAndDescription : new String[]{actionAndDescription[0], ""}; } + /** + * Processes user input by executing the appropriate action. + * Prints an error message if an invalid command is entered. + * + * @param userInput The user's input. + * @param fileDoesExist Whether the file exists or not. + */ private void processUserInput(String userInput, boolean fileDoesExist) { String[] actionAndDescription = splitActionAndDescription(userInput); String action = actionAndDescription[0]; @@ -88,11 +117,20 @@ private void processUserInput(String userInput, boolean fileDoesExist) { io.showCannotEditFile(); } } + /** + Executes the "list" action by printing the list of tasks to the user. + @param fileDoesExist A boolean indicating if the file exists or not. + */ private void executeListAction(boolean fileDoesExist) { if (fileDoesExist) { io.printList(tasks.getTasks()); } } + /** + Executes the "mark" action by marking the specified task as done and printing an acknowledgement message to the user. + @param description The task description provided by the user. + @param fileDoesExist A boolean indicating if the file exists or not. + */ private void executeMarkAction(String description, boolean fileDoesExist) { int index = Integer.parseInt(description) - 1; tasks.markAtIndex(index); @@ -100,6 +138,11 @@ private void executeMarkAction(String description, boolean fileDoesExist) { io.printMarked(tasks.getTasks(), index); } } + /** + Executes the "unmark" action by marking the specified task as undone and printing an acknowledgement message to the user. + @param description The task description provided by the user. + @param fileDoesExist A boolean indicating if the file exists or not. + */ private void executeUnmarkAction(String description, boolean fileDoesExist) { int index = Integer.parseInt(description) - 1; tasks.unmarkAtIndex(Integer.parseInt(description) - 1); @@ -107,6 +150,11 @@ private void executeUnmarkAction(String description, boolean fileDoesExist) { io.printUnmarked(tasks.getTasks(), index); } } + /** + Executes the "todo" action by adding a new Todo task to the task list and printing an acknowledgement message to the user. + @param description The task description provided by the user. + @param fileDoesExist A boolean indicating if the file exists or not. + */ private void executeTodoAction(String description, boolean fileDoesExist) { try { tasks.addTodoToList(description); @@ -117,6 +165,14 @@ private void executeTodoAction(String description, boolean fileDoesExist) { io.showInsufficientTodo(); } } + /** + Executes an event action if the file exists. + Parses the description into name, fromDate and toDate. + Adds the parsed task to the task list and prints acknowledgement if the file exists. + Otherwise, shows an error message. + @param description The description of the event task. + @param fileDoesExist Indicates whether the file exists. + */ private void executeEventAction(String description, boolean fileDoesExist) { if (fileDoesExist) { try { @@ -133,6 +189,14 @@ private void executeEventAction(String description, boolean fileDoesExist) { } } } + /** + Parses the description of an event task and returns an array containing the name, + fromDate and toDate. + @param description The description of the event task. + @return An array containing the name, fromDate and toDate. + @throws DukeException If there are insufficient arguments to parse the description. + */ + private static String[] splitDescriptionEvent(String description) throws DukeException { String[] indexArr = description.split("/", 3); if (indexArr.length < 3){ @@ -140,6 +204,14 @@ private static String[] splitDescriptionEvent(String description) throws DukeExc } return indexArr; } + /** + Executes a deadline action. + Parses the description into name and byDate. + Adds the parsed task to the task list and prints acknowledgement if the file exists. + Otherwise, shows an error message. + @param description The description of the deadline task. + @param fileDoesExist Indicates whether the file exists. + */ private void executeDeadlineAction(String description, boolean fileDoesExist) { try { String[] indexArr = splitDescriptionDeadline(description); @@ -153,6 +225,12 @@ private void executeDeadlineAction(String description, boolean fileDoesExist) { io.showInsufficientDeadline(); } } + /** + Parses the description of a deadline task and returns an array containing the name and byDate. + @param description The description of the deadline task. + @return An array containing the name and byDate. + @throws DukeException If there are insufficient arguments to parse the description. + */ private static String[] splitDescriptionDeadline(String description) throws DukeException { String[] indexArr = description.split("/", 2); if (indexArr.length < 2) { @@ -160,11 +238,21 @@ private static String[] splitDescriptionDeadline(String description) throws Duke } return indexArr; } + /** + Executes a delete action. + Deletes the task at the specified index and prints the deleted task and task list. + @param description The index of the task to delete. + */ private void executeDeleteAction(String description) { Integer index = Integer.parseInt(description) - 1; String deletedTask = tasks.deleteTaskAtIndex(index); io.printDeleted(tasks.getTasks(), deletedTask); } + /** + Executes a find action. + Finds tasks containing the specified keyword and prints them. + @param description The keyword to search for. + */ private void executeFindAction(String description){ ArrayList foundTasks = tasks.findTasks(description); io.printFind(); diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 85a5cb4c5..a14b8f1e8 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,6 +1,16 @@ +/** + Represents a task that is an event with a start time and end time. + Inherits from the Task class. + */ public class Event extends Task { protected String startTime; protected String endTime; + /** + Constructs an Event object with the specified information, start time and end time. + @param info the description of the event. + @param startTime the start time of the event. + @param endTime the end time of the event. + */ public Event(String info, String startTime, String endTime) { super(info); this.startTime = startTime; diff --git a/src/main/java/IO.java b/src/main/java/IO.java index 1a54f450e..f20542a3f 100644 --- a/src/main/java/IO.java +++ b/src/main/java/IO.java @@ -1,14 +1,26 @@ +/** + Handles Input/Output operations for Duke. + */ import java.util.ArrayList; - public class IO { public IO(){ } + /** + * Prints a welcome message. + */ public void printWelcome(){ System.out.println("Hello! I'm Duke \nWhat can I do for you?"); } + /** + * Prints a goodbye message. + */ public void printBye(){ System.out.println("Bye. Hope to see you again soon!"); } + /** + *Prints an acknowledgement message for adding a task to the list. + *@param "tasks" the current task list. + */ public void printAcknowledgement(ArrayList tasks) { int lastIndexOfTasks = tasks.size() - 1; System.out.println("Got it. I've added this task:"); @@ -19,20 +31,43 @@ public void printAcknowledgement(ArrayList tasks) { System.out.println("Now you have " + tasks.size() + " tasks in the list."); } } + /** + * Prints the current task list. + * + * @param tasks the current task list. + */ public void printList(ArrayList tasks) { for (int i = 0; i < tasks.size(); i++) { System.out.print(i + 1 + "."); System.out.println(tasks.get(i).toString()); } } + /** + * Prints a message confirming that a task has been marked as completed. + * + * @param tasks the current task list. + * @param index the index of the completed task in the list. + */ public void printMarked(ArrayList tasks, Integer index) { System.out.println("Nice! I've marked this task as done:"); System.out.println(tasks.get(index).toString()); } + /** + * Prints a message confirming that a task has been marked as incomplete. + * + * @param tasks the current task list. + * @param index the index of the incomplete task in the list. + */ public void printUnmarked(ArrayList tasks, Integer index) { System.out.println("OK, I've marked this task as not done yet:"); System.out.println(tasks.get(index).toString()); } + /** + * Prints a message confirming that a task has been deleted from the list. + * + * @param tasks the current task list. + * @param deletedTask the deleted task. + */ public void printDeleted(ArrayList tasks, String deletedTask) { System.out.println("Noted. I've removed this task:"); System.out.println(deletedTask); @@ -42,24 +77,45 @@ public void printDeleted(ArrayList tasks, String deletedTask) { System.out.println("Now you have " + tasks.size() + " tasks in the list."); } } + /** + * Prints a message indicating that matching tasks were found. + */ public void printFind() { System.out.println("Here are the matching tasks in your list:"); } + /** + * Prints an error message indicating that the program is unable to write or append to the file. + */ public void showCannotEditFile(){ System.out.println("Unable to write/append to file."); } + /** + * Prints an error message indicating that the program is unable to read from the file. + */ public void showLoadingError(){ System.out.println("Could not read file."); } + /** + * Prints an error message indicating that the user has entered an invalid command. + */ public void showInvalidCommand(){ System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-("); } + /** + * Prints an error message indicating that the user has not provided any description for a todo task. + */ public void showInsufficientTodo(){ System.out.println("Unable to add todo: No tasks given."); } + /** + * Prints an error message indicating that the user has not provided enough information to create an event task. + */ public void showInsufficientEvent(){ System.out.println("Not enough commands to execute \"event\""); } + /** + * Prints an error message indicating that the user has not provided enough information to create a deadline task. + */ public void showInsufficientDeadline(){ System.out.println("Not enough commands to execute \"deadline\""); } diff --git a/src/main/java/Memory.java b/src/main/java/Memory.java index 7d2ec2625..3aaf8db03 100644 --- a/src/main/java/Memory.java +++ b/src/main/java/Memory.java @@ -1,3 +1,8 @@ +/** + * Memory is a class that saves the tasks in the hard disk automatically whenever the task list changes. + * And loads the data from the hard disk when Duke starts up. + */ + import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -10,6 +15,12 @@ public class Memory { public Memory(String filePath) { this.filePath = filePath; } + /** + * Loads the contents of the file into an ArrayList of Strings. + * + * @return an ArrayList of Strings containing the contents of the file + * @throws IOException if there is an error reading from the file + */ public ArrayList loadFile() throws IOException { File F = new File(filePath); if (!F.exists()) { @@ -22,6 +33,11 @@ public ArrayList loadFile() throws IOException { } return fileContents; } + /** + * Writes the contents of the commandInput ArrayList to the file. + * + * @throws IOException if there is an error writing to the file + */ public void writeToFile() throws IOException { FileWriter fw = new FileWriter(this.filePath); for (String s : commandInput) { @@ -30,6 +46,12 @@ public void writeToFile() throws IOException { } fw.close(); } + /** + * Appends the specified userInput String to the file. + * + * @param userInput the String to append to the file + * @throws IOException if there is an error writing to the file + */ public void appendToFile(String userInput) throws IOException { FileWriter fw = new FileWriter(this.filePath, true); fw.write(userInput); diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 858fb7b4d..60693a5ba 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,6 +1,13 @@ +/** + Represents a general task with a name and completion status. + */ public class Task { protected String name; protected boolean isCompleted; + /** + Constructs a Task object with the specified name. + @param info the name of the task. + */ public Task(String info) { this.name = info; } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index f062d5924..d823a2678 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,3 +1,7 @@ +/** + TaskList is a class that handles the manipulation of tasks in an ArrayList. + It contains methods to add, delete, mark as done, and find tasks. + */ import java.util.ArrayList; public class TaskList { ArrayList tasks = new ArrayList<>(); @@ -6,29 +10,63 @@ public TaskList() { public ArrayList getTasks() { return tasks; } + /** + Marks the task at the given index as completed. + @param index The index of the task to be marked. + */ public void markAtIndex(Integer index) { tasks.get(index).isCompleted = true; } + /** + Marks the task at the given index as incomplete. + @param index The index of the task to be unmarked. + */ public void unmarkAtIndex(Integer index) { tasks.get(index).isCompleted = false; } + /** + Adds a Todo task to the list. + @param description The description of the Todo task. + @throws DukeException If description is an empty string. + */ public void addTodoToList(String description) throws DukeException { if(description.equals("")){ throw new DukeException(new IllegalArgumentException()); } tasks.add(new Todo(description)); } + /** + Adds a Deadline task to the list. + @param description The description of the Deadline task. + @param byDate The date and time by which the Deadline task should be completed. + */ public void addDeadlineToList(String description, String byDate) { tasks.add(new Deadline(description, byDate)); } + /** + Adds an Event task to the list. + @param description The description of the Event task. + @param fromDate The date and time from which the Event task starts. + @param toDate The date and time at which the Event task ends. + */ public void addEventToList(String description, String fromDate, String toDate) { tasks.add(new Event(description, fromDate, toDate)); } + /** + Deletes the task at the given index from the list. + @param index The index of the task to be deleted. + @return The name of the deleted task. + */ public String deleteTaskAtIndex(Integer index){ String nameOfToBeDeletedTask = tasks.get(index).name; tasks.remove((int)index); return nameOfToBeDeletedTask; } + /** + Finds tasks in the list that match the given description. + @param description The description to search for in the tasks. + @return An ArrayList of tasks that match the description. + */ public ArrayList findTasks(String description){ ArrayList matchedTasks = new ArrayList<>(); for(Task s:tasks){ diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index c17a7246b..43623d9cc 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -1,4 +1,12 @@ +/** + Represents a task that is a "todo item. + Inherits from the Task class. + */ public class Todo extends Task { + /** + Constructs a Todo object with the specified information. + @param info the description of the todo item. + */ public Todo (String info) { super (info); } From 1ebdea6ca22aa074ac177303da1e2f418c766b94 Mon Sep 17 00:00:00 2001 From: TJ-Hoo <88079327+TJ-Hoo@users.noreply.github.com> Date: Sun, 26 Feb 2023 00:27:19 +0800 Subject: [PATCH 17/27] Update README.md --- README.md | 81 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 8715d4d91..8a7b72f71 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,57 @@ -# Duke project template - -This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. - -## Setting up in Intellij - -Prerequisites: JDK 11, update Intellij to the most recent version. - -1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) -1. Open the project into Intellij as follows: - 1. Click `Open`. - 1. Select the project directory, and click `OK`. - 1. If there are any further prompts, accept the defaults. -1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
- In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: - ``` - Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| - ``` +#Duke Task Manager +Duke is a task manager that helps users keep track of their daily tasks. Users can add, edit, delete, and search for tasks using the command-line interface. Duke uses a simple text file to store the tasks. +This guide will help you get started. + +##Prerequisites +Before using Duke, please ensure that you have the following: +- Java 11 or later installed on your computer +- The Duke.jar file downloaded on your computer + +##Getting Started +1. Download the Duke.jar file to your computer. +2. Open a terminal or command prompt and navigate to the directory where the Duke.jar file is located. +3. Run the following command to start Duke: + +java -jar Duke.jar +4. If Duke is running properly, you should see a welcome message. + +##Features +Duke currently supports the following features: + +###Adding Tasks +To add a task, type one of the following commands followed by the task description: +- todo: adds a new todo task +- event: adds a new event task +- deadline: adds a new deadline task +For example: +todo buy groceries +event attend meeting /at 2022-03-01 10:00-12:00 +deadline submit report /by 2022-03-05 23:59 + +###Listing Tasks +To list all the tasks currently in Duke, type the command list. + +###Marking Tasks +To mark a task as done, type the command mark followed by the task number. For example: +mark 2 + +###Unmarking Tasks +To unmark a task as done, type the command unmark followed by the task number. +For example: +unmark 2 + +###Deleting Tasks +To delete a task, type the command delete followed by the task number. For example: +delete 2 + +###Finding Tasks +To find tasks containing a specific keyword, type the command find followed by the keyword. For example: +find groceries + +###Exiting Duke +To exit Duke, type the command bye. + +##Notes +All tasks added to Duke are automatically saved to a file named "Duke.txt" in the same directory as the Duke.jar file. +If the Duke.txt file already exists, Duke will load the tasks from the file when it starts. +If an error occurs while Duke is running, an error message will be displayed. From 31322c77aef2e9d6fbb2a2fb83d6405c4c27078e Mon Sep 17 00:00:00 2001 From: TJ-Hoo <88079327+TJ-Hoo@users.noreply.github.com> Date: Sun, 26 Feb 2023 00:29:09 +0800 Subject: [PATCH 18/27] Update README.md --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8a7b72f71..953e3ea6f 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ -#Duke Task Manager +# Duke Task Manager Duke is a task manager that helps users keep track of their daily tasks. Users can add, edit, delete, and search for tasks using the command-line interface. Duke uses a simple text file to store the tasks. This guide will help you get started. -##Prerequisites +## Prerequisites Before using Duke, please ensure that you have the following: - Java 11 or later installed on your computer - The Duke.jar file downloaded on your computer -##Getting Started +## Getting Started 1. Download the Duke.jar file to your computer. 2. Open a terminal or command prompt and navigate to the directory where the Duke.jar file is located. 3. Run the following command to start Duke: @@ -15,10 +15,10 @@ Before using Duke, please ensure that you have the following: java -jar Duke.jar 4. If Duke is running properly, you should see a welcome message. -##Features +## Features Duke currently supports the following features: -###Adding Tasks +### Adding Tasks To add a task, type one of the following commands followed by the task description: - todo: adds a new todo task - event: adds a new event task @@ -28,30 +28,30 @@ todo buy groceries event attend meeting /at 2022-03-01 10:00-12:00 deadline submit report /by 2022-03-05 23:59 -###Listing Tasks +### Listing Tasks To list all the tasks currently in Duke, type the command list. -###Marking Tasks +### Marking Tasks To mark a task as done, type the command mark followed by the task number. For example: mark 2 -###Unmarking Tasks +### Unmarking Tasks To unmark a task as done, type the command unmark followed by the task number. For example: unmark 2 -###Deleting Tasks +### Deleting Tasks To delete a task, type the command delete followed by the task number. For example: delete 2 -###Finding Tasks +### Finding Tasks To find tasks containing a specific keyword, type the command find followed by the keyword. For example: find groceries -###Exiting Duke +### Exiting Duke To exit Duke, type the command bye. -##Notes -All tasks added to Duke are automatically saved to a file named "Duke.txt" in the same directory as the Duke.jar file. -If the Duke.txt file already exists, Duke will load the tasks from the file when it starts. -If an error occurs while Duke is running, an error message will be displayed. +## Notes +- All tasks added to Duke are automatically saved to a file named "Duke.txt" in the same directory as the Duke.jar file. +- If the Duke.txt file already exists, Duke will load the tasks from the file when it starts. +- If an error occurs while Duke is running, an error message will be displayed. From 3986655cd340b305d82f798b621c22f6724c54a4 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Sun, 26 Feb 2023 00:30:11 +0800 Subject: [PATCH 19/27] no message --- src/main/java/Duke.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 797a0515f..f64d24ba5 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -165,7 +165,7 @@ private void executeDeleteAction(String description) { String deletedTask = tasks.deleteTaskAtIndex(index); io.printDeleted(tasks.getTasks(), deletedTask); } - private void executeFindAction(String description){ + private void executeFindAction(String description) { ArrayList foundTasks = tasks.findTasks(description); io.printFind(); io.printList(foundTasks); From 9c0ba57954e96cd5b5c674577d758136e82116a7 Mon Sep 17 00:00:00 2001 From: TJ-Hoo <88079327+TJ-Hoo@users.noreply.github.com> Date: Sun, 26 Feb 2023 11:15:57 +0800 Subject: [PATCH 20/27] Rename README.md to UserGuide --- README.md => UserGuide | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README.md => UserGuide (100%) diff --git a/README.md b/UserGuide similarity index 100% rename from README.md rename to UserGuide From c25b455653cf43434d11b638cd534f99d3caa359 Mon Sep 17 00:00:00 2001 From: TJ-Hoo <88079327+TJ-Hoo@users.noreply.github.com> Date: Sun, 26 Feb 2023 11:29:48 +0800 Subject: [PATCH 21/27] Update README.md --- docs/README.md | 86 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..953e3ea6f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,57 @@ -# User Guide - -## Features - -### Feature-ABC - -Description of the feature. - -### Feature-XYZ - -Description of the feature. - -## Usage - -### `Keyword` - Describe action - -Describe the action and its outcome. - -Example of usage: - -`keyword (optional arguments)` - -Expected outcome: - -Description of the outcome. - -``` -expected output -``` +# Duke Task Manager +Duke is a task manager that helps users keep track of their daily tasks. Users can add, edit, delete, and search for tasks using the command-line interface. Duke uses a simple text file to store the tasks. +This guide will help you get started. + +## Prerequisites +Before using Duke, please ensure that you have the following: +- Java 11 or later installed on your computer +- The Duke.jar file downloaded on your computer + +## Getting Started +1. Download the Duke.jar file to your computer. +2. Open a terminal or command prompt and navigate to the directory where the Duke.jar file is located. +3. Run the following command to start Duke: + +java -jar Duke.jar +4. If Duke is running properly, you should see a welcome message. + +## Features +Duke currently supports the following features: + +### Adding Tasks +To add a task, type one of the following commands followed by the task description: +- todo: adds a new todo task +- event: adds a new event task +- deadline: adds a new deadline task +For example: +todo buy groceries +event attend meeting /at 2022-03-01 10:00-12:00 +deadline submit report /by 2022-03-05 23:59 + +### Listing Tasks +To list all the tasks currently in Duke, type the command list. + +### Marking Tasks +To mark a task as done, type the command mark followed by the task number. For example: +mark 2 + +### Unmarking Tasks +To unmark a task as done, type the command unmark followed by the task number. +For example: +unmark 2 + +### Deleting Tasks +To delete a task, type the command delete followed by the task number. For example: +delete 2 + +### Finding Tasks +To find tasks containing a specific keyword, type the command find followed by the keyword. For example: +find groceries + +### Exiting Duke +To exit Duke, type the command bye. + +## Notes +- All tasks added to Duke are automatically saved to a file named "Duke.txt" in the same directory as the Duke.jar file. +- If the Duke.txt file already exists, Duke will load the tasks from the file when it starts. +- If an error occurs while Duke is running, an error message will be displayed. From f8faf8e557d6cc7df3471d463a045160899f20c8 Mon Sep 17 00:00:00 2001 From: TJ-Hoo <88079327+TJ-Hoo@users.noreply.github.com> Date: Sun, 26 Feb 2023 11:32:16 +0800 Subject: [PATCH 22/27] Update README.md --- docs/README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 953e3ea6f..452167a6d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -23,6 +23,7 @@ To add a task, type one of the following commands followed by the task descripti - todo: adds a new todo task - event: adds a new event task - deadline: adds a new deadline task + For example: todo buy groceries event attend meeting /at 2022-03-01 10:00-12:00 @@ -32,20 +33,27 @@ deadline submit report /by 2022-03-05 23:59 To list all the tasks currently in Duke, type the command list. ### Marking Tasks -To mark a task as done, type the command mark followed by the task number. For example: +To mark a task as done, type the command mark followed by the task number. + +For example: mark 2 ### Unmarking Tasks To unmark a task as done, type the command unmark followed by the task number. + For example: unmark 2 ### Deleting Tasks -To delete a task, type the command delete followed by the task number. For example: +To delete a task, type the command delete followed by the task number. + +For example: delete 2 ### Finding Tasks -To find tasks containing a specific keyword, type the command find followed by the keyword. For example: +To find tasks containing a specific keyword, type the command find followed by the keyword. + +For example: find groceries ### Exiting Duke From 9366fd69d54c6f6b665f6ff5722bd38919040669 Mon Sep 17 00:00:00 2001 From: TJ-Hoo <88079327+TJ-Hoo@users.noreply.github.com> Date: Sun, 26 Feb 2023 11:47:49 +0800 Subject: [PATCH 23/27] Update README.md --- docs/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 452167a6d..915fffd69 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,9 +25,9 @@ To add a task, type one of the following commands followed by the task descripti - deadline: adds a new deadline task For example: -todo buy groceries -event attend meeting /at 2022-03-01 10:00-12:00 -deadline submit report /by 2022-03-05 23:59 +todo borrow book +event project meeting /from Mon 2pm /to 4pm +deadline return book /by Sunday ### Listing Tasks To list all the tasks currently in Duke, type the command list. From 4eb7c4a51bd4475ad759c65248a901853a50de1e Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Sun, 26 Feb 2023 12:02:43 +0800 Subject: [PATCH 24/27] Minor Fixes --- duke.txt | 19 ----------- src/main/java/Duke.java | 75 ++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 57 deletions(-) diff --git a/duke.txt b/duke.txt index 607a96e36..e69de29bb 100644 --- a/duke.txt +++ b/duke.txt @@ -1,19 +0,0 @@ -todo cs2113 -todo cg2023 -list -mark 1 -todo cg2023 -todo cs2113 -list -mark 2 -mark 3 -mark 4 -event project meeting from 2pm to 4pm -event project meeting /from Mon 2pm /to 4pm -deadline return book /by Sunday -todo borrow book -deadline return book /by Sunday -todo borrow book -deadline return book /by Sunday -event project meeting /from Mon 2pm /to 4pm -todo diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d07e09ea..f47c522c5 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -8,7 +8,6 @@ public class Duke { public static void main(String[] args) { new Duke("DUKE/Duke.txt"); } - private Memory memory; private IO io; private TaskList tasks; @@ -74,50 +73,50 @@ private void processUserInput(String userInput, boolean fileDoesExist) { String description = actionAndDescription[1]; try { switch (action) { - case "bye": - io.printBye(); - break; - case "list": - executeListAction(fileDoesExist); - break; - case "mark": - executeMarkAction(description, fileDoesExist); - if (fileDoesExist) { - memory.writeToFile(); - } - break; + case "bye": + io.printBye(); + break; + case "list": + executeListAction(fileDoesExist); + break; + case "mark": + executeMarkAction(description, fileDoesExist); + if (fileDoesExist) { + memory.writeToFile(); + } + break; case "unmark": - executeUnmarkAction(description, fileDoesExist); - if (fileDoesExist) { - memory.writeToFile(); - } - break; + executeUnmarkAction(description, fileDoesExist); + if (fileDoesExist) { + memory.writeToFile(); + } + break; case "todo": - executeTodoAction(description, fileDoesExist); - if (fileDoesExist) { - memory.appendToFile(userInput); - } - break; + executeTodoAction(description, fileDoesExist); + if (fileDoesExist) { + memory.appendToFile(userInput); + } + break; case "event": - executeEventAction(description, fileDoesExist); - if (fileDoesExist) { - memory.appendToFile(userInput); - } - break; + executeEventAction(description, fileDoesExist); + if (fileDoesExist) { + memory.appendToFile(userInput); + } + break; case "deadline": - executeDeadlineAction(description, fileDoesExist); - if (fileDoesExist) { - memory.appendToFile(userInput); - } - break; + executeDeadlineAction(description, fileDoesExist); + if (fileDoesExist) { + memory.appendToFile(userInput); + } + break; case "delete": - executeDeleteAction(description); - break; + executeDeleteAction(description); + break; case "find": - executeFindAction(description); - break; + executeFindAction(description); + break; default: - io.showInvalidCommand(); + io.showInvalidCommand(); } } catch (IOException e) { io.showCannotEditFile(); From 3f092ce14eceb7a4f714742df1fc48893dd5c95b Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Tue, 28 Feb 2023 14:11:35 +0800 Subject: [PATCH 25/27] Minor improvements --- src/main/java/Duke.java | 15 +++------------ src/main/java/Parser.java | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 src/main/java/Parser.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f47c522c5..d602a09b9 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -11,6 +11,7 @@ public static void main(String[] args) { private Memory memory; private IO io; private TaskList tasks; + private Parser parser; /** * Constructor for Duke object. @@ -49,17 +50,6 @@ public void storeTasksInList(ArrayList inputCommands) throws IOException } } - /** - * Splits user input into an action and a description array. - * - * @param userInput The user's input. - * @return An array containing the action and description of the input. - */ - public String[] splitActionAndDescription(String userInput) { - String[] actionAndDescription = userInput.split(" ", 2); - return actionAndDescription.length == 2 ? actionAndDescription : new String[]{actionAndDescription[0], ""}; - } - /** * Processes user input by executing the appropriate action. * Prints an error message if an invalid command is entered. @@ -68,7 +58,8 @@ public String[] splitActionAndDescription(String userInput) { * @param fileDoesExist Whether the file exists or not. */ private void processUserInput(String userInput, boolean fileDoesExist) { - String[] actionAndDescription = splitActionAndDescription(userInput); + parser = new Parser(); + String[] actionAndDescription = parser.splitActionAndDescription(userInput); String action = actionAndDescription[0]; String description = actionAndDescription[1]; try { diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 000000000..e11bd6133 --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,24 @@ +public class Parser { + private String userInput; + + public Parser(String userInput) { + this.userInput = userInput; + } + + public Parser() { + } + + /** + * Splits user input into an action and a description array. + * + * @param userInput The user's input. + * @return An array containing the action and description of the input. + */ + public String[] splitActionAndDescription(String userInput){ + String[] commandAndArgs = userInput.split(" ",2); + if(commandAndArgs.length == 1){ + return new String[]{commandAndArgs[0],""}; + } + return commandAndArgs; + } +} From dc79a03c5334169cbaa429d13651e729a2b1f031 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Tue, 28 Feb 2023 14:21:09 +0800 Subject: [PATCH 26/27] Minor bug fixes --- src/main/java/Duke.java | 74 ++++++++++++++++++++--------------------- src/main/java/IO.java | 1 + 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index d602a09b9..3f51ded14 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -64,50 +64,50 @@ private void processUserInput(String userInput, boolean fileDoesExist) { String description = actionAndDescription[1]; try { switch (action) { - case "bye": - io.printBye(); - break; - case "list": - executeListAction(fileDoesExist); - break; - case "mark": - executeMarkAction(description, fileDoesExist); - if (fileDoesExist) { - memory.writeToFile(); - } - break; + case "bye": + io.printBye(); + break; + case "list": + executeListAction(fileDoesExist); + break; + case "mark": + executeMarkAction(description, fileDoesExist); + if (fileDoesExist) { + memory.writeToFile(); + } + break; case "unmark": - executeUnmarkAction(description, fileDoesExist); - if (fileDoesExist) { - memory.writeToFile(); - } - break; + executeUnmarkAction(description, fileDoesExist); + if (fileDoesExist) { + memory.writeToFile(); + } + break; case "todo": - executeTodoAction(description, fileDoesExist); - if (fileDoesExist) { - memory.appendToFile(userInput); - } - break; + executeTodoAction(description, fileDoesExist); + if (fileDoesExist) { + memory.appendToFile(userInput); + } + break; case "event": - executeEventAction(description, fileDoesExist); - if (fileDoesExist) { - memory.appendToFile(userInput); - } - break; + executeEventAction(description, fileDoesExist); + if (fileDoesExist) { + memory.appendToFile(userInput); + } + break; case "deadline": - executeDeadlineAction(description, fileDoesExist); - if (fileDoesExist) { - memory.appendToFile(userInput); - } - break; + executeDeadlineAction(description, fileDoesExist); + if (fileDoesExist) { + memory.appendToFile(userInput); + } + break; case "delete": - executeDeleteAction(description); - break; + executeDeleteAction(description); + break; case "find": - executeFindAction(description); - break; + executeFindAction(description); + break; default: - io.showInvalidCommand(); + io.showInvalidCommand(); } } catch (IOException e) { io.showCannotEditFile(); diff --git a/src/main/java/IO.java b/src/main/java/IO.java index f20542a3f..dac9c55d4 100644 --- a/src/main/java/IO.java +++ b/src/main/java/IO.java @@ -16,6 +16,7 @@ public void printWelcome(){ */ public void printBye(){ System.out.println("Bye. Hope to see you again soon!"); + System.exit(0); } /** *Prints an acknowledgement message for adding a task to the list. From fd2c8f9208a4e3fc2b751cf2b13777bd0fb29585 Mon Sep 17 00:00:00 2001 From: "TJ\\user" Date: Wed, 1 Mar 2023 15:20:40 +0800 Subject: [PATCH 27/27] Minor fix --- src/main/java/Duke.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3f51ded14..a534c814b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -5,9 +5,6 @@ import java.util.ArrayList; import java.io.IOException; public class Duke { - public static void main(String[] args) { - new Duke("DUKE/Duke.txt"); - } private Memory memory; private IO io; private TaskList tasks; @@ -49,6 +46,9 @@ public void storeTasksInList(ArrayList inputCommands) throws IOException processUserInput(s, false); } } + public static void main(String[] args) { + new Duke("DUKE/Duke.txt"); + } /** * Processes user input by executing the appropriate action.