From 2133998627313291bc3baf251fac2a690f86e8bb Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Wed, 1 Feb 2023 00:25:13 +0800 Subject: [PATCH 01/22] Greet and exit --- src/main/java/Duke.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..180bc3fb1 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,5 +6,8 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + System.out.println("Hello! I'm Duke"); + System.out.println("What can I do for you?"); + System.out.println("Bye! Hope to see you again soon!"); } } From aebbd76ba178cf370db8aac26bfdc002b1a43da6 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Wed, 1 Feb 2023 01:13:48 +0800 Subject: [PATCH 02/22] Greet, echo and exit --- src/main/java/Duke.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 180bc3fb1..1552d5e05 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,5 @@ +import java.util.Scanner; + public class Duke { public static void main(String[] args) { String logo = " ____ _ \n" @@ -8,6 +10,13 @@ public static void main(String[] args) { System.out.println("Hello from\n" + logo); System.out.println("Hello! I'm Duke"); System.out.println("What can I do for you?"); + Scanner input = new Scanner(System.in); + String text = input.next(); + while (!text.equals("bye")) + { + System.out.println(text); + text = input.next(); + } System.out.println("Bye! Hope to see you again soon!"); } } From c9dce36bf21af7087d98824acab244cc6862132c Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Wed, 1 Feb 2023 15:05:04 +0800 Subject: [PATCH 03/22] Add elements into a list --- src/main/java/Duke.java | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 1552d5e05..d72a06485 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,20 +2,35 @@ public class Duke { public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); System.out.println("Hello! I'm Duke"); System.out.println("What can I do for you?"); Scanner input = new Scanner(System.in); - String text = input.next(); + String text = input.nextLine(); + String[] list = new String[100]; + int j = 0; while (!text.equals("bye")) { - System.out.println(text); - text = input.next(); + if (text.equals("list")) + { + for (int i = 0; i < list.length; i += 1) + { + if (list[i] == null) + { + break; + } + else + { + System.out.println(i+1 + ". " + list[i]); + } + } + } + else + { + list[j] = text; + System.out.println("added:" + text); + j += 1; + } + text = input.nextLine(); } System.out.println("Bye! Hope to see you again soon!"); } From 7c733cacda975492e27c3ba57cd9bdded1ae655c Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Sat, 4 Feb 2023 03:41:03 +0800 Subject: [PATCH 04/22] added list to store items --- src/main/java/Duke.java | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index d72a06485..ccb0f1370 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,36 +2,34 @@ public class Duke { public static void main(String[] args) { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); System.out.println("Hello! I'm Duke"); System.out.println("What can I do for you?"); Scanner input = new Scanner(System.in); String text = input.nextLine(); String[] list = new String[100]; int j = 0; - while (!text.equals("bye")) - { - if (text.equals("list")) - { - for (int i = 0; i < list.length; i += 1) - { - if (list[i] == null) - { + while (!text.equals("bye")) { + if (text.equals("list")) { + for (int i = 0; i < list.length; i += 1) { + if (list[i] == null) { break; - } - else - { - System.out.println(i+1 + ". " + list[i]); + } else { + System.out.println(i + 1 + ". " + list[i]); } } - } - else - { + } else { list[j] = text; - System.out.println("added:" + text); + System.out.println("added: " + text); j += 1; } text = input.nextLine(); } System.out.println("Bye! Hope to see you again soon!"); } -} +} \ No newline at end of file From 1aa5dc008a98fb23f8dd095fb96353d42692b163 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Sat, 4 Feb 2023 03:51:33 +0800 Subject: [PATCH 05/22] added mark as done functionality --- src/main/java/Duke.java | 53 ++++++++++++++++++++++++----------------- src/main/java/Task.java | 26 ++++++++++++++++++++ 2 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ccb0f1370..7650c6a0e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,34 +1,43 @@ import java.util.Scanner; public class Duke { + private static void displayList(Task[] tasks, int numberOfTasks) { + for (int i = 0; i < numberOfTasks; i++) { + if (tasks[i] != null) { + System.out.println((i + 1) + ". " + "[" + tasks[i].getStatusIcon() + "] " + tasks[i].description); + } + } + } public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); System.out.println("Hello! I'm Duke"); System.out.println("What can I do for you?"); Scanner input = new Scanner(System.in); - String text = input.nextLine(); - String[] list = new String[100]; - int j = 0; - while (!text.equals("bye")) { - if (text.equals("list")) { - for (int i = 0; i < list.length; i += 1) { - if (list[i] == null) { - break; - } else { - System.out.println(i + 1 + ". " + list[i]); - } - } - } else { - list[j] = text; - System.out.println("added: " + text); - j += 1; + String text = input.nextLine(); // input the whole sentence into text + String[] splitText = text.split(" "); + Task[] tasks = new Task[100]; + int numberOfTasks = 1; + while (!splitText[0].equals("bye")) { + switch (splitText[0]) { + case "mark": + tasks[Integer.valueOf(splitText[1]) - 1].setDone(); + System.out.println("Nice! I've marked this task as done:"); + displayList(tasks, numberOfTasks); + break; + case "unmark": + tasks[Integer.valueOf(splitText[1]) - 1].setUndone(); + System.out.println("OK, I've marked this task as not done yet:"); + displayList(tasks, numberOfTasks); + break; + case "list": + displayList(tasks, numberOfTasks); + break; + default: + tasks[numberOfTasks - 1] = new Task(text); + numberOfTasks += 1; + break; } text = input.nextLine(); + splitText = text.split(" "); } System.out.println("Bye! Hope to see you again soon!"); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..3cbe8815c --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,26 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setDone() { + this.isDone = true; + } + + public void setUndone() { + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + +} \ No newline at end of file From cd54a1e7b98fdd3f2877b9a31df907125de3adf7 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Wed, 8 Feb 2023 16:45:14 +0800 Subject: [PATCH 06/22] added todo, event and deadline --- src/main/java/Deadline.java | 14 ++++++ src/main/java/Duke.java | 88 ++++++++++++++++++++++++++++--------- src/main/java/Event.java | 14 ++++++ src/main/java/Task.java | 5 +++ src/main/java/Todo.java | 11 +++++ 5 files changed, 111 insertions(+), 21 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..31302b9b8 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,14 @@ +public class Deadline extends Task { + + protected String end; + public Deadline(String description, String end) + { + super(description); + this.end = end; + } + + @Override + public void printTask() { + System.out.println("[D][" + getStatusIcon() + "] " + description + "(by:" + end + ")"); + } +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 7650c6a0e..049f54edc 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,43 +1,89 @@ import java.util.Scanner; public class Duke { - private static void displayList(Task[] tasks, int numberOfTasks) { - for (int i = 0; i < numberOfTasks; i++) { - if (tasks[i] != null) { - System.out.println((i + 1) + ". " + "[" + tasks[i].getStatusIcon() + "] " + tasks[i].description); - } + private static void displayList(Task[] tasks) { + for (int i = 0; i < Task.numberOfTasks; i += 1) { + System.out.print(i + 1 + ". "); + tasks[i].printTask(); } } + + private static void markTask(Task[] tasks, String task) { + tasks[Integer.parseInt(task) - 1].setDone(); + System.out.println("Nice! I've marked this task as done:"); + displayList(tasks); + } + + private static void unmarkTask(Task[] tasks, String task) { + tasks[Integer.parseInt(task) - 1].setUndone(); + System.out.println("OK, I've marked this task as not done yet:"); + displayList(tasks); + } + + private static void createTodo(Task[] tasks, String task) { + tasks[Task.numberOfTasks] = new Todo(task); + System.out.println("Got it. I've added this task:"); + tasks[Task.numberOfTasks - 1].printTask(); + System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); + } + + private static void createEvent(Task[] tasks, String task) { + String[] words = task.split("/from"); + String description = words[0]; + String[] words2 = words[1].split("/to"); + String start = words2[0]; + String end = words2[1]; + tasks[Task.numberOfTasks] = new Event(description, start, end); + System.out.println("Got it. I've added this task:"); + tasks[Task.numberOfTasks - 1].printTask(); + System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); + } + + private static void createDeadline(Task[] tasks, String task) { + String[] words = task.split("/by"); + String description = words[0]; + System.out.println(words[0]); + System.out.println(words[1]); + String end = words[1]; + tasks[Task.numberOfTasks] = new Deadline(description, end); + System.out.println("Got it. I've added this task:"); + tasks[Task.numberOfTasks - 1].printTask(); + System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); + } + + private static String[] getInput() { + Scanner input = new Scanner(System.in); + String text = input.nextLine(); // input the whole sentence into text + return text.split(" ", 2); + } public static void main(String[] args) { System.out.println("Hello! I'm Duke"); System.out.println("What can I do for you?"); - Scanner input = new Scanner(System.in); - String text = input.nextLine(); // input the whole sentence into text - String[] splitText = text.split(" "); + String[] splitText = getInput(); Task[] tasks = new Task[100]; - int numberOfTasks = 1; + Task.numberOfTasks = 0; while (!splitText[0].equals("bye")) { switch (splitText[0]) { case "mark": - tasks[Integer.valueOf(splitText[1]) - 1].setDone(); - System.out.println("Nice! I've marked this task as done:"); - displayList(tasks, numberOfTasks); + markTask(tasks, splitText[1]); break; case "unmark": - tasks[Integer.valueOf(splitText[1]) - 1].setUndone(); - System.out.println("OK, I've marked this task as not done yet:"); - displayList(tasks, numberOfTasks); + unmarkTask(tasks, splitText[1]); break; case "list": - displayList(tasks, numberOfTasks); + displayList(tasks); + break; + case "todo": + createTodo(tasks, splitText[1]); + break; + case "deadline": + createDeadline(tasks, splitText[1]); break; - default: - tasks[numberOfTasks - 1] = new Task(text); - numberOfTasks += 1; + case "event": + createEvent(tasks, splitText[1]); break; } - text = input.nextLine(); - splitText = text.split(" "); + splitText = getInput(); } System.out.println("Bye! Hope to see you again soon!"); } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..569eb6e67 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,14 @@ +public class Event extends Task { + protected String start; + protected String end; + public Event(String description, String start, String end) { + super(description); + this.start = start; + this.end = end; + } + + @Override + public void printTask() { + System.out.println("[E][" + getStatusIcon() + "] " + description + "(from:" + start + "to:" + end + ")"); + } +} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 3cbe8815c..bbe26279a 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,10 +1,12 @@ public class Task { protected String description; protected boolean isDone; + protected static int numberOfTasks; public Task(String description) { this.description = description; this.isDone = false; + numberOfTasks += 1; } public void setDescription(String description) { @@ -23,4 +25,7 @@ public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + public void printTask() { + System.out.println("task"); + } } \ No newline at end of file diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..c6cf073f4 --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,11 @@ +public class Todo extends Task { + + public Todo (String description) { + super(description); + } + + @Override + public void printTask() { + System.out.println("[T][" + getStatusIcon() + "] " + description); + } +} \ No newline at end of file From 33428dab68ce6657e77c04304cae66eb62313a37 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Wed, 15 Feb 2023 13:52:36 +0800 Subject: [PATCH 07/22] handle exceptions in commands --- src/main/java/Duke.java | 56 ++++++++++++++++--- src/main/java/EmptyDescriptionException.java | 14 +++++ .../java/TaskToMarkDoesNotExistException.java | 12 ++++ src/main/java/UnknownCommandException.java | 4 ++ 4 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 src/main/java/EmptyDescriptionException.java create mode 100644 src/main/java/TaskToMarkDoesNotExistException.java create mode 100644 src/main/java/UnknownCommandException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 049f54edc..4e85d543f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,6 +2,7 @@ public class Duke { private static void displayList(Task[] tasks) { + System.out.println("Here are the tasks in your list:"); for (int i = 0; i < Task.numberOfTasks; i += 1) { System.out.print(i + 1 + ". "); tasks[i].printTask(); @@ -56,35 +57,74 @@ private static String[] getInput() { String text = input.nextLine(); // input the whole sentence into text return text.split(" ", 2); } - public static void main(String[] args) { - System.out.println("Hello! I'm Duke"); - System.out.println("What can I do for you?"); + + private static void editList() throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { String[] splitText = getInput(); Task[] tasks = new Task[100]; Task.numberOfTasks = 0; while (!splitText[0].equals("bye")) { switch (splitText[0]) { case "mark": - markTask(tasks, splitText[1]); + try { + markTask(tasks, splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("marked"); + } catch (NullPointerException e) { + throw new TaskToMarkDoesNotExistException("mark"); + } break; case "unmark": - unmarkTask(tasks, splitText[1]); + try { + unmarkTask(tasks, splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("unmarked"); + } catch (NullPointerException e) { + throw new TaskToMarkDoesNotExistException("unmark"); + } break; case "list": displayList(tasks); break; case "todo": - createTodo(tasks, splitText[1]); + try { + createTodo(tasks, splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("todo"); + } break; case "deadline": - createDeadline(tasks, splitText[1]); + try { + createDeadline(tasks, splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("deadline"); + } break; case "event": - createEvent(tasks, splitText[1]); + try { + createEvent(tasks, splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("deadline"); + } break; + default: + throw new UnknownCommandException(); } splitText = getInput(); } System.out.println("Bye! Hope to see you again soon!"); } + + public static void main(String[] args) { + System.out.println("Hello! I'm Duke"); + System.out.println("What can I do for you?"); + try { + editList(); + } catch (UnknownCommandException e) { + System.out.println("OOPS!! I'm sorry but I don't know what that means :-("); + } catch (EmptyDescriptionException e) { + e.printErrorMessage(); + } catch (TaskToMarkDoesNotExistException e) { + e.printErrorMessage(); + } + } } \ No newline at end of file diff --git a/src/main/java/EmptyDescriptionException.java b/src/main/java/EmptyDescriptionException.java new file mode 100644 index 000000000..5461ce016 --- /dev/null +++ b/src/main/java/EmptyDescriptionException.java @@ -0,0 +1,14 @@ +public class EmptyDescriptionException extends Exception { + protected String typeOfTask; + + public EmptyDescriptionException (String typeOfTask) { + this.typeOfTask = typeOfTask; + } + public void printErrorMessage() { + if (typeOfTask.equals("marked") || typeOfTask.equals("unmarked")) { + System.out.println("OOPS!! Task to be " + typeOfTask + " was not specified!"); + } else { + System.out.println("OOPS!! The description of a " + typeOfTask + " cannot be empty!"); + } + } +} diff --git a/src/main/java/TaskToMarkDoesNotExistException.java b/src/main/java/TaskToMarkDoesNotExistException.java new file mode 100644 index 000000000..ee8bca3a0 --- /dev/null +++ b/src/main/java/TaskToMarkDoesNotExistException.java @@ -0,0 +1,12 @@ +public class TaskToMarkDoesNotExistException extends Exception { + protected String command; + + public TaskToMarkDoesNotExistException(String command) { + this.command = command; + } + + public void printErrorMessage() { + System.out.println("You can only " + command + " tasks that are currently in the list!"); + System.out.println("You only have " + Task.numberOfTasks + " tasks in your list."); + } +} diff --git a/src/main/java/UnknownCommandException.java b/src/main/java/UnknownCommandException.java new file mode 100644 index 000000000..8e3c9ee3b --- /dev/null +++ b/src/main/java/UnknownCommandException.java @@ -0,0 +1,4 @@ +public class UnknownCommandException extends Exception { + + +} From 983890b2a59285fba1a3ebe56d2e3ea928033655 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Wed, 15 Feb 2023 14:32:31 +0800 Subject: [PATCH 08/22] shift classes into packages --- src/main/java/Duke.java | 8 ++++++++ .../{ => duke/exceptions}/EmptyDescriptionException.java | 4 +++- .../exceptions}/TaskToMarkDoesNotExistException.java | 4 ++++ .../{ => duke/exceptions}/UnknownCommandException.java | 2 ++ src/main/java/{ => duke/tasks}/Deadline.java | 2 ++ src/main/java/{ => duke/tasks}/Event.java | 2 ++ src/main/java/{ => duke/tasks}/Task.java | 4 +++- src/main/java/{ => duke/tasks}/Todo.java | 2 ++ 8 files changed, 26 insertions(+), 2 deletions(-) rename src/main/java/{ => duke/exceptions}/EmptyDescriptionException.java (77%) rename src/main/java/{ => duke/exceptions}/TaskToMarkDoesNotExistException.java (89%) rename src/main/java/{ => duke/exceptions}/UnknownCommandException.java (70%) rename src/main/java/{ => duke/tasks}/Deadline.java (93%) rename src/main/java/{ => duke/tasks}/Event.java (95%) rename src/main/java/{ => duke/tasks}/Task.java (91%) rename src/main/java/{ => duke/tasks}/Todo.java (91%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4e85d543f..bd6a1f7c8 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,11 @@ +import duke.tasks.Task; +import duke.tasks.Deadline; +import duke.tasks.Event; +import duke.tasks.Todo; +import duke.exceptions.EmptyDescriptionException; +import duke.exceptions.TaskToMarkDoesNotExistException; +import duke.exceptions.UnknownCommandException; + import java.util.Scanner; public class Duke { diff --git a/src/main/java/EmptyDescriptionException.java b/src/main/java/duke/exceptions/EmptyDescriptionException.java similarity index 77% rename from src/main/java/EmptyDescriptionException.java rename to src/main/java/duke/exceptions/EmptyDescriptionException.java index 5461ce016..55c2cec27 100644 --- a/src/main/java/EmptyDescriptionException.java +++ b/src/main/java/duke/exceptions/EmptyDescriptionException.java @@ -1,3 +1,5 @@ +package duke.exceptions; + public class EmptyDescriptionException extends Exception { protected String typeOfTask; @@ -8,7 +10,7 @@ public void printErrorMessage() { if (typeOfTask.equals("marked") || typeOfTask.equals("unmarked")) { System.out.println("OOPS!! Task to be " + typeOfTask + " was not specified!"); } else { - System.out.println("OOPS!! The description of a " + typeOfTask + " cannot be empty!"); + System.out.println("OOPS!! The description of " + typeOfTask + " cannot be empty!"); } } } diff --git a/src/main/java/TaskToMarkDoesNotExistException.java b/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java similarity index 89% rename from src/main/java/TaskToMarkDoesNotExistException.java rename to src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java index ee8bca3a0..ea00b95cf 100644 --- a/src/main/java/TaskToMarkDoesNotExistException.java +++ b/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java @@ -1,3 +1,7 @@ +package duke.exceptions; + +import duke.tasks.Task; + public class TaskToMarkDoesNotExistException extends Exception { protected String command; diff --git a/src/main/java/UnknownCommandException.java b/src/main/java/duke/exceptions/UnknownCommandException.java similarity index 70% rename from src/main/java/UnknownCommandException.java rename to src/main/java/duke/exceptions/UnknownCommandException.java index 8e3c9ee3b..a60076d47 100644 --- a/src/main/java/UnknownCommandException.java +++ b/src/main/java/duke/exceptions/UnknownCommandException.java @@ -1,3 +1,5 @@ +package duke.exceptions; + public class UnknownCommandException extends Exception { diff --git a/src/main/java/Deadline.java b/src/main/java/duke/tasks/Deadline.java similarity index 93% rename from src/main/java/Deadline.java rename to src/main/java/duke/tasks/Deadline.java index 31302b9b8..c99e900ea 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -1,3 +1,5 @@ +package duke.tasks; + public class Deadline extends Task { protected String end; diff --git a/src/main/java/Event.java b/src/main/java/duke/tasks/Event.java similarity index 95% rename from src/main/java/Event.java rename to src/main/java/duke/tasks/Event.java index 569eb6e67..31a7e6966 100644 --- a/src/main/java/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -1,3 +1,5 @@ +package duke.tasks; + public class Event extends Task { protected String start; protected String end; diff --git a/src/main/java/Task.java b/src/main/java/duke/tasks/Task.java similarity index 91% rename from src/main/java/Task.java rename to src/main/java/duke/tasks/Task.java index bbe26279a..836959c07 100644 --- a/src/main/java/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -1,7 +1,9 @@ +package duke.tasks; + public class Task { protected String description; protected boolean isDone; - protected static int numberOfTasks; + public static int numberOfTasks; public Task(String description) { this.description = description; diff --git a/src/main/java/Todo.java b/src/main/java/duke/tasks/Todo.java similarity index 91% rename from src/main/java/Todo.java rename to src/main/java/duke/tasks/Todo.java index c6cf073f4..7e649e25d 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/duke/tasks/Todo.java @@ -1,3 +1,5 @@ +package duke.tasks; + public class Todo extends Task { public Todo (String description) { From d919bc17d70dd70d608b09ba003ead967d47a24f Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Sun, 26 Feb 2023 11:23:11 +0800 Subject: [PATCH 09/22] added delete task functionality --- src/main/java/Duke.java | 71 ++++++++++++------- src/main/java/META-INF/MANIFEST.MF | 3 + .../exceptions/EmptyDescriptionException.java | 2 +- .../TaskToMarkDoesNotExistException.java | 1 - src/main/java/duke/tasks/Task.java | 2 - 5 files changed, 49 insertions(+), 30 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index bd6a1f7c8..bb4123d42 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,58 +6,68 @@ import duke.exceptions.TaskToMarkDoesNotExistException; import duke.exceptions.UnknownCommandException; +import java.util.ArrayList; import java.util.Scanner; public class Duke { - private static void displayList(Task[] tasks) { + private static void displayList(ArrayList tasks) { System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < Task.numberOfTasks; i += 1) { + for (int i = 0; i < tasks.size(); i += 1) { System.out.print(i + 1 + ". "); - tasks[i].printTask(); + tasks.get(i).printTask(); } } - private static void markTask(Task[] tasks, String task) { - tasks[Integer.parseInt(task) - 1].setDone(); + private static void markTask(ArrayList tasks, String task) { + tasks.get(Integer.parseInt(task) - 1).setDone(); System.out.println("Nice! I've marked this task as done:"); displayList(tasks); } - private static void unmarkTask(Task[] tasks, String task) { - tasks[Integer.parseInt(task) - 1].setUndone(); + private static void unmarkTask(ArrayList tasks, String task) { + tasks.get(Integer.parseInt(task) - 1).setUndone(); System.out.println("OK, I've marked this task as not done yet:"); displayList(tasks); } - private static void createTodo(Task[] tasks, String task) { - tasks[Task.numberOfTasks] = new Todo(task); + private static void createTodo(ArrayList tasks, String task) { + Task todoToAdd = new Todo(task); + tasks.add(todoToAdd); System.out.println("Got it. I've added this task:"); - tasks[Task.numberOfTasks - 1].printTask(); - System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); + tasks.get(tasks.size() - 1).printTask(); + System.out.println("Now you have " + tasks.size() + " tasks in your list."); } - private static void createEvent(Task[] tasks, String task) { + private static void createEvent(ArrayList tasks, String task) { String[] words = task.split("/from"); String description = words[0]; String[] words2 = words[1].split("/to"); String start = words2[0]; String end = words2[1]; - tasks[Task.numberOfTasks] = new Event(description, start, end); + Task eventToAdd = new Event(description, start, end); + tasks.add(eventToAdd); System.out.println("Got it. I've added this task:"); - tasks[Task.numberOfTasks - 1].printTask(); - System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); + tasks.get(tasks.size() - 1).printTask(); + System.out.println("Now you have " + tasks.size() + " tasks in your list."); } - private static void createDeadline(Task[] tasks, String task) { + private static void createDeadline(ArrayList tasks, String task) { String[] words = task.split("/by"); String description = words[0]; - System.out.println(words[0]); - System.out.println(words[1]); String end = words[1]; - tasks[Task.numberOfTasks] = new Deadline(description, end); + Task deadlineToAdd = new Deadline(description, end); + tasks.add(deadlineToAdd); System.out.println("Got it. I've added this task:"); - tasks[Task.numberOfTasks - 1].printTask(); - System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); + tasks.get(tasks.size() - 1).printTask(); + System.out.println("Now you have " + tasks.size() + " tasks in your list."); + } + + private static void deleteTask(ArrayList tasks, String task) { + Task taskToDelete = tasks.get(Integer.parseInt(task) - 1); + tasks.remove(Integer.parseInt(task) - 1); + System.out.println("Noted. I've removed this task:"); + taskToDelete.printTask(); + System.out.println("Now you have " + tasks.size() + " tasks in your list."); } private static String[] getInput() { @@ -66,10 +76,8 @@ private static String[] getInput() { return text.split(" ", 2); } - private static void editList() throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { + private static void editList(ArrayList tasks) throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { String[] splitText = getInput(); - Task[] tasks = new Task[100]; - Task.numberOfTasks = 0; while (!splitText[0].equals("bye")) { switch (splitText[0]) { case "mark": @@ -111,7 +119,16 @@ private static void editList() throws UnknownCommandException, EmptyDescriptionE try { createEvent(tasks, splitText[1]); } catch (ArrayIndexOutOfBoundsException e) { - throw new EmptyDescriptionException("deadline"); + throw new EmptyDescriptionException("event"); + } + break; + case "delete": + try { + deleteTask(tasks, splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("deleted"); + } catch (IndexOutOfBoundsException e) { + throw new TaskToMarkDoesNotExistException("delete"); } break; default: @@ -125,14 +142,16 @@ private static void editList() throws UnknownCommandException, EmptyDescriptionE public static void main(String[] args) { System.out.println("Hello! I'm Duke"); System.out.println("What can I do for you?"); + ArrayList tasks = new ArrayList<>(); try { - editList(); + editList(tasks); } catch (UnknownCommandException e) { System.out.println("OOPS!! I'm sorry but I don't know what that means :-("); } catch (EmptyDescriptionException e) { e.printErrorMessage(); } catch (TaskToMarkDoesNotExistException e) { e.printErrorMessage(); + System.out.println("You only have " + tasks.size() + " tasks in your list."); } } } \ No newline at end of file diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..9f37e4e0a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + diff --git a/src/main/java/duke/exceptions/EmptyDescriptionException.java b/src/main/java/duke/exceptions/EmptyDescriptionException.java index 55c2cec27..8fe28f92b 100644 --- a/src/main/java/duke/exceptions/EmptyDescriptionException.java +++ b/src/main/java/duke/exceptions/EmptyDescriptionException.java @@ -7,7 +7,7 @@ public EmptyDescriptionException (String typeOfTask) { this.typeOfTask = typeOfTask; } public void printErrorMessage() { - if (typeOfTask.equals("marked") || typeOfTask.equals("unmarked")) { + if (typeOfTask.equals("marked") || typeOfTask.equals("unmarked") || typeOfTask.equals("deleted")) { System.out.println("OOPS!! Task to be " + typeOfTask + " was not specified!"); } else { System.out.println("OOPS!! The description of " + typeOfTask + " cannot be empty!"); diff --git a/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java b/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java index ea00b95cf..3bf9566e2 100644 --- a/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java +++ b/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java @@ -11,6 +11,5 @@ public TaskToMarkDoesNotExistException(String command) { public void printErrorMessage() { System.out.println("You can only " + command + " tasks that are currently in the list!"); - System.out.println("You only have " + Task.numberOfTasks + " tasks in your list."); } } diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java index 836959c07..7dbfa9177 100644 --- a/src/main/java/duke/tasks/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -3,12 +3,10 @@ public class Task { protected String description; protected boolean isDone; - public static int numberOfTasks; public Task(String description) { this.description = description; this.isDone = false; - numberOfTasks += 1; } public void setDescription(String description) { From f0554aab2232ffb05de67a7122d10ce3bc93d043 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Mon, 27 Feb 2023 11:59:34 +0800 Subject: [PATCH 10/22] store the data in txt file --- data/duke.txt | 0 src/main/java/Duke.java | 11 +++ src/main/java/Storage.java | 111 +++++++++++++++++++++++++ src/main/java/duke/tasks/Deadline.java | 1 + src/main/java/duke/tasks/Event.java | 1 + src/main/java/duke/tasks/Task.java | 18 ++++ src/main/java/duke/tasks/Todo.java | 2 +- 7 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 data/duke.txt create mode 100644 src/main/java/Storage.java diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index bb4123d42..98778e525 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,6 +6,7 @@ import duke.exceptions.TaskToMarkDoesNotExistException; import duke.exceptions.UnknownCommandException; +import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; @@ -136,6 +137,11 @@ private static void editList(ArrayList tasks) throws UnknownCommandExcepti } splitText = getInput(); } + try { + Storage.updateFile(tasks); + } catch (IOException e){ + System.out.println("Error updating data in file"); + } System.out.println("Bye! Hope to see you again soon!"); } @@ -143,6 +149,11 @@ public static void main(String[] args) { System.out.println("Hello! I'm Duke"); System.out.println("What can I do for you?"); ArrayList tasks = new ArrayList<>(); + try { + tasks = Storage.getData(); + } catch (IOException e) { + System.out.println("Error obtaining data from file"); + } try { editList(tasks); } catch (UnknownCommandException e) { diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 000000000..6388f4983 --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,111 @@ +import duke.tasks.Deadline; +import duke.tasks.Event; +import duke.tasks.Task; +import duke.tasks.Todo; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + private static final String filepath = "./data/duke.txt"; + + public static Task addTask(String description) { + String[] taskDescription; + taskDescription = description.split("|", 3); + Task taskToAdd; + switch (taskDescription[0]) { + case "T": + taskToAdd = new Todo(taskDescription[2]); + if (taskDescription[1].equals("1")) { + taskToAdd.setDone(); + } else { + taskToAdd.setUndone(); + } + break; + case "D": + String[] deadlineDescription = taskDescription[2].split("|", 2); + taskToAdd = new Deadline(deadlineDescription[0], deadlineDescription[1]); + if (taskDescription[1].equals("1")) { + taskToAdd.setDone(); + } else { + taskToAdd.setUndone(); + } + break; + case "E": + String[] eventDescription = taskDescription[2].split("|", 2); + String dates[] = eventDescription[1].split("-"); + String start = dates[0]; + String end = dates[1]; + taskToAdd = new Event(eventDescription[0], start, end); + if (taskDescription[1].equals("1")) { + taskToAdd.setDone(); + } else { + taskToAdd.setUndone(); + } + break; + default: + taskToAdd = null; + break; + } + return taskToAdd; + } + public static ArrayList getData() throws IOException { + ArrayList currentList = new ArrayList<>(); + File f = new File(filepath); + if (!f.getParentFile().exists()) { + f.getParentFile().mkdir(); + } + if (f.exists()) { + Scanner s = new Scanner(f); + while (s.hasNext()) { + String description = s.nextLine(); + currentList.add(addTask(description)); + } + } + return currentList; + } + + public static String formatTask(ArrayList list) { + String text = null; + for (Task i : list) { + if (i.getType().equals("todo")) { + text = "T" + " | "; + if (i.getStatusIcon().equals("X")) { + text += "1"; + } else { + text += "0"; + } + text += " | " + i.getDescription(); + } + if (i.getType().equals("deadline")) { + text = "D" + " | "; + if (i.getStatusIcon().equals("X")) { + text += "1"; + } else { + text += "0"; + } + text += " | " + i.getDescription() + " | " + i.getEnd(); + } + if (i.getType().equals("event")) { + text = "E" + " | "; + if (i.getStatusIcon().equals("X")) { + text += "1"; + } else { + text += "0"; + } + text += " | " + i.getDescription() + " | " + i.getStart() + "-" + i.getEnd(); + } + } + return text; + } + + public static void updateFile(ArrayList tasks) throws IOException { + FileWriter fw = new FileWriter(filepath); + String textToAdd = formatTask(tasks); + fw.write(textToAdd); + fw.close(); + } +} diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java index c99e900ea..f710ec0c8 100644 --- a/src/main/java/duke/tasks/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -2,6 +2,7 @@ public class Deadline extends Task { + protected String type = "deadline"; protected String end; public Deadline(String description, String end) { diff --git a/src/main/java/duke/tasks/Event.java b/src/main/java/duke/tasks/Event.java index 31a7e6966..6a6cade24 100644 --- a/src/main/java/duke/tasks/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -3,6 +3,7 @@ public class Event extends Task { protected String start; protected String end; + protected String type = "event"; public Event(String description, String start, String end) { super(description); this.start = start; diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java index 7dbfa9177..d6e38ae57 100644 --- a/src/main/java/duke/tasks/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -1,8 +1,26 @@ package duke.tasks; public class Task { + protected String description; protected boolean isDone; + protected String type; + protected String start; + protected String end; + + public String getDescription() { + return description; + } + public String getType() { + return type; + } + public String getStart() { + return start; + } + + public String getEnd() { + return end; + } public Task(String description) { this.description = description; diff --git a/src/main/java/duke/tasks/Todo.java b/src/main/java/duke/tasks/Todo.java index 7e649e25d..c774a2bc4 100644 --- a/src/main/java/duke/tasks/Todo.java +++ b/src/main/java/duke/tasks/Todo.java @@ -1,7 +1,7 @@ package duke.tasks; public class Todo extends Task { - + protected String type = "todo"; public Todo (String description) { super(description); } From 1792a0bdc93e2be545d9adbf68404594ba60d49a Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Mon, 27 Feb 2023 12:24:18 +0800 Subject: [PATCH 11/22] cleaned up code for storage --- data/duke.txt | 0 src/main/java/Duke.java | 82 ++++++++++----- src/main/java/Storage.java | 99 +++++++++++++++++++ .../TaskToMarkDoesNotExistException.java | 1 - src/main/java/duke/tasks/Deadline.java | 3 +- src/main/java/duke/tasks/Event.java | 7 +- src/main/java/duke/tasks/Task.java | 22 ++++- src/main/java/duke/tasks/Todo.java | 4 +- 8 files changed, 182 insertions(+), 36 deletions(-) create mode 100644 data/duke.txt create mode 100644 src/main/java/Storage.java diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index bd6a1f7c8..98778e525 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,58 +6,69 @@ import duke.exceptions.TaskToMarkDoesNotExistException; import duke.exceptions.UnknownCommandException; +import java.io.IOException; +import java.util.ArrayList; import java.util.Scanner; public class Duke { - private static void displayList(Task[] tasks) { + private static void displayList(ArrayList tasks) { System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < Task.numberOfTasks; i += 1) { + for (int i = 0; i < tasks.size(); i += 1) { System.out.print(i + 1 + ". "); - tasks[i].printTask(); + tasks.get(i).printTask(); } } - private static void markTask(Task[] tasks, String task) { - tasks[Integer.parseInt(task) - 1].setDone(); + private static void markTask(ArrayList tasks, String task) { + tasks.get(Integer.parseInt(task) - 1).setDone(); System.out.println("Nice! I've marked this task as done:"); displayList(tasks); } - private static void unmarkTask(Task[] tasks, String task) { - tasks[Integer.parseInt(task) - 1].setUndone(); + private static void unmarkTask(ArrayList tasks, String task) { + tasks.get(Integer.parseInt(task) - 1).setUndone(); System.out.println("OK, I've marked this task as not done yet:"); displayList(tasks); } - private static void createTodo(Task[] tasks, String task) { - tasks[Task.numberOfTasks] = new Todo(task); + private static void createTodo(ArrayList tasks, String task) { + Task todoToAdd = new Todo(task); + tasks.add(todoToAdd); System.out.println("Got it. I've added this task:"); - tasks[Task.numberOfTasks - 1].printTask(); - System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); + tasks.get(tasks.size() - 1).printTask(); + System.out.println("Now you have " + tasks.size() + " tasks in your list."); } - private static void createEvent(Task[] tasks, String task) { + private static void createEvent(ArrayList tasks, String task) { String[] words = task.split("/from"); String description = words[0]; String[] words2 = words[1].split("/to"); String start = words2[0]; String end = words2[1]; - tasks[Task.numberOfTasks] = new Event(description, start, end); + Task eventToAdd = new Event(description, start, end); + tasks.add(eventToAdd); System.out.println("Got it. I've added this task:"); - tasks[Task.numberOfTasks - 1].printTask(); - System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); + tasks.get(tasks.size() - 1).printTask(); + System.out.println("Now you have " + tasks.size() + " tasks in your list."); } - private static void createDeadline(Task[] tasks, String task) { + private static void createDeadline(ArrayList tasks, String task) { String[] words = task.split("/by"); String description = words[0]; - System.out.println(words[0]); - System.out.println(words[1]); String end = words[1]; - tasks[Task.numberOfTasks] = new Deadline(description, end); + Task deadlineToAdd = new Deadline(description, end); + tasks.add(deadlineToAdd); System.out.println("Got it. I've added this task:"); - tasks[Task.numberOfTasks - 1].printTask(); - System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); + tasks.get(tasks.size() - 1).printTask(); + System.out.println("Now you have " + tasks.size() + " tasks in your list."); + } + + private static void deleteTask(ArrayList tasks, String task) { + Task taskToDelete = tasks.get(Integer.parseInt(task) - 1); + tasks.remove(Integer.parseInt(task) - 1); + System.out.println("Noted. I've removed this task:"); + taskToDelete.printTask(); + System.out.println("Now you have " + tasks.size() + " tasks in your list."); } private static String[] getInput() { @@ -66,10 +77,8 @@ private static String[] getInput() { return text.split(" ", 2); } - private static void editList() throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { + private static void editList(ArrayList tasks) throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { String[] splitText = getInput(); - Task[] tasks = new Task[100]; - Task.numberOfTasks = 0; while (!splitText[0].equals("bye")) { switch (splitText[0]) { case "mark": @@ -111,7 +120,16 @@ private static void editList() throws UnknownCommandException, EmptyDescriptionE try { createEvent(tasks, splitText[1]); } catch (ArrayIndexOutOfBoundsException e) { - throw new EmptyDescriptionException("deadline"); + throw new EmptyDescriptionException("event"); + } + break; + case "delete": + try { + deleteTask(tasks, splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("deleted"); + } catch (IndexOutOfBoundsException e) { + throw new TaskToMarkDoesNotExistException("delete"); } break; default: @@ -119,20 +137,32 @@ private static void editList() throws UnknownCommandException, EmptyDescriptionE } splitText = getInput(); } + try { + Storage.updateFile(tasks); + } catch (IOException e){ + System.out.println("Error updating data in file"); + } System.out.println("Bye! Hope to see you again soon!"); } public static void main(String[] args) { System.out.println("Hello! I'm Duke"); System.out.println("What can I do for you?"); + ArrayList tasks = new ArrayList<>(); + try { + tasks = Storage.getData(); + } catch (IOException e) { + System.out.println("Error obtaining data from file"); + } try { - editList(); + editList(tasks); } catch (UnknownCommandException e) { System.out.println("OOPS!! I'm sorry but I don't know what that means :-("); } catch (EmptyDescriptionException e) { e.printErrorMessage(); } catch (TaskToMarkDoesNotExistException e) { e.printErrorMessage(); + System.out.println("You only have " + tasks.size() + " tasks in your list."); } } } \ No newline at end of file diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 000000000..f4be8321e --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,99 @@ +import duke.tasks.Deadline; +import duke.tasks.Event; +import duke.tasks.Task; +import duke.tasks.Todo; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + private static final String filepath = "./data/duke.txt"; + + public static Task addTask(String description) { + String[] taskDescription; + taskDescription = description.split("|", 3); + Task taskToAdd; + switch (taskDescription[0]) { + case "T": + taskToAdd = new Todo(taskDescription[2]); + if (taskDescription[1].equals("1")) { + taskToAdd.setDone(); + } else { + taskToAdd.setUndone(); + } + break; + case "D": + String[] deadlineDescription = taskDescription[2].split("|", 2); + taskToAdd = new Deadline(deadlineDescription[0], deadlineDescription[1]); + if (taskDescription[1].equals("1")) { + taskToAdd.setDone(); + } else { + taskToAdd.setUndone(); + } + break; + case "E": + String[] eventDescription = taskDescription[2].split("|", 2); + String dates[] = eventDescription[1].split("-"); + String start = dates[0]; + String end = dates[1]; + taskToAdd = new Event(eventDescription[0], start, end); + if (taskDescription[1].equals("1")) { + taskToAdd.setDone(); + } else { + taskToAdd.setUndone(); + } + break; + default: + taskToAdd = null; + break; + } + return taskToAdd; + } + public static ArrayList getData() throws IOException { + ArrayList currentList = new ArrayList<>(); + File f = new File(filepath); + if (!f.getParentFile().exists()) { + f.getParentFile().mkdir(); + } + if (f.exists()) { + Scanner s = new Scanner(f); + while (s.hasNext()) { + String description = s.nextLine(); + currentList.add(addTask(description)); + } + } + return currentList; + } + + public static String getNumberIcon(Task task) { + if (task.getStatusIcon().equals("X")) { + return "1"; + } + return "0"; + } + public static String formatTask(ArrayList tasks) throws IOException { + String text = null; + for (Task i : tasks) { + if (i.getType().equals("todo")) { + text = "T" + " | " + getNumberIcon(i) + " | " + i.getDescription(); + } + if (i.getType().equals("deadline")) { + text = "D" + " | " + getNumberIcon(i) + " | " + i.getDescription() + " | " + i.getEnd(); + } + if (i.getType().equals("event")) { + text = "E" + " | " + getNumberIcon(i) + " | " + i.getDescription() + " | " + i.getStart() + "-" + i.getEnd(); + } + } + return text; + } + + public static void updateFile(ArrayList tasks) throws IOException { + FileWriter fw = new FileWriter(filepath); + String textToAdd = formatTask(tasks); + fw.write(textToAdd); + fw.close(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java b/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java index ea00b95cf..3bf9566e2 100644 --- a/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java +++ b/src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java @@ -11,6 +11,5 @@ public TaskToMarkDoesNotExistException(String command) { public void printErrorMessage() { System.out.println("You can only " + command + " tasks that are currently in the list!"); - System.out.println("You only have " + Task.numberOfTasks + " tasks in your list."); } } diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java index c99e900ea..8b1602ca9 100644 --- a/src/main/java/duke/tasks/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -2,6 +2,7 @@ public class Deadline extends Task { + protected String type = "deadline"; protected String end; public Deadline(String description, String end) { @@ -13,4 +14,4 @@ public Deadline(String description, String end) public void printTask() { System.out.println("[D][" + getStatusIcon() + "] " + description + "(by:" + end + ")"); } -} \ No newline at end of file +} diff --git a/src/main/java/duke/tasks/Event.java b/src/main/java/duke/tasks/Event.java index 31a7e6966..46e07ee61 100644 --- a/src/main/java/duke/tasks/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -3,10 +3,11 @@ public class Event extends Task { protected String start; protected String end; + protected String type = "event"; public Event(String description, String start, String end) { - super(description); - this.start = start; - this.end = end; + super(description); + this.start = start; + this.end = end; } @Override diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java index 836959c07..2d0b1f7e3 100644 --- a/src/main/java/duke/tasks/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -1,14 +1,30 @@ package duke.tasks; public class Task { + protected String description; protected boolean isDone; - public static int numberOfTasks; + protected String type; + protected String start; + protected String end; + + public String getDescription() { + return description; + } + public String getType() { + return type; + } + public String getStart() { + return start; + } + + public String getEnd() { + return end; + } public Task(String description) { this.description = description; this.isDone = false; - numberOfTasks += 1; } public void setDescription(String description) { @@ -30,4 +46,4 @@ public String getStatusIcon() { public void printTask() { System.out.println("task"); } -} \ No newline at end of file +} diff --git a/src/main/java/duke/tasks/Todo.java b/src/main/java/duke/tasks/Todo.java index 7e649e25d..72d0583ef 100644 --- a/src/main/java/duke/tasks/Todo.java +++ b/src/main/java/duke/tasks/Todo.java @@ -1,7 +1,7 @@ package duke.tasks; public class Todo extends Task { - + protected String type = "todo"; public Todo (String description) { super(description); } @@ -10,4 +10,4 @@ public Todo (String description) { public void printTask() { System.out.println("[T][" + getStatusIcon() + "] " + description); } -} \ No newline at end of file +} From dde6fafe610cc476bdd15278e451bcb27f2983e5 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Mon, 27 Feb 2023 13:13:46 +0800 Subject: [PATCH 12/22] merge 'branch-Level-7' --- README.md | 4 ++-- src/main/java/META-INF/MANIFEST.MF | 3 --- src/main/java/{ => duke}/Duke.java | 4 +++- src/main/java/{ => duke}/Storage.java | 2 ++ text-ui-test/runtest.bat | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) delete mode 100644 src/main/java/META-INF/MANIFEST.MF rename src/main/java/{ => duke}/Duke.java (98%) rename src/main/java/{ => duke}/Storage.java (99%) diff --git a/README.md b/README.md index 8715d4d91..0f2208ab6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# duke.Duke project template This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. @@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version. 1. If there are any further prompts, accept the defaults. 1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: +3. After that, locate the `src/main/java/duke.Duke.java` file, right-click it, and choose `Run duke.Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from ____ _ diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF deleted file mode 100644 index 9f37e4e0a..000000000 --- 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/Duke.java b/src/main/java/duke/Duke.java similarity index 98% rename from src/main/java/Duke.java rename to src/main/java/duke/Duke.java index 98778e525..e0d3bab93 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,3 +1,5 @@ +package duke; + import duke.tasks.Task; import duke.tasks.Deadline; import duke.tasks.Event; @@ -146,7 +148,7 @@ private static void editList(ArrayList tasks) throws UnknownCommandExcepti } public static void main(String[] args) { - System.out.println("Hello! I'm Duke"); + System.out.println("Hello! I'm duke.Duke"); System.out.println("What can I do for you?"); ArrayList tasks = new ArrayList<>(); try { diff --git a/src/main/java/Storage.java b/src/main/java/duke/Storage.java similarity index 99% rename from src/main/java/Storage.java rename to src/main/java/duke/Storage.java index f1a5b9782..6c9c55958 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/duke/Storage.java @@ -1,3 +1,5 @@ +package duke; + import duke.tasks.Deadline; import duke.tasks.Event; import duke.tasks.Task; diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..62752b881 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Duke < input.txt > ACTUAL.TXT +java -classpath ..\bin duke.Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From a3e58387bf27f5edb3ea44bf4b8db39d8216d533 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Tue, 28 Feb 2023 09:21:17 +0800 Subject: [PATCH 13/22] added Ui class --- src/main/java/duke/Duke.java | 15 ++++----------- src/main/java/duke/Ui.java | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 src/main/java/duke/Ui.java diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index e0d3bab93..56209267d 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -73,14 +73,8 @@ private static void deleteTask(ArrayList tasks, String task) { System.out.println("Now you have " + tasks.size() + " tasks in your list."); } - private static String[] getInput() { - Scanner input = new Scanner(System.in); - String text = input.nextLine(); // input the whole sentence into text - return text.split(" ", 2); - } - private static void editList(ArrayList tasks) throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { - String[] splitText = getInput(); + String[] splitText = Ui.getInput(); while (!splitText[0].equals("bye")) { switch (splitText[0]) { case "mark": @@ -137,19 +131,18 @@ private static void editList(ArrayList tasks) throws UnknownCommandExcepti default: throw new UnknownCommandException(); } - splitText = getInput(); + splitText = Ui.getInput(); } try { Storage.updateFile(tasks); } catch (IOException e){ System.out.println("Error updating data in file"); } - System.out.println("Bye! Hope to see you again soon!"); + Ui.printBye(); } public static void main(String[] args) { - System.out.println("Hello! I'm duke.Duke"); - System.out.println("What can I do for you?"); + Ui.printGreet(); ArrayList tasks = new ArrayList<>(); try { tasks = Storage.getData(); diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java new file mode 100644 index 000000000..addd0f0c4 --- /dev/null +++ b/src/main/java/duke/Ui.java @@ -0,0 +1,25 @@ +package duke; + +import java.util.Scanner; + +public class Ui { + + public static void printGreet() { + System.out.println("Hello! I'm duke."); + System.out.println("What can I do for you?"); + } + + public static void printBye() { + System.out.println("Bye! Hope to see you again soon!"); + } + + public static String[] getInput() { + Scanner input = new Scanner(System.in); + String text = input.nextLine(); // input the whole sentence into text + return text.split(" ", 2); + } + + public static void printError(String error) { + System.out.println(error); + } +} From 8bb6a12086d0d6bbdd2fad2d30a81b94a52c0213 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Tue, 28 Feb 2023 10:23:03 +0800 Subject: [PATCH 14/22] added Parser and TaskList classes --- src/main/java/duke/Duke.java | 139 +--------------- src/main/java/duke/Parser.java | 11 ++ src/main/java/duke/TaskList.java | 150 ++++++++++++++++++ src/main/java/duke/Ui.java | 10 +- .../exceptions/UnknownCommandException.java | 5 +- 5 files changed, 174 insertions(+), 141 deletions(-) create mode 100644 src/main/java/duke/Parser.java create mode 100644 src/main/java/duke/TaskList.java diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 56209267d..5694af814 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,163 +1,32 @@ package duke; import duke.tasks.Task; -import duke.tasks.Deadline; -import duke.tasks.Event; -import duke.tasks.Todo; import duke.exceptions.EmptyDescriptionException; import duke.exceptions.TaskToMarkDoesNotExistException; import duke.exceptions.UnknownCommandException; import java.io.IOException; import java.util.ArrayList; -import java.util.Scanner; public class Duke { - private static void displayList(ArrayList tasks) { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < tasks.size(); i += 1) { - System.out.print(i + 1 + ". "); - tasks.get(i).printTask(); - } - } - - private static void markTask(ArrayList tasks, String task) { - tasks.get(Integer.parseInt(task) - 1).setDone(); - System.out.println("Nice! I've marked this task as done:"); - displayList(tasks); - } - - private static void unmarkTask(ArrayList tasks, String task) { - tasks.get(Integer.parseInt(task) - 1).setUndone(); - System.out.println("OK, I've marked this task as not done yet:"); - displayList(tasks); - } - - private static void createTodo(ArrayList tasks, String task) { - Task todoToAdd = new Todo(task); - tasks.add(todoToAdd); - System.out.println("Got it. I've added this task:"); - tasks.get(tasks.size() - 1).printTask(); - System.out.println("Now you have " + tasks.size() + " tasks in your list."); - } - - private static void createEvent(ArrayList tasks, String task) { - String[] words = task.split("/from"); - String description = words[0]; - String[] words2 = words[1].split("/to"); - String start = words2[0]; - String end = words2[1]; - Task eventToAdd = new Event(description, start, end); - tasks.add(eventToAdd); - System.out.println("Got it. I've added this task:"); - tasks.get(tasks.size() - 1).printTask(); - System.out.println("Now you have " + tasks.size() + " tasks in your list."); - } - - private static void createDeadline(ArrayList tasks, String task) { - String[] words = task.split("/by"); - String description = words[0]; - String end = words[1]; - Task deadlineToAdd = new Deadline(description, end); - tasks.add(deadlineToAdd); - System.out.println("Got it. I've added this task:"); - tasks.get(tasks.size() - 1).printTask(); - System.out.println("Now you have " + tasks.size() + " tasks in your list."); - } - - private static void deleteTask(ArrayList tasks, String task) { - Task taskToDelete = tasks.get(Integer.parseInt(task) - 1); - tasks.remove(Integer.parseInt(task) - 1); - System.out.println("Noted. I've removed this task:"); - taskToDelete.printTask(); - System.out.println("Now you have " + tasks.size() + " tasks in your list."); - } - - private static void editList(ArrayList tasks) throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { - String[] splitText = Ui.getInput(); - while (!splitText[0].equals("bye")) { - switch (splitText[0]) { - case "mark": - try { - markTask(tasks, splitText[1]); - } catch (ArrayIndexOutOfBoundsException e) { - throw new EmptyDescriptionException("marked"); - } catch (NullPointerException e) { - throw new TaskToMarkDoesNotExistException("mark"); - } - break; - case "unmark": - try { - unmarkTask(tasks, splitText[1]); - } catch (ArrayIndexOutOfBoundsException e) { - throw new EmptyDescriptionException("unmarked"); - } catch (NullPointerException e) { - throw new TaskToMarkDoesNotExistException("unmark"); - } - break; - case "list": - displayList(tasks); - break; - case "todo": - try { - createTodo(tasks, splitText[1]); - } catch (ArrayIndexOutOfBoundsException e) { - throw new EmptyDescriptionException("todo"); - } - break; - case "deadline": - try { - createDeadline(tasks, splitText[1]); - } catch (ArrayIndexOutOfBoundsException e) { - throw new EmptyDescriptionException("deadline"); - } - break; - case "event": - try { - createEvent(tasks, splitText[1]); - } catch (ArrayIndexOutOfBoundsException e) { - throw new EmptyDescriptionException("event"); - } - break; - case "delete": - try { - deleteTask(tasks, splitText[1]); - } catch (ArrayIndexOutOfBoundsException e) { - throw new EmptyDescriptionException("deleted"); - } catch (IndexOutOfBoundsException e) { - throw new TaskToMarkDoesNotExistException("delete"); - } - break; - default: - throw new UnknownCommandException(); - } - splitText = Ui.getInput(); - } - try { - Storage.updateFile(tasks); - } catch (IOException e){ - System.out.println("Error updating data in file"); - } - Ui.printBye(); - } public static void main(String[] args) { Ui.printGreet(); - ArrayList tasks = new ArrayList<>(); + ArrayList tasks = TaskList.tasks; try { tasks = Storage.getData(); } catch (IOException e) { System.out.println("Error obtaining data from file"); } try { - editList(tasks); + TaskList.editList(); } catch (UnknownCommandException e) { - System.out.println("OOPS!! I'm sorry but I don't know what that means :-("); + e.printErrorMessage(); } catch (EmptyDescriptionException e) { e.printErrorMessage(); } catch (TaskToMarkDoesNotExistException e) { e.printErrorMessage(); - System.out.println("You only have " + tasks.size() + " tasks in your list."); + Ui.printNumberOfTasks(tasks); } } } \ No newline at end of file diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 000000000..e99ee337e --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,11 @@ +package duke; + +import java.util.Scanner; + +public class Parser { + + public static String[] parse(Scanner input) { + String text = input.nextLine(); // input the whole sentence into text + return text.split(" ", 2); + } +} diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java new file mode 100644 index 000000000..5f0a90077 --- /dev/null +++ b/src/main/java/duke/TaskList.java @@ -0,0 +1,150 @@ +package duke; + +import duke.exceptions.EmptyDescriptionException; +import duke.exceptions.TaskToMarkDoesNotExistException; +import duke.exceptions.UnknownCommandException; +import duke.tasks.Deadline; +import duke.tasks.Event; +import duke.tasks.Task; +import duke.tasks.Todo; + +import java.io.IOException; +import java.util.ArrayList; + +public class TaskList { + protected static ArrayList tasks = new ArrayList<>(); + private static final String ADDED_COMMAND = "Got it. I've added this task:"; + private static final String REMOVED_COMMAND = "Noted. I've removed this task:"; + + public static void displayList() { + if (tasks.size() > 0) { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i += 1) { + System.out.print(i + 1 + ". "); + tasks.get(i).printTask(); + } + } else { + System.out.println("You currently don't have any tasks in your list"); + } + } + + public static void markTask(String task) { + tasks.get(Integer.parseInt(task) - 1).setDone(); + System.out.println("Nice! I've marked this task as done:"); + TaskList.displayList(); + } + + public static void unmarkTask(String task) { + tasks.get(Integer.parseInt(task) - 1).setUndone(); + System.out.println("OK, I've marked this task as not done yet:"); + TaskList.displayList(); + } + + public static void createTodo(String task) { + Task todoToAdd = new Todo(task); + tasks.add(todoToAdd); + System.out.println(ADDED_COMMAND); + tasks.get(tasks.size() - 1).printTask(); + Ui.printNumberOfTasks(tasks); + } + + public static void createEvent(String task) { + String[] input = task.split("/from"); + String description = input[0]; + String[] period = input[1].split("/to"); + String start = period[0]; + String end = period[1]; + Task eventToAdd = new Event(description, start, end); + tasks.add(eventToAdd); + System.out.println(ADDED_COMMAND); + tasks.get(tasks.size() - 1).printTask(); + Ui.printNumberOfTasks(tasks); + } + + public static void createDeadline(String task) { + String[] words = task.split("/by"); + String description = words[0]; + String end = words[1]; + Task deadlineToAdd = new Deadline(description, end); + tasks.add(deadlineToAdd); + System.out.println(ADDED_COMMAND); + tasks.get(tasks.size() - 1).printTask(); + Ui.printNumberOfTasks(tasks); + } + + public static void deleteTask(String task) { + Task taskToDelete = tasks.get(Integer.parseInt(task) - 1); + tasks.remove(Integer.parseInt(task) - 1); + System.out.println(REMOVED_COMMAND); + taskToDelete.printTask(); + Ui.printNumberOfTasks(tasks); + } + + public static void editList() throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { + String[] splitText = Ui.getInput(); + while (!splitText[0].equals("bye")) { + switch (splitText[0]) { + case "mark": + try { + TaskList.markTask(splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("marked"); + } catch (NullPointerException e) { + throw new TaskToMarkDoesNotExistException("mark"); + } + break; + case "unmark": + try { + TaskList.unmarkTask(splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("unmarked"); + } catch (NullPointerException e) { + throw new TaskToMarkDoesNotExistException("unmark"); + } + break; + case "list": + TaskList.displayList(); + break; + case "todo": + try { + TaskList.createTodo(splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("todo"); + } + break; + case "deadline": + try { + TaskList.createDeadline(splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("deadline"); + } + break; + case "event": + try { + TaskList.createEvent(splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("event"); + } + break; + case "delete": + try { + TaskList.deleteTask(splitText[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new EmptyDescriptionException("deleted"); + } catch (IndexOutOfBoundsException e) { + throw new TaskToMarkDoesNotExistException("delete"); + } + break; + default: + throw new UnknownCommandException(); + } + splitText = Ui.getInput(); + } + try { + Storage.updateFile(tasks); + } catch (IOException e){ + System.out.println("Error updating data in file"); + } + Ui.printBye(); + } +} diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index addd0f0c4..57ab84f98 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -1,5 +1,8 @@ package duke; +import duke.tasks.Task; + +import java.util.ArrayList; import java.util.Scanner; public class Ui { @@ -15,11 +18,10 @@ public static void printBye() { public static String[] getInput() { Scanner input = new Scanner(System.in); - String text = input.nextLine(); // input the whole sentence into text - return text.split(" ", 2); + return Parser.parse(input); } - public static void printError(String error) { - System.out.println(error); + public static void printNumberOfTasks(ArrayList tasks) { + System.out.println("You currently have " + tasks.size() + " tasks in your list."); } } diff --git a/src/main/java/duke/exceptions/UnknownCommandException.java b/src/main/java/duke/exceptions/UnknownCommandException.java index a60076d47..2eb3ab5ab 100644 --- a/src/main/java/duke/exceptions/UnknownCommandException.java +++ b/src/main/java/duke/exceptions/UnknownCommandException.java @@ -1,6 +1,7 @@ package duke.exceptions; public class UnknownCommandException extends Exception { - - + public void printErrorMessage() { + System.out.println("OOPS!! I'm sorry but I don't know what that means :-("); + } } From 48805a09c9fabe1010835d25732af2986bacc44a Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Tue, 28 Feb 2023 11:23:51 +0800 Subject: [PATCH 15/22] added find task functionality --- src/main/java/duke/TaskList.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 5f0a90077..7f7ad4341 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -80,6 +80,18 @@ public static void deleteTask(String task) { Ui.printNumberOfTasks(tasks); } + public static void findTask(String task) { + System.out.println("Here are the matching tasks in your list:"); + int matchingTasksCount = 0; + for (Task i : tasks) { + if (i.getDescription().contains(task)) { + matchingTasksCount++; + System.out.print(matchingTasksCount + ". "); + i.printTask(); + } + } + } + public static void editList() throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { String[] splitText = Ui.getInput(); while (!splitText[0].equals("bye")) { @@ -135,6 +147,9 @@ public static void editList() throws UnknownCommandException, EmptyDescriptionEx throw new TaskToMarkDoesNotExistException("delete"); } break; + case "find": + TaskList.findTask(splitText[1]); + break; default: throw new UnknownCommandException(); } From b4f2ebf46a9ed16cac93cf5f9b4fa6fa645f8c61 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Fri, 3 Mar 2023 15:31:15 +0800 Subject: [PATCH 16/22] added documentation to the code --- src/main/java/duke/Duke.java | 4 +++ src/main/java/duke/Parser.java | 9 ++++++ src/main/java/duke/Storage.java | 38 ++++++++++++++++++++++++++ src/main/java/duke/TaskList.java | 28 ++++++++++++++----- src/main/java/duke/Ui.java | 9 +++++- src/main/java/duke/tasks/Deadline.java | 11 +++++++- src/main/java/duke/tasks/Event.java | 12 ++++++++ src/main/java/duke/tasks/Task.java | 20 ++++++++++---- src/main/java/duke/tasks/Todo.java | 10 +++++++ 9 files changed, 127 insertions(+), 14 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 5694af814..2af9c44ba 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -8,6 +8,10 @@ import java.io.IOException; import java.util.ArrayList; + +/** + * Main class of Duke that contains code for Duke to function + */ public class Duke { public static void main(String[] args) { diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index e99ee337e..1e2f00042 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -2,8 +2,17 @@ import java.util.Scanner; +/** + * Deals with loading tasks from the file and saving tasks in the file + */ public class Parser { + /** + * Takes in the user input and split the sentence into 2 parts by the " " + * + * @param input line that user has inputted + * @return an array of Strings that contains the command at index 0 and the description of the task at index 1 + */ public static String[] parse(Scanner input) { String text = input.nextLine(); // input the whole sentence into text return text.split(" ", 2); diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 6c9c55958..c650448d9 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -11,9 +11,20 @@ import java.util.ArrayList; import java.util.Scanner; +/** + * Deals with loading tasks from the file and saving tasks in the file + */ public class Storage { private static final String filepath = "./data/duke.txt"; + /** + * Identify the type of task in the file, split the line into the + * correct variables of description, status and start, end date and return + * the Task with the required information + * + * @param description name of task + * @return taskToAdd the Task identified to be put into the ArrayList of Tasks + */ public static Task addTask(String description) { String[] taskDescription; taskDescription = description.split("|", 3); @@ -55,6 +66,12 @@ public static Task addTask(String description) { return taskToAdd; } + /** + * Read a file from the filepath and creates a new file if a current file does not exist + * + * @return an array of Tasks that stores the tasks in the file + * @throws IOException when file does not exist + */ public static ArrayList getData() throws IOException { ArrayList currentList = new ArrayList<>(); File f = new File(filepath); @@ -71,6 +88,13 @@ public static ArrayList getData() throws IOException { return currentList; } + /** + * Identifies whether the task is done or undone + * If the task is done, return "1" but if the task is not done, return "0" + * + * @param task the task that we are currently interested in + * @return a string of either a "0" or "1" + */ public static String getNumberIcon(Task task) { if (task.getStatusIcon().equals("X")) { return "1"; @@ -78,6 +102,14 @@ public static String getNumberIcon(Task task) { return "0"; } + /** + * Convert the information of the task into the correct format that will be stored in the file + * The format tells us the type of task, the status of it and the name of the task, split with "|" + * + * @param tasks the array of tasks + * @return a String of the Task in the correct format + * @throws IOException when file does not exist + */ public static String formatTask(ArrayList tasks) throws IOException { String text = null; for (Task i : tasks) { @@ -94,6 +126,12 @@ public static String formatTask(ArrayList tasks) throws IOException { return text; } + /** + * Write to the file specified by the filepath when there are changes made to the list of tasks + * + * @param tasks an array of tasks that will be added to the file + * @throws IOException when file does not exist + */ public static void updateFile(ArrayList tasks) throws IOException { FileWriter fw = new FileWriter(filepath); String textToAdd = formatTask(tasks); diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 7f7ad4341..7cc66dfd0 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -11,11 +11,18 @@ import java.io.IOException; import java.util.ArrayList; +/** + * Contains the task list + */ public class TaskList { protected static ArrayList tasks = new ArrayList<>(); private static final String ADDED_COMMAND = "Got it. I've added this task:"; private static final String REMOVED_COMMAND = "Noted. I've removed this task:"; + /** + * Prints out all the tasks in the list in the correct format + * Prints out "You currently don't have any tasks in your list" when list is empty + */ public static void displayList() { if (tasks.size() > 0) { System.out.println("Here are the tasks in your list:"); @@ -28,19 +35,19 @@ public static void displayList() { } } - public static void markTask(String task) { + private static void markTask(String task) { tasks.get(Integer.parseInt(task) - 1).setDone(); System.out.println("Nice! I've marked this task as done:"); TaskList.displayList(); } - public static void unmarkTask(String task) { + private static void unmarkTask(String task) { tasks.get(Integer.parseInt(task) - 1).setUndone(); System.out.println("OK, I've marked this task as not done yet:"); TaskList.displayList(); } - public static void createTodo(String task) { + private static void createTodo(String task) { Task todoToAdd = new Todo(task); tasks.add(todoToAdd); System.out.println(ADDED_COMMAND); @@ -48,7 +55,7 @@ public static void createTodo(String task) { Ui.printNumberOfTasks(tasks); } - public static void createEvent(String task) { + private static void createEvent(String task) { String[] input = task.split("/from"); String description = input[0]; String[] period = input[1].split("/to"); @@ -61,7 +68,7 @@ public static void createEvent(String task) { Ui.printNumberOfTasks(tasks); } - public static void createDeadline(String task) { + private static void createDeadline(String task) { String[] words = task.split("/by"); String description = words[0]; String end = words[1]; @@ -72,7 +79,7 @@ public static void createDeadline(String task) { Ui.printNumberOfTasks(tasks); } - public static void deleteTask(String task) { + private static void deleteTask(String task) { Task taskToDelete = tasks.get(Integer.parseInt(task) - 1); tasks.remove(Integer.parseInt(task) - 1); System.out.println(REMOVED_COMMAND); @@ -80,7 +87,7 @@ public static void deleteTask(String task) { Ui.printNumberOfTasks(tasks); } - public static void findTask(String task) { + private static void findTask(String task) { System.out.println("Here are the matching tasks in your list:"); int matchingTasksCount = 0; for (Task i : tasks) { @@ -92,6 +99,13 @@ public static void findTask(String task) { } } + /** + * Identify the command user has inputted and edit the list of tasks accordingly + * + * @throws UnknownCommandException when user inputs an invalid command + * @throws EmptyDescriptionException when user does not include the description of the task + * @throws TaskToMarkDoesNotExistException when user enters a task that does not exist + */ public static void editList() throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { String[] splitText = Ui.getInput(); while (!splitText[0].equals("bye")) { diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index 57ab84f98..2bb7e83ea 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -5,8 +5,10 @@ import java.util.ArrayList; import java.util.Scanner; +/** + * Deals with interactions with the user + */ public class Ui { - public static void printGreet() { System.out.println("Hello! I'm duke."); System.out.println("What can I do for you?"); @@ -21,6 +23,11 @@ public static String[] getInput() { return Parser.parse(input); } + /** + * Print the total number of tasks that are currently in the list + * + * @param tasks the array of tasks + */ public static void printNumberOfTasks(ArrayList tasks) { System.out.println("You currently have " + tasks.size() + " tasks in your list."); } diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java index 8b1602ca9..40887c7b0 100644 --- a/src/main/java/duke/tasks/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -1,9 +1,18 @@ package duke.tasks; +/** + * Represents tasks that are classified as a Deadline + */ public class Deadline extends Task { - protected String type = "deadline"; protected String end; + + /** + * Creates a new object of type Deadline + * + * @param description name of task + * @param end the date at which task has to be completed by + */ public Deadline(String description, String end) { super(description); diff --git a/src/main/java/duke/tasks/Event.java b/src/main/java/duke/tasks/Event.java index 46e07ee61..0f44eb39c 100644 --- a/src/main/java/duke/tasks/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -1,9 +1,21 @@ package duke.tasks; +/** + * Represents tasks that are classified as a Event + * These are tasks that have a start and end date + */ public class Event extends Task { protected String start; protected String end; protected String type = "event"; + + /** + * Creates an object of type Event + * + * @param description name of task + * @param start event's start date + * @param end event's end date + */ public Event(String description, String start, String end) { super(description); this.start = start; diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java index 2d0b1f7e3..571c7be40 100644 --- a/src/main/java/duke/tasks/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -1,5 +1,9 @@ package duke.tasks; +/** + * Represents tasks set by user + * Keeps track of the task type, description, dates and status + */ public class Task { protected String description; @@ -11,9 +15,11 @@ public class Task { public String getDescription() { return description; } + public String getType() { return type; } + public String getStart() { return start; } @@ -27,10 +33,6 @@ public Task(String description) { this.isDone = false; } - public void setDescription(String description) { - this.description = description; - } - public void setDone() { this.isDone = true; } @@ -39,10 +41,18 @@ public void setUndone() { this.isDone = false; } + /** + * Checks whether the task is done and returns the correct icon to represent the status of the task + * @return 'X' if the task is done and ' ' if task is not done + */ public String getStatusIcon() { - return (isDone ? "X" : " "); // mark done task with X + return (isDone ? "X" : " "); } + /** + * Prints the Task in the correct format required in the list + * The format is determined by the type of task + */ public void printTask() { System.out.println("task"); } diff --git a/src/main/java/duke/tasks/Todo.java b/src/main/java/duke/tasks/Todo.java index 72d0583ef..7dc744d93 100644 --- a/src/main/java/duke/tasks/Todo.java +++ b/src/main/java/duke/tasks/Todo.java @@ -1,7 +1,17 @@ package duke.tasks; +/** + * Represents tasks that are classified as a Todo + * These are tasks that does not have a start or end date + */ public class Todo extends Task { protected String type = "todo"; + + /** + * Creates an object of type Todo + * + * @param description name of task + */ public Todo (String description) { super(description); } From 4e684b7f5cacf72a7f73f90037fcf47dcaad5b19 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Fri, 3 Mar 2023 17:34:20 +0800 Subject: [PATCH 17/22] added user guide --- docs/README.md | 156 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 142 insertions(+), 14 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..3b047fcfe 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,157 @@ -# User Guide +# User Guide for Duke -## Features +## Features of Duke +1. [Add a Task into the List](#add-a-task-into-the-list) +2. [Removing a Task from the List](#removing-a-task-from-the-list) +3. [Changing the Status of a Task](#changing-the-status-of-a-task) +4. [View all Tasks in the List](#view-all-tasks-in-the-list) +5. [Finding a Task in the List](#finding-a-task-in-the-list) +6. [Exiting Duke](#exiting-duke) -### Feature-ABC +### Add a Task into the List +1. ``todo`` +- this command allows users to add a task of `TASK_NAME` that does not specify a time or date +#### Format of Input -Description of the feature. +``` +todo buy bread +``` -### Feature-XYZ +#### Duke: +``` +Got it. I've added this task: +[T][ ] buy bread +You currently have 1 tasks in your list. +``` -Description of the feature. +2. ``deadline`` +- This command allows users to add a task that needs to be done before a specific date or time +#### Format of Input -## Usage +``` +deadline finish tuna sandwich /by monday +``` -### `Keyword` - Describe action +#### Duke: +``` +Got it. I've added this task: +[D][ ] finish tuna sandwich (by: monday) +You currently have 1 tasks in your list. +``` + +3. ``event`` +- This command allows users to add a task that starts and ends at a specific date or time +#### Format of Input -Describe the action and its outcome. +``` +event family dinner at joyden /from 6pm /to 9pm +``` -Example of usage: +#### Duke: +``` +Got it. I've added this task: +[E][ ] family dinner at joyden (from: 6pm to: 9pm) +You currently have 1 tasks in your list. +``` -`keyword (optional arguments)` +### Removing a Task from the List +``delete`` +- This command allows users to delete a task from the list +- The number after the command is the index of the task in the list +#### Format of Input -Expected outcome: +``` +delete 3 +``` + +#### Duke: +``` +Noted. I've removed this task: +[T][ ] sell fridge +You currently have 2 tasks in your list. +``` + +### Changing the Status of a Task +1. ``mark`` +- This command allows users to mark a task as done +- The number after the command is the index of the task in the list +- The task on the list will be updated from `[ ]` to `[X]` +#### Format of Input + +``` +mark 1 +``` + +#### Duke: +``` +Nice! I've marked this task as done: +Here are the tasks in your list: +1. [T][X] pump petrol +``` -Description of the outcome. +2. ``unmark`` +- This command allows users to mark a task as not done +- The number after the command is the index of the task in the list +- The task on the list will be updated from `[X]` to `[ ]` +#### Format of Input + +``` +mark 1 +``` + +#### Duke: +``` +OK, I've marked this task as not done yet: +Here are the tasks in your list: +1. [T][ ] pump petrol +``` + +### View all Tasks in the List +``list`` +- This command allows users to see all tasks in their list + - Task of type `todo` will be displayed: `[T][ ] TASK_NAME` + - Task of type `deadline` will be displayed: `[D][ ] TASK_NAME (by: DATE)` + - Task of type `event` will be displayed: `[E][ ] TASK_NAME (from: START_DATE to: END_DATE)` +#### Format of Input + +``` +list +``` + +#### Duke: +``` +Here are the tasks in your list: +1. [T][ ] paint a portrait of a horse +2. [D][ ] buy christmas presents (by: christmas) +3. [T][X] decorate christmas tree +``` + +### Finding a Task in the List +``find`` +- This command allows users to see all tasks that contains the keyword in their list +#### Format of Input + +``` +find beef +``` + +#### Duke: +``` +Here are the matching tasks in your list: +1. [D][ ] cook beef shank (by: friday) +2. [E][ ] beef noodle party (from: 2am to: 5am) +``` + +### Exiting Duke +``bye`` +- This command allows users to exit the programme +#### Format of Input + +``` +bye +``` +#### Duke: ``` -expected output +Bye! Hope to see you again soon! ``` From e6b9e70dde193e7082e17ee4395f35b83253486f Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Fri, 3 Mar 2023 17:38:34 +0800 Subject: [PATCH 18/22] edited user guide --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 3b047fcfe..df873064a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -10,7 +10,7 @@ ### Add a Task into the List 1. ``todo`` -- this command allows users to add a task of `TASK_NAME` that does not specify a time or date +- this command allows users to add a task that does not specify a time or date #### Format of Input ``` @@ -96,7 +96,7 @@ Here are the tasks in your list: #### Format of Input ``` -mark 1 +unmark 1 ``` #### Duke: From 54766f352f7b70f21ec450aae3a1b87525be1a78 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Fri, 3 Mar 2023 17:40:39 +0800 Subject: [PATCH 19/22] changed the alignment of user guide --- docs/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/README.md b/docs/README.md index df873064a..2a5421cba 100644 --- a/docs/README.md +++ b/docs/README.md @@ -11,6 +11,7 @@ ### Add a Task into the List 1. ``todo`` - this command allows users to add a task that does not specify a time or date + #### Format of Input ``` @@ -26,6 +27,7 @@ You currently have 1 tasks in your list. 2. ``deadline`` - This command allows users to add a task that needs to be done before a specific date or time + #### Format of Input ``` @@ -41,6 +43,7 @@ You currently have 1 tasks in your list. 3. ``event`` - This command allows users to add a task that starts and ends at a specific date or time + #### Format of Input ``` @@ -58,6 +61,7 @@ You currently have 1 tasks in your list. ``delete`` - This command allows users to delete a task from the list - The number after the command is the index of the task in the list + #### Format of Input ``` @@ -76,6 +80,7 @@ You currently have 2 tasks in your list. - This command allows users to mark a task as done - The number after the command is the index of the task in the list - The task on the list will be updated from `[ ]` to `[X]` + #### Format of Input ``` @@ -93,6 +98,7 @@ Here are the tasks in your list: - This command allows users to mark a task as not done - The number after the command is the index of the task in the list - The task on the list will be updated from `[X]` to `[ ]` + #### Format of Input ``` @@ -112,6 +118,7 @@ Here are the tasks in your list: - Task of type `todo` will be displayed: `[T][ ] TASK_NAME` - Task of type `deadline` will be displayed: `[D][ ] TASK_NAME (by: DATE)` - Task of type `event` will be displayed: `[E][ ] TASK_NAME (from: START_DATE to: END_DATE)` + #### Format of Input ``` @@ -129,6 +136,7 @@ Here are the tasks in your list: ### Finding a Task in the List ``find`` - This command allows users to see all tasks that contains the keyword in their list + #### Format of Input ``` @@ -145,6 +153,7 @@ Here are the matching tasks in your list: ### Exiting Duke ``bye`` - This command allows users to exit the programme + #### Format of Input ``` From 1474f6e5510909a430923056976b720b84880d43 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Fri, 3 Mar 2023 19:46:00 +0800 Subject: [PATCH 20/22] changed duke to expected output --- docs/README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/README.md b/docs/README.md index 2a5421cba..d08486df6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -18,7 +18,7 @@ todo buy bread ``` -#### Duke: +#### Expected Output: ``` Got it. I've added this task: [T][ ] buy bread @@ -34,7 +34,7 @@ You currently have 1 tasks in your list. deadline finish tuna sandwich /by monday ``` -#### Duke: +#### Expected Output: ``` Got it. I've added this task: [D][ ] finish tuna sandwich (by: monday) @@ -50,7 +50,7 @@ You currently have 1 tasks in your list. event family dinner at joyden /from 6pm /to 9pm ``` -#### Duke: +#### Expected Output: ``` Got it. I've added this task: [E][ ] family dinner at joyden (from: 6pm to: 9pm) @@ -68,7 +68,7 @@ You currently have 1 tasks in your list. delete 3 ``` -#### Duke: +#### Expected Output: ``` Noted. I've removed this task: [T][ ] sell fridge @@ -87,7 +87,7 @@ You currently have 2 tasks in your list. mark 1 ``` -#### Duke: +#### Expected Output: ``` Nice! I've marked this task as done: Here are the tasks in your list: @@ -105,7 +105,7 @@ Here are the tasks in your list: unmark 1 ``` -#### Duke: +#### Expected Output: ``` OK, I've marked this task as not done yet: Here are the tasks in your list: @@ -125,7 +125,7 @@ Here are the tasks in your list: list ``` -#### Duke: +#### Expected Output: ``` Here are the tasks in your list: 1. [T][ ] paint a portrait of a horse @@ -143,7 +143,7 @@ Here are the tasks in your list: find beef ``` -#### Duke: +#### Expected Output: ``` Here are the matching tasks in your list: 1. [D][ ] cook beef shank (by: friday) @@ -160,7 +160,7 @@ Here are the matching tasks in your list: bye ``` -#### Duke: +#### Expected Output: ``` Bye! Hope to see you again soon! ``` From a1ba4eaa71cbeb27837dcb13e1d3d3461131c92d Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Fri, 3 Mar 2023 20:58:07 +0800 Subject: [PATCH 21/22] dealt with a few more exceptions --- src/main/java/META-INF/MANIFEST.MF | 3 +++ src/main/java/duke/Storage.java | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) 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 000000000..2c9a9745c --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.Duke + diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index c650448d9..ca71d24e1 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -108,9 +108,9 @@ public static String getNumberIcon(Task task) { * * @param tasks the array of tasks * @return a String of the Task in the correct format - * @throws IOException when file does not exist + * @throws NullPointerException */ - public static String formatTask(ArrayList tasks) throws IOException { + public static String formatTask(ArrayList tasks) throws NullPointerException { String text = null; for (Task i : tasks) { if (i.getType().equals("todo")) { @@ -131,11 +131,16 @@ public static String formatTask(ArrayList tasks) throws IOException { * * @param tasks an array of tasks that will be added to the file * @throws IOException when file does not exist + * @throws NullPointerException */ - public static void updateFile(ArrayList tasks) throws IOException { - FileWriter fw = new FileWriter(filepath); - String textToAdd = formatTask(tasks); - fw.write(textToAdd); - fw.close(); + public static void updateFile(ArrayList tasks) throws IOException, NullPointerException { + FileWriter fw = null; + try { + fw = new FileWriter(filepath); + String textToAdd = formatTask(tasks); + fw.write(textToAdd); + } finally { + fw.close(); + } } } From ac1e054ed909a01eb7ce66ec3221ae78290f79f3 Mon Sep 17 00:00:00 2001 From: javienneyeo Date: Fri, 3 Mar 2023 22:34:01 +0800 Subject: [PATCH 22/22] changed code for format task --- src/main/java/duke/Storage.java | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index ca71d24e1..b36465b0d 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -106,22 +106,20 @@ public static String getNumberIcon(Task task) { * Convert the information of the task into the correct format that will be stored in the file * The format tells us the type of task, the status of it and the name of the task, split with "|" * - * @param tasks the array of tasks - * @return a String of the Task in the correct format + * @param task + * @return a String of the task in the correct format * @throws NullPointerException */ - public static String formatTask(ArrayList tasks) throws NullPointerException { + public static String formatTask(Task task) throws NullPointerException { String text = null; - for (Task i : tasks) { - if (i.getType().equals("todo")) { - text = "T" + " | " + getNumberIcon(i) + " | " + i.getDescription(); - } - if (i.getType().equals("deadline")) { - text = "D" + " | " + getNumberIcon(i) + " | " + i.getDescription() + " | " + i.getEnd(); - } - if (i.getType().equals("event")) { - text = "E" + " | " + getNumberIcon(i) + " | " + i.getDescription() + " | " + i.getStart() + "-" + i.getEnd(); - } + if (task.getType().equals("todo")) { + text = "T" + " | " + getNumberIcon(task) + " | " + task.getDescription(); + } + if (task.getType().equals("deadline")) { + text = "D" + " | " + getNumberIcon(task) + " | " + task.getDescription() + " | " + task.getEnd(); + } + if (task.getType().equals("event")) { + text = "E" + " | " + getNumberIcon(task) + " | " + task.getDescription() + " | " + task.getStart() + "-" + task.getEnd(); } return text; } @@ -137,8 +135,10 @@ public static void updateFile(ArrayList tasks) throws IOException, NullPoi FileWriter fw = null; try { fw = new FileWriter(filepath); - String textToAdd = formatTask(tasks); - fw.write(textToAdd); + for (Task i : tasks) { + String textToAdd = formatTask(i); + fw.write(textToAdd); + } } finally { fw.close(); }