From 2347e0af3c3d9eedbacd9c473be8837293a6d10a Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Sun, 22 Jan 2023 18:55:49 +0800 Subject: [PATCH 01/16] Increment level 0, added a greeting --- src/main/java/Duke.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..2ad2c2c15 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,5 @@ -public class Duke { +public class +Duke { public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -6,5 +7,13 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + + String greeting = ("____________________________________________________________\n" + + "Hello! I'm Duke\n" + "What can I do for you?\n" + + "____________________________________________________________\n" + + "Bye. Hope to see you again soon!\n" + + "____________________________________________________________\n" + ); + System.out.println(greeting); } } From 5697dca554c5222ca648dc2b49c7504180acbef8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Mon, 23 Jan 2023 11:13:45 +0800 Subject: [PATCH 02/16] Level-1: Greet, Echo, Exit --- src/main/java/Duke.java | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 2ad2c2c15..dc5a813cc 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,5 @@ +import java.util.Scanner; + public class Duke { public static void main(String[] args) { @@ -9,11 +11,24 @@ public static void main(String[] args) { System.out.println("Hello from\n" + logo); String greeting = ("____________________________________________________________\n" - + "Hello! I'm Duke\n" + "What can I do for you?\n" - + "____________________________________________________________\n" - + "Bye. Hope to see you again soon!\n" - + "____________________________________________________________\n" - ); + + "Hello! I'm Duke\n" + "What can I do for you?\n" + + "____________________________________________________________\n" + ); + + String goodBye = ("Bye. Hope to see you again soon!\n" + + "____________________________________________________________\n"); + System.out.println(greeting); + Scanner myObj = new Scanner(System.in); + String userInput; + userInput = myObj.nextLine(); + + while (!userInput.equals("bye")) { + System.out.println("____________________________________________________________\n" + + userInput + "\n" + + "____________________________________________________________\n"); + userInput = myObj.nextLine(); + } + System.out.println(goodBye); } } From 9d838d8ec18f9717b8b934f5108e61d6064cd674 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Mon, 23 Jan 2023 11:45:15 +0800 Subject: [PATCH 03/16] Level-2: Add, List --- src/main/java/Duke.java | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index dc5a813cc..a1c69c842 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,5 @@ import java.util.Scanner; +import java.util.*; public class Duke { @@ -15,18 +16,30 @@ public static void main(String[] args) { + "____________________________________________________________\n" ); - String goodBye = ("Bye. Hope to see you again soon!\n" + String goodBye = ("____________________________________________________________\n" + + "Bye. Hope to see you again soon!\n" + "____________________________________________________________\n"); System.out.println(greeting); Scanner myObj = new Scanner(System.in); String userInput; userInput = myObj.nextLine(); + ArrayList toDolist = new ArrayList(100); while (!userInput.equals("bye")) { - System.out.println("____________________________________________________________\n" - + userInput + "\n" - + "____________________________________________________________\n"); + int i = 1; + if (userInput.equals("list")) { + for (String item : toDolist) { + System.out.println(i + "." + item); + i += 1; + } + System.out.println("____________________________________________________________\n"); + } else { + System.out.println("____________________________________________________________\n" + + "added:" + userInput + "\n" + + "____________________________________________________________"); + toDolist.add(userInput); + } userInput = myObj.nextLine(); } System.out.println(goodBye); From 39997c34dc32e674ed6095aa2968b45177bb8f3f Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Mon, 23 Jan 2023 15:28:40 +0800 Subject: [PATCH 04/16] Level 3. Mark as Done --- src/main/java/Duke.java | 48 +++++++++++++++++++++++++++++++---------- src/main/java/Task.java | 29 +++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a1c69c842..0e074c836 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,6 +3,18 @@ public class Duke { + public static boolean containsNumbers(String string) { + if (string == null || string.isEmpty()) { + return false; + } + for (int i = 0; i < string.length(); ++i) { + if (Character.isDigit(string.charAt(i))) { + return true; + } + } + return false; + } + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -24,21 +36,35 @@ public static void main(String[] args) { Scanner myObj = new Scanner(System.in); String userInput; userInput = myObj.nextLine(); - ArrayList toDolist = new ArrayList(100); + ArrayList toDoList = new ArrayList(100); while (!userInput.equals("bye")) { - int i = 1; - if (userInput.equals("list")) { - for (String item : toDolist) { - System.out.println(i + "." + item); - i += 1; + if (!containsNumbers(userInput)){ + if (userInput.equals("list")) { + System.out.println("Here are the tasks in your list:\n"); + for (Task item : toDoList) { + System.out.print((toDoList.indexOf(item)+1) + "."); + System.out.print(item.getStatusIcon()); + System.out.println(item.getDescription()); + } + System.out.println("____________________________________________________________\n"); + } else { + System.out.println("____________________________________________________________\n" + + "added:" + userInput + "\n" + + "____________________________________________________________"); + toDoList.add(new Task(userInput)); } - System.out.println("____________________________________________________________\n"); } else { - System.out.println("____________________________________________________________\n" - + "added:" + userInput + "\n" - + "____________________________________________________________"); - toDolist.add(userInput); + int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + if (userInput.contains("unmark")){ + toDoList.get(itemNumber).markAsUnDone(); + System.out.println("OK, I've marked this task as not done yet:\n" + " " + + toDoList.get(itemNumber).getStatusIcon() + toDoList.get(itemNumber).getDescription()); + } else { + toDoList.get(itemNumber).markAsDone(); + System.out.println("Nice! I've marked this task as done:\n" + " " + + toDoList.get(itemNumber).getStatusIcon() + toDoList.get(itemNumber).getDescription()); + } } userInput = myObj.nextLine(); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..5b147c950 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,29 @@ +public class Task { + protected String description; + protected boolean isDone; + protected int taskNumber; + + public String getDescription() { + return description; + } + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public void markAsDone() { + this.isDone = true; + } + + public void markAsUnDone() { + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "[X]" : "[ ]"); // mark done task with X + } + +} + + From 9aab82a2f1ab118e978a839045bba44d235d7a21 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Thu, 26 Jan 2023 11:59:46 +0800 Subject: [PATCH 05/16] To test for push --- src/main/java/Duke.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 0e074c836..3ea29e62e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -39,11 +39,11 @@ public static void main(String[] args) { ArrayList toDoList = new ArrayList(100); while (!userInput.equals("bye")) { - if (!containsNumbers(userInput)){ + if (!containsNumbers(userInput)) { if (userInput.equals("list")) { System.out.println("Here are the tasks in your list:\n"); for (Task item : toDoList) { - System.out.print((toDoList.indexOf(item)+1) + "."); + System.out.print((toDoList.indexOf(item) + 1) + "."); System.out.print(item.getStatusIcon()); System.out.println(item.getDescription()); } @@ -56,18 +56,18 @@ public static void main(String[] args) { } } else { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; - if (userInput.contains("unmark")){ + if (userInput.contains("unmark")) { toDoList.get(itemNumber).markAsUnDone(); System.out.println("OK, I've marked this task as not done yet:\n" + " " + toDoList.get(itemNumber).getStatusIcon() + toDoList.get(itemNumber).getDescription()); } else { toDoList.get(itemNumber).markAsDone(); System.out.println("Nice! I've marked this task as done:\n" + " " - + toDoList.get(itemNumber).getStatusIcon() + toDoList.get(itemNumber).getDescription()); + + toDoList.get(itemNumber).getStatusIcon() + toDoList.get(itemNumber).getDescription()); } } userInput = myObj.nextLine(); } System.out.println(goodBye); } -} +} \ No newline at end of file From b88f2efab166ac6e5527f9f4a9e6fe10341e7994 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Wed, 8 Feb 2023 23:24:06 +0800 Subject: [PATCH 06/16] Level-4,ToDos-Events-Deadlines --- src/main/java/Deadline.java | 14 +++++++ src/main/java/Duke.java | 81 ++++++++++++++++++++++++------------- src/main/java/Event.java | 16 ++++++++ src/main/java/Task.java | 9 ++++- 4 files changed, 90 insertions(+), 30 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..99e770baf --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,14 @@ +public class Deadline extends Task { + + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + super.getStatusIcon() + super.getDescription() + "(" + by + ")"; + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3ea29e62e..15de2ec30 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,5 +1,5 @@ import java.util.Scanner; -import java.util.*; +import java.util.ArrayList; public class Duke { @@ -23,49 +23,74 @@ public static void main(String[] args) { + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - String greeting = ("____________________________________________________________\n" - + "Hello! I'm Duke\n" + "What can I do for you?\n" - + "____________________________________________________________\n" - ); + String line = "____________________________________________________________\n"; - String goodBye = ("____________________________________________________________\n" - + "Bye. Hope to see you again soon!\n" - + "____________________________________________________________\n"); + String greeting = (line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); + + String goodBye = (line + "Bye. Hope to see you again soon!\n" + line); System.out.println(greeting); Scanner myObj = new Scanner(System.in); String userInput; + String userInputParts[]; + String dummy; userInput = myObj.nextLine(); - ArrayList toDoList = new ArrayList(100); + ArrayList taskList = new ArrayList(100); while (!userInput.equals("bye")) { - if (!containsNumbers(userInput)) { - if (userInput.equals("list")) { - System.out.println("Here are the tasks in your list:\n"); - for (Task item : toDoList) { - System.out.print((toDoList.indexOf(item) + 1) + "."); - System.out.print(item.getStatusIcon()); - System.out.println(item.getDescription()); - } - System.out.println("____________________________________________________________\n"); - } else { - System.out.println("____________________________________________________________\n" - + "added:" + userInput + "\n" - + "____________________________________________________________"); - toDoList.add(new Task(userInput)); + if (userInput.equals("list")) { + System.out.println("Here are the tasks in your list:\n"); + for (Task item : taskList) { + System.out.print((taskList.indexOf(item) + 1) + "."); + System.out.println(item.toString()); } - } else { + System.out.println(line); + } + + //For marking and unmarking items in the list + else if(userInput.contains("mark")) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; if (userInput.contains("unmark")) { - toDoList.get(itemNumber).markAsUnDone(); + taskList.get(itemNumber).markAsUnDone(); System.out.println("OK, I've marked this task as not done yet:\n" + " " - + toDoList.get(itemNumber).getStatusIcon() + toDoList.get(itemNumber).getDescription()); + + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); + System.out.println(line); } else { - toDoList.get(itemNumber).markAsDone(); + taskList.get(itemNumber).markAsDone(); System.out.println("Nice! I've marked this task as done:\n" + " " - + toDoList.get(itemNumber).getStatusIcon() + toDoList.get(itemNumber).getDescription()); + + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); + System.out.println(line); } + } else if (userInput.contains("event")){ + userInput = userInput.replace("event",""); + userInputParts = userInput.split("/"); + Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); + taskList.add(event); + System.out.println("Got it. I've added this task: "); + System.out.println("[E][ ] " + event.description + "(" + event.from + event.to + ")"); + System.out.println("Now you have " + taskList.size() + " tasks in the list. "); + System.out.println(line); + } else if (userInput.contains("deadline")) { + userInput = userInput.replace("deadline",""); + userInputParts = userInput.split("/"); + Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); + taskList.add(deadline); + System.out.println("[D][ ] " + deadline.description + "(" + userInputParts[1] + ")"); + System.out.println("Now you have " + taskList.size() + " tasks in the list. "); + System.out.println(line); + } else if (userInput.contains("todo")){ + userInput = userInput.replace("todo",""); + Task task = new Task(userInput); + taskList.add(task); + System.out.println("Got it. I've added this task: "); + System.out.println("[T][ ] " + task.description); + System.out.println("Now you have " + taskList.size() + " tasks in the list. "); + System.out.println(line); + } else { + System.out.println("Please enter a valid command!"); + System.out.println(line); } + userInput = myObj.nextLine(); } System.out.println(goodBye); diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..86cc2ef35 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,16 @@ +public class Event extends Task{ + protected String from; + protected String to; + + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + } + + @Override + public String toString(){ + return "[E]" + super.getStatusIcon() + super.getDescription() + "(" + from + to + ")"; + } + +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 5b147c950..4bc8afd11 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,7 +1,6 @@ public class Task { protected String description; protected boolean isDone; - protected int taskNumber; public String getDescription() { return description; @@ -13,10 +12,12 @@ public Task(String description) { } public void markAsDone() { + this.isDone = true; } - public void markAsUnDone() { + public void markAsUnDone() + { this.isDone = false; } @@ -24,6 +25,10 @@ public String getStatusIcon() { return (isDone ? "[X]" : "[ ]"); // mark done task with X } + @Override + public String toString(){ + return "[T]" + this.getStatusIcon() + description; + } } From f5c800f1ac6da121532030a5dbbeffc38c50f7f4 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Wed, 8 Feb 2023 23:43:54 +0800 Subject: [PATCH 07/16] Level-5,Handle-Errors --- src/main/java/Duke.java | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 15de2ec30..1f7ee989b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,18 +3,6 @@ public class Duke { - public static boolean containsNumbers(String string) { - if (string == null || string.isEmpty()) { - return false; - } - for (int i = 0; i < string.length(); ++i) { - if (Character.isDigit(string.charAt(i))) { - return true; - } - } - return false; - } - public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -33,7 +21,6 @@ public static void main(String[] args) { Scanner myObj = new Scanner(System.in); String userInput; String userInputParts[]; - String dummy; userInput = myObj.nextLine(); ArrayList taskList = new ArrayList(100); @@ -57,8 +44,7 @@ else if(userInput.contains("mark")) { System.out.println(line); } else { taskList.get(itemNumber).markAsDone(); - System.out.println("Nice! I've marked this task as done:\n" + " " - + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); + System.out.println("Nice! I've marked this task as done:\n" + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); System.out.println(line); } } else if (userInput.contains("event")){ @@ -80,6 +66,12 @@ else if(userInput.contains("mark")) { System.out.println(line); } else if (userInput.contains("todo")){ userInput = userInput.replace("todo",""); + userInput = userInput.replace(" ", ""); + if (userInput.isEmpty()){ + System.out.println("Please do not leave the description empty!"); + System.out.println(line); + continue; + } Task task = new Task(userInput); taskList.add(task); System.out.println("Got it. I've added this task: "); From c88df2af339f1b07cd22cbbd137c0c0d673bdaf3 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Tue, 28 Feb 2023 00:30:48 +0800 Subject: [PATCH 08/16] Modularized code, shortened main --- src/main/java/Commands/Text.java | 38 +++++++++++ src/main/java/Commands/command.java | 97 +++++++++++++++++++++++++++++ src/main/java/Commands/error.java | 26 ++++++++ src/main/java/Deadline.java | 14 ----- src/main/java/Duke.java | 88 +++++++------------------- src/main/java/Event.java | 16 ----- src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/Tasks/Deadline.java | 28 +++++++++ src/main/java/Tasks/Event.java | 35 +++++++++++ src/main/java/{ => Tasks}/Task.java | 9 ++- 10 files changed, 256 insertions(+), 98 deletions(-) create mode 100644 src/main/java/Commands/Text.java create mode 100644 src/main/java/Commands/command.java create mode 100644 src/main/java/Commands/error.java delete mode 100644 src/main/java/Deadline.java delete mode 100644 src/main/java/Event.java create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/Tasks/Deadline.java create mode 100644 src/main/java/Tasks/Event.java rename src/main/java/{ => Tasks}/Task.java (71%) diff --git a/src/main/java/Commands/Text.java b/src/main/java/Commands/Text.java new file mode 100644 index 000000000..18b942795 --- /dev/null +++ b/src/main/java/Commands/Text.java @@ -0,0 +1,38 @@ +package Commands; + +public class Text { + static String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + + static String line = "____________________________________________________________\n"; + static String greeting = (line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); + + static String goodBye = (line + "Bye. Hope to see you again soon!\n" + line); + + public static void printLogo(){ + System.out.println("Hello from\n" + logo); + System.out.println(greeting); + printHelp(); + } + + public static void printBye(){ + System.out.println(goodBye); + } + + public static void printHelp(){ + System.out.println("Here are the list of commands:"); + System.out.println("1. list (to see the current tasks saved)"); + System.out.println("2. todo (for tasks with no particular time to take note of)"); + System.out.println("3. event /from /to "); + System.out.println("4. deadline /by "); + System.out.println("5. mark (to mark a task as done)"); + System.out.println("6. unmark (to mark a task as not done)"); + System.out.println("7. delete (to remove a task from the list)"); + System.out.println("8. bye (to terminate the programme)"); + System.out.println(line); + } + +} diff --git a/src/main/java/Commands/command.java b/src/main/java/Commands/command.java new file mode 100644 index 000000000..d81e1a656 --- /dev/null +++ b/src/main/java/Commands/command.java @@ -0,0 +1,97 @@ +package Commands; +import Tasks.Deadline; +import Tasks.Event; +import Tasks.Task; +import java.util.ArrayList; +import static Commands.Text.line; + +public class command { + + public static void printList(ArrayList taskList) { + if (taskList.isEmpty()) { + error.listIsEmpty(); + error.invalidCommand(); + } else { + System.out.println("Here are the tasks in your list:\n"); + for (Task item : taskList) { + System.out.print((taskList.indexOf(item) + 1) + "."); + System.out.println(item.toString()); + } + System.out.println(line); + } + } + + public static void deleteItem(String userInput, ArrayList taskList) { + int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + if (itemNumber >= taskList.size()) { + error.indexNotFound(); + } else { + System.out.println(line); + System.out.println("Noted. I've removed this task:\n" + taskList.get(itemNumber).getSymbol() + + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription() + '\n'); + taskList.remove(itemNumber); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + System.out.println(line); + } + } + + public static void markItem(String userInput, ArrayList taskList) { + int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + if (itemNumber >= taskList.size()){ + error.indexNotFound(); + } else { + taskList.get(itemNumber).markAsDone(); + System.out.println("Nice! I've marked this task as done:\n" + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); + System.out.println(line); + } + } + + public static void unMarkItem(String userInput, ArrayList taskList) { + int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + if (itemNumber >= taskList.size()) { + error.indexNotFound(); + } else { + taskList.get(itemNumber).markAsUnDone(); + System.out.println("OK, I've marked this task as not done yet:\n" + " " + + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); + System.out.println(line); + } + } + + public static void createEvent(String userInput, ArrayList taskList){ + String userInputParts[]; + userInput = userInput.replace("event",""); + userInputParts = userInput.split("/"); + Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); + taskList.add(event); + System.out.println("Got it. I've added this task: "); + System.out.println(event.getSymbol() + event.getStatusIcon() + event.getDescription()); + System.out.println("Now you have " + taskList.size() + " tasks in the list. "); + System.out.println(line); + } + public static void createDeadline(String userInput, ArrayList taskList){ + String userInputParts[]; + userInput = userInput.replace("deadline",""); + userInputParts = userInput.split("/"); + Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); + taskList.add(deadline); + System.out.println(deadline.getSymbol() + deadline.getStatusIcon() + deadline.getDescription()); + System.out.println("Now you have " + taskList.size() + " tasks in the list. "); + System.out.println(line); + } + + public static void createToDo(String userInput, ArrayList taskList){ + userInput = userInput.replace("todo ",""); + String dummy = userInput.replace(" ",""); + if (dummy.isEmpty()){ + error.emptyDescription(); + } else { + Task task = new Task(userInput); + taskList.add(task); + System.out.println("Got it. I've added this task: "); + System.out.println(task.getSymbol() + task.getStatusIcon() + " " + task.getDescription()); + System.out.println("Now you have " + taskList.size() + " tasks in the list. "); + System.out.println(line); + } + } +} diff --git a/src/main/java/Commands/error.java b/src/main/java/Commands/error.java new file mode 100644 index 000000000..b146ec6bb --- /dev/null +++ b/src/main/java/Commands/error.java @@ -0,0 +1,26 @@ +package Commands; + +import static Commands.Text.line; + +public class error { + + public static void emptyDescription(){ + System.out.println("Please do not leave the description empty!"); + System.out.println(line); + } + public static void invalidCommand(){ + System.out.println("Please enter a valid command!"); + System.out.println(line); + } + + public static void indexNotFound(){ + System.out.println("Please ensure the number you have entered is within the list!"); + System.out.println(line); + } + + public static void listIsEmpty(){ + System.out.println("Invalid command! The list is empty!"); + System.out.println(line); + } + +} diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java deleted file mode 100644 index 99e770baf..000000000 --- a/src/main/java/Deadline.java +++ /dev/null @@ -1,14 +0,0 @@ -public class Deadline extends Task { - - protected String by; - - public Deadline(String description, String by) { - super(description); - this.by = by; - } - - @Override - public String toString() { - return "[D]" + super.getStatusIcon() + super.getDescription() + "(" + by + ")"; - } -} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 1f7ee989b..b2a07b542 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,90 +1,44 @@ +import Commands.Text; +import Commands.command; +import Commands.error; +import Tasks.Task; +import java.io.File; +import java.io.FileNotFoundException; import java.util.Scanner; import java.util.ArrayList; -public class -Duke { +public class Duke { public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - - String line = "____________________________________________________________\n"; - - String greeting = (line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); - - String goodBye = (line + "Bye. Hope to see you again soon!\n" + line); - - System.out.println(greeting); + Text.printLogo(); Scanner myObj = new Scanner(System.in); String userInput; - String userInputParts[]; userInput = myObj.nextLine(); ArrayList taskList = new ArrayList(100); - while (!userInput.equals("bye")) { if (userInput.equals("list")) { - System.out.println("Here are the tasks in your list:\n"); - for (Task item : taskList) { - System.out.print((taskList.indexOf(item) + 1) + "."); - System.out.println(item.toString()); - } - System.out.println(line); + command.printList(taskList); } - - //For marking and unmarking items in the list - else if(userInput.contains("mark")) { - int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + else if(userInput.contains("delete")){ + command.deleteItem(userInput, taskList); + } else if(userInput.contains("mark")) { if (userInput.contains("unmark")) { - taskList.get(itemNumber).markAsUnDone(); - System.out.println("OK, I've marked this task as not done yet:\n" + " " - + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); - System.out.println(line); + command.unMarkItem(userInput, taskList); } else { - taskList.get(itemNumber).markAsDone(); - System.out.println("Nice! I've marked this task as done:\n" + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); - System.out.println(line); + command.markItem(userInput, taskList); } } else if (userInput.contains("event")){ - userInput = userInput.replace("event",""); - userInputParts = userInput.split("/"); - Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); - taskList.add(event); - System.out.println("Got it. I've added this task: "); - System.out.println("[E][ ] " + event.description + "(" + event.from + event.to + ")"); - System.out.println("Now you have " + taskList.size() + " tasks in the list. "); - System.out.println(line); + command.createEvent(userInput,taskList); } else if (userInput.contains("deadline")) { - userInput = userInput.replace("deadline",""); - userInputParts = userInput.split("/"); - Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); - taskList.add(deadline); - System.out.println("[D][ ] " + deadline.description + "(" + userInputParts[1] + ")"); - System.out.println("Now you have " + taskList.size() + " tasks in the list. "); - System.out.println(line); + command.createDeadline(userInput, taskList); } else if (userInput.contains("todo")){ - userInput = userInput.replace("todo",""); - userInput = userInput.replace(" ", ""); - if (userInput.isEmpty()){ - System.out.println("Please do not leave the description empty!"); - System.out.println(line); - continue; - } - Task task = new Task(userInput); - taskList.add(task); - System.out.println("Got it. I've added this task: "); - System.out.println("[T][ ] " + task.description); - System.out.println("Now you have " + taskList.size() + " tasks in the list. "); - System.out.println(line); + command.createToDo(userInput,taskList); } else { - System.out.println("Please enter a valid command!"); - System.out.println(line); + error.invalidCommand(); + Text.printHelp(); } - + System.out.println("What would you like to do?"); userInput = myObj.nextLine(); } - System.out.println(goodBye); + Text.printBye(); } } \ No newline at end of file diff --git a/src/main/java/Event.java b/src/main/java/Event.java deleted file mode 100644 index 86cc2ef35..000000000 --- a/src/main/java/Event.java +++ /dev/null @@ -1,16 +0,0 @@ -public class Event extends Task{ - protected String from; - protected String to; - - public Event(String description, String from, String to) { - super(description); - this.from = from; - this.to = to; - } - - @Override - public String toString(){ - return "[E]" + super.getStatusIcon() + super.getDescription() + "(" + from + to + ")"; - } - -} diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..d2ffd5b4d --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + diff --git a/src/main/java/Tasks/Deadline.java b/src/main/java/Tasks/Deadline.java new file mode 100644 index 000000000..d361907da --- /dev/null +++ b/src/main/java/Tasks/Deadline.java @@ -0,0 +1,28 @@ +package Tasks; + +import Tasks.Task; + +public class Deadline extends Task { + + protected String by; + protected String symbol = "[D]"; + + public String getDescription() { + return super.getDescription(); + } + + public String getSymbol() { + return symbol; + } + + public Deadline(String description, String by) { + super(description); + this.by = by; + this.description = description + "(" + by + ")"; + } + + @Override + public String toString() { + return "[D]" + super.getStatusIcon() + super.getDescription() ; + } +} diff --git a/src/main/java/Tasks/Event.java b/src/main/java/Tasks/Event.java new file mode 100644 index 000000000..0ae0398e2 --- /dev/null +++ b/src/main/java/Tasks/Event.java @@ -0,0 +1,35 @@ +package Tasks; + +import Tasks.Task; + +public class Event extends Task { + protected String from; + protected String to; + protected String symbol = "[E]"; + + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + this.description = description + "(" + from + to + ")"; + } + + @Override + public String getSymbol() { + return symbol; + } + + public String getFrom() { + return from; + } + + public String getTo() { + return to; + } + + @Override + public String toString(){ + return "[E]" + super.getStatusIcon() + super.getDescription() + "(" + from + to + ")"; + } + +} diff --git a/src/main/java/Task.java b/src/main/java/Tasks/Task.java similarity index 71% rename from src/main/java/Task.java rename to src/main/java/Tasks/Task.java index 4bc8afd11..95418144b 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Tasks/Task.java @@ -1,6 +1,9 @@ +package Tasks; + public class Task { protected String description; protected boolean isDone; + protected String symbol = "[T]"; public String getDescription() { return description; @@ -11,6 +14,10 @@ public Task(String description) { this.isDone = false; } + public String getSymbol() { + return symbol; + } + public void markAsDone() { this.isDone = true; @@ -27,7 +34,7 @@ public String getStatusIcon() { @Override public String toString(){ - return "[T]" + this.getStatusIcon() + description; + return "[T]" + this.getStatusIcon() + " " + getDescription(); } } From bd2b27f519e9737bc61e96a8780e5df6168f33bf Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Wed, 1 Mar 2023 14:30:27 +0800 Subject: [PATCH 09/16] Added comments to explain code --- src/main/java/Commands/Text.java | 2 -- src/main/java/Commands/command.java | 12 ++++++++++-- src/main/java/Duke.java | 19 +++++++++---------- src/main/java/Tasks/Task.java | 4 +--- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main/java/Commands/Text.java b/src/main/java/Commands/Text.java index 18b942795..076225578 100644 --- a/src/main/java/Commands/Text.java +++ b/src/main/java/Commands/Text.java @@ -6,10 +6,8 @@ public class Text { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - static String line = "____________________________________________________________\n"; static String greeting = (line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); - static String goodBye = (line + "Bye. Hope to see you again soon!\n" + line); public static void printLogo(){ diff --git a/src/main/java/Commands/command.java b/src/main/java/Commands/command.java index d81e1a656..7bedc037f 100644 --- a/src/main/java/Commands/command.java +++ b/src/main/java/Commands/command.java @@ -4,10 +4,13 @@ import Tasks.Task; import java.util.ArrayList; import static Commands.Text.line; +import java.io.FileWriter; +import java.io.IOException; public class command { public static void printList(ArrayList taskList) { + //checks if list is empty first if (taskList.isEmpty()) { error.listIsEmpty(); error.invalidCommand(); @@ -23,6 +26,7 @@ public static void printList(ArrayList taskList) { public static void deleteItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + //to check if an item is within the index of the list if (itemNumber >= taskList.size()) { error.indexNotFound(); } else { @@ -37,6 +41,7 @@ public static void deleteItem(String userInput, ArrayList taskList) { public static void markItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + //to check if an item is within the index of the list if (itemNumber >= taskList.size()){ error.indexNotFound(); } else { @@ -48,6 +53,7 @@ public static void markItem(String userInput, ArrayList taskList) { public static void unMarkItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + //to check if an item is within the index of the list if (itemNumber >= taskList.size()) { error.indexNotFound(); } else { @@ -59,7 +65,7 @@ public static void unMarkItem(String userInput, ArrayList taskList) { } public static void createEvent(String userInput, ArrayList taskList){ - String userInputParts[]; + String[] userInputParts; userInput = userInput.replace("event",""); userInputParts = userInput.split("/"); Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); @@ -69,8 +75,9 @@ public static void createEvent(String userInput, ArrayList taskList){ System.out.println("Now you have " + taskList.size() + " tasks in the list. "); System.out.println(line); } + public static void createDeadline(String userInput, ArrayList taskList){ - String userInputParts[]; + String[] userInputParts; userInput = userInput.replace("deadline",""); userInputParts = userInput.split("/"); Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); @@ -83,6 +90,7 @@ public static void createDeadline(String userInput, ArrayList taskList){ public static void createToDo(String userInput, ArrayList taskList){ userInput = userInput.replace("todo ",""); String dummy = userInput.replace(" ",""); + //checks if description is empty and throws warning back to user if (dummy.isEmpty()){ error.emptyDescription(); } else { diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b2a07b542..baa838bfc 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -14,30 +14,29 @@ public static void main(String[] args) { String userInput; userInput = myObj.nextLine(); ArrayList taskList = new ArrayList(100); - while (!userInput.equals("bye")) { - if (userInput.equals("list")) { + while (!userInput.equals("bye")) { //if user inputs "by" the loop will break + if (userInput.equals("list")) { //to list all tasks command.printList(taskList); - } - else if(userInput.contains("delete")){ + } else if(userInput.contains("delete")){ //to delete an item from the list command.deleteItem(userInput, taskList); - } else if(userInput.contains("mark")) { + } else if(userInput.contains("mark")) { //to check if an input is to mark/unmark an item if (userInput.contains("unmark")) { command.unMarkItem(userInput, taskList); } else { command.markItem(userInput, taskList); } - } else if (userInput.contains("event")){ + } else if (userInput.contains("event")){ //to add an event to the list command.createEvent(userInput,taskList); - } else if (userInput.contains("deadline")) { + } else if (userInput.contains("deadline")) { //to add a deadline to the list command.createDeadline(userInput, taskList); - } else if (userInput.contains("todo")){ + } else if (userInput.contains("todo")){ //to add to do item to the list command.createToDo(userInput,taskList); - } else { + } else { //For any command that is not within the list of commands error.invalidCommand(); Text.printHelp(); } System.out.println("What would you like to do?"); - userInput = myObj.nextLine(); + userInput = myObj.nextLine(); //to take in next input } Text.printBye(); } diff --git a/src/main/java/Tasks/Task.java b/src/main/java/Tasks/Task.java index 95418144b..c3a145a1e 100644 --- a/src/main/java/Tasks/Task.java +++ b/src/main/java/Tasks/Task.java @@ -19,12 +19,10 @@ public String getSymbol() { } public void markAsDone() { - this.isDone = true; } - public void markAsUnDone() - { + public void markAsUnDone() { this.isDone = false; } From fcc0a6140482717ef8251a40bb9c0b91be467c40 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Wed, 1 Mar 2023 14:31:16 +0800 Subject: [PATCH 10/16] Revert "Added comments to explain code" This reverts commit bd2b27f519e9737bc61e96a8780e5df6168f33bf. --- src/main/java/Commands/Text.java | 2 ++ src/main/java/Commands/command.java | 12 ++---------- src/main/java/Duke.java | 19 ++++++++++--------- src/main/java/Tasks/Task.java | 4 +++- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/main/java/Commands/Text.java b/src/main/java/Commands/Text.java index 076225578..18b942795 100644 --- a/src/main/java/Commands/Text.java +++ b/src/main/java/Commands/Text.java @@ -6,8 +6,10 @@ public class Text { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; + static String line = "____________________________________________________________\n"; static String greeting = (line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); + static String goodBye = (line + "Bye. Hope to see you again soon!\n" + line); public static void printLogo(){ diff --git a/src/main/java/Commands/command.java b/src/main/java/Commands/command.java index 7bedc037f..d81e1a656 100644 --- a/src/main/java/Commands/command.java +++ b/src/main/java/Commands/command.java @@ -4,13 +4,10 @@ import Tasks.Task; import java.util.ArrayList; import static Commands.Text.line; -import java.io.FileWriter; -import java.io.IOException; public class command { public static void printList(ArrayList taskList) { - //checks if list is empty first if (taskList.isEmpty()) { error.listIsEmpty(); error.invalidCommand(); @@ -26,7 +23,6 @@ public static void printList(ArrayList taskList) { public static void deleteItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; - //to check if an item is within the index of the list if (itemNumber >= taskList.size()) { error.indexNotFound(); } else { @@ -41,7 +37,6 @@ public static void deleteItem(String userInput, ArrayList taskList) { public static void markItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; - //to check if an item is within the index of the list if (itemNumber >= taskList.size()){ error.indexNotFound(); } else { @@ -53,7 +48,6 @@ public static void markItem(String userInput, ArrayList taskList) { public static void unMarkItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; - //to check if an item is within the index of the list if (itemNumber >= taskList.size()) { error.indexNotFound(); } else { @@ -65,7 +59,7 @@ public static void unMarkItem(String userInput, ArrayList taskList) { } public static void createEvent(String userInput, ArrayList taskList){ - String[] userInputParts; + String userInputParts[]; userInput = userInput.replace("event",""); userInputParts = userInput.split("/"); Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); @@ -75,9 +69,8 @@ public static void createEvent(String userInput, ArrayList taskList){ System.out.println("Now you have " + taskList.size() + " tasks in the list. "); System.out.println(line); } - public static void createDeadline(String userInput, ArrayList taskList){ - String[] userInputParts; + String userInputParts[]; userInput = userInput.replace("deadline",""); userInputParts = userInput.split("/"); Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); @@ -90,7 +83,6 @@ public static void createDeadline(String userInput, ArrayList taskList){ public static void createToDo(String userInput, ArrayList taskList){ userInput = userInput.replace("todo ",""); String dummy = userInput.replace(" ",""); - //checks if description is empty and throws warning back to user if (dummy.isEmpty()){ error.emptyDescription(); } else { diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index baa838bfc..b2a07b542 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -14,29 +14,30 @@ public static void main(String[] args) { String userInput; userInput = myObj.nextLine(); ArrayList taskList = new ArrayList(100); - while (!userInput.equals("bye")) { //if user inputs "by" the loop will break - if (userInput.equals("list")) { //to list all tasks + while (!userInput.equals("bye")) { + if (userInput.equals("list")) { command.printList(taskList); - } else if(userInput.contains("delete")){ //to delete an item from the list + } + else if(userInput.contains("delete")){ command.deleteItem(userInput, taskList); - } else if(userInput.contains("mark")) { //to check if an input is to mark/unmark an item + } else if(userInput.contains("mark")) { if (userInput.contains("unmark")) { command.unMarkItem(userInput, taskList); } else { command.markItem(userInput, taskList); } - } else if (userInput.contains("event")){ //to add an event to the list + } else if (userInput.contains("event")){ command.createEvent(userInput,taskList); - } else if (userInput.contains("deadline")) { //to add a deadline to the list + } else if (userInput.contains("deadline")) { command.createDeadline(userInput, taskList); - } else if (userInput.contains("todo")){ //to add to do item to the list + } else if (userInput.contains("todo")){ command.createToDo(userInput,taskList); - } else { //For any command that is not within the list of commands + } else { error.invalidCommand(); Text.printHelp(); } System.out.println("What would you like to do?"); - userInput = myObj.nextLine(); //to take in next input + userInput = myObj.nextLine(); } Text.printBye(); } diff --git a/src/main/java/Tasks/Task.java b/src/main/java/Tasks/Task.java index c3a145a1e..95418144b 100644 --- a/src/main/java/Tasks/Task.java +++ b/src/main/java/Tasks/Task.java @@ -19,10 +19,12 @@ public String getSymbol() { } public void markAsDone() { + this.isDone = true; } - public void markAsUnDone() { + public void markAsUnDone() + { this.isDone = false; } From 502d1bc50fbc80c1f0343216ac9faedb35f0dedc Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Wed, 1 Mar 2023 14:31:24 +0800 Subject: [PATCH 11/16] Revert "Revert "Added comments to explain code"" This reverts commit fcc0a6140482717ef8251a40bb9c0b91be467c40. --- src/main/java/Commands/Text.java | 2 -- src/main/java/Commands/command.java | 12 ++++++++++-- src/main/java/Duke.java | 19 +++++++++---------- src/main/java/Tasks/Task.java | 4 +--- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main/java/Commands/Text.java b/src/main/java/Commands/Text.java index 18b942795..076225578 100644 --- a/src/main/java/Commands/Text.java +++ b/src/main/java/Commands/Text.java @@ -6,10 +6,8 @@ public class Text { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - static String line = "____________________________________________________________\n"; static String greeting = (line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); - static String goodBye = (line + "Bye. Hope to see you again soon!\n" + line); public static void printLogo(){ diff --git a/src/main/java/Commands/command.java b/src/main/java/Commands/command.java index d81e1a656..7bedc037f 100644 --- a/src/main/java/Commands/command.java +++ b/src/main/java/Commands/command.java @@ -4,10 +4,13 @@ import Tasks.Task; import java.util.ArrayList; import static Commands.Text.line; +import java.io.FileWriter; +import java.io.IOException; public class command { public static void printList(ArrayList taskList) { + //checks if list is empty first if (taskList.isEmpty()) { error.listIsEmpty(); error.invalidCommand(); @@ -23,6 +26,7 @@ public static void printList(ArrayList taskList) { public static void deleteItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + //to check if an item is within the index of the list if (itemNumber >= taskList.size()) { error.indexNotFound(); } else { @@ -37,6 +41,7 @@ public static void deleteItem(String userInput, ArrayList taskList) { public static void markItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + //to check if an item is within the index of the list if (itemNumber >= taskList.size()){ error.indexNotFound(); } else { @@ -48,6 +53,7 @@ public static void markItem(String userInput, ArrayList taskList) { public static void unMarkItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + //to check if an item is within the index of the list if (itemNumber >= taskList.size()) { error.indexNotFound(); } else { @@ -59,7 +65,7 @@ public static void unMarkItem(String userInput, ArrayList taskList) { } public static void createEvent(String userInput, ArrayList taskList){ - String userInputParts[]; + String[] userInputParts; userInput = userInput.replace("event",""); userInputParts = userInput.split("/"); Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); @@ -69,8 +75,9 @@ public static void createEvent(String userInput, ArrayList taskList){ System.out.println("Now you have " + taskList.size() + " tasks in the list. "); System.out.println(line); } + public static void createDeadline(String userInput, ArrayList taskList){ - String userInputParts[]; + String[] userInputParts; userInput = userInput.replace("deadline",""); userInputParts = userInput.split("/"); Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); @@ -83,6 +90,7 @@ public static void createDeadline(String userInput, ArrayList taskList){ public static void createToDo(String userInput, ArrayList taskList){ userInput = userInput.replace("todo ",""); String dummy = userInput.replace(" ",""); + //checks if description is empty and throws warning back to user if (dummy.isEmpty()){ error.emptyDescription(); } else { diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b2a07b542..baa838bfc 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -14,30 +14,29 @@ public static void main(String[] args) { String userInput; userInput = myObj.nextLine(); ArrayList taskList = new ArrayList(100); - while (!userInput.equals("bye")) { - if (userInput.equals("list")) { + while (!userInput.equals("bye")) { //if user inputs "by" the loop will break + if (userInput.equals("list")) { //to list all tasks command.printList(taskList); - } - else if(userInput.contains("delete")){ + } else if(userInput.contains("delete")){ //to delete an item from the list command.deleteItem(userInput, taskList); - } else if(userInput.contains("mark")) { + } else if(userInput.contains("mark")) { //to check if an input is to mark/unmark an item if (userInput.contains("unmark")) { command.unMarkItem(userInput, taskList); } else { command.markItem(userInput, taskList); } - } else if (userInput.contains("event")){ + } else if (userInput.contains("event")){ //to add an event to the list command.createEvent(userInput,taskList); - } else if (userInput.contains("deadline")) { + } else if (userInput.contains("deadline")) { //to add a deadline to the list command.createDeadline(userInput, taskList); - } else if (userInput.contains("todo")){ + } else if (userInput.contains("todo")){ //to add to do item to the list command.createToDo(userInput,taskList); - } else { + } else { //For any command that is not within the list of commands error.invalidCommand(); Text.printHelp(); } System.out.println("What would you like to do?"); - userInput = myObj.nextLine(); + userInput = myObj.nextLine(); //to take in next input } Text.printBye(); } diff --git a/src/main/java/Tasks/Task.java b/src/main/java/Tasks/Task.java index 95418144b..c3a145a1e 100644 --- a/src/main/java/Tasks/Task.java +++ b/src/main/java/Tasks/Task.java @@ -19,12 +19,10 @@ public String getSymbol() { } public void markAsDone() { - this.isDone = true; } - public void markAsUnDone() - { + public void markAsUnDone() { this.isDone = false; } From 8ba61934d5f1f1af2444810acae5893957289b00 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Wed, 1 Mar 2023 14:31:29 +0800 Subject: [PATCH 12/16] Revert "Revert "Revert "Added comments to explain code""" This reverts commit 502d1bc50fbc80c1f0343216ac9faedb35f0dedc. --- src/main/java/Commands/Text.java | 2 ++ src/main/java/Commands/command.java | 12 ++---------- src/main/java/Duke.java | 19 ++++++++++--------- src/main/java/Tasks/Task.java | 4 +++- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/main/java/Commands/Text.java b/src/main/java/Commands/Text.java index 076225578..18b942795 100644 --- a/src/main/java/Commands/Text.java +++ b/src/main/java/Commands/Text.java @@ -6,8 +6,10 @@ public class Text { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; + static String line = "____________________________________________________________\n"; static String greeting = (line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); + static String goodBye = (line + "Bye. Hope to see you again soon!\n" + line); public static void printLogo(){ diff --git a/src/main/java/Commands/command.java b/src/main/java/Commands/command.java index 7bedc037f..d81e1a656 100644 --- a/src/main/java/Commands/command.java +++ b/src/main/java/Commands/command.java @@ -4,13 +4,10 @@ import Tasks.Task; import java.util.ArrayList; import static Commands.Text.line; -import java.io.FileWriter; -import java.io.IOException; public class command { public static void printList(ArrayList taskList) { - //checks if list is empty first if (taskList.isEmpty()) { error.listIsEmpty(); error.invalidCommand(); @@ -26,7 +23,6 @@ public static void printList(ArrayList taskList) { public static void deleteItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; - //to check if an item is within the index of the list if (itemNumber >= taskList.size()) { error.indexNotFound(); } else { @@ -41,7 +37,6 @@ public static void deleteItem(String userInput, ArrayList taskList) { public static void markItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; - //to check if an item is within the index of the list if (itemNumber >= taskList.size()){ error.indexNotFound(); } else { @@ -53,7 +48,6 @@ public static void markItem(String userInput, ArrayList taskList) { public static void unMarkItem(String userInput, ArrayList taskList) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; - //to check if an item is within the index of the list if (itemNumber >= taskList.size()) { error.indexNotFound(); } else { @@ -65,7 +59,7 @@ public static void unMarkItem(String userInput, ArrayList taskList) { } public static void createEvent(String userInput, ArrayList taskList){ - String[] userInputParts; + String userInputParts[]; userInput = userInput.replace("event",""); userInputParts = userInput.split("/"); Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); @@ -75,9 +69,8 @@ public static void createEvent(String userInput, ArrayList taskList){ System.out.println("Now you have " + taskList.size() + " tasks in the list. "); System.out.println(line); } - public static void createDeadline(String userInput, ArrayList taskList){ - String[] userInputParts; + String userInputParts[]; userInput = userInput.replace("deadline",""); userInputParts = userInput.split("/"); Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); @@ -90,7 +83,6 @@ public static void createDeadline(String userInput, ArrayList taskList){ public static void createToDo(String userInput, ArrayList taskList){ userInput = userInput.replace("todo ",""); String dummy = userInput.replace(" ",""); - //checks if description is empty and throws warning back to user if (dummy.isEmpty()){ error.emptyDescription(); } else { diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index baa838bfc..b2a07b542 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -14,29 +14,30 @@ public static void main(String[] args) { String userInput; userInput = myObj.nextLine(); ArrayList taskList = new ArrayList(100); - while (!userInput.equals("bye")) { //if user inputs "by" the loop will break - if (userInput.equals("list")) { //to list all tasks + while (!userInput.equals("bye")) { + if (userInput.equals("list")) { command.printList(taskList); - } else if(userInput.contains("delete")){ //to delete an item from the list + } + else if(userInput.contains("delete")){ command.deleteItem(userInput, taskList); - } else if(userInput.contains("mark")) { //to check if an input is to mark/unmark an item + } else if(userInput.contains("mark")) { if (userInput.contains("unmark")) { command.unMarkItem(userInput, taskList); } else { command.markItem(userInput, taskList); } - } else if (userInput.contains("event")){ //to add an event to the list + } else if (userInput.contains("event")){ command.createEvent(userInput,taskList); - } else if (userInput.contains("deadline")) { //to add a deadline to the list + } else if (userInput.contains("deadline")) { command.createDeadline(userInput, taskList); - } else if (userInput.contains("todo")){ //to add to do item to the list + } else if (userInput.contains("todo")){ command.createToDo(userInput,taskList); - } else { //For any command that is not within the list of commands + } else { error.invalidCommand(); Text.printHelp(); } System.out.println("What would you like to do?"); - userInput = myObj.nextLine(); //to take in next input + userInput = myObj.nextLine(); } Text.printBye(); } diff --git a/src/main/java/Tasks/Task.java b/src/main/java/Tasks/Task.java index c3a145a1e..95418144b 100644 --- a/src/main/java/Tasks/Task.java +++ b/src/main/java/Tasks/Task.java @@ -19,10 +19,12 @@ public String getSymbol() { } public void markAsDone() { + this.isDone = true; } - public void markAsUnDone() { + public void markAsUnDone() + { this.isDone = false; } From 2ad344eeb7422badae937f3acb6445a51164e864 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Fri, 3 Mar 2023 15:56:16 +0800 Subject: [PATCH 13/16] Level-7 Save --- docs/README.md | 49 ++++++----- duke.txt | 3 + src/main/java/Commands/error.java | 26 ------ src/main/java/Duke.java | 52 +++++------- src/main/java/Tasks/Deadline.java | 6 +- src/main/java/Tasks/Event.java | 18 ++--- src/main/java/Tasks/Task.java | 12 ++- src/main/java/User/Parser.java | 38 +++++++++ src/main/java/User/Storage.java | 81 +++++++++++++++++++ .../command.java => User/TaskList.java} | 73 +++++++++++------ .../java/{Commands/Text.java => User/UI.java} | 27 ++++++- 11 files changed, 261 insertions(+), 124 deletions(-) create mode 100644 duke.txt delete mode 100644 src/main/java/Commands/error.java create mode 100644 src/main/java/User/Parser.java create mode 100644 src/main/java/User/Storage.java rename src/main/java/{Commands/command.java => User/TaskList.java} (53%) rename src/main/java/{Commands/Text.java => User/UI.java} (65%) diff --git a/docs/README.md b/docs/README.md index 8077118eb..cf14af59a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,38 @@ -# User Guide - -## Features - -### Feature-ABC - -Description of the feature. - -### Feature-XYZ - -Description of the feature. +# Duke +Welcome to Duke! This is a user guide for the task tracker Duke. + +## Installation +Requires: JDK 11 +1. Download the jar file for Duke under "Releases" +2. Extract it to an empty folder +3. Run the jar file +4. Enjoy! + +### Commands +Below are the list of commands that can be used with Duke +Please remember to follow the input format strictly! + +1. list || (to see the current tasks saved) +2. `todo ` || Adds a task with no particular time to take note of +3. `event /from /to ` || Adds an event with a start and end time +4. `deadline /by ` || Adds a task with a deadline +5. `mark ` || To mark a task as done +6. `unmark ` || To mark a task as not done +7. `delete ` || To remove a task from the list +8. `bye` || To terminate the programme ## Usage -### `Keyword` - Describe action - -Describe the action and its outcome. - Example of usage: -`keyword (optional arguments)` +`event study /from 2pm /tp 4pm` Expected outcome: - -Description of the outcome. - ``` -expected output +Got it. I've added this task: +[E][ ] study (From: 2pm to To: 4pm) +Now you have 1 tasks in the list. +____________________________________________________________ + +What would you like to do? ``` diff --git a/duke.txt b/duke.txt new file mode 100644 index 000000000..3a9579e34 --- /dev/null +++ b/duke.txt @@ -0,0 +1,3 @@ +[ ] : T : study +[X] : E : study (From:now To:later) +[ ] : D : study (By:later) \ No newline at end of file diff --git a/src/main/java/Commands/error.java b/src/main/java/Commands/error.java deleted file mode 100644 index b146ec6bb..000000000 --- a/src/main/java/Commands/error.java +++ /dev/null @@ -1,26 +0,0 @@ -package Commands; - -import static Commands.Text.line; - -public class error { - - public static void emptyDescription(){ - System.out.println("Please do not leave the description empty!"); - System.out.println(line); - } - public static void invalidCommand(){ - System.out.println("Please enter a valid command!"); - System.out.println(line); - } - - public static void indexNotFound(){ - System.out.println("Please ensure the number you have entered is within the list!"); - System.out.println(line); - } - - public static void listIsEmpty(){ - System.out.println("Invalid command! The list is empty!"); - System.out.println(line); - } - -} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b2a07b542..7f00b8269 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,44 +1,32 @@ -import Commands.Text; -import Commands.command; -import Commands.error; import Tasks.Task; -import java.io.File; +import User.Parser; +import User.Storage; +import User.TaskList; +import User.UI; import java.io.FileNotFoundException; -import java.util.Scanner; +import java.io.IOException; import java.util.ArrayList; +import java.util.Scanner; public class Duke { - public static void main(String[] args) { - Text.printLogo(); + + public static void main(String[] args) throws IOException { + try{ + TaskList.taskList = Storage.readFile("duke.txt"); + } catch (FileNotFoundException e) { + System.out.println("File not found, a new one will be created shortly!"); + } + UI.printLogo(); Scanner myObj = new Scanner(System.in); String userInput; userInput = myObj.nextLine(); - ArrayList taskList = new ArrayList(100); - while (!userInput.equals("bye")) { - if (userInput.equals("list")) { - command.printList(taskList); - } - else if(userInput.contains("delete")){ - command.deleteItem(userInput, taskList); - } else if(userInput.contains("mark")) { - if (userInput.contains("unmark")) { - command.unMarkItem(userInput, taskList); - } else { - command.markItem(userInput, taskList); - } - } else if (userInput.contains("event")){ - command.createEvent(userInput,taskList); - } else if (userInput.contains("deadline")) { - command.createDeadline(userInput, taskList); - } else if (userInput.contains("todo")){ - command.createToDo(userInput,taskList); - } else { - error.invalidCommand(); - Text.printHelp(); - } + while (!userInput.equals("bye")) { //if user inputs "bye" the loop will break + Parser.parsing(userInput); System.out.println("What would you like to do?"); - userInput = myObj.nextLine(); + userInput = myObj.nextLine(); //to take in next input } - Text.printBye(); + Storage.clearFile(); + Storage.updateFile(TaskList.taskList); + UI.printBye(); } } \ No newline at end of file diff --git a/src/main/java/Tasks/Deadline.java b/src/main/java/Tasks/Deadline.java index d361907da..397c781de 100644 --- a/src/main/java/Tasks/Deadline.java +++ b/src/main/java/Tasks/Deadline.java @@ -18,11 +18,15 @@ public String getSymbol() { public Deadline(String description, String by) { super(description); this.by = by; - this.description = description + "(" + by + ")"; + this.description = description + "(By:" + by + ")"; } @Override public String toString() { return "[D]" + super.getStatusIcon() + super.getDescription() ; } + @Override + public String toFile() { + return this.getStatusIcon() + " : " +"D"+ " :" + this.description; + } } diff --git a/src/main/java/Tasks/Event.java b/src/main/java/Tasks/Event.java index 0ae0398e2..30ac1e3c3 100644 --- a/src/main/java/Tasks/Event.java +++ b/src/main/java/Tasks/Event.java @@ -1,7 +1,5 @@ package Tasks; -import Tasks.Task; - public class Event extends Task { protected String from; protected String to; @@ -11,7 +9,7 @@ public Event(String description, String from, String to) { super(description); this.from = from; this.to = to; - this.description = description + "(" + from + to + ")"; + this.description = description + "(From:" + this.from + "To:" + this.to + ")"; } @Override @@ -19,17 +17,13 @@ public String getSymbol() { return symbol; } - public String getFrom() { - return from; - } - - public String getTo() { - return to; - } @Override public String toString(){ - return "[E]" + super.getStatusIcon() + super.getDescription() + "(" + from + to + ")"; + return "[E]" + super.getStatusIcon() + super.getDescription(); + } + @Override + public String toFile() { + return this.getStatusIcon() + " : " + "E" + " :" + this.description; } - } diff --git a/src/main/java/Tasks/Task.java b/src/main/java/Tasks/Task.java index 95418144b..2120212d1 100644 --- a/src/main/java/Tasks/Task.java +++ b/src/main/java/Tasks/Task.java @@ -19,12 +19,14 @@ public String getSymbol() { } public void markAsDone() { - this.isDone = true; } - public void markAsUnDone() - { + public boolean isDone() { + return isDone; + } + + public void markAsUnDone() { this.isDone = false; } @@ -36,6 +38,10 @@ public String getStatusIcon() { public String toString(){ return "[T]" + this.getStatusIcon() + " " + getDescription(); } + + public String toFile() { + return this.getStatusIcon() + " : " +"T"+ " : " + this.description; + } } diff --git a/src/main/java/User/Parser.java b/src/main/java/User/Parser.java new file mode 100644 index 000000000..840e643d0 --- /dev/null +++ b/src/main/java/User/Parser.java @@ -0,0 +1,38 @@ +package User; + +public class Parser { + public static void parsing(String userInput){ + String[] inputParts; + inputParts = userInput.split(" ",2); + String command = inputParts[0]; + switch(command){ + case("list"): + TaskList.printList(); + break; + case("delete"): + TaskList.deleteItem(userInput); + break; + case("mark"): + TaskList.markItem(userInput); + break; + case("unmark"): + TaskList.unMarkItem(userInput); + break; + case("event"): + TaskList.createEvent(userInput); + break; + case("todo"): + TaskList.createToDo(userInput); + break; + case("deadline"): + TaskList.createDeadline(userInput); + break; + case("help"): + UI.printHelp(); + break; + default: + UI.invalidCommand(); + break; + } + } +} diff --git a/src/main/java/User/Storage.java b/src/main/java/User/Storage.java new file mode 100644 index 000000000..71b92fb29 --- /dev/null +++ b/src/main/java/User/Storage.java @@ -0,0 +1,81 @@ +package User; +import Tasks.Deadline; +import Tasks.Task; +import Tasks.Event; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + + public static ArrayList readFile(String filePath) throws FileNotFoundException { + ArrayList tempTasks = new ArrayList(100); + String[] userInputParts; + File f = new File(filePath); // create a File for the given file path + Scanner s = new Scanner(f); // create a Scanner using the File as the source + while (s.hasNext()) { + String newLine = s.nextLine(); + String[] newInput = newLine.split(" : ",3); + String userInput = newInput[2]; + String taskType = newInput[1]; + switch (taskType) { + case "T": + Task task = new Task(userInput); + tempTasks.add(task); + break; + case "D": + userInput = userInput.replace("(",""); + userInput = userInput.replace(")",""); + userInputParts = userInput.split("By:"); + Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); + tempTasks.add(deadline); + break; + case "E": + userInput = userInput.replace("(",""); + userInput = userInput.replace(")",""); + userInputParts = userInput.split("From:"); + String description = userInputParts[0]; + userInputParts = userInputParts[1].split("To:"); + Event event = new Event(description, userInputParts[0], userInputParts[1]); + tempTasks.add(event); + break; + default: + UI.printHelp(); + break; + } + if (newInput[0].equals("[X]")) { + tempTasks.get(tempTasks.size() - 1).markAsDone(); + } + } + return tempTasks; + } + + public static void writeToFile(String toFile) throws IOException { + FileWriter fw = new FileWriter("duke.txt",true); + fw.write(toFile); + fw.close(); + } + + public static void clearFile() throws IOException { + FileWriter fw = new FileWriter("duke.txt"); + fw.write(""); + fw.close(); + } + + public static void updateFile(ArrayList taskList) { + for (int i = 0; i < taskList.size(); i++) { + try { + writeToFile(taskList.get(i).toFile()); + if (i < taskList.size()-1){ + writeToFile("\n"); + } + } catch (IOException e) { + System.out.println("ERROR!"); + } + } + } + +} diff --git a/src/main/java/Commands/command.java b/src/main/java/User/TaskList.java similarity index 53% rename from src/main/java/Commands/command.java rename to src/main/java/User/TaskList.java index d81e1a656..f9b6bf397 100644 --- a/src/main/java/Commands/command.java +++ b/src/main/java/User/TaskList.java @@ -1,16 +1,17 @@ -package Commands; +package User; import Tasks.Deadline; import Tasks.Event; import Tasks.Task; import java.util.ArrayList; -import static Commands.Text.line; +import static User.UI.line; -public class command { +public class TaskList { - public static void printList(ArrayList taskList) { + public static ArrayList taskList = new ArrayList(100); + + public static void printList(){ if (taskList.isEmpty()) { - error.listIsEmpty(); - error.invalidCommand(); + UI.listIsEmpty(); } else { System.out.println("Here are the tasks in your list:\n"); for (Task item : taskList) { @@ -21,10 +22,11 @@ public static void printList(ArrayList taskList) { } } - public static void deleteItem(String userInput, ArrayList taskList) { + public static void deleteItem(String userInput) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + //to check if an item is within the index of the list if (itemNumber >= taskList.size()) { - error.indexNotFound(); + UI.indexNotFound(); } else { System.out.println(line); System.out.println("Noted. I've removed this task:\n" + taskList.get(itemNumber).getSymbol() @@ -35,44 +37,62 @@ public static void deleteItem(String userInput, ArrayList taskList) { } } - public static void markItem(String userInput, ArrayList taskList) { + public static void markItem(String userInput) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + //to check if an item is within the index of the list + System.out.println(line); if (itemNumber >= taskList.size()){ - error.indexNotFound(); + UI.indexNotFound(); } else { - taskList.get(itemNumber).markAsDone(); - System.out.println("Nice! I've marked this task as done:\n" + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); - System.out.println(line); + if (taskList.get(itemNumber).isDone()){ + System.out.println("The task has already been completed!"); + System.out.println(line); + } else { + taskList.get(itemNumber).markAsDone(); + System.out.println("Nice! I've marked this task as done: \n" + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); + System.out.println(line); + } } } - public static void unMarkItem(String userInput, ArrayList taskList) { + public static void unMarkItem(String userInput) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; + //to check if an item is within the index of the list + System.out.println(line); if (itemNumber >= taskList.size()) { - error.indexNotFound(); + UI.indexNotFound(); } else { - taskList.get(itemNumber).markAsUnDone(); - System.out.println("OK, I've marked this task as not done yet:\n" + " " - + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); - System.out.println(line); + if (taskList.get(itemNumber).isDone()) { + taskList.get(itemNumber).markAsUnDone(); + System.out.println("OK, I've marked this task as not done yet: \n" + " " + + taskList.get(itemNumber).getStatusIcon() + taskList.get(itemNumber).getDescription()); + System.out.println(line); + } else { + System.out.println("The task has not been completed!"); + System.out.println(line); + } } } - public static void createEvent(String userInput, ArrayList taskList){ - String userInputParts[]; + public static void createEvent(String userInput){ + String[] userInputParts; userInput = userInput.replace("event",""); userInputParts = userInput.split("/"); + userInputParts[1] = userInputParts[1].replace("from ", ""); + userInputParts[2] = userInputParts[2].replace("to ", ""); Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); taskList.add(event); System.out.println("Got it. I've added this task: "); - System.out.println(event.getSymbol() + event.getStatusIcon() + event.getDescription()); + System.out.println(event.toString()); System.out.println("Now you have " + taskList.size() + " tasks in the list. "); System.out.println(line); } - public static void createDeadline(String userInput, ArrayList taskList){ - String userInputParts[]; + + public static void createDeadline(String userInput){ + String[] userInputParts; userInput = userInput.replace("deadline",""); userInputParts = userInput.split("/"); + userInputParts[1] = userInputParts[1].replace("by ", ""); Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); taskList.add(deadline); System.out.println(deadline.getSymbol() + deadline.getStatusIcon() + deadline.getDescription()); @@ -80,11 +100,12 @@ public static void createDeadline(String userInput, ArrayList taskList){ System.out.println(line); } - public static void createToDo(String userInput, ArrayList taskList){ + public static void createToDo(String userInput){ userInput = userInput.replace("todo ",""); String dummy = userInput.replace(" ",""); + //checks if description is empty and throws warning back to user if (dummy.isEmpty()){ - error.emptyDescription(); + UI.emptyDescription(); } else { Task task = new Task(userInput); taskList.add(task); diff --git a/src/main/java/Commands/Text.java b/src/main/java/User/UI.java similarity index 65% rename from src/main/java/Commands/Text.java rename to src/main/java/User/UI.java index 18b942795..755276581 100644 --- a/src/main/java/Commands/Text.java +++ b/src/main/java/User/UI.java @@ -1,15 +1,13 @@ -package Commands; +package User; -public class Text { +public class UI { static String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - static String line = "____________________________________________________________\n"; static String greeting = (line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); - static String goodBye = (line + "Bye. Hope to see you again soon!\n" + line); public static void printLogo(){ @@ -34,5 +32,26 @@ public static void printHelp(){ System.out.println("8. bye (to terminate the programme)"); System.out.println(line); } + public static void emptyDescription(){ + System.out.println("Please do not leave the description empty!"); + System.out.println(line); + } + + public static void invalidCommand(){ + System.out.println("Please enter a valid command!"); + System.out.println("Enter command 'help' to see the list of available commands!"); + System.out.println(line); + } + + public static void indexNotFound(){ + System.out.println("Please ensure the number you have entered is within the list!"); + System.out.println(line); + } + + public static void listIsEmpty(){ + System.out.println("Invalid command! The list is empty!"); + System.out.println(line); + } + } From 6fe26dec69e46803f749cdedd34f769d7c2d9abe Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Fri, 3 Mar 2023 16:33:10 +0800 Subject: [PATCH 14/16] Level-9-Find added --- duke.txt | 6 +++--- src/main/java/Tasks/Deadline.java | 4 ++-- src/main/java/Tasks/Event.java | 4 ++-- src/main/java/User/Parser.java | 7 +++++++ src/main/java/User/Storage.java | 2 +- src/main/java/User/TaskList.java | 28 +++++++++++++++++++++++++++- src/main/java/User/UI.java | 5 ++++- 7 files changed, 46 insertions(+), 10 deletions(-) diff --git a/duke.txt b/duke.txt index 3a9579e34..22c55b862 100644 --- a/duke.txt +++ b/duke.txt @@ -1,3 +1,3 @@ -[ ] : T : study -[X] : E : study (From:now To:later) -[ ] : D : study (By:later) \ No newline at end of file +[ ] : T : eat +[ ] : D : duke(By:2359) +[X] : E : study(From:now To:later) \ No newline at end of file diff --git a/src/main/java/Tasks/Deadline.java b/src/main/java/Tasks/Deadline.java index 397c781de..016bee52f 100644 --- a/src/main/java/Tasks/Deadline.java +++ b/src/main/java/Tasks/Deadline.java @@ -23,10 +23,10 @@ public Deadline(String description, String by) { @Override public String toString() { - return "[D]" + super.getStatusIcon() + super.getDescription() ; + return "[D]" + super.getStatusIcon() + " " + super.getDescription() ; } @Override public String toFile() { - return this.getStatusIcon() + " : " +"D"+ " :" + this.description; + return this.getStatusIcon() + " : " +"D"+ " : " + this.description; } } diff --git a/src/main/java/Tasks/Event.java b/src/main/java/Tasks/Event.java index 30ac1e3c3..f77489a25 100644 --- a/src/main/java/Tasks/Event.java +++ b/src/main/java/Tasks/Event.java @@ -20,10 +20,10 @@ public String getSymbol() { @Override public String toString(){ - return "[E]" + super.getStatusIcon() + super.getDescription(); + return "[E]" + super.getStatusIcon() + " " + super.getDescription(); } @Override public String toFile() { - return this.getStatusIcon() + " : " + "E" + " :" + this.description; + return this.getStatusIcon() + " : " + "E" + " : " + this.description; } } diff --git a/src/main/java/User/Parser.java b/src/main/java/User/Parser.java index 840e643d0..5dd69362e 100644 --- a/src/main/java/User/Parser.java +++ b/src/main/java/User/Parser.java @@ -30,6 +30,13 @@ public static void parsing(String userInput){ case("help"): UI.printHelp(); break; + case("find"): + if(!TaskList.taskList.isEmpty()) { + TaskList.find(userInput); + } else { + UI.listIsEmpty(); + } + break; default: UI.invalidCommand(); break; diff --git a/src/main/java/User/Storage.java b/src/main/java/User/Storage.java index 71b92fb29..fd53ffc3f 100644 --- a/src/main/java/User/Storage.java +++ b/src/main/java/User/Storage.java @@ -43,7 +43,7 @@ public static ArrayList readFile(String filePath) throws FileNotFoundExcep tempTasks.add(event); break; default: - UI.printHelp(); + UI.errorReadingFile(); break; } if (newInput[0].equals("[X]")) { diff --git a/src/main/java/User/TaskList.java b/src/main/java/User/TaskList.java index f9b6bf397..04032cb9a 100644 --- a/src/main/java/User/TaskList.java +++ b/src/main/java/User/TaskList.java @@ -78,6 +78,7 @@ public static void createEvent(String userInput){ String[] userInputParts; userInput = userInput.replace("event",""); userInputParts = userInput.split("/"); + userInputParts[0] = userInputParts[0].replace(" ",""); userInputParts[1] = userInputParts[1].replace("from ", ""); userInputParts[2] = userInputParts[2].replace("to ", ""); Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); @@ -92,10 +93,11 @@ public static void createDeadline(String userInput){ String[] userInputParts; userInput = userInput.replace("deadline",""); userInputParts = userInput.split("/"); + userInputParts[0] = userInputParts[0].replace(" ",""); userInputParts[1] = userInputParts[1].replace("by ", ""); Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); taskList.add(deadline); - System.out.println(deadline.getSymbol() + deadline.getStatusIcon() + deadline.getDescription()); + System.out.println(deadline.toString()); System.out.println("Now you have " + taskList.size() + " tasks in the list. "); System.out.println(line); } @@ -115,4 +117,28 @@ public static void createToDo(String userInput){ System.out.println(line); } } + + public static void find(String userInput) { + ArrayList findList = new ArrayList(100); + userInput = userInput.replace("find ", ""); + for (Task item : taskList) { + if (item.getDescription().contains(userInput)) { + findList.add(item); + } + } + if (!findList.isEmpty()) { + System.out.println(line); + System.out.println("Here are the matching tasks in your list:"); + for (Task item : findList) { + System.out.print((findList.indexOf(item) + 1) + "."); + System.out.println(item.toString()); + } + System.out.println(line); + } else { + System.out.println("Use another keyword!"); + System.out.println(line); + } + } + + } diff --git a/src/main/java/User/UI.java b/src/main/java/User/UI.java index 755276581..4e09be734 100644 --- a/src/main/java/User/UI.java +++ b/src/main/java/User/UI.java @@ -53,5 +53,8 @@ public static void listIsEmpty(){ System.out.println(line); } - + public static void errorReadingFile(){ + System.out.println("Error reading file!"); + System.out.println(line); + } } From 107d97fd5d09143a020e331023e73315f853d999 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Fri, 3 Mar 2023 17:37:12 +0800 Subject: [PATCH 15/16] Added Java Documentation --- README.md | 62 ++++++++++++++++++----------- duke.txt | 4 +- src/main/java/Duke.java | 6 +-- src/main/java/Tasks/Deadline.java | 14 +++++++ src/main/java/Tasks/Event.java | 10 ++++- src/main/java/Tasks/Task.java | 26 ++++++++++++ src/main/java/User/Parser.java | 4 ++ src/main/java/User/Storage.java | 20 +++++++++- src/main/java/User/TaskList.java | 66 ++++++++++++++++++++++++------- src/main/java/User/UI.java | 6 +++ 10 files changed, 171 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 8715d4d91..676cb9553 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,38 @@ -# 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 +Welcome to Duke! This is a user guide for the task tracker Duke. + +## Installation +Requires: JDK 11 +1. Download the jar file for Duke under "Releases" +2. Extract it to an empty folder +3. Run the jar file +4. Enjoy! + +### Commands +Below are the list of commands that can be used with Duke +Please remember to follow the input format strictly! + +1. list || (to see the current tasks saved) +2. `todo ` || Adds a task with no particular time to take note of +3. `event /from /to ` || Adds an event with a start and end time +4. `deadline /by ` || Adds a task with a deadline +5. `mark ` || To mark a task as done +6. `unmark ` || To mark a task as not done +7. `delete ` || To remove a task from the list +8. `bye` || To terminate the programme + +## Usage + +Example of usage: + +`event study /from 2pm /tp 4pm` + +Expected outcome: +``` +Got it. I've added this task: +[E][ ] study (From: 2pm to To: 4pm) +Now you have 1 tasks in the list. +____________________________________________________________ + +What would you like to do? +``` diff --git a/duke.txt b/duke.txt index 22c55b862..d8a02fbc3 100644 --- a/duke.txt +++ b/duke.txt @@ -1,3 +1 @@ -[ ] : T : eat -[ ] : D : duke(By:2359) -[X] : E : study(From:now To:later) \ No newline at end of file +[X] : T : eat \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 7f00b8269..7316f9df1 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,11 +1,9 @@ -import Tasks.Task; import User.Parser; import User.Storage; import User.TaskList; import User.UI; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; import java.util.Scanner; public class Duke { @@ -20,10 +18,10 @@ public static void main(String[] args) throws IOException { Scanner myObj = new Scanner(System.in); String userInput; userInput = myObj.nextLine(); - while (!userInput.equals("bye")) { //if user inputs "bye" the loop will break + while (!userInput.equals("bye")) { Parser.parsing(userInput); System.out.println("What would you like to do?"); - userInput = myObj.nextLine(); //to take in next input + userInput = myObj.nextLine(); } Storage.clearFile(); Storage.updateFile(TaskList.taskList); diff --git a/src/main/java/Tasks/Deadline.java b/src/main/java/Tasks/Deadline.java index 016bee52f..c72448e02 100644 --- a/src/main/java/Tasks/Deadline.java +++ b/src/main/java/Tasks/Deadline.java @@ -15,16 +15,30 @@ public String getSymbol() { return symbol; } + /** + * Constructs a deadline-type task + * @param description the activity that needs be done by the user + * @param by the deadline by which the user needs to finish said activity + */ public Deadline(String description, String by) { super(description); this.by = by; this.description = description + "(By:" + by + ")"; } + /** + * Returns a string representation of the deadline-type task to be printed for the user + * @return the formatted string output + */ @Override public String toString() { return "[D]" + super.getStatusIcon() + " " + super.getDescription() ; } + + /** + * Returns a string representation of the deadline-type task to be stored in the text file + * @return the formatted string output + */ @Override public String toFile() { return this.getStatusIcon() + " : " +"D"+ " : " + this.description; diff --git a/src/main/java/Tasks/Event.java b/src/main/java/Tasks/Event.java index f77489a25..f1b471591 100644 --- a/src/main/java/Tasks/Event.java +++ b/src/main/java/Tasks/Event.java @@ -17,11 +17,19 @@ public String getSymbol() { return symbol; } - + /** + * Returns the string in a format to be presented to the user + * @return the formatted string output + */ @Override public String toString(){ return "[E]" + super.getStatusIcon() + " " + super.getDescription(); } + + /** + * Returns the string in a format to be stored in the text file + * @return the formatted string to be stored in the text file + */ @Override public String toFile() { return this.getStatusIcon() + " : " + "E" + " : " + this.description; diff --git a/src/main/java/Tasks/Task.java b/src/main/java/Tasks/Task.java index 2120212d1..2c29a0b68 100644 --- a/src/main/java/Tasks/Task.java +++ b/src/main/java/Tasks/Task.java @@ -14,31 +14,57 @@ public Task(String description) { this.isDone = false; } + /** + * Returns the symbol of the task + * @return + */ public String getSymbol() { return symbol; } + /** + * marks the task as done + */ public void markAsDone() { this.isDone = true; } + /** + * Returns the boolean value of whether a task has been marked as done or not + * @return true if the task has been marked as done, false otherwise + */ public boolean isDone() { return isDone; } + /** + * marks the task as not done + */ public void markAsUnDone() { this.isDone = false; } + /** + * Checks if the task has been marked as done and returns the appropriate symbol + * @return returns "[X]" if done, and "[ ]" if not done + */ public String getStatusIcon() { return (isDone ? "[X]" : "[ ]"); // mark done task with X } + /** + * Returns a string representation of the task to be presented to the user + * @return the formatted string output + */ @Override public String toString(){ return "[T]" + this.getStatusIcon() + " " + getDescription(); } + /** + * Returns a string representation of the task to be stored in the text file + * @return the formatted string output + */ public String toFile() { return this.getStatusIcon() + " : " +"T"+ " : " + this.description; } diff --git a/src/main/java/User/Parser.java b/src/main/java/User/Parser.java index 5dd69362e..4c2abf914 100644 --- a/src/main/java/User/Parser.java +++ b/src/main/java/User/Parser.java @@ -1,6 +1,10 @@ package User; public class Parser { + /** + * Takes in the users command and carries out the corresponding action + * @param userInput the command that the user inputs + */ public static void parsing(String userInput){ String[] inputParts; inputParts = userInput.split(" ",2); diff --git a/src/main/java/User/Storage.java b/src/main/java/User/Storage.java index fd53ffc3f..8e3ab0473 100644 --- a/src/main/java/User/Storage.java +++ b/src/main/java/User/Storage.java @@ -10,7 +10,12 @@ import java.util.Scanner; public class Storage { - + /** + * Reads through the text file and initiates all the saved tasks + * @param filePath the string that represents the filepath where the text is stored + * @return The ArrayList with all the stored tasks + * @throws FileNotFoundException throws an error if the file is not found + */ public static ArrayList readFile(String filePath) throws FileNotFoundException { ArrayList tempTasks = new ArrayList(100); String[] userInputParts; @@ -53,18 +58,31 @@ public static ArrayList readFile(String filePath) throws FileNotFoundExcep return tempTasks; } + /** + * Writes a string into the text file to be stored + * @param toFile the formatted string from the tasks that are to be written into the text file + * @throws IOException if an I/O error occurs while writing to this file + */ public static void writeToFile(String toFile) throws IOException { FileWriter fw = new FileWriter("duke.txt",true); fw.write(toFile); fw.close(); } + /** + * Wipes the file clean + * @throws IOException if an I/O error occurs while writing to this file + */ public static void clearFile() throws IOException { FileWriter fw = new FileWriter("duke.txt"); fw.write(""); fw.close(); } + /** + * Updates the text file with the current list of tasks during the latest running of Duke + * @param taskList the ArrayList of tasks to be updated to the text file + */ public static void updateFile(ArrayList taskList) { for (int i = 0; i < taskList.size(); i++) { try { diff --git a/src/main/java/User/TaskList.java b/src/main/java/User/TaskList.java index 04032cb9a..475c0700b 100644 --- a/src/main/java/User/TaskList.java +++ b/src/main/java/User/TaskList.java @@ -9,6 +9,10 @@ public class TaskList { public static ArrayList taskList = new ArrayList(100); + /** + *Iterates through taskList and prints the task number, description, status and other + * details such as start and end time for the relevant task types + */ public static void printList(){ if (taskList.isEmpty()) { UI.listIsEmpty(); @@ -22,6 +26,10 @@ public static void printList(){ } } + /** + * Deletes a task from the array based on its index + * @param userInput The index of the item that is to be deleted + */ public static void deleteItem(String userInput) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; //to check if an item is within the index of the list @@ -37,6 +45,10 @@ public static void deleteItem(String userInput) { } } + /** + * Accesses the index of the item in the array and updates its status to be done + * @param userInput The index of the item to be marked as done + */ public static void markItem(String userInput) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; //to check if an item is within the index of the list @@ -54,7 +66,10 @@ public static void markItem(String userInput) { } } } - + /** + * Accesses the index of the item in the array and updates its status to be not done + * @param userInput The index of the item to be marked as not done + */ public static void unMarkItem(String userInput) { int itemNumber = Integer.parseInt(userInput.replaceAll("[^0-9]", "")) - 1; //to check if an item is within the index of the list @@ -74,11 +89,17 @@ public static void unMarkItem(String userInput) { } } + /** + * Creates an Event-type task and adds it to the array list + * The user input is parsed into the function and split into three parts: the description, + * the start time and the end time. + * A message at the end will inform the user what is the updated number of tasks that they have + * @param userInput The user input to be parsed and split into its parts. + */ public static void createEvent(String userInput){ String[] userInputParts; - userInput = userInput.replace("event",""); + userInput = userInput.replace("event ",""); userInputParts = userInput.split("/"); - userInputParts[0] = userInputParts[0].replace(" ",""); userInputParts[1] = userInputParts[1].replace("from ", ""); userInputParts[2] = userInputParts[2].replace("to ", ""); Event event = new Event(userInputParts[0], userInputParts[1], userInputParts[2]); @@ -88,12 +109,17 @@ public static void createEvent(String userInput){ System.out.println("Now you have " + taskList.size() + " tasks in the list. "); System.out.println(line); } - + /** + * Creates a Deadline-type task and adds it to the array list + * The user input is parsed into the function and split into two parts: the description + * and the deadline time. + * A message at the end will inform the user what is the updated number of tasks that they have + * @param userInput The user input to be parsed and split into its parts. + */ public static void createDeadline(String userInput){ String[] userInputParts; - userInput = userInput.replace("deadline",""); + userInput = userInput.replace("deadline ",""); userInputParts = userInput.split("/"); - userInputParts[0] = userInputParts[0].replace(" ",""); userInputParts[1] = userInputParts[1].replace("by ", ""); Deadline deadline = new Deadline(userInputParts[0], userInputParts[1]); taskList.add(deadline); @@ -101,7 +127,12 @@ public static void createDeadline(String userInput){ System.out.println("Now you have " + taskList.size() + " tasks in the list. "); System.out.println(line); } - + /** + * Creates an todo-type task and adds it to the array list + * The user input is parsed in the function + * A message at the end will inform the user what is the updated number of tasks that they have + * @param userInput The user input to be parsed + */ public static void createToDo(String userInput){ userInput = userInput.replace("todo ",""); String dummy = userInput.replace(" ",""); @@ -117,21 +148,28 @@ public static void createToDo(String userInput){ System.out.println(line); } } - + /** + * Prints out all items in the task list that contain the keyword and their respective indexes + * If no items are found with the given keyword, a prompt will be given to the user + * to use a different keyword + * @param userInput The keyword the user will use to look for the item + */ public static void find(String userInput) { ArrayList findList = new ArrayList(100); + ArrayList indexOfItem = new ArrayList(100); userInput = userInput.replace("find ", ""); - for (Task item : taskList) { - if (item.getDescription().contains(userInput)) { - findList.add(item); + for (int i = 0; i < taskList.size(); i++) { + if (taskList.get(i).getDescription().contains(userInput)) { + findList.add(taskList.get(i)); + indexOfItem.add(i+1); } } if (!findList.isEmpty()) { System.out.println(line); System.out.println("Here are the matching tasks in your list:"); - for (Task item : findList) { - System.out.print((findList.indexOf(item) + 1) + "."); - System.out.println(item.toString()); + for (int j = 0; j < indexOfItem.size(); j++) { + System.out.print((indexOfItem.get(j)) + "."); + System.out.println(findList.get(j).toString()); } System.out.println(line); } else { diff --git a/src/main/java/User/UI.java b/src/main/java/User/UI.java index 4e09be734..be29a9476 100644 --- a/src/main/java/User/UI.java +++ b/src/main/java/User/UI.java @@ -1,6 +1,12 @@ package User; public class UI { + + /** + *These are the list of prompts/greetings/error messages that will be presented to the user at the + * appropriate times + */ + static String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" From 0aa293f66835d6f08477e236826e44619c3cd46e Mon Sep 17 00:00:00 2001 From: "DESKTOP-FM5QJ06\\User" Date: Fri, 3 Mar 2023 18:22:15 +0800 Subject: [PATCH 16/16] Created JAR file --- duke.txt | 1 - src/main/java/User/TaskList.java | 1 - 2 files changed, 2 deletions(-) diff --git a/duke.txt b/duke.txt index d8a02fbc3..e69de29bb 100644 --- a/duke.txt +++ b/duke.txt @@ -1 +0,0 @@ -[X] : T : eat \ No newline at end of file diff --git a/src/main/java/User/TaskList.java b/src/main/java/User/TaskList.java index 475c0700b..a96f04b70 100644 --- a/src/main/java/User/TaskList.java +++ b/src/main/java/User/TaskList.java @@ -178,5 +178,4 @@ public static void find(String userInput) { } } - }