From aae34ba7915c6075b99091b82955f37e40d77989 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 19 Aug 2022 01:18:11 +0800 Subject: [PATCH 01/26] Add Increment: Level-0 --- 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 5d313334..f4ad96e5 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 logo = "____________________________________________________________\n" + + "Hello! I'm Duke\n" + + "What can I do for you?\\\n" + + "____________________________________________________________\n" + + "Bye. Hope to see you again soon!\n" + + "____________________________________________________________\n"; + System.out.println(logo); } } From 5701ddc97f05f7313996f2d28d13695fe09af13d Mon Sep 17 00:00:00 2001 From: John Date: Fri, 19 Aug 2022 01:59:56 +0800 Subject: [PATCH 02/26] Add Level-1 --- src/main/java/Duke.java | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f4ad96e5..958f9eab 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,11 +1,19 @@ +import java.util.Locale; +import java.util.Scanner; + public class Duke { public static void main(String[] args) { - String logo = "____________________________________________________________\n" - + "Hello! I'm Duke\n" - + "What can I do for you?\\\n" - + "____________________________________________________________\n" - + "Bye. Hope to see you again soon!\n" - + "____________________________________________________________\n"; - System.out.println(logo); + String welcomeText = "Hello! I am not Duke.\n" + + "I can echo you now.\n"; + System.out.println(welcomeText); + Scanner scanner = new Scanner(System.in); + while (true) { + String input = scanner.nextLine(); + if (input.toLowerCase().trim().equals("bye")) { + break; + } + System.out.println(input); + } + System.out.println("Byebye!!!"); } } From ba4ed0c556d464e73aa17c3cda10440eea39a509 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 20 Aug 2022 00:33:19 +0800 Subject: [PATCH 03/26] "Add Level-2" Improved code formatting --- src/main/java/Duke.java | 55 +++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 958f9eab..032cc818 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,18 +2,59 @@ import java.util.Scanner; public class Duke { - public static void main(String[] args) { - String welcomeText = "Hello! I am not Duke.\n" - + "I can echo you now.\n"; + public static void WelcomeUser() { + String welcomeText = "-------------------------------------\n" + + "Hello! I am not Duke.\n" + + "I can echo you now.\n" + + "-------------------------------------"; System.out.println(welcomeText); + } + + public static String CleanInput(String input) { + return input.toLowerCase().trim(); + } + + public static void ListWords(String[] list, int listSize) { + if (listSize != 0) { + for (int i = 0; i < listSize; i++) { + System.out.println((i + 1) + ": " + list[i]); + } + } + else { + System.out.println("The list is empty, Please add something!"); + } + } + + public static void AddWord(String[] wordList, String word, int pos) { + wordList[pos] = word; + System.out.println("Added \"" + word + "\""); + } + + public static void TrackInput() { Scanner scanner = new Scanner(System.in); + String[] wordList = new String[100]; + final String LINEDIVIDER = "-------------------------------------"; + int wordCount = 0; while (true) { - String input = scanner.nextLine(); - if (input.toLowerCase().trim().equals("bye")) { + String input = CleanInput(scanner.nextLine()); + System.out.println(LINEDIVIDER); + if (input.equals("bye")) { + System.out.println("Bye bye!!! \n" + LINEDIVIDER + "\n"); break; } - System.out.println(input); + else if (input.equals("list")) { + ListWords(wordList, wordCount); + } + else { + AddWord(wordList, input, wordCount); + wordCount++; + } + System.out.println(LINEDIVIDER); } - System.out.println("Byebye!!!"); + } + + public static void main(String[] args) { + WelcomeUser(); + TrackInput(); } } From 2ab974a4d370feac22168c050f246b8dc70007fc Mon Sep 17 00:00:00 2001 From: John Date: Sat, 20 Aug 2022 16:28:11 +0800 Subject: [PATCH 04/26] Add Level-3 Duke can now add tasks and mark/unmark them --- src/main/java/Duke.java | 51 +++++++++++++++++++++++++---------------- src/main/java/Task.java | 27 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 032cc818..9a018025 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -5,7 +5,13 @@ public class Duke { public static void WelcomeUser() { String welcomeText = "-------------------------------------\n" + "Hello! I am not Duke.\n" - + "I can echo you now.\n" + + "Here are a list of commands: \n" + + "\tadd : adds a task to the list\n" + + "\tlist : shows list of tasks\n" + + "\tmark : marks a task \n" + + "\tunmark : unmarks a task \n" + + "\tbye : exits the program \n" + + "I WILL CRASH IF YOU DO NOT COMPLY!!!\n" + "-------------------------------------"; System.out.println(welcomeText); } @@ -14,40 +20,45 @@ public static String CleanInput(String input) { return input.toLowerCase().trim(); } - public static void ListWords(String[] list, int listSize) { - if (listSize != 0) { - for (int i = 0; i < listSize; i++) { - System.out.println((i + 1) + ": " + list[i]); + public static void ListTasks(Task[] list) { + if (Task.getTaskCount() != 0) { + for (int i = 0; i < Task.getTaskCount(); i++) { + System.out.println(list[i].getStatusIcon() + (i + 1) + ": " + list[i].getDescription()); } - } - else { - System.out.println("The list is empty, Please add something!"); + } else { + System.out.println("The list is empty, please add something!"); } } - public static void AddWord(String[] wordList, String word, int pos) { - wordList[pos] = word; - System.out.println("Added \"" + word + "\""); + public static void AddTask(Task[] taskList, String taskDescription) { + taskList[Task.getTaskCount()] = new Task(taskDescription); + System.out.println("Added \"" + taskDescription + "\""); } public static void TrackInput() { Scanner scanner = new Scanner(System.in); - String[] wordList = new String[100]; + Task[] taskList = new Task[100]; final String LINEDIVIDER = "-------------------------------------"; - int wordCount = 0; while (true) { String input = CleanInput(scanner.nextLine()); System.out.println(LINEDIVIDER); if (input.equals("bye")) { System.out.println("Bye bye!!! \n" + LINEDIVIDER + "\n"); break; - } - else if (input.equals("list")) { - ListWords(wordList, wordCount); - } - else { - AddWord(wordList, input, wordCount); - wordCount++; + } else if (input.equals("list")) { + ListTasks(taskList); + } else if (input.startsWith("add ")) { + AddTask(taskList, input.substring(4)); + } else if (input.startsWith("mark ")) { + int toMark = Integer.parseInt(input.substring(5)) - 1; + System.out.println(taskList[toMark].getDescription() + " marked"); + taskList[toMark].setDone(true); + } else if (input.startsWith("unmark ")) { + int toMark = Integer.parseInt(input.substring(7)) - 1; + System.out.println(taskList[toMark].getDescription() + " unmarked"); + taskList[toMark].setDone(false); + } else { + System.out.println("Invalid command."); } System.out.println(LINEDIVIDER); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 00000000..82d21b35 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,27 @@ +public class Task { + protected String description; + protected boolean isDone; + protected static int taskCount = 0; + + public Task(String description) { + this.description = description; + this.isDone = false; + Task.taskCount++; + } + + public void setDone(boolean isDone) { + this.isDone = isDone; + } + + public String getStatusIcon() { + return isDone ? "[X]" : "[ ]"; + } + + public static int getTaskCount() { + return taskCount; + } + + public String getDescription() { + return description; + } +} From 8df32c3d4cac65d267aefa712d06d4b911711d75 Mon Sep 17 00:00:00 2001 From: jjtoh Date: Wed, 24 Aug 2022 22:08:01 +0800 Subject: [PATCH 05/26] Add A-CodingStandard Edited some code to align it with the given stardards --- src/main/java/Duke.java | 82 ++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 9a018025..3d3ee392 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,8 +1,10 @@ -import java.util.Locale; import java.util.Scanner; public class Duke { - public static void WelcomeUser() { + /** + * Prints the welcome message + */ + public static void welcomeUser() { String welcomeText = "-------------------------------------\n" + "Hello! I am not Duke.\n" + "Here are a list of commands: \n" @@ -16,11 +18,24 @@ public static void WelcomeUser() { System.out.println(welcomeText); } - public static String CleanInput(String input) { + /** + * Sets input to lowercase and removes any + * leading and trailing whitespaces. + * + * @param input Input entered by user. + * @return Lowercase and trimmed input. + */ + public static String cleanInput(String input) { return input.toLowerCase().trim(); } - public static void ListTasks(Task[] list) { + /** + * Prints the current list of tasks. + * If list is empty, print error message. + * + * @param list List of tasks. + */ + public static void listTasks(Task[] list) { if (Task.getTaskCount() != 0) { for (int i = 0; i < Task.getTaskCount(); i++) { System.out.println(list[i].getStatusIcon() + (i + 1) + ": " + list[i].getDescription()); @@ -30,42 +45,67 @@ public static void ListTasks(Task[] list) { } } - public static void AddTask(Task[] taskList, String taskDescription) { + /** + * Adds a task to the list of tasks. + * + * @param taskList The list of tasks. + * @param taskDescription The description of the task to be added. + */ + public static void addTask(Task[] taskList, String taskDescription) { taskList[Task.getTaskCount()] = new Task(taskDescription); System.out.println("Added \"" + taskDescription + "\""); } - public static void TrackInput() { + /** + * Marks or unmarks the task that user chooses. + * + * @param isMark User's choice to mark or unmark. + * @param taskList List of tasks. + * @param input User's input. + */ + public static void markTask(Boolean isMark, Task[] taskList, String input) { + if (isMark) { + int toMark = Integer.parseInt(input.substring(5)) - 1; + System.out.println(taskList[toMark].getDescription() + " marked"); + taskList[toMark].setDone(true); + } else { + int toMark = Integer.parseInt(input.substring(7)) - 1; + System.out.println(taskList[toMark].getDescription() + " unmarked"); + taskList[toMark].setDone(false); + } + } + + /** + * Method that takes in the user's input to allow them + * to interact with Duke. + */ + public static void trackInput() { Scanner scanner = new Scanner(System.in); Task[] taskList = new Task[100]; - final String LINEDIVIDER = "-------------------------------------"; + final String LINE_DIVIDER = "-------------------------------------"; while (true) { - String input = CleanInput(scanner.nextLine()); - System.out.println(LINEDIVIDER); + String input = cleanInput(scanner.nextLine()); + System.out.println(LINE_DIVIDER); if (input.equals("bye")) { - System.out.println("Bye bye!!! \n" + LINEDIVIDER + "\n"); + System.out.println("Bye bye!!! \n" + LINE_DIVIDER + "\n"); break; } else if (input.equals("list")) { - ListTasks(taskList); + listTasks(taskList); } else if (input.startsWith("add ")) { - AddTask(taskList, input.substring(4)); + addTask(taskList, input.substring(4)); } else if (input.startsWith("mark ")) { - int toMark = Integer.parseInt(input.substring(5)) - 1; - System.out.println(taskList[toMark].getDescription() + " marked"); - taskList[toMark].setDone(true); + markTask(true, taskList, input); } else if (input.startsWith("unmark ")) { - int toMark = Integer.parseInt(input.substring(7)) - 1; - System.out.println(taskList[toMark].getDescription() + " unmarked"); - taskList[toMark].setDone(false); + markTask(false, taskList, input); } else { System.out.println("Invalid command."); } - System.out.println(LINEDIVIDER); + System.out.println(LINE_DIVIDER); } } public static void main(String[] args) { - WelcomeUser(); - TrackInput(); + welcomeUser(); + trackInput(); } } From e8327470e700b3a4c61bc3d24568596dc569ab5c Mon Sep 17 00:00:00 2001 From: John Date: Tue, 30 Aug 2022 03:21:39 +0800 Subject: [PATCH 06/26] Add new java classes for level 4 --- src/main/java/Deadline.java | 2 ++ src/main/java/Event.java | 2 ++ src/main/java/Todo.java | 2 ++ 3 files changed, 6 insertions(+) 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 00000000..58f2892a --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,2 @@ +public class Deadline extends Task { +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 00000000..ed93f04c --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,2 @@ +public class Event extends Task { +} diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 00000000..62691524 --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,2 @@ +public class Todo extends Task { +} From 751198dbf59dfe6600415d800dec4791ba1a322e Mon Sep 17 00:00:00 2001 From: John Date: Wed, 31 Aug 2022 03:01:57 +0800 Subject: [PATCH 07/26] Add some incomplete code for level 4 --- src/main/java/Deadline.java | 11 ++++ src/main/java/Duke.java | 102 +------------------------------- src/main/java/Event.java | 12 ++++ src/main/java/InputHandler.java | 33 +++++++++++ src/main/java/TaskList.java | 30 ++++++++++ src/main/java/Todo.java | 8 +++ src/main/java/UI.java | 23 +++++++ 7 files changed, 119 insertions(+), 100 deletions(-) create mode 100644 src/main/java/InputHandler.java create mode 100644 src/main/java/TaskList.java create mode 100644 src/main/java/UI.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 58f2892a..573b7b87 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,2 +1,13 @@ public class Deadline extends Task { + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + by + ")"; + } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3d3ee392..aadc28e1 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,107 +1,9 @@ import java.util.Scanner; public class Duke { - /** - * Prints the welcome message - */ public static void welcomeUser() { - String welcomeText = "-------------------------------------\n" - + "Hello! I am not Duke.\n" - + "Here are a list of commands: \n" - + "\tadd : adds a task to the list\n" - + "\tlist : shows list of tasks\n" - + "\tmark : marks a task \n" - + "\tunmark : unmarks a task \n" - + "\tbye : exits the program \n" - + "I WILL CRASH IF YOU DO NOT COMPLY!!!\n" - + "-------------------------------------"; - System.out.println(welcomeText); - } - - /** - * Sets input to lowercase and removes any - * leading and trailing whitespaces. - * - * @param input Input entered by user. - * @return Lowercase and trimmed input. - */ - public static String cleanInput(String input) { - return input.toLowerCase().trim(); - } - - /** - * Prints the current list of tasks. - * If list is empty, print error message. - * - * @param list List of tasks. - */ - public static void listTasks(Task[] list) { - if (Task.getTaskCount() != 0) { - for (int i = 0; i < Task.getTaskCount(); i++) { - System.out.println(list[i].getStatusIcon() + (i + 1) + ": " + list[i].getDescription()); - } - } else { - System.out.println("The list is empty, please add something!"); - } - } - - /** - * Adds a task to the list of tasks. - * - * @param taskList The list of tasks. - * @param taskDescription The description of the task to be added. - */ - public static void addTask(Task[] taskList, String taskDescription) { - taskList[Task.getTaskCount()] = new Task(taskDescription); - System.out.println("Added \"" + taskDescription + "\""); - } - - /** - * Marks or unmarks the task that user chooses. - * - * @param isMark User's choice to mark or unmark. - * @param taskList List of tasks. - * @param input User's input. - */ - public static void markTask(Boolean isMark, Task[] taskList, String input) { - if (isMark) { - int toMark = Integer.parseInt(input.substring(5)) - 1; - System.out.println(taskList[toMark].getDescription() + " marked"); - taskList[toMark].setDone(true); - } else { - int toMark = Integer.parseInt(input.substring(7)) - 1; - System.out.println(taskList[toMark].getDescription() + " unmarked"); - taskList[toMark].setDone(false); - } - } - - /** - * Method that takes in the user's input to allow them - * to interact with Duke. - */ - public static void trackInput() { - Scanner scanner = new Scanner(System.in); - Task[] taskList = new Task[100]; - final String LINE_DIVIDER = "-------------------------------------"; - while (true) { - String input = cleanInput(scanner.nextLine()); - System.out.println(LINE_DIVIDER); - if (input.equals("bye")) { - System.out.println("Bye bye!!! \n" + LINE_DIVIDER + "\n"); - break; - } else if (input.equals("list")) { - listTasks(taskList); - } else if (input.startsWith("add ")) { - addTask(taskList, input.substring(4)); - } else if (input.startsWith("mark ")) { - markTask(true, taskList, input); - } else if (input.startsWith("unmark ")) { - markTask(false, taskList, input); - } else { - System.out.println("Invalid command."); - } - System.out.println(LINE_DIVIDER); - } + UI.welcomeUser(); + InputHandler.handleInput(); } public static void main(String[] args) { diff --git a/src/main/java/Event.java b/src/main/java/Event.java index ed93f04c..c0e8dac9 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,2 +1,14 @@ public class Event extends Task { + protected String at; + + public Event(String description, String at) { + super(description); + this.at = at; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (at: " + at + ")"; + } + } diff --git a/src/main/java/InputHandler.java b/src/main/java/InputHandler.java new file mode 100644 index 00000000..65b0b8bc --- /dev/null +++ b/src/main/java/InputHandler.java @@ -0,0 +1,33 @@ +public class InputHandler { + + private static final String TODO = "todo"; + private static final String LIST = "list"; + private static final String DEADLINE = "deadline"; + private static final String EVENT = "event"; + private static final String BYE = "bye"; + private static final String MARK = "add"; + private static final String UNMARK = "unmark"; + + public static void handleInput() { + while (true) { + String input = UI.getInput(); + if (input.startsWith(BYE)) { + break; + } else if (input.startsWith(TODO)) { + //do something + } else if (input.startsWith(LIST)) { + //do something + } else if (input.startsWith(DEADLINE)) { + //do something + } else if (input.startsWith(EVENT)) { + //do something + } else if (input.startsWith(MARK)) { + //do something + } else if (input.startsWith(UNMARK)) { + //do something + } else { + + } + } + } +} diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 00000000..a4e61bac --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,30 @@ +public class TaskList { + + private Task[] tasks; + private int taskCount = 0; + + public TaskList() { + this.tasks = new Task[100]; + } + + public void addDeadLine(Deadline deadline) { + tasks[taskCount] = deadline; + taskCount++; + } + + public void addTodo(Todo todo) { + tasks[taskCount] = todo; + taskCount++; + } + + public void addEvent(Event event) { + tasks[taskCount] = event; + taskCount++; + } + + public void printList() { + for (int i = 0; i < taskCount; i++) { + System.out.println(tasks[taskCount]); + } + } +} diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index 62691524..eabef3ab 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -1,2 +1,10 @@ public class Todo extends Task { + public Todo(String description) { + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } } diff --git a/src/main/java/UI.java b/src/main/java/UI.java new file mode 100644 index 00000000..6d2f16e8 --- /dev/null +++ b/src/main/java/UI.java @@ -0,0 +1,23 @@ +import java.util.Scanner; + +public class UI { + public static void welcomeUser() { + String welcomeText = "-------------------------------------\n" + + "Hello! I am not Duke.\n" + + "Here are a list of commands: \n" + + "\tadd : adds a task to the list\n" + + "\tlist : shows list of tasks\n" + + "\tmark : marks a task \n" + + "\tunmark : unmarks a task \n" + + "\tbye : exits the program \n" + + "I WILL CRASH IF YOU DO NOT COMPLY!!!\n" + + "-------------------------------------"; + System.out.println(welcomeText); + } + + public static String getInput() { + Scanner sc = new Scanner(System.in); + String input = sc.nextLine(); + return input.trim(); + } +} From ace71c163b9195e074fc062db67a8c85b1067ea4 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 1 Sep 2022 01:25:20 +0800 Subject: [PATCH 08/26] Add Level-4 with some improvements to code quality Coding quality may need further improvement. --- src/main/java/Deadline.java | 2 +- src/main/java/Duke.java | 5 ++-- src/main/java/Event.java | 2 +- src/main/java/InputHandler.java | 17 ++++++------ src/main/java/Task.java | 6 +--- src/main/java/TaskHandler.java | 49 +++++++++++++++++++++++++++++++++ src/main/java/TaskList.java | 47 +++++++++++++++++++++++++------ src/main/java/Todo.java | 2 +- src/main/java/UI.java | 6 ++-- 9 files changed, 106 insertions(+), 30 deletions(-) create mode 100644 src/main/java/TaskHandler.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 573b7b87..bca8eece 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -8,6 +8,6 @@ public Deadline(String description, String by) { @Override public String toString() { - return "[D]" + super.toString() + " (by: " + by + ")"; + return "[D]" + super.getStatusIcon() + " " + super.getDescription() + " (by: " + by + ")"; } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index aadc28e1..dd119855 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,13 +1,12 @@ import java.util.Scanner; public class Duke { - public static void welcomeUser() { + public static void startProgram() { UI.welcomeUser(); InputHandler.handleInput(); } public static void main(String[] args) { - welcomeUser(); - trackInput(); + startProgram(); } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index c0e8dac9..74f053b6 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -8,7 +8,7 @@ public Event(String description, String at) { @Override public String toString() { - return "[E]" + super.toString() + " (at: " + at + ")"; + return "[E]" + super.getStatusIcon()+ " " + super.getDescription() + " (at: " + at + ")"; } } diff --git a/src/main/java/InputHandler.java b/src/main/java/InputHandler.java index 65b0b8bc..6511b11c 100644 --- a/src/main/java/InputHandler.java +++ b/src/main/java/InputHandler.java @@ -5,28 +5,29 @@ public class InputHandler { private static final String DEADLINE = "deadline"; private static final String EVENT = "event"; private static final String BYE = "bye"; - private static final String MARK = "add"; + private static final String MARK = "mark"; private static final String UNMARK = "unmark"; public static void handleInput() { while (true) { String input = UI.getInput(); if (input.startsWith(BYE)) { + TaskHandler.handleBye(); break; } else if (input.startsWith(TODO)) { - //do something + TaskHandler.handleTodo(input); } else if (input.startsWith(LIST)) { - //do something + TaskHandler.handleList(); } else if (input.startsWith(DEADLINE)) { - //do something + TaskHandler.handleDeadline(input); } else if (input.startsWith(EVENT)) { - //do something + TaskHandler.handleEvent(input); } else if (input.startsWith(MARK)) { - //do something + TaskHandler.handleMark(input); } else if (input.startsWith(UNMARK)) { - //do something + TaskHandler.handleUnmark(input); } else { - + TaskHandler.handleInvalid(input); } } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 82d21b35..4010f3ad 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,12 +1,10 @@ public class Task { protected String description; protected boolean isDone; - protected static int taskCount = 0; public Task(String description) { this.description = description; this.isDone = false; - Task.taskCount++; } public void setDone(boolean isDone) { @@ -17,11 +15,9 @@ public String getStatusIcon() { return isDone ? "[X]" : "[ ]"; } - public static int getTaskCount() { - return taskCount; - } public String getDescription() { return description; } + } diff --git a/src/main/java/TaskHandler.java b/src/main/java/TaskHandler.java new file mode 100644 index 00000000..836bba32 --- /dev/null +++ b/src/main/java/TaskHandler.java @@ -0,0 +1,49 @@ +public class TaskHandler { + private static final String BYE_MESSAGE = "Bye! ;)"; + + public static void handleBye() { + System.out.println(BYE_MESSAGE); + } + + public static void handleTodo(String input) { + String[] splitInput = input.split(" ", 2); + Todo todo = new Todo(splitInput[1]); + TaskList.addTodo(todo); + } + + public static void handleList() { + TaskList.printList(); + } + + public static void handleMark(String input) { + String[] splitInput = input.split(" ", 2); + int target = Integer.parseInt(splitInput[1]); + TaskList.markTarget(target); + + } + + public static void handleUnmark(String input) { + String[] splitInput = input.split(" ", 2); + int target = Integer.parseInt(splitInput[1]); + TaskList.unmarkTarget(target); + } + + public static void handleDeadline(String input) { + String[] splitInput = input.split(" ", 2); + String[] splitCommand = splitInput[1].split(" /by ", 2); // 0 is description, 1 is by + Deadline deadline = new Deadline(splitCommand[0], splitCommand[1]); + TaskList.addDeadLine(deadline); + } + + public static void handleEvent(String input) { + String[] splitInput = input.split(" ", 2); + String[] splitCommand = splitInput[1].split(" /at ", 2); // 0 is description, 1 is by + Event event = new Event(splitCommand[0], splitCommand[1]); + TaskList.addEvent(event); + } + + public static void handleInvalid(String input) { + System.out.println("\"" + input + "\"" + " is not a valid command, please try again!"); + } + +} diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index a4e61bac..7d63ef04 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,30 +1,59 @@ public class TaskList { - private Task[] tasks; - private int taskCount = 0; + private static Task[] tasks = new Task[100]; + private static int taskCount = 0; + private static final String LINE_DIVIDER = "-------------------------------------"; + private static final String ROGER_MESSAGE = "Got it. I have added this task:"; - public TaskList() { - this.tasks = new Task[100]; + private static void printRoger(Task task) { + System.out.println(ROGER_MESSAGE); + System.out.println(task); + System.out.println("You now have " + taskCount + " tasks."); + System.out.println(LINE_DIVIDER); } - public void addDeadLine(Deadline deadline) { + private static void printMark(int target, boolean mark) { + if (mark) { + System.out.println("Marked this task: \n " + tasks[target - 1]); + } else { + System.out.println("Unmarked this task: \n" + tasks[target - 1]); + } + System.out.println(LINE_DIVIDER); + } + + public static void addDeadLine(Deadline deadline) { tasks[taskCount] = deadline; taskCount++; + printRoger(deadline); } - public void addTodo(Todo todo) { + public static void addTodo(Todo todo) { tasks[taskCount] = todo; taskCount++; + printRoger(todo); } - public void addEvent(Event event) { + public static void addEvent(Event event) { tasks[taskCount] = event; taskCount++; + printRoger(event); + } + + public static void markTarget(int target) { + tasks[target - 1].setDone(true); + printMark(target, true); + } + + public static void unmarkTarget(int target) { + tasks[target - 1].setDone(false); + printMark(target, false); } - public void printList() { + public static void printList() { + System.out.println("Here are a list of your tasks: "); for (int i = 0; i < taskCount; i++) { - System.out.println(tasks[taskCount]); + System.out.println(i + 1 + ". " + tasks[i]); } + System.out.println(LINE_DIVIDER); } } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index eabef3ab..14671a55 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -5,6 +5,6 @@ public Todo(String description) { @Override public String toString() { - return "[T]" + super.toString(); + return "[T]" + super.getStatusIcon() + " " + super.getDescription(); } } diff --git a/src/main/java/UI.java b/src/main/java/UI.java index 6d2f16e8..dd0bc634 100644 --- a/src/main/java/UI.java +++ b/src/main/java/UI.java @@ -5,10 +5,12 @@ public static void welcomeUser() { String welcomeText = "-------------------------------------\n" + "Hello! I am not Duke.\n" + "Here are a list of commands: \n" - + "\tadd : adds a task to the list\n" - + "\tlist : shows list of tasks\n" + + "\ttodo : adds a Todo to the list\n" + + "\tdeadline /by : adds a deadline and its deadline...\n" + + "\tevent /at : adds an event and its time...\n" + "\tmark : marks a task \n" + "\tunmark : unmarks a task \n" + + "\tlist : lists out all your tasks \n" + "\tbye : exits the program \n" + "I WILL CRASH IF YOU DO NOT COMPLY!!!\n" + "-------------------------------------"; From 5b1fa3667ddd878280f0e4007901d91933233d2e Mon Sep 17 00:00:00 2001 From: John Date: Thu, 1 Sep 2022 02:06:28 +0800 Subject: [PATCH 09/26] Add test cases Has issues running the tests --- src/main/java/UI.java | 10 +-- text-ui-test/EXPECTED.TXT | 138 ++++++++++++++++++++++++++++++++++++-- text-ui-test/input.txt | 32 +++++++++ 3 files changed, 168 insertions(+), 12 deletions(-) diff --git a/src/main/java/UI.java b/src/main/java/UI.java index dd0bc634..d20622dc 100644 --- a/src/main/java/UI.java +++ b/src/main/java/UI.java @@ -4,14 +4,14 @@ public class UI { public static void welcomeUser() { String welcomeText = "-------------------------------------\n" + "Hello! I am not Duke.\n" - + "Here are a list of commands: \n" + + "Here are a list of commands:\n" + "\ttodo : adds a Todo to the list\n" + "\tdeadline /by : adds a deadline and its deadline...\n" + "\tevent /at : adds an event and its time...\n" - + "\tmark : marks a task \n" - + "\tunmark : unmarks a task \n" - + "\tlist : lists out all your tasks \n" - + "\tbye : exits the program \n" + + "\tmark : marks a task\n" + + "\tunmark : unmarks a task\n" + + "\tlist : lists out all your tasks\n" + + "\tbye : exits the program\n" + "I WILL CRASH IF YOU DO NOT COMPLY!!!\n" + "-------------------------------------"; System.out.println(welcomeText); diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6..4ab1c857 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,131 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| - +------------------------------------- +Hello! I am not Duke. +Here are a list of commands: + todo : adds a Todo to the list + deadline /by : adds a deadline and its deadline... + event /at : adds an event and its time... + mark : marks a task + unmark : unmarks a task + list : lists out all your tasks + bye : exits the program +I WILL CRASH IF YOU DO NOT COMPLY!!! +------------------------------------- +Got it. I have added this task: +[T][ ] read book +You now have 1 tasks. +------------------------------------- +Marked this task: + [T][X] read book +------------------------------------- +Here are a list of your tasks: +1. [T][X] read book +------------------------------------- +Unmarked this task: +[T][ ] read book +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +------------------------------------- +Got it. I have added this task: +[D][ ] do dishes (by: midnight) +You now have 2 tasks. +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +2. [D][ ] do dishes (by: midnight) +------------------------------------- +Marked this task: + [D][X] do dishes (by: midnight) +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +2. [D][X] do dishes (by: midnight) +------------------------------------- +Unmarked this task: +[D][ ] do dishes (by: midnight) +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +2. [D][ ] do dishes (by: midnight) +------------------------------------- +Got it. I have added this task: +[E][ ] attend wedding (at: Sunday 1900) +You now have 3 tasks. +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +2. [D][ ] do dishes (by: midnight) +3. [E][ ] attend wedding (at: Sunday 1900) +------------------------------------- +Marked this task: + [E][X] attend wedding (at: Sunday 1900) +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +2. [D][ ] do dishes (by: midnight) +3. [E][X] attend wedding (at: Sunday 1900) +------------------------------------- +Unmarked this task: +[E][ ] attend wedding (at: Sunday 1900) +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +2. [D][ ] do dishes (by: midnight) +3. [E][ ] attend wedding (at: Sunday 1900) +------------------------------------- +"this is an invalid command" is not a valid command, please try again! +Got it. I have added this task: +[T][ ] read book again +You now have 4 tasks. +------------------------------------- +Got it. I have added this task: +[D][ ] do dishes again (by: morning) +You now have 5 tasks. +------------------------------------- +Got it. I have added this task: +[E][ ] attend funeral (at: 31st February) +You now have 6 tasks. +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +2. [D][ ] do dishes (by: midnight) +3. [E][ ] attend wedding (at: Sunday 1900) +4. [T][ ] read book again +5. [D][ ] do dishes again (by: morning) +6. [E][ ] attend funeral (at: 31st February) +------------------------------------- +Marked this task: + [T][X] read book again +------------------------------------- +Marked this task: + [D][X] do dishes again (by: morning) +------------------------------------- +Marked this task: + [E][X] attend funeral (at: 31st February) +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +2. [D][ ] do dishes (by: midnight) +3. [E][ ] attend wedding (at: Sunday 1900) +4. [T][X] read book again +5. [D][X] do dishes again (by: morning) +6. [E][X] attend funeral (at: 31st February) +------------------------------------- +Unmarked this task: +[T][ ] read book again +------------------------------------- +Unmarked this task: +[D][ ] do dishes again (by: morning) +------------------------------------- +Unmarked this task: +[E][ ] attend funeral (at: 31st February) +------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +2. [D][ ] do dishes (by: midnight) +3. [E][ ] attend wedding (at: Sunday 1900) +4. [T][ ] read book again +5. [D][ ] do dishes again (by: morning) +6. [E][ ] attend funeral (at: 31st February) +------------------------------------- +Bye! ;) \ No newline at end of file diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29b..894907db 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,32 @@ +todo read book +list +mark 1 +list +unmark 1 +list +deadline do dishes /by midnight +list +mark 2 +list +unmark 2 +list +event attend wedding /at Sunday 1900 +list +mark 3 +list +unmark 3 +list +this is an invalid command +todo read book again +deadline do dishes again /by morning +event attend funeral /at 31st February +list +mark 4 +mark 5 +mark 6 +list +unmark 4 +unmark 5 +unmark 6 +list +bye From efd57f2ee564d25204c85f5bec98f03452f84b06 Mon Sep 17 00:00:00 2001 From: jjtoh Date: Thu, 1 Sep 2022 14:57:45 +0800 Subject: [PATCH 10/26] Change expected output Testing still has issues --- text-ui-test/EXPECTED.TXT | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 4ab1c857..72eff8f5 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,19 +1,22 @@ ------------------------------------- Hello! I am not Duke. Here are a list of commands: - todo : adds a Todo to the list - deadline /by : adds a deadline and its deadline... - event /at : adds an event and its time... - mark : marks a task - unmark : unmarks a task - list : lists out all your tasks - bye : exits the program + todo : adds a Todo to the list + deadline /by : adds a deadline and its deadline... + event /at : adds an event and its time... + mark : marks a task + unmark : unmarks a task + list : lists out all your tasks + bye : exits the program I WILL CRASH IF YOU DO NOT COMPLY!!! ------------------------------------- Got it. I have added this task: [T][ ] read book You now have 1 tasks. ------------------------------------- +Here are a list of your tasks: +1. [T][ ] read book +------------------------------------- Marked this task: [T][X] read book ------------------------------------- From 7b0ca7f64b9b2e8ce7433d8f6edc69f27a11dd59 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 8 Sep 2022 03:54:49 +0800 Subject: [PATCH 11/26] Add Level-5 and A-Exception --- src/main/java/Duke.java | 2 - src/main/java/DukeException.java | 3 ++ src/main/java/Event.java | 2 +- src/main/java/InputHandler.java | 33 ++++++++++++--- src/main/java/TaskHandler.java | 69 +++++++++++++++++++++++--------- 5 files changed, 83 insertions(+), 26 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index dd119855..ecd8d61e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,5 +1,3 @@ -import java.util.Scanner; - public class Duke { public static void startProgram() { UI.welcomeUser(); diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 00000000..5e322ffd --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,3 @@ +public class DukeException extends Exception { + +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 74f053b6..cb6b346e 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -8,7 +8,7 @@ public Event(String description, String at) { @Override public String toString() { - return "[E]" + super.getStatusIcon()+ " " + super.getDescription() + " (at: " + at + ")"; + return "[E]" + super.getStatusIcon() + " " + super.getDescription() + " (at: " + at + ")"; } } diff --git a/src/main/java/InputHandler.java b/src/main/java/InputHandler.java index 6511b11c..27bda554 100644 --- a/src/main/java/InputHandler.java +++ b/src/main/java/InputHandler.java @@ -15,17 +15,40 @@ public static void handleInput() { TaskHandler.handleBye(); break; } else if (input.startsWith(TODO)) { - TaskHandler.handleTodo(input); + try { + TaskHandler.handleTodo(input); + } catch (DukeException e) { + System.out.println("todo what?"); + } } else if (input.startsWith(LIST)) { TaskHandler.handleList(); } else if (input.startsWith(DEADLINE)) { - TaskHandler.handleDeadline(input); + try { + TaskHandler.handleDeadline(input); + } catch (DukeException e) { + System.out.println("deadline missing some info..."); + ; + } } else if (input.startsWith(EVENT)) { - TaskHandler.handleEvent(input); + try { + TaskHandler.handleEvent(input); + } catch (DukeException e) { + System.out.println("event missing some info..."); + ; + } } else if (input.startsWith(MARK)) { - TaskHandler.handleMark(input); + try { + TaskHandler.handleMark(input); + } catch (DukeException e) { + System.out.println("mark what"); + ; + } } else if (input.startsWith(UNMARK)) { - TaskHandler.handleUnmark(input); + try { + TaskHandler.handleUnmark(input); + } catch (DukeException e) { + System.out.println("unmark what"); + } } else { TaskHandler.handleInvalid(input); } diff --git a/src/main/java/TaskHandler.java b/src/main/java/TaskHandler.java index 836bba32..735338ba 100644 --- a/src/main/java/TaskHandler.java +++ b/src/main/java/TaskHandler.java @@ -1,12 +1,16 @@ public class TaskHandler { private static final String BYE_MESSAGE = "Bye! ;)"; + public static final int SPLIT_AMOUNT = 2; public static void handleBye() { System.out.println(BYE_MESSAGE); } - public static void handleTodo(String input) { - String[] splitInput = input.split(" ", 2); + public static void handleTodo(String input) throws DukeException { + String[] splitInput = input.split(" ", SPLIT_AMOUNT); + if (splitInput.length != SPLIT_AMOUNT) { + throw new DukeException(); + } Todo todo = new Todo(splitInput[1]); TaskList.addTodo(todo); } @@ -15,30 +19,59 @@ public static void handleList() { TaskList.printList(); } - public static void handleMark(String input) { - String[] splitInput = input.split(" ", 2); - int target = Integer.parseInt(splitInput[1]); - TaskList.markTarget(target); + public static void handleMark(String input) throws DukeException { + String[] splitInput = input.split(" ", SPLIT_AMOUNT); + if (splitInput.length != SPLIT_AMOUNT) { + throw new DukeException(); + } + try { + int target = Integer.parseInt(splitInput[1]); + TaskList.markTarget(target); + } catch (NullPointerException e) { + System.out.println("There is no task with that index"); + } catch (NumberFormatException e) { + System.out.println("Index is a number..."); + } + } + public static void handleUnmark(String input) throws DukeException { + String[] splitInput = input.split(" ", SPLIT_AMOUNT); + if (splitInput.length != SPLIT_AMOUNT) { + throw new DukeException(); + } + try { + int target = Integer.parseInt(splitInput[1]); + TaskList.markTarget(target); + } catch (NullPointerException e) { + System.out.println("There is no task with that index"); + } catch (NumberFormatException e) { + System.out.println("Index is a number..."); + } } - public static void handleUnmark(String input) { - String[] splitInput = input.split(" ", 2); - int target = Integer.parseInt(splitInput[1]); - TaskList.unmarkTarget(target); + public static String[] splitTaskTime(String input) throws DukeException { + String[] splitInput = input.split(" ", SPLIT_AMOUNT); + if (splitInput.length != SPLIT_AMOUNT) { + throw new DukeException(); + } + return splitInput[1].split(" /by ", SPLIT_AMOUNT); } - public static void handleDeadline(String input) { - String[] splitInput = input.split(" ", 2); - String[] splitCommand = splitInput[1].split(" /by ", 2); // 0 is description, 1 is by - Deadline deadline = new Deadline(splitCommand[0], splitCommand[1]); + public static void handleDeadline(String input) throws DukeException { + String[] splitCommand = splitTaskTime(input); + if (splitCommand.length != SPLIT_AMOUNT) { + throw new DukeException(); + } + Deadline deadline = new Deadline(splitCommand[0], splitCommand[1]); //index 0 is description, index 1 is time TaskList.addDeadLine(deadline); } - public static void handleEvent(String input) { - String[] splitInput = input.split(" ", 2); - String[] splitCommand = splitInput[1].split(" /at ", 2); // 0 is description, 1 is by - Event event = new Event(splitCommand[0], splitCommand[1]); + public static void handleEvent(String input) throws DukeException { + String[] splitCommand = splitTaskTime(input); + if (splitCommand.length != SPLIT_AMOUNT) { + throw new DukeException(); + } + Event event = new Event(splitCommand[0], splitCommand[1]); //index 0 is description, index 1 is time TaskList.addEvent(event); } From ae8e7eb3a92115fdced1ba0cac7fcf32eda9e098 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 8 Sep 2022 04:04:28 +0800 Subject: [PATCH 12/26] Add A-Packages --- src/main/java/Duke.java | 3 +++ src/main/java/{ => exception}/DukeException.java | 2 ++ src/main/java/{ => handler}/InputHandler.java | 5 +++++ src/main/java/{ => handler}/TaskHandler.java | 8 ++++++++ src/main/java/{ => task}/Deadline.java | 2 ++ src/main/java/{ => task}/Event.java | 2 ++ src/main/java/{ => task}/Task.java | 2 ++ src/main/java/{ => task}/TaskList.java | 2 ++ src/main/java/{ => task}/Todo.java | 2 ++ src/main/java/{ => ui}/UI.java | 2 ++ 10 files changed, 30 insertions(+) rename src/main/java/{ => exception}/DukeException.java (71%) rename src/main/java/{ => handler}/InputHandler.java (96%) rename src/main/java/{ => handler}/TaskHandler.java (95%) rename src/main/java/{ => task}/Deadline.java (95%) rename src/main/java/{ => task}/Event.java (95%) rename src/main/java/{ => task}/Task.java (96%) rename src/main/java/{ => task}/TaskList.java (99%) rename src/main/java/{ => task}/Todo.java (94%) rename src/main/java/{ => ui}/UI.java (98%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ecd8d61e..f9dec8e2 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,6 @@ +import handler.InputHandler; +import ui.UI; + public class Duke { public static void startProgram() { UI.welcomeUser(); diff --git a/src/main/java/DukeException.java b/src/main/java/exception/DukeException.java similarity index 71% rename from src/main/java/DukeException.java rename to src/main/java/exception/DukeException.java index 5e322ffd..c449019f 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/exception/DukeException.java @@ -1,3 +1,5 @@ +package exception; + public class DukeException extends Exception { } diff --git a/src/main/java/InputHandler.java b/src/main/java/handler/InputHandler.java similarity index 96% rename from src/main/java/InputHandler.java rename to src/main/java/handler/InputHandler.java index 27bda554..d7dc919f 100644 --- a/src/main/java/InputHandler.java +++ b/src/main/java/handler/InputHandler.java @@ -1,3 +1,8 @@ +package handler; + +import ui.UI; +import exception.DukeException; + public class InputHandler { private static final String TODO = "todo"; diff --git a/src/main/java/TaskHandler.java b/src/main/java/handler/TaskHandler.java similarity index 95% rename from src/main/java/TaskHandler.java rename to src/main/java/handler/TaskHandler.java index 735338ba..df6b8d1b 100644 --- a/src/main/java/TaskHandler.java +++ b/src/main/java/handler/TaskHandler.java @@ -1,3 +1,11 @@ +package handler; + +import task.Deadline; +import task.Event; +import task.TaskList; +import task.Todo; +import exception.DukeException; + public class TaskHandler { private static final String BYE_MESSAGE = "Bye! ;)"; public static final int SPLIT_AMOUNT = 2; diff --git a/src/main/java/Deadline.java b/src/main/java/task/Deadline.java similarity index 95% rename from src/main/java/Deadline.java rename to src/main/java/task/Deadline.java index bca8eece..9f1fc775 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -1,3 +1,5 @@ +package task; + public class Deadline extends Task { protected String by; diff --git a/src/main/java/Event.java b/src/main/java/task/Event.java similarity index 95% rename from src/main/java/Event.java rename to src/main/java/task/Event.java index cb6b346e..6d00cc0d 100644 --- a/src/main/java/Event.java +++ b/src/main/java/task/Event.java @@ -1,3 +1,5 @@ +package task; + public class Event extends Task { protected String at; diff --git a/src/main/java/Task.java b/src/main/java/task/Task.java similarity index 96% rename from src/main/java/Task.java rename to src/main/java/task/Task.java index 4010f3ad..a768cbcf 100644 --- a/src/main/java/Task.java +++ b/src/main/java/task/Task.java @@ -1,3 +1,5 @@ +package task; + public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/TaskList.java b/src/main/java/task/TaskList.java similarity index 99% rename from src/main/java/TaskList.java rename to src/main/java/task/TaskList.java index 7d63ef04..7f43bdf8 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -1,3 +1,5 @@ +package task; + public class TaskList { private static Task[] tasks = new Task[100]; diff --git a/src/main/java/Todo.java b/src/main/java/task/Todo.java similarity index 94% rename from src/main/java/Todo.java rename to src/main/java/task/Todo.java index 14671a55..710e5f95 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/task/Todo.java @@ -1,3 +1,5 @@ +package task; + public class Todo extends Task { public Todo(String description) { super(description); diff --git a/src/main/java/UI.java b/src/main/java/ui/UI.java similarity index 98% rename from src/main/java/UI.java rename to src/main/java/ui/UI.java index d20622dc..40b94133 100644 --- a/src/main/java/UI.java +++ b/src/main/java/ui/UI.java @@ -1,3 +1,5 @@ +package ui; + import java.util.Scanner; public class UI { From 3ff482fb97a35061a04e932c312b47f66fb72584 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 14 Sep 2022 01:48:54 +0800 Subject: [PATCH 13/26] Add some temporary code Nothing of value is added --- src/main/java/handler/InputHandler.java | 9 +++++---- src/main/java/handler/TaskHandler.java | 2 +- src/main/java/task/TaskList.java | 23 +++++++++++++---------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/handler/InputHandler.java b/src/main/java/handler/InputHandler.java index d7dc919f..3fec967e 100644 --- a/src/main/java/handler/InputHandler.java +++ b/src/main/java/handler/InputHandler.java @@ -12,6 +12,7 @@ public class InputHandler { private static final String BYE = "bye"; private static final String MARK = "mark"; private static final String UNMARK = "unmark"; + private static final String DELETE = "DELETE"; public static void handleInput() { while (true) { @@ -32,21 +33,18 @@ public static void handleInput() { TaskHandler.handleDeadline(input); } catch (DukeException e) { System.out.println("deadline missing some info..."); - ; } } else if (input.startsWith(EVENT)) { try { TaskHandler.handleEvent(input); } catch (DukeException e) { System.out.println("event missing some info..."); - ; } } else if (input.startsWith(MARK)) { try { TaskHandler.handleMark(input); } catch (DukeException e) { System.out.println("mark what"); - ; } } else if (input.startsWith(UNMARK)) { try { @@ -54,7 +52,10 @@ public static void handleInput() { } catch (DukeException e) { System.out.println("unmark what"); } - } else { + } else if (input.startsWith(DELETE)) { + + } + else { TaskHandler.handleInvalid(input); } } diff --git a/src/main/java/handler/TaskHandler.java b/src/main/java/handler/TaskHandler.java index df6b8d1b..f2e29fb8 100644 --- a/src/main/java/handler/TaskHandler.java +++ b/src/main/java/handler/TaskHandler.java @@ -49,7 +49,7 @@ public static void handleUnmark(String input) throws DukeException { } try { int target = Integer.parseInt(splitInput[1]); - TaskList.markTarget(target); + TaskList.unmarkTarget(target); } catch (NullPointerException e) { System.out.println("There is no task with that index"); } catch (NumberFormatException e) { diff --git a/src/main/java/task/TaskList.java b/src/main/java/task/TaskList.java index 7f43bdf8..9f195dae 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -1,8 +1,10 @@ package task; +import java.util.ArrayList; + public class TaskList { - private static Task[] tasks = new Task[100]; + private static final ArrayList tasks = new ArrayList<>(); private static int taskCount = 0; private static final String LINE_DIVIDER = "-------------------------------------"; private static final String ROGER_MESSAGE = "Got it. I have added this task:"; @@ -16,45 +18,46 @@ private static void printRoger(Task task) { private static void printMark(int target, boolean mark) { if (mark) { - System.out.println("Marked this task: \n " + tasks[target - 1]); + System.out.println("Marked this task: \n " + tasks.get(target - 1)); } else { - System.out.println("Unmarked this task: \n" + tasks[target - 1]); + System.out.println("Unmarked this task: \n" + tasks.get(target - 1)); } System.out.println(LINE_DIVIDER); } public static void addDeadLine(Deadline deadline) { - tasks[taskCount] = deadline; + tasks.add(deadline); taskCount++; printRoger(deadline); } public static void addTodo(Todo todo) { - tasks[taskCount] = todo; + tasks.add(todo); taskCount++; printRoger(todo); } public static void addEvent(Event event) { - tasks[taskCount] = event; + tasks.add(event); taskCount++; printRoger(event); } public static void markTarget(int target) { - tasks[target - 1].setDone(true); + tasks.get(target - 1).setDone(true); printMark(target, true); } public static void unmarkTarget(int target) { - tasks[target - 1].setDone(false); + tasks.get(target - 1).setDone(false); printMark(target, false); } public static void printList() { System.out.println("Here are a list of your tasks: "); - for (int i = 0; i < taskCount; i++) { - System.out.println(i + 1 + ". " + tasks[i]); + int index = 1; + for (Task task : tasks) { + System.out.println(index + ". " + task); } System.out.println(LINE_DIVIDER); } From eaa8d407bdf639b0dba91d6e5e275767a0539b12 Mon Sep 17 00:00:00 2001 From: jjtoh Date: Wed, 14 Sep 2022 03:42:48 +0800 Subject: [PATCH 14/26] Add ability to delete Can merge as level 6 --- src/main/java/handler/InputHandler.java | 110 +++++++++++++++++++----- src/main/java/handler/TaskHandler.java | 77 +++++++++-------- src/main/java/task/TaskList.java | 9 ++ 3 files changed, 140 insertions(+), 56 deletions(-) diff --git a/src/main/java/handler/InputHandler.java b/src/main/java/handler/InputHandler.java index 3fec967e..937823d2 100644 --- a/src/main/java/handler/InputHandler.java +++ b/src/main/java/handler/InputHandler.java @@ -12,52 +12,120 @@ public class InputHandler { private static final String BYE = "bye"; private static final String MARK = "mark"; private static final String UNMARK = "unmark"; - private static final String DELETE = "DELETE"; + private static final String DELETE = "delete"; public static void handleInput() { - while (true) { + boolean exit = false; + while (!exit) { String input = UI.getInput(); - if (input.startsWith(BYE)) { + String command = getCommand(input); + switch (command) { + case (BYE): TaskHandler.handleBye(); + exit = true; break; - } else if (input.startsWith(TODO)) { + case (TODO): try { - TaskHandler.handleTodo(input); - } catch (DukeException e) { + TaskHandler.handleTodo(getAction(input)); + } catch (ArrayIndexOutOfBoundsException e) { System.out.println("todo what?"); } - } else if (input.startsWith(LIST)) { + break; + case (LIST): TaskHandler.handleList(); - } else if (input.startsWith(DEADLINE)) { + break; + case (DEADLINE): try { - TaskHandler.handleDeadline(input); + TaskHandler.handleDeadline(getAction(input)); } catch (DukeException e) { System.out.println("deadline missing some info..."); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("deadline what?"); } - } else if (input.startsWith(EVENT)) { + break; + case (EVENT): try { - TaskHandler.handleEvent(input); + TaskHandler.handleEvent(getAction(input)); } catch (DukeException e) { System.out.println("event missing some info..."); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("event what?"); } - } else if (input.startsWith(MARK)) { + break; + case (MARK): try { - TaskHandler.handleMark(input); - } catch (DukeException e) { + TaskHandler.handleMark(getAction(input)); + } catch (ArrayIndexOutOfBoundsException e) { System.out.println("mark what"); } - } else if (input.startsWith(UNMARK)) { + break; + case (UNMARK): try { - TaskHandler.handleUnmark(input); - } catch (DukeException e) { + TaskHandler.handleUnmark(getAction(input)); + } catch (ArrayIndexOutOfBoundsException e) { System.out.println("unmark what"); } - } else if (input.startsWith(DELETE)) { - - } - else { + break; + case (DELETE): + try { + TaskHandler.handleDelete(getAction(input)); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("delete what?"); + } + break; + default: TaskHandler.handleInvalid(input); } +// if (input.startsWith(BYE)) { +// TaskHandler.handleBye(); +// break; +// } else if (input.startsWith(TODO)) { +// try { +// TaskHandler.handleTodo(input); +// } catch (DukeException e) { +// System.out.println("todo what?"); +// } +// } else if (input.startsWith(LIST)) { +// TaskHandler.handleList(); +// } else if (input.startsWith(DEADLINE)) { +// try { +// TaskHandler.handleDeadline(input); +// } catch (DukeException e) { +// System.out.println("deadline missing some info..."); +// } +// } else if (input.startsWith(EVENT)) { +// try { +// TaskHandler.handleEvent(input); +// } catch (DukeException e) { +// System.out.println("event missing some info..."); +// } +// } else if (input.startsWith(MARK)) { +// try { +// TaskHandler.handleMark(input); +// } catch (DukeException e) { +// System.out.println("mark what"); +// } +// } else if (input.startsWith(UNMARK)) { +// try { +// TaskHandler.handleUnmark(input); +// } catch (DukeException e) { +// System.out.println("unmark what"); +// } +// } else if (input.startsWith(DELETE)) { +// +// } +// else { +// TaskHandler.handleInvalid(input); +// } } } + + private static String getCommand(String input) { + return input.split(" ", 2)[0].trim(); + } + + private static String getAction(String input) throws ArrayIndexOutOfBoundsException { + return input.split(" ", 2)[1].trim(); + } + } diff --git a/src/main/java/handler/TaskHandler.java b/src/main/java/handler/TaskHandler.java index f2e29fb8..9f002556 100644 --- a/src/main/java/handler/TaskHandler.java +++ b/src/main/java/handler/TaskHandler.java @@ -8,18 +8,14 @@ public class TaskHandler { private static final String BYE_MESSAGE = "Bye! ;)"; - public static final int SPLIT_AMOUNT = 2; + private static final int SPLIT_AMOUNT = 2; public static void handleBye() { System.out.println(BYE_MESSAGE); } - public static void handleTodo(String input) throws DukeException { - String[] splitInput = input.split(" ", SPLIT_AMOUNT); - if (splitInput.length != SPLIT_AMOUNT) { - throw new DukeException(); - } - Todo todo = new Todo(splitInput[1]); + public static void handleTodo(String input) { + Todo todo = new Todo(input); TaskList.addTodo(todo); } @@ -27,64 +23,75 @@ public static void handleList() { TaskList.printList(); } - public static void handleMark(String input) throws DukeException { - String[] splitInput = input.split(" ", SPLIT_AMOUNT); - if (splitInput.length != SPLIT_AMOUNT) { - throw new DukeException(); - } + public static void handleMark(String input) { try { - int target = Integer.parseInt(splitInput[1]); + int target = Integer.parseInt(input); TaskList.markTarget(target); - } catch (NullPointerException e) { + } catch (IndexOutOfBoundsException e) { System.out.println("There is no task with that index"); } catch (NumberFormatException e) { System.out.println("Index is a number..."); } } - public static void handleUnmark(String input) throws DukeException { - String[] splitInput = input.split(" ", SPLIT_AMOUNT); - if (splitInput.length != SPLIT_AMOUNT) { - throw new DukeException(); - } + public static void handleUnmark(String input) { + int index = readIndex(input); try { - int target = Integer.parseInt(splitInput[1]); - TaskList.unmarkTarget(target); - } catch (NullPointerException e) { - System.out.println("There is no task with that index"); - } catch (NumberFormatException e) { - System.out.println("Index is a number..."); + TaskList.unmarkTarget(index); + } catch (IndexOutOfBoundsException e) { + System.out.println("Index does not exist"); } } - public static String[] splitTaskTime(String input) throws DukeException { - String[] splitInput = input.split(" ", SPLIT_AMOUNT); - if (splitInput.length != SPLIT_AMOUNT) { - throw new DukeException(); - } - return splitInput[1].split(" /by ", SPLIT_AMOUNT); + private static String[] splitDeadlineTime(String input) throws DukeException { + return input.split(" /by ", SPLIT_AMOUNT); + } + private static String[] splitEventTime(String input) throws DukeException { + return input.split(" /at ", SPLIT_AMOUNT); } public static void handleDeadline(String input) throws DukeException { - String[] splitCommand = splitTaskTime(input); + String[] splitCommand = splitDeadlineTime(input); if (splitCommand.length != SPLIT_AMOUNT) { throw new DukeException(); } - Deadline deadline = new Deadline(splitCommand[0], splitCommand[1]); //index 0 is description, index 1 is time + String description = splitCommand[0]; + String by = splitCommand[1]; + Deadline deadline = new Deadline(description, by); TaskList.addDeadLine(deadline); } public static void handleEvent(String input) throws DukeException { - String[] splitCommand = splitTaskTime(input); + String[] splitCommand = splitEventTime(input); if (splitCommand.length != SPLIT_AMOUNT) { throw new DukeException(); } - Event event = new Event(splitCommand[0], splitCommand[1]); //index 0 is description, index 1 is time + String description = splitCommand[0]; + String at = splitCommand[1]; + Event event = new Event(description, at); TaskList.addEvent(event); } + public static void handleDelete(String input) { + int index = readIndex(input); + try { + TaskList.deleteTask(index); + } catch (IndexOutOfBoundsException e) { + System.out.println("Index does not exist."); + } + } + + private static int readIndex(String input) { + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + System.out.println("Index is a number..."); + } + return -1; + } public static void handleInvalid(String input) { System.out.println("\"" + input + "\"" + " is not a valid command, please try again!"); } + } diff --git a/src/main/java/task/TaskList.java b/src/main/java/task/TaskList.java index 9f195dae..868cb105 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -58,7 +58,16 @@ public static void printList() { int index = 1; for (Task task : tasks) { System.out.println(index + ". " + task); + index++; } System.out.println(LINE_DIVIDER); } + + public static void deleteTask(int index) { + System.out.println(tasks.get(index - 1) + " has been removed"); + tasks.remove(index - 1); + taskCount--; + System.out.println("There are " + taskCount + "tasks left"); + System.out.println(LINE_DIVIDER); + } } From 3e345a1996e0879fdfad259a6b21e338d62ba04e Mon Sep 17 00:00:00 2001 From: jjtoh Date: Wed, 14 Sep 2022 21:26:19 +0800 Subject: [PATCH 15/26] Shift some code around --- src/main/java/handler/InputHandler.java | 41 ------------------------- src/main/java/handler/TaskHandler.java | 18 +++++------ 2 files changed, 9 insertions(+), 50 deletions(-) diff --git a/src/main/java/handler/InputHandler.java b/src/main/java/handler/InputHandler.java index 937823d2..ac4155c7 100644 --- a/src/main/java/handler/InputHandler.java +++ b/src/main/java/handler/InputHandler.java @@ -76,47 +76,6 @@ public static void handleInput() { default: TaskHandler.handleInvalid(input); } -// if (input.startsWith(BYE)) { -// TaskHandler.handleBye(); -// break; -// } else if (input.startsWith(TODO)) { -// try { -// TaskHandler.handleTodo(input); -// } catch (DukeException e) { -// System.out.println("todo what?"); -// } -// } else if (input.startsWith(LIST)) { -// TaskHandler.handleList(); -// } else if (input.startsWith(DEADLINE)) { -// try { -// TaskHandler.handleDeadline(input); -// } catch (DukeException e) { -// System.out.println("deadline missing some info..."); -// } -// } else if (input.startsWith(EVENT)) { -// try { -// TaskHandler.handleEvent(input); -// } catch (DukeException e) { -// System.out.println("event missing some info..."); -// } -// } else if (input.startsWith(MARK)) { -// try { -// TaskHandler.handleMark(input); -// } catch (DukeException e) { -// System.out.println("mark what"); -// } -// } else if (input.startsWith(UNMARK)) { -// try { -// TaskHandler.handleUnmark(input); -// } catch (DukeException e) { -// System.out.println("unmark what"); -// } -// } else if (input.startsWith(DELETE)) { -// -// } -// else { -// TaskHandler.handleInvalid(input); -// } } } diff --git a/src/main/java/handler/TaskHandler.java b/src/main/java/handler/TaskHandler.java index 9f002556..5a6bed6e 100644 --- a/src/main/java/handler/TaskHandler.java +++ b/src/main/java/handler/TaskHandler.java @@ -8,6 +8,7 @@ public class TaskHandler { private static final String BYE_MESSAGE = "Bye! ;)"; + private static final String INDEX_NOT_FOUND = "There are no tasks with that index"; private static final int SPLIT_AMOUNT = 2; public static void handleBye() { @@ -25,8 +26,8 @@ public static void handleList() { public static void handleMark(String input) { try { - int target = Integer.parseInt(input); - TaskList.markTarget(target); + int index = readIndex(input); + TaskList.markTarget(index); } catch (IndexOutOfBoundsException e) { System.out.println("There is no task with that index"); } catch (NumberFormatException e) { @@ -35,9 +36,11 @@ public static void handleMark(String input) { } public static void handleUnmark(String input) { - int index = readIndex(input); try { + int index = readIndex(input); TaskList.unmarkTarget(index); + } catch (NumberFormatException e) { + System.out.println("Index is an integer"); } catch (IndexOutOfBoundsException e) { System.out.println("Index does not exist"); } @@ -73,25 +76,22 @@ public static void handleEvent(String input) throws DukeException { } public static void handleDelete(String input) { - int index = readIndex(input); try { + int index = readIndex(input); TaskList.deleteTask(index); } catch (IndexOutOfBoundsException e) { System.out.println("Index does not exist."); } } - private static int readIndex(String input) { + private static int readIndex(String input) throws NumberFormatException { try { return Integer.parseInt(input); } catch (NumberFormatException e) { - System.out.println("Index is a number..."); + throw e; } - return -1; } public static void handleInvalid(String input) { System.out.println("\"" + input + "\"" + " is not a valid command, please try again!"); } - - } From 86eff83149fd518d06bd9b1d51d320ecd7c0814f Mon Sep 17 00:00:00 2001 From: John Date: Thu, 15 Sep 2022 03:36:22 +0800 Subject: [PATCH 16/26] Shift some code around --- src/main/java/handler/TaskHandler.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/handler/TaskHandler.java b/src/main/java/handler/TaskHandler.java index 5a6bed6e..3ff9b87e 100644 --- a/src/main/java/handler/TaskHandler.java +++ b/src/main/java/handler/TaskHandler.java @@ -46,13 +46,6 @@ public static void handleUnmark(String input) { } } - private static String[] splitDeadlineTime(String input) throws DukeException { - return input.split(" /by ", SPLIT_AMOUNT); - } - private static String[] splitEventTime(String input) throws DukeException { - return input.split(" /at ", SPLIT_AMOUNT); - } - public static void handleDeadline(String input) throws DukeException { String[] splitCommand = splitDeadlineTime(input); if (splitCommand.length != SPLIT_AMOUNT) { @@ -91,6 +84,15 @@ private static int readIndex(String input) throws NumberFormatException { throw e; } } + + private static String[] splitDeadlineTime(String input) throws DukeException { + return input.split(" /by ", SPLIT_AMOUNT); + } + + private static String[] splitEventTime(String input) throws DukeException { + return input.split(" /at ", SPLIT_AMOUNT); + } + public static void handleInvalid(String input) { System.out.println("\"" + input + "\"" + " is not a valid command, please try again!"); } From 3ea366921c50a4a47deaaee44479cd9c75383914 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 16 Sep 2022 03:35:52 +0800 Subject: [PATCH 17/26] Add Level-7 Merge with level 7 branch in future --- data/tasks.txt | 0 src/main/java/Duke.java | 10 ++- src/main/java/file/FileManager.java | 110 ++++++++++++++++++++++++++++ src/main/java/task/Deadline.java | 4 + src/main/java/task/Event.java | 3 + src/main/java/task/Task.java | 3 + src/main/java/task/TaskList.java | 13 ++++ 7 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 data/tasks.txt create mode 100644 src/main/java/file/FileManager.java diff --git a/data/tasks.txt b/data/tasks.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f9dec8e2..0af67e4c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,13 +1,19 @@ +import file.FileManager; import handler.InputHandler; +import task.TaskList; import ui.UI; +import java.io.FileNotFoundException; + public class Duke { public static void startProgram() { - UI.welcomeUser(); - InputHandler.handleInput(); + UI.welcomeUser(); + FileManager.startReading(); + InputHandler.handleInput(); } public static void main(String[] args) { startProgram(); + TaskList.storeTasks(); } } diff --git a/src/main/java/file/FileManager.java b/src/main/java/file/FileManager.java new file mode 100644 index 00000000..a145f128 --- /dev/null +++ b/src/main/java/file/FileManager.java @@ -0,0 +1,110 @@ +package file; + +import task.*; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class FileManager { + + private static Path taskDataPath = Paths.get(System.getProperty("user.dir"), "data", "tasks.txt"); + + private static Path dataDirPath = Paths.get(System.getProperty("user.dir"), "data"); + + public static void writeToFile(Task task) throws IOException { + FileWriter fw = new FileWriter(taskDataPath.toString(), true); + String textToAdd = taskToText(task); + fw.write(textToAdd); + fw.close(); + } + + + public static void startReading() { + try { + makeMissingDirectory(); + readFile(); + } catch (FileNotFoundException e) { + System.out.println("File cannot be found"); + } catch (IOException e) { + System.out.println("Some IO error has occurred"); + } + } + + private static void readFile() throws FileNotFoundException { + String filePath = taskDataPath.toString(); + File f = new File(filePath); + Scanner s = new Scanner(f); + while (s.hasNext()) { + String[] input = s.nextLine().split(" | "); + switch (input[0]) { + case ("T"): + insertTodo(input); + break; + case ("D"): + insertDeadline(input); + break; + case ("E"): + insertEvent(input); + break; + } + } + } + + private static void makeMissingDirectory() throws IOException { + File dir = new File(dataDirPath.toString()); + if (!dir.exists()) { + dir.mkdir(); + } else { + return; + } + FileWriter fw = new FileWriter(taskDataPath.toString()); + } + + private static void insertTodo(String[] input) { + Todo todo = new Todo(input[2]); + if (input[1].equals("1")) { + todo.setDone(true); + } else { + todo.setDone(false); + } + TaskList.addTodo(todo); + } + + private static void insertDeadline(String[] input) { + Deadline deadline = new Deadline(input[2], input[3]); + if (input[1].equals("1")) { + deadline.setDone(true); + } else { + deadline.setDone(false); + } + TaskList.addDeadLine(deadline); + } + + private static void insertEvent(String[] input) { + Event event = new Event(input[2], input[3]); + if (input[1].equals("1")) { + event.setDone(true); + } else { + event.setDone(false); + } + TaskList.addEvent(event); + } + + private static String taskToText(Task task) { + if (task.getClass() == Todo.class) { + return "T | " + task.getStatusInNumber() + " | " + task.getDescription() + "\n"; + } else if (task.getClass() == Deadline.class) { + return "D | " + task.getStatusInNumber() + " | " + + task.getDescription() + " | " + ((Deadline) task).getBy() + "\n"; + } else { + return "E | " + task.getStatusInNumber() + " | " + + task.getDescription() + " | " + ((Event) task).getAt() + "\n"; + } + } +} diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index 9f1fc775..f4e34971 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -8,6 +8,10 @@ public Deadline(String description, String by) { this.by = by; } + public String getBy() { + return by; + } + @Override public String toString() { return "[D]" + super.getStatusIcon() + " " + super.getDescription() + " (by: " + by + ")"; diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index 6d00cc0d..4d331e79 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -8,6 +8,9 @@ public Event(String description, String at) { this.at = at; } + public String getAt() { + return at; + } @Override public String toString() { return "[E]" + super.getStatusIcon() + " " + super.getDescription() + " (at: " + at + ")"; diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index a768cbcf..e7ccc456 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -17,6 +17,9 @@ public String getStatusIcon() { return isDone ? "[X]" : "[ ]"; } + public int getStatusInNumber() { + return isDone ? 1 : 0; + } public String getDescription() { return description; diff --git a/src/main/java/task/TaskList.java b/src/main/java/task/TaskList.java index 868cb105..8b8aa770 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -1,5 +1,8 @@ package task; +import file.FileManager; + +import java.io.IOException; import java.util.ArrayList; public class TaskList { @@ -70,4 +73,14 @@ public static void deleteTask(int index) { System.out.println("There are " + taskCount + "tasks left"); System.out.println(LINE_DIVIDER); } + + public static void storeTasks() { + try { + for (Task task : tasks) { + FileManager.writeToFile(task); + } + } catch (IOException e) { + System.out.println("An IO error has occurred"); + } + } } From 94554eeda815c2da603e1e0fbba5846a9b718994 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 16 Sep 2022 03:43:37 +0800 Subject: [PATCH 18/26] Add jar file --- src/main/java/META-INF/MANIFEST.MF | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 00000000..9f37e4e0 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + From f143ba184d4c5ad20ea2e4ef367790799fae5c5c Mon Sep 17 00:00:00 2001 From: John Date: Fri, 16 Sep 2022 04:16:37 +0800 Subject: [PATCH 19/26] Revert "Add Level-7" This reverts commit 3ea366921c50a4a47deaaee44479cd9c75383914. --- data/tasks.txt | 0 src/main/java/Duke.java | 10 +-- src/main/java/file/FileManager.java | 110 ---------------------------- src/main/java/task/Deadline.java | 4 - src/main/java/task/Event.java | 3 - src/main/java/task/Task.java | 3 - src/main/java/task/TaskList.java | 13 ---- 7 files changed, 2 insertions(+), 141 deletions(-) delete mode 100644 data/tasks.txt delete mode 100644 src/main/java/file/FileManager.java diff --git a/data/tasks.txt b/data/tasks.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 0af67e4c..f9dec8e2 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,19 +1,13 @@ -import file.FileManager; import handler.InputHandler; -import task.TaskList; import ui.UI; -import java.io.FileNotFoundException; - public class Duke { public static void startProgram() { - UI.welcomeUser(); - FileManager.startReading(); - InputHandler.handleInput(); + UI.welcomeUser(); + InputHandler.handleInput(); } public static void main(String[] args) { startProgram(); - TaskList.storeTasks(); } } diff --git a/src/main/java/file/FileManager.java b/src/main/java/file/FileManager.java deleted file mode 100644 index a145f128..00000000 --- a/src/main/java/file/FileManager.java +++ /dev/null @@ -1,110 +0,0 @@ -package file; - -import task.*; - -import java.io.FileWriter; -import java.io.IOException; -import java.io.File; -import java.io.FileNotFoundException; -import java.util.Scanner; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -public class FileManager { - - private static Path taskDataPath = Paths.get(System.getProperty("user.dir"), "data", "tasks.txt"); - - private static Path dataDirPath = Paths.get(System.getProperty("user.dir"), "data"); - - public static void writeToFile(Task task) throws IOException { - FileWriter fw = new FileWriter(taskDataPath.toString(), true); - String textToAdd = taskToText(task); - fw.write(textToAdd); - fw.close(); - } - - - public static void startReading() { - try { - makeMissingDirectory(); - readFile(); - } catch (FileNotFoundException e) { - System.out.println("File cannot be found"); - } catch (IOException e) { - System.out.println("Some IO error has occurred"); - } - } - - private static void readFile() throws FileNotFoundException { - String filePath = taskDataPath.toString(); - File f = new File(filePath); - Scanner s = new Scanner(f); - while (s.hasNext()) { - String[] input = s.nextLine().split(" | "); - switch (input[0]) { - case ("T"): - insertTodo(input); - break; - case ("D"): - insertDeadline(input); - break; - case ("E"): - insertEvent(input); - break; - } - } - } - - private static void makeMissingDirectory() throws IOException { - File dir = new File(dataDirPath.toString()); - if (!dir.exists()) { - dir.mkdir(); - } else { - return; - } - FileWriter fw = new FileWriter(taskDataPath.toString()); - } - - private static void insertTodo(String[] input) { - Todo todo = new Todo(input[2]); - if (input[1].equals("1")) { - todo.setDone(true); - } else { - todo.setDone(false); - } - TaskList.addTodo(todo); - } - - private static void insertDeadline(String[] input) { - Deadline deadline = new Deadline(input[2], input[3]); - if (input[1].equals("1")) { - deadline.setDone(true); - } else { - deadline.setDone(false); - } - TaskList.addDeadLine(deadline); - } - - private static void insertEvent(String[] input) { - Event event = new Event(input[2], input[3]); - if (input[1].equals("1")) { - event.setDone(true); - } else { - event.setDone(false); - } - TaskList.addEvent(event); - } - - private static String taskToText(Task task) { - if (task.getClass() == Todo.class) { - return "T | " + task.getStatusInNumber() + " | " + task.getDescription() + "\n"; - } else if (task.getClass() == Deadline.class) { - return "D | " + task.getStatusInNumber() + " | " - + task.getDescription() + " | " + ((Deadline) task).getBy() + "\n"; - } else { - return "E | " + task.getStatusInNumber() + " | " - + task.getDescription() + " | " + ((Event) task).getAt() + "\n"; - } - } -} diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index f4e34971..9f1fc775 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -8,10 +8,6 @@ public Deadline(String description, String by) { this.by = by; } - public String getBy() { - return by; - } - @Override public String toString() { return "[D]" + super.getStatusIcon() + " " + super.getDescription() + " (by: " + by + ")"; diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index 4d331e79..6d00cc0d 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -8,9 +8,6 @@ public Event(String description, String at) { this.at = at; } - public String getAt() { - return at; - } @Override public String toString() { return "[E]" + super.getStatusIcon() + " " + super.getDescription() + " (at: " + at + ")"; diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index e7ccc456..a768cbcf 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -17,9 +17,6 @@ public String getStatusIcon() { return isDone ? "[X]" : "[ ]"; } - public int getStatusInNumber() { - return isDone ? 1 : 0; - } public String getDescription() { return description; diff --git a/src/main/java/task/TaskList.java b/src/main/java/task/TaskList.java index 8b8aa770..868cb105 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -1,8 +1,5 @@ package task; -import file.FileManager; - -import java.io.IOException; import java.util.ArrayList; public class TaskList { @@ -73,14 +70,4 @@ public static void deleteTask(int index) { System.out.println("There are " + taskCount + "tasks left"); System.out.println(LINE_DIVIDER); } - - public static void storeTasks() { - try { - for (Task task : tasks) { - FileManager.writeToFile(task); - } - } catch (IOException e) { - System.out.println("An IO error has occurred"); - } - } } From e06c4e320a02953d4c43354cee43c3aec7967ac1 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 16 Sep 2022 04:36:23 +0800 Subject: [PATCH 20/26] Add level 7 --- data/tasks.txt | 0 src/main/java/Duke.java | 10 ++- src/main/java/META-INF/MANIFEST.MF | 3 - src/main/java/file/FileManager.java | 110 ++++++++++++++++++++++++++++ src/main/java/task/Deadline.java | 4 + src/main/java/task/Event.java | 3 + src/main/java/task/Task.java | 3 + src/main/java/task/TaskList.java | 13 ++++ 8 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 data/tasks.txt delete mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/file/FileManager.java diff --git a/data/tasks.txt b/data/tasks.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f9dec8e2..0af67e4c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,13 +1,19 @@ +import file.FileManager; import handler.InputHandler; +import task.TaskList; import ui.UI; +import java.io.FileNotFoundException; + public class Duke { public static void startProgram() { - UI.welcomeUser(); - InputHandler.handleInput(); + UI.welcomeUser(); + FileManager.startReading(); + InputHandler.handleInput(); } public static void main(String[] args) { startProgram(); + TaskList.storeTasks(); } } diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF deleted file mode 100644 index 9f37e4e0..00000000 --- a/src/main/java/META-INF/MANIFEST.MF +++ /dev/null @@ -1,3 +0,0 @@ -Manifest-Version: 1.0 -Main-Class: Duke - diff --git a/src/main/java/file/FileManager.java b/src/main/java/file/FileManager.java new file mode 100644 index 00000000..a145f128 --- /dev/null +++ b/src/main/java/file/FileManager.java @@ -0,0 +1,110 @@ +package file; + +import task.*; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class FileManager { + + private static Path taskDataPath = Paths.get(System.getProperty("user.dir"), "data", "tasks.txt"); + + private static Path dataDirPath = Paths.get(System.getProperty("user.dir"), "data"); + + public static void writeToFile(Task task) throws IOException { + FileWriter fw = new FileWriter(taskDataPath.toString(), true); + String textToAdd = taskToText(task); + fw.write(textToAdd); + fw.close(); + } + + + public static void startReading() { + try { + makeMissingDirectory(); + readFile(); + } catch (FileNotFoundException e) { + System.out.println("File cannot be found"); + } catch (IOException e) { + System.out.println("Some IO error has occurred"); + } + } + + private static void readFile() throws FileNotFoundException { + String filePath = taskDataPath.toString(); + File f = new File(filePath); + Scanner s = new Scanner(f); + while (s.hasNext()) { + String[] input = s.nextLine().split(" | "); + switch (input[0]) { + case ("T"): + insertTodo(input); + break; + case ("D"): + insertDeadline(input); + break; + case ("E"): + insertEvent(input); + break; + } + } + } + + private static void makeMissingDirectory() throws IOException { + File dir = new File(dataDirPath.toString()); + if (!dir.exists()) { + dir.mkdir(); + } else { + return; + } + FileWriter fw = new FileWriter(taskDataPath.toString()); + } + + private static void insertTodo(String[] input) { + Todo todo = new Todo(input[2]); + if (input[1].equals("1")) { + todo.setDone(true); + } else { + todo.setDone(false); + } + TaskList.addTodo(todo); + } + + private static void insertDeadline(String[] input) { + Deadline deadline = new Deadline(input[2], input[3]); + if (input[1].equals("1")) { + deadline.setDone(true); + } else { + deadline.setDone(false); + } + TaskList.addDeadLine(deadline); + } + + private static void insertEvent(String[] input) { + Event event = new Event(input[2], input[3]); + if (input[1].equals("1")) { + event.setDone(true); + } else { + event.setDone(false); + } + TaskList.addEvent(event); + } + + private static String taskToText(Task task) { + if (task.getClass() == Todo.class) { + return "T | " + task.getStatusInNumber() + " | " + task.getDescription() + "\n"; + } else if (task.getClass() == Deadline.class) { + return "D | " + task.getStatusInNumber() + " | " + + task.getDescription() + " | " + ((Deadline) task).getBy() + "\n"; + } else { + return "E | " + task.getStatusInNumber() + " | " + + task.getDescription() + " | " + ((Event) task).getAt() + "\n"; + } + } +} diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index 9f1fc775..f4e34971 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -8,6 +8,10 @@ public Deadline(String description, String by) { this.by = by; } + public String getBy() { + return by; + } + @Override public String toString() { return "[D]" + super.getStatusIcon() + " " + super.getDescription() + " (by: " + by + ")"; diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index 6d00cc0d..4d331e79 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -8,6 +8,9 @@ public Event(String description, String at) { this.at = at; } + public String getAt() { + return at; + } @Override public String toString() { return "[E]" + super.getStatusIcon() + " " + super.getDescription() + " (at: " + at + ")"; diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index a768cbcf..e7ccc456 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -17,6 +17,9 @@ public String getStatusIcon() { return isDone ? "[X]" : "[ ]"; } + public int getStatusInNumber() { + return isDone ? 1 : 0; + } public String getDescription() { return description; diff --git a/src/main/java/task/TaskList.java b/src/main/java/task/TaskList.java index 868cb105..8b8aa770 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -1,5 +1,8 @@ package task; +import file.FileManager; + +import java.io.IOException; import java.util.ArrayList; public class TaskList { @@ -70,4 +73,14 @@ public static void deleteTask(int index) { System.out.println("There are " + taskCount + "tasks left"); System.out.println(LINE_DIVIDER); } + + public static void storeTasks() { + try { + for (Task task : tasks) { + FileManager.writeToFile(task); + } + } catch (IOException e) { + System.out.println("An IO error has occurred"); + } + } } From f56c21082d6faaf2ed8fbbd65f90360bdfbebbb0 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 16 Sep 2022 04:48:01 +0800 Subject: [PATCH 21/26] Add jar file --- src/main/java/META-INF/MANIFEST.MF | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 00000000..9f37e4e0 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + From 8e3a781f61283bdaaac11298e941984641f64fb0 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 28 Sep 2022 22:01:42 +0800 Subject: [PATCH 22/26] Add more OOP --- data/tasks.txt | 2 + src/main/java/Duke.java | 28 ++-- src/main/java/exception/DukeException.java | 3 + src/main/java/handler/InputHandler.java | 90 ------------ src/main/java/handler/TaskHandler.java | 99 ------------- src/main/java/parser/Parser.java | 134 ++++++++++++++++++ .../FileManager.java => storage/Storage.java} | 41 +++--- src/main/java/task/Task.java | 4 + src/main/java/task/TaskList.java | 71 +++++----- src/main/java/task/command/ByeCommand.java | 13 ++ src/main/java/task/command/Command.java | 10 ++ .../java/task/command/DeadlineCommand.java | 22 +++ src/main/java/task/command/DeleteCommand.java | 20 +++ src/main/java/task/command/EventCommand.java | 21 +++ .../java/task/command/InvalidCommand.java | 12 ++ src/main/java/task/command/ListCommand.java | 17 +++ src/main/java/task/command/MarkCommand.java | 19 +++ src/main/java/task/command/TodoCommand.java | 22 +++ src/main/java/task/command/UnmarkCommand.java | 19 +++ src/main/java/ui/UI.java | 90 +++++++++--- 20 files changed, 464 insertions(+), 273 deletions(-) delete mode 100644 src/main/java/handler/InputHandler.java delete mode 100644 src/main/java/handler/TaskHandler.java create mode 100644 src/main/java/parser/Parser.java rename src/main/java/{file/FileManager.java => storage/Storage.java} (73%) create mode 100644 src/main/java/task/command/ByeCommand.java create mode 100644 src/main/java/task/command/Command.java create mode 100644 src/main/java/task/command/DeadlineCommand.java create mode 100644 src/main/java/task/command/DeleteCommand.java create mode 100644 src/main/java/task/command/EventCommand.java create mode 100644 src/main/java/task/command/InvalidCommand.java create mode 100644 src/main/java/task/command/ListCommand.java create mode 100644 src/main/java/task/command/MarkCommand.java create mode 100644 src/main/java/task/command/TodoCommand.java create mode 100644 src/main/java/task/command/UnmarkCommand.java diff --git a/data/tasks.txt b/data/tasks.txt index e69de29b..95e08e18 100644 --- a/data/tasks.txt +++ b/data/tasks.txt @@ -0,0 +1,2 @@ +T | 0 | hello +T | 0 | hello diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 0af67e4c..6d4cf8a0 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,19 +1,27 @@ -import file.FileManager; -import handler.InputHandler; +import storage.Storage; +import parser.Parser; import task.TaskList; import ui.UI; -import java.io.FileNotFoundException; - public class Duke { - public static void startProgram() { - UI.welcomeUser(); - FileManager.startReading(); - InputHandler.handleInput(); + + private static Storage storage; + public static UI ui; + private static TaskList tasks; + + public Duke () { + Duke.storage = new Storage(); + Duke.ui = new UI(); + Duke.tasks = new TaskList(storage.startReading()); + } + + public void start() { + ui.welcomeUser(); + Parser parser = new Parser(); + parser.executeCommands(ui, tasks, storage); } public static void main(String[] args) { - startProgram(); - TaskList.storeTasks(); + new Duke().start(); } } diff --git a/src/main/java/exception/DukeException.java b/src/main/java/exception/DukeException.java index c449019f..5500ba15 100644 --- a/src/main/java/exception/DukeException.java +++ b/src/main/java/exception/DukeException.java @@ -2,4 +2,7 @@ public class DukeException extends Exception { + public DukeException(String message) { + super(message); + } } diff --git a/src/main/java/handler/InputHandler.java b/src/main/java/handler/InputHandler.java deleted file mode 100644 index ac4155c7..00000000 --- a/src/main/java/handler/InputHandler.java +++ /dev/null @@ -1,90 +0,0 @@ -package handler; - -import ui.UI; -import exception.DukeException; - -public class InputHandler { - - private static final String TODO = "todo"; - private static final String LIST = "list"; - private static final String DEADLINE = "deadline"; - private static final String EVENT = "event"; - private static final String BYE = "bye"; - private static final String MARK = "mark"; - private static final String UNMARK = "unmark"; - private static final String DELETE = "delete"; - - public static void handleInput() { - boolean exit = false; - while (!exit) { - String input = UI.getInput(); - String command = getCommand(input); - switch (command) { - case (BYE): - TaskHandler.handleBye(); - exit = true; - break; - case (TODO): - try { - TaskHandler.handleTodo(getAction(input)); - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("todo what?"); - } - break; - case (LIST): - TaskHandler.handleList(); - break; - case (DEADLINE): - try { - TaskHandler.handleDeadline(getAction(input)); - } catch (DukeException e) { - System.out.println("deadline missing some info..."); - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("deadline what?"); - } - break; - case (EVENT): - try { - TaskHandler.handleEvent(getAction(input)); - } catch (DukeException e) { - System.out.println("event missing some info..."); - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("event what?"); - } - break; - case (MARK): - try { - TaskHandler.handleMark(getAction(input)); - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("mark what"); - } - break; - case (UNMARK): - try { - TaskHandler.handleUnmark(getAction(input)); - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("unmark what"); - } - break; - case (DELETE): - try { - TaskHandler.handleDelete(getAction(input)); - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("delete what?"); - } - break; - default: - TaskHandler.handleInvalid(input); - } - } - } - - private static String getCommand(String input) { - return input.split(" ", 2)[0].trim(); - } - - private static String getAction(String input) throws ArrayIndexOutOfBoundsException { - return input.split(" ", 2)[1].trim(); - } - -} diff --git a/src/main/java/handler/TaskHandler.java b/src/main/java/handler/TaskHandler.java deleted file mode 100644 index 3ff9b87e..00000000 --- a/src/main/java/handler/TaskHandler.java +++ /dev/null @@ -1,99 +0,0 @@ -package handler; - -import task.Deadline; -import task.Event; -import task.TaskList; -import task.Todo; -import exception.DukeException; - -public class TaskHandler { - private static final String BYE_MESSAGE = "Bye! ;)"; - private static final String INDEX_NOT_FOUND = "There are no tasks with that index"; - private static final int SPLIT_AMOUNT = 2; - - public static void handleBye() { - System.out.println(BYE_MESSAGE); - } - - public static void handleTodo(String input) { - Todo todo = new Todo(input); - TaskList.addTodo(todo); - } - - public static void handleList() { - TaskList.printList(); - } - - public static void handleMark(String input) { - try { - int index = readIndex(input); - TaskList.markTarget(index); - } catch (IndexOutOfBoundsException e) { - System.out.println("There is no task with that index"); - } catch (NumberFormatException e) { - System.out.println("Index is a number..."); - } - } - - public static void handleUnmark(String input) { - try { - int index = readIndex(input); - TaskList.unmarkTarget(index); - } catch (NumberFormatException e) { - System.out.println("Index is an integer"); - } catch (IndexOutOfBoundsException e) { - System.out.println("Index does not exist"); - } - } - - public static void handleDeadline(String input) throws DukeException { - String[] splitCommand = splitDeadlineTime(input); - if (splitCommand.length != SPLIT_AMOUNT) { - throw new DukeException(); - } - String description = splitCommand[0]; - String by = splitCommand[1]; - Deadline deadline = new Deadline(description, by); - TaskList.addDeadLine(deadline); - } - - public static void handleEvent(String input) throws DukeException { - String[] splitCommand = splitEventTime(input); - if (splitCommand.length != SPLIT_AMOUNT) { - throw new DukeException(); - } - String description = splitCommand[0]; - String at = splitCommand[1]; - Event event = new Event(description, at); - TaskList.addEvent(event); - } - - public static void handleDelete(String input) { - try { - int index = readIndex(input); - TaskList.deleteTask(index); - } catch (IndexOutOfBoundsException e) { - System.out.println("Index does not exist."); - } - } - - private static int readIndex(String input) throws NumberFormatException { - try { - return Integer.parseInt(input); - } catch (NumberFormatException e) { - throw e; - } - } - - private static String[] splitDeadlineTime(String input) throws DukeException { - return input.split(" /by ", SPLIT_AMOUNT); - } - - private static String[] splitEventTime(String input) throws DukeException { - return input.split(" /at ", SPLIT_AMOUNT); - } - - public static void handleInvalid(String input) { - System.out.println("\"" + input + "\"" + " is not a valid command, please try again!"); - } -} diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java new file mode 100644 index 00000000..8c7a39bd --- /dev/null +++ b/src/main/java/parser/Parser.java @@ -0,0 +1,134 @@ +package parser; + +import storage.Storage; +import task.*; +import task.command.*; +import ui.UI; +import exception.DukeException; + +public class Parser { + + private final String TODO = "todo"; + private final String LIST = "list"; + private final String DEADLINE = "deadline"; + private final String EVENT = "event"; + private final String BYE = "bye"; + private final String MARK = "mark"; + private final String UNMARK = "unmark"; + private final String DELETE = "delete"; + private final String FIND = "FIND"; + private final int SPLIT_AMOUNT = 2; + private boolean exit = false; + + public void executeCommands(UI ui, TaskList tasks, Storage storage) { + while (!exit) { + try { + Command command = handleCommand(ui); + command.execute(tasks, ui, storage); + } + catch (DukeException e) { + System.out.println(e.getMessage()); + } + } + } + + public Command handleCommand(UI ui) throws DukeException { + String input = ui.getInput(); + String command = getCommand(input); + switch (command) { + case (BYE): + exit = true; + return new ByeCommand(); + case (LIST): + return new ListCommand(); + case (TODO): + Todo todo = handleTodo(getAction(input)); + return new TodoCommand(todo); + case (DEADLINE): + Deadline deadline = handleDeadline(getAction(input)); + return new DeadlineCommand(deadline); + case (EVENT): + Event event = handleEvent(getAction(input)); + return new EventCommand(event); + case (MARK): + int markIndex = readIndex(getAction(input)); + return new MarkCommand(markIndex); + case (UNMARK): + int unmarkIndex = readIndex(getAction(input)); + return new UnmarkCommand(unmarkIndex); + case (DELETE): + int deleteIndex = readIndex(getAction(input)); + return new DeleteCommand(deleteIndex); + default: + return new InvalidCommand(); + } + } + + + private String getCommand(String input) { + return input.split(" ", 2)[0].trim(); + } + + private String getAction(String input) throws DukeException { + String[] splitInput = input.split(" ", 2); + if (splitInput.length != 2) { + if (splitInput[1].equals(TODO) || splitInput[1].equals(DEADLINE) || splitInput[1].equals(EVENT) + ||splitInput[1].equals(FIND)) { + throw new DukeException("Missing input for command, input the command in this format: " + + splitInput[0] + "{TASK}"); + } else { + throw new DukeException("Missing input for command, input the command in this format: " + + splitInput[0] + "{ID}"); + } + + } + return splitInput[1]; + } + + public Todo handleTodo(String input) { + return new Todo(input); + } + + + public Deadline handleDeadline(String input) throws DukeException { + String[] splitCommand = splitDeadlineTime(input); + if (splitCommand.length != SPLIT_AMOUNT) { + throw new DukeException("Deadline is missing a timing."); + } + String description = splitCommand[0]; + String by = splitCommand[1]; + return new Deadline(description, by); + } + + public Event handleEvent(String input) throws DukeException { + String[] splitCommand = splitEventTime(input); + if (splitCommand.length != SPLIT_AMOUNT) { + throw new DukeException("Event is missing a timing."); + } + String description = splitCommand[0]; + String at = splitCommand[1]; + return new Event(description, at); + } + + + public int readIndex(String input) throws DukeException { + try { + int index = Integer.parseInt(input); + if (index < 1 || index > TaskList.getTaskCount()) { + throw new DukeException("Index out of bound."); + } + return index; + } catch (NumberFormatException e) { + throw new DukeException("A valid numeric number must be entered."); + } + } + + private String[] splitDeadlineTime(String input) throws DukeException { + return input.split(" /by ", SPLIT_AMOUNT); + } + + private String[] splitEventTime(String input) throws DukeException { + return input.split(" /at ", SPLIT_AMOUNT); + } + +} diff --git a/src/main/java/file/FileManager.java b/src/main/java/storage/Storage.java similarity index 73% rename from src/main/java/file/FileManager.java rename to src/main/java/storage/Storage.java index a145f128..d305852f 100644 --- a/src/main/java/file/FileManager.java +++ b/src/main/java/storage/Storage.java @@ -1,4 +1,4 @@ -package file; +package storage; import task.*; @@ -6,18 +6,18 @@ import java.io.IOException; import java.io.File; import java.io.FileNotFoundException; +import java.util.ArrayList; import java.util.Scanner; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -public class FileManager { +public class Storage { private static Path taskDataPath = Paths.get(System.getProperty("user.dir"), "data", "tasks.txt"); - private static Path dataDirPath = Paths.get(System.getProperty("user.dir"), "data"); + private ArrayList tasks = new ArrayList<>(); - public static void writeToFile(Task task) throws IOException { + public void writeToFile(Task task) throws IOException { FileWriter fw = new FileWriter(taskDataPath.toString(), true); String textToAdd = taskToText(task); fw.write(textToAdd); @@ -25,7 +25,7 @@ public static void writeToFile(Task task) throws IOException { } - public static void startReading() { + public ArrayList startReading() { try { makeMissingDirectory(); readFile(); @@ -34,23 +34,24 @@ public static void startReading() { } catch (IOException e) { System.out.println("Some IO error has occurred"); } + return tasks; } - private static void readFile() throws FileNotFoundException { + private void readFile() throws FileNotFoundException { String filePath = taskDataPath.toString(); File f = new File(filePath); Scanner s = new Scanner(f); while (s.hasNext()) { - String[] input = s.nextLine().split(" | "); + String[] input = s.nextLine().split(" \\| "); switch (input[0]) { case ("T"): - insertTodo(input); + insertTodo(input, tasks); break; case ("D"): - insertDeadline(input); + insertDeadline(input, tasks); break; case ("E"): - insertEvent(input); + insertEvent(input, tasks); break; } } @@ -66,37 +67,37 @@ private static void makeMissingDirectory() throws IOException { FileWriter fw = new FileWriter(taskDataPath.toString()); } - private static void insertTodo(String[] input) { + private void insertTodo(String[] input, ArrayList tasks) { Todo todo = new Todo(input[2]); if (input[1].equals("1")) { todo.setDone(true); } else { todo.setDone(false); } - TaskList.addTodo(todo); + tasks.add(todo); } - private static void insertDeadline(String[] input) { + private void insertDeadline(String[] input, ArrayList tasks) { Deadline deadline = new Deadline(input[2], input[3]); if (input[1].equals("1")) { deadline.setDone(true); } else { deadline.setDone(false); } - TaskList.addDeadLine(deadline); + tasks.add(deadline); } - private static void insertEvent(String[] input) { + private void insertEvent(String[] input, ArrayList tasks) { Event event = new Event(input[2], input[3]); if (input[1].equals("1")) { event.setDone(true); } else { event.setDone(false); } - TaskList.addEvent(event); + tasks.add(event); } - private static String taskToText(Task task) { + private String taskToText(Task task) { if (task.getClass() == Todo.class) { return "T | " + task.getStatusInNumber() + " | " + task.getDescription() + "\n"; } else if (task.getClass() == Deadline.class) { @@ -107,4 +108,8 @@ private static String taskToText(Task task) { + task.getDescription() + " | " + ((Event) task).getAt() + "\n"; } } + + public void deleteContent() throws IOException { + new FileWriter(taskDataPath.toString()).close(); + } } diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index e7ccc456..69a5c81b 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -13,6 +13,10 @@ public void setDone(boolean isDone) { this.isDone = isDone; } + public boolean getDone() { + return isDone; + } + public String getStatusIcon() { return isDone ? "[X]" : "[ ]"; } diff --git a/src/main/java/task/TaskList.java b/src/main/java/task/TaskList.java index 8b8aa770..86f43de6 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -1,59 +1,38 @@ package task; -import file.FileManager; +import storage.Storage; import java.io.IOException; import java.util.ArrayList; public class TaskList { - private static final ArrayList tasks = new ArrayList<>(); + private static ArrayList tasks; private static int taskCount = 0; private static final String LINE_DIVIDER = "-------------------------------------"; private static final String ROGER_MESSAGE = "Got it. I have added this task:"; - private static void printRoger(Task task) { - System.out.println(ROGER_MESSAGE); - System.out.println(task); - System.out.println("You now have " + taskCount + " tasks."); - System.out.println(LINE_DIVIDER); + public TaskList(ArrayList tasks) { + TaskList.tasks = tasks; + TaskList.taskCount = tasks.size(); } - private static void printMark(int target, boolean mark) { - if (mark) { - System.out.println("Marked this task: \n " + tasks.get(target - 1)); - } else { - System.out.println("Unmarked this task: \n" + tasks.get(target - 1)); - } - System.out.println(LINE_DIVIDER); + public static int getTaskCount() { + return taskCount; } - public static void addDeadLine(Deadline deadline) { - tasks.add(deadline); + public void addTask(Task task) { + tasks.add(task); taskCount++; - printRoger(deadline); } - public static void addTodo(Todo todo) { - tasks.add(todo); - taskCount++; - printRoger(todo); - } - public static void addEvent(Event event) { - tasks.add(event); - taskCount++; - printRoger(event); - } - - public static void markTarget(int target) { + public void markTarget(int target) { tasks.get(target - 1).setDone(true); - printMark(target, true); } - public static void unmarkTarget(int target) { + public void unmarkTarget(int target) { tasks.get(target - 1).setDone(false); - printMark(target, false); } public static void printList() { @@ -66,21 +45,35 @@ public static void printList() { System.out.println(LINE_DIVIDER); } - public static void deleteTask(int index) { - System.out.println(tasks.get(index - 1) + " has been removed"); + public void deleteTask(int index) { tasks.remove(index - 1); taskCount--; - System.out.println("There are " + taskCount + "tasks left"); - System.out.println(LINE_DIVIDER); } - public static void storeTasks() { + public ArrayList getTasks() { + return tasks; + } + + public Task getTask(int index) { + return tasks.get(index); + } + + public void storeTask(Task task, Storage storage) { + try { + storage.writeToFile(task); + } catch (IOException e) { + System.out.println("File not found/missing directory."); + } + } + + public void rewriteFile(Storage storage) { try { + storage.deleteContent(); for (Task task : tasks) { - FileManager.writeToFile(task); + storage.writeToFile(task); } } catch (IOException e) { - System.out.println("An IO error has occurred"); + System.out.println("File is not found/missing directory."); } } } diff --git a/src/main/java/task/command/ByeCommand.java b/src/main/java/task/command/ByeCommand.java new file mode 100644 index 00000000..d16d175c --- /dev/null +++ b/src/main/java/task/command/ByeCommand.java @@ -0,0 +1,13 @@ +package task.command; + +import storage.Storage; +import task.TaskList; +import ui.UI; + +public class ByeCommand extends Command { + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + ui.byeMessage(); + } +} diff --git a/src/main/java/task/command/Command.java b/src/main/java/task/command/Command.java new file mode 100644 index 00000000..60f80051 --- /dev/null +++ b/src/main/java/task/command/Command.java @@ -0,0 +1,10 @@ +package task.command; + +import storage.Storage; +import task.TaskList; +import ui.UI; + +public abstract class Command { + + public abstract void execute(TaskList tasks, UI ui, Storage storage); +} diff --git a/src/main/java/task/command/DeadlineCommand.java b/src/main/java/task/command/DeadlineCommand.java new file mode 100644 index 00000000..61eaa64d --- /dev/null +++ b/src/main/java/task/command/DeadlineCommand.java @@ -0,0 +1,22 @@ +package task.command; + +import storage.Storage; +import task.Deadline; +import task.TaskList; +import ui.UI; + +public class DeadlineCommand extends Command { + + private Deadline deadline; + + public DeadlineCommand(Deadline deadline) { + this.deadline = deadline; + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + tasks.addTask(deadline); + ui.addMessage(deadline, tasks); + tasks.storeTask(deadline, storage); + } +} diff --git a/src/main/java/task/command/DeleteCommand.java b/src/main/java/task/command/DeleteCommand.java new file mode 100644 index 00000000..e9252854 --- /dev/null +++ b/src/main/java/task/command/DeleteCommand.java @@ -0,0 +1,20 @@ +package task.command; + +import storage.Storage; +import task.TaskList; +import ui.UI; + +public class DeleteCommand extends Command { + + private int index; + + public DeleteCommand(int index) { + this.index = index; + } + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + ui.deleteMessage(tasks, index); + tasks.deleteTask(index); + tasks.rewriteFile(storage); + } +} diff --git a/src/main/java/task/command/EventCommand.java b/src/main/java/task/command/EventCommand.java new file mode 100644 index 00000000..be9fc6f4 --- /dev/null +++ b/src/main/java/task/command/EventCommand.java @@ -0,0 +1,21 @@ +package task.command; + +import storage.Storage; +import task.Event; +import task.TaskList; +import ui.UI; + +public class EventCommand extends Command { + private Event event; + + public EventCommand(Event event) { + this.event = event; + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + tasks.addTask(event); + ui.addMessage(event, tasks); + tasks.storeTask(event, storage); + } +} diff --git a/src/main/java/task/command/InvalidCommand.java b/src/main/java/task/command/InvalidCommand.java new file mode 100644 index 00000000..bdaed012 --- /dev/null +++ b/src/main/java/task/command/InvalidCommand.java @@ -0,0 +1,12 @@ +package task.command; + +import storage.Storage; +import task.TaskList; +import ui.UI; + +public class InvalidCommand extends Command{ + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + ui.invalidCommand(); + } +} diff --git a/src/main/java/task/command/ListCommand.java b/src/main/java/task/command/ListCommand.java new file mode 100644 index 00000000..f242f87c --- /dev/null +++ b/src/main/java/task/command/ListCommand.java @@ -0,0 +1,17 @@ +package task.command; + +import storage.Storage; +import task.Task; +import task.TaskList; +import ui.UI; + +import java.util.ArrayList; + +public class ListCommand extends Command { + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + ArrayList list = tasks.getTasks(); + ui.printList(list); + } +} diff --git a/src/main/java/task/command/MarkCommand.java b/src/main/java/task/command/MarkCommand.java new file mode 100644 index 00000000..d88e898d --- /dev/null +++ b/src/main/java/task/command/MarkCommand.java @@ -0,0 +1,19 @@ +package task.command; + +import storage.Storage; +import task.TaskList; +import ui.UI; + +public class MarkCommand extends Command { + private int index; + public MarkCommand(int index) { + this.index = index; + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + tasks.markTarget(index); + ui.markMessage(tasks, index); + tasks.rewriteFile(storage); + } +} diff --git a/src/main/java/task/command/TodoCommand.java b/src/main/java/task/command/TodoCommand.java new file mode 100644 index 00000000..1a1a0811 --- /dev/null +++ b/src/main/java/task/command/TodoCommand.java @@ -0,0 +1,22 @@ +package task.command; + +import storage.Storage; +import task.Task; +import task.TaskList; +import task.Todo; +import ui.UI; + +public class TodoCommand extends Command { + private Todo todo; + + public TodoCommand(Todo todo) { + this.todo = todo; + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + tasks.addTask(todo); + ui.addMessage(todo, tasks); + tasks.storeTask(todo, storage); + } +} diff --git a/src/main/java/task/command/UnmarkCommand.java b/src/main/java/task/command/UnmarkCommand.java new file mode 100644 index 00000000..cd69c127 --- /dev/null +++ b/src/main/java/task/command/UnmarkCommand.java @@ -0,0 +1,19 @@ +package task.command; + +import storage.Storage; +import task.TaskList; +import ui.UI; + +public class UnmarkCommand extends Command { + private int index; + public UnmarkCommand(int index) { + this.index = index; + } + + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + tasks.unmarkTarget(index); + ui.markMessage(tasks, index); + tasks.rewriteFile(storage); + } +} diff --git a/src/main/java/ui/UI.java b/src/main/java/ui/UI.java index 40b94133..450882a6 100644 --- a/src/main/java/ui/UI.java +++ b/src/main/java/ui/UI.java @@ -1,25 +1,81 @@ package ui; +import task.Task; +import task.TaskList; + +import java.util.ArrayList; import java.util.Scanner; public class UI { - public static void welcomeUser() { - String welcomeText = "-------------------------------------\n" - + "Hello! I am not Duke.\n" - + "Here are a list of commands:\n" - + "\ttodo : adds a Todo to the list\n" - + "\tdeadline /by : adds a deadline and its deadline...\n" - + "\tevent /at : adds an event and its time...\n" - + "\tmark : marks a task\n" - + "\tunmark : unmarks a task\n" - + "\tlist : lists out all your tasks\n" - + "\tbye : exits the program\n" - + "I WILL CRASH IF YOU DO NOT COMPLY!!!\n" - + "-------------------------------------"; - System.out.println(welcomeText); - } - - public static String getInput() { + + public final String LINE_DIVIDER = "-------------------------------------\n"; + public final String WELCOME_TEXT = "Hello I am Duke. Here are a list of command:\n" + + "todo {task} : Creates a todo\n" + + "deadline {deadline /by time} : Creates a deadline by time\n" + + "event {event /at time} : Creates an event at time\n" + + "mark/unmark {index} : Marks a task\n" + + "delete {index} : Deletes a task\n" + + "list : List all current tasks\n" + + "bye : Exits the program"; + public final String BYE_MESSAGE = "Goodbye, hope to see you again."; + + public void welcomeUser() { + formatMessage(WELCOME_TEXT); + } + + private void formatMessage(String message) { + System.out.print(LINE_DIVIDER + message + "\n" + LINE_DIVIDER); + } + + public void byeMessage() { + formatMessage(BYE_MESSAGE); + } + + public void addMessage(Task task, TaskList tasks) { + formatMessage("Got it. I have added this task:\n" + task + + "\nYou now have " + tasks.getTaskCount() + " tasks."); + } + + public void printList(ArrayList tasks) { + if (tasks.isEmpty()) { + formatMessage("You have no tasks currently."); + } else { + int size = tasks.size(); + String message = "Here is the list of your tasks: \n"; + for (int i = 0; i < size; i++) { + if (i == size - 1) { + message += (i + 1) + ". " + tasks.get(i); + } else { + message += (i + 1) + ". " + tasks.get(i) + "\n"; + } + } + formatMessage(message); + } + } + + public void markMessage(TaskList tasks, int index) { + Task task = tasks.getTask(index - 1); + if (!task.getDone()) { + formatMessage("Unmarked the following task:\n " + + task); + } else { + formatMessage("Marked the following task:\n" + + task); + } + } + + public void invalidCommand() { + formatMessage("You have entered an invalid command."); + } + + public void deleteMessage(TaskList tasks, int index) { + Task deletedTask = tasks.getTask(index - 1); + formatMessage("The following task has been deleted: \n" + + deletedTask + "\nYou now have " + (tasks.getTaskCount() - 1) + + " tasks."); + } + + public String getInput() { Scanner sc = new Scanner(System.in); String input = sc.nextLine(); return input.trim(); From 9413857f0763eb685290cc8f25890efcea6a7ff7 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 29 Sep 2022 00:44:16 +0800 Subject: [PATCH 23/26] Add Level 9 --- data/tasks.txt | 2 - src/main/java/exception/DukeException.java | 8 ++ src/main/java/parser/Parser.java | 112 ++++++++++++++---- src/main/java/storage/Storage.java | 87 +++++++++++++- src/main/java/task/Deadline.java | 20 ++++ src/main/java/task/Event.java | 20 ++++ src/main/java/task/Task.java | 33 ++++++ src/main/java/task/TaskList.java | 85 ++++++++----- src/main/java/task/Todo.java | 14 +++ src/main/java/task/command/ByeCommand.java | 11 ++ src/main/java/task/command/Command.java | 10 ++ .../java/task/command/DeadlineCommand.java | 19 ++- src/main/java/task/command/DeleteCommand.java | 20 +++- src/main/java/task/command/EventCommand.java | 18 ++- src/main/java/task/command/FindCommand.java | 41 +++++++ .../java/task/command/InvalidCommand.java | 13 +- src/main/java/task/command/ListCommand.java | 11 ++ src/main/java/task/command/MarkCommand.java | 19 ++- src/main/java/task/command/TodoCommand.java | 19 ++- src/main/java/task/command/UnmarkCommand.java | 19 ++- src/main/java/ui/UI.java | 18 +++ 21 files changed, 537 insertions(+), 62 deletions(-) create mode 100644 src/main/java/task/command/FindCommand.java diff --git a/data/tasks.txt b/data/tasks.txt index 95e08e18..e69de29b 100644 --- a/data/tasks.txt +++ b/data/tasks.txt @@ -1,2 +0,0 @@ -T | 0 | hello -T | 0 | hello diff --git a/src/main/java/exception/DukeException.java b/src/main/java/exception/DukeException.java index 5500ba15..878184fd 100644 --- a/src/main/java/exception/DukeException.java +++ b/src/main/java/exception/DukeException.java @@ -2,7 +2,15 @@ public class DukeException extends Exception { + private String message; + + /** + * Constructor for DukeException + * + * @param message The error message to be displayed + */ public DukeException(String message) { super(message); + this.message = message; } } diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index 8c7a39bd..7c15f9c7 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -6,6 +6,10 @@ import ui.UI; import exception.DukeException; +/** + * An object that deals with anything that the user inputs so that logic + * can be performed on them. + */ public class Parser { private final String TODO = "todo"; @@ -16,22 +20,35 @@ public class Parser { private final String MARK = "mark"; private final String UNMARK = "unmark"; private final String DELETE = "delete"; - private final String FIND = "FIND"; + private final String FIND = "find"; private final int SPLIT_AMOUNT = 2; private boolean exit = false; + /** + * This method calls the execute method of the commands that the user inputs. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ public void executeCommands(UI ui, TaskList tasks, Storage storage) { while (!exit) { try { Command command = handleCommand(ui); command.execute(tasks, ui, storage); - } - catch (DukeException e) { + } catch (DukeException e) { System.out.println(e.getMessage()); } } } + /** + * The method that identifies the command that the user is inputting. + * + * @param ui Object that handles all user interaction. + * @return Command object based on what the user inputs. + * @throws DukeException If user enters an invalid input. + */ public Command handleCommand(UI ui) throws DukeException { String input = ui.getInput(); String command = getCommand(input); @@ -42,54 +59,79 @@ public Command handleCommand(UI ui) throws DukeException { case (LIST): return new ListCommand(); case (TODO): - Todo todo = handleTodo(getAction(input)); + Todo todo = handleTodo(getArgument(input)); return new TodoCommand(todo); case (DEADLINE): - Deadline deadline = handleDeadline(getAction(input)); + Deadline deadline = handleDeadline(getArgument(input)); return new DeadlineCommand(deadline); case (EVENT): - Event event = handleEvent(getAction(input)); + Event event = handleEvent(getArgument(input)); return new EventCommand(event); case (MARK): - int markIndex = readIndex(getAction(input)); + int markIndex = readIndex(getArgument(input)); return new MarkCommand(markIndex); case (UNMARK): - int unmarkIndex = readIndex(getAction(input)); + int unmarkIndex = readIndex(getArgument(input)); return new UnmarkCommand(unmarkIndex); case (DELETE): - int deleteIndex = readIndex(getAction(input)); + int deleteIndex = readIndex(getArgument(input)); return new DeleteCommand(deleteIndex); + case (FIND): + String target = getArgument(input); + return new FindCommand(target); default: return new InvalidCommand(); } } - + /** + * Gets the specific command the user inputs. + * + * @param input What the user inputs in the CLI. + * @return The first word, which is the command that the user inputs. + */ private String getCommand(String input) { - return input.split(" ", 2)[0].trim(); + return input.split(" ", 2)[0].trim().toLowerCase(); } - private String getAction(String input) throws DukeException { - String[] splitInput = input.split(" ", 2); - if (splitInput.length != 2) { - if (splitInput[1].equals(TODO) || splitInput[1].equals(DEADLINE) || splitInput[1].equals(EVENT) - ||splitInput[1].equals(FIND)) { + /** + * Gets the argument that the user inputs. + * + * @param input What the user inputs in the CLI. + * @return The string after the first word, which is the argument. + * @throws DukeException If the user did not input the command. + */ + private String getArgument(String input) throws DukeException { + String[] splitInput = input.split(" ", SPLIT_AMOUNT); + if (splitInput.length != SPLIT_AMOUNT) { + if (splitInput[0].equals(TODO) || splitInput[0].equals(DEADLINE) || splitInput[0].equals(EVENT) + || splitInput[0].equals(FIND)) { throw new DukeException("Missing input for command, input the command in this format: " + - splitInput[0] + "{TASK}"); + splitInput[0] + " {TASK}"); } else { throw new DukeException("Missing input for command, input the command in this format: " + - splitInput[0] + "{ID}"); + splitInput[0] + " {ID}"); } - } return splitInput[1]; } + /** + * Creates a Todo object from the user's argument. + * + * @param input The argument that the user inputs. + * @return A new Todo object based on user inputs. + */ public Todo handleTodo(String input) { return new Todo(input); } - + /** + * Creates a Deadline object from the user's argument by identifying the description and timing. + * @param input The argument the user inputs. + * @return A new Deadline object based on the user's input. + * @throws DukeException If the user does not input the proper argument. + */ public Deadline handleDeadline(String input) throws DukeException { String[] splitCommand = splitDeadlineTime(input); if (splitCommand.length != SPLIT_AMOUNT) { @@ -100,6 +142,12 @@ public Deadline handleDeadline(String input) throws DukeException { return new Deadline(description, by); } + /** + * Creates an Event object from the user's argument by identifying the description and timing. + * @param input The argument the user inputs. + * @return A new Event object based on the user's input. + * @throws DukeException If the user does not input the proper argument. + */ public Event handleEvent(String input) throws DukeException { String[] splitCommand = splitEventTime(input); if (splitCommand.length != SPLIT_AMOUNT) { @@ -110,7 +158,13 @@ public Event handleEvent(String input) throws DukeException { return new Event(description, at); } - + /** + * Converts the user argument into an integer. + * + * @param input The argument the user inputs. + * @return An integer based on user's input. + * @throws DukeException If the user's argument cannot be converted into an integer. + */ public int readIndex(String input) throws DukeException { try { int index = Integer.parseInt(input); @@ -123,11 +177,23 @@ public int readIndex(String input) throws DukeException { } } - private String[] splitDeadlineTime(String input) throws DukeException { + /** + * Splits the user's deadline argument into its description and time. + * + * @param input The argument the user inputs. + * @return An array of strings that contain the deadline's description and time respectively. + */ + private String[] splitDeadlineTime(String input) { return input.split(" /by ", SPLIT_AMOUNT); } - private String[] splitEventTime(String input) throws DukeException { + /** + * Splits the user's event argument into its description and time. + * + * @param input The argument the user inputs. + * @return An array of strings that contain the event's description and time respectively. + */ + private String[] splitEventTime(String input) { return input.split(" /at ", SPLIT_AMOUNT); } diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index d305852f..cf7f2367 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -11,12 +11,21 @@ import java.nio.file.Path; import java.nio.file.Paths; +/** + * A class that handles the writing and reading of files in the local storage. + */ public class Storage { private static Path taskDataPath = Paths.get(System.getProperty("user.dir"), "data", "tasks.txt"); private static Path dataDirPath = Paths.get(System.getProperty("user.dir"), "data"); private ArrayList tasks = new ArrayList<>(); + /** + * Adds a task to the file by appending to it. + * + * @param task The task to be added. + * @throws IOException If the file is missing. + */ public void writeToFile(Task task) throws IOException { FileWriter fw = new FileWriter(taskDataPath.toString(), true); String textToAdd = taskToText(task); @@ -24,7 +33,12 @@ public void writeToFile(Task task) throws IOException { fw.close(); } - + /** + * Method that populates an ArrayList based on the tasks in the file. + * This method will create the file and its directory if they are missing. + * + * @return An ArrayList of tasks in the text file. + */ public ArrayList startReading() { try { makeMissingDirectory(); @@ -37,6 +51,11 @@ public ArrayList startReading() { return tasks; } + /** + * Reads the file from start to finish and identifies the tasks in it. + * + * @throws FileNotFoundException If file is not found. + */ private void readFile() throws FileNotFoundException { String filePath = taskDataPath.toString(); File f = new File(filePath); @@ -57,7 +76,12 @@ private void readFile() throws FileNotFoundException { } } - private static void makeMissingDirectory() throws IOException { + /** + * Makes the directory if it is missing. + * + * @throws IOException if the path is not found. + */ + private void makeMissingDirectory() throws IOException { File dir = new File(dataDirPath.toString()); if (!dir.exists()) { dir.mkdir(); @@ -67,6 +91,12 @@ private static void makeMissingDirectory() throws IOException { FileWriter fw = new FileWriter(taskDataPath.toString()); } + /** + * Inserts a Todo object into the list of tasks. + * + * @param input The substrings a single line in the text file separated by |. + * @param tasks The list that contains all tasks in the text file. + */ private void insertTodo(String[] input, ArrayList tasks) { Todo todo = new Todo(input[2]); if (input[1].equals("1")) { @@ -77,6 +107,12 @@ private void insertTodo(String[] input, ArrayList tasks) { tasks.add(todo); } + /** + * Inserts a Deadline object into the list of tasks. + * + * @param input The substrings a single line in the text file separated by |. + * @param tasks The list that contains all tasks in the text file. + */ private void insertDeadline(String[] input, ArrayList tasks) { Deadline deadline = new Deadline(input[2], input[3]); if (input[1].equals("1")) { @@ -87,6 +123,12 @@ private void insertDeadline(String[] input, ArrayList tasks) { tasks.add(deadline); } + /** + * Inserts an Event object into the list of tasks. + * + * @param input The substrings a single line in the text file separated by |. + * @param tasks The list that contains all tasks in the text file. + */ private void insertEvent(String[] input, ArrayList tasks) { Event event = new Event(input[2], input[3]); if (input[1].equals("1")) { @@ -97,6 +139,12 @@ private void insertEvent(String[] input, ArrayList tasks) { tasks.add(event); } + /** + * Converts a task into its text representation. + * + * @param task The task to be converted. + * @return The text representation of the task. + */ private String taskToText(Task task) { if (task.getClass() == Todo.class) { return "T | " + task.getStatusInNumber() + " | " + task.getDescription() + "\n"; @@ -109,7 +157,42 @@ private String taskToText(Task task) { } } + /** + * Deletes everything in the text file. + * + * @throws IOException If file is missing. + */ public void deleteContent() throws IOException { new FileWriter(taskDataPath.toString()).close(); } + + /** + * Stores a task into the local text file. + * + * @param task The task to be stored. + * @param storage Object that handles saving the user's task into the local storage. + */ + public void storeTask(Task task, Storage storage) { + try { + storage.writeToFile(task); + } catch (IOException e) { + System.out.println("File not found/missing directory."); + } + } + + /** + * Empties the text file and repopulates it with the updated tasks. + * + * @param storage Object that handles saving the user's task into the local storage. + */ + public void rewriteFile(Storage storage) { + try { + storage.deleteContent(); + for (Task task : tasks) { + storage.writeToFile(task); + } + } catch (IOException e) { + System.out.println("File is not found/missing directory."); + } + } } diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index f4e34971..53492a42 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -1,17 +1,37 @@ package task; +/** + * A class that represents a deadline. + */ public class Deadline extends Task { protected String by; + /** + * Constructor for Deadline. + * + * @param description Description of this deadline. + * @param by Due date of this deadline. + */ public Deadline(String description, String by) { super(description); this.by = by; } + /** + * Gets the due date of this deadline. + * + * @return Due date of this deadline. + */ public String getBy() { return by; } + /** + * Fixes how this deadline would look like if it were to be converted into a string + * based on its description and due date. + * + * @return The string representation of this deadline. + */ @Override public String toString() { return "[D]" + super.getStatusIcon() + " " + super.getDescription() + " (by: " + by + ")"; diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index 4d331e79..b0fdeac9 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -1,16 +1,36 @@ package task; +/** + * A class representing an event. + */ public class Event extends Task { protected String at; + /** + * Constructor for Event. + * + * @param description Description of this event. + * @param at When this event is at. + */ public Event(String description, String at) { super(description); this.at = at; } + /** + * Gets the date when the event is occurring. + * + * @return When the event is at. + */ public String getAt() { return at; } + /** + * Fixes how this deadline would look like if it were to be converted into a string + * based on its description and occurring date + * + * @return The string representation of this event. + */ @Override public String toString() { return "[E]" + super.getStatusIcon() + " " + super.getDescription() + " (at: " + at + ")"; diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index 69a5c81b..c8fb5be9 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -1,30 +1,63 @@ package task; +/** + * A class that represents the different forms of tasks in Duke. + */ public class Task { protected String description; protected boolean isDone; + /** + * Constructor for Task. + * + * @param description The task description. + */ public Task(String description) { this.description = description; this.isDone = false; } + /** + * Sets the done state of this task. + * + * @param isDone Whether this task is to be marked as done. + */ public void setDone(boolean isDone) { this.isDone = isDone; } + /** + * Gets the done state of this task. + * + * @return Whether this task is done. + */ public boolean getDone() { return isDone; } + /** + * Gets the status icon. + * + * @return [X] if isDone, [] otherwise. + */ public String getStatusIcon() { return isDone ? "[X]" : "[ ]"; } + /** + * Gets the status in terms of 1 and 0. + * + * @return 1 if isDone, 0 otherwise. + */ public int getStatusInNumber() { return isDone ? 1 : 0; } + /** + * Gets task description + * + * @return Description of this task. + */ public String getDescription() { return description; } diff --git a/src/main/java/task/TaskList.java b/src/main/java/task/TaskList.java index 86f43de6..7e025914 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -5,75 +5,104 @@ import java.io.IOException; import java.util.ArrayList; +/** + * A class that handles all the user's inputted tasks. + */ public class TaskList { private static ArrayList tasks; private static int taskCount = 0; - private static final String LINE_DIVIDER = "-------------------------------------"; - private static final String ROGER_MESSAGE = "Got it. I have added this task:"; + /** + * Constructor for TaskList. + * + * @param tasks The list of tasks. + */ public TaskList(ArrayList tasks) { TaskList.tasks = tasks; TaskList.taskCount = tasks.size(); } + /** + * Gets the number of tasks in the list. + * + * @return The number of tasks. + */ public static int getTaskCount() { return taskCount; } + /** + * Adds a task to the list. + * + * @param task The task to be added. + */ public void addTask(Task task) { tasks.add(task); taskCount++; } - + /** + * Marks a task on the list. + * + * @param target The index of the task to be marked. + */ public void markTarget(int target) { tasks.get(target - 1).setDone(true); } + /** + * Unmarks a task on the list. + * + * @param target The index of the task to be unmarked. + */ public void unmarkTarget(int target) { tasks.get(target - 1).setDone(false); } - public static void printList() { - System.out.println("Here are a list of your tasks: "); - int index = 1; - for (Task task : tasks) { - System.out.println(index + ". " + task); - index++; - } - System.out.println(LINE_DIVIDER); - } - + /** + * Deletes a task from the list. + * + * @param index The index of the task to be deleted. + */ public void deleteTask(int index) { tasks.remove(index - 1); taskCount--; } + /** + * Gets a copy of all the current tasks in the list. + * + * @return The list of all tasks. + */ public ArrayList getTasks() { return tasks; } + /** + * Gets the task of a specific index in the list. + * + * @param index The index of the task to get. + * @return The task of that index in the list. + */ public Task getTask(int index) { return tasks.get(index); } - public void storeTask(Task task, Storage storage) { - try { - storage.writeToFile(task); - } catch (IOException e) { - System.out.println("File not found/missing directory."); - } - } - - public void rewriteFile(Storage storage) { - try { - storage.deleteContent(); - for (Task task : tasks) { - storage.writeToFile(task); + /** + * Searches through all the tasks in the list to find for tasks that have + * descriptions containing a certain target word. + * + * @param target The word to be searched for. + * @return The list of all tasks that have descriptions containing that word. + */ + public ArrayList findInList(String target) { + ArrayList found = new ArrayList<>(); + for (Task task : tasks) { + if (task.getDescription().contains(target)) { + found.add(task); } - } catch (IOException e) { - System.out.println("File is not found/missing directory."); } + return found; } } diff --git a/src/main/java/task/Todo.java b/src/main/java/task/Todo.java index 710e5f95..ae3a324b 100644 --- a/src/main/java/task/Todo.java +++ b/src/main/java/task/Todo.java @@ -1,10 +1,24 @@ package task; +/** + * A clas representing a todo. + */ public class Todo extends Task { + /** + * Constructor for Todo + * + * @param description Description of this todo. + */ public Todo(String description) { super(description); } + /** + * Fixes how this Todo would look like if it were to be converted into a string + * based on its description. + * + * @return The string representation of this Todo. + */ @Override public String toString() { return "[T]" + super.getStatusIcon() + " " + super.getDescription(); diff --git a/src/main/java/task/command/ByeCommand.java b/src/main/java/task/command/ByeCommand.java index d16d175c..19bd5344 100644 --- a/src/main/java/task/command/ByeCommand.java +++ b/src/main/java/task/command/ByeCommand.java @@ -4,8 +4,19 @@ import task.TaskList; import ui.UI; +/** + * A class that represents a bye command + */ public class ByeCommand extends Command { + /** + * Executes the bye command by displaying the exit message + * when the user quits the program. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { ui.byeMessage(); diff --git a/src/main/java/task/command/Command.java b/src/main/java/task/command/Command.java index 60f80051..d0f136d4 100644 --- a/src/main/java/task/command/Command.java +++ b/src/main/java/task/command/Command.java @@ -4,7 +4,17 @@ import task.TaskList; import ui.UI; +/** + * An abstract class that can represent all other commands within Duke. + */ public abstract class Command { + /** + * An abstract method represents the execution of a command. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ public abstract void execute(TaskList tasks, UI ui, Storage storage); } diff --git a/src/main/java/task/command/DeadlineCommand.java b/src/main/java/task/command/DeadlineCommand.java index 61eaa64d..ccad3c0c 100644 --- a/src/main/java/task/command/DeadlineCommand.java +++ b/src/main/java/task/command/DeadlineCommand.java @@ -5,18 +5,35 @@ import task.TaskList; import ui.UI; +/** + * A class that represents a deadline command. + */ public class DeadlineCommand extends Command { private Deadline deadline; + /** + * Constructor for DeadlineCommand. + * + * @param deadline A deadline object. + */ public DeadlineCommand(Deadline deadline) { this.deadline = deadline; } + /** + * Executes the deadline command by adding a deadline in the list + * and storing it in the local storage. + * Displays a message telling user that the task has been added. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { tasks.addTask(deadline); ui.addMessage(deadline, tasks); - tasks.storeTask(deadline, storage); + storage.storeTask(deadline, storage); } } diff --git a/src/main/java/task/command/DeleteCommand.java b/src/main/java/task/command/DeleteCommand.java index e9252854..5f74c60f 100644 --- a/src/main/java/task/command/DeleteCommand.java +++ b/src/main/java/task/command/DeleteCommand.java @@ -4,17 +4,35 @@ import task.TaskList; import ui.UI; +/** + * A class that represents a delete command. + */ public class DeleteCommand extends Command { private int index; + /** + * Constructor for DeleteCommand. + * + * @param index Index of task to be deleted. + */ public DeleteCommand(int index) { this.index = index; } + + /** + * Executes the delete command by removing the task from the list + * and the local storage. + * Displays a message telling user that the task has been deleted. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { ui.deleteMessage(tasks, index); tasks.deleteTask(index); - tasks.rewriteFile(storage); + storage.rewriteFile(storage); } } diff --git a/src/main/java/task/command/EventCommand.java b/src/main/java/task/command/EventCommand.java index be9fc6f4..22a77ac2 100644 --- a/src/main/java/task/command/EventCommand.java +++ b/src/main/java/task/command/EventCommand.java @@ -5,17 +5,33 @@ import task.TaskList; import ui.UI; +/** + * A class that represents a event command. + */ public class EventCommand extends Command { private Event event; + /** + * Constructor for EventCommand. + * @param event An event object + */ public EventCommand(Event event) { this.event = event; } + /** + * Executes the event command by adding an event into the list + * and storing it in the local storage. + * Displays a message telling user that the task has been added. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { tasks.addTask(event); ui.addMessage(event, tasks); - tasks.storeTask(event, storage); + storage.storeTask(event, storage); } } diff --git a/src/main/java/task/command/FindCommand.java b/src/main/java/task/command/FindCommand.java new file mode 100644 index 00000000..b4ebfa93 --- /dev/null +++ b/src/main/java/task/command/FindCommand.java @@ -0,0 +1,41 @@ +package task.command; + +import storage.Storage; +import task.Task; +import task.TaskList; +import ui.UI; + +import java.util.ArrayList; + +/** + * A class that represents a find command. + */ +public class FindCommand extends Command { + + private String target; + + /** + * Constructor for FindCommand. + * + * @param target String that user wants to find in list. + */ + public FindCommand(String target) { + this.target = target; + } + + /** + * Executes the find command by searching the list of tasks for any + * occurence of the target string. + * Displays the list of matching items or shows a message that no matching + * items are found. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ + @Override + public void execute(TaskList tasks, UI ui, Storage storage) { + ArrayList found = tasks.findInList(target); + ui.printFound(found); + } +} diff --git a/src/main/java/task/command/InvalidCommand.java b/src/main/java/task/command/InvalidCommand.java index bdaed012..063635a1 100644 --- a/src/main/java/task/command/InvalidCommand.java +++ b/src/main/java/task/command/InvalidCommand.java @@ -4,7 +4,18 @@ import task.TaskList; import ui.UI; -public class InvalidCommand extends Command{ +/** + * A class that represents an invalid command. + */ +public class InvalidCommand extends Command { + /** + * Executes the invalid command by displaying a message telling the user + * that their command is invalid. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { ui.invalidCommand(); diff --git a/src/main/java/task/command/ListCommand.java b/src/main/java/task/command/ListCommand.java index f242f87c..11ceb143 100644 --- a/src/main/java/task/command/ListCommand.java +++ b/src/main/java/task/command/ListCommand.java @@ -7,8 +7,19 @@ import java.util.ArrayList; +/** + * A class that represents a list command. + */ public class ListCommand extends Command { + /** + * Executes the list command by displaying the list of all tasks current + * in the list, or tells user that the list is empty. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { ArrayList list = tasks.getTasks(); diff --git a/src/main/java/task/command/MarkCommand.java b/src/main/java/task/command/MarkCommand.java index d88e898d..4ffc8e8b 100644 --- a/src/main/java/task/command/MarkCommand.java +++ b/src/main/java/task/command/MarkCommand.java @@ -4,16 +4,33 @@ import task.TaskList; import ui.UI; +/** + * A class that represents the mark command. + */ public class MarkCommand extends Command { private int index; + + /** + * Construstor for MarkCommand. + * + * @param index Index of task to be marked. + */ public MarkCommand(int index) { this.index = index; } + /** + * Executes the mark command by marking the tasks of the index. + * Displays a message telling the user that the task has been marked. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { tasks.markTarget(index); ui.markMessage(tasks, index); - tasks.rewriteFile(storage); + storage.rewriteFile(storage); } } diff --git a/src/main/java/task/command/TodoCommand.java b/src/main/java/task/command/TodoCommand.java index 1a1a0811..2ee30a2e 100644 --- a/src/main/java/task/command/TodoCommand.java +++ b/src/main/java/task/command/TodoCommand.java @@ -6,17 +6,34 @@ import task.Todo; import ui.UI; +/** + * A class that represents the todo command. + */ public class TodoCommand extends Command { private Todo todo; + /** + * Constructor for TodoCommand. + * + * @param todo A Todo object. + */ public TodoCommand(Todo todo) { this.todo = todo; } + /** + * Executes the todo command by adding a task todo into the list + * and storing it in the local storage. + * Displays a message telling user that the task has been added. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { tasks.addTask(todo); ui.addMessage(todo, tasks); - tasks.storeTask(todo, storage); + storage.storeTask(todo, storage); } } diff --git a/src/main/java/task/command/UnmarkCommand.java b/src/main/java/task/command/UnmarkCommand.java index cd69c127..45275730 100644 --- a/src/main/java/task/command/UnmarkCommand.java +++ b/src/main/java/task/command/UnmarkCommand.java @@ -4,16 +4,33 @@ import task.TaskList; import ui.UI; +/** + * A class representing an unmark command. + */ public class UnmarkCommand extends Command { private int index; + + /** + * Constructor for UnmarkCommand. + * + * @param index Index of task to be unmarked. + */ public UnmarkCommand(int index) { this.index = index; } + /** + * Executes the mark command by unmarking the tasks of the index. + * Displays a message telling the user that the task has been unmarked. + * + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. + * @param storage Object that handles saving the user's task into the local storage. + */ @Override public void execute(TaskList tasks, UI ui, Storage storage) { tasks.unmarkTarget(index); ui.markMessage(tasks, index); - tasks.rewriteFile(storage); + storage.rewriteFile(storage); } } diff --git a/src/main/java/ui/UI.java b/src/main/java/ui/UI.java index 450882a6..cbaab28d 100644 --- a/src/main/java/ui/UI.java +++ b/src/main/java/ui/UI.java @@ -75,6 +75,24 @@ public void deleteMessage(TaskList tasks, int index) { + " tasks."); } + public void printFound(ArrayList found) { + String message = "Here are the matching tasks in your list:\n"; + int size = found.size(); + if (size == 0) { + message = "Found no matching tasks in your list."; + } else { + for (int i = 0; i < size; i++) { + if (i == size - 1) + { + message += (i + 1) + ". " + found.get(i); + } else { + message += (i + 1) + ". " + found.get(i) + "\n"; + } + } + } + formatMessage(message); + } + public String getInput() { Scanner sc = new Scanner(System.in); String input = sc.nextLine(); From d0aba840be2782bae560690d7bf4a04ba8328e33 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 29 Sep 2022 01:01:45 +0800 Subject: [PATCH 24/26] Add JavaDoc Repackaged some files --- src/main/java/META-INF/MANIFEST.MF | 2 +- src/main/java/{ => duke}/Duke.java | 10 ++++++---- .../java/{ => duke}/exception/DukeException.java | 2 +- src/main/java/{ => duke}/parser/Parser.java | 12 ++++++------ src/main/java/{ => duke}/storage/Storage.java | 4 ++-- src/main/java/{ => duke}/task/Deadline.java | 2 +- src/main/java/{ => duke}/task/Event.java | 2 +- src/main/java/{ => duke}/task/Task.java | 2 +- src/main/java/{ => duke}/task/TaskList.java | 5 +---- src/main/java/{ => duke}/task/Todo.java | 2 +- .../java/{ => duke}/task/command/ByeCommand.java | 10 +++++----- src/main/java/{ => duke}/task/command/Command.java | 8 ++++---- .../{ => duke}/task/command/DeadlineCommand.java | 12 ++++++------ .../java/{ => duke}/task/command/DeleteCommand.java | 10 +++++----- .../java/{ => duke}/task/command/EventCommand.java | 12 ++++++------ .../java/{ => duke}/task/command/FindCommand.java | 12 ++++++------ .../{ => duke}/task/command/InvalidCommand.java | 10 +++++----- .../java/{ => duke}/task/command/ListCommand.java | 12 ++++++------ .../java/{ => duke}/task/command/MarkCommand.java | 10 +++++----- .../java/{ => duke}/task/command/TodoCommand.java | 13 ++++++------- .../java/{ => duke}/task/command/UnmarkCommand.java | 10 +++++----- src/main/java/{ => duke}/ui/UI.java | 6 +++--- 22 files changed, 83 insertions(+), 85 deletions(-) rename src/main/java/{ => duke}/Duke.java (81%) rename src/main/java/{ => duke}/exception/DukeException.java (92%) rename src/main/java/{ => duke}/parser/Parser.java (97%) rename src/main/java/{ => duke}/storage/Storage.java (99%) rename src/main/java/{ => duke}/task/Deadline.java (97%) rename src/main/java/{ => duke}/task/Event.java (97%) rename src/main/java/{ => duke}/task/Task.java (98%) rename src/main/java/{ => duke}/task/TaskList.java (97%) rename src/main/java/{ => duke}/task/Todo.java (96%) rename src/main/java/{ => duke}/task/command/ByeCommand.java (75%) rename src/main/java/{ => duke}/task/command/Command.java (83%) rename src/main/java/{ => duke}/task/command/DeadlineCommand.java (81%) rename src/main/java/{ => duke}/task/command/DeleteCommand.java (83%) rename src/main/java/{ => duke}/task/command/EventCommand.java (81%) rename src/main/java/{ => duke}/task/command/FindCommand.java (82%) rename src/main/java/{ => duke}/task/command/InvalidCommand.java (74%) rename src/main/java/{ => duke}/task/command/ListCommand.java (74%) rename src/main/java/{ => duke}/task/command/MarkCommand.java (82%) rename src/main/java/{ => duke}/task/command/TodoCommand.java (81%) rename src/main/java/{ => duke}/task/command/UnmarkCommand.java (82%) rename src/main/java/{ => duke}/ui/UI.java (97%) diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF index 9f37e4e0..2c9a9745 100644 --- a/src/main/java/META-INF/MANIFEST.MF +++ b/src/main/java/META-INF/MANIFEST.MF @@ -1,3 +1,3 @@ Manifest-Version: 1.0 -Main-Class: Duke +Main-Class: duke.Duke diff --git a/src/main/java/Duke.java b/src/main/java/duke/Duke.java similarity index 81% rename from src/main/java/Duke.java rename to src/main/java/duke/Duke.java index 6d4cf8a0..23c14634 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,7 +1,9 @@ -import storage.Storage; -import parser.Parser; -import task.TaskList; -import ui.UI; +package duke; + +import duke.storage.Storage; +import duke.parser.Parser; +import duke.task.TaskList; +import duke.ui.UI; public class Duke { diff --git a/src/main/java/exception/DukeException.java b/src/main/java/duke/exception/DukeException.java similarity index 92% rename from src/main/java/exception/DukeException.java rename to src/main/java/duke/exception/DukeException.java index 878184fd..581c603e 100644 --- a/src/main/java/exception/DukeException.java +++ b/src/main/java/duke/exception/DukeException.java @@ -1,4 +1,4 @@ -package exception; +package duke.exception; public class DukeException extends Exception { diff --git a/src/main/java/parser/Parser.java b/src/main/java/duke/parser/Parser.java similarity index 97% rename from src/main/java/parser/Parser.java rename to src/main/java/duke/parser/Parser.java index 7c15f9c7..82082d80 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -1,10 +1,10 @@ -package parser; +package duke.parser; -import storage.Storage; -import task.*; -import task.command.*; -import ui.UI; -import exception.DukeException; +import duke.storage.Storage; +import duke.task.*; +import duke.task.command.*; +import duke.ui.UI; +import duke.exception.DukeException; /** * An object that deals with anything that the user inputs so that logic diff --git a/src/main/java/storage/Storage.java b/src/main/java/duke/storage/Storage.java similarity index 99% rename from src/main/java/storage/Storage.java rename to src/main/java/duke/storage/Storage.java index cf7f2367..fa021946 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -1,6 +1,6 @@ -package storage; +package duke.storage; -import task.*; +import duke.task.*; import java.io.FileWriter; import java.io.IOException; diff --git a/src/main/java/task/Deadline.java b/src/main/java/duke/task/Deadline.java similarity index 97% rename from src/main/java/task/Deadline.java rename to src/main/java/duke/task/Deadline.java index 53492a42..e61fb4f6 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,4 +1,4 @@ -package task; +package duke.task; /** * A class that represents a deadline. diff --git a/src/main/java/task/Event.java b/src/main/java/duke/task/Event.java similarity index 97% rename from src/main/java/task/Event.java rename to src/main/java/duke/task/Event.java index b0fdeac9..164a2e41 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,4 +1,4 @@ -package task; +package duke.task; /** * A class representing an event. diff --git a/src/main/java/task/Task.java b/src/main/java/duke/task/Task.java similarity index 98% rename from src/main/java/task/Task.java rename to src/main/java/duke/task/Task.java index c8fb5be9..d10042a7 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -1,4 +1,4 @@ -package task; +package duke.task; /** * A class that represents the different forms of tasks in Duke. diff --git a/src/main/java/task/TaskList.java b/src/main/java/duke/task/TaskList.java similarity index 97% rename from src/main/java/task/TaskList.java rename to src/main/java/duke/task/TaskList.java index 7e025914..e68e8218 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/duke/task/TaskList.java @@ -1,8 +1,5 @@ -package task; +package duke.task; -import storage.Storage; - -import java.io.IOException; import java.util.ArrayList; /** diff --git a/src/main/java/task/Todo.java b/src/main/java/duke/task/Todo.java similarity index 96% rename from src/main/java/task/Todo.java rename to src/main/java/duke/task/Todo.java index ae3a324b..ebf0fb74 100644 --- a/src/main/java/task/Todo.java +++ b/src/main/java/duke/task/Todo.java @@ -1,4 +1,4 @@ -package task; +package duke.task; /** * A clas representing a todo. diff --git a/src/main/java/task/command/ByeCommand.java b/src/main/java/duke/task/command/ByeCommand.java similarity index 75% rename from src/main/java/task/command/ByeCommand.java rename to src/main/java/duke/task/command/ByeCommand.java index 19bd5344..2f60085f 100644 --- a/src/main/java/task/command/ByeCommand.java +++ b/src/main/java/duke/task/command/ByeCommand.java @@ -1,8 +1,8 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.UI; /** * A class that represents a bye command @@ -10,7 +10,7 @@ public class ByeCommand extends Command { /** - * Executes the bye command by displaying the exit message + * Executes this bye command by displaying the exit message * when the user quits the program. * * @param ui Object that handles all user interaction. diff --git a/src/main/java/task/command/Command.java b/src/main/java/duke/task/command/Command.java similarity index 83% rename from src/main/java/task/command/Command.java rename to src/main/java/duke/task/command/Command.java index d0f136d4..3298c410 100644 --- a/src/main/java/task/command/Command.java +++ b/src/main/java/duke/task/command/Command.java @@ -1,8 +1,8 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.UI; /** * An abstract class that can represent all other commands within Duke. diff --git a/src/main/java/task/command/DeadlineCommand.java b/src/main/java/duke/task/command/DeadlineCommand.java similarity index 81% rename from src/main/java/task/command/DeadlineCommand.java rename to src/main/java/duke/task/command/DeadlineCommand.java index ccad3c0c..60f86991 100644 --- a/src/main/java/task/command/DeadlineCommand.java +++ b/src/main/java/duke/task/command/DeadlineCommand.java @@ -1,9 +1,9 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.Deadline; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.Deadline; +import duke.task.TaskList; +import duke.ui.UI; /** * A class that represents a deadline command. @@ -22,7 +22,7 @@ public DeadlineCommand(Deadline deadline) { } /** - * Executes the deadline command by adding a deadline in the list + * Executes this deadline command by adding a deadline in the list * and storing it in the local storage. * Displays a message telling user that the task has been added. * diff --git a/src/main/java/task/command/DeleteCommand.java b/src/main/java/duke/task/command/DeleteCommand.java similarity index 83% rename from src/main/java/task/command/DeleteCommand.java rename to src/main/java/duke/task/command/DeleteCommand.java index 5f74c60f..03efaf7a 100644 --- a/src/main/java/task/command/DeleteCommand.java +++ b/src/main/java/duke/task/command/DeleteCommand.java @@ -1,8 +1,8 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.UI; /** * A class that represents a delete command. @@ -21,7 +21,7 @@ public DeleteCommand(int index) { } /** - * Executes the delete command by removing the task from the list + * Executes this delete command by removing the task from the list * and the local storage. * Displays a message telling user that the task has been deleted. * diff --git a/src/main/java/task/command/EventCommand.java b/src/main/java/duke/task/command/EventCommand.java similarity index 81% rename from src/main/java/task/command/EventCommand.java rename to src/main/java/duke/task/command/EventCommand.java index 22a77ac2..76b63a52 100644 --- a/src/main/java/task/command/EventCommand.java +++ b/src/main/java/duke/task/command/EventCommand.java @@ -1,9 +1,9 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.Event; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.Event; +import duke.task.TaskList; +import duke.ui.UI; /** * A class that represents a event command. @@ -20,7 +20,7 @@ public EventCommand(Event event) { } /** - * Executes the event command by adding an event into the list + * Executes this event command by adding an event into the list * and storing it in the local storage. * Displays a message telling user that the task has been added. * diff --git a/src/main/java/task/command/FindCommand.java b/src/main/java/duke/task/command/FindCommand.java similarity index 82% rename from src/main/java/task/command/FindCommand.java rename to src/main/java/duke/task/command/FindCommand.java index b4ebfa93..fa88428b 100644 --- a/src/main/java/task/command/FindCommand.java +++ b/src/main/java/duke/task/command/FindCommand.java @@ -1,9 +1,9 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.Task; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.Task; +import duke.task.TaskList; +import duke.ui.UI; import java.util.ArrayList; @@ -24,7 +24,7 @@ public FindCommand(String target) { } /** - * Executes the find command by searching the list of tasks for any + * Executes this find command by searching the list of tasks for any * occurence of the target string. * Displays the list of matching items or shows a message that no matching * items are found. diff --git a/src/main/java/task/command/InvalidCommand.java b/src/main/java/duke/task/command/InvalidCommand.java similarity index 74% rename from src/main/java/task/command/InvalidCommand.java rename to src/main/java/duke/task/command/InvalidCommand.java index 063635a1..73b8755a 100644 --- a/src/main/java/task/command/InvalidCommand.java +++ b/src/main/java/duke/task/command/InvalidCommand.java @@ -1,15 +1,15 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.UI; /** * A class that represents an invalid command. */ public class InvalidCommand extends Command { /** - * Executes the invalid command by displaying a message telling the user + * Executes this invalid command by displaying a message telling the user * that their command is invalid. * * @param ui Object that handles all user interaction. diff --git a/src/main/java/task/command/ListCommand.java b/src/main/java/duke/task/command/ListCommand.java similarity index 74% rename from src/main/java/task/command/ListCommand.java rename to src/main/java/duke/task/command/ListCommand.java index 11ceb143..94ed29fa 100644 --- a/src/main/java/task/command/ListCommand.java +++ b/src/main/java/duke/task/command/ListCommand.java @@ -1,9 +1,9 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.Task; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.Task; +import duke.task.TaskList; +import duke.ui.UI; import java.util.ArrayList; @@ -13,7 +13,7 @@ public class ListCommand extends Command { /** - * Executes the list command by displaying the list of all tasks current + * Executes this list command by displaying the list of all tasks current * in the list, or tells user that the list is empty. * * @param ui Object that handles all user interaction. diff --git a/src/main/java/task/command/MarkCommand.java b/src/main/java/duke/task/command/MarkCommand.java similarity index 82% rename from src/main/java/task/command/MarkCommand.java rename to src/main/java/duke/task/command/MarkCommand.java index 4ffc8e8b..bf6dff81 100644 --- a/src/main/java/task/command/MarkCommand.java +++ b/src/main/java/duke/task/command/MarkCommand.java @@ -1,8 +1,8 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.UI; /** * A class that represents the mark command. @@ -20,7 +20,7 @@ public MarkCommand(int index) { } /** - * Executes the mark command by marking the tasks of the index. + * Executes this mark command by marking the tasks of the index. * Displays a message telling the user that the task has been marked. * * @param ui Object that handles all user interaction. diff --git a/src/main/java/task/command/TodoCommand.java b/src/main/java/duke/task/command/TodoCommand.java similarity index 81% rename from src/main/java/task/command/TodoCommand.java rename to src/main/java/duke/task/command/TodoCommand.java index 2ee30a2e..bf929228 100644 --- a/src/main/java/task/command/TodoCommand.java +++ b/src/main/java/duke/task/command/TodoCommand.java @@ -1,10 +1,9 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.Task; -import task.TaskList; -import task.Todo; -import ui.UI; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.task.Todo; +import duke.ui.UI; /** * A class that represents the todo command. @@ -22,7 +21,7 @@ public TodoCommand(Todo todo) { } /** - * Executes the todo command by adding a task todo into the list + * Executes this todo command by adding a task todo into the list * and storing it in the local storage. * Displays a message telling user that the task has been added. * diff --git a/src/main/java/task/command/UnmarkCommand.java b/src/main/java/duke/task/command/UnmarkCommand.java similarity index 82% rename from src/main/java/task/command/UnmarkCommand.java rename to src/main/java/duke/task/command/UnmarkCommand.java index 45275730..86b4ef64 100644 --- a/src/main/java/task/command/UnmarkCommand.java +++ b/src/main/java/duke/task/command/UnmarkCommand.java @@ -1,8 +1,8 @@ -package task.command; +package duke.task.command; -import storage.Storage; -import task.TaskList; -import ui.UI; +import duke.storage.Storage; +import duke.task.TaskList; +import duke.ui.UI; /** * A class representing an unmark command. @@ -20,7 +20,7 @@ public UnmarkCommand(int index) { } /** - * Executes the mark command by unmarking the tasks of the index. + * Executes this unmark command by unmarking the tasks of the index. * Displays a message telling the user that the task has been unmarked. * * @param ui Object that handles all user interaction. diff --git a/src/main/java/ui/UI.java b/src/main/java/duke/ui/UI.java similarity index 97% rename from src/main/java/ui/UI.java rename to src/main/java/duke/ui/UI.java index cbaab28d..5e0f6f01 100644 --- a/src/main/java/ui/UI.java +++ b/src/main/java/duke/ui/UI.java @@ -1,7 +1,7 @@ -package ui; +package duke.ui; -import task.Task; -import task.TaskList; +import duke.task.Task; +import duke.task.TaskList; import java.util.ArrayList; import java.util.Scanner; From 9a13001fceb6f4526c7c8349d4cc33e1f8c1d715 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 29 Sep 2022 02:05:46 +0800 Subject: [PATCH 25/26] Update README.md --- docs/README.md | 169 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 157 insertions(+), 12 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118e..ce576a17 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,174 @@ # User Guide +Duke is a robot that allows you to track tasks. These tasks can be in either Todos, Deadlines or Events. +You can also get your tasks in a .txt file for you to do whatever you want with it! +## Features +- [Adding a Todo: todo](#adding-a-todo-todo) +- [Adding a Deadline: deadline](#adding-a-deadline-deadline) +- [Adding an Event: event](#adding-an-event-event) +- [Marking a Task: mark](#marking-a-task-mark) +- [Unmarking a Task: unmark](#unmarking-a-task-unmark) +- [Deleting a Task: delete](#deleting-a-task-delete) +- [View all tasks: list](#view-all-tasks-list) +- [Exiting the program: bye](#exiting-the-program-bye) -## Features +### Adding a Todo: `todo` +Adds a Todo to the list -### Feature-ABC +Use the command: -Description of the feature. +`todo sometask` -### Feature-XYZ +Example of Usage: -Description of the feature. +`todo Math Tutorial` -## Usage +Expected outcome: + +``` +------------------------------------- +Got it. I have added this task: +[T][ ] Math Tutorial +You now have 1 tasks. +------------------------------------- +``` + +### Adding a Deadline: `deadline` +Adds a Deadline to the list + +Use the command: + +`deadline sometask /by somedate` + +Example of Usage: + +`deadline Group Project /by Friday` + +Expected outcome: + +``` +------------------------------------- +Got it. I have added this task: +[D][ ] Group Project (by: Friday) +You now have 2 tasks. +------------------------------------- +``` + +### Adding an Event: `event` +Adds an Event to the list + +Use the command: + +`event sometask /at somedate` + +Example of Usage: + +`event Concert /at Sunday 1900` + +Expected outcome: + +``` +------------------------------------- +Got it. I have added this task: +[E][ ] Concert (at: Sunday 1900) +You now have 3 tasks. +------------------------------------- +``` + +### Marking a Task: `mark` + +Marks a task. + +Use the command: + +`mark index` + +Example of usage: + +`mark 2` + +Expected outcome: + +``` +------------------------------------- +Marked the following task: +[D][X] Group Project (by: Friday) +------------------------------------- +``` + +### Unmarking a Task: `unmark` -### `Keyword` - Describe action +Unmarks a task. -Describe the action and its outcome. +Use the command: -Example of usage: +`unmark index` -`keyword (optional arguments)` +Example of usage: + +`unmark 2` Expected outcome: -Description of the outcome. +``` +------------------------------------- +Unmarked the following task: +[D][ ] Group Project (by: Friday) +------------------------------------- +``` + +### Deleting a Task: `delete` + +Deletes a task. + +Use the command: + +`Delete index` + +Example of usage: + +`delete 2` + +Expected outcome: ``` -expected output +------------------------------------- +The following task has been deleted: +[D][ ] Group Project (by: Friday) +You now have 2 tasks. +------------------------------------- ``` + +### View all tasks: `list` + +Displays all the tasks currently in the list. + +Use the command: + +`list` + +Expected outcome: + +``` +------------------------------------- +Here is the list of your tasks: +1. [T][ ] Math Tutorial +2. [E][ ] Concert (at: Sunday 1900) +------------------------------------- +``` + +### Exiting the program: `bye` + +Stops the execution of Duke. + +Use the command: + +`bye` + +Expected outcome: + +``` +------------------------------------- +Goodbye, hope to see you again. +------------------------------------- +``` + From bef3c36623d7525787612aa309292a22c74d6de5 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 29 Sep 2022 03:54:27 +0800 Subject: [PATCH 26/26] Update some code logic --- src/main/java/duke/Duke.java | 4 +-- src/main/java/duke/parser/Parser.java | 25 +++++++++++++------ src/main/java/duke/storage/Storage.java | 2 +- src/main/java/duke/task/Deadline.java | 2 +- src/main/java/duke/task/Event.java | 3 ++- src/main/java/duke/task/Task.java | 2 +- .../java/duke/task/command/ByeCommand.java | 4 +-- src/main/java/duke/task/command/Command.java | 4 +-- .../duke/task/command/DeadlineCommand.java | 4 +-- .../java/duke/task/command/DeleteCommand.java | 4 +-- .../java/duke/task/command/EventCommand.java | 5 ++-- .../java/duke/task/command/FindCommand.java | 6 ++--- .../duke/task/command/InvalidCommand.java | 4 +-- .../java/duke/task/command/ListCommand.java | 4 +-- .../java/duke/task/command/MarkCommand.java | 4 +-- .../java/duke/task/command/TodoCommand.java | 4 +-- .../java/duke/task/command/UnmarkCommand.java | 6 ++--- src/main/java/duke/ui/UI.java | 13 +++++++--- 18 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 23c14634..784484ac 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -17,13 +17,13 @@ public Duke () { Duke.tasks = new TaskList(storage.startReading()); } - public void start() { + public void run() { ui.welcomeUser(); Parser parser = new Parser(); parser.executeCommands(ui, tasks, storage); } public static void main(String[] args) { - new Duke().start(); + new Duke().run(); } } diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 82082d80..dc50270e 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -37,7 +37,7 @@ public void executeCommands(UI ui, TaskList tasks, Storage storage) { Command command = handleCommand(ui); command.execute(tasks, ui, storage); } catch (DukeException e) { - System.out.println(e.getMessage()); + ui.printErrorMessage(e); } } } @@ -104,13 +104,22 @@ private String getCommand(String input) { private String getArgument(String input) throws DukeException { String[] splitInput = input.split(" ", SPLIT_AMOUNT); if (splitInput.length != SPLIT_AMOUNT) { - if (splitInput[0].equals(TODO) || splitInput[0].equals(DEADLINE) || splitInput[0].equals(EVENT) - || splitInput[0].equals(FIND)) { - throw new DukeException("Missing input for command, input the command in this format: " + - splitInput[0] + " {TASK}"); - } else { - throw new DukeException("Missing input for command, input the command in this format: " + - splitInput[0] + " {ID}"); + switch(splitInput[0]) { + case (TODO): + throw new DukeException("Missing input for command, input the command in this format:\n" + + TODO + " taskname"); + case (DEADLINE): + throw new DukeException("Missing input for command, input the command in this format:\n" + + DEADLINE + " taskname /by date"); + case (EVENT): + throw new DukeException("Missing input for command, input the command in this format:\n" + + EVENT + " taskname /at date"); + case (FIND): + throw new DukeException("Missing input for command, input the command in this format:\n" + + FIND + " something"); + default: + throw new DukeException("Missing input for command, input the command in this format:\n" + + splitInput[0] + " id"); } } return splitInput[1]; diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index fa021946..cd693713 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -169,7 +169,7 @@ public void deleteContent() throws IOException { /** * Stores a task into the local text file. * - * @param task The task to be stored. + * @param task The task to be stored. * @param storage Object that handles saving the user's task into the local storage. */ public void storeTask(Task task, Storage storage) { diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index e61fb4f6..b305db70 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -10,7 +10,7 @@ public class Deadline extends Task { * Constructor for Deadline. * * @param description Description of this deadline. - * @param by Due date of this deadline. + * @param by Due date of this deadline. */ public Deadline(String description, String by) { super(description); diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 164a2e41..427cdab5 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -10,7 +10,7 @@ public class Event extends Task { * Constructor for Event. * * @param description Description of this event. - * @param at When this event is at. + * @param at When this event is at. */ public Event(String description, String at) { super(description); @@ -25,6 +25,7 @@ public Event(String description, String at) { public String getAt() { return at; } + /** * Fixes how this deadline would look like if it were to be converted into a string * based on its description and occurring date diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index d10042a7..5aeabf0e 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -18,7 +18,7 @@ public Task(String description) { } /** - * Sets the done state of this task. + * Sets the done state of this task. * * @param isDone Whether this task is to be marked as done. */ diff --git a/src/main/java/duke/task/command/ByeCommand.java b/src/main/java/duke/task/command/ByeCommand.java index 2f60085f..3eded4d5 100644 --- a/src/main/java/duke/task/command/ByeCommand.java +++ b/src/main/java/duke/task/command/ByeCommand.java @@ -13,8 +13,8 @@ public class ByeCommand extends Command { * Executes this bye command by displaying the exit message * when the user quits the program. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override diff --git a/src/main/java/duke/task/command/Command.java b/src/main/java/duke/task/command/Command.java index 3298c410..08fad6a3 100644 --- a/src/main/java/duke/task/command/Command.java +++ b/src/main/java/duke/task/command/Command.java @@ -12,8 +12,8 @@ public abstract class Command { /** * An abstract method represents the execution of a command. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ public abstract void execute(TaskList tasks, UI ui, Storage storage); diff --git a/src/main/java/duke/task/command/DeadlineCommand.java b/src/main/java/duke/task/command/DeadlineCommand.java index 60f86991..e616f015 100644 --- a/src/main/java/duke/task/command/DeadlineCommand.java +++ b/src/main/java/duke/task/command/DeadlineCommand.java @@ -26,8 +26,8 @@ public DeadlineCommand(Deadline deadline) { * and storing it in the local storage. * Displays a message telling user that the task has been added. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override diff --git a/src/main/java/duke/task/command/DeleteCommand.java b/src/main/java/duke/task/command/DeleteCommand.java index 03efaf7a..3c115634 100644 --- a/src/main/java/duke/task/command/DeleteCommand.java +++ b/src/main/java/duke/task/command/DeleteCommand.java @@ -25,8 +25,8 @@ public DeleteCommand(int index) { * and the local storage. * Displays a message telling user that the task has been deleted. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override diff --git a/src/main/java/duke/task/command/EventCommand.java b/src/main/java/duke/task/command/EventCommand.java index 76b63a52..aca4a9b2 100644 --- a/src/main/java/duke/task/command/EventCommand.java +++ b/src/main/java/duke/task/command/EventCommand.java @@ -13,6 +13,7 @@ public class EventCommand extends Command { /** * Constructor for EventCommand. + * * @param event An event object */ public EventCommand(Event event) { @@ -24,8 +25,8 @@ public EventCommand(Event event) { * and storing it in the local storage. * Displays a message telling user that the task has been added. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override diff --git a/src/main/java/duke/task/command/FindCommand.java b/src/main/java/duke/task/command/FindCommand.java index fa88428b..f4cec963 100644 --- a/src/main/java/duke/task/command/FindCommand.java +++ b/src/main/java/duke/task/command/FindCommand.java @@ -12,7 +12,7 @@ */ public class FindCommand extends Command { - private String target; + private final String target; /** * Constructor for FindCommand. @@ -29,8 +29,8 @@ public FindCommand(String target) { * Displays the list of matching items or shows a message that no matching * items are found. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override diff --git a/src/main/java/duke/task/command/InvalidCommand.java b/src/main/java/duke/task/command/InvalidCommand.java index 73b8755a..b1095cca 100644 --- a/src/main/java/duke/task/command/InvalidCommand.java +++ b/src/main/java/duke/task/command/InvalidCommand.java @@ -12,8 +12,8 @@ public class InvalidCommand extends Command { * Executes this invalid command by displaying a message telling the user * that their command is invalid. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override diff --git a/src/main/java/duke/task/command/ListCommand.java b/src/main/java/duke/task/command/ListCommand.java index 94ed29fa..7473fa0f 100644 --- a/src/main/java/duke/task/command/ListCommand.java +++ b/src/main/java/duke/task/command/ListCommand.java @@ -16,8 +16,8 @@ public class ListCommand extends Command { * Executes this list command by displaying the list of all tasks current * in the list, or tells user that the list is empty. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override diff --git a/src/main/java/duke/task/command/MarkCommand.java b/src/main/java/duke/task/command/MarkCommand.java index bf6dff81..b40ef482 100644 --- a/src/main/java/duke/task/command/MarkCommand.java +++ b/src/main/java/duke/task/command/MarkCommand.java @@ -23,8 +23,8 @@ public MarkCommand(int index) { * Executes this mark command by marking the tasks of the index. * Displays a message telling the user that the task has been marked. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override diff --git a/src/main/java/duke/task/command/TodoCommand.java b/src/main/java/duke/task/command/TodoCommand.java index bf929228..7154615b 100644 --- a/src/main/java/duke/task/command/TodoCommand.java +++ b/src/main/java/duke/task/command/TodoCommand.java @@ -25,8 +25,8 @@ public TodoCommand(Todo todo) { * and storing it in the local storage. * Displays a message telling user that the task has been added. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override diff --git a/src/main/java/duke/task/command/UnmarkCommand.java b/src/main/java/duke/task/command/UnmarkCommand.java index 86b4ef64..b50119a2 100644 --- a/src/main/java/duke/task/command/UnmarkCommand.java +++ b/src/main/java/duke/task/command/UnmarkCommand.java @@ -23,12 +23,12 @@ public UnmarkCommand(int index) { * Executes this unmark command by unmarking the tasks of the index. * Displays a message telling the user that the task has been unmarked. * - * @param ui Object that handles all user interaction. - * @param tasks Object that handles and tracks all tasks that the user has added. + * @param ui Object that handles all user interaction. + * @param tasks Object that handles and tracks all tasks that the user has added. * @param storage Object that handles saving the user's task into the local storage. */ @Override - public void execute(TaskList tasks, UI ui, Storage storage) { + public void execute(TaskList tasks, UI ui, Storage storage) { tasks.unmarkTarget(index); ui.markMessage(tasks, index); storage.rewriteFile(storage); diff --git a/src/main/java/duke/ui/UI.java b/src/main/java/duke/ui/UI.java index 5e0f6f01..3a4360fb 100644 --- a/src/main/java/duke/ui/UI.java +++ b/src/main/java/duke/ui/UI.java @@ -1,5 +1,6 @@ package duke.ui; +import duke.exception.DukeException; import duke.task.Task; import duke.task.TaskList; @@ -17,6 +18,7 @@ public class UI { + "delete {index} : Deletes a task\n" + "list : List all current tasks\n" + "bye : Exits the program"; + public final String BYE_MESSAGE = "Goodbye, hope to see you again."; public void welcomeUser() { @@ -33,7 +35,7 @@ public void byeMessage() { public void addMessage(Task task, TaskList tasks) { formatMessage("Got it. I have added this task:\n" + task - + "\nYou now have " + tasks.getTaskCount() + " tasks."); + + "\nYou now have " + TaskList.getTaskCount() + " tasks."); } public void printList(ArrayList tasks) { @@ -71,7 +73,7 @@ public void invalidCommand() { public void deleteMessage(TaskList tasks, int index) { Task deletedTask = tasks.getTask(index - 1); formatMessage("The following task has been deleted: \n" - + deletedTask + "\nYou now have " + (tasks.getTaskCount() - 1) + + deletedTask + "\nYou now have " + (TaskList.getTaskCount() - 1) + " tasks."); } @@ -82,8 +84,7 @@ public void printFound(ArrayList found) { message = "Found no matching tasks in your list."; } else { for (int i = 0; i < size; i++) { - if (i == size - 1) - { + if (i == size - 1) { message += (i + 1) + ". " + found.get(i); } else { message += (i + 1) + ". " + found.get(i) + "\n"; @@ -93,6 +94,10 @@ public void printFound(ArrayList found) { formatMessage(message); } + public void printErrorMessage(DukeException e) { + formatMessage(e.getMessage()); + } + public String getInput() { Scanner sc = new Scanner(System.in); String input = sc.nextLine();