From de8ed4d3acb71266dbfc40181b2df48d504ca29a Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Wed, 25 Jan 2023 01:24:15 +0800 Subject: [PATCH 01/21] Achieved Level 0 --- src/main/java/Duke.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..eed4c29e8 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -4,7 +4,13 @@ public static void main(String[] args) { + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; + + "|____/ \\__,_|_|\\_\\___|\n" + + "__________________________\n"; System.out.println("Hello from\n" + logo); + System.out.println("Hello! Do you need anything from me?\n" + + "I have only been trained to greet you so far.\n" + + "Once my owner is more proficient in what he does, he will give me more functions!"); + //To be added to make sure user input is read. + System.out.println("Thats all from me! Goodbye!"); } } From 5d5cc71c484865a88eac1d09a9d7ffd92f9aeffe Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Wed, 25 Jan 2023 22:41:32 +0800 Subject: [PATCH 02/21] Minor Change in spelling --- src/main/java/Duke.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index eed4c29e8..6d81ae184 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -11,6 +11,6 @@ public static void main(String[] args) { + "I have only been trained to greet you so far.\n" + "Once my owner is more proficient in what he does, he will give me more functions!"); //To be added to make sure user input is read. - System.out.println("Thats all from me! Goodbye!"); + System.out.println("That's all from me! Goodbye!"); } } From bc93fd2e550c1eddef7e2f9f2c63158eabf38d5d Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Wed, 25 Jan 2023 23:41:02 +0800 Subject: [PATCH 03/21] Finished Level 1 --- 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 6d81ae184..a2c45163f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,4 @@ +import java.util.Scanner; public class Duke { public static void main(String[] args) { String logo = " ____ _ \n" @@ -8,9 +9,17 @@ public static void main(String[] args) { + "__________________________\n"; System.out.println("Hello from\n" + logo); System.out.println("Hello! Do you need anything from me?\n" - + "I have only been trained to greet you so far.\n" + + "I have only been trained to greet and echo you so far.\n" + "Once my owner is more proficient in what he does, he will give me more functions!"); //To be added to make sure user input is read. + + String line; + Scanner in = new Scanner(System.in); + line = in.nextLine(); + while (!line.equals("Bye")) { + System.out.println(line); + line = in.nextLine(); + } System.out.println("That's all from me! Goodbye!"); } } From 7d21da97b3e0666c583197ed2f914bc56e5f40ff Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Thu, 26 Jan 2023 00:20:50 +0800 Subject: [PATCH 04/21] Finished Level-2 --- src/main/java/Duke.java | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a2c45163f..7b74a5a56 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -9,16 +9,43 @@ public static void main(String[] args) { + "__________________________\n"; System.out.println("Hello from\n" + logo); System.out.println("Hello! Do you need anything from me?\n" - + "I have only been trained to greet and echo you so far.\n" - + "Once my owner is more proficient in what he does, he will give me more functions!"); + + "I have only been trained to greet, echo and list you so far.\n" + + "Once my owner is more proficient in what he does, he will give me more functions!\n" + + "Key in a number based on the function\n 1) Echo \n 2) List"); //To be added to make sure user input is read. - + String[] items = new String[100]; String line; Scanner in = new Scanner(System.in); line = in.nextLine(); - while (!line.equals("Bye")) { - System.out.println(line); - line = in.nextLine(); + int action = Integer.parseInt(line); + switch (action) { + case 1: + line = in.nextLine(); + while (!line.equals("Bye")) { + System.out.println(line); + line = in.nextLine(); + } + break; + case 2: + int itemCounter = 0; + System.out.println("You may type anything to add to a list of a 100 items.\n" + + "Type List to list down everything you have added so far.\n" + + "Type Bye to exit."); + line = in.nextLine(); + while (!line.equals("Bye")) { + if (line.equals("List")) { + for (int iterator = 0; iterator < itemCounter; iterator++) { + System.out.println(iterator + 1 + ")" + items[iterator]); + } + line = in.nextLine(); + } else { + System.out.println("Added: " + line); + items[itemCounter] = line; + itemCounter++; + line = in.nextLine(); + } + } + break; } System.out.println("That's all from me! Goodbye!"); } From 6d10d3fc70c74c850f429e0d48c09109e402d3d5 Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Thu, 26 Jan 2023 01:26:07 +0800 Subject: [PATCH 05/21] Finished Level-3 --- src/main/java/Duke.java | 77 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 7b74a5a56..b83d07a83 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -13,7 +13,7 @@ public static void main(String[] args) { + "Once my owner is more proficient in what he does, he will give me more functions!\n" + "Key in a number based on the function\n 1) Echo \n 2) List"); //To be added to make sure user input is read. - String[] items = new String[100]; + Task[] actions = new Task[100]; String line; Scanner in = new Scanner(System.in); line = in.nextLine(); @@ -27,21 +27,61 @@ public static void main(String[] args) { } break; case 2: - int itemCounter = 0; - System.out.println("You may type anything to add to a list of a 100 items.\n" + int actionCounter = 0; + System.out.println("You may type anything to add to a list of a 100 things to do.\n" + "Type List to list down everything you have added so far.\n" + "Type Bye to exit."); line = in.nextLine(); while (!line.equals("Bye")) { if (line.equals("List")) { - for (int iterator = 0; iterator < itemCounter; iterator++) { - System.out.println(iterator + 1 + ")" + items[iterator]); + if (actionCounter == 0) { + System.out.println("There is nothing to list!"); + line = in.nextLine(); + continue; + } + for (int iterator = 0; iterator < actionCounter; iterator++) { + System.out.println(iterator + 1 + + ")[" + actions[iterator].getStatusIcon() + + "]" + + actions[iterator].getDescription() + ); + + } + System.out.println("You may now mark or unmark your tasks by typing Action followed by the task number! E.g. unmark 1 or mark 2\n" + + "To go back to editing your list, type edit"); + line = in.nextLine(); + while (!line.equals("edit")) { + String[] words = line.split(" "); + if (!(words[0].equals("mark") || words[0].equals("unmark") || words[0].equals("edit"))) { + System.out.println("Invalid Input!"); + line = in.nextLine(); + continue; + } + int taskNumber = Integer.parseInt(words[1]) - 1; + if (words[0].equals("mark")) { + actions[taskNumber].mark(); + } else { + // must be unmarked or some weird other mistype + actions[taskNumber].unmark(); + } + System.out.println("Done!\n" + + "[" + + actions[taskNumber].getStatusIcon() + + "]" + + actions[taskNumber].getDescription() + ); + line = in.nextLine(); + } + // have to edit before exit, might change this in the future + if (line.equals("edit")) { + System.out.println("Input to add to your list!"); } line = in.nextLine(); } else { System.out.println("Added: " + line); - items[itemCounter] = line; - itemCounter++; + Task toBeAdded = new Task(line); + actions[actionCounter] = toBeAdded; + actionCounter++; line = in.nextLine(); } } @@ -49,4 +89,27 @@ public static void main(String[] args) { } System.out.println("That's all from me! Goodbye!"); } + + public static class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + public void mark() { + this.isDone = true; + } + public void unmark() { + this.isDone = false; + } + public String getDescription() { + return(this.description); + } + } } From 444b5c02fb08e98a5d42d122c2baff962943788e Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Tue, 31 Jan 2023 20:28:05 +0800 Subject: [PATCH 06/21] Finished level-4 and edited level 0, 1, 2 and 3 --- src/main/java/Deadline.java | 13 +++ src/main/java/Duke.java | 161 +++++++++++++----------------------- src/main/java/Event.java | 17 ++++ src/main/java/Methods.java | 46 +++++++++++ src/main/java/Task.java | 27 ++++++ src/main/java/Todo.java | 12 +++ 6 files changed, 173 insertions(+), 103 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Methods.java create mode 100644 src/main/java/Task.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..62c8306bf --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,13 @@ +public class Deadline extends Task { + + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + by + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b83d07a83..071c86626 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,115 +1,70 @@ import java.util.Scanner; public class Duke { public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n" - + "__________________________\n"; - System.out.println("Hello from\n" + logo); - System.out.println("Hello! Do you need anything from me?\n" - + "I have only been trained to greet, echo and list you so far.\n" - + "Once my owner is more proficient in what he does, he will give me more functions!\n" - + "Key in a number based on the function\n 1) Echo \n 2) List"); - //To be added to make sure user input is read. + Methods.printGreetings(); + Task[] actions = new Task[100]; + int actionCounter = 0; + String line; Scanner in = new Scanner(System.in); - line = in.nextLine(); - int action = Integer.parseInt(line); - switch (action) { - case 1: - line = in.nextLine(); - while (!line.equals("Bye")) { - System.out.println(line); - line = in.nextLine(); - } - break; - case 2: - int actionCounter = 0; - System.out.println("You may type anything to add to a list of a 100 things to do.\n" - + "Type List to list down everything you have added so far.\n" - + "Type Bye to exit."); - line = in.nextLine(); - while (!line.equals("Bye")) { - if (line.equals("List")) { - if (actionCounter == 0) { - System.out.println("There is nothing to list!"); - line = in.nextLine(); - continue; - } - for (int iterator = 0; iterator < actionCounter; iterator++) { - System.out.println(iterator + 1 - + ")[" + actions[iterator].getStatusIcon() - + "]" - + actions[iterator].getDescription() - ); + int taskNumber; + String[] decisions; + Task toBeAdded; + String[] dates; + do { + line = in.nextLine(); + decisions = line.split(" "); + dates = line.split("/"); + switch (decisions[0]) { + case "echo": + System.out.println(Methods.findTaskDetails(line)); + break; - } - System.out.println("You may now mark or unmark your tasks by typing Action followed by the task number! E.g. unmark 1 or mark 2\n" - + "To go back to editing your list, type edit"); - line = in.nextLine(); - while (!line.equals("edit")) { - String[] words = line.split(" "); - if (!(words[0].equals("mark") || words[0].equals("unmark") || words[0].equals("edit"))) { - System.out.println("Invalid Input!"); - line = in.nextLine(); - continue; - } - int taskNumber = Integer.parseInt(words[1]) - 1; - if (words[0].equals("mark")) { - actions[taskNumber].mark(); - } else { - // must be unmarked or some weird other mistype - actions[taskNumber].unmark(); - } - System.out.println("Done!\n" - + "[" - + actions[taskNumber].getStatusIcon() - + "]" - + actions[taskNumber].getDescription() - ); - line = in.nextLine(); - } - // have to edit before exit, might change this in the future - if (line.equals("edit")) { - System.out.println("Input to add to your list!"); - } - line = in.nextLine(); - } else { - System.out.println("Added: " + line); - Task toBeAdded = new Task(line); - actions[actionCounter] = toBeAdded; - actionCounter++; - line = in.nextLine(); - } - } - break; - } - System.out.println("That's all from me! Goodbye!"); - } + case "todo": + toBeAdded = new Todo(Methods.findTaskDetails(line)); + actions[actionCounter] = toBeAdded; + actionCounter++; + Methods.printAcknowledgement(toBeAdded,actionCounter); + break; - public static class Task { - protected String description; - protected boolean isDone; + case "event": + toBeAdded = new Event(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1]), Methods.findTaskDetails(dates[2])); + actions[actionCounter] = toBeAdded; + actionCounter++; + Methods.printAcknowledgement(toBeAdded,actionCounter); + break; - public Task(String description) { - this.description = description; - this.isDone = false; - } + case "deadline": + toBeAdded = new Deadline(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1])); + actions[actionCounter] = toBeAdded; + actionCounter++; + Methods.printAcknowledgement(toBeAdded,actionCounter); + break; - public String getStatusIcon() { - return (isDone ? "X" : " "); // mark done task with X - } - public void mark() { - this.isDone = true; - } - public void unmark() { - this.isDone = false; - } - public String getDescription() { - return(this.description); - } + case "mark": + taskNumber = Integer.parseInt(decisions[1]) - 1; + actions[taskNumber].mark(); + Methods.printDoneMarkingTasks(actions[taskNumber]); + break; + + case "unmark": + taskNumber = Integer.parseInt(decisions[1]) - 1; + actions[taskNumber].unmark(); + Methods.printDoneMarkingTasks(actions[taskNumber]); + break; + + case "list": + if (actionCounter == 0) { + Methods.printEmptyList(); + continue; + } + for (int iterator = 0; iterator < actionCounter; iterator++) { + Methods.printListElement(iterator, actions[iterator]); + } + break; + } + } while (!decisions[0].equals("bye")); + System.out.println("That's all from me! Goodbye!"); } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..71064ec60 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,17 @@ +public class Event extends Task { + + protected String from; + protected String to; + + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (from: " + from + "to: " + to + ")"; + + } +} diff --git a/src/main/java/Methods.java b/src/main/java/Methods.java new file mode 100644 index 000000000..5f149411d --- /dev/null +++ b/src/main/java/Methods.java @@ -0,0 +1,46 @@ +import java.util.Scanner; +public class Methods { + public static void printGreetings() { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n" + + "__________________________\n"; + System.out.println("Hello from\n" + logo); + System.out.println("Hello! Do you need anything from me?\n" + + "I have only been trained to greet, echo and list you so far.\n" + + "Once my owner is more proficient in what he does, he will give me more functions!\n" + + "Key in a number based on the function\n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n" + + "When you wish to exit, do tell me by typing : bye"); + } + public static void print(String input) { + System.out.println(input); + } + + public static void printEmptyList() { + System.out.println("There is nothing to list!"); + } + + public static void printListElement(int iterator, Task action) { + Methods.print(iterator + 1 + + ")" + + action.toString()); + } + public static void printDoneMarkingTasks(Task action) { + Methods.print("Done!\n" + + action.toString() + ); + } + public static String findTaskDetails(String line) { + return line.substring(line.indexOf(" ") + 1); + } + public static void printAcknowledgement(Task action, int actionCounter) { + System.out.println("Got it. I've added this task:\n" + + action.toString() + + System.lineSeparator() + + "Now you have " + + actionCounter + + " tasks in the list."); + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..ee88b59db --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,27 @@ +public class Task { + protected String description; + protected boolean isDone; + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + public void mark() { + this.isDone = true; + } + public void unmark() { + this.isDone = false; + } + public String getDescription() { + return(this.description); + } + public String toString() { + return( "[" + + this.getStatusIcon() + + "] " + + this.getDescription()); + } +} \ No newline at end of file diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..99e63fa3c --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,12 @@ +public class Todo extends Task { + public Todo(String description) { + super(description); + } + @Override + public String toString() { + return "[T]" + super.toString(); + } + +} + + From f7d165c27298a49e1a6a73e231a09128f2e2440e Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Tue, 31 Jan 2023 22:16:27 +0800 Subject: [PATCH 07/21] Minor format changes --- src/main/java/Methods.java | 11 ++--------- src/main/java/Task.java | 5 +---- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/main/java/Methods.java b/src/main/java/Methods.java index 5f149411d..4586512ac 100644 --- a/src/main/java/Methods.java +++ b/src/main/java/Methods.java @@ -28,19 +28,12 @@ public static void printListElement(int iterator, Task action) { + action.toString()); } public static void printDoneMarkingTasks(Task action) { - Methods.print("Done!\n" - + action.toString() - ); + Methods.print("Done!\n" + action.toString()); } public static String findTaskDetails(String line) { return line.substring(line.indexOf(" ") + 1); } public static void printAcknowledgement(Task action, int actionCounter) { - System.out.println("Got it. I've added this task:\n" - + action.toString() - + System.lineSeparator() - + "Now you have " - + actionCounter - + " tasks in the list."); + System.out.println("Got it. I've added this task:\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index ee88b59db..0e7270971 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -19,9 +19,6 @@ public String getDescription() { return(this.description); } public String toString() { - return( "[" - + this.getStatusIcon() - + "] " - + this.getDescription()); + return( "[" + this.getStatusIcon() + "] " + this.getDescription()); } } \ No newline at end of file From 25dffa267f40cec1049e5c9c595f27e51737b865 Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Tue, 7 Feb 2023 21:26:01 +0800 Subject: [PATCH 08/21] Finished Level 5 --- src/main/java/Duke.java | 10 ++-- src/main/java/DukeException.java | 15 ++++++ src/main/java/Methods.java | 9 ++-- src/main/java/commandChecker.java | 84 +++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 src/main/java/DukeException.java create mode 100644 src/main/java/commandChecker.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 071c86626..fde39bc3a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -16,6 +16,10 @@ public static void main(String[] args) { line = in.nextLine(); decisions = line.split(" "); dates = line.split("/"); + commandChecker currentLoop = new commandChecker(decisions, dates, actionCounter); + if (currentLoop.hasErrors()) { + decisions[0] = "Invalidate"; + } switch (decisions[0]) { case "echo": System.out.println(Methods.findTaskDetails(line)); @@ -55,14 +59,12 @@ public static void main(String[] args) { break; case "list": - if (actionCounter == 0) { - Methods.printEmptyList(); - continue; - } for (int iterator = 0; iterator < actionCounter; iterator++) { Methods.printListElement(iterator, actions[iterator]); } break; + default: + Methods.printCurrentSupportedActions(); } } while (!decisions[0].equals("bye")); System.out.println("That's all from me! Goodbye!"); diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..3a556b567 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,15 @@ +public class DukeException extends Exception{ + public String description; + public DukeException(){ + this.description = "No Description"; + } + public DukeException(String description) { + this.description = description; + } + public void setDescription(String description) { + this.description = description; + } + public String getDescription() { + return this.description; + } +} diff --git a/src/main/java/Methods.java b/src/main/java/Methods.java index 4586512ac..e3b3746c0 100644 --- a/src/main/java/Methods.java +++ b/src/main/java/Methods.java @@ -11,17 +11,13 @@ public static void printGreetings() { System.out.println("Hello! Do you need anything from me?\n" + "I have only been trained to greet, echo and list you so far.\n" + "Once my owner is more proficient in what he does, he will give me more functions!\n" - + "Key in a number based on the function\n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n" + + " 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n" + "When you wish to exit, do tell me by typing : bye"); } public static void print(String input) { System.out.println(input); } - public static void printEmptyList() { - System.out.println("There is nothing to list!"); - } - public static void printListElement(int iterator, Task action) { Methods.print(iterator + 1 + ")" @@ -36,4 +32,7 @@ public static String findTaskDetails(String line) { public static void printAcknowledgement(Task action, int actionCounter) { System.out.println("Got it. I've added this task:\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); } + public static void printCurrentSupportedActions() { + Methods.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n"); + } } diff --git a/src/main/java/commandChecker.java b/src/main/java/commandChecker.java new file mode 100644 index 000000000..0da478921 --- /dev/null +++ b/src/main/java/commandChecker.java @@ -0,0 +1,84 @@ +public class commandChecker { + private final String[] decisions; + private final String[] dates; + private final int actionCounter; + boolean hasErrorFlags = false; + public commandChecker(String[] decisions, String[] dates, int actionCounter) { + this.decisions = decisions; + this.dates = dates; + this.actionCounter = actionCounter; + } + public boolean hasErrors(){ + try { + validateCommand(); + } catch (DukeException e) { + this.hasErrorFlags = true; + if (!e.getDescription().equals("No Description")) { + Methods.print("Invalid input, please try again! Error Description: " + e.getDescription() + System.lineSeparator()); + } + } finally { + return this.hasErrorFlags; + } + } + public void validateCommand() throws DukeException { + DukeException currentException = new DukeException(); + switch (decisions[0]) { + case "echo": + if (decisions.length < 2) { + currentException.setDescription("echo description cannot be empty!"); + throw currentException; + } + break; + case "todo": + if (decisions.length < 2) { + currentException.setDescription("todo description cannot be empty!"); + throw currentException; + } + break; + + case "event": + if (dates.length < 3) { + currentException.setDescription("event needs to have exactly two dates, /from date /to date"); + throw currentException; + } + break; + + case "deadline": + if (dates.length < 2) { + currentException.setDescription("deadline needs to have exactly one date, /by"); + throw currentException; + } + break; + + case "mark": + if (decisions.length < 2) { + currentException.setDescription("task you wish to mark is empty!"); + throw currentException; + } else if (decisions.length > 2) { + currentException.setDescription("you may only mark one task at a time"); + throw currentException; + } + break; + + case "unmark": + if (decisions.length < 2) { + currentException.setDescription("task you wish to unmark is empty!"); + throw currentException; + } else if (decisions.length > 2) { + currentException.setDescription("you may only unmark one task at a time"); + throw currentException; + } + break; + + case "list": + if (actionCounter == 0) { + currentException.setDescription("list is currently empty!"); + throw currentException; + } + break; + default: + currentException.setDescription("Invalid action word! e.g. echo, todo, list, etc."); + throw currentException; + } + } +} From 544ba6407a3c00f49d5953507fda3b5153bac629 Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Sun, 12 Feb 2023 16:52:43 +0800 Subject: [PATCH 09/21] Minor changes to main, added DukeSession and fixed minor bugs --- src/main/java/Duke.java | 75 ++-------------- src/main/java/DukeSession.java | 86 +++++++++++++++++++ src/main/java/{ => tasks}/Deadline.java | 5 +- src/main/java/{ => tasks}/Event.java | 6 +- src/main/java/{ => tasks}/Task.java | 15 +++- src/main/java/{ => tasks}/Todo.java | 3 + .../java/{ => utility}/DukeException.java | 10 ++- src/main/java/{ => utility}/Methods.java | 10 ++- .../java/{ => utility}/commandChecker.java | 23 ++++- 9 files changed, 153 insertions(+), 80 deletions(-) create mode 100644 src/main/java/DukeSession.java rename src/main/java/{ => tasks}/Deadline.java (86%) rename src/main/java/{ => tasks}/Event.java (83%) rename src/main/java/{ => tasks}/Task.java (71%) rename src/main/java/{ => tasks}/Todo.java (92%) rename src/main/java/{ => utility}/DukeException.java (78%) rename src/main/java/{ => utility}/Methods.java (97%) rename src/main/java/{ => utility}/commandChecker.java (82%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index fde39bc3a..69d354a9d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,72 +1,13 @@ +import tasks.Deadline; +import tasks.Event; +import tasks.Task; +import tasks.Todo; + import java.util.Scanner; public class Duke { public static void main(String[] args) { - Methods.printGreetings(); - - Task[] actions = new Task[100]; - int actionCounter = 0; - - String line; - Scanner in = new Scanner(System.in); - int taskNumber; - String[] decisions; - Task toBeAdded; - String[] dates; - do { - line = in.nextLine(); - decisions = line.split(" "); - dates = line.split("/"); - commandChecker currentLoop = new commandChecker(decisions, dates, actionCounter); - if (currentLoop.hasErrors()) { - decisions[0] = "Invalidate"; - } - switch (decisions[0]) { - case "echo": - System.out.println(Methods.findTaskDetails(line)); - break; - - case "todo": - toBeAdded = new Todo(Methods.findTaskDetails(line)); - actions[actionCounter] = toBeAdded; - actionCounter++; - Methods.printAcknowledgement(toBeAdded,actionCounter); - break; - - case "event": - toBeAdded = new Event(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1]), Methods.findTaskDetails(dates[2])); - actions[actionCounter] = toBeAdded; - actionCounter++; - Methods.printAcknowledgement(toBeAdded,actionCounter); - break; - - case "deadline": - toBeAdded = new Deadline(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1])); - actions[actionCounter] = toBeAdded; - actionCounter++; - Methods.printAcknowledgement(toBeAdded,actionCounter); - break; - - case "mark": - taskNumber = Integer.parseInt(decisions[1]) - 1; - actions[taskNumber].mark(); - Methods.printDoneMarkingTasks(actions[taskNumber]); - break; - - case "unmark": - taskNumber = Integer.parseInt(decisions[1]) - 1; - actions[taskNumber].unmark(); - Methods.printDoneMarkingTasks(actions[taskNumber]); - break; - - case "list": - for (int iterator = 0; iterator < actionCounter; iterator++) { - Methods.printListElement(iterator, actions[iterator]); - } - break; - default: - Methods.printCurrentSupportedActions(); - } - } while (!decisions[0].equals("bye")); - System.out.println("That's all from me! Goodbye!"); + DukeSession dukeSession = new DukeSession(); + dukeSession.execute(); } } + diff --git a/src/main/java/DukeSession.java b/src/main/java/DukeSession.java new file mode 100644 index 000000000..4e725376d --- /dev/null +++ b/src/main/java/DukeSession.java @@ -0,0 +1,86 @@ +import utility.Methods; +import utility.commandChecker; +import tasks.Deadline; +import tasks.Event; +import tasks.Task; +import tasks.Todo; + +import java.util.Scanner; + +import utility.Methods; + +public class DukeSession { + public static void execute() { + Methods.printGreetings(); + + Task[] actions = new Task[100]; + int actionCounter = 0; + + String line; + Scanner in = new Scanner(System.in); + int taskNumber; + String[] decisions; + Task toBeAdded; + String[] dates; + do { + line = in.nextLine(); + decisions = line.split(" "); + dates = line.split("/"); + commandChecker currentLoop = new commandChecker(decisions, dates, actionCounter); + if (currentLoop.hasErrors()) { + decisions[0] = "Invalidate"; + } + switch (decisions[0]) { + case "echo": + System.out.println(Methods.findTaskDetails(line)); + break; + + case "todo": + toBeAdded = new Todo(Methods.findTaskDetails(line)); + actions[actionCounter] = toBeAdded; + actionCounter++; + Methods.printAcknowledgement(toBeAdded, actionCounter); + break; + + case "event": + toBeAdded = new Event(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1]), Methods.findTaskDetails(dates[2])); + actions[actionCounter] = toBeAdded; + actionCounter++; + Methods.printAcknowledgement(toBeAdded, actionCounter); + break; + + case "deadline": + toBeAdded = new Deadline(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1])); + actions[actionCounter] = toBeAdded; + actionCounter++; + Methods.printAcknowledgement(toBeAdded, actionCounter); + break; + + case "mark": + taskNumber = Integer.parseInt(decisions[1]) - 1; + actions[taskNumber].mark(); + Methods.printDoneMarkingTasks(actions[taskNumber]); + break; + + case "unmark": + taskNumber = Integer.parseInt(decisions[1]) - 1; + actions[taskNumber].unmark(); + Methods.printDoneMarkingTasks(actions[taskNumber]); + break; + + case "list": + for (int iterator = 0; iterator < actionCounter; iterator++) { + Methods.printListElement(iterator, actions[iterator]); + } + break; + + case "bye": + break; + + default: + Methods.printCurrentSupportedActions(); + } + } while (!decisions[0].equals("bye")); + System.out.println("That's all from me! Goodbye!"); + } +} diff --git a/src/main/java/Deadline.java b/src/main/java/tasks/Deadline.java similarity index 86% rename from src/main/java/Deadline.java rename to src/main/java/tasks/Deadline.java index 62c8306bf..ef548e6cf 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/tasks/Deadline.java @@ -1,11 +1,14 @@ +package tasks; + public class Deadline extends Task { - protected String by; + private String by; public Deadline(String description, String by) { super(description); this.by = by; } + @Override public String toString() { return "[D]" + super.toString() + " (by: " + by + ")"; diff --git a/src/main/java/Event.java b/src/main/java/tasks/Event.java similarity index 83% rename from src/main/java/Event.java rename to src/main/java/tasks/Event.java index 71064ec60..4fde76066 100644 --- a/src/main/java/Event.java +++ b/src/main/java/tasks/Event.java @@ -1,7 +1,9 @@ +package tasks; + public class Event extends Task { - protected String from; - protected String to; + private String from; + private String to; public Event(String description, String from, String to) { super(description); diff --git a/src/main/java/Task.java b/src/main/java/tasks/Task.java similarity index 71% rename from src/main/java/Task.java rename to src/main/java/tasks/Task.java index 0e7270971..87fdd8421 100644 --- a/src/main/java/Task.java +++ b/src/main/java/tasks/Task.java @@ -1,6 +1,9 @@ +package tasks; + public class Task { - protected String description; - protected boolean isDone; + private String description; + private boolean isDone; + public Task(String description) { this.description = description; this.isDone = false; @@ -9,16 +12,20 @@ public Task(String description) { public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + public void mark() { this.isDone = true; } + public void unmark() { this.isDone = false; } + public String getDescription() { - return(this.description); + return (this.description); } + public String toString() { - return( "[" + this.getStatusIcon() + "] " + this.getDescription()); + return ("[" + this.getStatusIcon() + "] " + this.getDescription()); } } \ No newline at end of file diff --git a/src/main/java/Todo.java b/src/main/java/tasks/Todo.java similarity index 92% rename from src/main/java/Todo.java rename to src/main/java/tasks/Todo.java index 99e63fa3c..9795861eb 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/tasks/Todo.java @@ -1,7 +1,10 @@ +package tasks; + public class Todo extends Task { public Todo(String description) { super(description); } + @Override public String toString() { return "[T]" + super.toString(); diff --git a/src/main/java/DukeException.java b/src/main/java/utility/DukeException.java similarity index 78% rename from src/main/java/DukeException.java rename to src/main/java/utility/DukeException.java index 3a556b567..eddbaf903 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/utility/DukeException.java @@ -1,14 +1,20 @@ -public class DukeException extends Exception{ +package utility; + +public class DukeException extends Exception { public String description; - public DukeException(){ + + public DukeException() { this.description = "No Description"; } + public DukeException(String description) { this.description = description; } + public void setDescription(String description) { this.description = description; } + public String getDescription() { return this.description; } diff --git a/src/main/java/Methods.java b/src/main/java/utility/Methods.java similarity index 97% rename from src/main/java/Methods.java rename to src/main/java/utility/Methods.java index e3b3746c0..dfc431f3b 100644 --- a/src/main/java/Methods.java +++ b/src/main/java/utility/Methods.java @@ -1,4 +1,7 @@ -import java.util.Scanner; +package utility; + +import tasks.Task; + public class Methods { public static void printGreetings() { String logo = " ____ _ \n" @@ -14,6 +17,7 @@ public static void printGreetings() { + " 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n" + "When you wish to exit, do tell me by typing : bye"); } + public static void print(String input) { System.out.println(input); } @@ -23,15 +27,19 @@ public static void printListElement(int iterator, Task action) { + ")" + action.toString()); } + public static void printDoneMarkingTasks(Task action) { Methods.print("Done!\n" + action.toString()); } + public static String findTaskDetails(String line) { return line.substring(line.indexOf(" ") + 1); } + public static void printAcknowledgement(Task action, int actionCounter) { System.out.println("Got it. I've added this task:\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); } + public static void printCurrentSupportedActions() { Methods.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n"); } diff --git a/src/main/java/commandChecker.java b/src/main/java/utility/commandChecker.java similarity index 82% rename from src/main/java/commandChecker.java rename to src/main/java/utility/commandChecker.java index 0da478921..54574679d 100644 --- a/src/main/java/commandChecker.java +++ b/src/main/java/utility/commandChecker.java @@ -1,14 +1,18 @@ +package utility; + public class commandChecker { private final String[] decisions; private final String[] dates; private final int actionCounter; - boolean hasErrorFlags = false; + private boolean hasErrorFlags = false; + public commandChecker(String[] decisions, String[] dates, int actionCounter) { this.decisions = decisions; this.dates = dates; this.actionCounter = actionCounter; } - public boolean hasErrors(){ + + public boolean hasErrors() { try { validateCommand(); } catch (DukeException e) { @@ -20,7 +24,8 @@ public boolean hasErrors(){ return this.hasErrorFlags; } } - public void validateCommand() throws DukeException { + + private void validateCommand() throws DukeException { DukeException currentException = new DukeException(); switch (decisions[0]) { case "echo": @@ -58,6 +63,10 @@ public void validateCommand() throws DukeException { currentException.setDescription("you may only mark one task at a time"); throw currentException; } + if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { + currentException.setDescription("out of bounds!"); + throw currentException; + } break; case "unmark": @@ -68,6 +77,10 @@ public void validateCommand() throws DukeException { currentException.setDescription("you may only unmark one task at a time"); throw currentException; } + if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { + currentException.setDescription("out of bounds!"); + throw currentException; + } break; case "list": @@ -76,6 +89,10 @@ public void validateCommand() throws DukeException { throw currentException; } break; + + case "bye": + break; + default: currentException.setDescription("Invalid action word! e.g. echo, todo, list, etc."); throw currentException; From 8902eaee59dcf1d1e98e20f1a13c807e8b7c7a9b Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Sun, 12 Feb 2023 17:55:26 +0800 Subject: [PATCH 10/21] Level-6 done, with some minor changes to arraylist and actioncounter -> actions.size() --- src/main/java/DukeSession.java | 40 ++++++++++++----------- src/main/java/utility/Methods.java | 7 ++-- src/main/java/utility/commandChecker.java | 13 ++++++++ 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/main/java/DukeSession.java b/src/main/java/DukeSession.java index 4e725376d..46a2dfd19 100644 --- a/src/main/java/DukeSession.java +++ b/src/main/java/DukeSession.java @@ -1,20 +1,20 @@ import utility.Methods; import utility.commandChecker; + import tasks.Deadline; import tasks.Event; import tasks.Task; import tasks.Todo; import java.util.Scanner; +import java.util.ArrayList; -import utility.Methods; public class DukeSession { public static void execute() { Methods.printGreetings(); - Task[] actions = new Task[100]; - int actionCounter = 0; + ArrayList actions = new ArrayList<>(); String line; Scanner in = new Scanner(System.in); @@ -26,7 +26,7 @@ public static void execute() { line = in.nextLine(); decisions = line.split(" "); dates = line.split("/"); - commandChecker currentLoop = new commandChecker(decisions, dates, actionCounter); + commandChecker currentLoop = new commandChecker(decisions, dates, actions.size()); if (currentLoop.hasErrors()) { decisions[0] = "Invalidate"; } @@ -37,42 +37,44 @@ public static void execute() { case "todo": toBeAdded = new Todo(Methods.findTaskDetails(line)); - actions[actionCounter] = toBeAdded; - actionCounter++; - Methods.printAcknowledgement(toBeAdded, actionCounter); + actions.add(toBeAdded); + Methods.printAcknowledgement(toBeAdded, actions.size()); break; case "event": toBeAdded = new Event(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1]), Methods.findTaskDetails(dates[2])); - actions[actionCounter] = toBeAdded; - actionCounter++; - Methods.printAcknowledgement(toBeAdded, actionCounter); + actions.add(toBeAdded); + Methods.printAcknowledgement(toBeAdded, actions.size()); break; case "deadline": toBeAdded = new Deadline(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1])); - actions[actionCounter] = toBeAdded; - actionCounter++; - Methods.printAcknowledgement(toBeAdded, actionCounter); + actions.add(toBeAdded); + Methods.printAcknowledgement(toBeAdded, actions.size()); break; case "mark": taskNumber = Integer.parseInt(decisions[1]) - 1; - actions[taskNumber].mark(); - Methods.printDoneMarkingTasks(actions[taskNumber]); + actions.get(taskNumber).mark(); + Methods.printDoneMarkingTasks(actions.get(taskNumber)); break; case "unmark": taskNumber = Integer.parseInt(decisions[1]) - 1; - actions[taskNumber].unmark(); - Methods.printDoneMarkingTasks(actions[taskNumber]); + actions.get(taskNumber).unmark(); + Methods.printDoneMarkingTasks(actions.get(taskNumber)); break; case "list": - for (int iterator = 0; iterator < actionCounter; iterator++) { - Methods.printListElement(iterator, actions[iterator]); + for (int iterator = 0; iterator < actions.size(); iterator++) { + Methods.printListElement(iterator, actions.get(iterator)); } break; + case "delete": + taskNumber = Integer.parseInt(decisions[1]) - 1; + Methods.printDeleteAcknowledgement(actions.get(taskNumber), actions.size() - 1); + actions.remove(taskNumber); + break; case "bye": break; diff --git a/src/main/java/utility/Methods.java b/src/main/java/utility/Methods.java index dfc431f3b..b1cd527e1 100644 --- a/src/main/java/utility/Methods.java +++ b/src/main/java/utility/Methods.java @@ -14,7 +14,7 @@ public static void printGreetings() { System.out.println("Hello! Do you need anything from me?\n" + "I have only been trained to greet, echo and list you so far.\n" + "Once my owner is more proficient in what he does, he will give me more functions!\n" - + " 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n" + + " 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n" + "When you wish to exit, do tell me by typing : bye"); } @@ -41,6 +41,9 @@ public static void printAcknowledgement(Task action, int actionCounter) { } public static void printCurrentSupportedActions() { - Methods.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n"); + Methods.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n"); + } + public static void printDeleteAcknowledgement(Task action, int actionCounter) { + print("Got it. I've removed this task" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); } } diff --git a/src/main/java/utility/commandChecker.java b/src/main/java/utility/commandChecker.java index 54574679d..5197d3d7b 100644 --- a/src/main/java/utility/commandChecker.java +++ b/src/main/java/utility/commandChecker.java @@ -90,6 +90,19 @@ private void validateCommand() throws DukeException { } break; + case "delete": + if (decisions.length < 2) { + currentException.setDescription("task you wish to delete does not exist!"); + throw currentException; + } else if (decisions.length > 2) { + currentException.setDescription("you may only delete one task at a time"); + throw currentException; + } + if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { + currentException.setDescription("out of bounds!"); + throw currentException; + } + case "bye": break; From 959c5a43f7ef80d967cde2cf20a135c8c56f5d20 Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Sun, 12 Feb 2023 22:31:00 +0800 Subject: [PATCH 11/21] Finished Level-7, fixed some bugs for Level-6 as well --- src/main/java/Duke.java | 8 +- src/main/java/DukeSession.java | 16 ++-- src/main/java/file/methods/FileHandler.java | 83 +++++++++++++++++++++ src/main/java/tasks/Deadline.java | 5 ++ src/main/java/tasks/Event.java | 4 + src/main/java/tasks/Task.java | 5 ++ src/main/java/tasks/Todo.java | 7 +- src/main/java/utility/Methods.java | 2 +- 8 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 src/main/java/file/methods/FileHandler.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 69d354a9d..77aabdcf6 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,12 +1,10 @@ -import tasks.Deadline; -import tasks.Event; -import tasks.Task; -import tasks.Todo; - +import utility.Methods; import java.util.Scanner; public class Duke { public static void main(String[] args) { DukeSession dukeSession = new DukeSession(); + Methods.printGreetings(); + dukeSession.setUpArrayList(); dukeSession.execute(); } } diff --git a/src/main/java/DukeSession.java b/src/main/java/DukeSession.java index 46a2dfd19..160aedfe7 100644 --- a/src/main/java/DukeSession.java +++ b/src/main/java/DukeSession.java @@ -1,3 +1,4 @@ +import file.methods.FileHandler; import utility.Methods; import utility.commandChecker; @@ -6,22 +7,23 @@ import tasks.Task; import tasks.Todo; +import java.io.File; import java.util.Scanner; import java.util.ArrayList; public class DukeSession { - public static void execute() { - Methods.printGreetings(); - - ArrayList actions = new ArrayList<>(); - + private ArrayList actions = new ArrayList<>(); + public void setUpArrayList() { + FileHandler.setUpFile(actions); + } + public void execute() { String line; - Scanner in = new Scanner(System.in); int taskNumber; String[] decisions; Task toBeAdded; String[] dates; + Scanner in = new Scanner(System.in); do { line = in.nextLine(); decisions = line.split(" "); @@ -83,6 +85,8 @@ public static void execute() { Methods.printCurrentSupportedActions(); } } while (!decisions[0].equals("bye")); + FileHandler.saveFile(actions); System.out.println("That's all from me! Goodbye!"); } + } diff --git a/src/main/java/file/methods/FileHandler.java b/src/main/java/file/methods/FileHandler.java new file mode 100644 index 000000000..5eae0b1de --- /dev/null +++ b/src/main/java/file/methods/FileHandler.java @@ -0,0 +1,83 @@ +package file.methods; + + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + + +import tasks.Task; +import tasks.Todo; +import tasks.Event; +import tasks.Deadline; + +public class FileHandler { + + public static void setUpFile(ArrayList actions) { + try { + File f = new File("data/savedList.txt"); + File directory = new File("data"); + if (!directory.exists()) { + boolean success = directory.mkdir(); + if (!success) { + System.out.println("Met an error creating the directory"); + } + } + if (!f.exists()) { + f.createNewFile(); + return; + } + String line; + String[] decisions; + Scanner in = new Scanner(f); + Task toBeAdded; + while (in.hasNext()) { + line = in.nextLine(); + decisions = line.split("/"); + switch(decisions[0]) { + case "t": + toBeAdded = new Todo(decisions[2]); + if (decisions[1].equals("X")) { + toBeAdded.mark(); + } + actions.add(toBeAdded); + break; + + case "d": + toBeAdded = new Deadline(decisions[2],decisions[3]); + if (decisions[1].equals("X")) { + toBeAdded.mark(); + } + actions.add(toBeAdded); + break; + + case "e": + toBeAdded = new Event(decisions[2],decisions[3],decisions[4]); + if (decisions[1].equals("X")) { + toBeAdded.mark(); + } + actions.add(toBeAdded); + break; + + default: + System.out.println("File has been corrupted"); + } + } + } catch (IOException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } + } + public static void saveFile(ArrayList actions) { + try { + FileWriter fw = new FileWriter("data/savedList.txt"); + for (Task i : actions) { + fw.write(i.getCommand() + System.lineSeparator()); + } + fw.close(); + } catch (IOException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } + } +} diff --git a/src/main/java/tasks/Deadline.java b/src/main/java/tasks/Deadline.java index ef548e6cf..732a03196 100644 --- a/src/main/java/tasks/Deadline.java +++ b/src/main/java/tasks/Deadline.java @@ -13,4 +13,9 @@ public Deadline(String description, String by) { public String toString() { return "[D]" + super.toString() + " (by: " + by + ")"; } + + + public String getCommand() { + return "d/" + this.getStatusIcon() + "/" + this.getDescription() + "/" + this.by; + } } \ No newline at end of file diff --git a/src/main/java/tasks/Event.java b/src/main/java/tasks/Event.java index 4fde76066..3100a9c41 100644 --- a/src/main/java/tasks/Event.java +++ b/src/main/java/tasks/Event.java @@ -16,4 +16,8 @@ public String toString() { return "[E]" + super.toString() + " (from: " + from + "to: " + to + ")"; } + + public String getCommand() { + return "e/" + this.getStatusIcon() + "/" + this.getDescription() + "/" + this.from + "/" + this.to ; + } } diff --git a/src/main/java/tasks/Task.java b/src/main/java/tasks/Task.java index 87fdd8421..782530080 100644 --- a/src/main/java/tasks/Task.java +++ b/src/main/java/tasks/Task.java @@ -4,6 +4,7 @@ public class Task { private String description; private boolean isDone; + private String command; public Task(String description) { this.description = description; this.isDone = false; @@ -28,4 +29,8 @@ public String getDescription() { public String toString() { return ("[" + this.getStatusIcon() + "] " + this.getDescription()); } + + public String getCommand() { + return this.command; + } } \ No newline at end of file diff --git a/src/main/java/tasks/Todo.java b/src/main/java/tasks/Todo.java index 9795861eb..aa1f63efd 100644 --- a/src/main/java/tasks/Todo.java +++ b/src/main/java/tasks/Todo.java @@ -1,15 +1,20 @@ package tasks; public class Todo extends Task { + public Todo(String description) { super(description); } - @Override public String toString() { return "[T]" + super.toString(); } + + public String getCommand() { + return "t/" + this.getStatusIcon() + "/"+ this.getDescription(); + } + } diff --git a/src/main/java/utility/Methods.java b/src/main/java/utility/Methods.java index b1cd527e1..ca64e870b 100644 --- a/src/main/java/utility/Methods.java +++ b/src/main/java/utility/Methods.java @@ -44,6 +44,6 @@ public static void printCurrentSupportedActions() { Methods.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n"); } public static void printDeleteAcknowledgement(Task action, int actionCounter) { - print("Got it. I've removed this task" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); + print("Got it. I've removed this task\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); } } From 8a06c04432bb140998829a791d0dd0aa9f677d0a Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Sun, 12 Feb 2023 23:02:01 +0800 Subject: [PATCH 12/21] No idea what this does. Im trying to keep everything up to date. --- .gitignore | 1 + src/main/java/META-INF/MANIFEST.MF | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/.gitignore b/.gitignore index 2873e189e..2ae219250 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT +data/savedList.txt diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..9f37e4e0a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + From e0c6dc9baf4edc1241ee64b71d946444562f9948 Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Thu, 2 Mar 2023 02:48:17 +0800 Subject: [PATCH 13/21] Finished Level-9 --- src/main/java/Duke.java | 7 +- src/main/java/DukeSession.java | 92 --------- src/main/java/file/methods/FileHandler.java | 83 --------- src/main/java/file/storage/Storage.java | 97 ++++++++++ src/main/java/parser/DukeSession.java | 146 +++++++++++++++ src/main/java/tasks/Deadline.java | 2 +- src/main/java/tasks/Event.java | 2 +- .../java/utility/{Methods.java => Ui.java} | 24 ++- src/main/java/utility/commandChecker.java | 176 ++++++++++++------ 9 files changed, 377 insertions(+), 252 deletions(-) delete mode 100644 src/main/java/DukeSession.java delete mode 100644 src/main/java/file/methods/FileHandler.java create mode 100644 src/main/java/file/storage/Storage.java create mode 100644 src/main/java/parser/DukeSession.java rename src/main/java/utility/{Methods.java => Ui.java} (75%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 77aabdcf6..7ffd58c16 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,9 +1,10 @@ -import utility.Methods; -import java.util.Scanner; +import parser.DukeSession; +import utility.Ui; + public class Duke { public static void main(String[] args) { DukeSession dukeSession = new DukeSession(); - Methods.printGreetings(); + Ui.printGreetings(); dukeSession.setUpArrayList(); dukeSession.execute(); } diff --git a/src/main/java/DukeSession.java b/src/main/java/DukeSession.java deleted file mode 100644 index 160aedfe7..000000000 --- a/src/main/java/DukeSession.java +++ /dev/null @@ -1,92 +0,0 @@ -import file.methods.FileHandler; -import utility.Methods; -import utility.commandChecker; - -import tasks.Deadline; -import tasks.Event; -import tasks.Task; -import tasks.Todo; - -import java.io.File; -import java.util.Scanner; -import java.util.ArrayList; - - -public class DukeSession { - private ArrayList actions = new ArrayList<>(); - public void setUpArrayList() { - FileHandler.setUpFile(actions); - } - public void execute() { - String line; - int taskNumber; - String[] decisions; - Task toBeAdded; - String[] dates; - Scanner in = new Scanner(System.in); - do { - line = in.nextLine(); - decisions = line.split(" "); - dates = line.split("/"); - commandChecker currentLoop = new commandChecker(decisions, dates, actions.size()); - if (currentLoop.hasErrors()) { - decisions[0] = "Invalidate"; - } - switch (decisions[0]) { - case "echo": - System.out.println(Methods.findTaskDetails(line)); - break; - - case "todo": - toBeAdded = new Todo(Methods.findTaskDetails(line)); - actions.add(toBeAdded); - Methods.printAcknowledgement(toBeAdded, actions.size()); - break; - - case "event": - toBeAdded = new Event(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1]), Methods.findTaskDetails(dates[2])); - actions.add(toBeAdded); - Methods.printAcknowledgement(toBeAdded, actions.size()); - break; - - case "deadline": - toBeAdded = new Deadline(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1])); - actions.add(toBeAdded); - Methods.printAcknowledgement(toBeAdded, actions.size()); - break; - - case "mark": - taskNumber = Integer.parseInt(decisions[1]) - 1; - actions.get(taskNumber).mark(); - Methods.printDoneMarkingTasks(actions.get(taskNumber)); - break; - - case "unmark": - taskNumber = Integer.parseInt(decisions[1]) - 1; - actions.get(taskNumber).unmark(); - Methods.printDoneMarkingTasks(actions.get(taskNumber)); - break; - - case "list": - for (int iterator = 0; iterator < actions.size(); iterator++) { - Methods.printListElement(iterator, actions.get(iterator)); - } - break; - case "delete": - taskNumber = Integer.parseInt(decisions[1]) - 1; - Methods.printDeleteAcknowledgement(actions.get(taskNumber), actions.size() - 1); - actions.remove(taskNumber); - break; - - case "bye": - break; - - default: - Methods.printCurrentSupportedActions(); - } - } while (!decisions[0].equals("bye")); - FileHandler.saveFile(actions); - System.out.println("That's all from me! Goodbye!"); - } - -} diff --git a/src/main/java/file/methods/FileHandler.java b/src/main/java/file/methods/FileHandler.java deleted file mode 100644 index 5eae0b1de..000000000 --- a/src/main/java/file/methods/FileHandler.java +++ /dev/null @@ -1,83 +0,0 @@ -package file.methods; - - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Scanner; - - -import tasks.Task; -import tasks.Todo; -import tasks.Event; -import tasks.Deadline; - -public class FileHandler { - - public static void setUpFile(ArrayList actions) { - try { - File f = new File("data/savedList.txt"); - File directory = new File("data"); - if (!directory.exists()) { - boolean success = directory.mkdir(); - if (!success) { - System.out.println("Met an error creating the directory"); - } - } - if (!f.exists()) { - f.createNewFile(); - return; - } - String line; - String[] decisions; - Scanner in = new Scanner(f); - Task toBeAdded; - while (in.hasNext()) { - line = in.nextLine(); - decisions = line.split("/"); - switch(decisions[0]) { - case "t": - toBeAdded = new Todo(decisions[2]); - if (decisions[1].equals("X")) { - toBeAdded.mark(); - } - actions.add(toBeAdded); - break; - - case "d": - toBeAdded = new Deadline(decisions[2],decisions[3]); - if (decisions[1].equals("X")) { - toBeAdded.mark(); - } - actions.add(toBeAdded); - break; - - case "e": - toBeAdded = new Event(decisions[2],decisions[3],decisions[4]); - if (decisions[1].equals("X")) { - toBeAdded.mark(); - } - actions.add(toBeAdded); - break; - - default: - System.out.println("File has been corrupted"); - } - } - } catch (IOException e) { - System.out.println("Something went wrong: " + e.getMessage()); - } - } - public static void saveFile(ArrayList actions) { - try { - FileWriter fw = new FileWriter("data/savedList.txt"); - for (Task i : actions) { - fw.write(i.getCommand() + System.lineSeparator()); - } - fw.close(); - } catch (IOException e) { - System.out.println("Something went wrong: " + e.getMessage()); - } - } -} diff --git a/src/main/java/file/storage/Storage.java b/src/main/java/file/storage/Storage.java new file mode 100644 index 000000000..298c51aa0 --- /dev/null +++ b/src/main/java/file/storage/Storage.java @@ -0,0 +1,97 @@ +package file.storage; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + + +import tasks.Task; +import tasks.Todo; +import tasks.Event; +import tasks.Deadline; + +public class Storage { + final static String DEFAULT_SAVE_PATH = "data/savedList.txt"; + final static String DEFAULT_DIRECTORY_NAME = "data"; + + private static File directoryAndFileChecker() throws IOException { + File f = new File(DEFAULT_SAVE_PATH); + File directory = new File(DEFAULT_DIRECTORY_NAME); + try { + if (!directory.exists()) { + directory.mkdir(); + } + if (!f.exists()) { + f.createNewFile(); + } + } catch (IOException e){ + System.out.println("Something went wrong: " + e.getMessage()); + } + return f; + } + public static void setUpFile(ArrayList actions) { + try { + File f = directoryAndFileChecker(); + String line; + String[] decisions; + Scanner in = new Scanner(f); + while (in.hasNext()) { + line = in.nextLine(); + decisions = line.split("/"); + switch (decisions[0]) { + case "t": + setUpToDo(decisions, actions); + break; + case "d": + setUpDeadline(decisions, actions); + break; + case "e": + setUpEvent(decisions, actions); + break; + default: + System.out.println("File has been corrupted"); + } + } + } catch (IOException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } + } + + public static void saveFile(ArrayList actions) { + try { + FileWriter fw = new FileWriter(DEFAULT_SAVE_PATH); + for (Task i : actions) { + fw.write(i.getCommand() + System.lineSeparator()); + } + fw.close(); + } catch (IOException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } + } + + private static void setUpToDo(String[] decisions, ArrayList actions) { + Task toBeAdded = new Todo(decisions[2]); + if (decisions[1].equals("X")) { + toBeAdded.mark(); + } + actions.add(toBeAdded); + } + + private static void setUpDeadline(String[] decisions, ArrayList actions) { + Task toBeAdded = new Deadline(decisions[2], decisions[3]); + if (decisions[1].equals("X")) { + toBeAdded.mark(); + } + actions.add(toBeAdded); + } + + private static void setUpEvent(String[] decisions, ArrayList actions) { + Task toBeAdded = new Event(decisions[2], decisions[3], decisions[4]); + if (decisions[1].equals("X")) { + toBeAdded.mark(); + } + actions.add(toBeAdded); + } +} diff --git a/src/main/java/parser/DukeSession.java b/src/main/java/parser/DukeSession.java new file mode 100644 index 000000000..b51a01438 --- /dev/null +++ b/src/main/java/parser/DukeSession.java @@ -0,0 +1,146 @@ +package parser; + +import file.storage.Storage; +import utility.Ui; +import utility.commandChecker; + +import tasks.Deadline; +import tasks.Event; +import tasks.Task; +import tasks.Todo; + +import java.util.Scanner; +import java.util.ArrayList; + + +public class DukeSession { + private ArrayList actions = new ArrayList<>(); + + public void setUpArrayList() { + Storage.setUpFile(actions); + } + + public void execute() { + String line; + String[] decisions; + String[] dates; + Scanner in = new Scanner(System.in); + do { + line = in.nextLine(); + decisions = line.split(" "); + dates = line.split("/"); + commandChecker currentLoop = new commandChecker(decisions, dates, actions.size()); + if (currentLoop.hasErrors()) { + decisions[0] = "Invalidate"; + } + handleInputs(line, decisions, dates); + } while (!decisions[0].equals("bye")); + Storage.saveFile(actions); + System.out.println("That's all from me! Goodbye!"); + } + + private void handleInputs(String line, String[] decisions, String[] dates) { + switch (decisions[0]) { + case "echo": + System.out.println(findTaskDetails(line)); + break; + case "todo": + handleToDo(line); + break; + case "event": + handleEvent(dates); + break; + case "deadline": + handleDeadline(dates); + break; + case "mark": + handleMarkTask(decisions); + break; + case "unmark": + handleUnmarkTask(decisions); + break; + case "list": + handleList(); + break; + case "delete": + handleDeleteTask(decisions); + break; + case "find": + handleFindTask(line); + break; + case "bye": + break; + default: + Ui.printCurrentSupportedActions(); + } + } + + private void handleToDo(String line) { + Task toBeAdded = new Todo(findTaskDetails(line)); + actions.add(toBeAdded); + Ui.printAcknowledgement(toBeAdded, actions.size()); + } + + private void handleEvent(String[] dates) { + Task toBeAdded = new Event(findTaskDetails(dates[0]), findTaskDetails(dates[1]), findTaskDetails(dates[2])); + actions.add(toBeAdded); + Ui.printAcknowledgement(toBeAdded, actions.size()); + } + + private void handleDeadline(String[] dates) { + Task toBeAdded = new Deadline(findTaskDetails(dates[0]), findTaskDetails(dates[1])); + actions.add(toBeAdded); + Ui.printAcknowledgement(toBeAdded, actions.size()); + } + + private void handleMarkTask(String[] decisions) { + int taskNumber = Integer.parseInt(decisions[1]) - 1; + actions.get(taskNumber).mark(); + Ui.printDoneMarkingTasks(actions.get(taskNumber)); + } + + private void handleUnmarkTask(String[] decisions) { + int taskNumber = Integer.parseInt(decisions[1]) - 1; + actions.get(taskNumber).unmark(); + Ui.printDoneMarkingTasks(actions.get(taskNumber)); + } + + private void handleList() { + for (int i = 0; i < actions.size(); i++) { + Ui.printListElement(i, actions.get(i)); + } + } + + private void handleDeleteTask(String[] decisions) { + int taskNumber = Integer.parseInt(decisions[1]) - 1; + Ui.printDeleteAcknowledgement(actions.get(taskNumber), actions.size() - 1); + actions.remove(taskNumber); + } + + private void handleFindTask(String line) { + String termToFind = findTaskDetails(line); + Ui.print(termToFind); + int iterator = 0; + int printedTasks = 0; + for (Task searchTerm : actions) { + if (searchTerm.getDescription().contains(termToFind)) { + Ui.printListElement(iterator, actions.get(iterator)); + printedTasks++; + } + iterator++; + } + if (printedTasks > 0) { + Ui.printFindAcknowledgement(); + } else if (printedTasks == 0) { + Ui.printCannotFindAcknowledgement(); + } else { + Ui.print("Oh dear, Something has went wrong!"); + } + } + + private static String findTaskDetails(String line) { + return line.substring(line.indexOf(" ") + 1); + } + + +} diff --git a/src/main/java/tasks/Deadline.java b/src/main/java/tasks/Deadline.java index 732a03196..0d1a12bbd 100644 --- a/src/main/java/tasks/Deadline.java +++ b/src/main/java/tasks/Deadline.java @@ -11,7 +11,7 @@ public Deadline(String description, String by) { @Override public String toString() { - return "[D]" + super.toString() + " (by: " + by + ")"; + return "[D]" + super.toString() + "(by: " + by + ")"; } diff --git a/src/main/java/tasks/Event.java b/src/main/java/tasks/Event.java index 3100a9c41..5880efdb3 100644 --- a/src/main/java/tasks/Event.java +++ b/src/main/java/tasks/Event.java @@ -13,7 +13,7 @@ public Event(String description, String from, String to) { @Override public String toString() { - return "[E]" + super.toString() + " (from: " + from + "to: " + to + ")"; + return "[E]" + super.toString() + "(from: " + from + "to: " + to + ")"; } diff --git a/src/main/java/utility/Methods.java b/src/main/java/utility/Ui.java similarity index 75% rename from src/main/java/utility/Methods.java rename to src/main/java/utility/Ui.java index ca64e870b..cbbd66c2e 100644 --- a/src/main/java/utility/Methods.java +++ b/src/main/java/utility/Ui.java @@ -2,7 +2,7 @@ import tasks.Task; -public class Methods { +public class Ui { public static void printGreetings() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -14,7 +14,7 @@ public static void printGreetings() { System.out.println("Hello! Do you need anything from me?\n" + "I have only been trained to greet, echo and list you so far.\n" + "Once my owner is more proficient in what he does, he will give me more functions!\n" - + " 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n" + + " 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n 8)find\n" + "When you wish to exit, do tell me by typing : bye"); } @@ -23,27 +23,31 @@ public static void print(String input) { } public static void printListElement(int iterator, Task action) { - Methods.print(iterator + 1 - + ")" - + action.toString()); + Ui.print(iterator + 1 + ")" + action.toString()); } public static void printDoneMarkingTasks(Task action) { - Methods.print("Done!\n" + action.toString()); + Ui.print("Done!\n" + action.toString()); } - public static String findTaskDetails(String line) { - return line.substring(line.indexOf(" ") + 1); - } public static void printAcknowledgement(Task action, int actionCounter) { System.out.println("Got it. I've added this task:\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); } public static void printCurrentSupportedActions() { - Methods.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n"); + Ui.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n 8)find\n"); } public static void printDeleteAcknowledgement(Task action, int actionCounter) { print("Got it. I've removed this task\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); } + + public static void printFindAcknowledgement() { + Ui.print("Here you go!"); + } + public static void printCannotFindAcknowledgement() { + Ui.print("I am sorry, but I did not find any matches."); + } + + } diff --git a/src/main/java/utility/commandChecker.java b/src/main/java/utility/commandChecker.java index 5197d3d7b..db1cbbcca 100644 --- a/src/main/java/utility/commandChecker.java +++ b/src/main/java/utility/commandChecker.java @@ -18,7 +18,7 @@ public boolean hasErrors() { } catch (DukeException e) { this.hasErrorFlags = true; if (!e.getDescription().equals("No Description")) { - Methods.print("Invalid input, please try again! Error Description: " + e.getDescription() + System.lineSeparator()); + Ui.print("Invalid input, please try again! Error Description: " + e.getDescription() + System.lineSeparator()); } } finally { return this.hasErrorFlags; @@ -29,86 +29,138 @@ private void validateCommand() throws DukeException { DukeException currentException = new DukeException(); switch (decisions[0]) { case "echo": - if (decisions.length < 2) { - currentException.setDescription("echo description cannot be empty!"); - throw currentException; - } + validateEcho(currentException); break; case "todo": - if (decisions.length < 2) { - currentException.setDescription("todo description cannot be empty!"); - throw currentException; - } + validateToDo(currentException); break; - case "event": - if (dates.length < 3) { - currentException.setDescription("event needs to have exactly two dates, /from date /to date"); - throw currentException; - } + validateEvent(currentException); break; - case "deadline": - if (dates.length < 2) { - currentException.setDescription("deadline needs to have exactly one date, /by"); - throw currentException; - } + validateDeadline(currentException); break; - case "mark": - if (decisions.length < 2) { - currentException.setDescription("task you wish to mark is empty!"); - throw currentException; - } else if (decisions.length > 2) { - currentException.setDescription("you may only mark one task at a time"); - throw currentException; - } - if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { - currentException.setDescription("out of bounds!"); - throw currentException; - } + validateMarkTask(currentException); break; - case "unmark": - if (decisions.length < 2) { - currentException.setDescription("task you wish to unmark is empty!"); - throw currentException; - } else if (decisions.length > 2) { - currentException.setDescription("you may only unmark one task at a time"); - throw currentException; - } - if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { - currentException.setDescription("out of bounds!"); - throw currentException; - } + validateUnmarkTask(currentException); break; - case "list": - if (actionCounter == 0) { - currentException.setDescription("list is currently empty!"); - throw currentException; - } + validateList(currentException); break; - case "delete": - if (decisions.length < 2) { - currentException.setDescription("task you wish to delete does not exist!"); - throw currentException; - } else if (decisions.length > 2) { - currentException.setDescription("you may only delete one task at a time"); - throw currentException; - } - if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { - currentException.setDescription("out of bounds!"); - throw currentException; - } - + validateDeleteTask(currentException); + break; + case "find": + validateFindTask(currentException); + break; case "bye": break; - default: currentException.setDescription("Invalid action word! e.g. echo, todo, list, etc."); throw currentException; } } + + private void validateEcho(DukeException currentException) throws DukeException { + if (decisions.length < 2) { + currentException.setDescription("echo description cannot be empty!"); + throw currentException; + } + } + + private void validateToDo(DukeException currentException) throws DukeException { + if (decisions.length < 2) { + currentException.setDescription("todo description cannot be empty!"); + throw currentException; + } + } + + private void validateEvent(DukeException currentException) throws DukeException { + if (dates.length < 3) { + currentException.setDescription("event needs to have exactly two dates, /from date /to date"); + throw currentException; + } + } + + private void validateDeadline(DukeException currentException) throws DukeException { + if (dates.length < 2) { + currentException.setDescription("deadline needs to have exactly one date, /by"); + throw currentException; + } + } + + private void validateMarkTask(DukeException currentException) throws DukeException { + if (decisions.length < 2) { + currentException.setDescription("the description of the task you wish to mark is empty!"); + throw currentException; + } else if (decisions.length > 2) { + currentException.setDescription("you may only mark one task at a time"); + throw currentException; + } + if (actionCounter == 0) { + currentException.setDescription("list is currently empty!"); + throw currentException; + } + if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { + currentException.setDescription("out of bounds!"); + throw currentException; + } + } + + private void validateUnmarkTask(DukeException currentException) throws DukeException { + if (decisions.length < 2) { + currentException.setDescription("the description of the task you wish to unmark is empty!"); + throw currentException; + } else if (decisions.length > 2) { + currentException.setDescription("you may only unmark one task at a time"); + throw currentException; + } + if (actionCounter == 0) { + currentException.setDescription("list is currently empty!"); + throw currentException; + } + if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { + currentException.setDescription("out of bounds!"); + throw currentException; + } + } + + private void validateList(DukeException currentException) throws DukeException { + if (actionCounter == 0) { + currentException.setDescription("list is currently empty!"); + throw currentException; + } + } + + private void validateDeleteTask(DukeException currentException) throws DukeException { + if (decisions.length < 2) { + currentException.setDescription("the description of the task you wish to delete is empty!"); + throw currentException; + } else if (decisions.length > 2) { + currentException.setDescription("you may only delete one task at a time"); + throw currentException; + } + if (actionCounter == 0) { + currentException.setDescription("list is currently empty!"); + throw currentException; + } + if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { + currentException.setDescription("out of bounds!"); + throw currentException; + } + } + private void validateFindTask(DukeException currentException) throws DukeException { + if (decisions.length < 2) { + currentException.setDescription("the description of the task you wish to find cannot be empty!"); + throw currentException; + } + if (actionCounter == 0) { + currentException.setDescription("list is currently empty!"); + throw currentException; + } + } + } + From 77ef37e975a94bb919d1c9e40450459ce3d30b10 Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Fri, 3 Mar 2023 04:31:54 +0800 Subject: [PATCH 14/21] Implemented JavaDoc and other Ui debugging and formatted strings. --- src/main/java/Duke.java | 10 ++ src/main/java/file/storage/Storage.java | 60 +++++++--- src/main/java/parser/DukeSession.java | 70 +++++++----- src/main/java/tasks/Deadline.java | 39 ++++++- src/main/java/tasks/Event.java | 44 +++++++- src/main/java/tasks/Task.java | 68 +++++++++-- src/main/java/tasks/Todo.java | 34 +++++- src/main/java/utility/DukeException.java | 28 ++++- src/main/java/utility/Ui.java | 130 ++++++++++++++++++---- src/main/java/utility/commandChecker.java | 109 ++++++++++++------ 10 files changed, 473 insertions(+), 119 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 7ffd58c16..be3e1f6d5 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,17 @@ import parser.DukeSession; import utility.Ui; +/** + * Represents the Main of the entire project, a chatbot named DUKE. + * It is made simple to understand the flow of the whole project. + */ public class Duke { + + /** + * Calls the session to start and initialises the session. + * + * @param args Contains any information the user inputs when running the process in the terminal. + */ public static void main(String[] args) { DukeSession dukeSession = new DukeSession(); Ui.printGreetings(); diff --git a/src/main/java/file/storage/Storage.java b/src/main/java/file/storage/Storage.java index 298c51aa0..fe85aae4a 100644 --- a/src/main/java/file/storage/Storage.java +++ b/src/main/java/file/storage/Storage.java @@ -6,15 +6,28 @@ import java.util.ArrayList; import java.util.Scanner; - import tasks.Task; import tasks.Todo; import tasks.Event; import tasks.Deadline; +import utility.Ui; +/** + * Represents the Storage class, where it is used to deal with file and storage matters. + * It sets up the locally stored file first. + * It will check for the file in the default save path. If it does not exist, it will create the file and directory. + * If the local copy exists, it will load it and be incorporated into this session (DukeSession). + * After the user exits, it will also save the file to the default save path. + */ public class Storage { - final static String DEFAULT_SAVE_PATH = "data/savedList.txt"; - final static String DEFAULT_DIRECTORY_NAME = "data"; + private static final String DEFAULT_SAVE_PATH = "data/savedList.txt"; + private static final String DEFAULT_DIRECTORY_NAME = "data"; + private static final String FILE_CORRUPTED_MESSAGE = "File has been corrupted"; + private static final String ERROR_MESSAGE = "Something went wrong: "; + private static final String DEFAULT_TODO_SYMBOL = "t"; + private static final String DEFAULT_DEADLINE_SYMBOL = "d"; + private static final String DEFAULT_EVENT_SYMBOL = "e"; + private static final String DEFAULT_MARKED_TASK_SYMBOL = "X"; private static File directoryAndFileChecker() throws IOException { File f = new File(DEFAULT_SAVE_PATH); @@ -26,11 +39,20 @@ private static File directoryAndFileChecker() throws IOException { if (!f.exists()) { f.createNewFile(); } - } catch (IOException e){ - System.out.println("Something went wrong: " + e.getMessage()); + } catch (IOException e) { + System.out.println(ERROR_MESSAGE + e.getMessage()); } return f; } + + /** + * It will call the method directoryAndFileChecker to ensure that all file and directory conflicts are resolved. + * After that, it reads the file and parses the data it loaded. + * Other setup methods are called to load different tasks and ensure that this session is same as the saved + * local text file. + * + * @param actions This contains the arraylist of Tasks. + */ public static void setUpFile(ArrayList actions) { try { File f = directoryAndFileChecker(); @@ -39,41 +61,47 @@ public static void setUpFile(ArrayList actions) { Scanner in = new Scanner(f); while (in.hasNext()) { line = in.nextLine(); - decisions = line.split("/"); + decisions = line.split(Ui.DEFAULT_FLAG_SEPARATOR); switch (decisions[0]) { - case "t": + case DEFAULT_TODO_SYMBOL: setUpToDo(decisions, actions); break; - case "d": + case DEFAULT_DEADLINE_SYMBOL: setUpDeadline(decisions, actions); break; - case "e": + case DEFAULT_EVENT_SYMBOL: setUpEvent(decisions, actions); break; default: - System.out.println("File has been corrupted"); + Ui.print(FILE_CORRUPTED_MESSAGE); } } } catch (IOException e) { - System.out.println("Something went wrong: " + e.getMessage()); + System.out.println(ERROR_MESSAGE + e.getMessage()); } } + /** + * Saves any recorded information of this current DukeSession. + * The arraylist in DukeSession is saved into a default save path in a text file. + * + * @param actions This contains the arraylist of Tasks. + */ public static void saveFile(ArrayList actions) { try { FileWriter fw = new FileWriter(DEFAULT_SAVE_PATH); for (Task i : actions) { - fw.write(i.getCommand() + System.lineSeparator()); + fw.write(i.getSaveCommand() + System.lineSeparator()); } fw.close(); } catch (IOException e) { - System.out.println("Something went wrong: " + e.getMessage()); + System.out.println(ERROR_MESSAGE + e.getMessage()); } } private static void setUpToDo(String[] decisions, ArrayList actions) { Task toBeAdded = new Todo(decisions[2]); - if (decisions[1].equals("X")) { + if (decisions[1].equals(DEFAULT_MARKED_TASK_SYMBOL)) { toBeAdded.mark(); } actions.add(toBeAdded); @@ -81,7 +109,7 @@ private static void setUpToDo(String[] decisions, ArrayList actions) { private static void setUpDeadline(String[] decisions, ArrayList actions) { Task toBeAdded = new Deadline(decisions[2], decisions[3]); - if (decisions[1].equals("X")) { + if (decisions[1].equals(DEFAULT_MARKED_TASK_SYMBOL)) { toBeAdded.mark(); } actions.add(toBeAdded); @@ -89,7 +117,7 @@ private static void setUpDeadline(String[] decisions, ArrayList actions) { private static void setUpEvent(String[] decisions, ArrayList actions) { Task toBeAdded = new Event(decisions[2], decisions[3], decisions[4]); - if (decisions[1].equals("X")) { + if (decisions[1].equals(DEFAULT_MARKED_TASK_SYMBOL)) { toBeAdded.mark(); } actions.add(toBeAdded); diff --git a/src/main/java/parser/DukeSession.java b/src/main/java/parser/DukeSession.java index b51a01438..435e91b28 100644 --- a/src/main/java/parser/DukeSession.java +++ b/src/main/java/parser/DukeSession.java @@ -1,6 +1,7 @@ package parser; import file.storage.Storage; + import utility.Ui; import utility.commandChecker; @@ -12,14 +13,33 @@ import java.util.Scanner; import java.util.ArrayList; - +/** + * Represents the whole session of Duke's communication. + * It creates an arraylist that stores all tasks that the user has given in this session. + * It also parses the input that the user has entered. After that, it sends the broken down inputs to + * the commandChecker class, where the inputs are validated. + * If there are no errors in the input, it is executed. + */ public class DukeSession { + private static final String INPUT_ERROR_DETECTED = "Invalidate"; + private ArrayList actions = new ArrayList<>(); + /** + * Calls the function setUpFile in the Storage class. It is used here to ensure arraylist is already setup. + */ public void setUpArrayList() { Storage.setUpFile(actions); } + /** + * This method parses the inputs into decisions and dates. + * It then calls the method of commandChecker, hasErrors. + * hasErrors will then validate the inputs and then notify DukeSession of any errors caught. + * If there are no errors detected, it will call the method handleInputs where they are processed. + * If errors are detected, user input is discarded. + * + */ public void execute() { String line; String[] decisions; @@ -27,48 +47,48 @@ public void execute() { Scanner in = new Scanner(System.in); do { line = in.nextLine(); - decisions = line.split(" "); - dates = line.split("/"); + decisions = line.split(Ui.DEFAULT_LINE_SEPARATOR); + dates = line.split(Ui.DEFAULT_FLAG_SEPARATOR); commandChecker currentLoop = new commandChecker(decisions, dates, actions.size()); if (currentLoop.hasErrors()) { - decisions[0] = "Invalidate"; + decisions[0] = INPUT_ERROR_DETECTED; } handleInputs(line, decisions, dates); - } while (!decisions[0].equals("bye")); + } while (!decisions[0].equals(Ui.DEFAULT_EXIT)); Storage.saveFile(actions); - System.out.println("That's all from me! Goodbye!"); + Ui.printExitMessage(); } private void handleInputs(String line, String[] decisions, String[] dates) { switch (decisions[0]) { - case "echo": + case Ui.DEFAULT_ECHO: System.out.println(findTaskDetails(line)); break; - case "todo": + case Ui.DEFAULT_TODO: handleToDo(line); break; - case "event": + case Ui.DEFAULT_EVENT: handleEvent(dates); break; - case "deadline": + case Ui.DEFAULT_DEADLINE: handleDeadline(dates); break; - case "mark": + case Ui.DEFAULT_MARK_TASK: handleMarkTask(decisions); break; - case "unmark": + case Ui.DEFAULT_UNMARK_TASK: handleUnmarkTask(decisions); break; - case "list": + case Ui.DEFAULT_LIST_ALL_TASKS: handleList(); break; - case "delete": + case Ui.DEFAULT_DELETE: handleDeleteTask(decisions); break; - case "find": + case Ui.DEFAULT_FIND: handleFindTask(line); break; - case "bye": + case Ui.DEFAULT_EXIT: break; default: Ui.printCurrentSupportedActions(); @@ -119,28 +139,28 @@ private void handleDeleteTask(String[] decisions) { private void handleFindTask(String line) { String termToFind = findTaskDetails(line); - Ui.print(termToFind); int iterator = 0; - int printedTasks = 0; + int numberOfPrintedTasks = 0; for (Task searchTerm : actions) { - if (searchTerm.getDescription().contains(termToFind)) { + boolean containsMatchingTerm = searchTerm.getDescription().toLowerCase().contains(termToFind.toLowerCase()); + if (containsMatchingTerm) { Ui.printListElement(iterator, actions.get(iterator)); - printedTasks++; + numberOfPrintedTasks++; } iterator++; } - if (printedTasks > 0) { + if (numberOfPrintedTasks > 0) { Ui.printFindAcknowledgement(); - } else if (printedTasks == 0) { + } else if (numberOfPrintedTasks == 0) { Ui.printCannotFindAcknowledgement(); } else { - Ui.print("Oh dear, Something has went wrong!"); + Ui.printDefaultErrorMessage(); } } private static String findTaskDetails(String line) { - return line.substring(line.indexOf(" ") + 1); + int correctLineIndex = line.indexOf(Ui.DEFAULT_LINE_SEPARATOR) + 1; + return line.substring(correctLineIndex); } - } diff --git a/src/main/java/tasks/Deadline.java b/src/main/java/tasks/Deadline.java index 0d1a12bbd..d247849be 100644 --- a/src/main/java/tasks/Deadline.java +++ b/src/main/java/tasks/Deadline.java @@ -1,21 +1,52 @@ package tasks; +import utility.Ui; + +/** + * Represents the class Deadline, which inherits from the parent class Task. + * When created, it is stored in the arraylist in DukeSession. + * It is a task, which contains a description and 1 date field, /by. + * E.g. deadline Submit this Assignment /by Mar 3rd 2359 + */ public class Deadline extends Task { + private static final String DEFAULT_DEADLINE_SYMBOL = "[D]"; + private static final String DEFAULT_BY_FORMATTER = "(by: "; + private static final String END_BRACKET = ")"; + private static final String DEFAULT_DEADLINE_SAVE_SYMBOL = "d/"; private String by; + /** + * Initialises an object of the Class Deadline. + * + * @param description Contains the description of the deadline that the user wants to do. + * @param by Contains the time or date of the due date of the deadline task. + */ public Deadline(String description, String by) { super(description); this.by = by; } + /** + * Returns a string that contains the information of the description of the deadline task. + * It also contains information about whether it is marked, and the due date. + * It is properly formatted so that the user will be able to understand it. + * + * @return Returns a formatted string of the deadline task that you wish to display to the user. + */ @Override public String toString() { - return "[D]" + super.toString() + "(by: " + by + ")"; + return DEFAULT_DEADLINE_SYMBOL + super.toString() + DEFAULT_BY_FORMATTER + by + END_BRACKET; } - - public String getCommand() { - return "d/" + this.getStatusIcon() + "/" + this.getDescription() + "/" + this.by; + /** + * Returns a string that is formatted specifically to save it into a local text file. + * + * @return Returns a formatted string of the deadline task that you wish to save. + */ + public String getSaveCommand() { + return DEFAULT_DEADLINE_SAVE_SYMBOL + this.getStatusIcon() + + Ui.DEFAULT_FLAG_SEPARATOR + this.getDescription() + + Ui.DEFAULT_FLAG_SEPARATOR + this.by; } } \ No newline at end of file diff --git a/src/main/java/tasks/Event.java b/src/main/java/tasks/Event.java index 5880efdb3..4cdbd1fd2 100644 --- a/src/main/java/tasks/Event.java +++ b/src/main/java/tasks/Event.java @@ -1,23 +1,59 @@ package tasks; +import utility.Ui; + +/** + * Represents the class Event, which inherits from the parent class Task. + * When created, it is stored in the arraylist in DukeSession. + * It is a task, which contains a description and 2 date fields, /from and /to. + * E.g. event attend CS2113T tutorial /from thursday 9am /to 10am. + */ public class Event extends Task { + private static final String DEFAULT_EVENT_SYMBOL = "[E]"; + private static final String DEFAULT_FROM_FORMATTER = "from: "; + private static final String DEFAULT_TO_FORMATTER = "to: "; + private static final String DEFAULT_EVENT_SAVE_SYMBOL = "e/"; + private static final String OPEN_BRACKET = "("; + private static final String CLOSE_BRACKET = ")"; private String from; + private String to; + /** + * Initialises an object of the Class Event. + * + * @param description Contains the description of the event that the user wants to do. + * @param from Contains the starting date or time of the event. + * @param to Contains the ending date or time of the event. + */ public Event(String description, String from, String to) { super(description); this.from = from; this.to = to; } + /** + * Returns a string that contains the information of the description of the event + * It also contains information about whether it is marked and the start and end of the event. + * It is properly formatted so that the user will be able to understand it. + * + * @return Returns a formatted string of the event that you wish to display to the user. + */ @Override public String toString() { - return "[E]" + super.toString() + "(from: " + from + "to: " + to + ")"; - + return DEFAULT_EVENT_SYMBOL + super.toString() + OPEN_BRACKET + DEFAULT_FROM_FORMATTER + from + + DEFAULT_TO_FORMATTER + to + CLOSE_BRACKET; } - public String getCommand() { - return "e/" + this.getStatusIcon() + "/" + this.getDescription() + "/" + this.from + "/" + this.to ; + /** + * Returns a string that is formatted specifically to save the event into a local text file. + * + * @return Returns a formatted string of the event that you wish to save. + */ + public String getSaveCommand() { + return DEFAULT_EVENT_SAVE_SYMBOL + this.getStatusIcon() + Ui.DEFAULT_FLAG_SEPARATOR + this.getDescription() + + Ui.DEFAULT_FLAG_SEPARATOR + this.from + + Ui.DEFAULT_FLAG_SEPARATOR + this.to; } } diff --git a/src/main/java/tasks/Task.java b/src/main/java/tasks/Task.java index 782530080..0bbddb91c 100644 --- a/src/main/java/tasks/Task.java +++ b/src/main/java/tasks/Task.java @@ -1,36 +1,86 @@ package tasks; +import utility.Ui; + +/** + * Represents the parent class Task, which is a parent to Todo, Event, Deadline. + * It contains a description, and another isDone variable that indicates whether the task is done (or marked). + * There are also methods to produce a string that is properly formatted to the user or to save in storage. + */ public class Task { + private static final String DEFAULT_MARKED_TASK_SYMBOL = "X"; + private static final String START_BRACKET = "["; + private static final String END_BRACKET = "] "; + private String description; - private boolean isDone; - private String command; + private boolean isMarked; + + private String saveCommand; + + /** + * Initialises an object of the class Task. + * + * @param description Contains the details of the description of the task. + */ public Task(String description) { this.description = description; - this.isDone = false; + this.isMarked = false; } + /** + * Returns a string that shows if the task is marked by the user. + * + * @return The string that shows if it is marked or not. + */ public String getStatusIcon() { - return (isDone ? "X" : " "); // mark done task with X + return (isMarked ? DEFAULT_MARKED_TASK_SYMBOL : Ui.DEFAULT_LINE_SEPARATOR); } + /** + * Allows the user to mark the tasks. + * The mark and unmark method is used specifically so that users can mark or unmark tasks more simply. + * By using setMark and getMark, it will allow the user to unmark a marked task accidentally. + * + * Can be performed even if the task is already marked. + */ public void mark() { - this.isDone = true; + this.isMarked = true; } + /** + * Allows the user to unmark the tasks. + * Can be performed even if the task is already unmarked. + */ public void unmark() { - this.isDone = false; + this.isMarked = false; } + /** + * Returns the description of the task. + * + * @return Returns the string that contains the description. + */ public String getDescription() { return (this.description); } + /** + * Returns a string that contains the information of the description of the task and also whether it is marked. + * It is properly formatted so that the user will be able to understand it. + * + * @return Returns a formatted string of the task that you wish to display to the user. + */ public String toString() { - return ("[" + this.getStatusIcon() + "] " + this.getDescription()); + return (START_BRACKET + this.getStatusIcon() + END_BRACKET + this.getDescription()); } - public String getCommand() { - return this.command; + /** + * Returns a string that is formatted specifically to save it into a local text file. + * + * @return Returns a formatted string of the task that you wish to save. + */ + public String getSaveCommand() { + return this.saveCommand; } } \ No newline at end of file diff --git a/src/main/java/tasks/Todo.java b/src/main/java/tasks/Todo.java index aa1f63efd..b847232a8 100644 --- a/src/main/java/tasks/Todo.java +++ b/src/main/java/tasks/Todo.java @@ -1,20 +1,44 @@ package tasks; +import utility.Ui; + +/** + * Represents the class ToDo, which inherits from the parent class Task. + * When created, it is stored in the arraylist in DukeSession. + * It is a task, which only contains a description. + * E.g. todo Say Goodnight To Duke + */ public class Todo extends Task { + private static final String DEFAULT_TODO_SYMBOL = "[T]"; + private static final String DEFAULT_TODO_SAVE_SYMBOL = "t/"; + /** + * Initialises an object of the Class Todo. + * + * @param description Contains the description of the task that the user wants to do. + */ public Todo(String description) { super(description); } + + /** + * Returns the formatted string that you wish to display to the user. + * + * @return Formatted string of a Todo task. + */ @Override public String toString() { - return "[T]" + super.toString(); + return DEFAULT_TODO_SYMBOL + super.toString(); } - - public String getCommand() { - return "t/" + this.getStatusIcon() + "/"+ this.getDescription(); + /** + * Returns the formatted string that you wish to save to the local text file. + * + * @return The formatted string of a Todo task that you wish to save. + */ + public String getSaveCommand() { + return DEFAULT_TODO_SAVE_SYMBOL + this.getStatusIcon() + Ui.DEFAULT_FLAG_SEPARATOR + this.getDescription(); } - } diff --git a/src/main/java/utility/DukeException.java b/src/main/java/utility/DukeException.java index eddbaf903..8715a29f6 100644 --- a/src/main/java/utility/DukeException.java +++ b/src/main/java/utility/DukeException.java @@ -1,20 +1,46 @@ package utility; +/** + * Represents a custom Exception class for Duke. It contains a description of the errors thrown by Duke. + * The description will describe the error found, and it will be displayed to the user. + */ public class DukeException extends Exception { + private static final String DEFAULT_EMPTY_DESCRIPTION = "No Description"; + public String description; + /** + * Creates an object of the class DukeException. + * This has no inputs and a default empty description is applied. + */ public DukeException() { - this.description = "No Description"; + this.description = DEFAULT_EMPTY_DESCRIPTION; } + /** + * Creates an object of the class DukeException. + * This has inputs, and it will be stored into the description. + * + * @param description This will contain the description of the error found in commandChecker. + */ public DukeException(String description) { this.description = description; } + /** + * Sets the description of the error to be stored in DukeException. + * + * @param description Contains the description of the error given in commandChecker. + */ public void setDescription(String description) { this.description = description; } + /** + * Sets the description of the error to be stored in DukeException. + * + * @return description of the error stored in DukeException. + */ public String getDescription() { return this.description; } diff --git a/src/main/java/utility/Ui.java b/src/main/java/utility/Ui.java index cbbd66c2e..2642a743d 100644 --- a/src/main/java/utility/Ui.java +++ b/src/main/java/utility/Ui.java @@ -2,52 +2,140 @@ import tasks.Task; +/** + * A class in charge of interacting with the user. + * It also contains common messages or String literals that are commonly used throughout Duke. + * All methods are public and static. + */ public class Ui { + private static final String DEFAULT_LOGO = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n" + + "__________________________\n"; + private static final String GREETING_MESSAGE = "Hello! Do you need " + + "anything from me?\n" + + "Once my owner is more proficient in what he does, he will give me more functions!\n"; + private static final String LIST_OF_SUPPORTED_FUNCTIONS = "I am currently only able to do: \n " + + "1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n 8)find\n" + + "When you wish to exit, do tell me by typing : bye"; + private static final String DEFAULT_LIST_FORMATTING = ") "; + private static final String DEFAULT_ACKNOWLEDGEMENT = "Done!"; + private static final String DEFAULT_TASK_ADDED_ACKNOWLEDGEMENT = "Got it. I've added this task:\n"; + private static final String DEFAULT_TASK_DELETED_ACKNOWLEDGEMENT = "Got it. I've removed this task\n"; + private static final String START_OF_USER_REMINDER = "Now you have "; + private static final String END_OF_USER_REMINDER = " tasks in the list."; + private static final String DEFAULT_FAILED_TO_FIND_MESSAGE = "I am sorry, but I did not find any matches."; + private static final String DEFAULT_ERROR_MESSAGE = "Oh dear, Something has went wrong!"; + private static final String DEFAULT_EXIT_MESSAGE = "That's all from me! Goodbye!"; + public static final String DEFAULT_ECHO = "echo"; + public static final String DEFAULT_TODO = "todo"; + public static final String DEFAULT_EVENT = "event"; + public static final String DEFAULT_DEADLINE = "deadline"; + public static final String DEFAULT_MARK_TASK = "mark"; + public static final String DEFAULT_UNMARK_TASK = "unmark"; + public static final String DEFAULT_LIST_ALL_TASKS = "list"; + public static final String DEFAULT_DELETE = "delete"; + public static final String DEFAULT_FIND = "find"; + public static final String DEFAULT_EXIT = "bye"; + public static final String DEFAULT_LINE_SEPARATOR = " "; + public static final String DEFAULT_FLAG_SEPARATOR = "/"; + + /** + * Prints the standard greeting for users to see. + */ public static void printGreetings() { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n" - + "__________________________\n"; - System.out.println("Hello from\n" + logo); - System.out.println("Hello! Do you need anything from me?\n" - + "I have only been trained to greet, echo and list you so far.\n" - + "Once my owner is more proficient in what he does, he will give me more functions!\n" - + " 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n 8)find\n" - + "When you wish to exit, do tell me by typing : bye"); + Ui.print(DEFAULT_LOGO + GREETING_MESSAGE + LIST_OF_SUPPORTED_FUNCTIONS); } + /** + * A shorter printing method call that utilises System.out.print + * + * @param input String that contains what is going to be printed for the user. + */ public static void print(String input) { System.out.println(input); } + /** + * Prints an element of a list of Tasks with proper formatting. + * + * @param iterator The iterator or index or the list you are accessing. + * @param action The list of Tasks that the user wishes to do. + */ public static void printListElement(int iterator, Task action) { - Ui.print(iterator + 1 + ")" + action.toString()); + int correctListElementNumber = iterator + 1; + Ui.print(correctListElementNumber + DEFAULT_LIST_FORMATTING + action.toString()); } + /** + * Prints the acknowledgement message when a task is marked or unmarked. + * + * @param action The list of Tasks that the user wishes to do. + */ public static void printDoneMarkingTasks(Task action) { - Ui.print("Done!\n" + action.toString()); + Ui.print(DEFAULT_ACKNOWLEDGEMENT + System.lineSeparator() + action.toString()); } - - + /** + * It prints the acknowledgement after a task has been added. + * It then reminds the user how many tasks are currently in the list. + * + * @param action The list of Tasks that the user wishes to do. + * @param actionCounter The current size of the list action. + */ public static void printAcknowledgement(Task action, int actionCounter) { - System.out.println("Got it. I've added this task:\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); + Ui.print(DEFAULT_TASK_ADDED_ACKNOWLEDGEMENT + action.toString() + System.lineSeparator() + + START_OF_USER_REMINDER + actionCounter + END_OF_USER_REMINDER); } + /** + * Prints the message containing the currently supported actions. + */ public static void printCurrentSupportedActions() { - Ui.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n 8)find\n"); + Ui.print(LIST_OF_SUPPORTED_FUNCTIONS); } + + /** + * It prints the acknowledgement after a task has been deleted. + * It then reminds the user how many tasks are currently in the list. + * + * @param action The list of Tasks that the user wishes to do. + * @param actionCounter The current size of the list action. + */ public static void printDeleteAcknowledgement(Task action, int actionCounter) { - print("Got it. I've removed this task\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); + Ui.print(DEFAULT_TASK_DELETED_ACKNOWLEDGEMENT + action.toString() + System.lineSeparator() + + START_OF_USER_REMINDER + actionCounter + END_OF_USER_REMINDER); } + /** + * It prints the acknowledgement after a relevant find/search is done successfully. + */ public static void printFindAcknowledgement() { - Ui.print("Here you go!"); + Ui.print(DEFAULT_ACKNOWLEDGEMENT); } + + /** + * It prints the acknowledgement after a relevant find/search is done but was unsuccessful. + */ public static void printCannotFindAcknowledgement() { - Ui.print("I am sorry, but I did not find any matches."); + Ui.print(DEFAULT_FAILED_TO_FIND_MESSAGE); + } + + /** + * It prints the acknowledgement after the exit command is called. + * It also says goodbye to the user. + */ + public static void printExitMessage() { + Ui.print(DEFAULT_EXIT_MESSAGE); } + /** + * It prints the default error message when something has gone wrong. + * It is used when the commandChecker fails to figure out what went wrong. + */ + public static void printDefaultErrorMessage() { + Ui.print(DEFAULT_ERROR_MESSAGE); + } } diff --git a/src/main/java/utility/commandChecker.java b/src/main/java/utility/commandChecker.java index db1cbbcca..f9f14e4ae 100644 --- a/src/main/java/utility/commandChecker.java +++ b/src/main/java/utility/commandChecker.java @@ -1,163 +1,204 @@ package utility; +/** + * Represents a class used to check for proper usage of commands. + * It serves to accept the inputs after it has been broken down, and check for relevant fields to be filled. + * When an error is detected, it will throw a custom exception: DukeException, which contains the relevant information. + */ public class commandChecker { + private static final String DEFAULT_EMPTY_DESCRIPTION = "No Description"; + private static final String DEFAULT_CAUGHT_ERROR_MESSAGE = "Invalid input, please try again! Error Description: "; + private static final String DEFAULT_INPUT_ERROR_MESSAGE = "Invalid action word! e.g. echo, todo, list, etc."; + private static final String DEFAULT_MISSING_DESCRIPTION_MESSAGE = " description cannot be empty!"; + private static final String DEFAULT_EVENT_ERROR_MESSAGE = "event needs a description and /from date /to date"; + private static final String DEFAULT_DEADLINE_ERROR_MESSAGE = "deadline needs to have a description and /by date"; + private static final String DEFAULT_MARKING_ERROR = "you may only mark or unmark one task at a time"; + private static final String DEFAULT_LIST_EMPTY_ERROR = "list is currently empty!"; + private static final String DEFAULT_DELETE_ERROR = "you may only delete one task at a time"; + private static final String DEFAULT_OUT_OF_BOUND_ERROR = "out of bounds!"; + private final String[] decisions; + private final String[] dates; + private final int actionCounter; + private boolean hasErrorFlags = false; + /** + * Initialise the commandChecker class. It takes in filtered and broken down inputs that are parsed through + * by DukeSession. + * + * @param decisions The parsed input that contains important information about the type of action. + * @param dates The parsed input that contains important information about the dates of the task. + * @param actionCounter The current size of the list action. + */ public commandChecker(String[] decisions, String[] dates, int actionCounter) { this.decisions = decisions; this.dates = dates; this.actionCounter = actionCounter; } + /** + * Returns a flag of whether errors have been detected in the user's input. + * It calls the method validate command, which will be throwing the exception DukeException. + * This method is called for every user input in DukeSession. + * + * @return True or False of whether there are any errors detected. + */ public boolean hasErrors() { try { validateCommand(); } catch (DukeException e) { this.hasErrorFlags = true; - if (!e.getDescription().equals("No Description")) { - Ui.print("Invalid input, please try again! Error Description: " + e.getDescription() + System.lineSeparator()); + boolean isErrorMessageSameAsDefault = e.getDescription().equals(DEFAULT_EMPTY_DESCRIPTION); + if (!isErrorMessageSameAsDefault) { + Ui.print(DEFAULT_CAUGHT_ERROR_MESSAGE + e.getDescription() + System.lineSeparator()); } - } finally { - return this.hasErrorFlags; } + return this.hasErrorFlags; } + /** + * Validates the user input according to the correct action word. + * It will throw an exception when the custom exception class DukeException is captured. + * + * @throws DukeException if input has an incorrect action word or missing description or dates. + */ private void validateCommand() throws DukeException { DukeException currentException = new DukeException(); switch (decisions[0]) { - case "echo": + case Ui.DEFAULT_ECHO: validateEcho(currentException); break; - case "todo": + case Ui.DEFAULT_TODO: validateToDo(currentException); break; - case "event": + case Ui.DEFAULT_EVENT: validateEvent(currentException); break; - case "deadline": + case Ui.DEFAULT_DEADLINE: validateDeadline(currentException); break; - case "mark": + case Ui.DEFAULT_MARK_TASK: validateMarkTask(currentException); break; - case "unmark": + case Ui.DEFAULT_UNMARK_TASK: validateUnmarkTask(currentException); break; - case "list": + case Ui.DEFAULT_LIST_ALL_TASKS: validateList(currentException); break; - case "delete": + case Ui.DEFAULT_DELETE: validateDeleteTask(currentException); break; - case "find": + case Ui.DEFAULT_FIND: validateFindTask(currentException); break; - case "bye": + case Ui.DEFAULT_EXIT: break; default: - currentException.setDescription("Invalid action word! e.g. echo, todo, list, etc."); + currentException.setDescription(DEFAULT_INPUT_ERROR_MESSAGE); throw currentException; } } private void validateEcho(DukeException currentException) throws DukeException { if (decisions.length < 2) { - currentException.setDescription("echo description cannot be empty!"); + currentException.setDescription(Ui.DEFAULT_ECHO + DEFAULT_MISSING_DESCRIPTION_MESSAGE); throw currentException; } } private void validateToDo(DukeException currentException) throws DukeException { if (decisions.length < 2) { - currentException.setDescription("todo description cannot be empty!"); + currentException.setDescription(Ui.DEFAULT_TODO + DEFAULT_MISSING_DESCRIPTION_MESSAGE); throw currentException; } } private void validateEvent(DukeException currentException) throws DukeException { if (dates.length < 3) { - currentException.setDescription("event needs to have exactly two dates, /from date /to date"); + currentException.setDescription(DEFAULT_EVENT_ERROR_MESSAGE); throw currentException; } } private void validateDeadline(DukeException currentException) throws DukeException { if (dates.length < 2) { - currentException.setDescription("deadline needs to have exactly one date, /by"); + currentException.setDescription(DEFAULT_DEADLINE_ERROR_MESSAGE); throw currentException; } } private void validateMarkTask(DukeException currentException) throws DukeException { if (decisions.length < 2) { - currentException.setDescription("the description of the task you wish to mark is empty!"); + currentException.setDescription(Ui.DEFAULT_MARK_TASK + DEFAULT_MISSING_DESCRIPTION_MESSAGE); throw currentException; } else if (decisions.length > 2) { - currentException.setDescription("you may only mark one task at a time"); + currentException.setDescription(DEFAULT_MARKING_ERROR); throw currentException; } if (actionCounter == 0) { - currentException.setDescription("list is currently empty!"); + currentException.setDescription(DEFAULT_LIST_EMPTY_ERROR); throw currentException; } if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { - currentException.setDescription("out of bounds!"); + currentException.setDescription(DEFAULT_OUT_OF_BOUND_ERROR); throw currentException; } } private void validateUnmarkTask(DukeException currentException) throws DukeException { if (decisions.length < 2) { - currentException.setDescription("the description of the task you wish to unmark is empty!"); + currentException.setDescription(Ui.DEFAULT_UNMARK_TASK + DEFAULT_MISSING_DESCRIPTION_MESSAGE); throw currentException; } else if (decisions.length > 2) { - currentException.setDescription("you may only unmark one task at a time"); + currentException.setDescription(DEFAULT_MARKING_ERROR); throw currentException; } if (actionCounter == 0) { - currentException.setDescription("list is currently empty!"); + currentException.setDescription(DEFAULT_LIST_EMPTY_ERROR); throw currentException; } if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { - currentException.setDescription("out of bounds!"); + currentException.setDescription(DEFAULT_OUT_OF_BOUND_ERROR); throw currentException; } } private void validateList(DukeException currentException) throws DukeException { if (actionCounter == 0) { - currentException.setDescription("list is currently empty!"); + currentException.setDescription(DEFAULT_LIST_EMPTY_ERROR); throw currentException; } } private void validateDeleteTask(DukeException currentException) throws DukeException { if (decisions.length < 2) { - currentException.setDescription("the description of the task you wish to delete is empty!"); + currentException.setDescription(Ui.DEFAULT_DELETE + DEFAULT_MISSING_DESCRIPTION_MESSAGE); throw currentException; } else if (decisions.length > 2) { - currentException.setDescription("you may only delete one task at a time"); + currentException.setDescription(DEFAULT_DELETE_ERROR); throw currentException; } if (actionCounter == 0) { - currentException.setDescription("list is currently empty!"); + currentException.setDescription(DEFAULT_LIST_EMPTY_ERROR); throw currentException; } if (Integer.parseInt(decisions[1]) > actionCounter || Integer.parseInt(decisions[1]) < 1) { - currentException.setDescription("out of bounds!"); + currentException.setDescription(DEFAULT_OUT_OF_BOUND_ERROR); throw currentException; } } + private void validateFindTask(DukeException currentException) throws DukeException { if (decisions.length < 2) { - currentException.setDescription("the description of the task you wish to find cannot be empty!"); + currentException.setDescription(Ui.DEFAULT_FIND + DEFAULT_MISSING_DESCRIPTION_MESSAGE); throw currentException; } if (actionCounter == 0) { - currentException.setDescription("list is currently empty!"); + currentException.setDescription(DEFAULT_LIST_EMPTY_ERROR); throw currentException; } } From f423633c1e8370aed4d34926e5b08a2e980015c3 Mon Sep 17 00:00:00 2001 From: geraldkoh4 <88551280+geraldkoh4@users.noreply.github.com> Date: Fri, 3 Mar 2023 06:03:17 +0800 Subject: [PATCH 15/21] Update README.md --- docs/README.md | 123 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 110 insertions(+), 13 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..37a67dcff 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,126 @@ # User Guide -## Features +## `Introduction` -### Feature-ABC +This is Duke, a chatbot. He is here to assist you in compiling a task list that you can reference to. -Description of the feature. +## `Features` -### Feature-XYZ +1) Add a Task +2) List all Tasks +3) Mark/Unmark a Task +4) Find a Task +5) Delete a Task +6) Exit -Description of the feature. +### 1.1) Add a Task (To Do) -## Usage +Add a "To Do" Task to the list. -### `Keyword` - Describe action +`Format`: todo {description} -Describe the action and its outcome. +*Sample Input Command*: `todo Fly a Kite` -Example of usage: +*Output*: +``` +Got it. I've added this task: +[T][ ] Fly a Kite +Now you have 1 tasks in the list. +``` + +### 1.2) Add a Task (Deadline) + +Add a "Deadline" Task to the list. + +**`Format`**: deadline {description} /by {time/date} + +*Sample Input Command*: **`deadline Finish My Homework /by Today`** + +*Output*: +``` +Got it. I've added this task: +[D][ ] Finish My Homework (by: Today) +Now you have 2 tasks in the list. +``` + +### 1.3) Add a Task (Event) + +Add a "Event" Task to the list. + +**`Format`**: event {description} /from {time/date} /to {time/date} + +*Sample Input Command*: **`event Play Video Games /from Today 12pm /to Today Night`** + +*Output*: +``` +Got it. I've added this task: +[E][ ] Play Video Games (from: Today 12pm to: Today Night) +Now you have 3 tasks in the list. +``` + +### 2) List all Tasks + +Lists every Task you currently have in your list. + +**`Format`**: list + +*Output*: +``` +1) [T][ ] Fly a Kite +2) [D][ ] Finish My Homework (by: Today) +3) [E][ ] Play Video Games (from: Today 12pm to: Today Night) +``` + +### 3) Mark/Unmark a Task + +Mark or unmark a Task to note if you have completed the Task through the tasknumber. + +**`Format`**: mark {tasknumber} + +*Sample Input Command*: **`mark 1`** + +*Output*: +``` +Done! +[T][X] Fly a Kite +``` + +### 4) Find a Task + +Find a task through a search criteria. + +**`Format`**: find {search criteria} + +*Sample Input Command*: **`find Play`** + +*Output*: +``` +3) [E][ ] Play Video Games (from: Today 12pm to: Today Night) +Done! +``` + +### 5) Delete a Task + +Delete a task that you no longer need through the tasknumber. + +**`Format`**: delete {tasknumber} + +*Sample Input Command*: **`delete 1`** + +*Output*: +``` +Got it. I've removed this task +[T][X] Fly a Kite +Now you have 2 tasks in the list. +``` -`keyword (optional arguments)` +### 6) Exit -Expected outcome: +Exit from the program. -Description of the outcome. +**`Format`**: bye +*Output*: ``` -expected output +That's all from me! Goodbye! ``` From cbc2097109b67b1e06d4c969772087c683d49b24 Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Fri, 3 Mar 2023 16:04:37 +0800 Subject: [PATCH 16/21] Final and Minor formatting changes. Hope everything is working well! --- src/main/java/file/storage/Storage.java | 22 ++++---- src/main/java/parser/DukeSession.java | 63 +++++++++++---------- src/main/java/tasks/Deadline.java | 2 +- src/main/java/tasks/Event.java | 4 +- src/main/java/tasks/Task.java | 2 +- src/main/java/utility/Ui.java | 7 ++- src/main/java/utility/commandChecker.java | 68 +++++++++++------------ 7 files changed, 84 insertions(+), 84 deletions(-) diff --git a/src/main/java/file/storage/Storage.java b/src/main/java/file/storage/Storage.java index fe85aae4a..f1f481e5f 100644 --- a/src/main/java/file/storage/Storage.java +++ b/src/main/java/file/storage/Storage.java @@ -63,17 +63,17 @@ public static void setUpFile(ArrayList actions) { line = in.nextLine(); decisions = line.split(Ui.DEFAULT_FLAG_SEPARATOR); switch (decisions[0]) { - case DEFAULT_TODO_SYMBOL: - setUpToDo(decisions, actions); - break; - case DEFAULT_DEADLINE_SYMBOL: - setUpDeadline(decisions, actions); - break; - case DEFAULT_EVENT_SYMBOL: - setUpEvent(decisions, actions); - break; - default: - Ui.print(FILE_CORRUPTED_MESSAGE); + case DEFAULT_TODO_SYMBOL: + setUpToDo(decisions, actions); + break; + case DEFAULT_DEADLINE_SYMBOL: + setUpDeadline(decisions, actions); + break; + case DEFAULT_EVENT_SYMBOL: + setUpEvent(decisions, actions); + break; + default: + Ui.print(FILE_CORRUPTED_MESSAGE); } } } catch (IOException e) { diff --git a/src/main/java/parser/DukeSession.java b/src/main/java/parser/DukeSession.java index 435e91b28..cb85b0cd3 100644 --- a/src/main/java/parser/DukeSession.java +++ b/src/main/java/parser/DukeSession.java @@ -38,7 +38,6 @@ public void setUpArrayList() { * hasErrors will then validate the inputs and then notify DukeSession of any errors caught. * If there are no errors detected, it will call the method handleInputs where they are processed. * If errors are detected, user input is discarded. - * */ public void execute() { String line; @@ -61,37 +60,37 @@ public void execute() { private void handleInputs(String line, String[] decisions, String[] dates) { switch (decisions[0]) { - case Ui.DEFAULT_ECHO: - System.out.println(findTaskDetails(line)); - break; - case Ui.DEFAULT_TODO: - handleToDo(line); - break; - case Ui.DEFAULT_EVENT: - handleEvent(dates); - break; - case Ui.DEFAULT_DEADLINE: - handleDeadline(dates); - break; - case Ui.DEFAULT_MARK_TASK: - handleMarkTask(decisions); - break; - case Ui.DEFAULT_UNMARK_TASK: - handleUnmarkTask(decisions); - break; - case Ui.DEFAULT_LIST_ALL_TASKS: - handleList(); - break; - case Ui.DEFAULT_DELETE: - handleDeleteTask(decisions); - break; - case Ui.DEFAULT_FIND: - handleFindTask(line); - break; - case Ui.DEFAULT_EXIT: - break; - default: - Ui.printCurrentSupportedActions(); + case Ui.DEFAULT_ECHO: + System.out.println(findTaskDetails(line)); + break; + case Ui.DEFAULT_TODO: + handleToDo(line); + break; + case Ui.DEFAULT_EVENT: + handleEvent(dates); + break; + case Ui.DEFAULT_DEADLINE: + handleDeadline(dates); + break; + case Ui.DEFAULT_MARK_TASK: + handleMarkTask(decisions); + break; + case Ui.DEFAULT_UNMARK_TASK: + handleUnmarkTask(decisions); + break; + case Ui.DEFAULT_LIST_ALL_TASKS: + handleList(); + break; + case Ui.DEFAULT_DELETE: + handleDeleteTask(decisions); + break; + case Ui.DEFAULT_FIND: + handleFindTask(line); + break; + case Ui.DEFAULT_EXIT: + break; + default: + Ui.printCurrentSupportedActions(); } } diff --git a/src/main/java/tasks/Deadline.java b/src/main/java/tasks/Deadline.java index d247849be..077b08367 100644 --- a/src/main/java/tasks/Deadline.java +++ b/src/main/java/tasks/Deadline.java @@ -20,7 +20,7 @@ public class Deadline extends Task { * Initialises an object of the Class Deadline. * * @param description Contains the description of the deadline that the user wants to do. - * @param by Contains the time or date of the due date of the deadline task. + * @param by Contains the time or date of the due date of the deadline task. */ public Deadline(String description, String by) { super(description); diff --git a/src/main/java/tasks/Event.java b/src/main/java/tasks/Event.java index 4cdbd1fd2..d0a1fe736 100644 --- a/src/main/java/tasks/Event.java +++ b/src/main/java/tasks/Event.java @@ -24,8 +24,8 @@ public class Event extends Task { * Initialises an object of the Class Event. * * @param description Contains the description of the event that the user wants to do. - * @param from Contains the starting date or time of the event. - * @param to Contains the ending date or time of the event. + * @param from Contains the starting date or time of the event. + * @param to Contains the ending date or time of the event. */ public Event(String description, String from, String to) { super(description); diff --git a/src/main/java/tasks/Task.java b/src/main/java/tasks/Task.java index 0bbddb91c..4411ef4b3 100644 --- a/src/main/java/tasks/Task.java +++ b/src/main/java/tasks/Task.java @@ -41,7 +41,7 @@ public String getStatusIcon() { * Allows the user to mark the tasks. * The mark and unmark method is used specifically so that users can mark or unmark tasks more simply. * By using setMark and getMark, it will allow the user to unmark a marked task accidentally. - * + *

* Can be performed even if the task is already marked. */ public void mark() { diff --git a/src/main/java/utility/Ui.java b/src/main/java/utility/Ui.java index 2642a743d..ae53ccd74 100644 --- a/src/main/java/utility/Ui.java +++ b/src/main/java/utility/Ui.java @@ -62,7 +62,7 @@ public static void print(String input) { * Prints an element of a list of Tasks with proper formatting. * * @param iterator The iterator or index or the list you are accessing. - * @param action The list of Tasks that the user wishes to do. + * @param action The list of Tasks that the user wishes to do. */ public static void printListElement(int iterator, Task action) { int correctListElementNumber = iterator + 1; @@ -77,11 +77,12 @@ public static void printListElement(int iterator, Task action) { public static void printDoneMarkingTasks(Task action) { Ui.print(DEFAULT_ACKNOWLEDGEMENT + System.lineSeparator() + action.toString()); } + /** * It prints the acknowledgement after a task has been added. * It then reminds the user how many tasks are currently in the list. * - * @param action The list of Tasks that the user wishes to do. + * @param action The list of Tasks that the user wishes to do. * @param actionCounter The current size of the list action. */ public static void printAcknowledgement(Task action, int actionCounter) { @@ -100,7 +101,7 @@ public static void printCurrentSupportedActions() { * It prints the acknowledgement after a task has been deleted. * It then reminds the user how many tasks are currently in the list. * - * @param action The list of Tasks that the user wishes to do. + * @param action The list of Tasks that the user wishes to do. * @param actionCounter The current size of the list action. */ public static void printDeleteAcknowledgement(Task action, int actionCounter) { diff --git a/src/main/java/utility/commandChecker.java b/src/main/java/utility/commandChecker.java index f9f14e4ae..645647029 100644 --- a/src/main/java/utility/commandChecker.java +++ b/src/main/java/utility/commandChecker.java @@ -29,8 +29,8 @@ public class commandChecker { * Initialise the commandChecker class. It takes in filtered and broken down inputs that are parsed through * by DukeSession. * - * @param decisions The parsed input that contains important information about the type of action. - * @param dates The parsed input that contains important information about the dates of the task. + * @param decisions The parsed input that contains important information about the type of action. + * @param dates The parsed input that contains important information about the dates of the task. * @param actionCounter The current size of the list action. */ public commandChecker(String[] decisions, String[] dates, int actionCounter) { @@ -68,38 +68,38 @@ public boolean hasErrors() { private void validateCommand() throws DukeException { DukeException currentException = new DukeException(); switch (decisions[0]) { - case Ui.DEFAULT_ECHO: - validateEcho(currentException); - break; - case Ui.DEFAULT_TODO: - validateToDo(currentException); - break; - case Ui.DEFAULT_EVENT: - validateEvent(currentException); - break; - case Ui.DEFAULT_DEADLINE: - validateDeadline(currentException); - break; - case Ui.DEFAULT_MARK_TASK: - validateMarkTask(currentException); - break; - case Ui.DEFAULT_UNMARK_TASK: - validateUnmarkTask(currentException); - break; - case Ui.DEFAULT_LIST_ALL_TASKS: - validateList(currentException); - break; - case Ui.DEFAULT_DELETE: - validateDeleteTask(currentException); - break; - case Ui.DEFAULT_FIND: - validateFindTask(currentException); - break; - case Ui.DEFAULT_EXIT: - break; - default: - currentException.setDescription(DEFAULT_INPUT_ERROR_MESSAGE); - throw currentException; + case Ui.DEFAULT_ECHO: + validateEcho(currentException); + break; + case Ui.DEFAULT_TODO: + validateToDo(currentException); + break; + case Ui.DEFAULT_EVENT: + validateEvent(currentException); + break; + case Ui.DEFAULT_DEADLINE: + validateDeadline(currentException); + break; + case Ui.DEFAULT_MARK_TASK: + validateMarkTask(currentException); + break; + case Ui.DEFAULT_UNMARK_TASK: + validateUnmarkTask(currentException); + break; + case Ui.DEFAULT_LIST_ALL_TASKS: + validateList(currentException); + break; + case Ui.DEFAULT_DELETE: + validateDeleteTask(currentException); + break; + case Ui.DEFAULT_FIND: + validateFindTask(currentException); + break; + case Ui.DEFAULT_EXIT: + break; + default: + currentException.setDescription(DEFAULT_INPUT_ERROR_MESSAGE); + throw currentException; } } From c9f98f4809525574d233c3693410ff67dd5641aa Mon Sep 17 00:00:00 2001 From: geraldkoh4 <88551280+geraldkoh4@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:22:51 +0800 Subject: [PATCH 17/21] Update README.md --- docs/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/README.md b/docs/README.md index 37a67dcff..d3bee1e22 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,12 +6,12 @@ This is Duke, a chatbot. He is here to assist you in compiling a task list that ## `Features` -1) Add a Task -2) List all Tasks -3) Mark/Unmark a Task -4) Find a Task -5) Delete a Task -6) Exit +1) Add a Task +2) List all Tasks +3) Mark/Unmark a Task +4) Find a Task +5) Delete a Task +6) Exit ### 1.1) Add a Task (To Do) From 78316921ab3713227cf946ef75205f519533888b Mon Sep 17 00:00:00 2001 From: "DESKTOP-VAFN8PH\\Gerald" Date: Fri, 3 Mar 2023 22:07:26 +0800 Subject: [PATCH 18/21] Minor Bug fixes --- src/main/java/utility/commandChecker.java | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main/java/utility/commandChecker.java b/src/main/java/utility/commandChecker.java index 645647029..1e3e8c4b2 100644 --- a/src/main/java/utility/commandChecker.java +++ b/src/main/java/utility/commandChecker.java @@ -1,5 +1,6 @@ package utility; + /** * Represents a class used to check for proper usage of commands. * It serves to accept the inputs after it has been broken down, and check for relevant fields to be filled. @@ -17,6 +18,17 @@ public class commandChecker { private static final String DEFAULT_DELETE_ERROR = "you may only delete one task at a time"; private static final String DEFAULT_OUT_OF_BOUND_ERROR = "out of bounds!"; + private static final String DEFAULT_DEADLINE_FLAG_ERROR = "Please use / only for flags! " + + "e.g. deadline {description} /by {date/time}"; + + private static final String DEFAULT_EVENT_FLAG_ERROR = "Please use / only for flags! " + + "e.g event {description} /from {date/time} /to {date/time} "; + private static final String DEFAULT_DEADLINE_FLAG = "by "; + + private static final String DEFAULT_EVENT_FROM_FLAG = "from "; + + private static final String DEFAULT_EVENT_TO_FLAG = "to "; + private final String[] decisions; private final String[] dates; @@ -122,6 +134,10 @@ private void validateEvent(DukeException currentException) throws DukeException currentException.setDescription(DEFAULT_EVENT_ERROR_MESSAGE); throw currentException; } + if (!isCorrectEventFlags()) { + currentException.setDescription(DEFAULT_EVENT_FLAG_ERROR); + throw currentException; + } } private void validateDeadline(DukeException currentException) throws DukeException { @@ -129,6 +145,10 @@ private void validateDeadline(DukeException currentException) throws DukeExcepti currentException.setDescription(DEFAULT_DEADLINE_ERROR_MESSAGE); throw currentException; } + if (!isCorrectDeadlineFlag()) { + currentException.setDescription(DEFAULT_DEADLINE_FLAG_ERROR); + throw currentException; + } } private void validateMarkTask(DukeException currentException) throws DukeException { @@ -203,5 +223,32 @@ private void validateFindTask(DukeException currentException) throws DukeExcepti } } + private boolean isCorrectDeadlineFlag() { + boolean hasCorrectFlag = false; + try { + String flagChecker = dates[1].substring(0, 3); + if (flagChecker.equals(DEFAULT_DEADLINE_FLAG)) { + hasCorrectFlag = true; + } + } catch (StringIndexOutOfBoundsException e) { + return false; + } + return hasCorrectFlag; + } + + private boolean isCorrectEventFlags() { + boolean hasCorrectFlags = false; + try { + String fromFlagChecker = dates[1].substring(0, 5); + String toFlagChecker = dates[2].substring(0, 3); + if (fromFlagChecker.equals(DEFAULT_EVENT_FROM_FLAG) && toFlagChecker.equals(DEFAULT_EVENT_TO_FLAG)) { + hasCorrectFlags = true; + } + } catch (StringIndexOutOfBoundsException e) { + return false; + } + return hasCorrectFlags; + } + } From 6df56eeb56c56d70980d7c1d752131471970119a Mon Sep 17 00:00:00 2001 From: geraldkoh4 <88551280+geraldkoh4@users.noreply.github.com> Date: Fri, 3 Mar 2023 22:15:55 +0800 Subject: [PATCH 19/21] Update README.md --- docs/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/README.md b/docs/README.md index d3bee1e22..f2be0bc54 100644 --- a/docs/README.md +++ b/docs/README.md @@ -124,3 +124,29 @@ Exit from the program. ``` That's all from me! Goodbye! ``` + +# Note + +## Test - Echo + +Echo is a command that you can use to test if the chatbot is responding! + +**`Format`**: echo {description} + +*Sample Input Command*: **`echo 1`** + +*Output*: +``` +1 +``` +# Additional Note + +## Flags + +Do not use the character '/'. It is solely reserved for flags. +Example of the flags include: +Deadline by flag: "/by " +Event from flag: "/from " +Event to flag: "/to " + +Thank you! From f4792f0d2405e62cbe157fbad2875d8aeb675aa1 Mon Sep 17 00:00:00 2001 From: geraldkoh4 <88551280+geraldkoh4@users.noreply.github.com> Date: Fri, 3 Mar 2023 22:16:37 +0800 Subject: [PATCH 20/21] Update README.md --- docs/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/README.md b/docs/README.md index f2be0bc54..261fca78e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -144,9 +144,9 @@ Echo is a command that you can use to test if the chatbot is responding! ## Flags Do not use the character '/'. It is solely reserved for flags. -Example of the flags include: -Deadline by flag: "/by " -Event from flag: "/from " -Event to flag: "/to " +Example of the flags include: +Deadline by flag: "/by " +Event from flag: "/from " +Event to flag: "/to " Thank you! From a7d571ed18d4d1d20c090f3acf402a12dba58867 Mon Sep 17 00:00:00 2001 From: geraldkoh4 <88551280+geraldkoh4@users.noreply.github.com> Date: Fri, 3 Mar 2023 22:26:10 +0800 Subject: [PATCH 21/21] Update README.md --- docs/README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 261fca78e..717657da7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,6 +4,13 @@ This is Duke, a chatbot. He is here to assist you in compiling a task list that you can reference to. +## `Download` + +1) Make sure that your computer supports Java 11 or above. +2) Download this [jar file](https://github.com/geraldkoh4/ip/releases/download/A-Release/Duke.jar). +3) Copy the file path. +4) Go into your console and enter `"java -jar {file path}"`. + ## `Features` 1) Add a Task @@ -17,9 +24,9 @@ This is Duke, a chatbot. He is here to assist you in compiling a task list that Add a "To Do" Task to the list. -`Format`: todo {description} +**`Format`**: todo {description} -*Sample Input Command*: `todo Fly a Kite` +*Sample Input Command*: **`todo Fly a Kite`** *Output*: ``` @@ -139,7 +146,6 @@ Echo is a command that you can use to test if the chatbot is responding! ``` 1 ``` -# Additional Note ## Flags