From 60a88c03c8ac490eb689aa7235a2c11265938af3 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Sun, 15 Aug 2021 16:26:09 +0800 Subject: [PATCH 01/30] Level-0: Greet --- src/main/java/Duke.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..3c80db1f8 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,11 @@ public class Duke { public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + String text = "____________________________________________________________\n" + + " Hello! I'm Duke\n" + + " What can I do for you?\n" + + "____________________________________________________________\n" + + " Bye. Hope to see you again soon!\n" + + "____________________________________________________________"; + System.out.println(text); } } From 8747f622c6e6a5d7e9d365e9c794af1179070874 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Wed, 25 Aug 2021 14:47:36 +0800 Subject: [PATCH 02/30] Level-1 --- src/main/java/Duke.java | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3c80db1f8..26192606c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,11 +1,27 @@ +import java.util.Scanner; + public class Duke { public static void main(String[] args) { - String text = "____________________________________________________________\n" - + " Hello! I'm Duke\n" - + " What can I do for you?\n" - + "____________________________________________________________\n" - + " Bye. Hope to see you again soon!\n" - + "____________________________________________________________"; - System.out.println(text); + Scanner in = new Scanner(System.in); + String linebreak = " ____________________________________________________________\n"; + String greetings = linebreak + + " Welcome to the Duke Chatbot\n" + + " What would you like to do today?\n" + + linebreak; + String farewell = " Bye. Hope to see you again soon!\n" + + linebreak; + + System.out.println(greetings); + while (true) { + String command = in.nextLine(); + if (command.equals("bye")) { + System.out.println(farewell); + break; + } + System.out.println(linebreak); + System.out.println(" " + command); + System.out.println(linebreak); + } + } } From ab4602c6028aef00173068c5e2eda3b51c3c5524 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Wed, 25 Aug 2021 16:06:55 +0800 Subject: [PATCH 03/30] Level 2 --- src/main/java/Duke.java | 25 +++++---------------- src/main/java/TaskManager.java | 20 +++++++++++++++++ src/main/java/UI.java | 40 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 src/main/java/TaskManager.java create mode 100644 src/main/java/UI.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 26192606c..649d36345 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,26 +2,13 @@ public class Duke { public static void main(String[] args) { - Scanner in = new Scanner(System.in); - String linebreak = " ____________________________________________________________\n"; - String greetings = linebreak - + " Welcome to the Duke Chatbot\n" - + " What would you like to do today?\n" - + linebreak; - String farewell = " Bye. Hope to see you again soon!\n" - + linebreak; + TaskManager taskmanager = new TaskManager(); + Scanner scanner = new Scanner(System.in); + UI ui = new UI(taskmanager, scanner); + ui.start(); + + - System.out.println(greetings); - while (true) { - String command = in.nextLine(); - if (command.equals("bye")) { - System.out.println(farewell); - break; - } - System.out.println(linebreak); - System.out.println(" " + command); - System.out.println(linebreak); - } } } diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java new file mode 100644 index 000000000..cf430731d --- /dev/null +++ b/src/main/java/TaskManager.java @@ -0,0 +1,20 @@ +import java.util.ArrayList; + +public class TaskManager { + private ArrayList toDoList; + + public TaskManager() { + this.toDoList = new ArrayList<>(); + } + + public void add(String task) { + toDoList.add(task); + } + + + public void list() { + for (int i = 0; i < toDoList.size(); i++) { + System.out.println(" " + (i + 1) + ". " + toDoList.get(i)); + } + } +} diff --git a/src/main/java/UI.java b/src/main/java/UI.java new file mode 100644 index 000000000..31d618c17 --- /dev/null +++ b/src/main/java/UI.java @@ -0,0 +1,40 @@ +import java.util.Scanner; + +public class UI { + private TaskManager taskmanager; + private Scanner scanner; + + public UI(TaskManager taskmanager, Scanner scanner) { + this.taskmanager = taskmanager; + this.scanner = scanner; + } + + public void start() { + String linebreak = " ____________________________________________________________\n"; + String greetings = linebreak + + " Welcome to the Duke Chatbot\n" + + " What would you like to do today?\n" + + linebreak; + String farewell = " Bye. Hope to see you again soon!\n" + + linebreak; + + System.out.println(greetings); + + while (true) { + String command = scanner.nextLine(); + switch (command) { + case "bye": + System.out.println(farewell); + break; + case "list": + System.out.print(linebreak); + taskmanager.list(); + System.out.println(linebreak); + break; + default: + System.out.println(linebreak + " added: " + command + "\n" + linebreak); + taskmanager.add(command); + } + } + } +} From b43adfa99a0c4a3add3699b8a7da02cbf0987c03 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Wed, 25 Aug 2021 18:13:22 +0800 Subject: [PATCH 04/30] Level-3 --- src/main/java/Duke.java | 4 ---- src/main/java/Task.java | 17 +++++++++++++++++ src/main/java/TaskManager.java | 17 ++++++++++++++--- src/main/java/UI.java | 27 ++++++++++++++++++--------- 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 649d36345..4de8493a5 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,9 +6,5 @@ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); UI ui = new UI(taskmanager, scanner); ui.start(); - - - - } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..82e59613b --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,17 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public void taskDone() { + this.isDone = true; + } + + public String getName() { + return this.description; + } +} diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index cf430731d..8295b8950 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -1,20 +1,31 @@ import java.util.ArrayList; public class TaskManager { - private ArrayList toDoList; + private ArrayList toDoList; public TaskManager() { this.toDoList = new ArrayList<>(); } - public void add(String task) { + public void add(Task task) { toDoList.add(task); } + public void checkDone(int tasknumber) { + toDoList.get(tasknumber - 1).taskDone(); + } + + public String getName(int index) { + return toDoList.get(index - 1).getName(); + } public void list() { for (int i = 0; i < toDoList.size(); i++) { - System.out.println(" " + (i + 1) + ". " + toDoList.get(i)); + if (toDoList.get(i).isDone) { + System.out.println(" " + (i + 1) + ".[X] " + toDoList.get(i).getName()); + } else { + System.out.println(" " + (i + 1) + ".[ ] " + toDoList.get(i).getName()); + } } } } diff --git a/src/main/java/UI.java b/src/main/java/UI.java index 31d618c17..7dcadf2eb 100644 --- a/src/main/java/UI.java +++ b/src/main/java/UI.java @@ -15,26 +15,35 @@ public void start() { + " Welcome to the Duke Chatbot\n" + " What would you like to do today?\n" + linebreak; - String farewell = " Bye. Hope to see you again soon!\n" - + linebreak; - + String farewell = " Bye. Hope to see you again soon!"; System.out.println(greetings); while (true) { - String command = scanner.nextLine(); - switch (command) { + String input = scanner.nextLine(); + String command[] = input.split(" "); + String firstword = command[0]; + Task t = new Task(input); + + System.out.print(linebreak); + switch (firstword) { case "bye": System.out.println(farewell); break; case "list": - System.out.print(linebreak); + System.out.println(" Here are the tasks in your list:"); taskmanager.list(); - System.out.println(linebreak); + break; + case "done": + System.out.println(" Nice! I've marked this task as done: "); + int tasknumber = Integer.parseInt(command[1]); + System.out.println(" [X] " + taskmanager.getName(tasknumber)); + taskmanager.checkDone(tasknumber); break; default: - System.out.println(linebreak + " added: " + command + "\n" + linebreak); - taskmanager.add(command); + System.out.println(" added: " + input); + taskmanager.add(t); } + System.out.print(linebreak); } } } From 68d69b3ad268361810f906e3857bafa5f2b65604 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 26 Aug 2021 13:24:14 +0800 Subject: [PATCH 05/30] A-CodingStandard Tweaked code to comply with coding standard Added javadocs for non-trivial methods and classes Added logo --- src/main/java/Duke.java | 4 +-- src/main/java/TaskManager.java | 20 ++++++++++-- src/main/java/UI.java | 49 ---------------------------- src/main/java/Ui.java | 58 ++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 53 deletions(-) delete mode 100644 src/main/java/UI.java create mode 100644 src/main/java/Ui.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4de8493a5..c9a3bebff 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,9 +2,9 @@ public class Duke { public static void main(String[] args) { - TaskManager taskmanager = new TaskManager(); + TaskManager taskManager = new TaskManager(); Scanner scanner = new Scanner(System.in); - UI ui = new UI(taskmanager, scanner); + Ui ui = new Ui(taskManager, scanner); ui.start(); } } diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 8295b8950..ac8d27b68 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -7,18 +7,34 @@ public TaskManager() { this.toDoList = new ArrayList<>(); } + /** + * Adds task to toDoList ArrayList + * @param task Task to be checked + */ public void add(Task task) { toDoList.add(task); } - public void checkDone(int tasknumber) { - toDoList.get(tasknumber - 1).taskDone(); + /** + * Checks if specified task is done + * @param taskNumber task number of task to be checked + */ + public void checkDone(int taskNumber) { + toDoList.get(taskNumber - 1).taskDone(); } + /** + * Returns name of task given task number + * @param index index of task in toDoList ArrayList + * @return Name of Task + */ public String getName(int index) { return toDoList.get(index - 1).getName(); } + /** + * Prints out all the tasks and if they have been done + */ public void list() { for (int i = 0; i < toDoList.size(); i++) { if (toDoList.get(i).isDone) { diff --git a/src/main/java/UI.java b/src/main/java/UI.java deleted file mode 100644 index 7dcadf2eb..000000000 --- a/src/main/java/UI.java +++ /dev/null @@ -1,49 +0,0 @@ -import java.util.Scanner; - -public class UI { - private TaskManager taskmanager; - private Scanner scanner; - - public UI(TaskManager taskmanager, Scanner scanner) { - this.taskmanager = taskmanager; - this.scanner = scanner; - } - - public void start() { - String linebreak = " ____________________________________________________________\n"; - String greetings = linebreak - + " Welcome to the Duke Chatbot\n" - + " What would you like to do today?\n" - + linebreak; - String farewell = " Bye. Hope to see you again soon!"; - System.out.println(greetings); - - while (true) { - String input = scanner.nextLine(); - String command[] = input.split(" "); - String firstword = command[0]; - Task t = new Task(input); - - System.out.print(linebreak); - switch (firstword) { - case "bye": - System.out.println(farewell); - break; - case "list": - System.out.println(" Here are the tasks in your list:"); - taskmanager.list(); - break; - case "done": - System.out.println(" Nice! I've marked this task as done: "); - int tasknumber = Integer.parseInt(command[1]); - System.out.println(" [X] " + taskmanager.getName(tasknumber)); - taskmanager.checkDone(tasknumber); - break; - default: - System.out.println(" added: " + input); - taskmanager.add(t); - } - System.out.print(linebreak); - } - } -} diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 000000000..ad809b7ad --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,58 @@ +import java.util.Scanner; + +public class Ui { + private TaskManager taskManager; + private Scanner scanner; + + public Ui(TaskManager taskManager, Scanner scanner) { + this.taskManager = taskManager; + this.scanner = scanner; + } + + public void start() { + String lineBreak = " ____________________________________________________________\n"; + String logo = "\n" + + " \n" + + " ,--. ,------. ,--. ,--. ,--. \n" + + ",-' '-.,---.| .-. \\ ,---.| | `--',---,-' '-. \n" + + "'-. .-| .-. | | \\ | .-. | | ,--( .-'-. .-' \n" + + " | | ' '-' | '--' ' '-' | '--| .-' `)| | \n" + + " `--' `---'`-------' `---'`-----`--`----' `--' \n" + + " \n"; + String greetings = lineBreak + + logo + + " Welcome to the toDoList Chatbot\n" + + " What would you like to do today?\n" + + lineBreak; + String farewell = " Bye. Hope to see you again soon!"; + System.out.println(greetings); + + while (true) { + String input = scanner.nextLine(); + String command[] = input.split(" "); + String firstWord = command[0]; + Task t = new Task(input); + + System.out.print(lineBreak); + switch (firstWord) { + case "bye": + System.out.println(farewell); + break; + case "list": + System.out.println(" Here are the tasks in your list:"); + taskManager.list(); + break; + case "done": + System.out.println(" Nice! I've marked this task as done: "); + int taskNumber = Integer.parseInt(command[1]); + System.out.println(" [X] " + taskManager.getName(taskNumber)); + taskManager.checkDone(taskNumber); + break; + default: + System.out.println(" added: " + input); + taskManager.add(t); + } + System.out.print(lineBreak); + } + } +} From c3cd436ecc7bae788104d7a0ceb41bba94533460 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 26 Aug 2021 22:15:17 +0800 Subject: [PATCH 06/30] Minor typo changes --- src/main/java/TaskManager.java | 3 +++ src/main/java/Ui.java | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index ac8d27b68..de76ad005 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -9,6 +9,7 @@ public TaskManager() { /** * Adds task to toDoList ArrayList + * * @param task Task to be checked */ public void add(Task task) { @@ -17,6 +18,7 @@ public void add(Task task) { /** * Checks if specified task is done + * * @param taskNumber task number of task to be checked */ public void checkDone(int taskNumber) { @@ -25,6 +27,7 @@ public void checkDone(int taskNumber) { /** * Returns name of task given task number + * * @param index index of task in toDoList ArrayList * @return Name of Task */ diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index ad809b7ad..ca571641e 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -29,7 +29,7 @@ public void start() { while (true) { String input = scanner.nextLine(); - String command[] = input.split(" "); + String[] command = input.split(" "); String firstWord = command[0]; Task t = new Task(input); From 6b638842aff15eded4992afc6bcb5952c417b36a Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 2 Sep 2021 13:06:34 +0800 Subject: [PATCH 07/30] Level-4 --- src/main/java/Deadline.java | 22 +++++++ src/main/java/Event.java | 20 ++++++ src/main/java/Task.java | 20 +++++- src/main/java/TaskManager.java | 109 ++++++++++++++++++++++++++++----- src/main/java/ToDo.java | 13 ++++ src/main/java/Ui.java | 10 +-- 6 files changed, 172 insertions(+), 22 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.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..99c9f427a --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,22 @@ +public class Deadline extends Task { + + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + public String getIcon() { + return "[D]"; + } + + public String getBy() { + return "(by:" + by + ")"; + } + + @Override + public String toString() { + return getIcon() + super.toString() + "(by:" + by + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..577bfee2a --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,20 @@ +public class Event extends Task{ + protected String at; + + public Event(String description, String at) { + super(description); + this.at = at; + } + + public String getIcon() { + return "[E]"; + } + public String getAt() { + return "(at:" + at + ")"; + } + + @Override + public String toString() { + return getIcon() + super.toString() + "(at:" + at + ")"; + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 82e59613b..74627955f 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -11,7 +11,23 @@ public void taskDone() { this.isDone = true; } - public String getName() { - return this.description; + public String getStatusIcon() { + return (isDone ? "[X]" : "[ ]"); } + public String getIcon() { + return ""; + } + public String getBy() { + return ""; + } + public String getAt() { + return ""; + } + + + public String toString() { + return getStatusIcon() + " " + this.description ; + } + + } diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index de76ad005..8500c57e1 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -1,19 +1,43 @@ import java.util.ArrayList; public class TaskManager { - private ArrayList toDoList; + private ArrayList taskList; public TaskManager() { - this.toDoList = new ArrayList<>(); + this.taskList = new ArrayList<>(); } /** - * Adds task to toDoList ArrayList + * Adds task to taskList ArrayList * * @param task Task to be checked */ - public void add(Task task) { - toDoList.add(task); + public void add(String task) { + String taskType = getCommand(task); + Task newTask; + String description = getDescription(task); + switch(taskType) { + case "todo": + newTask = new ToDo(description); + taskList.add(newTask); + break; + case "deadline": + String by = getDate(task); + newTask = new Deadline(description, by); + taskList.add(newTask); + break; + case "event": + String at = getDate(task); + newTask = new Event(description, at); + taskList.add(newTask); + break; + default: + System.out.println(" Invalid command, please try again"); + return; + } + + System.out.println(" Got it. I've added this task:\n " + newTask); + printSize(); } /** @@ -22,29 +46,84 @@ public void add(Task task) { * @param taskNumber task number of task to be checked */ public void checkDone(int taskNumber) { - toDoList.get(taskNumber - 1).taskDone(); + taskList.get(taskNumber - 1).taskDone(); + } + + public String getDescription(String task) { + String description; + int separator; + if (getCommand(task).equals("todo")) { + description = task.substring(5); + } else if (getCommand(task).equals("deadline")) { + separator = task.indexOf("/by"); + description = task.substring(9, separator); + } else if (getCommand(task).equals("event")){ + separator = task.indexOf("/at"); + description = task.substring(6, separator); + } else { + description = null; + } + return description; + } + + public String getCommand(String task) { + String[] command = task.split(" "); + String taskType = command[0]; + + return taskType; + } + public void printSize() { + if (getSize() == 1) { + System.out.println(" Now you have " + 1 + " task in the list."); + } else { + System.out.println(" Now you have " + getSize() + " tasks in the list."); + } } /** * Returns name of task given task number * - * @param index index of task in toDoList ArrayList + * @param index index of task in taskList ArrayList * @return Name of Task */ public String getName(int index) { - return toDoList.get(index - 1).getName(); + return taskList.get(index - 1).toString(); } - /** - * Prints out all the tasks and if they have been done - */ + + + public int getSize() { + return taskList.size(); + } + + public void list() { - for (int i = 0; i < toDoList.size(); i++) { - if (toDoList.get(i).isDone) { - System.out.println(" " + (i + 1) + ".[X] " + toDoList.get(i).getName()); + for (int i = 0; i < taskList.size(); i++) { + Task t = taskList.get(i); + if (t.getIcon().equals("[D]")) { + System.out.println(" " + (i + 1) + "." + t); + } else if (t.getIcon().equals("[E]")) { + System.out.println(" " + (i + 1) + "." + t); } else { - System.out.println(" " + (i + 1) + ".[ ] " + toDoList.get(i).getName()); + System.out.println(" " + (i + 1) + "." + t); } } } + + + + public String getDate(String description) { + String date; + if (description.contains("/by")) { + int indexOfSeparator = description.indexOf("/by"); + date = description.substring(indexOfSeparator + 3); + } else if (description.contains("/at")) { + int indexOfSeparator = description.indexOf("/at"); + date = description.substring(indexOfSeparator + 3); + } else { + date = null; + } + return date; + } + } diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java new file mode 100644 index 000000000..fa0af7026 --- /dev/null +++ b/src/main/java/ToDo.java @@ -0,0 +1,13 @@ +public class ToDo extends Task{ + public ToDo(String description) { + super(description); + } + + public String getIcon() { + return "[T]"; + } + @Override + public String toString() { + return getIcon() + super.toString(); + } +} diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index ca571641e..cfa0a21c3 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -31,13 +31,13 @@ public void start() { String input = scanner.nextLine(); String[] command = input.split(" "); String firstWord = command[0]; - Task t = new Task(input); + //Task t = new Task(input); System.out.print(lineBreak); switch (firstWord) { case "bye": System.out.println(farewell); - break; + return; case "list": System.out.println(" Here are the tasks in your list:"); taskManager.list(); @@ -45,12 +45,12 @@ public void start() { case "done": System.out.println(" Nice! I've marked this task as done: "); int taskNumber = Integer.parseInt(command[1]); - System.out.println(" [X] " + taskManager.getName(taskNumber)); taskManager.checkDone(taskNumber); + System.out.println(" " + taskManager.getName(taskNumber)); break; + default: - System.out.println(" added: " + input); - taskManager.add(t); + taskManager.add(input); } System.out.print(lineBreak); } From 9089498edde2d9f92b6ed732ffcbdc31a8d7ac88 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 2 Sep 2021 13:20:10 +0800 Subject: [PATCH 08/30] A-CodeQuality --- src/main/java/Deadline.java | 10 +++++----- src/main/java/Event.java | 12 ++++++------ src/main/java/Task.java | 5 +---- src/main/java/TaskManager.java | 9 +-------- src/main/java/Ui.java | 1 - 5 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 99c9f427a..c19584f14 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,22 +1,22 @@ public class Deadline extends Task { - protected String by; + protected String timing; public Deadline(String description, String by) { super(description); - this.by = by; + this.timing = by; } public String getIcon() { return "[D]"; } - public String getBy() { - return "(by:" + by + ")"; + public String getTiming() { + return "(by:" + timing + ")"; } @Override public String toString() { - return getIcon() + super.toString() + "(by:" + by + ")"; + return getIcon() + super.toString() + getTiming(); } } \ No newline at end of file diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 577bfee2a..2f20527d9 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,20 +1,20 @@ public class Event extends Task{ - protected String at; + protected String timing; - public Event(String description, String at) { + public Event(String description, String timing) { super(description); - this.at = at; + this.timing = timing; } public String getIcon() { return "[E]"; } - public String getAt() { - return "(at:" + at + ")"; + public String getTiming() { + return "(at:" + timing + ")"; } @Override public String toString() { - return getIcon() + super.toString() + "(at:" + at + ")"; + return getIcon() + super.toString() + getTiming(); } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 74627955f..a4406b0d9 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -17,10 +17,7 @@ public String getStatusIcon() { public String getIcon() { return ""; } - public String getBy() { - return ""; - } - public String getAt() { + public String getTiming() { return ""; } diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 8500c57e1..21ee8c302 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -100,18 +100,11 @@ public int getSize() { public void list() { for (int i = 0; i < taskList.size(); i++) { Task t = taskList.get(i); - if (t.getIcon().equals("[D]")) { - System.out.println(" " + (i + 1) + "." + t); - } else if (t.getIcon().equals("[E]")) { - System.out.println(" " + (i + 1) + "." + t); - } else { - System.out.println(" " + (i + 1) + "." + t); - } + System.out.println(" " + (i + 1) + "." + t); } } - public String getDate(String description) { String date; if (description.contains("/by")) { diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index cfa0a21c3..408f4bb1d 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -31,7 +31,6 @@ public void start() { String input = scanner.nextLine(); String[] command = input.split(" "); String firstWord = command[0]; - //Task t = new Task(input); System.out.print(lineBreak); switch (firstWord) { From 5aab948a91dc9cf323a0d870ad9f4ffb7ab6fcd2 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 9 Sep 2021 01:40:18 +0800 Subject: [PATCH 09/30] Level 5. Handle Errors --- src/main/java/Duke.java | 2 +- src/main/java/DukeException.java | 3 ++ src/main/java/TaskManager.java | 70 +++++++++++++++++++++++--------- src/main/java/Ui.java | 43 ++++++++++++-------- 4 files changed, 80 insertions(+), 38 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index c9a3bebff..091fec01e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,7 @@ import java.util.Scanner; public class Duke { - public static void main(String[] args) { + public static void main(String[] args) throws DukeException { TaskManager taskManager = new TaskManager(); Scanner scanner = new Scanner(System.in); Ui ui = new Ui(taskManager, scanner); diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..6b2ab1bca --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,3 @@ +public class DukeException extends Exception { + +} \ No newline at end of file diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 21ee8c302..977f7842d 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -12,11 +12,15 @@ public TaskManager() { * * @param task Task to be checked */ - public void add(String task) { + public void add(String task) throws DukeException { + String[] command = task.split(" "); + if (command.length < 2) { + throw new DukeException(); + } String taskType = getCommand(task); Task newTask; String description = getDescription(task); - switch(taskType) { + switch (taskType) { case "todo": newTask = new ToDo(description); taskList.add(newTask); @@ -40,13 +44,15 @@ public void add(String task) { printSize(); } - /** - * Checks if specified task is done - * - * @param taskNumber task number of task to be checked - */ - public void checkDone(int taskNumber) { - taskList.get(taskNumber - 1).taskDone(); + + + public void checkDone(String[] command) { + try { + Integer.parseInt(command[1]); + } catch (NumberFormatException e) { + throw new NumberFormatException(); + } + taskList.get(Integer.parseInt(command[1]) - 1).taskDone(); } public String getDescription(String task) { @@ -57,7 +63,7 @@ public String getDescription(String task) { } else if (getCommand(task).equals("deadline")) { separator = task.indexOf("/by"); description = task.substring(9, separator); - } else if (getCommand(task).equals("event")){ + } else if (getCommand(task).equals("event")) { separator = task.indexOf("/at"); description = task.substring(6, separator); } else { @@ -72,6 +78,7 @@ public String getCommand(String task) { return taskType; } + public void printSize() { if (getSize() == 1) { System.out.println(" Now you have " + 1 + " task in the list."); @@ -91,32 +98,55 @@ public String getName(int index) { } - public int getSize() { return taskList.size(); } public void list() { - for (int i = 0; i < taskList.size(); i++) { - Task t = taskList.get(i); - System.out.println(" " + (i + 1) + "." + t); + try { + if (taskList.size() == 0) { + System.out.println(" ☹ OOPS!!! List is empty"); + return; + } + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < taskList.size(); i++) { + Task t = taskList.get(i); + System.out.println(" " + (i + 1) + "." + t); + } + } catch (IndexOutOfBoundsException i) { + System.out.println("Error! Please contact admin"); } + } public String getDate(String description) { String date; - if (description.contains("/by")) { - int indexOfSeparator = description.indexOf("/by"); + String taskType = getCommand(description); + int indexOfSeparator; + switch (taskType) { + case "deadline": + indexOfSeparator = description.indexOf("/by"); date = description.substring(indexOfSeparator + 3); - } else if (description.contains("/at")) { - int indexOfSeparator = description.indexOf("/at"); - date = description.substring(indexOfSeparator + 3); - } else { + if (date.isEmpty()) { + System.out.println(" ☹ OOPS!!! Please enter a date"); + + } + break; + case "event": + indexOfSeparator = description.indexOf("/at"); + date = description.substring(indexOfSeparator + 3); + if (date.isEmpty()) { + System.out.println(" ☹ OOPS!!! Please enter a date"); + } + break; + default: date = null; + break; } return date; } + } diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 408f4bb1d..8b50f932a 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -33,23 +33,32 @@ public void start() { String firstWord = command[0]; System.out.print(lineBreak); - switch (firstWord) { - case "bye": - System.out.println(farewell); - return; - case "list": - System.out.println(" Here are the tasks in your list:"); - taskManager.list(); - break; - case "done": - System.out.println(" Nice! I've marked this task as done: "); - int taskNumber = Integer.parseInt(command[1]); - taskManager.checkDone(taskNumber); - System.out.println(" " + taskManager.getName(taskNumber)); - break; - - default: - taskManager.add(input); + try { + switch (firstWord) { + case "bye": + System.out.println(farewell); + return; + case "list": + taskManager.list(); + break; + case "done": + int taskNumber = Integer.parseInt(command[1]); + System.out.println(" Nice! I've marked this task as done: "); + taskManager.checkDone(command); + System.out.println(" " + taskManager.getName(taskNumber)); + break; + case "todo": + case "deadline": + case "event": + taskManager.add(input); + break; + default: + System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + } + } catch (DukeException e) { + System.out.println(" ☹ OOPS!!! The description of a " + firstWord + " cannot be empty."); + } catch (NumberFormatException e) { + System.out.println(" ☹ OOPS!!! The task's index should be an integer."); } System.out.print(lineBreak); } From 5d11a58ae357da6545eaa0c81fc41d0b381c2c39 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 9 Sep 2021 01:42:24 +0800 Subject: [PATCH 10/30] bug fix --- src/main/java/Ui.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 408f4bb1d..a11b40c5d 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -31,7 +31,6 @@ public void start() { String input = scanner.nextLine(); String[] command = input.split(" "); String firstWord = command[0]; - System.out.print(lineBreak); switch (firstWord) { case "bye": From 753e0a8d9f752011003db89b100898f179545b01 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 9 Sep 2021 12:30:36 +0800 Subject: [PATCH 11/30] Formatting issues --- src/main/java/Duke.java | 2 +- src/main/java/Event.java | 1 + src/main/java/Task.java | 3 ++- src/main/java/TaskManager.java | 25 +++++++++++++------------ 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 091fec01e..c9a3bebff 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,7 @@ import java.util.Scanner; public class Duke { - public static void main(String[] args) throws DukeException { + public static void main(String[] args) { TaskManager taskManager = new TaskManager(); Scanner scanner = new Scanner(System.in); Ui ui = new Ui(taskManager, scanner); diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 2f20527d9..049b6c815 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -9,6 +9,7 @@ public Event(String description, String timing) { public String getIcon() { return "[E]"; } + public String getTiming() { return "(at:" + timing + ")"; } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index a4406b0d9..f50ac4f8f 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -14,14 +14,15 @@ public void taskDone() { public String getStatusIcon() { return (isDone ? "[X]" : "[ ]"); } + public String getIcon() { return ""; } + public String getTiming() { return ""; } - public String toString() { return getStatusIcon() + " " + this.description ; } diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 977f7842d..058e6c896 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -39,13 +39,10 @@ public void add(String task) throws DukeException { System.out.println(" Invalid command, please try again"); return; } - System.out.println(" Got it. I've added this task:\n " + newTask); printSize(); } - - public void checkDone(String[] command) { try { Integer.parseInt(command[1]); @@ -72,9 +69,14 @@ public String getDescription(String task) { return description; } - public String getCommand(String task) { - String[] command = task.split(" "); - String taskType = command[0]; + /** + * Returns type of Task given command + * @param command + * @return Type of Task + */ + public String getCommand(String command) { + String[] task = command.split(" "); + String taskType = task[0]; return taskType; } @@ -97,12 +99,10 @@ public String getName(int index) { return taskList.get(index - 1).toString(); } - public int getSize() { return taskList.size(); } - public void list() { try { if (taskList.size() == 0) { @@ -120,7 +120,11 @@ public void list() { } - + /** + * Returns date of event or deadline + * @param description command entered + * @return Date of event or deadline + */ public String getDate(String description) { String date; String taskType = getCommand(description); @@ -131,7 +135,6 @@ public String getDate(String description) { date = description.substring(indexOfSeparator + 3); if (date.isEmpty()) { System.out.println(" ☹ OOPS!!! Please enter a date"); - } break; case "event": @@ -147,6 +150,4 @@ public String getDate(String description) { } return date; } - - } From 326a6f296a1ad31743e7c933527378e8eb4d5658 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Fri, 10 Sep 2021 12:29:15 +0800 Subject: [PATCH 12/30] Refactor packages --- src/main/java/{ => duke}/DukeException.java | 2 ++ src/main/java/{ => duke/command}/Duke.java | 5 +++++ src/main/java/{ => duke/command}/Ui.java | 4 ++++ src/main/java/{ => duke/task}/Deadline.java | 2 ++ src/main/java/{ => duke/task}/Event.java | 4 +++- src/main/java/{ => duke/task}/Task.java | 2 ++ src/main/java/{ => duke/task}/TaskManager.java | 4 ++++ src/main/java/{ => duke/task}/ToDo.java | 4 +++- 8 files changed, 25 insertions(+), 2 deletions(-) rename src/main/java/{ => duke}/DukeException.java (76%) rename src/main/java/{ => duke/command}/Duke.java (83%) rename src/main/java/{ => duke/command}/Ui.java (97%) rename src/main/java/{ => duke/task}/Deadline.java (95%) rename src/main/java/{ => duke/task}/Event.java (88%) rename src/main/java/{ => duke/task}/Task.java (96%) rename src/main/java/{ => duke/task}/TaskManager.java (98%) rename src/main/java/{ => duke/task}/ToDo.java (81%) diff --git a/src/main/java/DukeException.java b/src/main/java/duke/DukeException.java similarity index 76% rename from src/main/java/DukeException.java rename to src/main/java/duke/DukeException.java index 6b2ab1bca..f09d58afb 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/duke/DukeException.java @@ -1,3 +1,5 @@ +package duke; + public class DukeException extends Exception { } \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/duke/command/Duke.java similarity index 83% rename from src/main/java/Duke.java rename to src/main/java/duke/command/Duke.java index c9a3bebff..b391ee690 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/duke/command/Duke.java @@ -1,5 +1,10 @@ +package duke.command; + +import duke.task.TaskManager; + import java.util.Scanner; + public class Duke { public static void main(String[] args) { TaskManager taskManager = new TaskManager(); diff --git a/src/main/java/Ui.java b/src/main/java/duke/command/Ui.java similarity index 97% rename from src/main/java/Ui.java rename to src/main/java/duke/command/Ui.java index 0d5a4a4cc..5471fadc6 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/duke/command/Ui.java @@ -1,3 +1,7 @@ +package duke.command; + +import duke.task.TaskManager; +import duke.DukeException; import java.util.Scanner; public class Ui { diff --git a/src/main/java/Deadline.java b/src/main/java/duke/task/Deadline.java similarity index 95% rename from src/main/java/Deadline.java rename to src/main/java/duke/task/Deadline.java index c19584f14..7a6c1f0ff 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,3 +1,5 @@ +package duke.task; + public class Deadline extends Task { protected String timing; diff --git a/src/main/java/Event.java b/src/main/java/duke/task/Event.java similarity index 88% rename from src/main/java/Event.java rename to src/main/java/duke/task/Event.java index 049b6c815..8f3ce1149 100644 --- a/src/main/java/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,4 +1,6 @@ -public class Event extends Task{ +package duke.task; + +public class Event extends Task { protected String timing; public Event(String description, String timing) { diff --git a/src/main/java/Task.java b/src/main/java/duke/task/Task.java similarity index 96% rename from src/main/java/Task.java rename to src/main/java/duke/task/Task.java index f50ac4f8f..7274b5edf 100644 --- a/src/main/java/Task.java +++ b/src/main/java/duke/task/Task.java @@ -1,3 +1,5 @@ +package duke.task; + public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/TaskManager.java b/src/main/java/duke/task/TaskManager.java similarity index 98% rename from src/main/java/TaskManager.java rename to src/main/java/duke/task/TaskManager.java index 058e6c896..692fc54a2 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -1,3 +1,7 @@ +package duke.task; + +import duke.DukeException; + import java.util.ArrayList; public class TaskManager { diff --git a/src/main/java/ToDo.java b/src/main/java/duke/task/ToDo.java similarity index 81% rename from src/main/java/ToDo.java rename to src/main/java/duke/task/ToDo.java index fa0af7026..4df012c4f 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -1,4 +1,6 @@ -public class ToDo extends Task{ +package duke.task; + +public class ToDo extends Task { public ToDo(String description) { super(description); } From 30ba7869a9bd0f5a7f0cf2c049ed8488c673a1c3 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Tue, 14 Sep 2021 18:03:09 +0800 Subject: [PATCH 13/30] Add Increment: Level-6 Delete --- src/main/java/duke/command/Ui.java | 75 ++++++++++------- src/main/java/duke/task/TaskManager.java | 102 ++++++++++++++++------- 2 files changed, 117 insertions(+), 60 deletions(-) diff --git a/src/main/java/duke/command/Ui.java b/src/main/java/duke/command/Ui.java index 5471fadc6..64db82750 100644 --- a/src/main/java/duke/command/Ui.java +++ b/src/main/java/duke/command/Ui.java @@ -8,53 +8,70 @@ public class Ui { private TaskManager taskManager; private Scanner scanner; + private static final String LINE = " ____________________________________________________________\n"; + private static final String LOGO = "\n" + + " \n" + + " ,--. ,------. ,--. ,--. ,--. \n" + + ",-' '-.,---.| .-. \\ ,---.| | `--',---,-' '-. \n" + + "'-. .-| .-. | | \\ | .-. | | ,--( .-'-. .-' \n" + + " | | ' '-' | '--' ' '-' | '--| .-' `)| | \n" + + " `--' `---'`-------' `---'`-----`--`----' `--' \n" + + " \n"; + private static final String GREETINGS = LINE + + LOGO + + " Welcome to the toDoList Chatbot\n" + + " What would you like to do today?\n" + + LINE; + private static final String FAREWELL = " Bye. Hope to see you again soon!"; + + private static final String TO_DO = "todo"; + private static final String DEADLINE = "deadline"; + private static final String EVENT = "event"; + private static final String BYE = "bye"; + private static final String LIST = "list"; + private static final String DONE = "done"; + private static final String DELETE = "delete"; + private static final String ADD_SUCCESS = " Nice! I've marked this task as done: "; + private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; + public Ui(TaskManager taskManager, Scanner scanner) { this.taskManager = taskManager; this.scanner = scanner; } public void start() { - String lineBreak = " ____________________________________________________________\n"; - String logo = "\n" + - " \n" + - " ,--. ,------. ,--. ,--. ,--. \n" + - ",-' '-.,---.| .-. \\ ,---.| | `--',---,-' '-. \n" + - "'-. .-| .-. | | \\ | .-. | | ,--( .-'-. .-' \n" + - " | | ' '-' | '--' ' '-' | '--| .-' `)| | \n" + - " `--' `---'`-------' `---'`-----`--`----' `--' \n" + - " \n"; - String greetings = lineBreak - + logo - + " Welcome to the toDoList Chatbot\n" - + " What would you like to do today?\n" - + lineBreak; - String farewell = " Bye. Hope to see you again soon!"; - System.out.println(greetings); - - while (true) { + System.out.println(GREETINGS); + boolean isExit = false; + while (!isExit) { String input = scanner.nextLine(); String[] command = input.split(" "); String firstWord = command[0]; - System.out.print(lineBreak); + + System.out.print(LINE); try { switch (firstWord) { - case "bye": - System.out.println(farewell); - return; - case "list": + case BYE: + System.out.println(FAREWELL); + isExit = true; + break; + case LIST: taskManager.list(); break; - case "done": + case DONE: int taskNumber = Integer.parseInt(command[1]); - System.out.println(" Nice! I've marked this task as done: "); + System.out.println(ADD_SUCCESS); taskManager.checkDone(command); System.out.println(" " + taskManager.getName(taskNumber)); break; - case "todo": - case "deadline": - case "event": + case TO_DO: + case DEADLINE: + case EVENT: taskManager.add(input); break; + case DELETE: + System.out.println(DELETE_SUCCESS); + taskManager.deleteTask(command); + break; default: System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } @@ -63,7 +80,7 @@ public void start() { } catch (NumberFormatException e) { System.out.println(" ☹ OOPS!!! The task's index should be an integer."); } - System.out.print(lineBreak); + System.out.print(LINE); } } } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 692fc54a2..e838d59ef 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -7,6 +7,17 @@ public class TaskManager { private ArrayList taskList; + private static final String TO_DO = "todo"; + private static final String DEADLINE = "deadline"; + private static final String EVENT = "event"; + private static final String ADDED_TASK = " Got it. I've added this task:\n "; + private static final String BY_SEPARATOR = "by"; + private static final String AT_SEPARATOR = "at"; + private static final int TODO_STRING_LENGTH = 5; + private static final int DEADLINE_STRING_LENGTH = 9; + private static final int EVENT_STRING_LENGTH = 6; + private static final int SEPARATOR_STRING_LENGTH = 3; + public TaskManager() { this.taskList = new ArrayList<>(); } @@ -22,31 +33,49 @@ public void add(String task) throws DukeException { throw new DukeException(); } String taskType = getCommand(task); - Task newTask; - String description = getDescription(task); switch (taskType) { - case "todo": - newTask = new ToDo(description); - taskList.add(newTask); + case TO_DO: + addToDo(task); break; - case "deadline": - String by = getDate(task); - newTask = new Deadline(description, by); - taskList.add(newTask); + case DEADLINE: + addDeadline(task); break; - case "event": - String at = getDate(task); - newTask = new Event(description, at); - taskList.add(newTask); + case EVENT: + addEvent(task); break; default: System.out.println(" Invalid command, please try again"); return; } - System.out.println(" Got it. I've added this task:\n " + newTask); printSize(); } + public void addToDo(String task) { + Task newTask; + String description = getDescription(task); + newTask = new ToDo(description); + taskList.add(newTask); + System.out.println(ADDED_TASK + newTask); + } + + public void addDeadline(String task) { + Task newTask; + String description = getDescription(task); + String by = getDate(task); + newTask = new Deadline(description, by); + taskList.add(newTask); + System.out.println(ADDED_TASK + newTask); + } + + public void addEvent(String task) { + Task newTask; + String description = getDescription(task); + String at = getDate(task); + newTask = new Event(description, at); + taskList.add(newTask); + System.out.println(ADDED_TASK + newTask); + } + public void checkDone(String[] command) { try { Integer.parseInt(command[1]); @@ -59,20 +88,22 @@ public void checkDone(String[] command) { public String getDescription(String task) { String description; int separator; - if (getCommand(task).equals("todo")) { - description = task.substring(5); - } else if (getCommand(task).equals("deadline")) { - separator = task.indexOf("/by"); - description = task.substring(9, separator); - } else if (getCommand(task).equals("event")) { - separator = task.indexOf("/at"); - description = task.substring(6, separator); + if (getCommand(task).equals(TO_DO)) { + description = task.substring(TODO_STRING_LENGTH); + } else if (getCommand(task).equals(DEADLINE)) { + separator = task.indexOf(BY_SEPARATOR); + description = task.substring(DEADLINE_STRING_LENGTH, separator); + } else if (getCommand(task).equals(EVENT)) { + separator = task.indexOf(AT_SEPARATOR); + description = task.substring(EVENT_STRING_LENGTH, separator); } else { description = null; } return description; } + + /** * Returns type of Task given command * @param command @@ -125,25 +156,26 @@ public void list() { } /** - * Returns date of event or deadline - * @param description command entered - * @return Date of event or deadline + * Returns date of event or deadline. + * + * @param description command entered. + * @return Date of event or deadline. */ public String getDate(String description) { String date; String taskType = getCommand(description); int indexOfSeparator; switch (taskType) { - case "deadline": - indexOfSeparator = description.indexOf("/by"); - date = description.substring(indexOfSeparator + 3); + case DEADLINE: + indexOfSeparator = description.indexOf(BY_SEPARATOR); + date = description.substring(indexOfSeparator + SEPARATOR_STRING_LENGTH); if (date.isEmpty()) { System.out.println(" ☹ OOPS!!! Please enter a date"); } break; - case "event": - indexOfSeparator = description.indexOf("/at"); - date = description.substring(indexOfSeparator + 3); + case EVENT: + indexOfSeparator = description.indexOf(AT_SEPARATOR); + date = description.substring(indexOfSeparator + SEPARATOR_STRING_LENGTH); if (date.isEmpty()) { System.out.println(" ☹ OOPS!!! Please enter a date"); } @@ -154,4 +186,12 @@ public String getDate(String description) { } return date; } + + public void deleteTask(String[] input) { + int taskToDelete = Integer.parseInt(input[1]); + Task thisTask = taskList.get(taskToDelete - 1); + System.out.println(" " + thisTask); + taskList.remove(taskToDelete - 1); + printSize(); + } } From 3a8a6f4e327e3cef30a68fe531be26babe53f251 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Wed, 15 Sep 2021 17:29:47 +0800 Subject: [PATCH 14/30] Cleaned up TaskManager --- data/output.txt | 0 src/main/java/duke/data/Storage.java | 4 +++ src/main/java/duke/task/TaskManager.java | 39 ++++++++++-------------- 3 files changed, 20 insertions(+), 23 deletions(-) create mode 100644 data/output.txt create mode 100644 src/main/java/duke/data/Storage.java diff --git a/data/output.txt b/data/output.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/main/java/duke/data/Storage.java b/src/main/java/duke/data/Storage.java new file mode 100644 index 000000000..0ad7664e9 --- /dev/null +++ b/src/main/java/duke/data/Storage.java @@ -0,0 +1,4 @@ +package duke.data; + +public class Storage { +} diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index e838d59ef..182249325 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -33,15 +33,19 @@ public void add(String task) throws DukeException { throw new DukeException(); } String taskType = getCommand(task); + String description = getDescription(task); + String date; switch (taskType) { case TO_DO: - addToDo(task); + addToDo(description); break; case DEADLINE: - addDeadline(task); + date = getDate(task); + addDeadline(description, date); break; case EVENT: - addEvent(task); + date = getDate(task); + addEvent(description, date); break; default: System.out.println(" Invalid command, please try again"); @@ -50,30 +54,19 @@ public void add(String task) throws DukeException { printSize(); } - public void addToDo(String task) { - Task newTask; - String description = getDescription(task); - newTask = new ToDo(description); - taskList.add(newTask); - System.out.println(ADDED_TASK + newTask); + public void addToDo(String description) { + taskList.add(new ToDo(description)); + System.out.println(ADDED_TASK + new ToDo(description)); } - public void addDeadline(String task) { - Task newTask; - String description = getDescription(task); - String by = getDate(task); - newTask = new Deadline(description, by); - taskList.add(newTask); - System.out.println(ADDED_TASK + newTask); + public void addDeadline(String description, String time) { + taskList.add(new Deadline(description, time)); + System.out.println(ADDED_TASK + new Deadline(description, time)); } - public void addEvent(String task) { - Task newTask; - String description = getDescription(task); - String at = getDate(task); - newTask = new Event(description, at); - taskList.add(newTask); - System.out.println(ADDED_TASK + newTask); + public void addEvent(String description, String time) { + taskList.add(new Event(description, time)); + System.out.println(ADDED_TASK + new Event(description, time)); } public void checkDone(String[] command) { From 793418f3a3b091065a0daf04cc1e5698ef329291 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 16 Sep 2021 16:06:07 +0800 Subject: [PATCH 15/30] Level-7: Save --- data/output.txt | 1 + src/main/java/duke/command/Duke.java | 4 +- src/main/java/duke/command/Ui.java | 32 +++++++++++- src/main/java/duke/data/Storage.java | 62 ++++++++++++++++++++++++ src/main/java/duke/task/Deadline.java | 8 ++- src/main/java/duke/task/Event.java | 7 ++- src/main/java/duke/task/Task.java | 12 +++++ src/main/java/duke/task/TaskManager.java | 19 +++++--- src/main/java/duke/task/ToDo.java | 5 +- 9 files changed, 133 insertions(+), 17 deletions(-) diff --git a/data/output.txt b/data/output.txt index e69de29bb..3dba6b8ab 100644 --- a/data/output.txt +++ b/data/output.txt @@ -0,0 +1 @@ +T | 0 | add book diff --git a/src/main/java/duke/command/Duke.java b/src/main/java/duke/command/Duke.java index b391ee690..cdb4f2442 100644 --- a/src/main/java/duke/command/Duke.java +++ b/src/main/java/duke/command/Duke.java @@ -1,12 +1,12 @@ package duke.command; import duke.task.TaskManager; - +import java.io.IOException; import java.util.Scanner; public class Duke { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { TaskManager taskManager = new TaskManager(); Scanner scanner = new Scanner(System.in); Ui ui = new Ui(taskManager, scanner); diff --git a/src/main/java/duke/command/Ui.java b/src/main/java/duke/command/Ui.java index 64db82750..a559ad19d 100644 --- a/src/main/java/duke/command/Ui.java +++ b/src/main/java/duke/command/Ui.java @@ -1,7 +1,14 @@ package duke.command; +import duke.data.Storage; import duke.task.TaskManager; import duke.DukeException; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Scanner; public class Ui { @@ -33,13 +40,35 @@ public class Ui { private static final String DELETE = "delete"; private static final String ADD_SUCCESS = " Nice! I've marked this task as done: "; private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; + private static final String PATH_NAME = "data/output.txt"; public Ui(TaskManager taskManager, Scanner scanner) { this.taskManager = taskManager; this.scanner = scanner; } - public void start() { + private static void saveData() { + try { + String pathName = PATH_NAME; + //create folder with file if absent initially + Path path = Paths.get(pathName); + Files.createDirectories(path.getParent()); + Storage.writeToFile(pathName); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void loadData() throws IOException { + try { + Storage.load(PATH_NAME); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + public void start() throws IOException { + loadData(); System.out.println(GREETINGS); boolean isExit = false; while (!isExit) { @@ -82,5 +111,6 @@ public void start() { } System.out.print(LINE); } + saveData(); } } diff --git a/src/main/java/duke/data/Storage.java b/src/main/java/duke/data/Storage.java index 0ad7664e9..ad0032a01 100644 --- a/src/main/java/duke/data/Storage.java +++ b/src/main/java/duke/data/Storage.java @@ -1,4 +1,66 @@ package duke.data; +import duke.task.Deadline; +import duke.task.Event; +import duke.task.Task; +import duke.task.TaskManager; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Scanner; + public class Storage { + + public static void load(String filePath) throws IOException { + File newFile = new File(filePath); + Scanner scanner = new Scanner(newFile); + + while (scanner.hasNext()) { + String line = scanner.nextLine(); + String[] array = line.split(" | "); + switch (array[0]) { + case "T": + TaskManager.addToDo(array[2]); + break; + case "D": + TaskManager.addDeadline(array[2], array[3]); + break; + case "E": + TaskManager.addEvent(array[2], array[3]); + break; + } + } + } + + + public static void appendToFile(String filePath, String textToAppend) throws IOException { + FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode + fw.write(textToAppend); + fw.close(); + } + + public static void writeToFile(String filePath) throws IOException { + File file = new File(filePath); + if (file.createNewFile()) { + System.out.println("File created"); + } + String textToAppend; + for (Task task: TaskManager.taskList) { + String taskType = task.getIcon(); + String status = task.getStatus(); + String description = task.getDescription(); + String timing = task.getTime(); + + textToAppend = taskType + " | " + status + " | " + description; + if (task instanceof Event || task instanceof Deadline) { + textToAppend += " | " + timing; + } + textToAppend += "\n"; + appendToFile(filePath, textToAppend); + } + } + + + } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 7a6c1f0ff..2a436851c 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -10,15 +10,19 @@ public Deadline(String description, String by) { } public String getIcon() { - return "[D]"; + return "D"; } public String getTiming() { return "(by:" + timing + ")"; } + public String getTime() { + return timing; + } + @Override public String toString() { - return getIcon() + super.toString() + getTiming(); + return "[" + getIcon() + "]" + super.toString() + getTiming(); } } \ No newline at end of file diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 8f3ce1149..4ded5adc6 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -9,15 +9,18 @@ public Event(String description, String timing) { } public String getIcon() { - return "[E]"; + return "E"; } public String getTiming() { return "(at:" + timing + ")"; } + public String getTime() { + return timing; + } @Override public String toString() { - return getIcon() + super.toString() + getTiming(); + return "[" + getIcon() + "]" + super.toString() + getTiming(); } } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 7274b5edf..121483597 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -17,6 +17,10 @@ public String getStatusIcon() { return (isDone ? "[X]" : "[ ]"); } + public String getStatus() { + return (isDone ? "1" : "0"); + } + public String getIcon() { return ""; } @@ -25,6 +29,14 @@ public String getTiming() { return ""; } + public String getDescription() { + return description; + } + + public String getTime() { + return ""; + } + public String toString() { return getStatusIcon() + " " + this.description ; } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 182249325..58aa2c145 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -5,14 +5,14 @@ import java.util.ArrayList; public class TaskManager { - private ArrayList taskList; + public static ArrayList taskList; private static final String TO_DO = "todo"; private static final String DEADLINE = "deadline"; private static final String EVENT = "event"; private static final String ADDED_TASK = " Got it. I've added this task:\n "; - private static final String BY_SEPARATOR = "by"; - private static final String AT_SEPARATOR = "at"; + private static final String BY_SEPARATOR = "/by"; + private static final String AT_SEPARATOR = "/at"; private static final int TODO_STRING_LENGTH = 5; private static final int DEADLINE_STRING_LENGTH = 9; private static final int EVENT_STRING_LENGTH = 6; @@ -54,17 +54,17 @@ public void add(String task) throws DukeException { printSize(); } - public void addToDo(String description) { + public static void addToDo(String description) { taskList.add(new ToDo(description)); System.out.println(ADDED_TASK + new ToDo(description)); } - public void addDeadline(String description, String time) { + public static void addDeadline(String description, String time) { taskList.add(new Deadline(description, time)); System.out.println(ADDED_TASK + new Deadline(description, time)); } - public void addEvent(String description, String time) { + public static void addEvent(String description, String time) { taskList.add(new Event(description, time)); System.out.println(ADDED_TASK + new Event(description, time)); } @@ -85,10 +85,10 @@ public String getDescription(String task) { description = task.substring(TODO_STRING_LENGTH); } else if (getCommand(task).equals(DEADLINE)) { separator = task.indexOf(BY_SEPARATOR); - description = task.substring(DEADLINE_STRING_LENGTH, separator); + description = task.substring(DEADLINE_STRING_LENGTH, separator - 1); } else if (getCommand(task).equals(EVENT)) { separator = task.indexOf(AT_SEPARATOR); - description = task.substring(EVENT_STRING_LENGTH, separator); + description = task.substring(EVENT_STRING_LENGTH, separator - 1); } else { description = null; } @@ -109,6 +109,8 @@ public String getCommand(String command) { return taskType; } + + public void printSize() { if (getSize() == 1) { System.out.println(" Now you have " + 1 + " task in the list."); @@ -131,6 +133,7 @@ public int getSize() { return taskList.size(); } + public void list() { try { if (taskList.size() == 0) { diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java index 4df012c4f..478d106ba 100644 --- a/src/main/java/duke/task/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -6,10 +6,11 @@ public ToDo(String description) { } public String getIcon() { - return "[T]"; + return "T"; } + @Override public String toString() { - return getIcon() + super.toString(); + return "[" + getIcon() + "]"+ super.toString(); } } From 5cf8dd0bd980b4575a3343cb3febca6f3ed6cee3 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 16 Sep 2021 22:50:47 +0800 Subject: [PATCH 16/30] add manifest file --- src/main/resources/META-INF/MANIFEST.MF | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/resources/META-INF/MANIFEST.MF diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 000000000..d749a8d57 --- /dev/null +++ b/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.command.Duke + From 6b52c9eb9756a71d16eec027228fb26a1bf9cbf5 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 16 Sep 2021 23:25:23 +0800 Subject: [PATCH 17/30] Bugfix of loading of tasks from file --- data/output.txt | 2 +- src/main/java/duke/data/Storage.java | 6 +++--- src/main/java/duke/task/TaskManager.java | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/data/output.txt b/data/output.txt index 3dba6b8ab..5df1ba272 100644 --- a/data/output.txt +++ b/data/output.txt @@ -1 +1 @@ -T | 0 | add book +D | 0 | return book | Sunday diff --git a/src/main/java/duke/data/Storage.java b/src/main/java/duke/data/Storage.java index ad0032a01..ceed1857e 100644 --- a/src/main/java/duke/data/Storage.java +++ b/src/main/java/duke/data/Storage.java @@ -21,13 +21,13 @@ public static void load(String filePath) throws IOException { String[] array = line.split(" | "); switch (array[0]) { case "T": - TaskManager.addToDo(array[2]); + TaskManager.loadToDoFromFile(array[2]); break; case "D": - TaskManager.addDeadline(array[2], array[3]); + TaskManager.loadDeadlineFromFile(array[2], array[3]); break; case "E": - TaskManager.addEvent(array[2], array[3]); + TaskManager.loadEventFromFile(array[2], array[3]); break; } } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 58aa2c145..15154be73 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -59,16 +59,29 @@ public static void addToDo(String description) { System.out.println(ADDED_TASK + new ToDo(description)); } + public static void loadToDoFromFile(String description) { + taskList.add(new ToDo(description)); + } + public static void addDeadline(String description, String time) { taskList.add(new Deadline(description, time)); System.out.println(ADDED_TASK + new Deadline(description, time)); } + public static void loadDeadlineFromFile(String description, String time) { + taskList.add(new Deadline(description, time)); + } + public static void addEvent(String description, String time) { taskList.add(new Event(description, time)); System.out.println(ADDED_TASK + new Event(description, time)); } + public static void loadEventFromFile(String description, String time) { + taskList.add(new Event(description, time)); + } + + public void checkDone(String[] command) { try { Integer.parseInt(command[1]); @@ -112,8 +125,10 @@ public String getCommand(String command) { public void printSize() { - if (getSize() == 1) { - System.out.println(" Now you have " + 1 + " task in the list."); + if (getSize() == 0) { + System.out.println(" Now you have 0 task in the list."); + } else if (getSize() == 1) { + System.out.println(" Now you have 1 task in the list."); } else { System.out.println(" Now you have " + getSize() + " tasks in the list."); } From 3d9240670bb8d41f88dc77602cdefdcc0f3272e1 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Tue, 28 Sep 2021 14:53:22 +0800 Subject: [PATCH 18/30] Finalise and clean up Storage class --- data/output.txt | 4 +- src/main/java/duke/command/Duke.java | 5 +- src/main/java/duke/command/Ui.java | 35 +++--------- src/main/java/duke/data/Storage.java | 69 +++++++++++++++++++----- src/main/java/duke/task/Deadline.java | 5 ++ src/main/java/duke/task/Event.java | 5 ++ src/main/java/duke/task/Task.java | 4 +- src/main/java/duke/task/TaskManager.java | 20 ++++--- src/main/java/duke/task/ToDo.java | 5 ++ 9 files changed, 93 insertions(+), 59 deletions(-) diff --git a/data/output.txt b/data/output.txt index 5df1ba272..a1391eb83 100644 --- a/data/output.txt +++ b/data/output.txt @@ -1 +1,3 @@ -D | 0 | return book | Sunday +T | 0 | add book +D | 1 | bye | 1040 +T | 0 | see book diff --git a/src/main/java/duke/command/Duke.java b/src/main/java/duke/command/Duke.java index cdb4f2442..505f4f538 100644 --- a/src/main/java/duke/command/Duke.java +++ b/src/main/java/duke/command/Duke.java @@ -1,14 +1,15 @@ package duke.command; +import duke.data.Storage; import duke.task.TaskManager; -import java.io.IOException; import java.util.Scanner; public class Duke { - public static void main(String[] args) throws IOException { + public static void main(String[] args) { TaskManager taskManager = new TaskManager(); Scanner scanner = new Scanner(System.in); + //Storage storage = new Storage(); Ui ui = new Ui(taskManager, scanner); ui.start(); } diff --git a/src/main/java/duke/command/Ui.java b/src/main/java/duke/command/Ui.java index a559ad19d..6436cf04f 100644 --- a/src/main/java/duke/command/Ui.java +++ b/src/main/java/duke/command/Ui.java @@ -4,14 +4,10 @@ import duke.task.TaskManager; import duke.DukeException; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Scanner; public class Ui { + //private Storage storage; private TaskManager taskManager; private Scanner scanner; @@ -40,35 +36,16 @@ public class Ui { private static final String DELETE = "delete"; private static final String ADD_SUCCESS = " Nice! I've marked this task as done: "; private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; - private static final String PATH_NAME = "data/output.txt"; + //private static final String PATH_NAME = "data/output.txt"; public Ui(TaskManager taskManager, Scanner scanner) { this.taskManager = taskManager; this.scanner = scanner; + //this.storage = storage; } - private static void saveData() { - try { - String pathName = PATH_NAME; - //create folder with file if absent initially - Path path = Paths.get(pathName); - Files.createDirectories(path.getParent()); - Storage.writeToFile(pathName); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static void loadData() throws IOException { - try { - Storage.load(PATH_NAME); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - - public void start() throws IOException { - loadData(); + public void start() { + Storage.loadData(); System.out.println(GREETINGS); boolean isExit = false; while (!isExit) { @@ -110,7 +87,7 @@ public void start() throws IOException { System.out.println(" ☹ OOPS!!! The task's index should be an integer."); } System.out.print(LINE); + Storage.saveData(); } - saveData(); } } diff --git a/src/main/java/duke/data/Storage.java b/src/main/java/duke/data/Storage.java index ceed1857e..f70f6ca14 100644 --- a/src/main/java/duke/data/Storage.java +++ b/src/main/java/duke/data/Storage.java @@ -6,34 +6,53 @@ import duke.task.TaskManager; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Scanner; public class Storage { - public static void load(String filePath) throws IOException { - File newFile = new File(filePath); - Scanner scanner = new Scanner(newFile); + private static final String PATH_NAME = "data/output.txt"; + public static void createFile(){ + try { + Path path = Paths.get(PATH_NAME); // + Files.createDirectories(path.getParent()); + } catch (IOException e) { + System.out.println("Error in creating file"); + } + } + + public static void readFromFile(String filePath) throws FileNotFoundException { + File file = new File(filePath); + Scanner scanner = new Scanner(file); while (scanner.hasNext()) { String line = scanner.nextLine(); - String[] array = line.split(" | "); - switch (array[0]) { + String[] details = line.split("\\|"); + String taskType = details[0].trim(); + boolean isDone = details[1].trim().equals("1"); + String description = details[2].trim(); + String timing; + switch (taskType) { case "T": - TaskManager.loadToDoFromFile(array[2]); + TaskManager.loadToDoFromFile(description, isDone); break; case "D": - TaskManager.loadDeadlineFromFile(array[2], array[3]); + timing = details[3].trim(); + TaskManager.loadDeadlineFromFile(description, timing, isDone); break; case "E": - TaskManager.loadEventFromFile(array[2], array[3]); + timing = details[3].trim(); + TaskManager.loadEventFromFile(description, timing, isDone); break; } } } - public static void appendToFile(String filePath, String textToAppend) throws IOException { FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode fw.write(textToAppend); @@ -41,11 +60,10 @@ public static void appendToFile(String filePath, String textToAppend) throws IOE } public static void writeToFile(String filePath) throws IOException { - File file = new File(filePath); - if (file.createNewFile()) { - System.out.println("File created"); - } String textToAppend; + FileWriter fw = new FileWriter(filePath, false); + fw.write(""); //clear the file + fw.close(); for (Task task: TaskManager.taskList) { String taskType = task.getIcon(); String status = task.getStatus(); @@ -54,13 +72,36 @@ public static void writeToFile(String filePath) throws IOException { textToAppend = taskType + " | " + status + " | " + description; if (task instanceof Event || task instanceof Deadline) { - textToAppend += " | " + timing; + textToAppend += " |" + timing; } textToAppend += "\n"; appendToFile(filePath, textToAppend); } } + public static void saveData() { + try { + Path path = Paths.get(PATH_NAME); + Files.createDirectories(path.getParent()); + writeToFile(PATH_NAME); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void loadData() { + try { + readFromFile(PATH_NAME); + } catch (FileNotFoundException e) { + createFile(); + } + } + + + + + + } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 2a436851c..33e266975 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -9,6 +9,11 @@ public Deadline(String description, String by) { this.timing = by; } + public Deadline(String description, String by, boolean isDone) { + this(description, by); + super.taskDone(isDone); + } + public String getIcon() { return "D"; } diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 4ded5adc6..ca7ad5a47 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -8,6 +8,11 @@ public Event(String description, String timing) { this.timing = timing; } + public Event(String description, String timing, boolean isDone) { + this(description, timing); + super.taskDone(isDone); + } + public String getIcon() { return "E"; } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 121483597..116b09f4a 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -9,8 +9,8 @@ public Task(String description) { this.isDone = false; } - public void taskDone() { - this.isDone = true; + public void taskDone(boolean isDone) { + this.isDone = isDone; } public String getStatusIcon() { diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 15154be73..348799bb8 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -59,8 +59,8 @@ public static void addToDo(String description) { System.out.println(ADDED_TASK + new ToDo(description)); } - public static void loadToDoFromFile(String description) { - taskList.add(new ToDo(description)); + public static void loadToDoFromFile(String description, boolean isDone) { + taskList.add(new ToDo(description, isDone)); } public static void addDeadline(String description, String time) { @@ -68,8 +68,8 @@ public static void addDeadline(String description, String time) { System.out.println(ADDED_TASK + new Deadline(description, time)); } - public static void loadDeadlineFromFile(String description, String time) { - taskList.add(new Deadline(description, time)); + public static void loadDeadlineFromFile(String description, String time, boolean isDone) { + taskList.add(new Deadline(description, time, isDone)); } public static void addEvent(String description, String time) { @@ -77,18 +77,19 @@ public static void addEvent(String description, String time) { System.out.println(ADDED_TASK + new Event(description, time)); } - public static void loadEventFromFile(String description, String time) { - taskList.add(new Event(description, time)); + public static void loadEventFromFile(String description, String time, boolean isDone) { + taskList.add(new Event(description, time, isDone)); } - + //set task as done public void checkDone(String[] command) { + //catch if input after done is integer try { Integer.parseInt(command[1]); } catch (NumberFormatException e) { throw new NumberFormatException(); } - taskList.get(Integer.parseInt(command[1]) - 1).taskDone(); + taskList.get(Integer.parseInt(command[1]) - 1).taskDone(true); } public String getDescription(String task) { @@ -108,8 +109,6 @@ public String getDescription(String task) { return description; } - - /** * Returns type of Task given command * @param command @@ -148,7 +147,6 @@ public int getSize() { return taskList.size(); } - public void list() { try { if (taskList.size() == 0) { diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java index 478d106ba..9671cec55 100644 --- a/src/main/java/duke/task/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -5,6 +5,11 @@ public ToDo(String description) { super(description); } + public ToDo(String description, boolean isDone) { + this(description); + super.taskDone(isDone); + } + public String getIcon() { return "T"; } From 054a4d6840882b3cb9d40492bfef47be80641300 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Tue, 28 Sep 2021 18:08:22 +0800 Subject: [PATCH 19/30] Added Parser class --- src/main/java/duke/command/Duke.java | 2 - src/main/java/duke/command/Ui.java | 72 ++++++++++++------------ src/main/java/duke/data/Storage.java | 11 +--- src/main/java/duke/task/Parser.java | 57 +++++++++++++++++++ src/main/java/duke/task/TaskManager.java | 20 +++---- 5 files changed, 104 insertions(+), 58 deletions(-) create mode 100644 src/main/java/duke/task/Parser.java diff --git a/src/main/java/duke/command/Duke.java b/src/main/java/duke/command/Duke.java index 505f4f538..b44f5b74e 100644 --- a/src/main/java/duke/command/Duke.java +++ b/src/main/java/duke/command/Duke.java @@ -1,6 +1,5 @@ package duke.command; -import duke.data.Storage; import duke.task.TaskManager; import java.util.Scanner; @@ -9,7 +8,6 @@ public class Duke { public static void main(String[] args) { TaskManager taskManager = new TaskManager(); Scanner scanner = new Scanner(System.in); - //Storage storage = new Storage(); Ui ui = new Ui(taskManager, scanner); ui.start(); } diff --git a/src/main/java/duke/command/Ui.java b/src/main/java/duke/command/Ui.java index 6436cf04f..a410e02d0 100644 --- a/src/main/java/duke/command/Ui.java +++ b/src/main/java/duke/command/Ui.java @@ -1,6 +1,7 @@ package duke.command; import duke.data.Storage; +import duke.task.Parser; import duke.task.TaskManager; import duke.DukeException; @@ -36,56 +37,53 @@ public class Ui { private static final String DELETE = "delete"; private static final String ADD_SUCCESS = " Nice! I've marked this task as done: "; private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; - //private static final String PATH_NAME = "data/output.txt"; public Ui(TaskManager taskManager, Scanner scanner) { this.taskManager = taskManager; this.scanner = scanner; - //this.storage = storage; } public void start() { Storage.loadData(); System.out.println(GREETINGS); + boolean isExit = false; while (!isExit) { String input = scanner.nextLine(); - String[] command = input.split(" "); - String firstWord = command[0]; - System.out.print(LINE); - try { - switch (firstWord) { - case BYE: - System.out.println(FAREWELL); - isExit = true; - break; - case LIST: - taskManager.list(); - break; - case DONE: - int taskNumber = Integer.parseInt(command[1]); - System.out.println(ADD_SUCCESS); - taskManager.checkDone(command); - System.out.println(" " + taskManager.getName(taskNumber)); - break; - case TO_DO: - case DEADLINE: - case EVENT: - taskManager.add(input); - break; - case DELETE: - System.out.println(DELETE_SUCCESS); - taskManager.deleteTask(command); - break; - default: - System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); - } - } catch (DukeException e) { - System.out.println(" ☹ OOPS!!! The description of a " + firstWord + " cannot be empty."); - } catch (NumberFormatException e) { - System.out.println(" ☹ OOPS!!! The task's index should be an integer."); - } + isExit = Parser.parse(input); +// try { +// switch (command) { +// case BYE: +// System.out.println(FAREWELL); +// isExit = true; +// break; +// case LIST: +// taskManager.list(); +// break; +// case DONE: +// int taskNumber = Integer.parseInt(params[1]); +// System.out.println(ADD_SUCCESS); +// taskManager.checkDone(params); +// System.out.println(" " + taskManager.getName(taskNumber)); +// break; +// case TO_DO: +// case DEADLINE: +// case EVENT: +// taskManager.add(input); +// break; +// case DELETE: +// System.out.println(DELETE_SUCCESS); +// taskManager.deleteTask(params); +// break; +// default: +// System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); +// } +// } catch (DukeException e) { +// System.out.println(" ☹ OOPS!!! The description of a " + command + " cannot be empty."); +// } catch (NumberFormatException e) { +// System.out.println(" ☹ OOPS!!! The task's index should be an integer."); +// } System.out.print(LINE); Storage.saveData(); } diff --git a/src/main/java/duke/data/Storage.java b/src/main/java/duke/data/Storage.java index f70f6ca14..c0817ca1d 100644 --- a/src/main/java/duke/data/Storage.java +++ b/src/main/java/duke/data/Storage.java @@ -72,7 +72,7 @@ public static void writeToFile(String filePath) throws IOException { textToAppend = taskType + " | " + status + " | " + description; if (task instanceof Event || task instanceof Deadline) { - textToAppend += " |" + timing; + textToAppend += " | " + timing; } textToAppend += "\n"; appendToFile(filePath, textToAppend); @@ -96,12 +96,5 @@ public static void loadData() { createFile(); } } - - - - - - - - + } diff --git a/src/main/java/duke/task/Parser.java b/src/main/java/duke/task/Parser.java new file mode 100644 index 000000000..aed9629c1 --- /dev/null +++ b/src/main/java/duke/task/Parser.java @@ -0,0 +1,57 @@ +package duke.task; + +import duke.DukeException; + +public class Parser { + + private static final String FAREWELL = " Bye. Hope to see you again soon!"; + private static final String TO_DO = "todo"; + private static final String DEADLINE = "deadline"; + private static final String EVENT = "event"; + private static final String BYE = "bye"; + private static final String LIST = "list"; + private static final String DONE = "done"; + private static final String DELETE = "delete"; + private static final String ADD_SUCCESS = " Nice! I've marked this task as done: "; + private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; + + + public static boolean parse(String input) { + String[] params = input.split(" "); + String command = params[0]; + try { + switch (command) { + case BYE: + System.out.println(FAREWELL); + return true; + case LIST: + TaskManager.list(); + break; + case DONE: + int taskNumber = Integer.parseInt(params[1]); + System.out.println(ADD_SUCCESS); + TaskManager.checkDone(params); + System.out.println(" " + TaskManager.getName(taskNumber)); + break; + case TO_DO: + case DEADLINE: + case EVENT: + TaskManager.add(input); + break; + case DELETE: + System.out.println(DELETE_SUCCESS); + TaskManager.deleteTask(params); + break; + default: + System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + } + } catch (DukeException e) { + System.out.println(" ☹ OOPS!!! The description of a " + command + " cannot be empty."); + } catch (NumberFormatException e) { + System.out.println(" ☹ OOPS!!! The task's index should be an integer."); + } + return false; + } + + +} diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 348799bb8..e6f325b54 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -27,7 +27,7 @@ public TaskManager() { * * @param task Task to be checked */ - public void add(String task) throws DukeException { + public static void add(String task) throws DukeException { String[] command = task.split(" "); if (command.length < 2) { throw new DukeException(); @@ -82,7 +82,7 @@ public static void loadEventFromFile(String description, String time, boolean is } //set task as done - public void checkDone(String[] command) { + public static void checkDone(String[] command) { //catch if input after done is integer try { Integer.parseInt(command[1]); @@ -92,7 +92,7 @@ public void checkDone(String[] command) { taskList.get(Integer.parseInt(command[1]) - 1).taskDone(true); } - public String getDescription(String task) { + public static String getDescription(String task) { String description; int separator; if (getCommand(task).equals(TO_DO)) { @@ -114,7 +114,7 @@ public String getDescription(String task) { * @param command * @return Type of Task */ - public String getCommand(String command) { + public static String getCommand(String command) { String[] task = command.split(" "); String taskType = task[0]; @@ -123,7 +123,7 @@ public String getCommand(String command) { - public void printSize() { + public static void printSize() { if (getSize() == 0) { System.out.println(" Now you have 0 task in the list."); } else if (getSize() == 1) { @@ -139,15 +139,15 @@ public void printSize() { * @param index index of task in taskList ArrayList * @return Name of Task */ - public String getName(int index) { + public static String getName(int index) { return taskList.get(index - 1).toString(); } - public int getSize() { + public static int getSize() { return taskList.size(); } - public void list() { + public static void list() { try { if (taskList.size() == 0) { System.out.println(" ☹ OOPS!!! List is empty"); @@ -170,7 +170,7 @@ public void list() { * @param description command entered. * @return Date of event or deadline. */ - public String getDate(String description) { + public static String getDate(String description) { String date; String taskType = getCommand(description); int indexOfSeparator; @@ -196,7 +196,7 @@ public String getDate(String description) { return date; } - public void deleteTask(String[] input) { + public static void deleteTask(String[] input) { int taskToDelete = Integer.parseInt(input[1]); Task thisTask = taskList.get(taskToDelete - 1); System.out.println(" " + thisTask); From 770747efbe42fc8c29efb8dd7dd9052707f56138 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Tue, 28 Sep 2021 20:16:15 +0800 Subject: [PATCH 20/30] Added Find --- src/main/java/duke/command/Duke.java | 2 +- src/main/java/duke/command/Ui.java | 46 ------------------------ src/main/java/duke/data/Storage.java | 1 - src/main/java/duke/task/Deadline.java | 1 - src/main/java/duke/task/Event.java | 1 + src/main/java/duke/task/Parser.java | 11 +++--- src/main/java/duke/task/Task.java | 2 -- src/main/java/duke/task/TaskManager.java | 40 ++++++++++++++------- 8 files changed, 36 insertions(+), 68 deletions(-) diff --git a/src/main/java/duke/command/Duke.java b/src/main/java/duke/command/Duke.java index b44f5b74e..05a93e618 100644 --- a/src/main/java/duke/command/Duke.java +++ b/src/main/java/duke/command/Duke.java @@ -3,8 +3,8 @@ import duke.task.TaskManager; import java.util.Scanner; - public class Duke { + public static void main(String[] args) { TaskManager taskManager = new TaskManager(); Scanner scanner = new Scanner(System.in); diff --git a/src/main/java/duke/command/Ui.java b/src/main/java/duke/command/Ui.java index a410e02d0..5e9eaffe2 100644 --- a/src/main/java/duke/command/Ui.java +++ b/src/main/java/duke/command/Ui.java @@ -3,12 +3,10 @@ import duke.data.Storage; import duke.task.Parser; import duke.task.TaskManager; -import duke.DukeException; import java.util.Scanner; public class Ui { - //private Storage storage; private TaskManager taskManager; private Scanner scanner; @@ -26,17 +24,6 @@ public class Ui { + " Welcome to the toDoList Chatbot\n" + " What would you like to do today?\n" + LINE; - private static final String FAREWELL = " Bye. Hope to see you again soon!"; - - private static final String TO_DO = "todo"; - private static final String DEADLINE = "deadline"; - private static final String EVENT = "event"; - private static final String BYE = "bye"; - private static final String LIST = "list"; - private static final String DONE = "done"; - private static final String DELETE = "delete"; - private static final String ADD_SUCCESS = " Nice! I've marked this task as done: "; - private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; public Ui(TaskManager taskManager, Scanner scanner) { this.taskManager = taskManager; @@ -46,44 +33,11 @@ public Ui(TaskManager taskManager, Scanner scanner) { public void start() { Storage.loadData(); System.out.println(GREETINGS); - boolean isExit = false; while (!isExit) { String input = scanner.nextLine(); System.out.print(LINE); isExit = Parser.parse(input); -// try { -// switch (command) { -// case BYE: -// System.out.println(FAREWELL); -// isExit = true; -// break; -// case LIST: -// taskManager.list(); -// break; -// case DONE: -// int taskNumber = Integer.parseInt(params[1]); -// System.out.println(ADD_SUCCESS); -// taskManager.checkDone(params); -// System.out.println(" " + taskManager.getName(taskNumber)); -// break; -// case TO_DO: -// case DEADLINE: -// case EVENT: -// taskManager.add(input); -// break; -// case DELETE: -// System.out.println(DELETE_SUCCESS); -// taskManager.deleteTask(params); -// break; -// default: -// System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); -// } -// } catch (DukeException e) { -// System.out.println(" ☹ OOPS!!! The description of a " + command + " cannot be empty."); -// } catch (NumberFormatException e) { -// System.out.println(" ☹ OOPS!!! The task's index should be an integer."); -// } System.out.print(LINE); Storage.saveData(); } diff --git a/src/main/java/duke/data/Storage.java b/src/main/java/duke/data/Storage.java index c0817ca1d..ede488949 100644 --- a/src/main/java/duke/data/Storage.java +++ b/src/main/java/duke/data/Storage.java @@ -96,5 +96,4 @@ public static void loadData() { createFile(); } } - } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 33e266975..840d8f0de 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,7 +1,6 @@ package duke.task; public class Deadline extends Task { - protected String timing; public Deadline(String description, String by) { diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index ca7ad5a47..b8ed8c45e 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -24,6 +24,7 @@ public String getTiming() { public String getTime() { return timing; } + @Override public String toString() { return "[" + getIcon() + "]" + super.toString() + getTiming(); diff --git a/src/main/java/duke/task/Parser.java b/src/main/java/duke/task/Parser.java index aed9629c1..feacdf032 100644 --- a/src/main/java/duke/task/Parser.java +++ b/src/main/java/duke/task/Parser.java @@ -3,7 +3,6 @@ import duke.DukeException; public class Parser { - private static final String FAREWELL = " Bye. Hope to see you again soon!"; private static final String TO_DO = "todo"; private static final String DEADLINE = "deadline"; @@ -12,9 +11,10 @@ public class Parser { private static final String LIST = "list"; private static final String DONE = "done"; private static final String DELETE = "delete"; + private static final String FIND = "find"; private static final String ADD_SUCCESS = " Nice! I've marked this task as done: "; private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; - + private static final String SHOW_MATCHES = " Here are the matching tasks in your list:"; public static boolean parse(String input) { String[] params = input.split(" "); @@ -42,6 +42,11 @@ public static boolean parse(String input) { System.out.println(DELETE_SUCCESS); TaskManager.deleteTask(params); break; + case FIND: + System.out.println(SHOW_MATCHES); + String query = params[1]; + TaskManager.find(query); + break; default: System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } @@ -52,6 +57,4 @@ public static boolean parse(String input) { } return false; } - - } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 116b09f4a..cad3a9d34 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -40,6 +40,4 @@ public String getTime() { public String toString() { return getStatusIcon() + " " + this.description ; } - - } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index e6f325b54..a69adf031 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -3,6 +3,7 @@ import duke.DukeException; import java.util.ArrayList; +import java.util.stream.Collectors; public class TaskManager { public static ArrayList taskList; @@ -22,6 +23,7 @@ public TaskManager() { this.taskList = new ArrayList<>(); } + /** * Adds task to taskList ArrayList * @@ -81,7 +83,6 @@ public static void loadEventFromFile(String description, String time, boolean is taskList.add(new Event(description, time, isDone)); } - //set task as done public static void checkDone(String[] command) { //catch if input after done is integer try { @@ -92,17 +93,17 @@ public static void checkDone(String[] command) { taskList.get(Integer.parseInt(command[1]) - 1).taskDone(true); } - public static String getDescription(String task) { + public static String getDescription(String input) { String description; int separator; - if (getCommand(task).equals(TO_DO)) { - description = task.substring(TODO_STRING_LENGTH); - } else if (getCommand(task).equals(DEADLINE)) { - separator = task.indexOf(BY_SEPARATOR); - description = task.substring(DEADLINE_STRING_LENGTH, separator - 1); - } else if (getCommand(task).equals(EVENT)) { - separator = task.indexOf(AT_SEPARATOR); - description = task.substring(EVENT_STRING_LENGTH, separator - 1); + if (getCommand(input).equals(TO_DO)) { + description = input.substring(TODO_STRING_LENGTH); + } else if (getCommand(input).equals(DEADLINE)) { + separator = input.indexOf(BY_SEPARATOR); + description = input.substring(DEADLINE_STRING_LENGTH, separator - 1); + } else if (getCommand(input).equals(EVENT)) { + separator = input.indexOf(AT_SEPARATOR); + description = input.substring(EVENT_STRING_LENGTH, separator - 1); } else { description = null; } @@ -121,8 +122,6 @@ public static String getCommand(String command) { return taskType; } - - public static void printSize() { if (getSize() == 0) { System.out.println(" Now you have 0 task in the list."); @@ -159,7 +158,7 @@ public static void list() { System.out.println(" " + (i + 1) + "." + t); } } catch (IndexOutOfBoundsException i) { - System.out.println("Error! Please contact admin"); + System.out.println("Error! Index out of bounds"); } } @@ -203,4 +202,19 @@ public static void deleteTask(String[] input) { taskList.remove(taskToDelete - 1); printSize(); } + + public static ArrayList filterTasksByString(ArrayList tasks, String filterString) { + ArrayList filteredList = (ArrayList) tasks.stream() + .filter((t) -> t.getDescription().contains(filterString)) + .collect(Collectors.toList()); + return filteredList; + } + + public static void find(String input) { + ArrayList filteredList = TaskManager.filterTasksByString(taskList, input); + for (int i = 0; i < filteredList.size(); i++) { + Task t = filteredList.get(i); + System.out.println(" " + (i + 1) + "." + t); + } + } } From 99938baf551f99d5b8e1cb6cae589e6763998f52 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Tue, 28 Sep 2021 21:14:00 +0800 Subject: [PATCH 21/30] Added JavaDocs --- src/main/java/duke/command/Ui.java | 3 + src/main/java/duke/data/Storage.java | 30 ++++++++- src/main/java/duke/task/Deadline.java | 33 ++++++++++ src/main/java/duke/task/Event.java | 34 ++++++++++ src/main/java/duke/task/Parser.java | 8 ++- src/main/java/duke/task/Task.java | 46 ++++++++++++- src/main/java/duke/task/TaskManager.java | 84 ++++++++++++++++++++---- src/main/java/duke/task/ToDo.java | 21 ++++++ 8 files changed, 245 insertions(+), 14 deletions(-) diff --git a/src/main/java/duke/command/Ui.java b/src/main/java/duke/command/Ui.java index a410e02d0..7486f39f0 100644 --- a/src/main/java/duke/command/Ui.java +++ b/src/main/java/duke/command/Ui.java @@ -43,6 +43,9 @@ public Ui(TaskManager taskManager, Scanner scanner) { this.scanner = scanner; } + /** + * Starts the program. + */ public void start() { Storage.loadData(); System.out.println(GREETINGS); diff --git a/src/main/java/duke/data/Storage.java b/src/main/java/duke/data/Storage.java index c0817ca1d..4b187ad26 100644 --- a/src/main/java/duke/data/Storage.java +++ b/src/main/java/duke/data/Storage.java @@ -18,15 +18,24 @@ public class Storage { private static final String PATH_NAME = "data/output.txt"; + /** + * Creates file inside its directory. + */ public static void createFile(){ try { - Path path = Paths.get(PATH_NAME); // + Path path = Paths.get(PATH_NAME); Files.createDirectories(path.getParent()); } catch (IOException e) { System.out.println("Error in creating file"); } } + /** + * Reads content from file and adds into TaskList. + * + * @param filePath Path where file is read from. + * @throws FileNotFoundException If file not found. + */ public static void readFromFile(String filePath) throws FileNotFoundException { File file = new File(filePath); Scanner scanner = new Scanner(file); @@ -53,12 +62,25 @@ public static void readFromFile(String filePath) throws FileNotFoundException { } } + /** + * Appends to end of file. + * + * @param filePath Path to append file to. + * @param textToAppend Text to be appended. + * @throws IOException If an I/O exception has occurred. + */ public static void appendToFile(String filePath, String textToAppend) throws IOException { FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode fw.write(textToAppend); fw.close(); } + /** + * Writes to file. + * + * @param filePath Path to write file to. + * @throws IOException If an I/O exception has occurred. + */ public static void writeToFile(String filePath) throws IOException { String textToAppend; FileWriter fw = new FileWriter(filePath, false); @@ -79,6 +101,9 @@ public static void writeToFile(String filePath) throws IOException { } } + /** + * Saves data into file. + */ public static void saveData() { try { Path path = Paths.get(PATH_NAME); @@ -89,6 +114,9 @@ public static void saveData() { } } + /** + * Loads data from file. + */ public static void loadData() { try { readFromFile(PATH_NAME); diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 33e266975..6cd307823 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -4,28 +4,61 @@ public class Deadline extends Task { protected String timing; + /** + * Constructs Deadline class given description and timing. + * + * @param description Description of deadline. + * @param by Due date of deadline. + */ public Deadline(String description, String by) { super(description); this.timing = by; } + /** + * Constructs Deadline class given description, timing and status. + * + * @param description Description of deadline. + * @param by Due date of deadline. + * @param isDone Status of deadline. + */ public Deadline(String description, String by, boolean isDone) { this(description, by); super.taskDone(isDone); } + /** + * Gets icon of deadline. + * + * @return Icon of deadline. + */ public String getIcon() { return "D"; } + /** + * Gets timing of deadline. + * + * @return timing of deadline. + */ public String getTiming() { return "(by:" + timing + ")"; } + /** + * Gets time of deadline. + * + * @return time of deadline. + */ public String getTime() { return timing; } + /** + * Returns string representing event. + * + * @return String representing event. + */ @Override public String toString() { return "[" + getIcon() + "]" + super.toString() + getTiming(); diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index ca7ad5a47..bd8a5104d 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -3,27 +3,61 @@ public class Event extends Task { protected String timing; + /** + * Constructs Event class given description and timing. + * + * @param description Description of event. + * @param timing Timing of event. + */ public Event(String description, String timing) { super(description); this.timing = timing; } + /** + * Constructs Event class given description, timing and status. + * + * @param description Description of event. + * @param timing Timing of event. + * @param isDone Status of event. + */ public Event(String description, String timing, boolean isDone) { this(description, timing); super.taskDone(isDone); } + /** + * Gets icon of event. + * + * @return Icon of event. + */ public String getIcon() { return "E"; } + /** + * Gets timing of event. + * + * @return Timing of event. + */ public String getTiming() { return "(at:" + timing + ")"; } + /** + * Gets time of event. + * + * @return Time of event. + */ public String getTime() { return timing; } + + /** + * Returns string representing event. + * + * @return String representing event. + */ @Override public String toString() { return "[" + getIcon() + "]" + super.toString() + getTiming(); diff --git a/src/main/java/duke/task/Parser.java b/src/main/java/duke/task/Parser.java index aed9629c1..727476e41 100644 --- a/src/main/java/duke/task/Parser.java +++ b/src/main/java/duke/task/Parser.java @@ -16,6 +16,12 @@ public class Parser { private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; + /** + * Parses input and executes respective command. + * + * @param input Input to be parsed. + * @return False if "bye" command not entered. + */ public static boolean parse(String input) { String[] params = input.split(" "); String command = params[0]; @@ -30,7 +36,7 @@ public static boolean parse(String input) { case DONE: int taskNumber = Integer.parseInt(params[1]); System.out.println(ADD_SUCCESS); - TaskManager.checkDone(params); + TaskManager.setDone(params); System.out.println(" " + TaskManager.getName(taskNumber)); break; case TO_DO: diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 116b09f4a..084b37d35 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -4,42 +4,86 @@ public class Task { protected String description; protected boolean isDone; + /** + * Constructs task class given description. + * + * @param description Description of task. + */ public Task(String description) { this.description = description; this.isDone = false; } + /** + * Sets task as done. + * + * @param isDone Status of task. + */ public void taskDone(boolean isDone) { this.isDone = isDone; } + /** + * Gets status icon of task. + * + * @return Status icon of task. + */ public String getStatusIcon() { return (isDone ? "[X]" : "[ ]"); } + /** + * Gets status of task. + * + * @return Status of task. + */ public String getStatus() { return (isDone ? "1" : "0"); } + /** + * Gets icon of task. + * + * @return Icon of task. + */ public String getIcon() { return ""; } + /** + * Gets Timing of task. + * + * @return timing of task. + */ public String getTiming() { return ""; } + /** + * Gets description of task. + * + * @return Description of task. + */ public String getDescription() { return description; } + /** + * Gets time of task + * + * @return Time of task. + */ public String getTime() { return ""; } + /** + * Returns a string representing the task. + * + * @return String representing task. + */ public String toString() { return getStatusIcon() + " " + this.description ; } - } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index e6f325b54..ccf31f602 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -23,9 +23,10 @@ public TaskManager() { } /** - * Adds task to taskList ArrayList + * Adds task to taskList based on their task Type. * - * @param task Task to be checked + * @param task Command input. + * @throws DukeException If there is no input after command. */ public static void add(String task) throws DukeException { String[] command = task.split(" "); @@ -54,36 +55,77 @@ public static void add(String task) throws DukeException { printSize(); } + /** + * Adds ToDo to taskList given description. + * + * @param description Description of ToDo. + */ public static void addToDo(String description) { taskList.add(new ToDo(description)); System.out.println(ADDED_TASK + new ToDo(description)); } + /** + * Loads ToDo from file to taskList without printing. + * + * @param description Description of ToDo. + * @param isDone Previous status of ToDo from file. + */ public static void loadToDoFromFile(String description, boolean isDone) { taskList.add(new ToDo(description, isDone)); } + /** + * Adds Deadline to taskList given description. + * + * @param description Description of deadline. + * @param time Time of deadline. + */ public static void addDeadline(String description, String time) { taskList.add(new Deadline(description, time)); System.out.println(ADDED_TASK + new Deadline(description, time)); } + /** + * Loads Deadline from file to taskList without printing. + * + * @param description Description of Deadline. + * @param time Time of Deadline. + * @param isDone Previous status of Deadline from file. + */ public static void loadDeadlineFromFile(String description, String time, boolean isDone) { taskList.add(new Deadline(description, time, isDone)); } + /** + * Adds Event to taskList given description. + * + * @param description Description of event. + * @param time Time of event. + */ public static void addEvent(String description, String time) { taskList.add(new Event(description, time)); System.out.println(ADDED_TASK + new Event(description, time)); } + /** + * Loads Event from file to taskList without printing. + * + * @param description Description of Event. + * @param time Time of Event. + * @param isDone Previous status of Event from file. + */ public static void loadEventFromFile(String description, String time, boolean isDone) { taskList.add(new Event(description, time, isDone)); } - //set task as done - public static void checkDone(String[] command) { - //catch if input after done is integer + /** + * Sets task to done. + * + * @param command Array of command for the task. + * @throws NumberFormatException If input is not in numeric type. + */ + public static void setDone(String[] command) throws NumberFormatException{ try { Integer.parseInt(command[1]); } catch (NumberFormatException e) { @@ -92,6 +134,12 @@ public static void checkDone(String[] command) { taskList.get(Integer.parseInt(command[1]) - 1).taskDone(true); } + /** + * Gets description of given task. + * + * @param task Task where description is needed. + * @return Description of task. + */ public static String getDescription(String task) { String description; int separator; @@ -110,19 +158,22 @@ public static String getDescription(String task) { } /** - * Returns type of Task given command - * @param command - * @return Type of Task + * Returns type of Task given command. + * + * @param input Input used to obtain task type. + * @return Type of Task. */ - public static String getCommand(String command) { - String[] task = command.split(" "); + public static String getCommand(String input) { + String[] task = input.split(" "); String taskType = task[0]; return taskType; } - + /** + * Prints number of tasks in list + */ public static void printSize() { if (getSize() == 0) { System.out.println(" Now you have 0 task in the list."); @@ -143,10 +194,17 @@ public static String getName(int index) { return taskList.get(index - 1).toString(); } + /** + * Returns size of list. + * @return Size of list + */ public static int getSize() { return taskList.size(); } + /** + * Lists out all the tasks in taskList + */ public static void list() { try { if (taskList.size() == 0) { @@ -196,6 +254,10 @@ public static String getDate(String description) { return date; } + /** + * Deletes task given task index + * @param input Input with the index of task to be deleted + */ public static void deleteTask(String[] input) { int taskToDelete = Integer.parseInt(input[1]); Task thisTask = taskList.get(taskToDelete - 1); diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java index 9671cec55..0740eb67a 100644 --- a/src/main/java/duke/task/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -1,19 +1,40 @@ package duke.task; public class ToDo extends Task { + /** + * Constructs a ToDo task given description. + * + * @param description Description of task. + */ public ToDo(String description) { super(description); } + /** + * Constructs a Todo task given description and isDone status. + * + * @param description Description of task. + * @param isDone Status of task. + */ public ToDo(String description, boolean isDone) { this(description); super.taskDone(isDone); } + /** + * Returns icon of Todo. + * + * @return Icon of Todo + */ public String getIcon() { return "T"; } + /** + * Returns a string representing the ToDo. + * + * @return String representing ToDo. + */ @Override public String toString() { return "[" + getIcon() + "]"+ super.toString(); From 433daa2acca321f25c26c1d05e900e0c855269d4 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Wed, 29 Sep 2021 16:18:20 +0800 Subject: [PATCH 22/30] Testing user guide --- docs/README.md | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..de1ed3c2b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,15 +1,42 @@ # User Guide - +toDoList is a Command Line Interface (CLI) application that +helps you to manage your tasks. + +## Contents of User Guide +- [Features] (#user-content-features) + 1. [`list` - List all tasks] (#user-content-listing-all-tasks) + 2. [`todo` - Create a todo task] (#user-content-adding-a-todo) + 3. [`deadline` - Create a deadline task] (#user-content-adding-a-deadline) + 4. [`event` - Create an event task] (#user-content-adding-an-event) + 5. [`done` - Mark task as done] (#user-content-mark-as-done) + 6. [`delete` - Delete task from list] (#user-content-delete-task) + 7. [`find` - Find tasks based on keyword] (#user-content-find-task) + 8. [`bye` - Exit program] (#user-content-exit-program) ## Features +toDoList supports 3 types of tasks: `To-Do`, `Deadline` and `Event`. -### Feature-ABC +### Listing all tasks Description of the feature. -### Feature-XYZ +### Adding a Todo Description of the feature. +### Adding a Deadline + +### Adding an Event + +### Mark as done + +### Delete task + +### Find task + +### Exit program + + + ## Usage ### `Keyword` - Describe action From c89f1a976a76dfa75ef34f02ef55cafce505e794 Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Wed, 29 Sep 2021 16:19:50 +0800 Subject: [PATCH 23/30] test --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index de1ed3c2b..44f1df023 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,7 +4,7 @@ helps you to manage your tasks. ## Contents of User Guide - [Features] (#user-content-features) - 1. [`list` - List all tasks] (#user-content-listing-all-tasks) + 1. [`list` - List all tasks] (#listing-all-tasks) 2. [`todo` - Create a todo task] (#user-content-adding-a-todo) 3. [`deadline` - Create a deadline task] (#user-content-adding-a-deadline) 4. [`event` - Create an event task] (#user-content-adding-an-event) From 86057c286d833fc670ddcd96851a261bf2dd1a6c Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Wed, 29 Sep 2021 16:53:03 +0800 Subject: [PATCH 24/30] test user guide --- docs/README.md | 130 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 106 insertions(+), 24 deletions(-) diff --git a/docs/README.md b/docs/README.md index 44f1df023..cd600fc20 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,53 +4,135 @@ helps you to manage your tasks. ## Contents of User Guide - [Features] (#user-content-features) - 1. [`list` - List all tasks] (#listing-all-tasks) - 2. [`todo` - Create a todo task] (#user-content-adding-a-todo) - 3. [`deadline` - Create a deadline task] (#user-content-adding-a-deadline) - 4. [`event` - Create an event task] (#user-content-adding-an-event) - 5. [`done` - Mark task as done] (#user-content-mark-as-done) - 6. [`delete` - Delete task from list] (#user-content-delete-task) - 7. [`find` - Find tasks based on keyword] (#user-content-find-task) - 8. [`bye` - Exit program] (#user-content-exit-program) + 1. [`list` - List all tasks](#listing-all-tasks) + 2. [`todo` - Create a todo task](#user-content-adding-a-todo) + 3. [`deadline` - Create a deadline task](#user-content-adding-a-deadline) + 4. [`event` - Create an event task](#user-content-adding-an-event) + 5. [`done` - Mark task as done](#user-content-mark-as-done) + 6. [`delete` - Delete task from list](#user-content-delete-task) + 7. [`find` - Find tasks based on keyword](#user-content-find-task) + 8. [`bye` - Exit program](#user-content-exit-program) ## Features toDoList supports 3 types of tasks: `To-Do`, `Deadline` and `Event`. ### Listing all tasks -Description of the feature. +Shows a list of all tasks. -### Adding a Todo +Format: `list` -Description of the feature. +Example of usage: +``` +list +``` +Expected outcome: +``` + Here are the tasks in your list: + 1.[T][ ] add book + 2.[E][X] see book (at: 1030) + 3.[D][ ] submit work (by: 1040) +``` -### Adding a Deadline +### Adding a Todo +Adds a Todo task -### Adding an Event +Format: `todo ` -### Mark as done +Example of usage: +``` +todo add book +``` +Expected outcome: +``` + Got it. I've added this task: + [T][ ] add book + Now you have 6 tasks in the list. +``` -### Delete task +### Adding a Deadline +Adds a Deadline task, followed by date -### Find task +Format: `deadline /by ` -### Exit program +Example of usage: +``` +deadline finish assignment /by 29/09/21 2359 +``` +Expected outcome: +``` + Got it. I've added this task: + [D][ ] finish assignment (by: 29/09/21 2359) + Now you have 7 tasks in the list. +``` +### Adding an Event +Adds an Event task, followed by date +Format: `event /at ` -## Usage +Example of usage: +``` +event birthday party /at 30/10/21 1800 +``` +Expected outcome: +``` + Got it. I've added this task: + [E][ ] birthday party (at: 30/10/21 1800) + Now you have 8 tasks in the list. +``` -### `Keyword` - Describe action +### Mark as done +Marks a task as done + +Format: `done ` -Describe the action and its outcome. +Example of usage: +``` +done 3 +``` +Expected outcome: +``` + Nice! I've marked this task as done: + [D][X] finish assignment (by: 29/09/21 2359) +``` -Example of usage: +### Delete task +Deletes a task from the list. -`keyword (optional arguments)` +Format: `delete ` +Example of usage: +``` +delete 2 +``` Expected outcome: +``` + Noted. I've removed this task: + [E][ ] birthday party (at: 30/10/21 1800) + Now you have 3 tasks in the list. +``` + +### Find task +Finds tasks given keyword -Description of the outcome. +Format: `find ` +Example of usage: ``` -expected output +find book ``` +Expected outcome: +``` + Here are the matching tasks in your list: + 1.[D][ ] throw book (by: 01/01/22) + 2.[T][ ] buy new book +``` + + +### Exit program +Exits from toDoList. + +Format: `bye` + + + From 60db3bb2655054449f04a59562420b1140f1b05f Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Wed, 29 Sep 2021 17:14:43 +0800 Subject: [PATCH 25/30] Finalise User Guide --- docs/README.md | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/docs/README.md b/docs/README.md index cd600fc20..0c6b0a682 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3,15 +3,17 @@ toDoList is a Command Line Interface (CLI) application that helps you to manage your tasks. ## Contents of User Guide -- [Features] (#user-content-features) +- Features 1. [`list` - List all tasks](#listing-all-tasks) - 2. [`todo` - Create a todo task](#user-content-adding-a-todo) - 3. [`deadline` - Create a deadline task](#user-content-adding-a-deadline) - 4. [`event` - Create an event task](#user-content-adding-an-event) - 5. [`done` - Mark task as done](#user-content-mark-as-done) - 6. [`delete` - Delete task from list](#user-content-delete-task) - 7. [`find` - Find tasks based on keyword](#user-content-find-task) - 8. [`bye` - Exit program](#user-content-exit-program) + 2. [`todo` - Create a todo task](#adding-a-todo) + 3. [`deadline` - Create a deadline task](#adding-a-deadline) + 4. [`event` - Create an event task](#adding-an-event) + 5. [`done` - Mark task as done](#mark-as-done) + 6. [`delete` - Delete task from list](#delete-task) + 7. [`find` - Find tasks based on keyword](#find-task) + 8. [`bye` - Exit program](#exit-program) +- Command Summary + ## Features toDoList supports 3 types of tasks: `To-Do`, `Deadline` and `Event`. @@ -29,8 +31,8 @@ Expected outcome: ``` Here are the tasks in your list: 1.[T][ ] add book - 2.[E][X] see book (at: 1030) - 3.[D][ ] submit work (by: 1040) + 2.[E][X] see book (at: 29/09/21 2359) + 3.[D][ ] submit work (by: 30/10/21 1800) ``` ### Adding a Todo @@ -134,5 +136,16 @@ Exits from toDoList. Format: `bye` +## Command Summary +|Commands|Format, Examples| +|---|---| +|**List**| `list` +|**Todo**| `todo `
eg: `todo add book` +|**Deadline**| `deadline /by `
eg: `deadline finish assignment /by 29/09/21 2359` +|**Event**| `event /at `
eg: `event birthday party /at 30/10/21 1800` +|**Done**| `done `
eg: `done 3` +|**Delete**| `delete `
eg: `delete 2` +|**Find**| `find `
eg: `find book` +|**Bye**| `bye` From b89640a7e75688d27e85e8ab82d70dd73b0c30db Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Thu, 30 Sep 2021 20:51:31 +0800 Subject: [PATCH 26/30] Final touching up --- src/main/java/duke/command/Duke.java | 1 + src/main/java/duke/data/Storage.java | 8 ++-- src/main/java/duke/task/Deadline.java | 2 +- src/main/java/duke/task/Event.java | 3 +- src/main/java/duke/task/Parser.java | 6 ++- src/main/java/duke/task/TaskManager.java | 51 ++++++++++++------------ 6 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/main/java/duke/command/Duke.java b/src/main/java/duke/command/Duke.java index 05a93e618..98b7e88df 100644 --- a/src/main/java/duke/command/Duke.java +++ b/src/main/java/duke/command/Duke.java @@ -1,6 +1,7 @@ package duke.command; import duke.task.TaskManager; + import java.util.Scanner; public class Duke { diff --git a/src/main/java/duke/data/Storage.java b/src/main/java/duke/data/Storage.java index 90974e1dd..3f0312ae5 100644 --- a/src/main/java/duke/data/Storage.java +++ b/src/main/java/duke/data/Storage.java @@ -17,6 +17,8 @@ public class Storage { private static final String PATH_NAME = "data/output.txt"; + private static final String FILE_CREATION_ERROR = "Error in creating file"; + private static final String SEPARATOR = " | "; /** * Creates file inside its directory. @@ -26,7 +28,7 @@ public static void createFile(){ Path path = Paths.get(PATH_NAME); Files.createDirectories(path.getParent()); } catch (IOException e) { - System.out.println("Error in creating file"); + System.out.println(FILE_CREATION_ERROR); } } @@ -92,9 +94,9 @@ public static void writeToFile(String filePath) throws IOException { String description = task.getDescription(); String timing = task.getTime(); - textToAppend = taskType + " | " + status + " | " + description; + textToAppend = taskType + SEPARATOR + status + SEPARATOR + description; if (task instanceof Event || task instanceof Deadline) { - textToAppend += " | " + timing; + textToAppend += SEPARATOR + timing; } textToAppend += "\n"; appendToFile(filePath, textToAppend); diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 6427a0b39..39c84be49 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -41,7 +41,7 @@ public String getIcon() { * @return timing of deadline. */ public String getTiming() { - return "(by:" + timing + ")"; + return " (by: " + timing + ")"; } /** diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 8581fbee4..bc27a5bb7 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -41,7 +41,7 @@ public String getIcon() { * @return Timing of event. */ public String getTiming() { - return "(at:" + timing + ")"; + return " (at: " + timing + ")"; } /** @@ -53,7 +53,6 @@ public String getTime() { return timing; } - /** * Returns string representing event. * diff --git a/src/main/java/duke/task/Parser.java b/src/main/java/duke/task/Parser.java index 79b5db6e9..ca40408cc 100644 --- a/src/main/java/duke/task/Parser.java +++ b/src/main/java/duke/task/Parser.java @@ -15,6 +15,8 @@ public class Parser { private static final String ADD_SUCCESS = " Nice! I've marked this task as done: "; private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; private static final String SHOW_MATCHES = " Here are the matching tasks in your list:"; + private static final String INVALID_COMMAND = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; + private static final String INVALID_TASK_INDEX = " ☹ OOPS!!! The task's index should be an integer."; /** * Parses input and executes respective command. @@ -54,12 +56,12 @@ public static boolean parse(String input) { TaskManager.find(query); break; default: - System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + System.out.println(INVALID_COMMAND); } } catch (DukeException e) { System.out.println(" ☹ OOPS!!! The description of a " + command + " cannot be empty."); } catch (NumberFormatException e) { - System.out.println(" ☹ OOPS!!! The task's index should be an integer."); + System.out.println(INVALID_TASK_INDEX); } return false; } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 40589c4a0..7611b2764 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -12,46 +12,51 @@ public class TaskManager { private static final String DEADLINE = "deadline"; private static final String EVENT = "event"; private static final String ADDED_TASK = " Got it. I've added this task:\n "; + private static final String ENTER_DATE = " ☹ OOPS!!! Please enter a date"; + private static final String OUT_OF_BOUNDS = "Error! Index out of bounds"; + private static final String EMPTY_LIST = " ☹ OOPS!!! List is empty"; + private static final String SHOW_TASKS = " Here are the tasks in your list:"; + private static final String INVALID_COMMAND = " Invalid command, please try again"; private static final String BY_SEPARATOR = "/by"; private static final String AT_SEPARATOR = "/at"; private static final int TODO_STRING_LENGTH = 5; private static final int DEADLINE_STRING_LENGTH = 9; private static final int EVENT_STRING_LENGTH = 6; private static final int SEPARATOR_STRING_LENGTH = 3; + private static final int MINIMUM_COMMAND_LENGTH = 2; public TaskManager() { this.taskList = new ArrayList<>(); } - /** * Adds task to taskList based on their task Type. * - * @param task Command input. + * @param input Command input. * @throws DukeException If there is no input after command. */ - public static void add(String task) throws DukeException { - String[] command = task.split(" "); - if (command.length < 2) { + public static void add(String input) throws DukeException { + String[] command = input.split(" "); + if (command.length < MINIMUM_COMMAND_LENGTH) { throw new DukeException(); } - String taskType = getCommand(task); - String description = getDescription(task); + String taskType = getCommand(input); + String description = getDescription(input); String date; switch (taskType) { case TO_DO: addToDo(description); break; case DEADLINE: - date = getDate(task); + date = getDate(input); addDeadline(description, date); break; case EVENT: - date = getDate(task); + date = getDate(input); addEvent(description, date); break; default: - System.out.println(" Invalid command, please try again"); + System.out.println(INVALID_COMMAND); return; } printSize(); @@ -121,20 +126,19 @@ public static void loadEventFromFile(String description, String time, boolean is taskList.add(new Event(description, time, isDone)); } - /** * Sets task to done. * - * @param command Array of command for the task. + * @param input Array of command for the task. * @throws NumberFormatException If input is not in numeric type. */ - public static void setDone(String[] command) throws NumberFormatException{ + public static void setDone(String[] input) throws NumberFormatException{ try { - Integer.parseInt(command[1]); + Integer.parseInt(input[1]); } catch (NumberFormatException e) { throw new NumberFormatException(); } - taskList.get(Integer.parseInt(command[1]) - 1).taskDone(true); + taskList.get(Integer.parseInt(input[1]) - 1).taskDone(true); } /** @@ -143,7 +147,6 @@ public static void setDone(String[] command) throws NumberFormatException{ * @param input Task where description is needed. * @return Description of task. */ - public static String getDescription(String input) { String description; int separator; @@ -174,11 +177,9 @@ public static String getCommand(String input) { return taskType; } - /** * Prints number of tasks in list */ - public static void printSize() { if (getSize() == 0) { System.out.println(" Now you have 0 task in the list."); @@ -213,16 +214,16 @@ public static int getSize() { public static void list() { try { if (taskList.size() == 0) { - System.out.println(" ☹ OOPS!!! List is empty"); + System.out.println(EMPTY_LIST); return; } - System.out.println(" Here are the tasks in your list:"); + System.out.println(SHOW_TASKS); for (int i = 0; i < taskList.size(); i++) { Task t = taskList.get(i); System.out.println(" " + (i + 1) + "." + t); } } catch (IndexOutOfBoundsException i) { - System.out.println("Error! Index out of bounds"); + System.out.println(OUT_OF_BOUNDS); } } @@ -240,16 +241,16 @@ public static String getDate(String description) { switch (taskType) { case DEADLINE: indexOfSeparator = description.indexOf(BY_SEPARATOR); - date = description.substring(indexOfSeparator + SEPARATOR_STRING_LENGTH); + date = description.substring(indexOfSeparator + SEPARATOR_STRING_LENGTH + 1); if (date.isEmpty()) { - System.out.println(" ☹ OOPS!!! Please enter a date"); + System.out.println(ENTER_DATE); } break; case EVENT: indexOfSeparator = description.indexOf(AT_SEPARATOR); - date = description.substring(indexOfSeparator + SEPARATOR_STRING_LENGTH); + date = description.substring(indexOfSeparator + SEPARATOR_STRING_LENGTH + 1); if (date.isEmpty()) { - System.out.println(" ☹ OOPS!!! Please enter a date"); + System.out.println(ENTER_DATE); } break; default: From 8fbb849f302259715d445908d97da2c4b3ce1e1d Mon Sep 17 00:00:00 2001 From: Zhihuang Wang Date: Fri, 1 Oct 2021 12:35:49 +0800 Subject: [PATCH 27/30] Update README.md --- docs/README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/README.md b/docs/README.md index 0c6b0a682..501c26159 100644 --- a/docs/README.md +++ b/docs/README.md @@ -137,15 +137,15 @@ Exits from toDoList. Format: `bye` ## Command Summary -|Commands|Format, Examples| -|---|---| -|**List**| `list` -|**Todo**| `todo `
eg: `todo add book` -|**Deadline**| `deadline /by `
eg: `deadline finish assignment /by 29/09/21 2359` -|**Event**| `event /at `
eg: `event birthday party /at 30/10/21 1800` -|**Done**| `done `
eg: `done 3` -|**Delete**| `delete `
eg: `delete 2` -|**Find**| `find `
eg: `find book` -|**Bye**| `bye` +Commands|Format, Examples +--------|---------------- +**List**| `list` +**Todo**| `todo `
eg: `todo add book` +**Deadline**| `deadline /by `
eg: `deadline finish assignment /by 29/09/21 2359` +**Event**| `event /at `
eg: `event birthday party /at 30/10/21 1800` +**Done**| `done `
eg: `done 3` +**Delete**| `delete `
eg: `delete 2` +**Find**| `find `
eg: `find book` +**Bye**| `bye` From 1e3f1a908c0f1748332e710f1c48fbb25e2ac881 Mon Sep 17 00:00:00 2001 From: zh1huang <59475240+zh1huang@users.noreply.github.com> Date: Fri, 1 Oct 2021 13:06:42 +0800 Subject: [PATCH 28/30] Update README.md --- docs/README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/README.md b/docs/README.md index 501c26159..810ffa6a4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -137,15 +137,15 @@ Exits from toDoList. Format: `bye` ## Command Summary -Commands|Format, Examples ---------|---------------- -**List**| `list` -**Todo**| `todo `
eg: `todo add book` -**Deadline**| `deadline /by `
eg: `deadline finish assignment /by 29/09/21 2359` -**Event**| `event /at `
eg: `event birthday party /at 30/10/21 1800` -**Done**| `done `
eg: `done 3` -**Delete**| `delete `
eg: `delete 2` -**Find**| `find `
eg: `find book` -**Bye**| `bye` +|Commands |Format, Examples | +|:--- |:--- | +|**List** | `list` | +|**Todo** | `todo `
eg: `todo add book` | +|**Deadline**| `deadline /by `
eg: `deadline finish assignment /by 29/09/21 2359`| +|**Event** | `event /at `
eg: `event birthday party /at 30/10/21 1800` | +|**Done** | `done `
eg: `done 3` | +|**Delete** | `delete `
eg: `delete 2` | +|**Find** | `find `
eg: `find book` | +|**Bye** | `bye` | From e99c7f834e6a265cff5258bf439cb25142b645a8 Mon Sep 17 00:00:00 2001 From: zh1huang <59475240+zh1huang@users.noreply.github.com> Date: Fri, 1 Oct 2021 16:03:21 +0800 Subject: [PATCH 29/30] Update README.md --- docs/README.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/docs/README.md b/docs/README.md index 810ffa6a4..b84ecf767 100644 --- a/docs/README.md +++ b/docs/README.md @@ -136,16 +136,17 @@ Exits from toDoList. Format: `bye` -## Command Summary -|Commands |Format, Examples | -|:--- |:--- | -|**List** | `list` | -|**Todo** | `todo `
eg: `todo add book` | -|**Deadline**| `deadline /by `
eg: `deadline finish assignment /by 29/09/21 2359`| -|**Event** | `event /at `
eg: `event birthday party /at 30/10/21 1800` | -|**Done** | `done `
eg: `done 3` | -|**Delete** | `delete `
eg: `delete 2` | -|**Find** | `find `
eg: `find book` | -|**Bye** | `bye` | +## Command Summary + +|Commands |Format, Examples | +| ---- | ---- | +|**List** | list | +|**Todo** | todo
eg: todo add book | +|**Deadline**| deadline /by
eg: `deadline finish assignment /by 29/09/21 2359`| +|**Event** | event /at
eg: event birthday party /at 30/10/21 1800 | +|**Done** | done
eg: done 3 | +|**Delete** | delete
eg: delete 2 | +|**Find** | find
eg: find book | +|**Bye** | bye | From ab2da158b4573bd9ce788871bebf89d7f1d73281 Mon Sep 17 00:00:00 2001 From: zh1huang <59475240+zh1huang@users.noreply.github.com> Date: Fri, 1 Oct 2021 16:05:37 +0800 Subject: [PATCH 30/30] Update README.md --- docs/README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/README.md b/docs/README.md index b84ecf767..1ab36f660 100644 --- a/docs/README.md +++ b/docs/README.md @@ -138,15 +138,15 @@ Format: `bye` ## Command Summary -|Commands |Format, Examples | -| ---- | ---- | -|**List** | list | -|**Todo** | todo
eg: todo add book | -|**Deadline**| deadline /by
eg: `deadline finish assignment /by 29/09/21 2359`| -|**Event** | event /at
eg: event birthday party /at 30/10/21 1800 | -|**Done** | done
eg: done 3 | -|**Delete** | delete
eg: delete 2 | -|**Find** | find
eg: find book | -|**Bye** | bye | +|Commands |Format, Examples | +| ---- | ---- | +|**List** | `list` | +|**Todo** | `todo `
eg: `todo add book` | +|**Deadline**| `deadline /by `
eg: `deadline finish assignment /by 29/09/21 2359`| +|**Event** | `event /at `
eg: `event birthday party /at 30/10/21 1800` | +|**Done** | `done `
eg: `done 3` | +|**Delete** | `delete `
eg: `delete 2` | +|**Find** | `find `
eg: `find book` | +|**Bye** | `bye` |