From 5e12eadcd03ee61fe27c5a943a3793a540f97819 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Wed, 2 Feb 2022 20:14:08 +0800 Subject: [PATCH 01/23] Added Level 0 Greet --- 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..8d55d68c9 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,19 @@ public class Duke { + + private static String HORIZONTAL_LINE = "____________________________________________________________"; + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + System.out.println("Hello from\n" + logo + "\n" + + HORIZONTAL_LINE + "\n" + + "Hello! I'm Duke\n" + + "What can I do for you?\n" + + HORIZONTAL_LINE + "\n" + + "Bye. Hope to see you again soon!\n" + + HORIZONTAL_LINE); } } From b80b8281cea23297df587eadaee1d7444617f22b Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Thu, 3 Feb 2022 21:26:58 +0800 Subject: [PATCH 02/23] Added Level 1. Greet, Echo, Exit --- src/main/java/Duke.java | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 8d55d68c9..4f9a29e48 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,5 +1,8 @@ +import java.util.Scanner; + public class Duke { + public static Scanner sc = new Scanner(System.in); private static String HORIZONTAL_LINE = "____________________________________________________________"; public static void main(String[] args) { @@ -9,11 +12,24 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo + "\n" - + HORIZONTAL_LINE + "\n" - + "Hello! I'm Duke\n" - + "What can I do for you?\n" - + HORIZONTAL_LINE + "\n" - + "Bye. Hope to see you again soon!\n" - + HORIZONTAL_LINE); + + HORIZONTAL_LINE + "\n" + + "Hello! I'm Duke\n" + + "What can I do for you?\n" + + HORIZONTAL_LINE); + + while (sc.hasNext()) { + String input = sc.next(); + if (input.equals("bye")) { + break; + } else { + System.out.println(HORIZONTAL_LINE + "\n" + + input + "\n" + + HORIZONTAL_LINE); + } + } + + System.out.println(HORIZONTAL_LINE + "\n" + + "Bye. Hope to see you again soon!\n" + + HORIZONTAL_LINE); } -} +} \ No newline at end of file From 5d36d39b26782a8117be237077d677436c4b23fe Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Thu, 3 Feb 2022 21:39:57 +0800 Subject: [PATCH 03/23] Added Level 2. Add, List --- src/main/java/Duke.java | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4f9a29e48..4ad25ab31 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,7 +2,9 @@ public class Duke { - public static Scanner sc = new Scanner(System.in); + private static Scanner sc = new Scanner(System.in); + private static String[] list = new String[100]; + private static int stringCounter = 0; private static String HORIZONTAL_LINE = "____________________________________________________________"; public static void main(String[] args) { @@ -18,12 +20,24 @@ public static void main(String[] args) { + HORIZONTAL_LINE); while (sc.hasNext()) { - String input = sc.next(); + String input = sc.nextLine(); if (input.equals("bye")) { break; + } else if (input.equals("list")) { + System.out.println(HORIZONTAL_LINE); + for (int i = 0; i < list.length; i++) { + if (list[i] != null) { + System.out.println(String.valueOf(i + 1) + ". " + list[i]); + } else { + System.out.println(HORIZONTAL_LINE); + break; + } + } } else { + list[stringCounter] = input; + stringCounter++; System.out.println(HORIZONTAL_LINE + "\n" - + input + "\n" + + "added: " + input + "\n" + HORIZONTAL_LINE); } } From 6008445ec9287d7fee80790fd214a19117598034 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Thu, 3 Feb 2022 22:23:46 +0800 Subject: [PATCH 04/23] Added Level 3. Mark as Done --- src/main/java/Duke.java | 26 +++++++++++++++++++++----- src/main/java/Task.java | 30 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4ad25ab31..efd75750a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,8 +3,8 @@ public class Duke { private static Scanner sc = new Scanner(System.in); - private static String[] list = new String[100]; - private static int stringCounter = 0; + private static Task[] list = new Task[100]; + private static int taskCounter = 0; private static String HORIZONTAL_LINE = "____________________________________________________________"; public static void main(String[] args) { @@ -25,17 +25,33 @@ public static void main(String[] args) { break; } else if (input.equals("list")) { System.out.println(HORIZONTAL_LINE); + System.out.println("Here are the tasks in your list:"); for (int i = 0; i < list.length; i++) { if (list[i] != null) { - System.out.println(String.valueOf(i + 1) + ". " + list[i]); + System.out.println(String.valueOf(i + 1) + "." + list[i]); } else { System.out.println(HORIZONTAL_LINE); break; } } + } else if (input.contains("unmark ")) { + int markInt = Integer.parseInt(input.substring(7)) - 1; + list[markInt].setDone(false); + System.out.println(HORIZONTAL_LINE + "\n" + + "OK, I've marked this task as not done yet:\n" + + list[markInt] + "\n" + + HORIZONTAL_LINE); + } else if (input.contains("mark ")) { + int markInt = Integer.parseInt(input.substring(5)) - 1; + list[markInt].setDone(true); + System.out.println(HORIZONTAL_LINE + "\n" + + "Nice! I've marked this task as done:\n" + + list[markInt] + "\n" + + HORIZONTAL_LINE); } else { - list[stringCounter] = input; - stringCounter++; + Task inputTask = new Task(input); + list[taskCounter] = inputTask; + taskCounter++; System.out.println(HORIZONTAL_LINE + "\n" + "added: " + input + "\n" + HORIZONTAL_LINE); diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..fa94604bc --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,30 @@ +class Task { + + public String task; + public Boolean isDone = false; + + Task(String task) { + this.task = task; + } + + public void setDone(Boolean isDone) { + this.isDone = isDone; + } + + public String getTask() { + return this.task; + } + + public boolean isDone() { + return this.isDone; + } + + @Override + public String toString() { + String mark = " "; + if (this.isDone()) { + mark = "X"; + } + return String.format("[%s] %s", mark, task); + } +} \ No newline at end of file From 5fc1f21a07647f4b0cdbde8d52890bf1e8a58135 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Feb 2022 05:55:41 +0800 Subject: [PATCH 05/23] Added Level 4. ToDos, Events, Deadlines --- src/main/java/Deadlines.java | 18 ++++++++++++++++++ src/main/java/Duke.java | 32 ++++++++++++++++++++++++++++++-- src/main/java/Event.java | 18 ++++++++++++++++++ src/main/java/Task.java | 26 ++++++++++++++++---------- src/main/java/ToDos.java | 11 +++++++++++ 5 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 src/main/java/Deadlines.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/ToDos.java diff --git a/src/main/java/Deadlines.java b/src/main/java/Deadlines.java new file mode 100644 index 000000000..f1b9ca568 --- /dev/null +++ b/src/main/java/Deadlines.java @@ -0,0 +1,18 @@ +class Deadline extends Task { + + protected String deadline; + + Deadline(String description, String deadline) { + super(description); + this.deadline = deadline; + } + + String getDeadline() { + return this.deadline; + } + + @Override + public String toString() { + return String.format("[D]%s (by: %s)", super.toString(), this.deadline); + } +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index efd75750a..c5ee9f18e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -21,8 +21,10 @@ public static void main(String[] args) { while (sc.hasNext()) { String input = sc.nextLine(); + if (input.equals("bye")) { break; + } else if (input.equals("list")) { System.out.println(HORIZONTAL_LINE); System.out.println("Here are the tasks in your list:"); @@ -34,6 +36,7 @@ public static void main(String[] args) { break; } } + } else if (input.contains("unmark ")) { int markInt = Integer.parseInt(input.substring(7)) - 1; list[markInt].setDone(false); @@ -41,6 +44,7 @@ public static void main(String[] args) { + "OK, I've marked this task as not done yet:\n" + list[markInt] + "\n" + HORIZONTAL_LINE); + } else if (input.contains("mark ")) { int markInt = Integer.parseInt(input.substring(5)) - 1; list[markInt].setDone(true); @@ -48,14 +52,38 @@ public static void main(String[] args) { + "Nice! I've marked this task as done:\n" + list[markInt] + "\n" + HORIZONTAL_LINE); + } else { - Task inputTask = new Task(input); + Task inputTask; + if (input.contains("todo")) { + String inputString = input.substring(5); + inputTask = new ToDo(inputString); + } else if (input.contains("deadline")) { + int slashInt = input.indexOf("/"); + String inputString = input.substring(9, slashInt); + String inputDate = input.substring(slashInt + 4); + inputTask = new Deadline(inputString, inputDate); + } else { + int slashInt = input.indexOf("/"); + String inputString = input.substring(6, slashInt); + String inputDate = input.substring(slashInt + 4); + inputTask = new Event(inputString, inputDate); + } list[taskCounter] = inputTask; taskCounter++; System.out.println(HORIZONTAL_LINE + "\n" - + "added: " + input + "\n" + + "Got it. I've added this task:\n" + + " " + inputTask.toString() + "\n" + + "Now you have " + String.valueOf(taskCounter) + " tasks in the list.\n" + HORIZONTAL_LINE); } + /* + else { + + System.out.println(HORIZONTAL_LINE + "\n" + + "added: " + input + "\n" + + HORIZONTAL_LINE); + } */ } System.out.println(HORIZONTAL_LINE + "\n" diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..85c24dea6 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,18 @@ +class Event extends Task { + + protected String time; + + Event(String description, String time) { + super(description); + this.time = time; + } + + protected String getTime() { + return this.time; + } + + @Override + public String toString() { + return String.format("[E]%s (at: %s)", super.toString(), this.time); + } +} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index fa94604bc..8df72c83e 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,30 +1,36 @@ class Task { - public String task; + public String description; public Boolean isDone = false; + public String mark = " "; - Task(String task) { - this.task = task; + Task(String description) { + this.description = description; } public void setDone(Boolean isDone) { this.isDone = isDone; } - public String getTask() { - return this.task; + public String getDescription() { + return this.description; } public boolean isDone() { return this.isDone; } - @Override - public String toString() { - String mark = " "; + public String markIsDone() { if (this.isDone()) { - mark = "X"; + this.mark = "X"; + } else { + this.mark = " "; } - return String.format("[%s] %s", mark, task); + return this.mark; + } + + @Override + public String toString() { + return String.format("[%s] %s", this.markIsDone(), this.description); } } \ No newline at end of file diff --git a/src/main/java/ToDos.java b/src/main/java/ToDos.java new file mode 100644 index 000000000..191474596 --- /dev/null +++ b/src/main/java/ToDos.java @@ -0,0 +1,11 @@ +class ToDo extends Task { + + ToDo(String description) { + super(description); + } + + @Override + public String toString() { + return String.format("[T]%s", super.toString()); + } +} \ No newline at end of file From cbe61419a248330457a34d3e4e45c9cb36dd3345 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 11 Feb 2022 06:25:36 +0800 Subject: [PATCH 06/23] Added Level 5. Handle Errors --- src/main/java/AlreadyMarkedException.java | 3 + src/main/java/AlreadyUnmarkedException.java | 3 + src/main/java/Duke.java | 241 +++++++++++++----- src/main/java/NoSlashException.java | 3 + src/main/java/SingleWordCommandException.java | 3 + text-ui-test/runtest.bat | 2 + 6 files changed, 187 insertions(+), 68 deletions(-) create mode 100644 src/main/java/AlreadyMarkedException.java create mode 100644 src/main/java/AlreadyUnmarkedException.java create mode 100644 src/main/java/NoSlashException.java create mode 100644 src/main/java/SingleWordCommandException.java diff --git a/src/main/java/AlreadyMarkedException.java b/src/main/java/AlreadyMarkedException.java new file mode 100644 index 000000000..e36339f81 --- /dev/null +++ b/src/main/java/AlreadyMarkedException.java @@ -0,0 +1,3 @@ +public class AlreadyMarkedException extends Exception { + +} \ No newline at end of file diff --git a/src/main/java/AlreadyUnmarkedException.java b/src/main/java/AlreadyUnmarkedException.java new file mode 100644 index 000000000..3cd909625 --- /dev/null +++ b/src/main/java/AlreadyUnmarkedException.java @@ -0,0 +1,3 @@ +public class AlreadyUnmarkedException extends Exception { + +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index c5ee9f18e..930e13f8b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,5 @@ +import java.lang.reflect.Array; +import java.security.SignatureException; import java.util.Scanner; public class Duke { @@ -7,87 +9,190 @@ public class Duke { private static int taskCounter = 0; private static String HORIZONTAL_LINE = "____________________________________________________________"; - public static void main(String[] args) { + private static void printFormat(String... args) { + System.out.println(HORIZONTAL_LINE); + for (String arg : args) { + System.out.println(arg); + } + System.out.println(HORIZONTAL_LINE); + } + + private static void intro() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo + "\n" - + HORIZONTAL_LINE + "\n" - + "Hello! I'm Duke\n" - + "What can I do for you?\n" - + HORIZONTAL_LINE); + System.out.println("Hello from\n" + logo); + printFormat("Hello! I'm Duke", + "What can I do for you?", + "type 'commands' for the list of commands"); + } - while (sc.hasNext()) { - String input = sc.nextLine(); + private static void handleInput(String input) + throws SingleWordCommandException, IndexOutOfBoundsException, NoSlashException, AlreadyMarkedException, + AlreadyUnmarkedException { + if (input.contains("list")) { + if (input.equals("list")) { + handleList(); + } else { + throw new SingleWordCommandException(); + } - if (input.equals("bye")) { - break; - - } else if (input.equals("list")) { - System.out.println(HORIZONTAL_LINE); - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < list.length; i++) { - if (list[i] != null) { - System.out.println(String.valueOf(i + 1) + "." + list[i]); - } else { - System.out.println(HORIZONTAL_LINE); - break; - } - } + } else if (input.contains("unmark")) { + handleUnmark(input); + + } else if (input.contains("mark")) { + handleMark(input); - } else if (input.contains("unmark ")) { - int markInt = Integer.parseInt(input.substring(7)) - 1; - list[markInt].setDone(false); - System.out.println(HORIZONTAL_LINE + "\n" - + "OK, I've marked this task as not done yet:\n" - + list[markInt] + "\n" - + HORIZONTAL_LINE); - - } else if (input.contains("mark ")) { - int markInt = Integer.parseInt(input.substring(5)) - 1; - list[markInt].setDone(true); - System.out.println(HORIZONTAL_LINE + "\n" - + "Nice! I've marked this task as done:\n" - + list[markInt] + "\n" - + HORIZONTAL_LINE); + } else if (input.contains("todo")) { + handleToDo(input); + } else if (input.contains("deadline")) { + if (input.contains("/")) { + handleDeadline(input); } else { - Task inputTask; - if (input.contains("todo")) { - String inputString = input.substring(5); - inputTask = new ToDo(inputString); - } else if (input.contains("deadline")) { - int slashInt = input.indexOf("/"); - String inputString = input.substring(9, slashInt); - String inputDate = input.substring(slashInt + 4); - inputTask = new Deadline(inputString, inputDate); - } else { - int slashInt = input.indexOf("/"); - String inputString = input.substring(6, slashInt); - String inputDate = input.substring(slashInt + 4); - inputTask = new Event(inputString, inputDate); - } - list[taskCounter] = inputTask; - taskCounter++; - System.out.println(HORIZONTAL_LINE + "\n" - + "Got it. I've added this task:\n" - + " " + inputTask.toString() + "\n" - + "Now you have " + String.valueOf(taskCounter) + " tasks in the list.\n" - + HORIZONTAL_LINE); + throw new NoSlashException(); + } + + } else if (input.contains("event")) { + if (input.contains("/")) { + handleEvent(input); + } else { + throw new NoSlashException(); + } + + } else if (input.contains("commands")) { + if (input.equals("commands")) { + handleCommand(); + } else { + throw new SingleWordCommandException(); + } + + } else { + printFormat("OOPS!!! I'm sorry, but I don't know what that means :-(", + "Please type in 'commands' if you are not sure of the commands"); + + } + } + + private static void handleList() { + System.out.println(HORIZONTAL_LINE); + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < list.length; i++) { + if (list[i] != null) { + System.out.println(String.valueOf(i + 1) + "." + list[i]); + } else { + System.out.println(HORIZONTAL_LINE); + break; } - /* - else { + } + } + + private static void handleUnmark(String input) throws AlreadyUnmarkedException { + int markInt = Integer.parseInt(input.substring(7)) - 1; + if (list[markInt].isDone()) { + list[markInt].setDone(false); + printFormat("OK, I've marked this task as not done yet:", + list[markInt].toString()); + } else { + throw new AlreadyUnmarkedException(); + } + } - System.out.println(HORIZONTAL_LINE + "\n" - + "added: " + input + "\n" - + HORIZONTAL_LINE); - } */ + private static void handleMark(String input) throws AlreadyMarkedException { + int markInt = Integer.parseInt(input.substring(5)) - 1; + if (list[markInt].isDone()) { + throw new AlreadyMarkedException(); + } else { + list[markInt].setDone(true); + printFormat("Nice! I've marked this task as done:", + list[markInt].toString()); } + } + + private static void handleToDo(String input) { + String[] subStrings = input.split(" ", 2); + Task newTask = new ToDo(subStrings[1]); + list[taskCounter] = newTask; + taskCounter++; + printFormat("Got it. I've added this task:", + " " + newTask.toString(), + "Now you have " + String.valueOf(taskCounter) + " tasks in the list."); + } + + private static void handleDeadline(String input) { + String[] subStrings = input.split(" ", 2); + subStrings = subStrings[1].split("/", 2); + String inputString = subStrings[0]; + subStrings = subStrings[1].split(" ", 2); + String inputPreposition = subStrings[0]; + String inputDate = subStrings[1]; + Task newTask = new Deadline(inputString, inputDate); + list[taskCounter] = newTask; + taskCounter++; + printFormat("Got it. I've added this task:", + " " + newTask.toString(), + "Now you have " + String.valueOf(taskCounter) + " tasks in the list."); + } + + private static void handleEvent(String input) { + String[] subStrings = input.split(" ", 2); + subStrings = subStrings[1].split("/", 2); + String inputString = subStrings[0]; + subStrings = subStrings[1].split(" ", 2); + String inputPreposition = subStrings[0]; + String inputDate = subStrings[1]; + Task newTask = new Event(inputString, inputDate); + list[taskCounter] = newTask; + taskCounter++; + printFormat("Got it. I've added this task:", + " " + newTask.toString(), + "Now you have " + String.valueOf(taskCounter) + " tasks in the list."); + } - System.out.println(HORIZONTAL_LINE + "\n" - + "Bye. Hope to see you again soon!\n" - + HORIZONTAL_LINE); + private static void handleCommand() { + printFormat("List of valid commands:", + "'list' - lists out all tasks and its details", + "'mark (X)' - marks item X on the list as done", + " (X is a number) e.g. 'mark 3' marks item 3 on the list", + "'unmark (X)' - unmarks item X on the list as done", + " (X is a number) e.g. 'unmark 3' unmarks item 3 on the list", + "'todo (task)' - ToDos are tasks without specific deadlines", + " (task is a string) e.g. 'todo buy shampoo' adds the task 'buy shampoo' to the list", + "'deadline (task) /by (date/time)' - Deadlines are tasks that need to be done before a specific date/time", + " e.g. 'deadline math homework /by tues 2pm' adds a task with a deadline to the list", + "'event (task) /at (date/time)' - Events are tasks that start at a specific time and ends at a specific time", + " e.g. 'event project meeting /at sunday 8-10pm' adds a task with a time range"); + } + + private static void bye() { + printFormat("Bye. Hope to see you again soon!"); + } + + public static void main(String[] args) { + intro(); + while (sc.hasNext()) { + String input = sc.nextLine(); + if (input.equals("bye")) { + break; + } else { + try { + handleInput(input); + } catch (SingleWordCommandException e) { + printFormat("Oh no! There should not be any words after '" + input.split(" ")[1] + "'"); + + } catch (IndexOutOfBoundsException e) { + printFormat("Oh no! You have to include the details of the task '" + input + "'"); + } catch (NoSlashException e) { + printFormat("Oh no! You need to include '/' in your task '" + input + "'"); + } catch (AlreadyMarkedException e) { + printFormat("Oh no! The item is already marked!"); + } catch (AlreadyUnmarkedException e) { + printFormat("Oh no! The item is already unmarked!"); + } + } + } + bye(); } } \ No newline at end of file diff --git a/src/main/java/NoSlashException.java b/src/main/java/NoSlashException.java new file mode 100644 index 000000000..13e5a5946 --- /dev/null +++ b/src/main/java/NoSlashException.java @@ -0,0 +1,3 @@ +public class NoSlashException extends Exception { + +} \ No newline at end of file diff --git a/src/main/java/SingleWordCommandException.java b/src/main/java/SingleWordCommandException.java new file mode 100644 index 000000000..d33918615 --- /dev/null +++ b/src/main/java/SingleWordCommandException.java @@ -0,0 +1,3 @@ +public class SingleWordCommandException extends Exception { + +} \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..2c0921c3c 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -19,3 +19,5 @@ java -classpath ..\bin Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT + +Pause From 31969f48d0ac576459a0be49c0827ad026189745 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Mon, 14 Feb 2022 02:15:22 +0800 Subject: [PATCH 07/23] Added more exception cases --- src/main/java/Duke.java | 19 ++++--- src/main/java/InvalidNumberException.java | 3 ++ text-ui-test/EXPECTED.TXT | 65 +++++++++++++++++++++++ text-ui-test/input.txt | 14 +++++ 4 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 src/main/java/InvalidNumberException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 930e13f8b..b11d5d443 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,5 +1,3 @@ -import java.lang.reflect.Array; -import java.security.SignatureException; import java.util.Scanner; public class Duke { @@ -31,7 +29,7 @@ private static void intro() { private static void handleInput(String input) throws SingleWordCommandException, IndexOutOfBoundsException, NoSlashException, AlreadyMarkedException, - AlreadyUnmarkedException { + AlreadyUnmarkedException, InvalidNumberException { if (input.contains("list")) { if (input.equals("list")) { handleList(); @@ -89,9 +87,11 @@ private static void handleList() { } } - private static void handleUnmark(String input) throws AlreadyUnmarkedException { + private static void handleUnmark(String input) throws AlreadyUnmarkedException, InvalidNumberException { int markInt = Integer.parseInt(input.substring(7)) - 1; - if (list[markInt].isDone()) { + if (markInt + 1> taskCounter) { + throw new InvalidNumberException(); + } else if (list[markInt].isDone()) { list[markInt].setDone(false); printFormat("OK, I've marked this task as not done yet:", list[markInt].toString()); @@ -100,9 +100,11 @@ private static void handleUnmark(String input) throws AlreadyUnmarkedException { } } - private static void handleMark(String input) throws AlreadyMarkedException { + private static void handleMark(String input) throws AlreadyMarkedException, InvalidNumberException { int markInt = Integer.parseInt(input.substring(5)) - 1; - if (list[markInt].isDone()) { + if (markInt + 1> taskCounter) { + throw new InvalidNumberException(); + } else if (list[markInt].isDone()) { throw new AlreadyMarkedException(); } else { list[markInt].setDone(true); @@ -181,7 +183,6 @@ public static void main(String[] args) { handleInput(input); } catch (SingleWordCommandException e) { printFormat("Oh no! There should not be any words after '" + input.split(" ")[1] + "'"); - } catch (IndexOutOfBoundsException e) { printFormat("Oh no! You have to include the details of the task '" + input + "'"); } catch (NoSlashException e) { @@ -190,6 +191,8 @@ public static void main(String[] args) { printFormat("Oh no! The item is already marked!"); } catch (AlreadyUnmarkedException e) { printFormat("Oh no! The item is already unmarked!"); + } catch (InvalidNumberException e) { + printFormat("Oh no! The number you have chosen is not valid!"); } } } diff --git a/src/main/java/InvalidNumberException.java b/src/main/java/InvalidNumberException.java new file mode 100644 index 000000000..d4420ab55 --- /dev/null +++ b/src/main/java/InvalidNumberException.java @@ -0,0 +1,3 @@ +public class InvalidNumberException extends Exception { + +} \ No newline at end of file diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..8b49a913e 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -5,3 +5,68 @@ Hello from | |_| | |_| | < __/ |____/ \__,_|_|\_\___| +Hello! I'm Duke +What can I do for you? +type 'commands' for the list of commands +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: + [T][ ] read book +Now you have 1 tasks in the list. +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: + [D][ ] return book (by: June 6th) +Now you have 2 tasks in the list. +____________________________________________________________ +____________________________________________________________ +OOPS!!! I'm sorry, but I don't know what that means :-( +Please type in 'commands' if you are not sure of the commands +____________________________________________________________ +____________________________________________________________ +Here are the tasks in your list: +1.[T][ ] read book +2.[D][ ] return book (by: June 6th) +____________________________________________________________ +____________________________________________________________ +Nice! I've marked this task as done: +[T][X] read book +____________________________________________________________ +____________________________________________________________ +Nice! I've marked this task as done: +[D][X] return book (by: June 6th) +____________________________________________________________ +____________________________________________________________ +OK, I've marked this task as not done yet: +[T][ ] read book +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: + [D][ ] return book (by: Sunday) +Now you have 3 tasks in the list. +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: + [E][ ] project meeting (at: Mon 2-4pm) +Now you have 4 tasks in the list. +____________________________________________________________ +____________________________________________________________ +Here are the tasks in your list: +1.[T][ ] read book +2.[D][X] return book (by: June 6th) +3.[D][ ] return book (by: Sunday) +4.[E][ ] project meeting (at: Mon 2-4pm) +____________________________________________________________ +____________________________________________________________ +Oh no! You have to include the details of the task 'todo' +____________________________________________________________ +____________________________________________________________ +OOPS!!! I'm sorry, but I don't know what that means :-( +Please type in 'commands' if you are not sure of the commands +____________________________________________________________ +____________________________________________________________ +Oh no! The item is already marked! +____________________________________________________________ +____________________________________________________________ +Bye. Hope to see you again soon! +____________________________________________________________ \ No newline at end of file diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..10af9508d 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,14 @@ +todo read book +deadline return book /by June 6th +Event project meeting /at Aug 6th 2-4pm +list +mark 1 +mark 2 +unmark 1 +deadline return book /by Sunday +event project meeting /at Mon 2-4pm +list +todo +test invalid command +mark 2 + From 7e7725324fb86e6ed5d3dd51f6a38426728a8039 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Thu, 17 Feb 2022 19:22:56 +0800 Subject: [PATCH 08/23] Divided classes into packages --- src/main/java/{ => duke}/Duke.java | 2 ++ .../java/{ => duke/exception}/AlreadyMarkedException.java | 2 ++ .../{ => duke/exception}/AlreadyUnmarkedException.java | 2 ++ .../java/{ => duke/exception}/InvalidNumberException.java | 2 ++ src/main/java/{ => duke/exception}/NoSlashException.java | 2 ++ .../{ => duke/exception}/SingleWordCommandException.java | 2 ++ src/main/java/{Deadlines.java => duke/task/Deadline.java} | 6 ++++-- src/main/java/{ => duke/task}/Event.java | 8 +++++--- src/main/java/{ => duke/task}/Task.java | 6 ++++-- src/main/java/{ToDos.java => duke/task/ToDo.java} | 6 ++++-- 10 files changed, 29 insertions(+), 9 deletions(-) rename src/main/java/{ => duke}/Duke.java (99%) rename src/main/java/{ => duke/exception}/AlreadyMarkedException.java (71%) rename src/main/java/{ => duke/exception}/AlreadyUnmarkedException.java (71%) rename src/main/java/{ => duke/exception}/InvalidNumberException.java (71%) rename src/main/java/{ => duke/exception}/NoSlashException.java (69%) rename src/main/java/{ => duke/exception}/SingleWordCommandException.java (72%) rename src/main/java/{Deadlines.java => duke/task/Deadline.java} (72%) rename src/main/java/{ => duke/task}/Event.java (65%) rename src/main/java/{ => duke/task}/Task.java (89%) rename src/main/java/{ToDos.java => duke/task/ToDo.java} (62%) diff --git a/src/main/java/Duke.java b/src/main/java/duke/Duke.java similarity index 99% rename from src/main/java/Duke.java rename to src/main/java/duke/Duke.java index b11d5d443..1b37af981 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,4 +1,6 @@ import java.util.Scanner; +import duke.task.*; +import duke.exception.*; public class Duke { diff --git a/src/main/java/AlreadyMarkedException.java b/src/main/java/duke/exception/AlreadyMarkedException.java similarity index 71% rename from src/main/java/AlreadyMarkedException.java rename to src/main/java/duke/exception/AlreadyMarkedException.java index e36339f81..8fbb60cb4 100644 --- a/src/main/java/AlreadyMarkedException.java +++ b/src/main/java/duke/exception/AlreadyMarkedException.java @@ -1,3 +1,5 @@ +package duke.exception; + public class AlreadyMarkedException extends Exception { } \ No newline at end of file diff --git a/src/main/java/AlreadyUnmarkedException.java b/src/main/java/duke/exception/AlreadyUnmarkedException.java similarity index 71% rename from src/main/java/AlreadyUnmarkedException.java rename to src/main/java/duke/exception/AlreadyUnmarkedException.java index 3cd909625..b50afb733 100644 --- a/src/main/java/AlreadyUnmarkedException.java +++ b/src/main/java/duke/exception/AlreadyUnmarkedException.java @@ -1,3 +1,5 @@ +package duke.exception; + public class AlreadyUnmarkedException extends Exception { } \ No newline at end of file diff --git a/src/main/java/InvalidNumberException.java b/src/main/java/duke/exception/InvalidNumberException.java similarity index 71% rename from src/main/java/InvalidNumberException.java rename to src/main/java/duke/exception/InvalidNumberException.java index d4420ab55..d8030b520 100644 --- a/src/main/java/InvalidNumberException.java +++ b/src/main/java/duke/exception/InvalidNumberException.java @@ -1,3 +1,5 @@ +package duke.exception; + public class InvalidNumberException extends Exception { } \ No newline at end of file diff --git a/src/main/java/NoSlashException.java b/src/main/java/duke/exception/NoSlashException.java similarity index 69% rename from src/main/java/NoSlashException.java rename to src/main/java/duke/exception/NoSlashException.java index 13e5a5946..9a4be38f2 100644 --- a/src/main/java/NoSlashException.java +++ b/src/main/java/duke/exception/NoSlashException.java @@ -1,3 +1,5 @@ +package duke.exception; + public class NoSlashException extends Exception { } \ No newline at end of file diff --git a/src/main/java/SingleWordCommandException.java b/src/main/java/duke/exception/SingleWordCommandException.java similarity index 72% rename from src/main/java/SingleWordCommandException.java rename to src/main/java/duke/exception/SingleWordCommandException.java index d33918615..0636c5488 100644 --- a/src/main/java/SingleWordCommandException.java +++ b/src/main/java/duke/exception/SingleWordCommandException.java @@ -1,3 +1,5 @@ +package duke.exception; + public class SingleWordCommandException extends Exception { } \ No newline at end of file diff --git a/src/main/java/Deadlines.java b/src/main/java/duke/task/Deadline.java similarity index 72% rename from src/main/java/Deadlines.java rename to src/main/java/duke/task/Deadline.java index f1b9ca568..76b841240 100644 --- a/src/main/java/Deadlines.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,8 +1,10 @@ -class Deadline extends Task { +package duke.task; + +public class Deadline extends Task { protected String deadline; - Deadline(String description, String deadline) { + public Deadline(String description, String deadline) { super(description); this.deadline = deadline; } diff --git a/src/main/java/Event.java b/src/main/java/duke/task/Event.java similarity index 65% rename from src/main/java/Event.java rename to src/main/java/duke/task/Event.java index 85c24dea6..0b41dcdc3 100644 --- a/src/main/java/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,13 +1,15 @@ -class Event extends Task { +package duke.task; + +public class Event extends Task { protected String time; - Event(String description, String time) { + public Event(String description, String time) { super(description); this.time = time; } - protected String getTime() { + public String getTime() { return this.time; } diff --git a/src/main/java/Task.java b/src/main/java/duke/task/Task.java similarity index 89% rename from src/main/java/Task.java rename to src/main/java/duke/task/Task.java index 8df72c83e..5f150bafb 100644 --- a/src/main/java/Task.java +++ b/src/main/java/duke/task/Task.java @@ -1,10 +1,12 @@ -class Task { +package duke.task; + +public class Task { public String description; public Boolean isDone = false; public String mark = " "; - Task(String description) { + public Task(String description) { this.description = description; } diff --git a/src/main/java/ToDos.java b/src/main/java/duke/task/ToDo.java similarity index 62% rename from src/main/java/ToDos.java rename to src/main/java/duke/task/ToDo.java index 191474596..89e6d18a0 100644 --- a/src/main/java/ToDos.java +++ b/src/main/java/duke/task/ToDo.java @@ -1,6 +1,8 @@ -class ToDo extends Task { +package duke.task; + +public class ToDo extends Task { - ToDo(String description) { + public ToDo(String description) { super(description); } From 839374b70ed6178b7c36e77bc06bd5ffc4288cc2 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 18 Feb 2022 09:16:27 +0800 Subject: [PATCH 09/23] Changed task list to ArrayList --- src/main/java/duke/Duke.java | 37 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 1b37af981..0202c0172 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,11 +1,12 @@ import java.util.Scanner; import duke.task.*; import duke.exception.*; +import java.util.ArrayList; public class Duke { private static Scanner sc = new Scanner(System.in); - private static Task[] list = new Task[100]; + private static ArrayList list = new ArrayList(); private static int taskCounter = 0; private static String HORIZONTAL_LINE = "____________________________________________________________"; @@ -67,8 +68,8 @@ private static void handleInput(String input) handleCommand(); } else { throw new SingleWordCommandException(); - } - + } + } else { printFormat("OOPS!!! I'm sorry, but I don't know what that means :-(", "Please type in 'commands' if you are not sure of the commands"); @@ -79,24 +80,20 @@ private static void handleInput(String input) private static void handleList() { System.out.println(HORIZONTAL_LINE); System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < list.length; i++) { - if (list[i] != null) { - System.out.println(String.valueOf(i + 1) + "." + list[i]); - } else { - System.out.println(HORIZONTAL_LINE); - break; - } + for (int i = 0; i < list.size(); i++) { + System.out.println(String.valueOf(i + 1) + "." + list.get(i).toString()); } + System.out.println(HORIZONTAL_LINE); } private static void handleUnmark(String input) throws AlreadyUnmarkedException, InvalidNumberException { int markInt = Integer.parseInt(input.substring(7)) - 1; - if (markInt + 1> taskCounter) { + if (markInt + 1 > taskCounter) { throw new InvalidNumberException(); - } else if (list[markInt].isDone()) { - list[markInt].setDone(false); + } else if (list.get(markInt).isDone()) { + list.get(markInt).setDone(false); printFormat("OK, I've marked this task as not done yet:", - list[markInt].toString()); + list.get(markInt).toString()); } else { throw new AlreadyUnmarkedException(); } @@ -106,19 +103,19 @@ private static void handleMark(String input) throws AlreadyMarkedException, Inva int markInt = Integer.parseInt(input.substring(5)) - 1; if (markInt + 1> taskCounter) { throw new InvalidNumberException(); - } else if (list[markInt].isDone()) { + } else if (list.get(markInt).isDone()) { throw new AlreadyMarkedException(); } else { - list[markInt].setDone(true); + list.get(markInt).setDone(true); printFormat("Nice! I've marked this task as done:", - list[markInt].toString()); + list.get(markInt).toString()); } } private static void handleToDo(String input) { String[] subStrings = input.split(" ", 2); Task newTask = new ToDo(subStrings[1]); - list[taskCounter] = newTask; + list.add(newTask); taskCounter++; printFormat("Got it. I've added this task:", " " + newTask.toString(), @@ -133,7 +130,7 @@ private static void handleDeadline(String input) { String inputPreposition = subStrings[0]; String inputDate = subStrings[1]; Task newTask = new Deadline(inputString, inputDate); - list[taskCounter] = newTask; + list.add(newTask); taskCounter++; printFormat("Got it. I've added this task:", " " + newTask.toString(), @@ -148,7 +145,7 @@ private static void handleEvent(String input) { String inputPreposition = subStrings[0]; String inputDate = subStrings[1]; Task newTask = new Event(inputString, inputDate); - list[taskCounter] = newTask; + list.add(newTask); taskCounter++; printFormat("Got it. I've added this task:", " " + newTask.toString(), From ad89ac01a7d84cbc05d7872db8deda495c497ebd Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 18 Feb 2022 09:17:58 +0800 Subject: [PATCH 10/23] Added Level 6: Delete --- src/main/java/duke/Duke.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 0202c0172..a7729f7db 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -68,8 +68,11 @@ private static void handleInput(String input) handleCommand(); } else { throw new SingleWordCommandException(); - } - + } + + } else if (input.contains("delete")) { + handleDelete(input); + } else { printFormat("OOPS!!! I'm sorry, but I don't know what that means :-(", "Please type in 'commands' if you are not sure of the commands"); @@ -112,6 +115,19 @@ private static void handleMark(String input) throws AlreadyMarkedException, Inva } } + private static void handleDelete(String input) throws InvalidNumberException { + int deleteInt = Integer.parseInt(input.substring(7)) - 1; + if (deleteInt + 1 > taskCounter) { + throw new InvalidNumberException(); + } else { + Task deletedTask = list.get(deleteInt); + list.remove(deleteInt); + taskCounter--; + printFormat("Noted. I've removed this task:", deletedTask.toString(), + "Now you have " + String.valueOf(taskCounter) + " tasks in the list."); + } + } + private static void handleToDo(String input) { String[] subStrings = input.split(" ", 2); Task newTask = new ToDo(subStrings[1]); From a3b43cc9a4f558c8ee0047b74d76c2d8aa8745e2 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 25 Feb 2022 22:56:50 +0800 Subject: [PATCH 11/23] Added Level 7: Save --- data/duke.txt | 1 + src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/duke/Duke.java | 101 +++++++++++++++++++++++++- src/main/java/duke/task/Deadline.java | 8 ++ src/main/java/duke/task/Event.java | 10 ++- src/main/java/duke/task/Task.java | 6 +- src/main/java/duke/task/ToDo.java | 8 ++ 7 files changed, 131 insertions(+), 6 deletions(-) create mode 100644 data/duke.txt create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 000000000..a39af4121 --- /dev/null +++ b/data/duke.txt @@ -0,0 +1 @@ +1|D|0|test|tmr diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..9f37e4e0a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 0202c0172..672ff1a09 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,6 +1,15 @@ import java.util.Scanner; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.Path; +import java.io.FileNotFoundException; import duke.task.*; import duke.exception.*; +import duke.task.Event; + import java.util.ArrayList; public class Duke { @@ -9,7 +18,10 @@ public class Duke { private static ArrayList list = new ArrayList(); private static int taskCounter = 0; private static String HORIZONTAL_LINE = "____________________________________________________________"; - + private static String DATA_DIRECTORY = System.getProperty("user.dir") + "/data"; + private static String DATA_FILE = DATA_DIRECTORY + "/duke.txt"; + private static Path dataDirectoryPath = Paths.get(DATA_DIRECTORY); + private static void printFormat(String... args) { System.out.println(HORIZONTAL_LINE); for (String arg : args) { @@ -99,7 +111,8 @@ private static void handleUnmark(String input) throws AlreadyUnmarkedException, } } - private static void handleMark(String input) throws AlreadyMarkedException, InvalidNumberException { + private static void handleMark(String input) throws AlreadyMarkedException, InvalidNumberException, + NumberFormatException { int markInt = Integer.parseInt(input.substring(5)) - 1; if (markInt + 1> taskCounter) { throw new InvalidNumberException(); @@ -117,6 +130,7 @@ private static void handleToDo(String input) { Task newTask = new ToDo(subStrings[1]); list.add(newTask); taskCounter++; + //writeToFile("T", "0", subStrings[1]); printFormat("Got it. I've added this task:", " " + newTask.toString(), "Now you have " + String.valueOf(taskCounter) + " tasks in the list."); @@ -167,12 +181,85 @@ private static void handleCommand() { " e.g. 'event project meeting /at sunday 8-10pm' adds a task with a time range"); } - private static void bye() { + private static void writeToFile() throws IOException { + FileWriter fw = new FileWriter(DATA_FILE, false); + int taskNum = 0; + for (Task task : list) { + taskNum++; + fw.write(convertTask(taskNum, task)); + } + fw.close(); + } + + private static String convertTask(int taskNum, Task task) { + // num|type|T/F|description|(date) + String line = String.valueOf(taskNum) + "|" + task.getType() + "|"; + if (task.isDone) { + line += "1|"; + } else { + line += "0|"; + } + line += task.getDescription().substring(0, task.getDescription().length() - 1); + if (task.toString().contains("(")) { + line += "|" + task.getDate(); + } + line += System.lineSeparator(); + return line; + } + + private static void readFileContents() throws IOException { + boolean directoryExists = new File(DATA_DIRECTORY).exists(); + boolean fileExists = new File(DATA_FILE).exists(); + File dataFile = new File(DATA_FILE); + if (!directoryExists) { + Files.createDirectory(dataDirectoryPath); + } + if (!fileExists) { + dataFile.createNewFile(); + } + Scanner s = new Scanner(dataFile); + while (s.hasNext()) { + parseData(s.nextLine()); + } + } + + private static void parseData(String data) { + String[] dataArr = data.split("\\|"); + Task newTask; + if (dataArr[1].equals("T")) { + newTask = new ToDo(dataArr[3]); + if (Integer.parseInt(dataArr[2]) == 1) { + newTask.setDone(true); + } + } else if (dataArr[1].equals("D")) { + newTask = new Deadline(dataArr[3], dataArr[4]); + if (Integer.parseInt(dataArr[2]) == 1) { + newTask.setDone(true); + } + } else { + newTask = new Event(dataArr[3], dataArr[4]); + if (Integer.parseInt(dataArr[2]) == 1) { + newTask.setDone(true); + } + } + list.add(newTask); + } + + private static void bye() throws IOException { + writeToFile(); printFormat("Bye. Hope to see you again soon!"); } public static void main(String[] args) { intro(); + try { + readFileContents(); + taskCounter = list.size(); + } catch (FileNotFoundException e) { + + } catch (IOException e) { + + } while (sc.hasNext()) { String input = sc.nextLine(); if (input.equals("bye")) { @@ -192,9 +279,15 @@ public static void main(String[] args) { printFormat("Oh no! The item is already unmarked!"); } catch (InvalidNumberException e) { printFormat("Oh no! The number you have chosen is not valid!"); + } catch (NumberFormatException e) { + printFormat("Oh no! You need to be using a number instead!"); } } } - bye(); + try { + bye(); + } catch (IOException e) { + printFormat("Oh no! Something went wrong!"); + } } } \ No newline at end of file diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 76b841240..d10c12c4f 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -13,6 +13,14 @@ String getDeadline() { return this.deadline; } + public String getType() { + return "D"; + } + + public String getDate() { + return this.deadline; + } + @Override public String toString() { return String.format("[D]%s (by: %s)", super.toString(), this.deadline); diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 0b41dcdc3..ff9387f99 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -9,7 +9,15 @@ public Event(String description, String time) { this.time = time; } - public String getTime() { + public String getTime() { + return this.time; + } + + public String getType() { + return "E"; + } + + public String getDate() { return this.time; } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 5f150bafb..6ae990904 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -1,6 +1,6 @@ package duke.task; -public class Task { +public abstract class Task { public String description; public Boolean isDone = false; @@ -31,6 +31,10 @@ public String markIsDone() { return this.mark; } + public abstract String getType(); + + public abstract String getDate(); + @Override public String toString() { return String.format("[%s] %s", this.markIsDone(), this.description); diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java index 89e6d18a0..bd983e7c0 100644 --- a/src/main/java/duke/task/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -6,6 +6,14 @@ public ToDo(String description) { super(description); } + public String getType() { + return "T"; + } + + public String getDate() { + return ""; + } + @Override public String toString() { return String.format("[T]%s", super.toString()); From 1c785393ce3ce58db96a484c24c38fa3d6a61cd5 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Wed, 2 Mar 2022 03:49:57 +0800 Subject: [PATCH 12/23] Added more OOP --- data/duke.txt | 5 +- src/main/java/duke/Duke.java | 320 ++---------------- src/main/java/duke/command/ByeCommand.java | 14 + src/main/java/duke/command/Command.java | 9 + .../java/duke/command/DeadlineCommand.java | 29 ++ src/main/java/duke/command/DeleteCommand.java | 21 ++ src/main/java/duke/command/EventCommand.java | 29 ++ .../java/duke/command/InvalidCommand.java | 10 + src/main/java/duke/command/ListCommand.java | 14 + src/main/java/duke/command/MarkCommand.java | 20 ++ src/main/java/duke/command/ToDoCommand.java | 21 ++ src/main/java/duke/command/UnmarkCommand.java | 20 ++ .../java/duke/command/commandsCommand.java | 14 + .../java/duke/exception/DukeException.java | 14 + src/main/java/duke/parser/Parser.java | 192 +++++++++++ src/main/java/duke/storage/Storage.java | 86 +++++ src/main/java/duke/task/Deadline.java | 2 +- src/main/java/duke/task/Event.java | 2 +- src/main/java/duke/task/Task.java | 2 +- src/main/java/duke/task/ToDo.java | 2 +- src/main/java/duke/ui/Ui.java | 94 +++++ 21 files changed, 624 insertions(+), 296 deletions(-) create mode 100644 src/main/java/duke/command/ByeCommand.java create mode 100644 src/main/java/duke/command/Command.java create mode 100644 src/main/java/duke/command/DeadlineCommand.java create mode 100644 src/main/java/duke/command/DeleteCommand.java create mode 100644 src/main/java/duke/command/EventCommand.java create mode 100644 src/main/java/duke/command/InvalidCommand.java create mode 100644 src/main/java/duke/command/ListCommand.java create mode 100644 src/main/java/duke/command/MarkCommand.java create mode 100644 src/main/java/duke/command/ToDoCommand.java create mode 100644 src/main/java/duke/command/UnmarkCommand.java create mode 100644 src/main/java/duke/command/commandsCommand.java create mode 100644 src/main/java/duke/exception/DukeException.java create mode 100644 src/main/java/duke/parser/Parser.java create mode 100644 src/main/java/duke/storage/Storage.java create mode 100644 src/main/java/duke/ui/Ui.java diff --git a/data/duke.txt b/data/duke.txt index a39af4121..9f1ff9eb0 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1 +1,4 @@ -1|D|0|test|tmr +1|T|0|test +2|D|1|something|tmr +3|E|1|testing|3pm +4|T|0|testtest diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 955e2f409..ad6db791f 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,309 +1,47 @@ +package main.java.duke; + import java.util.Scanner; -import java.io.File; -import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.Path; -import java.io.FileNotFoundException; -import duke.task.*; -import duke.exception.*; -import duke.task.Event; - import java.util.ArrayList; +import main.java.duke.ui.Ui; +import main.java.duke.parser.Parser; +import main.java.duke.command.Command; +import main.java.duke.task.Task; +import main.java.duke.exception.DukeException; +import main.java.duke.storage.Storage; public class Duke { private static Scanner sc = new Scanner(System.in); - private static ArrayList list = new ArrayList(); - private static int taskCounter = 0; - private static String HORIZONTAL_LINE = "____________________________________________________________"; - private static String DATA_DIRECTORY = System.getProperty("user.dir") + "/data"; - private static String DATA_FILE = DATA_DIRECTORY + "/duke.txt"; - private static Path dataDirectoryPath = Paths.get(DATA_DIRECTORY); - - private static void printFormat(String... args) { - System.out.println(HORIZONTAL_LINE); - for (String arg : args) { - System.out.println(arg); - } - System.out.println(HORIZONTAL_LINE); - } - - private static void intro() { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - printFormat("Hello! I'm Duke", - "What can I do for you?", - "type 'commands' for the list of commands"); - } - - private static void handleInput(String input) - throws SingleWordCommandException, IndexOutOfBoundsException, NoSlashException, AlreadyMarkedException, - AlreadyUnmarkedException, InvalidNumberException { - if (input.contains("list")) { - if (input.equals("list")) { - handleList(); - } else { - throw new SingleWordCommandException(); - } - - } else if (input.contains("unmark")) { - handleUnmark(input); - - } else if (input.contains("mark")) { - handleMark(input); - - } else if (input.contains("todo")) { - handleToDo(input); - - } else if (input.contains("deadline")) { - if (input.contains("/")) { - handleDeadline(input); - } else { - throw new NoSlashException(); - } - - } else if (input.contains("event")) { - if (input.contains("/")) { - handleEvent(input); - } else { - throw new NoSlashException(); - } - - } else if (input.contains("commands")) { - if (input.equals("commands")) { - handleCommand(); - } else { - throw new SingleWordCommandException(); - } - - } else if (input.contains("delete")) { - handleDelete(input); - - } else { - printFormat("OOPS!!! I'm sorry, but I don't know what that means :-(", - "Please type in 'commands' if you are not sure of the commands"); - - } - } - - private static void handleList() { - System.out.println(HORIZONTAL_LINE); - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < list.size(); i++) { - System.out.println(String.valueOf(i + 1) + "." + list.get(i).toString()); - } - System.out.println(HORIZONTAL_LINE); - } - - private static void handleUnmark(String input) throws AlreadyUnmarkedException, InvalidNumberException { - int markInt = Integer.parseInt(input.substring(7)) - 1; - if (markInt + 1 > taskCounter) { - throw new InvalidNumberException(); - } else if (list.get(markInt).isDone()) { - list.get(markInt).setDone(false); - printFormat("OK, I've marked this task as not done yet:", - list.get(markInt).toString()); - } else { - throw new AlreadyUnmarkedException(); - } - } - - private static void handleMark(String input) throws AlreadyMarkedException, InvalidNumberException, - NumberFormatException { - int markInt = Integer.parseInt(input.substring(5)) - 1; - if (markInt + 1> taskCounter) { - throw new InvalidNumberException(); - } else if (list.get(markInt).isDone()) { - throw new AlreadyMarkedException(); - } else { - list.get(markInt).setDone(true); - printFormat("Nice! I've marked this task as done:", - list.get(markInt).toString()); - } - } - - private static void handleDelete(String input) throws InvalidNumberException { - int deleteInt = Integer.parseInt(input.substring(7)) - 1; - if (deleteInt + 1 > taskCounter) { - throw new InvalidNumberException(); - } else { - Task deletedTask = list.get(deleteInt); - list.remove(deleteInt); - taskCounter--; - printFormat("Noted. I've removed this task:", deletedTask.toString(), - "Now you have " + String.valueOf(taskCounter) + " tasks in the list."); - } - } + public static ArrayList tasks = new ArrayList(); + public static int taskCounter = 0; + private final Parser parser = new Parser(); - private static void handleToDo(String input) { - String[] subStrings = input.split(" ", 2); - Task newTask = new ToDo(subStrings[1]); - list.add(newTask); - taskCounter++; - //writeToFile("T", "0", subStrings[1]); - printFormat("Got it. I've added this task:", - " " + newTask.toString(), - "Now you have " + String.valueOf(taskCounter) + " tasks in the list."); - } - - private static void handleDeadline(String input) { - String[] subStrings = input.split(" ", 2); - subStrings = subStrings[1].split("/", 2); - String inputString = subStrings[0]; - subStrings = subStrings[1].split(" ", 2); - String inputPreposition = subStrings[0]; - String inputDate = subStrings[1]; - Task newTask = new Deadline(inputString, inputDate); - list.add(newTask); - taskCounter++; - printFormat("Got it. I've added this task:", - " " + newTask.toString(), - "Now you have " + String.valueOf(taskCounter) + " tasks in the list."); - } - - private static void handleEvent(String input) { - String[] subStrings = input.split(" ", 2); - subStrings = subStrings[1].split("/", 2); - String inputString = subStrings[0]; - subStrings = subStrings[1].split(" ", 2); - String inputPreposition = subStrings[0]; - String inputDate = subStrings[1]; - Task newTask = new Event(inputString, inputDate); - list.add(newTask); - taskCounter++; - printFormat("Got it. I've added this task:", - " " + newTask.toString(), - "Now you have " + String.valueOf(taskCounter) + " tasks in the list."); - } - - private static void handleCommand() { - printFormat("List of valid commands:", - "'list' - lists out all tasks and its details", - "'mark (X)' - marks item X on the list as done", - " (X is a number) e.g. 'mark 3' marks item 3 on the list", - "'unmark (X)' - unmarks item X on the list as done", - " (X is a number) e.g. 'unmark 3' unmarks item 3 on the list", - "'todo (task)' - ToDos are tasks without specific deadlines", - " (task is a string) e.g. 'todo buy shampoo' adds the task 'buy shampoo' to the list", - "'deadline (task) /by (date/time)' - Deadlines are tasks that need to be done before a specific date/time", - " e.g. 'deadline math homework /by tues 2pm' adds a task with a deadline to the list", - "'event (task) /at (date/time)' - Events are tasks that start at a specific time and ends at a specific time", - " e.g. 'event project meeting /at sunday 8-10pm' adds a task with a time range"); - } - - private static void writeToFile() throws IOException { - FileWriter fw = new FileWriter(DATA_FILE, false); - int taskNum = 0; - for (Task task : list) { - taskNum++; - fw.write(convertTask(taskNum, task)); - } - fw.close(); - } - - private static String convertTask(int taskNum, Task task) { - // num|type|T/F|description|(date) - String line = String.valueOf(taskNum) + "|" + task.getType() + "|"; - if (task.isDone) { - line += "1|"; - } else { - line += "0|"; - } - line += task.getDescription().substring(0, task.getDescription().length() - 1); - if (task.toString().contains("(")) { - line += "|" + task.getDate(); - } - line += System.lineSeparator(); - return line; - } - - private static void readFileContents() throws IOException { - boolean directoryExists = new File(DATA_DIRECTORY).exists(); - boolean fileExists = new File(DATA_FILE).exists(); - File dataFile = new File(DATA_FILE); - if (!directoryExists) { - Files.createDirectory(dataDirectoryPath); - } - if (!fileExists) { - dataFile.createNewFile(); - } - Scanner s = new Scanner(dataFile); - while (s.hasNext()) { - parseData(s.nextLine()); - } - } - - private static void parseData(String data) { - String[] dataArr = data.split("\\|"); - Task newTask; - if (dataArr[1].equals("T")) { - newTask = new ToDo(dataArr[3]); - if (Integer.parseInt(dataArr[2]) == 1) { - newTask.setDone(true); - } - } else if (dataArr[1].equals("D")) { - newTask = new Deadline(dataArr[3], dataArr[4]); - if (Integer.parseInt(dataArr[2]) == 1) { - newTask.setDone(true); - } - } else { - newTask = new Event(dataArr[3], dataArr[4]); - if (Integer.parseInt(dataArr[2]) == 1) { - newTask.setDone(true); - } - } - list.add(newTask); - } - - private static void bye() throws IOException { - writeToFile(); - printFormat("Bye. Hope to see you again soon!"); - } - - public static void main(String[] args) { - intro(); + private void run() { try { - readFileContents(); - taskCounter = list.size(); - } catch (FileNotFoundException e) { - + Storage.load(); } catch (IOException e) { - + System.out.println("ERROR IN LOADING FILE"); } - while (sc.hasNext()) { - String input = sc.nextLine(); - if (input.equals("bye")) { - break; - } else { - try { - handleInput(input); - } catch (SingleWordCommandException e) { - printFormat("Oh no! There should not be any words after '" + input.split(" ")[1] + "'"); - } catch (IndexOutOfBoundsException e) { - printFormat("Oh no! You have to include the details of the task '" + input + "'"); - } catch (NoSlashException e) { - printFormat("Oh no! You need to include '/' in your task '" + input + "'"); - } catch (AlreadyMarkedException e) { - printFormat("Oh no! The item is already marked!"); - } catch (AlreadyUnmarkedException e) { - printFormat("Oh no! The item is already unmarked!"); - } catch (InvalidNumberException e) { - printFormat("Oh no! The number you have chosen is not valid!"); - } catch (NumberFormatException e) { - printFormat("Oh no! You need to be using a number instead!"); - } + Ui.printIntro(); + String input = ""; + while (!input.equals("bye")) { + try { + input = sc.nextLine(); + Command command = parser.parse(input); + command.execute(); + } catch (DukeException e) { + Ui.printError(e); } } try { - bye(); + Storage.writeToFile(); } catch (IOException e) { - printFormat("Oh no! Something went wrong!"); + System.out.println("ERROR IN WRITING FILE"); } } + + public static void main(String[] args) { + new Duke().run(); + } } \ No newline at end of file diff --git a/src/main/java/duke/command/ByeCommand.java b/src/main/java/duke/command/ByeCommand.java new file mode 100644 index 000000000..c0bdab77c --- /dev/null +++ b/src/main/java/duke/command/ByeCommand.java @@ -0,0 +1,14 @@ +package main.java.duke.command; + +import main.java.duke.ui.Ui; + +public class ByeCommand implements Command { + + public ByeCommand() { + + } + + public void execute() { + Ui.printBye(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java new file mode 100644 index 000000000..a9928ce0b --- /dev/null +++ b/src/main/java/duke/command/Command.java @@ -0,0 +1,9 @@ +package main.java.duke.command; + +import main.java.duke.exception.DukeException; + +public interface Command { + + public void execute() throws DukeException; + +} \ No newline at end of file diff --git a/src/main/java/duke/command/DeadlineCommand.java b/src/main/java/duke/command/DeadlineCommand.java new file mode 100644 index 000000000..6b4ec6a45 --- /dev/null +++ b/src/main/java/duke/command/DeadlineCommand.java @@ -0,0 +1,29 @@ +package main.java.duke.command; + +import main.java.duke.task.Deadline; +import main.java.duke.Duke; +import main.java.duke.ui.Ui; +import main.java.duke.exception.DukeException; + +public class DeadlineCommand implements Command { + + private String input; + + public DeadlineCommand(String input) { + this.input = input; + } + + public void execute() throws DukeException { + String[] splitString = input.split("/by ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need to include a date after '/by'!"); + } else if (splitString[0].equals("")) { + throw new DukeException("Oh no! You need a description for this event!"); + } else { + Deadline task = new Deadline(splitString[0], splitString[1]); + Duke.tasks.add(task); + Duke.taskCounter++; + Ui.printTask(task); + } + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java new file mode 100644 index 000000000..9fb0c60a9 --- /dev/null +++ b/src/main/java/duke/command/DeleteCommand.java @@ -0,0 +1,21 @@ +package main.java.duke.command; + +import main.java.duke.task.Task; +import main.java.duke.Duke; +import main.java.duke.ui.Ui; + +public class DeleteCommand implements Command { + + private final int deleteInt; + + public DeleteCommand(int deleteInt) { + this.deleteInt = deleteInt; + } + + public void execute() { + Task task = Duke.tasks.get(deleteInt - 1); + Duke.tasks.remove(deleteInt - 1); + Duke.taskCounter--; + Ui.printDelete(task); + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/EventCommand.java b/src/main/java/duke/command/EventCommand.java new file mode 100644 index 000000000..d2f616fc5 --- /dev/null +++ b/src/main/java/duke/command/EventCommand.java @@ -0,0 +1,29 @@ +package main.java.duke.command; + +import main.java.duke.task.Event; +import main.java.duke.Duke; +import main.java.duke.ui.Ui; +import main.java.duke.exception.DukeException; + +public class EventCommand implements Command { + + private String input; + + public EventCommand(String input) { + this.input = input; + } + + public void execute() throws DukeException { + String[] splitString = input.split("/at ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need to include a date after '/at'!"); + } else if (splitString[0].equals("")) { + throw new DukeException("Oh no! You need a description for this event!"); + } else { + Event task = new Event(splitString[0], splitString[1]); + Duke.tasks.add(task); + Duke.taskCounter++; + Ui.printTask(task); + } + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/InvalidCommand.java b/src/main/java/duke/command/InvalidCommand.java new file mode 100644 index 000000000..9a786f149 --- /dev/null +++ b/src/main/java/duke/command/InvalidCommand.java @@ -0,0 +1,10 @@ +package main.java.duke.command; + +import main.java.duke.ui.Ui; + +public class InvalidCommand implements Command { + + public void execute() { + Ui.printInvalid(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java new file mode 100644 index 000000000..9e1be9487 --- /dev/null +++ b/src/main/java/duke/command/ListCommand.java @@ -0,0 +1,14 @@ +package main.java.duke.command; + +import main.java.duke.ui.Ui; + +public class ListCommand implements Command { + + public ListCommand() { + + } + + public void execute() { + Ui.printList(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java new file mode 100644 index 000000000..264477a0e --- /dev/null +++ b/src/main/java/duke/command/MarkCommand.java @@ -0,0 +1,20 @@ +package main.java.duke.command; + +import main.java.duke.task.Task; +import main.java.duke.Duke; +import main.java.duke.ui.Ui; + +public class MarkCommand implements Command { + + private final int markInt; + + public MarkCommand(int markInt) { + this.markInt = markInt; + } + + public void execute() { + Task task = Duke.tasks.get(markInt - 1); + task.setDone(true); + Ui.printMark(markInt, task); + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/ToDoCommand.java b/src/main/java/duke/command/ToDoCommand.java new file mode 100644 index 000000000..4e62431c1 --- /dev/null +++ b/src/main/java/duke/command/ToDoCommand.java @@ -0,0 +1,21 @@ +package main.java.duke.command; + +import main.java.duke.task.ToDo; +import main.java.duke.Duke; +import main.java.duke.ui.Ui; + +public class ToDoCommand implements Command { + + private String input; + + public ToDoCommand(String input) { + this.input = input; + } + + public void execute() { + ToDo task = new ToDo(this.input); + Duke.tasks.add(task); + Duke.taskCounter++; + Ui.printTask(task); + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java new file mode 100644 index 000000000..c19789bb1 --- /dev/null +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -0,0 +1,20 @@ +package main.java.duke.command; + +import main.java.duke.task.Task; +import main.java.duke.Duke; +import main.java.duke.ui.Ui; + +public class UnmarkCommand implements Command { + + private final int markInt; + + public UnmarkCommand(int markInt) { + this.markInt = markInt; + } + + public void execute() { + Task task = Duke.tasks.get(markInt - 1); + task.setDone(false); + Ui.printUnmark(markInt, task); + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/commandsCommand.java b/src/main/java/duke/command/commandsCommand.java new file mode 100644 index 000000000..dbfd2d772 --- /dev/null +++ b/src/main/java/duke/command/commandsCommand.java @@ -0,0 +1,14 @@ +package main.java.duke.command; + +import main.java.duke.ui.Ui; + +public class commandsCommand implements Command { + + public commandsCommand() { + + } + + public void execute() { + Ui.printCommand(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/exception/DukeException.java b/src/main/java/duke/exception/DukeException.java new file mode 100644 index 000000000..7a3a33274 --- /dev/null +++ b/src/main/java/duke/exception/DukeException.java @@ -0,0 +1,14 @@ +package main.java.duke.exception; + +public class DukeException extends Exception { + + private final String error; + + public DukeException(String error) { + this.error = error; + } + + public String getMessage() { + return this.error; + } +} \ No newline at end of file diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java new file mode 100644 index 000000000..140a53bda --- /dev/null +++ b/src/main/java/duke/parser/Parser.java @@ -0,0 +1,192 @@ +package main.java.duke.parser; + +import main.java.duke.Duke; +import main.java.duke.ui.Ui; +import main.java.duke.exception.DukeException; +import main.java.duke.command.Command; +import main.java.duke.command.ListCommand; +import main.java.duke.command.UnmarkCommand; +import main.java.duke.command.MarkCommand; +import main.java.duke.command.DeleteCommand; +import main.java.duke.command.ToDoCommand; +import main.java.duke.command.DeadlineCommand; +import main.java.duke.command.EventCommand; +import main.java.duke.command.InvalidCommand; +import main.java.duke.command.commandsCommand; +import main.java.duke.command.ByeCommand; + +public class Parser { + + private final String LIST = "list"; + private final String UNMARK = "unmark"; + private final String MARK = "mark"; + private final String TODO = "todo"; + private final String DEADLINE = "deadline"; + private final String EVENT = "event"; + private final String COMMANDS = "commands"; + private final String DELETE = "delete"; + private final String BYE = "bye"; + + public Command parse(String input) throws DukeException { + String lowerCaseInput = input.toLowerCase(); + String lowerCaseFirstWord = lowerCaseInput.split(" ")[0]; + if (lowerCaseFirstWord.equals(LIST)) { + return parseList(); + + } else if (lowerCaseFirstWord.equals(UNMARK)) { + return parseUnmark(input); + + } else if (lowerCaseFirstWord.equals(MARK)) { + return parseMark(input); + + } else if (lowerCaseFirstWord.equals(TODO)) { + return parseToDo(input); + + } else if (lowerCaseFirstWord.equals(DEADLINE)) { + if (input.contains("/by")) { + return parseDeadline(input); + } else { + throw new DukeException("Oh no! You need to include a '/by' in the command!"); + } + + } else if (lowerCaseFirstWord.equals(EVENT)) { + if (input.contains("/at")) { + return parseEvent(input); + } else { + throw new DukeException("Oh no! You need to include a '/at' in the command!"); + } + + } else if (lowerCaseFirstWord.equals(COMMANDS)) { + return parseCommands(); + + } else if (lowerCaseFirstWord.equals(DELETE)) { + return parseDelete(input); + + } else if (lowerCaseFirstWord.equals(BYE)) { + return new ByeCommand(); + + } else { + return new InvalidCommand(); + + } + } + + private boolean isNum(String string) { + try { + Integer.parseInt(string); + } catch (NumberFormatException e) { + return false; + } + return true; + } + + private Command parseList() { + return new ListCommand(); + } + + private Command parseUnmark(String input) throws DukeException { + String[] splitString = input.split(" ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need to choose a task to unmark!"); + + } else if (isNum(splitString[1])) { + int markInt = Integer.parseInt(splitString[1]); + if ((markInt > Duke.taskCounter) || (markInt < 1)) { + throw new DukeException("Oh no! The number you have chosen is not valid!"); + + } else if (!Duke.tasks.get(markInt - 1).isDone()) { + throw new DukeException("Oh no! The task you have selected is not been marked!"); + + } + else { + return new UnmarkCommand(markInt); + + } + } else { + throw new DukeException("Oh no! You need to choose a number!"); + + } + } + + private Command parseMark(String input) throws DukeException { + String[] splitString = input.split(" ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need to choose a task to unmark!"); + + } else if (isNum(splitString[1])) { + int markInt = Integer.parseInt(splitString[1]); + if ((markInt > Duke.taskCounter) || (markInt < 1)) { + throw new DukeException("Oh no! The number you have chosen is not valid!"); + + } else if (Duke.tasks.get(markInt - 1).isDone()) { + throw new DukeException("Oh no! The task you have selected is already been marked"); + + } + else { + return new MarkCommand(markInt); + + } + } else { + throw new DukeException("Oh no! You need to choose a number!"); + + } + } + + private Command parseDelete(String input) throws DukeException { + String[] splitString = input.split(" ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need to choose a task to unmark!"); + + } else if (isNum(splitString[1])) { + int deleteInt = Integer.parseInt(splitString[1]); + if ((deleteInt + 1 > Duke.taskCounter) || (deleteInt < 1)) { + throw new DukeException("Oh no! The number you have chosen is not valid!"); + + } else { + return new DeleteCommand(deleteInt); + + } + } else { + throw new DukeException("Oh no! You need to choose a number!"); + + } + } + + private Command parseToDo(String input) throws DukeException { + String[] splitString = input.split(" ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need to enter the description of the task!"); + + } else { + return new ToDoCommand(splitString[1]); + + } + } + + private Command parseDeadline(String input) throws DukeException { + String[] splitString = input.split(" ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need to enter the description of the task!"); + + } else { + return new DeadlineCommand(splitString[1]); + + } + } + + private Command parseEvent(String input) throws DukeException { + String[] splitString = input.split(" ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need to enter the description of the task!"); + + } else { + return new EventCommand(splitString[1]); + + } + } + + private Command parseCommands() { + return new commandsCommand(); + } + +} \ No newline at end of file diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java new file mode 100644 index 000000000..fad7b31ac --- /dev/null +++ b/src/main/java/duke/storage/Storage.java @@ -0,0 +1,86 @@ +package main.java.duke.storage; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; +import main.java.duke.task.Task; +import main.java.duke.task.ToDo; +import main.java.duke.task.Deadline; +import main.java.duke.task.Event; +import main.java.duke.Duke; + +public class Storage { + + private static String DATA_DIRECTORY = System.getProperty("user.dir") + "/data"; + private static String DATA_FILE = DATA_DIRECTORY + "/duke.txt"; + private static Path dataDirectoryPath = Paths.get(DATA_DIRECTORY); + + public static void load() throws IOException { + boolean directoryExists = new File(DATA_DIRECTORY).exists(); + boolean fileExists = new File(DATA_FILE).exists(); + File dataFile = new File(DATA_FILE); + if (!directoryExists) { + Files.createDirectory(dataDirectoryPath); + } + if (!fileExists) { + dataFile.createNewFile(); + } + Scanner s = new Scanner(dataFile); + while (s.hasNext()) { + parseData(s.nextLine()); + } + Duke.taskCounter = Duke.tasks.size(); + } + + private static void parseData(String data) { + String[] dataArr = data.split("\\|"); + Task newTask; + if (dataArr[1].equals("T")) { + newTask = new ToDo(dataArr[3]); + if (Integer.parseInt(dataArr[2]) == 1) { + newTask.setDone(true); + } + } else if (dataArr[1].equals("D")) { + newTask = new Deadline(dataArr[3], dataArr[4]); + if (Integer.parseInt(dataArr[2]) == 1) { + newTask.setDone(true); + } + } else { + newTask = new Event(dataArr[3], dataArr[4]); + if (Integer.parseInt(dataArr[2]) == 1) { + newTask.setDone(true); + } + } + Duke.tasks.add(newTask); + } + + public static void writeToFile() throws IOException { + FileWriter fw = new FileWriter(DATA_FILE, false); + int taskNum = 0; + for (Task task : Duke.tasks) { + taskNum++; + fw.write(convertTask(taskNum, task)); + } + fw.close(); + } + + // num|type|T/F|description|(date) + private static String convertTask(int taskNum, Task task) { + String line = String.valueOf(taskNum) + "|" + task.getType() + "|"; + if (task.isDone) { + line += "1|"; + } else { + line += "0|"; + } + line += task.getDescription(); + if (task.toString().contains("(")) { + line += "|" + task.getDate(); + } + line += System.lineSeparator(); + return line; + } +} \ No newline at end of file diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index d10c12c4f..92c14864c 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,4 +1,4 @@ -package duke.task; +package main.java.duke.task; public class Deadline extends Task { diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index ff9387f99..6edb8b86f 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,4 +1,4 @@ -package duke.task; +package main.java.duke.task; public class Event extends Task { diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 6ae990904..bad7847cd 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -1,4 +1,4 @@ -package duke.task; +package main.java.duke.task; public abstract class Task { diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java index bd983e7c0..9d0c5ecc4 100644 --- a/src/main/java/duke/task/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -1,4 +1,4 @@ -package duke.task; +package main.java.duke.task; public class ToDo extends Task { diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java new file mode 100644 index 000000000..166ac322d --- /dev/null +++ b/src/main/java/duke/ui/Ui.java @@ -0,0 +1,94 @@ +package main.java.duke.ui; + +/** + * The Ui class handles all print statements + */ + +import java.util.ArrayList; +import main.java.duke.task.Task; +import main.java.duke.Duke; +import main.java.duke.exception.DukeException; + +public class Ui { + + private final static String HORIZONTAL_LINE = "____________________________________________________________"; + + public static void printFormat(String... args) { + System.out.println(HORIZONTAL_LINE); + for (String arg : args) { + System.out.println(arg); + } + System.out.println(HORIZONTAL_LINE); + } + + public static void printIntro() { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); + printFormat("Hello! I'm Duke", + "What can I do for you?", + "type 'commands' for the list of commands"); + } + + public static void printList() { + System.out.println(HORIZONTAL_LINE); + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < Duke.tasks.size(); i++) { + System.out.println(String.valueOf(i + 1) + "." + Duke.tasks.get(i).toString()); + } + System.out.println(HORIZONTAL_LINE); + } + + public static void printUnmark(int markInt, Task task) { + printFormat("OK, I've marked this task as not done yet:", + task.toString()); + } + + public static void printMark(int markInt, Task task) { + printFormat("Nice! I've marked this task as done:", + task.toString()); + } + + public static void printDelete(Task task) { + printFormat("Noted. I've removed this task:", task.toString(), + "Now you have " + String.valueOf(Duke.taskCounter) + " tasks in the list."); + } + + public static void printTask(Task task) { + printFormat("Got it. I've added this task:", + " " + task.toString(), + "Now you have " + String.valueOf(Duke.taskCounter) + " tasks in the list."); + } + + public static void printCommand() { + printFormat("List of valid commands:", + "'list' - lists out all tasks and its details", + "'mark X' - marks item X on the list as done", + " (X is a number) e.g. 'mark 3' marks item 3 on the list", + "'unmark X' - unmarks item X on the list as done", + " (X is a number) e.g. 'unmark 3' unmarks item 3 on the list", + "'todo X' - ToDos are tasks without specific deadlines", + " (X is a string) e.g. 'todo buy shampoo' adds the task 'buy shampoo' to the list", + "'deadline X /by date time' - Deadlines are tasks that need to be done before a specific date/time", + " e.g. 'deadline math homework /by tues 2pm' adds a task with a deadline to the list", + "'event X /at date time' - Events are tasks that start at a specific time and ends at a specific time", + " e.g. 'event project meeting /at sunday 8-10pm' adds a task with a time range"); + } + + public static void printBye() { + printFormat("Bye. Hope to see you again soon!"); + } + + public static void printInvalid() { + printFormat("OOPS!!! I'm sorry, but I don't know what that means :-(", + "Please type in 'commands' if you are not sure of the commands"); + } + + public static void printError(DukeException e) { + System.out.println(e.getMessage()); + } + +} \ No newline at end of file From 5572363581dd116f26d991d282298b3a8c2409c3 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Wed, 2 Mar 2022 04:23:01 +0800 Subject: [PATCH 13/23] Added Level 9 - Find --- src/main/java/duke/command/FindCommand.java | 23 ++++++++++++++ src/main/java/duke/parser/Parser.java | 5 +++ src/main/java/duke/ui/Ui.java | 34 ++++++++++++++++++--- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/main/java/duke/command/FindCommand.java diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java new file mode 100644 index 000000000..642af4b43 --- /dev/null +++ b/src/main/java/duke/command/FindCommand.java @@ -0,0 +1,23 @@ +package main.java.duke.command; + +import main.java.duke.ui.Ui; +import main.java.duke.Duke; +import main.java.duke.exception.DukeException; + +public class FindCommand implements Command { + + private final String input; + + public FindCommand(String input) { + this.input = input; + } + + public void execute() throws DukeException { + String[] splitString = input.split(" ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need include a keyword to search for"); + } else { + Ui.printFind(splitString[1]); + } + } +} \ No newline at end of file diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 140a53bda..164a03ebd 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -14,6 +14,7 @@ import main.java.duke.command.InvalidCommand; import main.java.duke.command.commandsCommand; import main.java.duke.command.ByeCommand; +import main.java.duke.command.FindCommand; public class Parser { @@ -26,6 +27,7 @@ public class Parser { private final String COMMANDS = "commands"; private final String DELETE = "delete"; private final String BYE = "bye"; + private final String FIND = "find"; public Command parse(String input) throws DukeException { String lowerCaseInput = input.toLowerCase(); @@ -65,6 +67,9 @@ public Command parse(String input) throws DukeException { } else if (lowerCaseFirstWord.equals(BYE)) { return new ByeCommand(); + } else if (lowerCaseFirstWord.equals(FIND)) { + return new FindCommand(input); + } else { return new InvalidCommand(); diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 166ac322d..66effb68a 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -11,7 +11,8 @@ public class Ui { - private final static String HORIZONTAL_LINE = "____________________________________________________________"; + private final static String HORIZONTAL_LINE = "____________________________________" + + "________________________"; public static void printFormat(String... args) { System.out.println(HORIZONTAL_LINE); @@ -42,6 +43,23 @@ public static void printList() { System.out.println(HORIZONTAL_LINE); } + public static void printFind(String keyword) { + Boolean foundTask = false; + System.out.println(HORIZONTAL_LINE); + System.out.println("Here are the matching tasks in your list:"); + for (int i = 0; i < Duke.tasks.size(); i++) { + Task task = Duke.tasks.get(i); + if (task.toString().contains(keyword)) { + foundTask = true; + System.out.println(String.valueOf(i + 1) + "." + task.toString()); + } + } + if (!foundTask) { + System.out.println("There are no matching task in your list"); + } + System.out.println(HORIZONTAL_LINE); + } + public static void printUnmark(int markInt, Task task) { printFormat("OK, I've marked this task as not done yet:", task.toString()); @@ -70,11 +88,17 @@ public static void printCommand() { " (X is a number) e.g. 'mark 3' marks item 3 on the list", "'unmark X' - unmarks item X on the list as done", " (X is a number) e.g. 'unmark 3' unmarks item 3 on the list", + "'delete X' - deletes item X on the list", + " (X is a number) e.g. 'delete 3' deletes item 3 on the list", + "'find X - lists items in the list that contain the keyword X", + " (X is a string) e.g. 'find book' lists items in the list that contain 'book'", "'todo X' - ToDos are tasks without specific deadlines", - " (X is a string) e.g. 'todo buy shampoo' adds the task 'buy shampoo' to the list", - "'deadline X /by date time' - Deadlines are tasks that need to be done before a specific date/time", - " e.g. 'deadline math homework /by tues 2pm' adds a task with a deadline to the list", - "'event X /at date time' - Events are tasks that start at a specific time and ends at a specific time", + " (X is a string) e.g. 'todo buy soap' adds the task 'buy soap' to the list", + "'deadline X /by date time' - Deadlines are tasks that need " + + "to be done before a specific date/time", + " e.g. 'deadline homework /by mon 2pm' adds a task with a deadline to the list", + "'event X /at date time' - Events are tasks that start at a specific " + + "time and ends at a specific time", " e.g. 'event project meeting /at sunday 8-10pm' adds a task with a time range"); } From 8da9e3045129c7d315cc7f232da5119556dcb905 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 07:32:11 +0800 Subject: [PATCH 14/23] Added Level 8 - Dates and Times --- data/duke.txt | 7 +- src/main/java/duke/Duke.java | 3 + src/main/java/duke/command/ByeCommand.java | 2 +- .../java/duke/command/CheckDateCommand.java | 25 ++++++ src/main/java/duke/command/Command.java | 80 ++++++++++++++++++- .../java/duke/command/DeadlineCommand.java | 7 +- src/main/java/duke/command/DeleteCommand.java | 2 +- src/main/java/duke/command/EventCommand.java | 55 +++++++++++-- .../java/duke/command/InvalidCommand.java | 2 +- src/main/java/duke/command/ListCommand.java | 2 +- src/main/java/duke/command/MarkCommand.java | 2 +- src/main/java/duke/command/ToDoCommand.java | 2 +- src/main/java/duke/command/UnmarkCommand.java | 2 +- .../java/duke/command/commandsCommand.java | 2 +- src/main/java/duke/parser/Parser.java | 37 ++++++--- src/main/java/duke/storage/Storage.java | 17 ++-- src/main/java/duke/task/Deadline.java | 33 +++++--- src/main/java/duke/task/Event.java | 46 ++++++++--- src/main/java/duke/task/Task.java | 8 +- src/main/java/duke/task/ToDo.java | 15 +++- src/main/java/duke/ui/Ui.java | 41 +++++++++- 21 files changed, 322 insertions(+), 68 deletions(-) create mode 100644 src/main/java/duke/command/CheckDateCommand.java diff --git a/data/duke.txt b/data/duke.txt index 9f1ff9eb0..00f16e71f 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,4 +1,3 @@ -1|T|0|test -2|D|1|something|tmr -3|E|1|testing|3pm -4|T|0|testtest +1|E|0|eventtest|2045-03-02|2pm|2012-05-04|4pm +2|D|0|test deadline|2043-05-06|1am +3|T|0|test diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index ad6db791f..fb64e0227 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -22,6 +22,9 @@ private void run() { Storage.load(); } catch (IOException e) { System.out.println("ERROR IN LOADING FILE"); + } catch (DukeException e) { + System.out.println(e.getMessage()); + return; } Ui.printIntro(); String input = ""; diff --git a/src/main/java/duke/command/ByeCommand.java b/src/main/java/duke/command/ByeCommand.java index c0bdab77c..133300914 100644 --- a/src/main/java/duke/command/ByeCommand.java +++ b/src/main/java/duke/command/ByeCommand.java @@ -2,7 +2,7 @@ import main.java.duke.ui.Ui; -public class ByeCommand implements Command { +public class ByeCommand extends Command { public ByeCommand() { diff --git a/src/main/java/duke/command/CheckDateCommand.java b/src/main/java/duke/command/CheckDateCommand.java new file mode 100644 index 000000000..18da3afd5 --- /dev/null +++ b/src/main/java/duke/command/CheckDateCommand.java @@ -0,0 +1,25 @@ +package main.java.duke.command; + +import main.java.duke.exception.DukeException; +import main.java.duke.ui.Ui; +import java.time.LocalDate; + +public class CheckDateCommand extends Command { + + private final String date; + + public CheckDateCommand(String date) { + this.date = date; + } + + public void execute() throws DukeException { + if (isValidDate(convertDate(this.date))) { + LocalDate localDate = LocalDate.parse(convertDate(this.date)); + Ui.printCheckDate(localDate); + + } else { + throw new DukeException("Oh no! You have typed an invalid date!\n" + + "The format for date is DD/MM/YYYY !, e.g. 15/02/2022"); + } + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java index a9928ce0b..7d4a37f0f 100644 --- a/src/main/java/duke/command/Command.java +++ b/src/main/java/duke/command/Command.java @@ -1,9 +1,85 @@ package main.java.duke.command; +import main.java.duke.Duke; import main.java.duke.exception.DukeException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; -public interface Command { +public abstract class Command { - public void execute() throws DukeException; + protected boolean isNum(String string) { + try { + Integer.parseInt(string); + } catch (NumberFormatException e) { + return false; + } + return true; + } + + protected String convertDate(String date) throws DukeException { + String[] dateTimeArray = date.split(" "); + String[] dateArray = dateTimeArray[0].split("/"); + if (dateArray.length != 3) { + throw new DukeException("Oh no! Your date format is invalid!\n" + + "The format for date is DD/MM/YYYY !, e.g. 15/02/2022"); + } else if (!isNum(dateArray[0]) || !isNum(dateArray[1]) || !isNum(dateArray[2])) { + throw new DukeException("Oh no! Your date format is invalid!\n" + + "The format for date is DD/MM/YYYY !, e.g. 15/02/2022"); + } + String dayString = String.format("%02d", Integer.parseInt(dateArray[0])); + String monthString = String.format("%02d", Integer.parseInt(dateArray[1])); + String yearString; + String localDateString; + if (dateArray[2].length() == 2) { + yearString = "20" + dateArray[2]; + } else if (dateArray[2].length() == 4) { + yearString = dateArray[2]; + } else { + throw new DukeException("Oh no! You have typed an invalid date!\n" + + "The format for date is DD/MM/YYYY !, e.g. 15/02/2022"); + } + + localDateString = yearString + "-" + monthString + "-" + dayString; + if (isValidDate(localDateString)) { + return localDateString; + } else { + throw new DukeException("Oh no! You have typed an invalid date!\n" + + "The format for date is DD/MM/YYYY !, e.g. 15/02/2022"); + } + } + + protected boolean isValidDate(String date) { + try { + LocalDate.parse(date); + } catch (DateTimeParseException e) { + return false; + } + return true; + } + + protected String convertTime(String date) throws DukeException { + String[] dateTimeArray = date.split(" "); + String time; + if (dateTimeArray.length < 2) { + throw new DukeException("Oh no! You did not key in a time"); + } else { + time = dateTimeArray[1]; + if (isNum(time)) { + if (Integer.parseInt(time) < 2400 && Integer.parseInt(time) >= 0 && + Integer.parseInt(time) % 100 < 60) { + return time; + } else { + throw new DukeException("Oh no! The time you entered is invalid!\n" + + "The format for time is in 24-hour format! e.g. 1800"); + } + } else { + throw new DukeException("Oh no! The time you entered is invalid!\n" + + "The format for time is in 24-hour format! e.g. 1800"); + } + } + } + + public abstract void execute() throws DukeException; } \ No newline at end of file diff --git a/src/main/java/duke/command/DeadlineCommand.java b/src/main/java/duke/command/DeadlineCommand.java index 6b4ec6a45..b44a73492 100644 --- a/src/main/java/duke/command/DeadlineCommand.java +++ b/src/main/java/duke/command/DeadlineCommand.java @@ -5,7 +5,7 @@ import main.java.duke.ui.Ui; import main.java.duke.exception.DukeException; -public class DeadlineCommand implements Command { +public class DeadlineCommand extends Command { private String input; @@ -14,13 +14,14 @@ public DeadlineCommand(String input) { } public void execute() throws DukeException { - String[] splitString = input.split("/by ", 2); + String[] splitString = input.split(" /by ", 2); if (splitString.length < 2) { throw new DukeException("Oh no! You need to include a date after '/by'!"); } else if (splitString[0].equals("")) { throw new DukeException("Oh no! You need a description for this event!"); } else { - Deadline task = new Deadline(splitString[0], splitString[1]); + Deadline task = new Deadline(splitString[0], convertDate(splitString[1]), + convertTime(splitString[1])); Duke.tasks.add(task); Duke.taskCounter++; Ui.printTask(task); diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java index 9fb0c60a9..504aa0c97 100644 --- a/src/main/java/duke/command/DeleteCommand.java +++ b/src/main/java/duke/command/DeleteCommand.java @@ -4,7 +4,7 @@ import main.java.duke.Duke; import main.java.duke.ui.Ui; -public class DeleteCommand implements Command { +public class DeleteCommand extends Command { private final int deleteInt; diff --git a/src/main/java/duke/command/EventCommand.java b/src/main/java/duke/command/EventCommand.java index d2f616fc5..d57c73ad5 100644 --- a/src/main/java/duke/command/EventCommand.java +++ b/src/main/java/duke/command/EventCommand.java @@ -4,8 +4,10 @@ import main.java.duke.Duke; import main.java.duke.ui.Ui; import main.java.duke.exception.DukeException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; -public class EventCommand implements Command { +public class EventCommand extends Command { private String input; @@ -13,17 +15,58 @@ public EventCommand(String input) { this.input = input; } + private boolean isValidDates(String startDate, String startTime, + String endDate, String endTime) { + LocalDate startLocalDate = LocalDate.parse(startDate); + LocalDate endLocalDate = LocalDate.parse(endDate); + if (startLocalDate.isBefore(endLocalDate)) { + return true; + + } else if (startLocalDate.equals(endLocalDate) && + Integer.parseInt(endTime) > Integer.parseInt(startTime)) { + return true; + + } else { + return false; + + } + } + public void execute() throws DukeException { - String[] splitString = input.split("/at ", 2); + String[] splitString = input.split(" /at ", 2); if (splitString.length < 2) { throw new DukeException("Oh no! You need to include a date after '/at'!"); } else if (splitString[0].equals("")) { throw new DukeException("Oh no! You need a description for this event!"); } else { - Event task = new Event(splitString[0], splitString[1]); - Duke.tasks.add(task); - Duke.taskCounter++; - Ui.printTask(task); + String date = splitString[1]; + if (date.contains(" /to ")) { + String[] timings = date.split(" /to "); + if (timings.length < 2) { + throw new DukeException("Oh no! You need to include a date after '/to'!"); + } else if (timings[0].equals("")) { + throw new DukeException("Oh no! You need to include a date before '/to'!"); + } else { + String startDate = convertDate(timings[0]); + String startTime = convertTime(timings[0]); + String endDate = convertDate(timings[1]); + String endTime = convertTime(timings[1]); + if (isValidDates(startDate, startTime, endDate, endTime)) { + Event task = new Event(splitString[0], startDate, + startTime, endDate, endTime); + Duke.tasks.add(task); + Duke.taskCounter++; + Ui.printTask(task); + } else { + throw new DukeException("Oh no! Your ending date is after " + + "your starting date!"); + } + } + } else { + throw new DukeException("Oh no! You need to include a '/to' between" + + " your start and end time!"); + } + } } } \ No newline at end of file diff --git a/src/main/java/duke/command/InvalidCommand.java b/src/main/java/duke/command/InvalidCommand.java index 9a786f149..96259fb46 100644 --- a/src/main/java/duke/command/InvalidCommand.java +++ b/src/main/java/duke/command/InvalidCommand.java @@ -2,7 +2,7 @@ import main.java.duke.ui.Ui; -public class InvalidCommand implements Command { +public class InvalidCommand extends Command { public void execute() { Ui.printInvalid(); diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java index 9e1be9487..8a1e45b31 100644 --- a/src/main/java/duke/command/ListCommand.java +++ b/src/main/java/duke/command/ListCommand.java @@ -2,7 +2,7 @@ import main.java.duke.ui.Ui; -public class ListCommand implements Command { +public class ListCommand extends Command { public ListCommand() { diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java index 264477a0e..eea1d334b 100644 --- a/src/main/java/duke/command/MarkCommand.java +++ b/src/main/java/duke/command/MarkCommand.java @@ -4,7 +4,7 @@ import main.java.duke.Duke; import main.java.duke.ui.Ui; -public class MarkCommand implements Command { +public class MarkCommand extends Command { private final int markInt; diff --git a/src/main/java/duke/command/ToDoCommand.java b/src/main/java/duke/command/ToDoCommand.java index 4e62431c1..936e72271 100644 --- a/src/main/java/duke/command/ToDoCommand.java +++ b/src/main/java/duke/command/ToDoCommand.java @@ -4,7 +4,7 @@ import main.java.duke.Duke; import main.java.duke.ui.Ui; -public class ToDoCommand implements Command { +public class ToDoCommand extends Command { private String input; diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java index c19789bb1..678fd1230 100644 --- a/src/main/java/duke/command/UnmarkCommand.java +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -4,7 +4,7 @@ import main.java.duke.Duke; import main.java.duke.ui.Ui; -public class UnmarkCommand implements Command { +public class UnmarkCommand extends Command { private final int markInt; diff --git a/src/main/java/duke/command/commandsCommand.java b/src/main/java/duke/command/commandsCommand.java index dbfd2d772..1f4075944 100644 --- a/src/main/java/duke/command/commandsCommand.java +++ b/src/main/java/duke/command/commandsCommand.java @@ -2,7 +2,7 @@ import main.java.duke.ui.Ui; -public class commandsCommand implements Command { +public class commandsCommand extends Command { public commandsCommand() { diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 140a53bda..cc3b96913 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -13,6 +13,7 @@ import main.java.duke.command.EventCommand; import main.java.duke.command.InvalidCommand; import main.java.duke.command.commandsCommand; +import main.java.duke.command.CheckDateCommand; import main.java.duke.command.ByeCommand; public class Parser { @@ -25,44 +26,47 @@ public class Parser { private final String EVENT = "event"; private final String COMMANDS = "commands"; private final String DELETE = "delete"; + private final String CHECK_DATE = "check date"; private final String BYE = "bye"; public Command parse(String input) throws DukeException { String lowerCaseInput = input.toLowerCase(); - String lowerCaseFirstWord = lowerCaseInput.split(" ")[0]; - if (lowerCaseFirstWord.equals(LIST)) { + if (lowerCaseInput.startsWith(LIST)) { return parseList(); - } else if (lowerCaseFirstWord.equals(UNMARK)) { + } else if (lowerCaseInput.startsWith(UNMARK)) { return parseUnmark(input); - } else if (lowerCaseFirstWord.equals(MARK)) { + } else if (lowerCaseInput.startsWith(MARK)) { return parseMark(input); - } else if (lowerCaseFirstWord.equals(TODO)) { + } else if (lowerCaseInput.startsWith(TODO)) { return parseToDo(input); - } else if (lowerCaseFirstWord.equals(DEADLINE)) { + } else if (lowerCaseInput.startsWith(DEADLINE)) { if (input.contains("/by")) { return parseDeadline(input); } else { throw new DukeException("Oh no! You need to include a '/by' in the command!"); } - } else if (lowerCaseFirstWord.equals(EVENT)) { + } else if (lowerCaseInput.startsWith(EVENT)) { if (input.contains("/at")) { return parseEvent(input); } else { throw new DukeException("Oh no! You need to include a '/at' in the command!"); } - } else if (lowerCaseFirstWord.equals(COMMANDS)) { + } else if (lowerCaseInput.startsWith(COMMANDS)) { return parseCommands(); - } else if (lowerCaseFirstWord.equals(DELETE)) { + } else if (lowerCaseInput.startsWith(DELETE)) { return parseDelete(input); - } else if (lowerCaseFirstWord.equals(BYE)) { + } else if (lowerCaseInput.startsWith(CHECK_DATE)) { + return parseCheckDate(input); + + } else if (lowerCaseInput.startsWith(BYE)) { return new ByeCommand(); } else { @@ -139,7 +143,7 @@ private Command parseDelete(String input) throws DukeException { } else if (isNum(splitString[1])) { int deleteInt = Integer.parseInt(splitString[1]); - if ((deleteInt + 1 > Duke.taskCounter) || (deleteInt < 1)) { + if ((deleteInt > Duke.taskCounter) || (deleteInt < 1)) { throw new DukeException("Oh no! The number you have chosen is not valid!"); } else { @@ -185,6 +189,17 @@ private Command parseEvent(String input) throws DukeException { } } + private Command parseCheckDate(String input) throws DukeException { + String[] splitString = input.split(" ", 3); + if (splitString.length < 3) { + throw new DukeException("Oh no! You need to enter the date that you want to check!"); + + } else { + return new CheckDateCommand(splitString[2]); + + } + } + private Command parseCommands() { return new commandsCommand(); } diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index fad7b31ac..54dd1d912 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -12,6 +12,7 @@ import main.java.duke.task.Deadline; import main.java.duke.task.Event; import main.java.duke.Duke; +import main.java.duke.exception.DukeException; public class Storage { @@ -19,7 +20,7 @@ public class Storage { private static String DATA_FILE = DATA_DIRECTORY + "/duke.txt"; private static Path dataDirectoryPath = Paths.get(DATA_DIRECTORY); - public static void load() throws IOException { + public static void load() throws IOException, DukeException { boolean directoryExists = new File(DATA_DIRECTORY).exists(); boolean fileExists = new File(DATA_FILE).exists(); File dataFile = new File(DATA_FILE); @@ -36,7 +37,7 @@ public static void load() throws IOException { Duke.taskCounter = Duke.tasks.size(); } - private static void parseData(String data) { + private static void parseData(String data) throws DukeException { String[] dataArr = data.split("\\|"); Task newTask; if (dataArr[1].equals("T")) { @@ -45,15 +46,17 @@ private static void parseData(String data) { newTask.setDone(true); } } else if (dataArr[1].equals("D")) { - newTask = new Deadline(dataArr[3], dataArr[4]); + newTask = new Deadline(dataArr[3], dataArr[4], dataArr[5]); if (Integer.parseInt(dataArr[2]) == 1) { newTask.setDone(true); } - } else { - newTask = new Event(dataArr[3], dataArr[4]); + } else if (dataArr[1].equals("E")) { + newTask = new Event(dataArr[3], dataArr[4], dataArr[5], dataArr[6], dataArr[7]); if (Integer.parseInt(dataArr[2]) == 1) { newTask.setDone(true); } + } else { + throw new DukeException("ERROR IN DATA FILE"); } Duke.tasks.add(newTask); } @@ -71,14 +74,14 @@ public static void writeToFile() throws IOException { // num|type|T/F|description|(date) private static String convertTask(int taskNum, Task task) { String line = String.valueOf(taskNum) + "|" + task.getType() + "|"; - if (task.isDone) { + if (task.isDone()) { line += "1|"; } else { line += "0|"; } line += task.getDescription(); if (task.toString().contains("(")) { - line += "|" + task.getDate(); + line += "|" + task.getDateTime(); } line += System.lineSeparator(); return line; diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 92c14864c..781382545 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,28 +1,41 @@ package main.java.duke.task; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + public class Deadline extends Task { - protected String deadline; + private final String date; + private final String time; + private final LocalDate localDate; - public Deadline(String description, String deadline) { + public Deadline(String description, String date, String time) { super(description); - this.deadline = deadline; - } - - String getDeadline() { - return this.deadline; + this.date = date; + this.time = time; + this.localDate = LocalDate.parse(date); } public String getType() { return "D"; } - public String getDate() { - return this.deadline; + public LocalDate getStartDate() { + return this.localDate; + } + + public LocalDate getEndDate() { + return this.localDate; + } + + public String getDateTime() { + return this.date + "|" + this.time; } @Override public String toString() { - return String.format("[D]%s (by: %s)", super.toString(), this.deadline); + String dateTime = this.localDate. + format(DateTimeFormatter.ofPattern("MMM d yyyy")).toString() + " " + this.time; + return String.format("[D]%s (by: %s)", super.toString(), dateTime); } } \ No newline at end of file diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 6edb8b86f..03e37b60d 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,28 +1,54 @@ package main.java.duke.task; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + public class Event extends Task { - protected String time; + private final String startDate; + private final String startTime; + private final String endDate; + private final String endTime; + private final LocalDate localStartDate; + private final LocalDate localEndDate; - public Event(String description, String time) { + public Event(String description, String startDate, String startTime, + String endDate, String endTime) { super(description); - this.time = time; - } - - public String getTime() { - return this.time; + this.startDate = startDate; + this.startTime = startTime; + this.endDate = endDate; + this.endTime = endTime; + this.localStartDate = LocalDate.parse(startDate); + this.localEndDate = LocalDate.parse(endDate); } public String getType() { return "E"; } - public String getDate() { - return this.time; + public LocalDate getStartDate() { + return this.localStartDate; + } + + public LocalDate getEndDate() { + return this.localEndDate; + } + + public String getDateTime() { + return String.format("%s|%s|%s|%s", this.startDate, this.startTime, + this.endDate, this.endTime); } @Override public String toString() { - return String.format("[E]%s (at: %s)", super.toString(), this.time); + String startDateTime = this.localStartDate. + format(DateTimeFormatter.ofPattern("MMM d yyyy")).toString() + " " + + this.startTime; + String endDateTime = this.localEndDate. + format(DateTimeFormatter.ofPattern("MMM d yyyy")).toString() + " " + + this.endTime; + return String.format("[E]%s (at: %s to %s)", super.toString(), + startDateTime, endDateTime); } } \ No newline at end of file diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index bad7847cd..30a70837c 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -1,5 +1,7 @@ package main.java.duke.task; +import java.time.LocalDate; + public abstract class Task { public String description; @@ -33,7 +35,11 @@ public String markIsDone() { public abstract String getType(); - public abstract String getDate(); + public abstract LocalDate getStartDate(); + + public abstract LocalDate getEndDate(); + + public abstract String getDateTime(); @Override public String toString() { diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java index 9d0c5ecc4..03211fd20 100644 --- a/src/main/java/duke/task/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -1,5 +1,8 @@ package main.java.duke.task; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + public class ToDo extends Task { public ToDo(String description) { @@ -10,8 +13,16 @@ public String getType() { return "T"; } - public String getDate() { - return ""; + public LocalDate getStartDate() { + return null; + } + + public LocalDate getEndDate() { + return null; + } + + public String getDateTime() { + return null; } @Override diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 166ac322d..5f6f00399 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -8,6 +8,8 @@ import main.java.duke.task.Task; import main.java.duke.Duke; import main.java.duke.exception.DukeException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; public class Ui { @@ -72,10 +74,41 @@ public static void printCommand() { " (X is a number) e.g. 'unmark 3' unmarks item 3 on the list", "'todo X' - ToDos are tasks without specific deadlines", " (X is a string) e.g. 'todo buy shampoo' adds the task 'buy shampoo' to the list", - "'deadline X /by date time' - Deadlines are tasks that need to be done before a specific date/time", - " e.g. 'deadline math homework /by tues 2pm' adds a task with a deadline to the list", - "'event X /at date time' - Events are tasks that start at a specific time and ends at a specific time", - " e.g. 'event project meeting /at sunday 8-10pm' adds a task with a time range"); + "'deadline X /by DD/MM/YYYY TIME' - Deadlines are tasks that need to" + + " be done before a specific date and time in 24-hour format", + " e.g. 'deadline math homework /by 2/3/2022 1400' adds a " + + "task with deadline Mar 2 2022 1400 to the list", + "'event X /at DD/MM/YYYY TIME /to DD/MM/YYYY TIME' - Events are tasks that start" + + " at a specific date and time and ends at a specific date and time", + " e.g. 'event project meeting /at 3/3/2022 0900' /to 4/3/2022 1800 " + + "adds a task with a time range"); + } + + public static void printCheckDate(LocalDate localDate) { + System.out.println(HORIZONTAL_LINE); + System.out.println("Here are the tasks on " + + localDate.format(DateTimeFormatter.ofPattern("MMM d yyyy")).toString() + ":"); + for (int i = 0; i < Duke.tasks.size(); i++) { + Task task = Duke.tasks.get(i); + if (isOnDate(task, localDate)) { + System.out.println(String.valueOf(i + 1) + "." + task.toString()); + } + } + System.out.println(HORIZONTAL_LINE); + } + + private static boolean isOnDate(Task task, LocalDate localDate) { + if (task.getType().equals("D")) { + if (localDate.equals(task.getStartDate())) { + return true; + } + } else if (task.getType().equals("E")) { + if (localDate.equals(task.getStartDate()) || localDate.equals(task.getEndDate()) || + (localDate.isAfter(task.getStartDate()) && localDate.isBefore(task.getEndDate()))) { + return true; + } + } + return false; } public static void printBye() { From d26c13695edbc90ec36e903310c977dacde6d779 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 07:38:23 +0800 Subject: [PATCH 15/23] Updated master branch --- src/main/java/duke/command/FindCommand.java | 23 ++++++++++++++ src/main/java/duke/parser/Parser.java | 5 +++ src/main/java/duke/ui/Ui.java | 34 ++++++++++++++++++--- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/main/java/duke/command/FindCommand.java diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java new file mode 100644 index 000000000..642af4b43 --- /dev/null +++ b/src/main/java/duke/command/FindCommand.java @@ -0,0 +1,23 @@ +package main.java.duke.command; + +import main.java.duke.ui.Ui; +import main.java.duke.Duke; +import main.java.duke.exception.DukeException; + +public class FindCommand implements Command { + + private final String input; + + public FindCommand(String input) { + this.input = input; + } + + public void execute() throws DukeException { + String[] splitString = input.split(" ", 2); + if (splitString.length < 2) { + throw new DukeException("Oh no! You need include a keyword to search for"); + } else { + Ui.printFind(splitString[1]); + } + } +} \ No newline at end of file diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 140a53bda..164a03ebd 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -14,6 +14,7 @@ import main.java.duke.command.InvalidCommand; import main.java.duke.command.commandsCommand; import main.java.duke.command.ByeCommand; +import main.java.duke.command.FindCommand; public class Parser { @@ -26,6 +27,7 @@ public class Parser { private final String COMMANDS = "commands"; private final String DELETE = "delete"; private final String BYE = "bye"; + private final String FIND = "find"; public Command parse(String input) throws DukeException { String lowerCaseInput = input.toLowerCase(); @@ -65,6 +67,9 @@ public Command parse(String input) throws DukeException { } else if (lowerCaseFirstWord.equals(BYE)) { return new ByeCommand(); + } else if (lowerCaseFirstWord.equals(FIND)) { + return new FindCommand(input); + } else { return new InvalidCommand(); diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 166ac322d..66effb68a 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -11,7 +11,8 @@ public class Ui { - private final static String HORIZONTAL_LINE = "____________________________________________________________"; + private final static String HORIZONTAL_LINE = "____________________________________" + + "________________________"; public static void printFormat(String... args) { System.out.println(HORIZONTAL_LINE); @@ -42,6 +43,23 @@ public static void printList() { System.out.println(HORIZONTAL_LINE); } + public static void printFind(String keyword) { + Boolean foundTask = false; + System.out.println(HORIZONTAL_LINE); + System.out.println("Here are the matching tasks in your list:"); + for (int i = 0; i < Duke.tasks.size(); i++) { + Task task = Duke.tasks.get(i); + if (task.toString().contains(keyword)) { + foundTask = true; + System.out.println(String.valueOf(i + 1) + "." + task.toString()); + } + } + if (!foundTask) { + System.out.println("There are no matching task in your list"); + } + System.out.println(HORIZONTAL_LINE); + } + public static void printUnmark(int markInt, Task task) { printFormat("OK, I've marked this task as not done yet:", task.toString()); @@ -70,11 +88,17 @@ public static void printCommand() { " (X is a number) e.g. 'mark 3' marks item 3 on the list", "'unmark X' - unmarks item X on the list as done", " (X is a number) e.g. 'unmark 3' unmarks item 3 on the list", + "'delete X' - deletes item X on the list", + " (X is a number) e.g. 'delete 3' deletes item 3 on the list", + "'find X - lists items in the list that contain the keyword X", + " (X is a string) e.g. 'find book' lists items in the list that contain 'book'", "'todo X' - ToDos are tasks without specific deadlines", - " (X is a string) e.g. 'todo buy shampoo' adds the task 'buy shampoo' to the list", - "'deadline X /by date time' - Deadlines are tasks that need to be done before a specific date/time", - " e.g. 'deadline math homework /by tues 2pm' adds a task with a deadline to the list", - "'event X /at date time' - Events are tasks that start at a specific time and ends at a specific time", + " (X is a string) e.g. 'todo buy soap' adds the task 'buy soap' to the list", + "'deadline X /by date time' - Deadlines are tasks that need " + + "to be done before a specific date/time", + " e.g. 'deadline homework /by mon 2pm' adds a task with a deadline to the list", + "'event X /at date time' - Events are tasks that start at a specific " + + "time and ends at a specific time", " e.g. 'event project meeting /at sunday 8-10pm' adds a task with a time range"); } From a7f8ae9504057ec54ed74b8e4c95d456d38c6231 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 07:51:34 +0800 Subject: [PATCH 16/23] Resolved conflicts --- src/main/java/duke/command/FindCommand.java | 2 +- src/main/java/duke/parser/Parser.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java index 642af4b43..5e30a205c 100644 --- a/src/main/java/duke/command/FindCommand.java +++ b/src/main/java/duke/command/FindCommand.java @@ -4,7 +4,7 @@ import main.java.duke.Duke; import main.java.duke.exception.DukeException; -public class FindCommand implements Command { +public class FindCommand extends Command { private final String input; diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index c463c65af..f8201d8fd 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -71,7 +71,7 @@ public Command parse(String input) throws DukeException { } else if (lowerCaseInput.startsWith(BYE)) { return new ByeCommand(); - } else if (lowerCaseFirstWord.equals(FIND)) { + } else if (lowerCaseInput.startsWith(FIND)) { return new FindCommand(input); } else { From 5f7b0fca1a492a4caafcc0c04d9004bc3f1d2bed Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 08:36:18 +0800 Subject: [PATCH 17/23] Removed unused java files --- src/main/java/duke/exception/AlreadyMarkedException.java | 5 ----- src/main/java/duke/exception/AlreadyUnmarkedException.java | 5 ----- src/main/java/duke/exception/InvalidNumberException.java | 5 ----- src/main/java/duke/exception/NoSlashException.java | 5 ----- src/main/java/duke/exception/SingleWordCommandException.java | 5 ----- 5 files changed, 25 deletions(-) delete mode 100644 src/main/java/duke/exception/AlreadyMarkedException.java delete mode 100644 src/main/java/duke/exception/AlreadyUnmarkedException.java delete mode 100644 src/main/java/duke/exception/InvalidNumberException.java delete mode 100644 src/main/java/duke/exception/NoSlashException.java delete mode 100644 src/main/java/duke/exception/SingleWordCommandException.java diff --git a/src/main/java/duke/exception/AlreadyMarkedException.java b/src/main/java/duke/exception/AlreadyMarkedException.java deleted file mode 100644 index 8fbb60cb4..000000000 --- a/src/main/java/duke/exception/AlreadyMarkedException.java +++ /dev/null @@ -1,5 +0,0 @@ -package duke.exception; - -public class AlreadyMarkedException extends Exception { - -} \ No newline at end of file diff --git a/src/main/java/duke/exception/AlreadyUnmarkedException.java b/src/main/java/duke/exception/AlreadyUnmarkedException.java deleted file mode 100644 index b50afb733..000000000 --- a/src/main/java/duke/exception/AlreadyUnmarkedException.java +++ /dev/null @@ -1,5 +0,0 @@ -package duke.exception; - -public class AlreadyUnmarkedException extends Exception { - -} \ No newline at end of file diff --git a/src/main/java/duke/exception/InvalidNumberException.java b/src/main/java/duke/exception/InvalidNumberException.java deleted file mode 100644 index d8030b520..000000000 --- a/src/main/java/duke/exception/InvalidNumberException.java +++ /dev/null @@ -1,5 +0,0 @@ -package duke.exception; - -public class InvalidNumberException extends Exception { - -} \ No newline at end of file diff --git a/src/main/java/duke/exception/NoSlashException.java b/src/main/java/duke/exception/NoSlashException.java deleted file mode 100644 index 9a4be38f2..000000000 --- a/src/main/java/duke/exception/NoSlashException.java +++ /dev/null @@ -1,5 +0,0 @@ -package duke.exception; - -public class NoSlashException extends Exception { - -} \ No newline at end of file diff --git a/src/main/java/duke/exception/SingleWordCommandException.java b/src/main/java/duke/exception/SingleWordCommandException.java deleted file mode 100644 index 0636c5488..000000000 --- a/src/main/java/duke/exception/SingleWordCommandException.java +++ /dev/null @@ -1,5 +0,0 @@ -package duke.exception; - -public class SingleWordCommandException extends Exception { - -} \ No newline at end of file From 6dbe24654fd8827ba701289b0702abf14e0ee1d3 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 14:39:52 +0800 Subject: [PATCH 18/23] Added JavaDocs for all Classes and methods --- src/main/java/duke/Duke.java | 21 +++-- src/main/java/duke/command/ByeCommand.java | 19 ++++- .../java/duke/command/CheckDateCommand.java | 15 ++++ src/main/java/duke/command/Command.java | 35 ++++++++ .../java/duke/command/DeadlineCommand.java | 15 ++++ src/main/java/duke/command/DeleteCommand.java | 13 +++ src/main/java/duke/command/EventCommand.java | 24 ++++++ src/main/java/duke/command/FindCommand.java | 15 ++++ .../java/duke/command/InvalidCommand.java | 8 ++ src/main/java/duke/command/ListCommand.java | 11 ++- src/main/java/duke/command/MarkCommand.java | 13 +++ src/main/java/duke/command/ToDoCommand.java | 13 +++ src/main/java/duke/command/UnmarkCommand.java | 13 +++ .../java/duke/command/commandsCommand.java | 11 ++- .../java/duke/exception/DukeException.java | 15 ++++ src/main/java/duke/parser/Parser.java | 79 ++++++++++++++++++- src/main/java/duke/storage/Storage.java | 36 ++++++++- src/main/java/duke/task/Deadline.java | 37 +++++++++ src/main/java/duke/task/Event.java | 39 +++++++++ src/main/java/duke/task/Task.java | 60 +++++++++++++- src/main/java/duke/task/ToDo.java | 28 +++++++ src/main/java/duke/ui/Ui.java | 74 ++++++++++++++++- 22 files changed, 568 insertions(+), 26 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index fb64e0227..3935ad4e9 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -10,6 +10,11 @@ import main.java.duke.exception.DukeException; import main.java.duke.storage.Storage; +/** + * Main class for Duke that runs the bot. The bot takes in user inputs, and has various commands + * that would perform various tasks + */ + public class Duke { private static Scanner sc = new Scanner(System.in); @@ -17,6 +22,11 @@ public class Duke { public static int taskCounter = 0; private final Parser parser = new Parser(); + /** + * Main method used to run the bot. It calls the load() method from the Storage class to + * load data saved from previous uses, will ask for user inputs, will call the writeToFile() + * method from Storage to update the latest information. + */ private void run() { try { Storage.load(); @@ -37,13 +47,14 @@ private void run() { Ui.printError(e); } } - try { - Storage.writeToFile(); - } catch (IOException e) { - System.out.println("ERROR IN WRITING FILE"); - } + } + /** + * Main method for Duke. + * + * @param args NIL. + */ public static void main(String[] args) { new Duke().run(); } diff --git a/src/main/java/duke/command/ByeCommand.java b/src/main/java/duke/command/ByeCommand.java index 133300914..01c962305 100644 --- a/src/main/java/duke/command/ByeCommand.java +++ b/src/main/java/duke/command/ByeCommand.java @@ -1,14 +1,25 @@ package main.java.duke.command; +import java.io.IOException; import main.java.duke.ui.Ui; +import main.java.duke.storage.Storage; -public class ByeCommand extends Command { - - public ByeCommand() { +/** + * Class for ByeCommand. It is created when the user wants to close Duke. + */ - } +public class ByeCommand extends Command { + /** + * Method to carry out the command. It initiates the writing of the task list into + * the txt file, and prints a goodbye message before closing Duke. + */ public void execute() { + try { + Storage.writeToFile(); + } catch (IOException e) { + System.out.println("ERROR IN WRITING FILE"); + } Ui.printBye(); } } \ No newline at end of file diff --git a/src/main/java/duke/command/CheckDateCommand.java b/src/main/java/duke/command/CheckDateCommand.java index 18da3afd5..92c30babf 100644 --- a/src/main/java/duke/command/CheckDateCommand.java +++ b/src/main/java/duke/command/CheckDateCommand.java @@ -4,14 +4,29 @@ import main.java.duke.ui.Ui; import java.time.LocalDate; +/** + * Class for the CheckDateCommand. It is created when the user wants to check if a date + * has any task. + */ + public class CheckDateCommand extends Command { private final String date; + /** + * Constructor for CheckDateCommand. + * + * @param date String date that is being checked in the format DD/MM/YYYY + */ public CheckDateCommand(String date) { this.date = date; } + /** + * Method to carry out the command. Calls a Ui method to show tasks on the date. + * + * @throws DukeException If user input is invalid. + */ public void execute() throws DukeException { if (isValidDate(convertDate(this.date))) { LocalDate localDate = LocalDate.parse(convertDate(this.date)); diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java index 7d4a37f0f..1cef15f9e 100644 --- a/src/main/java/duke/command/Command.java +++ b/src/main/java/duke/command/Command.java @@ -6,8 +6,18 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; +/** + * Abstract class for all Commands. Contains basic methods for different commands. + */ + public abstract class Command { + /** + * Method to check if a String is a number. + * + * @param string String that is being checked. + * @return Boolean value whether the String is a number. + */ protected boolean isNum(String string) { try { Integer.parseInt(string); @@ -17,6 +27,13 @@ protected boolean isNum(String string) { return true; } + /** + * Method to convert a date from DD/MM/YYYY to YYYY-MM-DD. + * + * @param date String date in DD/MM/YYYY time format. + * @return String date in YYYY-MM-DD format. + * @throws DukeException If input date format is invalid. + */ protected String convertDate(String date) throws DukeException { String[] dateTimeArray = date.split(" "); String[] dateArray = dateTimeArray[0].split("/"); @@ -49,6 +66,12 @@ protected String convertDate(String date) throws DukeException { } } + /** + * Checks if date in YYYY-MM-DD is a valid date. + * + * @param date String date in YYYY-MM-DD format. + * @return Boolean value whether date is a valid date. + */ protected boolean isValidDate(String date) { try { LocalDate.parse(date); @@ -58,6 +81,13 @@ protected boolean isValidDate(String date) { return true; } + /** + * Method that checks if time input is valid. + * + * @param date String date in DD/MM/YYYY time format. + * @return String time in 24-hour format. + * @throws DukeException If time is invalid. + */ protected String convertTime(String date) throws DukeException { String[] dateTimeArray = date.split(" "); String time; @@ -80,6 +110,11 @@ protected String convertTime(String date) throws DukeException { } } + /** + * Abstract method that all commands have. + * + * @throws DukeException If command cannot be executed due to invalid inputs. + */ public abstract void execute() throws DukeException; } \ No newline at end of file diff --git a/src/main/java/duke/command/DeadlineCommand.java b/src/main/java/duke/command/DeadlineCommand.java index b44a73492..6427ea795 100644 --- a/src/main/java/duke/command/DeadlineCommand.java +++ b/src/main/java/duke/command/DeadlineCommand.java @@ -5,14 +5,29 @@ import main.java.duke.ui.Ui; import main.java.duke.exception.DukeException; +/** + * Class for DeadlineCommand. It is called when the user wants to create a Deadline. + */ + public class DeadlineCommand extends Command { private String input; + /** + * Constructor for DeadlineCommand. + * + * @param input User input. + */ public DeadlineCommand(String input) { this.input = input; } + /** + * Method to carry out the command. It checks if user input is valid, creates a new Deadline + * and adds it onto the task list. It then calls the Ui to print if the task has been added. + * + * @throws DukeException If user input is invalid. + */ public void execute() throws DukeException { String[] splitString = input.split(" /by ", 2); if (splitString.length < 2) { diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java index 504aa0c97..cf9d27ef0 100644 --- a/src/main/java/duke/command/DeleteCommand.java +++ b/src/main/java/duke/command/DeleteCommand.java @@ -4,14 +4,27 @@ import main.java.duke.Duke; import main.java.duke.ui.Ui; +/** + * Class for DeleteCommand. It is created when the user wants to delete a task from the task list. + */ + public class DeleteCommand extends Command { private final int deleteInt; + /** + * Constructor for DeleteCommand. + * + * @param deleteInt The task of number deleteInt that the user wants to remove. + */ public DeleteCommand(int deleteInt) { this.deleteInt = deleteInt; } + /** + * Method to carry out the command. It gets the task to be removed from the task list, and + * calls the Ui command to show the task that has been deleted. + */ public void execute() { Task task = Duke.tasks.get(deleteInt - 1); Duke.tasks.remove(deleteInt - 1); diff --git a/src/main/java/duke/command/EventCommand.java b/src/main/java/duke/command/EventCommand.java index d57c73ad5..64fcdaffc 100644 --- a/src/main/java/duke/command/EventCommand.java +++ b/src/main/java/duke/command/EventCommand.java @@ -7,14 +7,32 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; +/** + * Class for EventCommand. It is created when the user wants to create a new Event. + */ + public class EventCommand extends Command { private String input; + /** + * Constructor for EventCommand. + * + * @param input User input. + */ public EventCommand(String input) { this.input = input; } + /** + * Method to check if user input dates are valid. + * + * @param startDate The start date in YYYY-MM-DD. + * @param startTime The start time in 24-hour format. + * @param endDate The end date in YYYY-MM-DD. + * @param endTime The end time in 24-hour format. + * @return Boolean value whether the input dates are valid. + */ private boolean isValidDates(String startDate, String startTime, String endDate, String endTime) { LocalDate startLocalDate = LocalDate.parse(startDate); @@ -32,6 +50,12 @@ private boolean isValidDates(String startDate, String startTime, } } + /** + * Method to carry out the command. It checks if user input is valid at various points. If + * valid, it creates an Event and adds it onto the task list. + * + * @throws DukeException If user input is invalid. + */ public void execute() throws DukeException { String[] splitString = input.split(" /at ", 2); if (splitString.length < 2) { diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java index 5e30a205c..72a4f56d4 100644 --- a/src/main/java/duke/command/FindCommand.java +++ b/src/main/java/duke/command/FindCommand.java @@ -4,14 +4,29 @@ import main.java.duke.Duke; import main.java.duke.exception.DukeException; +/** + * Class for FindCommand. It is created when the user requests to search for a specific keyword in + * the tasks in the list. + */ public class FindCommand extends Command { private final String input; + /** + * Constructor for FindCommand. + * + * @param input User input. + */ public FindCommand(String input) { this.input = input; } + /** + * Method to carry out the command. It calls Ui to search for tasks with description + * matching the keyword. + * + * @throws DukeException If user input is invalid. + */ public void execute() throws DukeException { String[] splitString = input.split(" ", 2); if (splitString.length < 2) { diff --git a/src/main/java/duke/command/InvalidCommand.java b/src/main/java/duke/command/InvalidCommand.java index 96259fb46..ec334b033 100644 --- a/src/main/java/duke/command/InvalidCommand.java +++ b/src/main/java/duke/command/InvalidCommand.java @@ -2,8 +2,16 @@ import main.java.duke.ui.Ui; +/** + * Class for InvalidCommand. It is called when the user inputs an invalid command. + */ + public class InvalidCommand extends Command { + /** + * Method to carry out the command. It calls Ui to print out a message to let the user know + * the command is invalid. + */ public void execute() { Ui.printInvalid(); } diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java index 8a1e45b31..2deeb40b9 100644 --- a/src/main/java/duke/command/ListCommand.java +++ b/src/main/java/duke/command/ListCommand.java @@ -2,12 +2,15 @@ import main.java.duke.ui.Ui; -public class ListCommand extends Command { - - public ListCommand() { +/** + * Class for ListCommand. It is created when the user requests for the task list. + */ - } +public class ListCommand extends Command { + /** + * Method to carry out the command. It calls Ui to print out the tasks in the list. + */ public void execute() { Ui.printList(); } diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java index eea1d334b..bd04b740c 100644 --- a/src/main/java/duke/command/MarkCommand.java +++ b/src/main/java/duke/command/MarkCommand.java @@ -4,14 +4,27 @@ import main.java.duke.Duke; import main.java.duke.ui.Ui; +/** + * Class for MarkCommand. It is created when the user requests to mark a task. + */ + public class MarkCommand extends Command { private final int markInt; + /** + * Constructor for MarkCommand. + * + * @param markInt The task of number markInt in the task list that the user wants to mark. + */ public MarkCommand(int markInt) { this.markInt = markInt; } + /** + * Method to carry out the command. It marks the task and calls Ui let the user know the + * task has been marked. + */ public void execute() { Task task = Duke.tasks.get(markInt - 1); task.setDone(true); diff --git a/src/main/java/duke/command/ToDoCommand.java b/src/main/java/duke/command/ToDoCommand.java index 936e72271..af2c90fc3 100644 --- a/src/main/java/duke/command/ToDoCommand.java +++ b/src/main/java/duke/command/ToDoCommand.java @@ -4,14 +4,27 @@ import main.java.duke.Duke; import main.java.duke.ui.Ui; +/** + * Class for ToDoCommand. It is created when the user requests to create a new ToDo. + */ + public class ToDoCommand extends Command { private String input; + /** + * Constructor for ToDoCommand. + * + * @param input User input. + */ public ToDoCommand(String input) { this.input = input; } + /** + * Method to carry out command. It creates a ToDo which is added onto the list, and calls + * Ui to print that the ToDo has been added onto the task list. + */ public void execute() { ToDo task = new ToDo(this.input); Duke.tasks.add(task); diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java index 678fd1230..f9af1c7b5 100644 --- a/src/main/java/duke/command/UnmarkCommand.java +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -4,14 +4,27 @@ import main.java.duke.Duke; import main.java.duke.ui.Ui; +/** + * Class for UnmarkCommand. It is created when the user requests to unmark a task. + */ + public class UnmarkCommand extends Command { private final int markInt; + /** + * Constructor for UnmarkCommand. + * + * @param markInt The task of number markInt in the task list that the user wants to unmark. + */ public UnmarkCommand(int markInt) { this.markInt = markInt; } + /** + * Method to carry out the command. It unmarks the task and calls Ui let the user know the + * task has been unmarked. + */ public void execute() { Task task = Duke.tasks.get(markInt - 1); task.setDone(false); diff --git a/src/main/java/duke/command/commandsCommand.java b/src/main/java/duke/command/commandsCommand.java index 1f4075944..3654eec06 100644 --- a/src/main/java/duke/command/commandsCommand.java +++ b/src/main/java/duke/command/commandsCommand.java @@ -2,12 +2,15 @@ import main.java.duke.ui.Ui; -public class commandsCommand extends Command { +/** + * Class for CommandsCommand. It is created when a user request for the command list. + */ - public commandsCommand() { - - } +public class CommandsCommand extends Command { + /** + * Method to carry out the command. Calls Ui to print out all valid commands into a list. + */ public void execute() { Ui.printCommand(); } diff --git a/src/main/java/duke/exception/DukeException.java b/src/main/java/duke/exception/DukeException.java index 7a3a33274..258b1e652 100644 --- a/src/main/java/duke/exception/DukeException.java +++ b/src/main/java/duke/exception/DukeException.java @@ -1,13 +1,28 @@ package main.java.duke.exception; +/** + * Class for the DukeException. It is the exception that handles almost all errors raised by + * invalid inputs from the user. + */ + public class DukeException extends Exception { private final String error; + /** + * Constructor for DukeException. + * + * @param error String error message + */ public DukeException(String error) { this.error = error; } + /** + * Method to get the error message. + * + * @return The error message. + */ public String getMessage() { return this.error; } diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index f8201d8fd..8aceac290 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -12,11 +12,15 @@ import main.java.duke.command.DeadlineCommand; import main.java.duke.command.EventCommand; import main.java.duke.command.InvalidCommand; -import main.java.duke.command.commandsCommand; +import main.java.duke.command.CommandsCommand; import main.java.duke.command.CheckDateCommand; import main.java.duke.command.ByeCommand; import main.java.duke.command.FindCommand; +/** + * Class for the Parser object. It is the class used to parse user inputs and calls Commands + * from the input. + */ public class Parser { private final String LIST = "list"; @@ -31,6 +35,13 @@ public class Parser { private final String BYE = "bye"; private final String FIND = "find"; + /** + * Main method that takes in the initial user input and decides how to deal with it. + * + * @param input The user input. + * @return Calls another method to handle the input. + * @throws DukeException If the user input is invalid. + */ public Command parse(String input) throws DukeException { String lowerCaseInput = input.toLowerCase(); if (lowerCaseInput.startsWith(LIST)) { @@ -80,6 +91,12 @@ public Command parse(String input) throws DukeException { } } + /** + * Method to check if a String is a number + * + * @param string String that is being checked. + * @return Boolean value whether it is a number. + */ private boolean isNum(String string) { try { Integer.parseInt(string); @@ -89,10 +106,21 @@ private boolean isNum(String string) { return true; } + /** + * Method to parse user input asking for the task list. + * @return ListCommand which will be executed. + */ private Command parseList() { return new ListCommand(); } + /** + * Method to parse user input asking to unmark a specific task. + * + * @param input User input. + * @return UnmarkCommand to be executed. + * @throws DukeException If input is invalid. + */ private Command parseUnmark(String input) throws DukeException { String[] splitString = input.split(" ", 2); if (splitString.length < 2) { @@ -117,6 +145,13 @@ private Command parseUnmark(String input) throws DukeException { } } + /** + * Method to parse user input asking to mark a specific task. + * + * @param input User input. + * @return MarkCommand to be executed. + * @throws DukeException If input is invalid. + */ private Command parseMark(String input) throws DukeException { String[] splitString = input.split(" ", 2); if (splitString.length < 2) { @@ -141,6 +176,13 @@ private Command parseMark(String input) throws DukeException { } } + /** + * Method to parse user input asking to delete a specific task. + * + * @param input User input. + * @return DeleteCommand to be executed. + * @throws DukeException If input is invalid. + */ private Command parseDelete(String input) throws DukeException { String[] splitString = input.split(" ", 2); if (splitString.length < 2) { @@ -161,6 +203,13 @@ private Command parseDelete(String input) throws DukeException { } } + /** + * Method to parse user input requesting to create a new ToDo. + * + * @param input User input. + * @return ToDoCommand to be executed. + * @throws DukeException If input is invalid. + */ private Command parseToDo(String input) throws DukeException { String[] splitString = input.split(" ", 2); if (splitString.length < 2) { @@ -172,6 +221,13 @@ private Command parseToDo(String input) throws DukeException { } } + /** + * Method to parse user input requesting to create a new Deadline. + * + * @param input User input. + * @return DeadlineCommand to be executed. + * @throws DukeException If input is invalid. + */ private Command parseDeadline(String input) throws DukeException { String[] splitString = input.split(" ", 2); if (splitString.length < 2) { @@ -183,6 +239,13 @@ private Command parseDeadline(String input) throws DukeException { } } + /** + * Method to parse user input requesting to create a new Event. + * + * @param input User input. + * @return EventCommand to be executed. + * @throws DukeException If input is invalid. + */ private Command parseEvent(String input) throws DukeException { String[] splitString = input.split(" ", 2); if (splitString.length < 2) { @@ -194,6 +257,13 @@ private Command parseEvent(String input) throws DukeException { } } + /** + * Method to parse user input requesting to check if a specific date has any task. + * + * @param input User input. + * @return CheckDateCommand to be executed. + * @throws DukeException If input is invalid. + */ private Command parseCheckDate(String input) throws DukeException { String[] splitString = input.split(" ", 3); if (splitString.length < 3) { @@ -205,8 +275,13 @@ private Command parseCheckDate(String input) throws DukeException { } } + /** + * Method to parse user input requesting to see the command list. + * + * @return commandsCommand to be executed. + */ private Command parseCommands() { - return new commandsCommand(); + return new CommandsCommand(); } } \ No newline at end of file diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index 54dd1d912..83a8feebc 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -14,12 +14,25 @@ import main.java.duke.Duke; import main.java.duke.exception.DukeException; +/** + * Class for the Storage object. It contains methods to read from a data file, and to write onto + * it after Duke has ended. + * + * @throws IOException If an input or output exception occured. + */ + public class Storage { private static String DATA_DIRECTORY = System.getProperty("user.dir") + "/data"; private static String DATA_FILE = DATA_DIRECTORY + "/duke.txt"; private static Path dataDirectoryPath = Paths.get(DATA_DIRECTORY); + /** + * Method used to initiate read from data/duke.txt, and to retrieve when Duke is started. + * + * @throws IOException If an input or output exception occured. + * @throws DukeException If data file is corrupted or contains unexpected information. + */ public static void load() throws IOException, DukeException { boolean directoryExists = new File(DATA_DIRECTORY).exists(); boolean fileExists = new File(DATA_FILE).exists(); @@ -32,12 +45,18 @@ public static void load() throws IOException, DukeException { } Scanner s = new Scanner(dataFile); while (s.hasNext()) { - parseData(s.nextLine()); + readData(s.nextLine()); } Duke.taskCounter = Duke.tasks.size(); } - private static void parseData(String data) throws DukeException { + /** + * Method that reads lines in duke.txt and adds them to the task list. + * + * @param data Input line from duke.txt. + * @throws DukeException If task type is of an unexpected value. + */ + private static void readData(String data) throws DukeException { String[] dataArr = data.split("\\|"); Task newTask; if (dataArr[1].equals("T")) { @@ -61,6 +80,11 @@ private static void parseData(String data) throws DukeException { Duke.tasks.add(newTask); } + /** + * Method that is used to initiate the writing to data/duke.txt before closing Duke. + * + * @throws IOException If an input or output exception occured. + */ public static void writeToFile() throws IOException { FileWriter fw = new FileWriter(DATA_FILE, false); int taskNum = 0; @@ -71,7 +95,13 @@ public static void writeToFile() throws IOException { fw.close(); } - // num|type|T/F|description|(date) + /** + * Method that converts a task into a String that can be readable for future use. + * + * @param taskNum The number of the task on the list. + * @param task The task that is being converted. + * @return A string which is written onto data/duke.txt. + */ private static String convertTask(int taskNum, Task task) { String line = String.valueOf(taskNum) + "|" + task.getType() + "|"; if (task.isDone()) { diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 781382545..16fbf6461 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -3,12 +3,23 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; +/** + * Class for the Deadline object. Deadlines are Tasks that do not have a specific due date. + */ + public class Deadline extends Task { private final String date; private final String time; private final LocalDate localDate; + /** + * Constructor for Deadline object. + * + * @param description String describing the task. + * @param date String date in YYYY-MM-DD that the task is due by . + * @param time String time in 24-hour format that the task is due by. + */ public Deadline(String description, String date, String time) { super(description); this.date = date; @@ -16,22 +27,48 @@ public Deadline(String description, String date, String time) { this.localDate = LocalDate.parse(date); } + /** + * Method used to show that the Task is a Deadline. + * + * @return A String "D". + */ public String getType() { return "D"; } + /** + * Method used to retrieve the date the deadline is due. + * + * @return LocalDate of the due date. + */ public LocalDate getStartDate() { return this.localDate; } + /** + * Duplicate method used to retrieve the date the deadline is due. + * + * @return LocalDate of the due date. + */ public LocalDate getEndDate() { return this.localDate; } + /** + * Formats the timing of the event into date|time for saving. + * + * @return String in the format for writing to file. + */ public String getDateTime() { return this.date + "|" + this.time; } + /** + * Converts the parameters of the Deadline into a readable line with it's description + * and due date and time. + * + * @return String that contains the information of the Deadline. + */ @Override public String toString() { String dateTime = this.localDate. diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 03e37b60d..e19c8eb40 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -3,6 +3,10 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; +/** + * Class for the Event object. An event is a task that has a start date and an end date. + */ + public class Event extends Task { private final String startDate; @@ -12,6 +16,15 @@ public class Event extends Task { private final LocalDate localStartDate; private final LocalDate localEndDate; + /** + * Constructor for the Event class. + * + * @param description String describing the event. + * @param startDate String of the date it starts on in YYYY-MM-DD. + * @param startTime String of the time it starts in 24-hour format. + * @param endDate String of the date it ends on in YYYY-MM-DD. + * @param endTime String of the time it ends in 24-hour format. + */ public Event(String description, String startDate, String startTime, String endDate, String endTime) { super(description); @@ -23,23 +36,49 @@ public Event(String description, String startDate, String startTime, this.localEndDate = LocalDate.parse(endDate); } + /** + * Method used to show that the Task is an Event. + * + * @return A String "E" + */ public String getType() { return "E"; } + /** + * Method used to retrieve the start date of the event. + * + * @return LocalDate of the start date. + */ public LocalDate getStartDate() { return this.localStartDate; } + /** + * Method used to retrieve the end date of the event. + * + * @return LocalDate of the end date. + */ public LocalDate getEndDate() { return this.localEndDate; } + /** + * Formats the timing of the event into start date|start time|end date|end time for saving. + * + * @return String in the format for writing to file. + */ public String getDateTime() { return String.format("%s|%s|%s|%s", this.startDate, this.startTime, this.endDate, this.endTime); } + /** + * Converts the parameters of the Event into a readable line with it's description, + * start and end dates and times. + * + * @return String that contains the information of the Event. + */ @Override public String toString() { String startDateTime = this.localStartDate. diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 30a70837c..cde309aa6 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -2,29 +2,58 @@ import java.time.LocalDate; +/** + * Abstract class for ToDos, Deadlines, and Events. It contains basic methods for the + * 3 sub-classes. + */ public abstract class Task { public String description; public Boolean isDone = false; public String mark = " "; + /** + * Constructor for the Task object. + * + * @param description String of the description of the task. + */ public Task(String description) { this.description = description; } + /** + * Marks the task as done or not done. + * + * @param isDone Boolean value that marks the task as done or not. + */ public void setDone(Boolean isDone) { this.isDone = isDone; } + /** + * Method to return the description of the task. + * + * @return String desciption of the task. + */ public String getDescription() { return this.description; } + /** + * Method to find out if the task is done. + * + * @return Boolean value whether the task is done. + */ public boolean isDone() { return this.isDone; } - public String markIsDone() { + /** + * Method to show whether a task is done with an "X", or " " if not done. + * + * @return A String "X" or " " depending on whether the task is done. + */ + public String getMark() { if (this.isDone()) { this.mark = "X"; } else { @@ -33,16 +62,43 @@ public String markIsDone() { return this.mark; } + /** + * Abstract method to find out the type of task. + * + * @return String "T" for ToDos, String "D" for Deadlines, String "E" for Events. + */ public abstract String getType(); + /** + * Abstract method to get the starting date of a task. + * + * @return A LocalDate of the starting date of a task. + */ public abstract LocalDate getStartDate(); + /** + * Abstract method to get the ending date of a task. + * + * @return A LocalDate of the ending date of a task. + */ public abstract LocalDate getEndDate(); + /** + * Abstract method to get the date and time of a task in the format for writing onto + * the save file. + * + * @return A String containing the dates and times of the task. + */ public abstract String getDateTime(); + /** + * The method to represent the Task object as a String + * + * @return A partially complete String that shows whether the task is marked as done, and + * it's description. + */ @Override public String toString() { - return String.format("[%s] %s", this.markIsDone(), this.description); + return String.format("[%s] %s", this.getMark(), this.description); } } \ No newline at end of file diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java index 03211fd20..18e494a05 100644 --- a/src/main/java/duke/task/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -3,28 +3,56 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; +/** + * Class for the ToDo object. ToDos are Tasks that do not have any specific due date. + */ + public class ToDo extends Task { + /** + * Constructor for the ToDo object. + * + * @param description A String describing the task. + */ public ToDo(String description) { super(description); } + /** + * Method used to show that the Task is an ToDo. + * + * @return A String "T". + */ public String getType() { return "T"; } + /** + * Un-used method from Task + */ public LocalDate getStartDate() { return null; } + /** + * Un-used method from Task + */ public LocalDate getEndDate() { return null; } + /** + * Un-used method from Task + */ public String getDateTime() { return null; } + /** + * The method to represent the ToDo object as a String + * + * @return String that contains the information of the ToDo + */ @Override public String toString() { return String.format("[T]%s", super.toString()); diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 4b4f17b55..6025a4e1b 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -1,7 +1,8 @@ package main.java.duke.ui; /** - * The Ui class handles all print statements + * The Ui class handles all print statements. It contains all methods for printing + * the result of various commands. */ import java.util.ArrayList; @@ -16,6 +17,13 @@ public class Ui { private final static String HORIZONTAL_LINE = "____________________________________" + "________________________"; + /** + * This method is the main method used format outputs such that they appear within 2 + * horizontal lines. + * + * @param args A vararg that takes in Strings that are to be printed on a new horizontal line + * between the 2 horizontal lines. + */ public static void printFormat(String... args) { System.out.println(HORIZONTAL_LINE); for (String arg : args) { @@ -24,6 +32,9 @@ public static void printFormat(String... args) { System.out.println(HORIZONTAL_LINE); } + /** + * Prints out the DUKE ASCII art, and welcomes the user. + */ public static void printIntro() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -36,6 +47,9 @@ public static void printIntro() { "type 'commands' for the list of commands"); } + /** + * Prints out all tasks description (and date) and numbers them. + */ public static void printList() { System.out.println(HORIZONTAL_LINE); System.out.println("Here are the tasks in your list:"); @@ -45,6 +59,11 @@ public static void printList() { System.out.println(HORIZONTAL_LINE); } + /** + * Searches for task description based on a keyword, and prints out the list of those tasks. + * + * @param keyword A String that is used to search in the description of the tasks. + */ public static void printFind(String keyword) { Boolean foundTask = false; System.out.println(HORIZONTAL_LINE); @@ -62,27 +81,54 @@ public static void printFind(String keyword) { System.out.println(HORIZONTAL_LINE); } + /** + * Prints a sentence that lets the user know that the task has been unmarked. + * + * @param markInt Integer number of the task in the list. + * @param task The task that has been unmarked. + */ public static void printUnmark(int markInt, Task task) { printFormat("OK, I've marked this task as not done yet:", task.toString()); } + /** + * Prints a sentence that lets the user know that the task has been marked. + * + * @param markInt Integer number of the task in the list. + * @param task The task that has been marked. + */ public static void printMark(int markInt, Task task) { printFormat("Nice! I've marked this task as done:", task.toString()); } - + + /** + * Prints a sentence that lets the user know the task has been deleted, and lets the + * user know how many tasks are left in the task list. + * + * @param task Task that has been removed from the list. + */ public static void printDelete(Task task) { printFormat("Noted. I've removed this task:", task.toString(), "Now you have " + String.valueOf(Duke.taskCounter) + " tasks in the list."); } + /** + * Method used to let the user know that the task has been added onto the list. It also shows + * the total number of tasks in the list. + * + * @param task Task that had been added into the list. + */ public static void printTask(Task task) { printFormat("Got it. I've added this task:", " " + task.toString(), "Now you have " + String.valueOf(Duke.taskCounter) + " tasks in the list."); } + /** + * Prints out possible commands the user can use. + */ public static void printCommand() { printFormat("List of valid commands:", "'list' - lists out all tasks and its details", @@ -106,6 +152,11 @@ public static void printCommand() { "adds a task with a time range"); } + /** + * Prints out tasks that are due or happening on a specific date. + * + * @param localDate The date which the user wants to know if there is anything happening. + */ public static void printCheckDate(LocalDate localDate) { System.out.println(HORIZONTAL_LINE); System.out.println("Here are the tasks on " + @@ -119,6 +170,13 @@ public static void printCheckDate(LocalDate localDate) { System.out.println(HORIZONTAL_LINE); } + /** + * Method used to check if a task is due or happening on a specific date. + * + * @param task The task that is being checked. + * @param localDate The date that the task is being checked on. + * @return A boolean value whether the task is happening on the date. + */ private static boolean isOnDate(Task task, LocalDate localDate) { if (task.getType().equals("D")) { if (localDate.equals(task.getStartDate())) { @@ -133,15 +191,27 @@ private static boolean isOnDate(Task task, LocalDate localDate) { return false; } + /** + * Prints out a goodbye before shutting the bot down. + */ public static void printBye() { printFormat("Bye. Hope to see you again soon!"); } + /** + * Prints out a message to let the user know his command was invalid, and suggests the user + * to use the 'command' command if the user is unsure of commands availiable. + */ public static void printInvalid() { printFormat("OOPS!!! I'm sorry, but I don't know what that means :-(", "Please type in 'commands' if you are not sure of the commands"); } + /** + * Method used to print messages from Exceptions. Usually indicates what the user did wrong. + * + * @param e A DukeException that was thrown during processing of user input. + */ public static void printError(DukeException e) { System.out.println(e.getMessage()); } From 13225326acd78ff685201110e1cce0913a964678 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 16:25:16 +0800 Subject: [PATCH 19/23] Added User Guide --- docs/README.md | 88 ++++++++++++++++++++++++++++++----- src/main/java/duke/ui/Ui.java | 36 +++++++------- 2 files changed, 96 insertions(+), 28 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..d6c64fb86 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,93 @@ # User Guide +Duke is a chat-bot that manages your tasks. It is accessed via a Command Line Interface (CLI). -## Features +#Quick start +1. Ensure that you have **Java 11** installed on your system. +2. Download the latest [ip.jar](https://github.com/cheshire-doge/ip) +3. Open a **Command Line** and change your directory to the folder containing **ip.jar** +4. Enter `java -jar ip.jar` to start Duke +5. Refer to **Usage** below or enter `commands` into the Command Line to view commands. -### Feature-ABC +## Features -Description of the feature. +### Storing of Tasks `ToDo`, `Deadline`, `Event` -### Feature-XYZ +**ToDos** are tasks that do not have a date they have to be accomplished by. -Description of the feature. +**Deadlines** are tasks that have a deadline they have to be accomplished by. + +**Events** are tasks that run for a period of time, with a starting time and ending time. + +### Mark/Unmark Tasks + +Mark or Unmark tasks to remind you if you have completed them. + +### Find Tasks + +Search for tasks using specific keywords. + +### Other Features! + +**Delete**, **Check Date**, **List** ## Usage -### `Keyword` - Describe action +### `todo` - Creates a ToDo + +Example: + +`todo DESCRIPTION` - Adds a ToDo with DESCRIPTION onto the task list + +``` +todo Groceries +____________________________________________________________ +Got it. I've added this task: + [T][ ] Groceries +Now you have 1 tasks in the list. +``` -Describe the action and its outcome. +### `deadline` - Creates a Deadline -Example of usage: +`deadline DESCRIPTION /by DD/MM/YYYY time` - Adds a deadline with DESCRIPTION and deadline -`keyword (optional arguments)` +``` +deadline iP /by 03/03/2022 2359 +____________________________________________________________ +Got it. I've added this task: + [D][ ] iP (by: Mar 3 2022 2359) +Now you have 6 tasks in the list. +``` -Expected outcome: +### `event` - Creates an Event -Description of the outcome. +`event DESCRIPTION /at DD/MM/YYYY time /to DD/MM/YYYY time` - Adds an event with DESCRIPTION, start time and end time. ``` -expected output +event Project Meeting /at 05/03/2022 2000 /to 05/03/2022 2200 +____________________________________________________________ +Got it. I've added this task: + [E][ ] Project Meeting (at: Mar 5 2022 2000 to Mar 5 2022 2200) +Now you have 3 tasks in the list. ``` + +### `list` - Lists out all tasks + +`list` - All tasks will be listed down + +### `mark`/`unmark` `NUMBER` - marks/unmarks task NUMBER in the list + +`mark 3` - Task 3 in the list would be marked + +### `find KEYWORD` - searches the list based on KEYWORD + +`find project` - Lists down every Task in the list containing `project` + +### `check date DD/MM/YYYY` - checks the list if any task is due or happens on that day +`check date 04/03/2021` - Checks if any task happens or is due on 04/03/2021 + +### `delete NUMBER` - deletes task NUMBER in the list +`delete 1` - Deletes the first task on the list + +### `bye` - closes the bot + +### `commands` - lists down all commands \ No newline at end of file diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 6025a4e1b..d37b42e5f 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -70,7 +70,7 @@ public static void printFind(String keyword) { System.out.println("Here are the matching tasks in your list:"); for (int i = 0; i < Duke.tasks.size(); i++) { Task task = Duke.tasks.get(i); - if (task.toString().contains(keyword)) { + if (task.toString().toLowerCase().contains(keyword.toLowerCase())) { foundTask = true; System.out.println(String.valueOf(i + 1) + "." + task.toString()); } @@ -132,24 +132,28 @@ public static void printTask(Task task) { public static void printCommand() { printFormat("List of valid commands:", "'list' - lists out all tasks and its details", - "'mark X' - marks item X on the list as done", - " (X is a number) e.g. 'mark 3' marks item 3 on the list", - "'unmark X' - unmarks item X on the list as done", - " (X is a number) e.g. 'unmark 3' unmarks item 3 on the list", - "'delete X' - deletes item X on the list", - " (X is a number) e.g. 'delete 3' deletes item 3 on the list", - "'find X - lists items in the list that contain the keyword X", - " (X is a string) e.g. 'find book' lists items in the list that contain 'book'", - "'todo X' - ToDos are tasks without specific deadlines", - " (X is a string) e.g. 'todo buy shampoo' adds the task 'buy shampoo' to the list", - "'deadline X /by DD/MM/YYYY TIME' - Deadlines are tasks that need to" + - " be done before a specific date and time in 24-hour format", + "'mark NUMBER' - marks item NUMBER on the list as done", + " e.g. 'mark 3' marks item 3 on the list", + "'unmark NUMBER' - unmarks item NUMBER on the list as done", + " e.g. 'unmark 3' unmarks item 3 on the list", + "'delete NUMBER' - deletes item NUMBER on the list", + " e.g. 'delete 3' deletes item 3 on the list", + "'find KEYWORD' - lists items in the list that contain KEYWORD", + " e.g. 'find book' lists items in the list that contain 'book'", + "'todo DESC' - ToDos are tasks without specific deadlines", + " e.g. 'todo buy shampoo' adds the task 'buy shampoo' to the list", + "'deadline DESC /by DD/MM/YYYY TIME' - Deadlines are tasks that need to" + + "be done before a specific date and time in 24-hour format", " e.g. 'deadline math homework /by 2/3/2022 1400' adds a " + "task with deadline Mar 2 2022 1400 to the list", - "'event X /at DD/MM/YYYY TIME /to DD/MM/YYYY TIME' - Events are tasks that start" + - " at a specific date and time and ends at a specific date and time", + "'event DESC /at DD/MM/YYYY TIME /to DD/MM/YYYY TIME' - Events are tasks that" + + "start at a specific date and time and ends at a specific date and time", " e.g. 'event project meeting /at 3/3/2022 0900' /to 4/3/2022 1800 " + - "adds a task with a time range"); + "adds a task with a time range", + "'check date DD/MM/YYYY' - checks if there are tasks due or occuring" + + " on that date", + " e.g. 'check date 04/03/2022' - checks if anything is on 04/03/2022", + "'bye' - saves changes and closes Duke"); } /** From 3c2c3b1c02859f459fc9fbdca2f5375cbcaab932 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 18:47:59 +0800 Subject: [PATCH 20/23] Edits to text-ui-test --- data/duke.txt | 5 +-- text-ui-test/EXPECTED.TXT | 91 +++++++++++++++++++++++++++++---------- text-ui-test/input.txt | 22 ++++++++-- text-ui-test/runtest.bat | 5 ++- 4 files changed, 91 insertions(+), 32 deletions(-) diff --git a/data/duke.txt b/data/duke.txt index 00f16e71f..84066e73c 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,3 +1,2 @@ -1|E|0|eventtest|2045-03-02|2pm|2012-05-04|4pm -2|D|0|test deadline|2043-05-06|1am -3|T|0|test +1|T|0|read book +2|D|1|return book|2022-06-06|1700 diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 8b49a913e..07420f524 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -4,11 +4,6 @@ Hello from | | | | | | | |/ / _ \ | |_| | |_| | < __/ |____/ \__,_|_|\_\___| - -Hello! I'm Duke -What can I do for you? -type 'commands' for the list of commands -____________________________________________________________ ____________________________________________________________ Got it. I've added this task: [T][ ] read book @@ -16,17 +11,19 @@ Now you have 1 tasks in the list. ____________________________________________________________ ____________________________________________________________ Got it. I've added this task: - [D][ ] return book (by: June 6th) + [D][ ] return book (by: Jun 6 2022 1700) Now you have 2 tasks in the list. ____________________________________________________________ ____________________________________________________________ -OOPS!!! I'm sorry, but I don't know what that means :-( -Please type in 'commands' if you are not sure of the commands +Got it. I've added this task: + [E][ ] project meeting (at: Mar 12 2021 0600 to Mar 13 2022 0700) +Now you have 3 tasks in the list. ____________________________________________________________ ____________________________________________________________ Here are the tasks in your list: 1.[T][ ] read book -2.[D][ ] return book (by: June 6th) +2.[D][ ] return book (by: Jun 6 2022 1700) +3.[E][ ] project meeting (at: Mar 12 2021 0600 to Mar 13 2022 0700) ____________________________________________________________ ____________________________________________________________ Nice! I've marked this task as done: @@ -34,39 +31,87 @@ Nice! I've marked this task as done: ____________________________________________________________ ____________________________________________________________ Nice! I've marked this task as done: -[D][X] return book (by: June 6th) +[D][X] return book (by: Jun 6 2022 1700) ____________________________________________________________ ____________________________________________________________ OK, I've marked this task as not done yet: [T][ ] read book ____________________________________________________________ +Oh no! The time you entered is invalid! +The format for time is in 24-hour format! e.g. 1800 +Oh no! You have typed an invalid date! +The format for date is DD/MM/YYYY !, e.g. 15/02/2022 ____________________________________________________________ -Got it. I've added this task: - [D][ ] return book (by: Sunday) -Now you have 3 tasks in the list. +Here are the tasks in your list: +1.[T][ ] read book +2.[D][X] return book (by: Jun 6 2022 1700) +3.[E][ ] project meeting (at: Mar 12 2021 0600 to Mar 13 2022 0700) ____________________________________________________________ +Oh no! You need to enter the description of the task! ____________________________________________________________ -Got it. I've added this task: - [E][ ] project meeting (at: Mon 2-4pm) -Now you have 4 tasks in the list. +OOPS!!! I'm sorry, but I don't know what that means :-( +Please type in 'commands' if you are not sure of the commands ____________________________________________________________ +Oh no! The task you have selected is already been marked ____________________________________________________________ -Here are the tasks in your list: +Here are the matching tasks in your list: 1.[T][ ] read book -2.[D][X] return book (by: June 6th) -3.[D][ ] return book (by: Sunday) -4.[E][ ] project meeting (at: Mon 2-4pm) +2.[D][X] return book (by: Jun 6 2022 1700) +____________________________________________________________ +____________________________________________________________ +Noted. I've removed this task: +[E][ ] project meeting (at: Mar 12 2021 0600 to Mar 13 2022 0700) +Now you have 2 tasks in the list. ____________________________________________________________ +Oh no! The number you have chosen is not valid! ____________________________________________________________ -Oh no! You have to include the details of the task 'todo' +Here are the tasks on Jun 6 2022: +2.[D][X] return book (by: Jun 6 2022 1700) ____________________________________________________________ ____________________________________________________________ +Here are the tasks on Jun 7 2022: +____________________________________________________________ +Oh no! You need to choose a number! +____________________________________________________________ OOPS!!! I'm sorry, but I don't know what that means :-( Please type in 'commands' if you are not sure of the commands ____________________________________________________________ +Oh no! You need to choose a number! +____________________________________________________________ +OOPS!!! I'm sorry, but I don't know what that means :-( +Please type in 'commands' if you are not sure of the commands ____________________________________________________________ -Oh no! The item is already marked! ____________________________________________________________ +Here are the tasks in your list: +1.[T][ ] read book +2.[D][X] return book (by: Jun 6 2022 1700) +____________________________________________________________ +____________________________________________________________ +List of valid commands: +'list' - lists out all tasks and its details +'mark NUMBER' - marks item NUMBER on the list as done + e.g. 'mark 3' marks item 3 on the list +'unmark NUMBER' - unmarks item NUMBER on the list as done + e.g. 'unmark 3' unmarks item 3 on the list +'delete NUMBER' - deletes item NUMBER on the list + e.g. 'delete 3' deletes item 3 on the list +'find KEYWORD' - lists items in the list that contain KEYWORD + e.g. 'find book' lists items in the list that contain 'book' +'todo DESC' - ToDos are tasks without specific deadlines + e.g. 'todo buy shampoo' adds the task 'buy shampoo' to the list +'deadline DESC /by DD/MM/YYYY TIME' - Deadlines are tasks that need tobe done before a specific date and time in 24-hour format + e.g. 'deadline math homework /by 2/3/2022 1400' adds a task with deadline Mar 2 2022 1400 to the list +'event DESC /at DD/MM/YYYY TIME /to DD/MM/YYYY TIME' - Events are tasks thatstart at a specific date and time and ends at a specific date and time + e.g. 'event project meeting /at 3/3/2022 0900' /to 4/3/2022 1800 adds a task with a time range +'check date DD/MM/YYYY' - checks if there are tasks due or occuring on that date + e.g. 'check date 04/03/2022' - checks if anything is on 04/03/2022 +'bye' - saves changes and closes Duke +____________________________________________________________ +Oh no! You need to include a date after '/by'! +Oh no! The time you entered is invalid! +The format for time is in 24-hour format! e.g. 1800 ____________________________________________________________ Bye. Hope to see you again soon! -____________________________________________________________ \ No newline at end of file +____________________________________________________________ + +Process finished with exit code 0 \ No newline at end of file diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 10af9508d..5c851f61a 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -1,14 +1,28 @@ todo read book -deadline return book /by June 6th -Event project meeting /at Aug 6th 2-4pm +deadline return book /by 06/06/2022 1700 +Event project meeting /at 12/03/2021 0600 /to 13/03/2022 0700 list mark 1 mark 2 unmark 1 -deadline return book /by Sunday -event project meeting /at Mon 2-4pm +deadline return book /by 03/04/2023 2401 +event project meeting /at 04/13/0000 1234 /to 01/12/3214 list todo test invalid command mark 2 +find book +delete 3 +delete 6 +check date 06/06/2022 +check date 07/06/2022 +mark test +test +delete test +desfasda +list2131 +commands +deadline /by 04/06/22 1543 +event testttt /at 06/08/23 1567 /to 06/04/24 1322 +bye diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 2c0921c3c..832026065 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -5,15 +5,16 @@ if not exist ..\bin mkdir ..\bin REM delete output from previous run if exist ACTUAL.TXT del ACTUAL.TXT - +Pause REM compile the code into the bin folder javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** + Pause exit /b 1 ) REM no error here, errorlevel == 0 - +Pause REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT java -classpath ..\bin Duke < input.txt > ACTUAL.TXT From 280fb24c9a8c20ed4886e0f053758a18874bccd2 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 18:49:12 +0800 Subject: [PATCH 21/23] Edits to README --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index d6c64fb86..b70071238 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,7 +1,7 @@ # User Guide Duke is a chat-bot that manages your tasks. It is accessed via a Command Line Interface (CLI). -#Quick start +##Quick start 1. Ensure that you have **Java 11** installed on your system. 2. Download the latest [ip.jar](https://github.com/cheshire-doge/ip) 3. Open a **Command Line** and change your directory to the folder containing **ip.jar** From f55cc7bd3750027ab0984dc6f23a54498640cfab Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 18:52:50 +0800 Subject: [PATCH 22/23] Updated README ip.jar link --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index b70071238..e3d56258b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3,7 +3,7 @@ Duke is a chat-bot that manages your tasks. It is accessed via a Command Line In ##Quick start 1. Ensure that you have **Java 11** installed on your system. -2. Download the latest [ip.jar](https://github.com/cheshire-doge/ip) +2. Download the latest [ip.jar](https://github.com/cheshire-doge/ip/releases/tag/A-Release) 3. Open a **Command Line** and change your directory to the folder containing **ip.jar** 4. Enter `java -jar ip.jar` to start Duke 5. Refer to **Usage** below or enter `commands` into the Command Line to view commands. From 2e5b0a17f8bcaf24d9f1a8e81ec42251da5f5e99 Mon Sep 17 00:00:00 2001 From: "LAPTOP-ABRSSH4C\\clare" Date: Fri, 4 Mar 2022 23:58:07 +0800 Subject: [PATCH 23/23] Changes to Find command --- src/main/java/META-INF/MANIFEST.MF | 2 +- src/main/java/duke/ui/Ui.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF index 9f37e4e0a..8f308fd8e 100644 --- a/src/main/java/META-INF/MANIFEST.MF +++ b/src/main/java/META-INF/MANIFEST.MF @@ -1,3 +1,3 @@ Manifest-Version: 1.0 -Main-Class: Duke +Main-Class: main.java.duke.Duke diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index d37b42e5f..77c449bc7 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -70,7 +70,7 @@ public static void printFind(String keyword) { System.out.println("Here are the matching tasks in your list:"); for (int i = 0; i < Duke.tasks.size(); i++) { Task task = Duke.tasks.get(i); - if (task.toString().toLowerCase().contains(keyword.toLowerCase())) { + if (task.getDescription().toLowerCase().contains(keyword.toLowerCase())) { foundTask = true; System.out.println(String.valueOf(i + 1) + "." + task.toString()); }