From 12a4d6fd5c3667057a44cc789de0b9a6900f5121 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Wed, 1 Feb 2023 00:25:36 +0800 Subject: [PATCH 01/22] Add Greetings --- src/main/java/Duke.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..c75a863ac 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -5,6 +5,11 @@ public static void main(String[] args) { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; + + String line = "_________________________________\n"; System.out.println("Hello from\n" + logo); + System.out.println(line + "Hello! I'm Duke\n" + "What can I do for you?"); + System.out.print(line + "Bye. Hope to see you again soon!\n" + line); } + } From 3c5745741cbea1e4538ce373c41446e464e717c1 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Wed, 1 Feb 2023 01:07:50 +0800 Subject: [PATCH 02/22] Add userInput --- src/main/java/Duke.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index c75a863ac..e6b947797 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,8 +10,16 @@ public static void main(String[] args) { String line = "_________________________________\n"; System.out.println("Hello from\n" + logo); - System.out.println(line + "Hello! I'm Duke\n" + "What can I do for you?"); - System.out.print(line + "Bye. Hope to see you again soon!\n" + line); + System.out.print(line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); + while(true) { + Scanner userInput = new Scanner(System.in); + String name = userInput.nextLine(); + if (name.equals("bye")) { + System.out.print("Bye. Hope to see you again soon!\n" + line); + break; + } else { + System.out.print(line + name + "\n" + line); + } + } } - } From a96a8be6a0cc37475949e435ac343dce81dfdfd7 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Wed, 1 Feb 2023 09:13:26 +0800 Subject: [PATCH 03/22] Add List --- src/main/java/Duke.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index e6b947797..4cc5e92cb 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -11,14 +11,24 @@ public static void main(String[] args) { String line = "_________________________________\n"; System.out.println("Hello from\n" + logo); System.out.print(line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); + String[] list = new String[100]; + int listCount = 0; while(true) { Scanner userInput = new Scanner(System.in); - String name = userInput.nextLine(); - if (name.equals("bye")) { - System.out.print("Bye. Hope to see you again soon!\n" + line); + String input = userInput.nextLine(); + if (input.equals("bye")) { + System.out.print(line + "Bye. Hope to see you again soon!\n" + line); break; + } else if(input.equals("list")) { + System.out.print(line); + for (int i = 0; i < listCount; i++) { + System.out.println(i + 1 + ". " + list[i]); + } + System.out.print(line); } else { - System.out.print(line + name + "\n" + line); + list[listCount] = input; + listCount++; + System.out.print(line + "added: " + input + "\n" + line); } } } From 57d0c6a39a72a97984ff5c6392471ab2a7c9f259 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Wed, 1 Feb 2023 09:58:41 +0800 Subject: [PATCH 04/22] Add mark to list --- src/main/java/Duke.java | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4cc5e92cb..2ecbeef16 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -8,10 +8,12 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - String line = "_________________________________\n"; + String line = "____________________________________________________\n"; System.out.println("Hello from\n" + logo); System.out.print(line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); String[] list = new String[100]; + boolean[] isDone = new boolean[100]; + String toFill; int listCount = 0; while(true) { Scanner userInput = new Scanner(System.in); @@ -19,10 +21,26 @@ public static void main(String[] args) { if (input.equals("bye")) { System.out.print(line + "Bye. Hope to see you again soon!\n" + line); break; - } else if(input.equals("list")) { + } else if (input.startsWith("mark")) { + int mark = Integer.parseInt(input.substring(5)); + isDone[mark - 1] = true; + System.out.println(line + "Nice! I've marked this task as done:\n" + " [X] " + list[mark - 1]); + System.out.print(line); + } else if (input.startsWith("unmark")) { + int unmark = Integer.parseInt(input.substring(7)); + isDone[unmark - 1] = false; + System.out.println(line + "OK, I've marked this task as not done yet:\n" + " [ ] "+ list[unmark - 1]); System.out.print(line); + } else if(input.equals("list")) { + System.out.print(line + "Here are the tasks in your list:\n"); for (int i = 0; i < listCount; i++) { - System.out.println(i + 1 + ". " + list[i]); + if (isDone[i]) { + toFill = "X"; + } + else { + toFill = " "; + } + System.out.println(i + 1 + ". [" + toFill + "] " + list[i]); } System.out.print(line); } else { From bd71f694b700bafba0eee0ff7308565ab166757c Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Fri, 3 Feb 2023 01:00:05 +0800 Subject: [PATCH 05/22] Add todo, event and deadline --- src/main/java/Deadline.java | 12 +++++++++ src/main/java/Duke.java | 51 ++++++++++++++++++++++--------------- src/main/java/Event.java | 16 ++++++++++++ src/main/java/Task.java | 36 ++++++++++++++++++++++++++ src/main/java/Todo.java | 11 ++++++++ 5 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Task.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..cea5cf57c --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,12 @@ +public class Deadline extends Task { + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + @Override + public String toString() { + return "[D]" + super.toString() + "(by: " + by + ")"; + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 2ecbeef16..bf2d10f1f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -11,10 +11,9 @@ public static void main(String[] args) { String line = "____________________________________________________\n"; System.out.println("Hello from\n" + logo); System.out.print(line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); - String[] list = new String[100]; - boolean[] isDone = new boolean[100]; - String toFill; - int listCount = 0; + Task[] task = new Task[100]; + int taskCount = 0; + int errorCount = 0; while(true) { Scanner userInput = new Scanner(System.in); String input = userInput.nextLine(); @@ -23,30 +22,42 @@ public static void main(String[] args) { break; } else if (input.startsWith("mark")) { int mark = Integer.parseInt(input.substring(5)); - isDone[mark - 1] = true; - System.out.println(line + "Nice! I've marked this task as done:\n" + " [X] " + list[mark - 1]); + task[mark - 1].markDone(); System.out.print(line); } else if (input.startsWith("unmark")) { int unmark = Integer.parseInt(input.substring(7)); - isDone[unmark - 1] = false; - System.out.println(line + "OK, I've marked this task as not done yet:\n" + " [ ] "+ list[unmark - 1]); + task[unmark - 1].umarkDone(); System.out.print(line); - } else if(input.equals("list")) { + } else if (input.equals("list")) { System.out.print(line + "Here are the tasks in your list:\n"); - for (int i = 0; i < listCount; i++) { - if (isDone[i]) { - toFill = "X"; - } - else { - toFill = " "; - } - System.out.println(i + 1 + ". [" + toFill + "] " + list[i]); + for (int i = 0; i < taskCount; i++) { + System.out.println(i + 1 + "." + task[i]); } System.out.print(line); + } else if (input.startsWith("todo")) { + task[taskCount] = new Todo(input.substring(5)); + taskCount++; + System.out.println(line + "Got it. I've added this task:\n" + task[taskCount - 1]); + System.out.print("Now you have " + taskCount + " tasks in the list.\n" + line); + } else if (input.startsWith("deadline")) { + int byIndex = input.indexOf("/by"); + task[taskCount] = new Deadline(input.substring(9, byIndex), input.substring(byIndex + 4)); + taskCount++; + System.out.println(line + "Got it. I've added this task:\n" + task[taskCount - 1]); + System.out.print("Now you have " + taskCount + " tasks in the list.\n" + line); + } else if (input.startsWith("event")) { + int fromIndex = input.indexOf("/from"); + int toIndex = input.indexOf("/to"); + task[taskCount] = new Event(input.substring(6, fromIndex), input.substring(fromIndex + 6, toIndex), input.substring(toIndex + 4)); + taskCount++; + System.out.println(line + "Got it. I've added this task:\n" + task[taskCount - 1]); + System.out.print("Now you have " + taskCount + " tasks in the list.\n" + line); + } else if (errorCount == 10) { + System.out.print(line +"Maybe you can try in another language. Goodbye!\n" + line); + break; } else { - list[listCount] = input; - listCount++; - System.out.print(line + "added: " + input + "\n" + line); + System.out.print(line + "Sorry, could you please repeat what you just said\n" + line); + errorCount++; } } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..0b31d1f93 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,16 @@ +public class Event extends Task { + + protected String from; + protected String to; + + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + } + + @Override + public String toString() { + return "[E]" + super.toString() + "(from: " + from + "to: " + to + ")"; + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..bf6b772d9 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,36 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.isDone = false; + this.description = description; + } + + public void markDone() { + isDone = true; + System.out.println("Nice! I've marked this task as done:\n" + this); + } + + public void umarkDone() { + isDone = false; + System.out.println("OK, I've marked this task as not done yet:\n" + this); + } + public String getDescription() { + return description; + } + + public String status(){ + if(isDone) { + return "X"; + } + else { + return " "; + } + } + + @Override + public String toString() { + return "[" + status() + "]" + getDescription(); + } +} diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..931cfd4f9 --- /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 String toString() { + return "[T]" + super.toString(); + } +} From 7473a4ced3da0ac8562f8f50db88ab6e2296b42c Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Thu, 16 Feb 2023 10:47:02 +0800 Subject: [PATCH 06/22] Changed variable namings --- src/main/java/Duke.java | 48 ++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index bf2d10f1f..7de56eec9 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,61 +2,61 @@ public class Duke { public static void main(String[] args) { - String logo = " ____ _ \n" + String LOGO = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - String line = "____________________________________________________\n"; - System.out.println("Hello from\n" + logo); - System.out.print(line + "Hello! I'm Duke\n" + "What can I do for you?\n" + line); - Task[] task = new Task[100]; + String DIVIDER = "____________________________________________________\n"; + System.out.println("Hello from\n" + LOGO); + System.out.print(DIVIDER + "Hello! I'm Duke\n" + "What can I do for you?\n" + DIVIDER); + Task[] tasks = new Task[100]; int taskCount = 0; int errorCount = 0; while(true) { Scanner userInput = new Scanner(System.in); String input = userInput.nextLine(); if (input.equals("bye")) { - System.out.print(line + "Bye. Hope to see you again soon!\n" + line); + System.out.print(DIVIDER + "Bye. Hope to see you again soon!\n" + DIVIDER); break; } else if (input.startsWith("mark")) { int mark = Integer.parseInt(input.substring(5)); - task[mark - 1].markDone(); - System.out.print(line); + tasks[mark - 1].markDone(); + System.out.print(DIVIDER); } else if (input.startsWith("unmark")) { int unmark = Integer.parseInt(input.substring(7)); - task[unmark - 1].umarkDone(); - System.out.print(line); + tasks[unmark - 1].umarkDone(); + System.out.print(DIVIDER); } else if (input.equals("list")) { - System.out.print(line + "Here are the tasks in your list:\n"); + System.out.print(DIVIDER + "Here are the tasks in your list:\n"); for (int i = 0; i < taskCount; i++) { - System.out.println(i + 1 + "." + task[i]); + System.out.println(i + 1 + "." + tasks[i]); } - System.out.print(line); + System.out.print(DIVIDER); } else if (input.startsWith("todo")) { - task[taskCount] = new Todo(input.substring(5)); + tasks[taskCount] = new Todo(input.substring(5)); taskCount++; - System.out.println(line + "Got it. I've added this task:\n" + task[taskCount - 1]); - System.out.print("Now you have " + taskCount + " tasks in the list.\n" + line); + System.out.println(DIVIDER + "Got it. I've added this tasks:\n" + tasks[taskCount - 1]); + System.out.print("Now you have " + taskCount + " tasks in the list.\n" + DIVIDER); } else if (input.startsWith("deadline")) { int byIndex = input.indexOf("/by"); - task[taskCount] = new Deadline(input.substring(9, byIndex), input.substring(byIndex + 4)); + tasks[taskCount] = new Deadline(input.substring(9, byIndex), input.substring(byIndex + 4)); taskCount++; - System.out.println(line + "Got it. I've added this task:\n" + task[taskCount - 1]); - System.out.print("Now you have " + taskCount + " tasks in the list.\n" + line); + System.out.println(DIVIDER + "Got it. I've added this tasks:\n" + tasks[taskCount - 1]); + System.out.print("Now you have " + taskCount + " tasks in the list.\n" + DIVIDER); } else if (input.startsWith("event")) { int fromIndex = input.indexOf("/from"); int toIndex = input.indexOf("/to"); - task[taskCount] = new Event(input.substring(6, fromIndex), input.substring(fromIndex + 6, toIndex), input.substring(toIndex + 4)); + tasks[taskCount] = new Event(input.substring(6, fromIndex), input.substring(fromIndex + 6, toIndex), input.substring(toIndex + 4)); taskCount++; - System.out.println(line + "Got it. I've added this task:\n" + task[taskCount - 1]); - System.out.print("Now you have " + taskCount + " tasks in the list.\n" + line); + System.out.println(DIVIDER + "Got it. I've added this tasks:\n" + tasks[taskCount - 1]); + System.out.print("Now you have " + taskCount + " tasks in the list.\n" + DIVIDER); } else if (errorCount == 10) { - System.out.print(line +"Maybe you can try in another language. Goodbye!\n" + line); + System.out.print(DIVIDER +"Maybe you can try in another language. Goodbye!\n" + DIVIDER); break; } else { - System.out.print(line + "Sorry, could you please repeat what you just said\n" + line); + System.out.print(DIVIDER + "Sorry, could you please repeat what you just said\n" + DIVIDER); errorCount++; } } From fa25e2dd67a98679b22411c85aac5d7ce67841f5 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Thu, 16 Feb 2023 16:02:29 +0800 Subject: [PATCH 07/22] Changed to dynamic size array. Updated class methods. --- src/main/java/Duke.java | 112 +++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 37 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 7de56eec9..ba9877087 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,6 +1,66 @@ +import java.util.ArrayList; import java.util.Scanner; public class Duke { + + public static ArrayListtasks = new ArrayList<>(); + public static void printExit() { + System.out.println("Bye. Hope to see you again soon!"); + } + + public static void printDivider() { + String DIVIDER = "____________________________________________________"; + System.out.println(DIVIDER); + } + + public static void markTask(String input) { + int taskNumber = Integer.parseInt(input.substring(5)) - 1; + tasks.get(taskNumber).markDone(); + } + + public static void unmarkTask(String input) { + int taskNumber = Integer.parseInt(input.substring(7)) - 1; + tasks.get(taskNumber).umarkDone(); + } + + public static void printList() { + printDivider(); + System.out.println("Here are the tasks in your list:"); + int count = 1; + for (Task output : tasks) { + System.out.println(count + "." + output); + count++; + } + printDivider(); + } + + public static void createTodo(String input) { + tasks.add(new Todo(input.substring(5))); + printDivider(); + System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); + System.out.println("Now you have " + (tasks.size() - 1) + " tasks in the list."); + printDivider(); + } + + public static void createDeadline(String input) { + int byIndex = input.indexOf("/by"); + tasks.add(new Deadline(input.substring(9, byIndex), input.substring(byIndex + 4))); + printDivider(); + System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); + System.out.println("Now you have " + (tasks.size() - 1) + " tasks in the list."); + printDivider(); + } + + public static void createEvent(String input) { + int fromIndex = input.indexOf("/from"); + int toIndex = input.indexOf("/to"); + tasks.add(new Event(input.substring(6, fromIndex), input.substring(fromIndex + 6, toIndex), input.substring(toIndex + 4))); + printDivider(); + System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); + System.out.println("Now you have " + (tasks.size() - 1) + " tasks in the list."); + printDivider(); + } + public static void main(String[] args) { String LOGO = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -8,56 +68,34 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - String DIVIDER = "____________________________________________________\n"; System.out.println("Hello from\n" + LOGO); - System.out.print(DIVIDER + "Hello! I'm Duke\n" + "What can I do for you?\n" + DIVIDER); - Task[] tasks = new Task[100]; - int taskCount = 0; - int errorCount = 0; + printDivider(); + System.out.println("Hello! I'm Duke\n" + "What can I do for you?\n"); + printDivider(); + while(true) { Scanner userInput = new Scanner(System.in); String input = userInput.nextLine(); if (input.equals("bye")) { - System.out.print(DIVIDER + "Bye. Hope to see you again soon!\n" + DIVIDER); + printExit(); + printDivider(); break; } else if (input.startsWith("mark")) { - int mark = Integer.parseInt(input.substring(5)); - tasks[mark - 1].markDone(); - System.out.print(DIVIDER); + markTask(input); + printDivider(); } else if (input.startsWith("unmark")) { - int unmark = Integer.parseInt(input.substring(7)); - tasks[unmark - 1].umarkDone(); - System.out.print(DIVIDER); + unmarkTask(input); + printDivider(); } else if (input.equals("list")) { - System.out.print(DIVIDER + "Here are the tasks in your list:\n"); - for (int i = 0; i < taskCount; i++) { - System.out.println(i + 1 + "." + tasks[i]); - } - System.out.print(DIVIDER); + printList(); } else if (input.startsWith("todo")) { - tasks[taskCount] = new Todo(input.substring(5)); - taskCount++; - System.out.println(DIVIDER + "Got it. I've added this tasks:\n" + tasks[taskCount - 1]); - System.out.print("Now you have " + taskCount + " tasks in the list.\n" + DIVIDER); + createTodo(input); } else if (input.startsWith("deadline")) { - int byIndex = input.indexOf("/by"); - tasks[taskCount] = new Deadline(input.substring(9, byIndex), input.substring(byIndex + 4)); - taskCount++; - System.out.println(DIVIDER + "Got it. I've added this tasks:\n" + tasks[taskCount - 1]); - System.out.print("Now you have " + taskCount + " tasks in the list.\n" + DIVIDER); + createDeadline(input); } else if (input.startsWith("event")) { - int fromIndex = input.indexOf("/from"); - int toIndex = input.indexOf("/to"); - tasks[taskCount] = new Event(input.substring(6, fromIndex), input.substring(fromIndex + 6, toIndex), input.substring(toIndex + 4)); - taskCount++; - System.out.println(DIVIDER + "Got it. I've added this tasks:\n" + tasks[taskCount - 1]); - System.out.print("Now you have " + taskCount + " tasks in the list.\n" + DIVIDER); - } else if (errorCount == 10) { - System.out.print(DIVIDER +"Maybe you can try in another language. Goodbye!\n" + DIVIDER); - break; + createEvent(input); } else { - System.out.print(DIVIDER + "Sorry, could you please repeat what you just said\n" + DIVIDER); - errorCount++; + System.out.print("Sorry, could you please repeat what you just said\n" ); } } } From 323003417ff58b54f8ff512726c97d82dae26d23 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Thu, 16 Feb 2023 19:41:19 +0800 Subject: [PATCH 08/22] Add exceptions --- src/main/java/CommandNotFoundException.java | 2 + src/main/java/Duke.java | 137 ++++++++++++++------ src/main/java/DukeException.java | 2 + src/main/java/Event.java | 2 +- src/main/java/IllegalFormatException.java | 2 + src/main/java/InvalidInputException.java | 2 + 6 files changed, 106 insertions(+), 41 deletions(-) create mode 100644 src/main/java/CommandNotFoundException.java create mode 100644 src/main/java/DukeException.java create mode 100644 src/main/java/IllegalFormatException.java create mode 100644 src/main/java/InvalidInputException.java diff --git a/src/main/java/CommandNotFoundException.java b/src/main/java/CommandNotFoundException.java new file mode 100644 index 000000000..62d0ba9ad --- /dev/null +++ b/src/main/java/CommandNotFoundException.java @@ -0,0 +1,2 @@ +public class CommandNotFoundException extends DukeException{ +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ba9877087..982589653 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -4,22 +4,85 @@ public class Duke { public static ArrayListtasks = new ArrayList<>(); - public static void printExit() { - System.out.println("Bye. Hope to see you again soon!"); - } + + public static boolean proceedToNextCommand = true; public static void printDivider() { String DIVIDER = "____________________________________________________"; System.out.println(DIVIDER); } - public static void markTask(String input) { - int taskNumber = Integer.parseInt(input.substring(5)) - 1; + public static void executeCommand(String input) throws CommandNotFoundException { + if (input.equals("bye")) { + printExit(); + proceedToNextCommand = false; + System.exit(0); + } else if (input.startsWith("mark")) { + printDivider(); + try { + markTask(input); + } catch (NumberFormatException e) { + System.out.println("Task to be marked is not a number."); + } catch (IndexOutOfBoundsException e) { + System.out.println("Task number to be marked is not within the list"); + } + printDivider(); + } else if (input.startsWith("unmark")) { + printDivider(); + try { + unmarkTask(input); + } catch (NumberFormatException e) { + System.out.println("Task to be unmarked is not a number."); + } catch (IndexOutOfBoundsException e) { + System.out.println("Task number to be unmarked is not within the list"); + } + printDivider(); + } else if (input.equals("list")) { + printList(); + } else if (input.startsWith("todo")) { + printDivider(); + try { + createTodo(input); + } catch (IndexOutOfBoundsException e) { + System.out.println("The description of a todo cannot be empty."); + } + printDivider(); + } else if (input.startsWith("deadline")) { + printDivider(); + try { + createDeadline(input); + } catch (IndexOutOfBoundsException | InvalidInputException e) { + System.out.println("Incomplete description or date of the deadline\n" + "Please input in the following format: deadline /by "); + } + printDivider(); + } else if (input.startsWith("event")) { + printDivider(); + try { + createEvent(input); + } catch (IllegalFormatException e) { + System.out.println("The /from cannot go after /to\n" + "Please input in the following format: event /from /to "); + } catch (IndexOutOfBoundsException | InvalidInputException e) { + System.out.println("Incomplete description or date of the event\n" + "Please input in the following format: event /from /to "); + } + printDivider(); + } else { + throw new CommandNotFoundException(); + } + } + + public static void printExit() { + printDivider(); + System.out.println("Bye. Hope to see you again soon!"); + printDivider(); + } + + public static void markTask(String input) { + int taskNumber = Integer.parseInt(input.substring(4).trim()) - 1; tasks.get(taskNumber).markDone(); } - public static void unmarkTask(String input) { - int taskNumber = Integer.parseInt(input.substring(7)) - 1; + public static void unmarkTask(String input) { + int taskNumber = Integer.parseInt(input.substring(6).trim()) - 1; tasks.get(taskNumber).umarkDone(); } @@ -35,32 +98,40 @@ public static void printList() { } public static void createTodo(String input) { - tasks.add(new Todo(input.substring(5))); - printDivider(); + tasks.add(new Todo(input.substring(4).trim())); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + (tasks.size() - 1) + " tasks in the list."); - printDivider(); + System.out.println("Now you have " + (tasks.size()) + " tasks in the list."); } - public static void createDeadline(String input) { + public static void createDeadline(String input) throws InvalidInputException { int byIndex = input.indexOf("/by"); - tasks.add(new Deadline(input.substring(9, byIndex), input.substring(byIndex + 4))); - printDivider(); + String description = input.substring(8, byIndex).trim(); + String deadline = input.substring(byIndex + 3).trim(); + if (description.length() < 1 | deadline.length() < 1) { + throw new InvalidInputException(); + } + tasks.add(new Deadline(description, deadline)); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + (tasks.size() - 1) + " tasks in the list."); - printDivider(); + System.out.println("Now you have " + (tasks.size()) + " tasks in the list."); } - public static void createEvent(String input) { + public static void createEvent(String input) throws IllegalFormatException, InvalidInputException { int fromIndex = input.indexOf("/from"); int toIndex = input.indexOf("/to"); - tasks.add(new Event(input.substring(6, fromIndex), input.substring(fromIndex + 6, toIndex), input.substring(toIndex + 4))); - printDivider(); + String description = input.substring(5, fromIndex).trim(); + String eventStart = input.substring(fromIndex + 5, toIndex).trim(); + String eventEnd = input.substring(toIndex + 3).trim(); + if (description.length() < 1 | eventStart.length() < 1 | eventEnd.length() < 1) { + throw new InvalidInputException(); + } else if (fromIndex > toIndex) { + throw new IllegalFormatException(); + } + tasks.add(new Event(description, eventStart, eventEnd)); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + (tasks.size() - 1) + " tasks in the list."); - printDivider(); + System.out.println("Now you have " + (tasks.size()) + " tasks in the list."); } + public static void main(String[] args) { String LOGO = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -73,29 +144,15 @@ public static void main(String[] args) { System.out.println("Hello! I'm Duke\n" + "What can I do for you?\n"); printDivider(); - while(true) { + while(proceedToNextCommand) { Scanner userInput = new Scanner(System.in); String input = userInput.nextLine(); - if (input.equals("bye")) { - printExit(); + try { + executeCommand(input); + } catch (CommandNotFoundException e) { printDivider(); - break; - } else if (input.startsWith("mark")) { - markTask(input); + System.out.println("I'm sorry, but I don't know what that means =("); printDivider(); - } else if (input.startsWith("unmark")) { - unmarkTask(input); - printDivider(); - } else if (input.equals("list")) { - printList(); - } else if (input.startsWith("todo")) { - createTodo(input); - } else if (input.startsWith("deadline")) { - createDeadline(input); - } else if (input.startsWith("event")) { - createEvent(input); - } else { - System.out.print("Sorry, could you please repeat what you just said\n" ); } } } diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..1dca41907 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,2 @@ +public class DukeException extends Exception{ +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 0b31d1f93..3d512869a 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -11,6 +11,6 @@ public Event(String description, String from, String to) { @Override public String toString() { - return "[E]" + super.toString() + "(from: " + from + "to: " + to + ")"; + return "[E]" + super.toString() + "(from: " + from + " to: " + to + ")"; } } diff --git a/src/main/java/IllegalFormatException.java b/src/main/java/IllegalFormatException.java new file mode 100644 index 000000000..9f6987dee --- /dev/null +++ b/src/main/java/IllegalFormatException.java @@ -0,0 +1,2 @@ +public class IllegalFormatException extends DukeException{ +} diff --git a/src/main/java/InvalidInputException.java b/src/main/java/InvalidInputException.java new file mode 100644 index 000000000..d67236024 --- /dev/null +++ b/src/main/java/InvalidInputException.java @@ -0,0 +1,2 @@ +public class InvalidInputException extends DukeException{ +} From bb5533e154c9bf35cc79dec0c5f41089f6e0d917 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Thu, 16 Feb 2023 19:55:50 +0800 Subject: [PATCH 09/22] Add package and organized classes --- src/main/java/Duke.java | 8 ++ .../exception}/CommandNotFoundException.java | 2 + .../{ => duke/exception}/DukeException.java | 2 + .../exception}/IllegalFormatException.java | 2 + .../exception}/InvalidInputException.java | 2 + src/main/java/{ => duke/task}/Deadline.java | 26 ++++--- src/main/java/{ => duke/task}/Event.java | 34 +++++---- src/main/java/{ => duke/task}/Task.java | 74 ++++++++++--------- src/main/java/{ => duke/task}/Todo.java | 24 +++--- 9 files changed, 99 insertions(+), 75 deletions(-) rename src/main/java/{ => duke/exception}/CommandNotFoundException.java (68%) rename src/main/java/{ => duke/exception}/DukeException.java (62%) rename src/main/java/{ => duke/exception}/IllegalFormatException.java (67%) rename src/main/java/{ => duke/exception}/InvalidInputException.java (67%) rename src/main/java/{ => duke/task}/Deadline.java (89%) rename src/main/java/{ => duke/task}/Event.java (90%) rename src/main/java/{ => duke/task}/Task.java (93%) rename src/main/java/{ => duke/task}/Todo.java (85%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 982589653..cb4ae2fe6 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,11 @@ +import duke.exception.CommandNotFoundException; +import duke.exception.IllegalFormatException; +import duke.exception.InvalidInputException; +import duke.task.Deadline; +import duke.task.Event; +import duke.task.Task; +import duke.task.Todo; + import java.util.ArrayList; import java.util.Scanner; diff --git a/src/main/java/CommandNotFoundException.java b/src/main/java/duke/exception/CommandNotFoundException.java similarity index 68% rename from src/main/java/CommandNotFoundException.java rename to src/main/java/duke/exception/CommandNotFoundException.java index 62d0ba9ad..15485cdc3 100644 --- a/src/main/java/CommandNotFoundException.java +++ b/src/main/java/duke/exception/CommandNotFoundException.java @@ -1,2 +1,4 @@ +package duke.exception; + public class CommandNotFoundException extends DukeException{ } diff --git a/src/main/java/DukeException.java b/src/main/java/duke/exception/DukeException.java similarity index 62% rename from src/main/java/DukeException.java rename to src/main/java/duke/exception/DukeException.java index 1dca41907..ceea79b71 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/duke/exception/DukeException.java @@ -1,2 +1,4 @@ +package duke.exception; + public class DukeException extends Exception{ } diff --git a/src/main/java/IllegalFormatException.java b/src/main/java/duke/exception/IllegalFormatException.java similarity index 67% rename from src/main/java/IllegalFormatException.java rename to src/main/java/duke/exception/IllegalFormatException.java index 9f6987dee..69acac0da 100644 --- a/src/main/java/IllegalFormatException.java +++ b/src/main/java/duke/exception/IllegalFormatException.java @@ -1,2 +1,4 @@ +package duke.exception; + public class IllegalFormatException extends DukeException{ } diff --git a/src/main/java/InvalidInputException.java b/src/main/java/duke/exception/InvalidInputException.java similarity index 67% rename from src/main/java/InvalidInputException.java rename to src/main/java/duke/exception/InvalidInputException.java index d67236024..23f7a51f2 100644 --- a/src/main/java/InvalidInputException.java +++ b/src/main/java/duke/exception/InvalidInputException.java @@ -1,2 +1,4 @@ +package duke.exception; + public class InvalidInputException extends DukeException{ } diff --git a/src/main/java/Deadline.java b/src/main/java/duke/task/Deadline.java similarity index 89% rename from src/main/java/Deadline.java rename to src/main/java/duke/task/Deadline.java index cea5cf57c..4950dbfd3 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,12 +1,14 @@ -public class Deadline extends Task { - protected String by; - - public Deadline(String description, String by) { - super(description); - this.by = by; - } - @Override - public String toString() { - return "[D]" + super.toString() + "(by: " + by + ")"; - } -} +package duke.task; + +public class Deadline extends Task { + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + @Override + public String toString() { + return "[D]" + super.toString() + "(by: " + by + ")"; + } +} diff --git a/src/main/java/Event.java b/src/main/java/duke/task/Event.java similarity index 90% rename from src/main/java/Event.java rename to src/main/java/duke/task/Event.java index 3d512869a..c87593096 100644 --- a/src/main/java/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,16 +1,18 @@ -public class Event extends Task { - - protected String from; - protected String to; - - public Event(String description, String from, String to) { - super(description); - this.from = from; - this.to = to; - } - - @Override - public String toString() { - return "[E]" + super.toString() + "(from: " + from + " to: " + to + ")"; - } -} +package duke.task; + +public class Event extends Task { + + protected String from; + protected String to; + + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + } + + @Override + public String toString() { + return "[E]" + super.toString() + "(from: " + from + " to: " + to + ")"; + } +} diff --git a/src/main/java/Task.java b/src/main/java/duke/task/Task.java similarity index 93% rename from src/main/java/Task.java rename to src/main/java/duke/task/Task.java index bf6b772d9..f01168084 100644 --- a/src/main/java/Task.java +++ b/src/main/java/duke/task/Task.java @@ -1,36 +1,38 @@ -public class Task { - protected String description; - protected boolean isDone; - - public Task(String description) { - this.isDone = false; - this.description = description; - } - - public void markDone() { - isDone = true; - System.out.println("Nice! I've marked this task as done:\n" + this); - } - - public void umarkDone() { - isDone = false; - System.out.println("OK, I've marked this task as not done yet:\n" + this); - } - public String getDescription() { - return description; - } - - public String status(){ - if(isDone) { - return "X"; - } - else { - return " "; - } - } - - @Override - public String toString() { - return "[" + status() + "]" + getDescription(); - } -} +package duke.task; + +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.isDone = false; + this.description = description; + } + + public void markDone() { + isDone = true; + System.out.println("Nice! I've marked this task as done:\n" + this); + } + + public void umarkDone() { + isDone = false; + System.out.println("OK, I've marked this task as not done yet:\n" + this); + } + public String getDescription() { + return description; + } + + public String status(){ + if(isDone) { + return "X"; + } + else { + return " "; + } + } + + @Override + public String toString() { + return "[" + status() + "]" + getDescription(); + } +} diff --git a/src/main/java/Todo.java b/src/main/java/duke/task/Todo.java similarity index 85% rename from src/main/java/Todo.java rename to src/main/java/duke/task/Todo.java index 931cfd4f9..34a684b20 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/duke/task/Todo.java @@ -1,11 +1,13 @@ -public class Todo extends Task { - - public Todo(String description) { - super(description); - } - - @Override - public String toString() { - return "[T]" + super.toString(); - } -} +package duke.task; + +public class Todo extends Task { + + public Todo(String description) { + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} From 6fa95ff45fb6067c0c7376ef0ba673c22ceba073 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Fri, 17 Feb 2023 01:38:31 +0800 Subject: [PATCH 10/22] Add remove of tasks --- src/main/java/Duke.java | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index cb4ae2fe6..355cbb535 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -73,6 +73,16 @@ public static void executeCommand(String input) throws CommandNotFoundException System.out.println("Incomplete description or date of the event\n" + "Please input in the following format: event /from /to "); } printDivider(); + } else if (input.startsWith("delete")) { + printDivider(); + try { + deleteTask(input); + } catch (NumberFormatException e) { + System.out.println("Task to be deleted is not a number."); + } catch (IndexOutOfBoundsException e) { + System.out.println("Task number to be deleted is not within the list"); + } + printDivider(); } else { throw new CommandNotFoundException(); } @@ -108,7 +118,7 @@ public static void printList() { public static void createTodo(String input) { tasks.add(new Todo(input.substring(4).trim())); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + (tasks.size()) + " tasks in the list."); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } public static void createDeadline(String input) throws InvalidInputException { @@ -120,7 +130,7 @@ public static void createDeadline(String input) throws InvalidInputException { } tasks.add(new Deadline(description, deadline)); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + (tasks.size()) + " tasks in the list."); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } public static void createEvent(String input) throws IllegalFormatException, InvalidInputException { @@ -136,9 +146,15 @@ public static void createEvent(String input) throws IllegalFormatException, Inva } tasks.add(new Event(description, eventStart, eventEnd)); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + (tasks.size()) + " tasks in the list."); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); } + public static void deleteTask(String input) { + int deleteIndex = Integer.parseInt(input.substring(6).trim()); + System.out.println("Noted. I've removed this task:\n" + tasks.get(deleteIndex - 1)); + tasks.remove(tasks.get(deleteIndex - 1)); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + } public static void main(String[] args) { String LOGO = " ____ _ \n" From ca05154324bc8cbd1f840d7bceb64c78d93b876a Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Fri, 17 Feb 2023 01:39:04 +0800 Subject: [PATCH 11/22] Add space for readability --- src/main/java/duke/task/Task.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index f01168084..36b0f2bb8 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -33,6 +33,6 @@ public String status(){ @Override public String toString() { - return "[" + status() + "]" + getDescription(); + return "[" + status() + "] " + getDescription(); } } From b07c63e3d31ce8e1b1b411fdb1b0b11b09d9b7d9 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Fri, 17 Feb 2023 18:13:27 +0800 Subject: [PATCH 12/22] Change certain class variables to protected --- src/main/java/Duke.java | 24 ++++++++++++------------ src/main/java/duke/task/Task.java | 6 +++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 355cbb535..557373dd7 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -11,16 +11,16 @@ public class Duke { - public static ArrayListtasks = new ArrayList<>(); + protected static ArrayListtasks = new ArrayList<>(); - public static boolean proceedToNextCommand = true; + protected static boolean proceedToNextCommand = true; - public static void printDivider() { + protected static void printDivider() { String DIVIDER = "____________________________________________________"; System.out.println(DIVIDER); } - public static void executeCommand(String input) throws CommandNotFoundException { + protected static void executeCommand(String input) throws CommandNotFoundException { if (input.equals("bye")) { printExit(); proceedToNextCommand = false; @@ -88,23 +88,23 @@ public static void executeCommand(String input) throws CommandNotFoundException } } - public static void printExit() { + protected static void printExit() { printDivider(); System.out.println("Bye. Hope to see you again soon!"); printDivider(); } - public static void markTask(String input) { + protected static void markTask(String input) { int taskNumber = Integer.parseInt(input.substring(4).trim()) - 1; tasks.get(taskNumber).markDone(); } - public static void unmarkTask(String input) { + protected static void unmarkTask(String input) { int taskNumber = Integer.parseInt(input.substring(6).trim()) - 1; tasks.get(taskNumber).umarkDone(); } - public static void printList() { + protected static void printList() { printDivider(); System.out.println("Here are the tasks in your list:"); int count = 1; @@ -115,13 +115,13 @@ public static void printList() { printDivider(); } - public static void createTodo(String input) { + protected static void createTodo(String input) { tasks.add(new Todo(input.substring(4).trim())); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } - public static void createDeadline(String input) throws InvalidInputException { + protected static void createDeadline(String input) throws InvalidInputException { int byIndex = input.indexOf("/by"); String description = input.substring(8, byIndex).trim(); String deadline = input.substring(byIndex + 3).trim(); @@ -133,7 +133,7 @@ public static void createDeadline(String input) throws InvalidInputException { System.out.println("Now you have " + tasks.size() + " tasks in the list."); } - public static void createEvent(String input) throws IllegalFormatException, InvalidInputException { + protected static void createEvent(String input) throws IllegalFormatException, InvalidInputException { int fromIndex = input.indexOf("/from"); int toIndex = input.indexOf("/to"); String description = input.substring(5, fromIndex).trim(); @@ -149,7 +149,7 @@ public static void createEvent(String input) throws IllegalFormatException, Inva System.out.println("Now you have " + tasks.size() + " tasks in the list."); } - public static void deleteTask(String input) { + protected static void deleteTask(String input) { int deleteIndex = Integer.parseInt(input.substring(6).trim()); System.out.println("Noted. I've removed this task:\n" + tasks.get(deleteIndex - 1)); tasks.remove(tasks.get(deleteIndex - 1)); diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 36b0f2bb8..fb62d1237 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -4,7 +4,7 @@ public class Task { protected String description; protected boolean isDone; - public Task(String description) { + protected Task(String description) { this.isDone = false; this.description = description; } @@ -18,11 +18,11 @@ public void umarkDone() { isDone = false; System.out.println("OK, I've marked this task as not done yet:\n" + this); } - public String getDescription() { + protected String getDescription() { return description; } - public String status(){ + protected String status(){ if(isDone) { return "X"; } From f44576f4149c5bb43beaa7d74e00d1ffea0d8624 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Wed, 8 Mar 2023 09:32:42 +0800 Subject: [PATCH 13/22] Add find task --- src/main/java/Duke.java | 28 ++++++++++++++++++++++++++++ src/main/java/duke/task/Task.java | 4 ++++ 2 files changed, 32 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 557373dd7..30b837be1 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -83,6 +83,16 @@ protected static void executeCommand(String input) throws CommandNotFoundExcepti System.out.println("Task number to be deleted is not within the list"); } printDivider(); + } else if (input.startsWith("find")) { + printDivider(); + try { + findTask(input); + } catch (IllegalFormatException e) { + System.out.println("The keyword of the task cannot be empty."); + } catch (InvalidInputException e) { + System.out.println("No such task found in the list."); + } + printDivider(); } else { throw new CommandNotFoundException(); } @@ -156,6 +166,24 @@ protected static void deleteTask(String input) { System.out.println("Now you have " + tasks.size() + " tasks in the list."); } + protected static void findTask(String input) throws IllegalFormatException, InvalidInputException{ + String keyword = input.substring(4).trim(); + if (keyword.equals("")) { + throw new IllegalFormatException(); + } + System.out.println("Here are the matching tasks in your list:"); + int count = 1; + for (Task task : tasks) { + if (task.findMatch(keyword)) { + System.out.println(count + "." + task); + count++; + } + } + if (count == 1) { + throw new InvalidInputException(); + } + } + public static void main(String[] args) { String LOGO = " ____ _ \n" + "| _ \\ _ _| | _____ \n" diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index fb62d1237..76fabfde8 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -18,6 +18,10 @@ public void umarkDone() { isDone = false; System.out.println("OK, I've marked this task as not done yet:\n" + this); } + + public boolean findMatch(String partialName) { + return description.contains(partialName); + } protected String getDescription() { return description; } From e5fb6f97b223adeb5a56f8b803d34f3095bba4f1 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Fri, 10 Mar 2023 13:54:16 +0800 Subject: [PATCH 14/22] Add JavaDoc --- src/main/java/Duke.java | 74 +++++++++++++++++++++++++++++-- src/main/java/duke/task/Task.java | 21 +++++++++ 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 30b837be1..9557ebee7 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -13,13 +13,23 @@ public class Duke { protected static ArrayListtasks = new ArrayList<>(); + /** + * Represents a boolean that will switch to false upon the "bye" command. + * The loop for command input will continue so long as it is not terminated by this boolean function. + */ protected static boolean proceedToNextCommand = true; - protected static void printDivider() { String DIVIDER = "____________________________________________________"; System.out.println(DIVIDER); } + /** + * This function will determine the command that was given by the user input and execute the + * commands accordingly to its specified usage. + * + * @param input Input string that is provided by user in the CLI. + * @throws CommandNotFoundException if command is not found as specified. + */ protected static void executeCommand(String input) throws CommandNotFoundException { if (input.equals("bye")) { printExit(); @@ -98,22 +108,40 @@ protected static void executeCommand(String input) throws CommandNotFoundExcepti } } + /** + * Represents the function that will print the exit message. + */ protected static void printExit() { printDivider(); System.out.println("Bye. Hope to see you again soon!"); printDivider(); } + /** + * Represents the function that will mark the task as done as according to the task class. + * Task to be marked has to be given by the user in an integer that is within the size of the list of tasks. + * + * @param input Input string that is provided by the user. + */ protected static void markTask(String input) { int taskNumber = Integer.parseInt(input.substring(4).trim()) - 1; tasks.get(taskNumber).markDone(); } + /** + * Represents the function that will mark the task as not done according to the task class. + * Task to be unmarked has to be given by the user in an integer that is within the size of the list of tasks + * + * @param input Input string that is provided by the user. + */ protected static void unmarkTask(String input) { int taskNumber = Integer.parseInt(input.substring(6).trim()) - 1; tasks.get(taskNumber).umarkDone(); } + /** + * Represents the function that will print the tasks within the list. + */ protected static void printList() { printDivider(); System.out.println("Here are the tasks in your list:"); @@ -125,24 +153,47 @@ protected static void printList() { printDivider(); } + /** + * Represents the function that will create as new to do as according to the task class. + * It will also inform users that the task has been successfully added. + * + * @param input Input string that is provided by the user. + */ protected static void createTodo(String input) { tasks.add(new Todo(input.substring(4).trim())); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } + /** + * Represents the function that will create the task of type deadline. + * It requires the user to input both the description and date of the deadline. + * It will also inform users that the deadline task has been successfully added. + * + * @param input Input string that is provided by the user. + * @throws InvalidInputException if either the deadline date or description is not found. + */ protected static void createDeadline(String input) throws InvalidInputException { int byIndex = input.indexOf("/by"); String description = input.substring(8, byIndex).trim(); - String deadline = input.substring(byIndex + 3).trim(); - if (description.length() < 1 | deadline.length() < 1) { + String date = input.substring(byIndex + 3).trim(); + if (description.length() < 1 | date.length() < 1) { throw new InvalidInputException(); } - tasks.add(new Deadline(description, deadline)); + tasks.add(new Deadline(description, date)); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } + /** + * Represents the function that will create the task of type event. + * It requires the user to input the description, event start date and event end date. + * It will also inform users that the event task has been successfully added. + * + * @param input Input string that is provided by the user. + * @throws IllegalFormatException if the order of from has been placed after the to + * @throws InvalidInputException if either the expected description or event start or event end date is not found. + */ protected static void createEvent(String input) throws IllegalFormatException, InvalidInputException { int fromIndex = input.indexOf("/from"); int toIndex = input.indexOf("/to"); @@ -159,6 +210,13 @@ protected static void createEvent(String input) throws IllegalFormatException, I System.out.println("Now you have " + tasks.size() + " tasks in the list."); } + /** + * Represents the function that will remove the task as specified within the list. + * It requires the user to input the task number that is within the size of the list of task. + * It will also inform users that the task has been successfully removed. + * + * @param input Input string that is provided by the user. + */ protected static void deleteTask(String input) { int deleteIndex = Integer.parseInt(input.substring(6).trim()); System.out.println("Noted. I've removed this task:\n" + tasks.get(deleteIndex - 1)); @@ -166,6 +224,14 @@ protected static void deleteTask(String input) { System.out.println("Now you have " + tasks.size() + " tasks in the list."); } + /** + * Represents the function that will help the user search the task that contains the specific keyword + * It will print out the filtered list of task that only contains that specific keyword. + * + * @param input Input string that is provided by the user. + * @throws IllegalFormatException if the task that has to been searched has not been specified by the user. + * @throws InvalidInputException if there is not task left within the list. + */ protected static void findTask(String input) throws IllegalFormatException, InvalidInputException{ String keyword = input.substring(4).trim(); if (keyword.equals("")) { diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 76fabfde8..55bbe1e53 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -4,24 +4,45 @@ public class Task { protected String description; protected boolean isDone; + /** + * Represents the function that will create the new task according to the user input + * and mark the task as not done initially when being created. + * + * @param description Description of the command that was given by the user. + */ protected Task(String description) { this.isDone = false; this.description = description; } + /** + * Represents the function that will mark the task as done. + * It will also inform the user that the task has been successfully marked as done. + */ public void markDone() { isDone = true; System.out.println("Nice! I've marked this task as done:\n" + this); } + /** + * Represents the function that will mark the task as not done. + * It will also inform the user that the task has been successfully unmarked as done. + */ public void umarkDone() { isDone = false; System.out.println("OK, I've marked this task as not done yet:\n" + this); } + /** + * Represents the function that will determine if the keyword is found within the task, + * + * @param partialName Keyword that has to be found within the specified task in the list. + * @return Returns true if the specified task in the list contains the keyword and false otherwise. + */ public boolean findMatch(String partialName) { return description.contains(partialName); } + protected String getDescription() { return description; } From 47689bc2de2118607fd13cc6f0d8d4bcd524859c Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Tue, 14 Mar 2023 00:05:55 +0800 Subject: [PATCH 15/22] Update of JavaDoc --- src/main/java/duke/task/Deadline.java | 6 ++++++ src/main/java/duke/task/Task.java | 12 +++++++++++- src/main/java/duke/task/Todo.java | 6 ++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 4950dbfd3..5361fca98 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -7,6 +7,12 @@ public Deadline(String description, String by) { super(description); this.by = by; } + + /** + * Printing the Task that includes whether it is done + * + * @return Returns a string that describes the object + */ @Override public String toString() { return "[D]" + super.toString() + "(by: " + by + ")"; diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 55bbe1e53..d103ac77a 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -5,7 +5,7 @@ public class Task { protected boolean isDone; /** - * Represents the function that will create the new task according to the user input + * Represents the function that will create a Task object according to the user input * and mark the task as not done initially when being created. * * @param description Description of the command that was given by the user. @@ -47,6 +47,11 @@ protected String getDescription() { return description; } + /** + * Represents the function that will mark the Task as done + * + * @return Returns a string that reflects if the task is done. + */ protected String status(){ if(isDone) { return "X"; @@ -56,6 +61,11 @@ protected String status(){ } } + /** + * Printing the Task that includes whether it is done + * + * @return Returns a string that describes the object + */ @Override public String toString() { return "[" + status() + "] " + getDescription(); diff --git a/src/main/java/duke/task/Todo.java b/src/main/java/duke/task/Todo.java index 34a684b20..72b3e4b43 100644 --- a/src/main/java/duke/task/Todo.java +++ b/src/main/java/duke/task/Todo.java @@ -6,6 +6,12 @@ public Todo(String description) { super(description); } + + /** + * Printing the Task that includes whether it is done + * + * @return Returns a string that describes the object + */ @Override public String toString() { return "[T]" + super.toString(); From 805e73b44388f4814575ac6b97fc6283a8f4b87c Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Tue, 14 Mar 2023 01:11:26 +0800 Subject: [PATCH 16/22] Refactor a bug for empty todo --- src/main/java/Duke.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 9557ebee7..f65bfcb61 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -61,7 +61,7 @@ protected static void executeCommand(String input) throws CommandNotFoundExcepti printDivider(); try { createTodo(input); - } catch (IndexOutOfBoundsException e) { + } catch (InvalidInputException e) { System.out.println("The description of a todo cannot be empty."); } printDivider(); @@ -159,8 +159,12 @@ protected static void printList() { * * @param input Input string that is provided by the user. */ - protected static void createTodo(String input) { - tasks.add(new Todo(input.substring(4).trim())); + protected static void createTodo(String input) throws InvalidInputException { + String description = input.substring(4).trim(); + if (description.length() < 1) { + throw new InvalidInputException(); + } + tasks.add(new Todo(description)); System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); System.out.println("Now you have " + tasks.size() + " tasks in the list."); } From 97208baf14c79eb0dcaf9c047247527fa21b8f1c Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Tue, 14 Mar 2023 01:12:26 +0800 Subject: [PATCH 17/22] Add automated text-ui --- text-ui-test/EXPECTED.TXT | 93 +++++++++++++++++++++++++++++++++++++++ text-ui-test/input.txt | 20 +++++++++ 2 files changed, 113 insertions(+) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..2c0b55423 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -4,4 +4,97 @@ Hello from | | | | | | | |/ / _ \ | |_| | |_| | < __/ |____/ \__,_|_|\_\___| +____________________________________________________ +Hello! I'm Duke +What can I do for you? +____________________________________________________ +____________________________________________________ +Got it. I've added this tasks: +[T][ ] borrow book +Now you have 1 tasks in the list. +____________________________________________________ +____________________________________________________ +Here are the tasks in your list: +1.[T][ ] borrow book +____________________________________________________ +____________________________________________________ +Got it. I've added this tasks: +[D][ ] return book(by: Sunday) +Now you have 2 tasks in the list. +____________________________________________________ +____________________________________________________ +Got it. I've added this tasks: +[E][ ] project meeting(from: Mon 2pm to: 4pm) +Now you have 3 tasks in the list. +____________________________________________________ +____________________________________________________ +Nice! I've marked this task as done: +[T][X] borrow book +____________________________________________________ +____________________________________________________ +Here are the tasks in your list: +1.[T][X] borrow book +2.[D][ ] return book(by: Sunday) +3.[E][ ] project meeting(from: Mon 2pm to: 4pm) +____________________________________________________ +____________________________________________________ +Nice! I've marked this task as done: +[D][X] return book(by: Sunday) +____________________________________________________ +____________________________________________________ +Here are the tasks in your list: +1.[T][X] borrow book +2.[D][X] return book(by: Sunday) +3.[E][ ] project meeting(from: Mon 2pm to: 4pm) +____________________________________________________ +____________________________________________________ +OK, I've marked this task as not done yet: +[T][ ] borrow book +____________________________________________________ +____________________________________________________ +Here are the tasks in your list: +1.[T][ ] borrow book +2.[D][X] return book(by: Sunday) +3.[E][ ] project meeting(from: Mon 2pm to: 4pm) +____________________________________________________ +____________________________________________________ +Nice! I've marked this task as done: +[E][X] project meeting(from: Mon 2pm to: 4pm) +____________________________________________________ +____________________________________________________ +Here are the tasks in your list: +1.[T][ ] borrow book +2.[D][X] return book(by: Sunday) +3.[E][X] project meeting(from: Mon 2pm to: 4pm) +____________________________________________________ +____________________________________________________ +The description of a todo cannot be empty. +____________________________________________________ +____________________________________________________ +Incomplete description or date of the deadline +Please input in the following format: deadline /by +____________________________________________________ +____________________________________________________ +Incomplete description or date of the event +Please input in the following format: event /from /to +____________________________________________________ +____________________________________________________ +Task to be marked is not a number. +____________________________________________________ +____________________________________________________ +Task to be unmarked is not a number. +____________________________________________________ +____________________________________________________ +Noted. I've removed this task: +[E][X] project meeting(from: Mon 2pm to: 4pm) +Now you have 2 tasks in the list. +____________________________________________________ +____________________________________________________ +Here are the tasks in your list: +1.[T][ ] borrow book +2.[D][X] return book(by: Sunday) +____________________________________________________ +____________________________________________________ +Bye. Hope to see you again soon! +____________________________________________________ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..1b65692f3 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,20 @@ +todo borrow book +list +deadline return book /by Sunday +event project meeting /from Mon 2pm /to 4pm +mark 1 +list +mark 2 +list +unmark 1 +list +mark 3 +list +todo +deadline +event +mark +unmark +delete 3 +list +bye From a839b28cb9fcd4e7dc9ed98d30fdab8688de1497 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Tue, 14 Mar 2023 22:10:57 +0800 Subject: [PATCH 18/22] Add user guide --- docs/README.md | 160 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 147 insertions(+), 13 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..98d4b77a3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,163 @@ # User Guide +Duke is an application for managing task list via a **Command Line Interface (CLI)** that helps you to remember the tasks that you do not wish to forget. -## Features ++ [Quick Start](#quick-start) ++ [Features](#features) + + [Create a ToDo task: `todo`](#create-a-todo-task--todo) + + [Create a Deadline task: `deadline`](#create-an-deadline-task--deadline) + + [Create an Event task: `event`](#create-an-event-task--event) + + [Mark a task as done: `mark`](#mark-a-task-as-done--mark) + + [Mark a task as not done: `unmark`](#mark-a-task-as-not-done--unmark) + + [List out the tasks: `list`](#list-out-the-tasks--list) + + [Delete a specific task: `delete`](#delete-a-specific-task--delete) + + [Find a task by keyword: `find`](#find-a-task-by-keyword--find) + + [Exit the application: `bye`](#exit-the-application--bye) -### Feature-ABC +### Quick Start +1. Ensure that you have Java 11 installed. +2. Download the latest duke.jar from [here](). +3. Copy the file to the folder you want your Duke to be in. +4. Open a command terminal, navigate into the folder using `cd` +5. Run the application using `-jar duke.jar` -Description of the feature. +### Features +#### Create a ToDo Task: `todo` +Adds a todo task into the list. -### Feature-XYZ +Format: `todo TASK_NAME` -Description of the feature. +Example: `todo borrow book` -## Usage +Output: +``` +____________________________________________________ +Got it. I've added this tasks: +[T][ ] borrow book +Now you have 1 tasks in the list. +____________________________________________________ +``` +#### Create an Deadline Task: `deadline` +Adds a deadline task into the list that includes the date of the deadline. + +Format: `deadline TASK_NAME /by DATE` -### `Keyword` - Describe action +Example: `deadline return book /by Sunday` + +Output: +``` +____________________________________________________ +Got it. I've added this tasks: +[D][ ] return book(by: Sunday) +Now you have 2 tasks in the list. +____________________________________________________ +``` -Describe the action and its outcome. +#### Create an Event Task: `event` +Adds an event task into the list that include the start and end time of the event. + +Format: `event TASK_NAME /from START_TIME /to END_TIME` + +Example: `event project meeting /from Mon 2pm /to 4pm` + +Output: +``` +____________________________________________________ +Got it. I've added this tasks: +[E][ ] project meeting(from: Mon 2pm to: 4pm) +Now you have 3 tasks in the list. +____________________________________________________ +``` -Example of usage: +#### Mark a task as done: `mark` +Mark an existing task in the list as completed. + +Format: `mark TASK_NUMBER` + +Example: `mark 1` + +Output: +``` +____________________________________________________ +Nice! I've marked this task as done: +[T][X] borrow book +____________________________________________________ +``` + +#### Mark a task as not done: `unmark` +Mark an existing task in the list as not completed. + +Format: `unmark TASK_NUMBER` + +Example: `unmark 1` + +Output: +``` +____________________________________________________ +Nice! I've marked this task as not done yet: +[T][ ] borrow book +____________________________________________________ +``` + +#### List out the tasks: `list` +List out the tasks in the order it was added. + +Format: `list` + +Example: `list` + +Output: +``` +____________________________________________________ +Here are the tasks in your list: +1.[T][ ] borrow book +2.[D][ ] return book(by: Sunday) +3.[E][ ] project meeting(from: Mon 2pm to: 4pm) +____________________________________________________ +``` + + +#### Delete a specific task: `delete` +Remove a specific task by its number in the list. + +Format: `delete TASK_NUMBER` + +Example: `delete 3` + +Output: +``` +____________________________________________________ +Noted. I've removed this task: +[E][ ] project meeting(from: Mon 2pm to: 4pm) +Now you have 2 tasks in the list. +____________________________________________________ +``` + +#### Find a task by keyword: `find` +Find the tasks containing the keyword. + +Format: `find KEYWORD` + +Example: `find book` + +Output: +``` +____________________________________________________ +Here are the matching tasks in your list: +1.[T][ ] borrow book +2.[D][ ] return book(by: Sunday) +____________________________________________________ +``` -`keyword (optional arguments)` +#### Exit the application: `bye` +Exits the application Duke. -Expected outcome: +Format: `bye` -Description of the outcome. +Example: `bye` +Output: ``` -expected output +____________________________________________________ +Bye. Hope to see you again soon! +____________________________________________________ ``` From ca10bdd13eefe9fa107f3a2f2dd531e07cc709f5 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Thu, 16 Mar 2023 09:39:01 +0800 Subject: [PATCH 19/22] Major refactor to include more OOP --- src/main/java/Duke.java | 290 ++---------------- src/main/java/Parser.java | 40 +++ src/main/java/TaskList.java | 174 +++++++++++ src/main/java/UI.java | 91 ++++++ .../duke/exception/MissingInputException.java | 4 + src/main/java/duke/task/Task.java | 25 +- text-ui-test/EXPECTED.TXT | 20 +- 7 files changed, 347 insertions(+), 297 deletions(-) create mode 100644 src/main/java/Parser.java create mode 100644 src/main/java/TaskList.java create mode 100644 src/main/java/UI.java create mode 100644 src/main/java/duke/exception/MissingInputException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f65bfcb61..9e62e1baa 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,281 +1,45 @@ import duke.exception.CommandNotFoundException; +import duke.exception.DukeException; import duke.exception.IllegalFormatException; import duke.exception.InvalidInputException; -import duke.task.Deadline; -import duke.task.Event; -import duke.task.Task; -import duke.task.Todo; - -import java.util.ArrayList; -import java.util.Scanner; +import duke.exception.MissingInputException; public class Duke { - protected static ArrayListtasks = new ArrayList<>(); + protected UI ui = new UI(); + protected TaskList taskList = new TaskList(); + protected Parser parser = new Parser(); - /** - * Represents a boolean that will switch to false upon the "bye" command. - * The loop for command input will continue so long as it is not terminated by this boolean function. - */ - protected static boolean proceedToNextCommand = true; - protected static void printDivider() { - String DIVIDER = "____________________________________________________"; - System.out.println(DIVIDER); - } + public void run() { + ui.printHelloMessage(); + boolean proceedToNextCommand = true; - /** - * This function will determine the command that was given by the user input and execute the - * commands accordingly to its specified usage. - * - * @param input Input string that is provided by user in the CLI. - * @throws CommandNotFoundException if command is not found as specified. - */ - protected static void executeCommand(String input) throws CommandNotFoundException { - if (input.equals("bye")) { - printExit(); - proceedToNextCommand = false; - System.exit(0); - } else if (input.startsWith("mark")) { - printDivider(); - try { - markTask(input); - } catch (NumberFormatException e) { - System.out.println("Task to be marked is not a number."); - } catch (IndexOutOfBoundsException e) { - System.out.println("Task number to be marked is not within the list"); - } - printDivider(); - } else if (input.startsWith("unmark")) { - printDivider(); - try { - unmarkTask(input); - } catch (NumberFormatException e) { - System.out.println("Task to be unmarked is not a number."); - } catch (IndexOutOfBoundsException e) { - System.out.println("Task number to be unmarked is not within the list"); - } - printDivider(); - } else if (input.equals("list")) { - printList(); - } else if (input.startsWith("todo")) { - printDivider(); - try { - createTodo(input); - } catch (InvalidInputException e) { - System.out.println("The description of a todo cannot be empty."); - } - printDivider(); - } else if (input.startsWith("deadline")) { - printDivider(); + while (proceedToNextCommand) { + String userInput = UI.readCommand(); + ui.printDivider(); try { - createDeadline(input); - } catch (IndexOutOfBoundsException | InvalidInputException e) { - System.out.println("Incomplete description or date of the deadline\n" + "Please input in the following format: deadline /by "); - } - printDivider(); - } else if (input.startsWith("event")) { - printDivider(); - try { - createEvent(input); - } catch (IllegalFormatException e) { - System.out.println("The /from cannot go after /to\n" + "Please input in the following format: event /from /to "); - } catch (IndexOutOfBoundsException | InvalidInputException e) { - System.out.println("Incomplete description or date of the event\n" + "Please input in the following format: event /from /to "); - } - printDivider(); - } else if (input.startsWith("delete")) { - printDivider(); - try { - deleteTask(input); - } catch (NumberFormatException e) { - System.out.println("Task to be deleted is not a number."); - } catch (IndexOutOfBoundsException e) { - System.out.println("Task number to be deleted is not within the list"); - } - printDivider(); - } else if (input.startsWith("find")) { - printDivider(); - try { - findTask(input); + proceedToNextCommand = parser.executeCommand(userInput); + if (userInput.equals("bye")) { + break; + } + } catch (CommandNotFoundException e) { + ui.printErrorMessage("I'm sorry, but I don't know what that means."); + } catch (MissingInputException e) { + ui.printErrorMessage("Description of the command is empty."); } catch (IllegalFormatException e) { - System.out.println("The keyword of the task cannot be empty."); + ui.printErrorMessage("Please check the format of command in the user guide."); } catch (InvalidInputException e) { - System.out.println("No such task found in the list."); - } - printDivider(); - } else { - throw new CommandNotFoundException(); - } - } - - /** - * Represents the function that will print the exit message. - */ - protected static void printExit() { - printDivider(); - System.out.println("Bye. Hope to see you again soon!"); - printDivider(); - } - - /** - * Represents the function that will mark the task as done as according to the task class. - * Task to be marked has to be given by the user in an integer that is within the size of the list of tasks. - * - * @param input Input string that is provided by the user. - */ - protected static void markTask(String input) { - int taskNumber = Integer.parseInt(input.substring(4).trim()) - 1; - tasks.get(taskNumber).markDone(); - } - - /** - * Represents the function that will mark the task as not done according to the task class. - * Task to be unmarked has to be given by the user in an integer that is within the size of the list of tasks - * - * @param input Input string that is provided by the user. - */ - protected static void unmarkTask(String input) { - int taskNumber = Integer.parseInt(input.substring(6).trim()) - 1; - tasks.get(taskNumber).umarkDone(); - } - - /** - * Represents the function that will print the tasks within the list. - */ - protected static void printList() { - printDivider(); - System.out.println("Here are the tasks in your list:"); - int count = 1; - for (Task output : tasks) { - System.out.println(count + "." + output); - count++; - } - printDivider(); - } - - /** - * Represents the function that will create as new to do as according to the task class. - * It will also inform users that the task has been successfully added. - * - * @param input Input string that is provided by the user. - */ - protected static void createTodo(String input) throws InvalidInputException { - String description = input.substring(4).trim(); - if (description.length() < 1) { - throw new InvalidInputException(); - } - tasks.add(new Todo(description)); - System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - - /** - * Represents the function that will create the task of type deadline. - * It requires the user to input both the description and date of the deadline. - * It will also inform users that the deadline task has been successfully added. - * - * @param input Input string that is provided by the user. - * @throws InvalidInputException if either the deadline date or description is not found. - */ - protected static void createDeadline(String input) throws InvalidInputException { - int byIndex = input.indexOf("/by"); - String description = input.substring(8, byIndex).trim(); - String date = input.substring(byIndex + 3).trim(); - if (description.length() < 1 | date.length() < 1) { - throw new InvalidInputException(); - } - tasks.add(new Deadline(description, date)); - System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - - /** - * Represents the function that will create the task of type event. - * It requires the user to input the description, event start date and event end date. - * It will also inform users that the event task has been successfully added. - * - * @param input Input string that is provided by the user. - * @throws IllegalFormatException if the order of from has been placed after the to - * @throws InvalidInputException if either the expected description or event start or event end date is not found. - */ - protected static void createEvent(String input) throws IllegalFormatException, InvalidInputException { - int fromIndex = input.indexOf("/from"); - int toIndex = input.indexOf("/to"); - String description = input.substring(5, fromIndex).trim(); - String eventStart = input.substring(fromIndex + 5, toIndex).trim(); - String eventEnd = input.substring(toIndex + 3).trim(); - if (description.length() < 1 | eventStart.length() < 1 | eventEnd.length() < 1) { - throw new InvalidInputException(); - } else if (fromIndex > toIndex) { - throw new IllegalFormatException(); - } - tasks.add(new Event(description, eventStart, eventEnd)); - System.out.println("Got it. I've added this tasks:\n" + tasks.get(tasks.size() - 1)); - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - - /** - * Represents the function that will remove the task as specified within the list. - * It requires the user to input the task number that is within the size of the list of task. - * It will also inform users that the task has been successfully removed. - * - * @param input Input string that is provided by the user. - */ - protected static void deleteTask(String input) { - int deleteIndex = Integer.parseInt(input.substring(6).trim()); - System.out.println("Noted. I've removed this task:\n" + tasks.get(deleteIndex - 1)); - tasks.remove(tasks.get(deleteIndex - 1)); - System.out.println("Now you have " + tasks.size() + " tasks in the list."); - } - - /** - * Represents the function that will help the user search the task that contains the specific keyword - * It will print out the filtered list of task that only contains that specific keyword. - * - * @param input Input string that is provided by the user. - * @throws IllegalFormatException if the task that has to been searched has not been specified by the user. - * @throws InvalidInputException if there is not task left within the list. - */ - protected static void findTask(String input) throws IllegalFormatException, InvalidInputException{ - String keyword = input.substring(4).trim(); - if (keyword.equals("")) { - throw new IllegalFormatException(); - } - System.out.println("Here are the matching tasks in your list:"); - int count = 1; - for (Task task : tasks) { - if (task.findMatch(keyword)) { - System.out.println(count + "." + task); - count++; + ui.printErrorMessage("Please check if the input is correct."); + } catch (NumberFormatException e) { + ui.printErrorMessage("Task number should be an integer."); + } catch (DukeException e) { + ui.printErrorMessage("Error..."); } - } - if (count == 1) { - throw new InvalidInputException(); + ui.printDivider(); } } public static void main(String[] args) { - String LOGO = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - - System.out.println("Hello from\n" + LOGO); - printDivider(); - System.out.println("Hello! I'm Duke\n" + "What can I do for you?\n"); - printDivider(); - - while(proceedToNextCommand) { - Scanner userInput = new Scanner(System.in); - String input = userInput.nextLine(); - try { - executeCommand(input); - } catch (CommandNotFoundException e) { - printDivider(); - System.out.println("I'm sorry, but I don't know what that means =("); - printDivider(); - } - } + new Duke().run(); } } diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 000000000..0def85bce --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,40 @@ +import duke.exception.CommandNotFoundException; +import duke.exception.DukeException; + +public class Parser { + protected TaskList tasklist = new TaskList(); + protected UI ui = new UI(); + + /** + * Interprets the command that was given by the user input and execute the commands accordingly to its specified usage. + * + * @return false if "bye" executed to close the loop and exit the application + * @param userInput Input string that is provided by user in the CLI. + * @throws CommandNotFoundException if command is not found. + */ + public boolean executeCommand(String userInput) throws DukeException { + if (userInput.equals("bye")) { + ui.printByeMessage(); + return false; + } else if (userInput.equals("list")) { + tasklist.showList(); + } else if (userInput.startsWith("todo")) { + tasklist.createTodo(userInput); + } else if (userInput.startsWith("deadline")) { + tasklist.createDeadline(userInput); + } else if (userInput.startsWith("event")) { + tasklist.createEvent(userInput); + } else if (userInput.startsWith("mark")) { + tasklist.markTask(userInput); + } else if (userInput.startsWith("unmark")) { + tasklist.unmarkTask(userInput); + } else if (userInput.startsWith("delete")) { + tasklist.deleteTask(userInput); + } else if (userInput.startsWith("find")) { + tasklist.findTask(userInput); + } else { + throw new CommandNotFoundException(); + } + return true; + } +} diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 000000000..aa9511a50 --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,174 @@ +import duke.exception.IllegalFormatException; +import duke.exception.InvalidInputException; +import duke.exception.MissingInputException; +import duke.task.Deadline; +import duke.task.Event; +import duke.task.Task; +import duke.task.Todo; + +import java.util.ArrayList; + +public class TaskList { + + protected ArrayList tasks = new ArrayList<>(); + + protected UI ui = new UI(); + + protected void showList() { + ui.printList(tasks, tasks.size()); + } + + /** + * Marks the task as completed based on its index. + * + * @param userInput Input string that is provided by the user. + * @throws InvalidInputException when the index provided is not within the task list + * @throws MissingInputException when the index is not provided by the user. + */ + protected void markTask(String userInput) throws InvalidInputException, MissingInputException { + if (userInput.substring(4).trim().equals("")) { + throw new MissingInputException(); + } else { + int taskIndex = Integer.parseInt(userInput.substring(4).trim()); + if (taskIndex == 0 || taskIndex > tasks.size()){ + throw new InvalidInputException(); + } else { + Task markedTask = tasks.get(taskIndex - 1); + markedTask.markDone(); + ui.printTaskMarked(markedTask); + } + } + } + + /** + * Undo the mark on the task based on its index. + * + * @param userInput Input string that is provided by the user. + * @throws InvalidInputException when the index provided is not within the task list. + * @throws MissingInputException when the index is not provided by the user. + */ + protected void unmarkTask(String userInput) throws InvalidInputException, MissingInputException { + if (userInput.substring(6).trim().equals("")) { + throw new MissingInputException(); + } else { + int taskIndex = Integer.parseInt(userInput.substring(6).trim()); + if (taskIndex == 0 || taskIndex > tasks.size()){ + throw new InvalidInputException(); + } else { + Task unmarkedTask = tasks.get(taskIndex - 1); + unmarkedTask.unmarkDone(); + ui.printTaskUnmarked(unmarkedTask); + } + } + } + + + /** + * Creates a Todo task by taking the substring of the word(s) after "todo". + * + * @param userInput Input string that is provided by the user. + * @throws MissingInputException when the description of the task if not provided. + */ + protected void createTodo(String userInput) throws MissingInputException { + if (userInput.substring(4).trim().equals("")) { + throw new MissingInputException(); + } else { + String description = userInput.substring(4).trim(); + Todo todoTask = new Todo(description); + tasks.add(todoTask ); + ui.printTaskAdded(todoTask, tasks.size()); + } + } + + /** + * Creates a deadline task by taking the substring of the words after "deadline" and before "/by". + * The deadline of the task is based on the substring of word(s) after "/by'. + * + * @param userInput Input string that is provided by the user. + * @throws MissingInputException if either the deadline date or description is incomplete. + * @throws InvalidInputException if "/by" is no provided. + */ + protected void createDeadline(String userInput) throws InvalidInputException, MissingInputException { + if ((userInput.substring(8).trim().equals("") || userInput.indexOf("/by") < 8)) { + throw new InvalidInputException(); + } else { + int byIndex = userInput.indexOf("/by"); + String description = userInput.substring(8, byIndex).trim(); + String date = userInput.substring(byIndex + 3).trim(); + if (description.length() < 1 | date.length() < 1) { + throw new MissingInputException(); + } + Deadline deadlineTask = new Deadline(description, date); + tasks.add(deadlineTask); + ui.printTaskAdded(deadlineTask, tasks.size()); + } + } + + /** + * Creates an event task by taking the substring of the words after "event" and before "/from". + * Start time of the event is derived from taking the substring between "/from" and "/to". + * End time of the event is derived from taking the substring after "/to". + * + * @param userInput Input string that is provided by the user. + * @throws IllegalFormatException if the order of "/from" has been placed after "/to". + * @throws InvalidInputException if either the expected description "/from" or "/to" is not provided. + * @throws MissingInputException if either the description, start time or end time is incomplete. + */ + protected void createEvent(String userInput) throws IllegalFormatException, InvalidInputException, MissingInputException { + if (userInput.substring(5).trim().equals("") || userInput.indexOf("/from") < 5 || userInput.indexOf("/to") < 5) { + throw new InvalidInputException(); + } else { + int fromIndex = userInput.indexOf("/from"); + int toIndex = userInput.indexOf("/to"); + String description = userInput.substring(5, fromIndex).trim(); + String eventStart = userInput.substring(fromIndex + 5, toIndex).trim(); + String eventEnd = userInput.substring(toIndex + 3).trim(); + if (description.length() < 1 | eventStart.length() < 1 | eventEnd.length() < 1) { + throw new MissingInputException(); + } else if (fromIndex > toIndex) { + throw new IllegalFormatException(); + } + Event eventTask = new Event(description, eventStart, eventEnd); + tasks.add(eventTask); + ui.printTaskAdded(eventTask, tasks.size()); + } + } + + /** + * Removes the task in the list based on the index provided. + * + * @param userInput Input string that is provided by the user. + * @throws InvalidInputException if the task to be deleted is not within the list + */ + protected void deleteTask(String userInput) throws InvalidInputException { + int deleteIndex = Integer.parseInt(userInput.substring(6).trim()); + if (deleteIndex > tasks.size() || deleteIndex == 0) { + throw new InvalidInputException(); + } else { + Task deleteTask = tasks.get(deleteIndex - 1); + tasks.remove(deleteTask); + ui.printTaskDeleted(deleteTask, tasks.size()); + } + } + + /** + * Finds the tasks in the list that contains the keyword provided and store in a new taskList. + * + * @param userInput Input string that is provided by the user. + * @throws MissingInputException if the keyword is not provided. + */ + protected void findTask(String userInput) throws MissingInputException { + if (userInput.substring(4).trim().equals("")) { + throw new MissingInputException(); + } else { + String keyword = userInput.substring(4).trim(); + TaskList taskList = new TaskList(); + for (Task task : tasks) { + if (task.findMatch(keyword)) { + taskList.tasks.add(task); + } + } + ui.printTaskFound(taskList, taskList.tasks.size()); + } + } +} diff --git a/src/main/java/UI.java b/src/main/java/UI.java new file mode 100644 index 000000000..bf72cec65 --- /dev/null +++ b/src/main/java/UI.java @@ -0,0 +1,91 @@ +import duke.task.Task; +import java.util.ArrayList; +import java.util.Scanner; + +public class UI { + protected final String LOGO = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + protected void printDivider() { + final String DIVIDER = "____________________________________________________"; + System.out.println(DIVIDER); + } + + public void printHelloMessage() { + System.out.println("Hello from\n" + LOGO); + printDivider(); + System.out.println("Hello! I'm Duke\n" + "What can I do for you?\n"); + printDivider(); + } + + public void printByeMessage() { + System.out.println("Bye. Hope to see you again soon!"); + printDivider(); + } + + public static String readCommand() { + Scanner scanner = new Scanner(System.in); + return scanner.nextLine(); + } + + public void printTaskMarked(Task description) { + System.out.println("Nice! I've marked this task as done:\n" + description); + } + + public void printTaskUnmarked(Task description) { + System.out.println("Nice! I've marked this task as not done yet:\n" + description); + } + public void printTaskAdded(Task description, int size) { + System.out.println("Got it. I've added this tasks:\n" + description); + System.out.println("Now you have " + size + " tasks in the list."); + } + + public void printTaskDeleted(Task description, int size) { + System.out.println("Noted. I've removed this task:\n" + description); + System.out.println("Now you have " + size + " tasks in the list."); + } + + /** + * Checks if there is any task present before printing the list of tasks + * or a message that tells the user that there is no task in the list. + * + * @param tasks List of task that has been stored in the array to be printed. + * @param size Size of the array to determine if there is any task to be printed. + */ + public void printList(ArrayList tasks, int size) { + if (size > 0) { + System.out.println("Here are the tasks in your list:"); + int count = 1; + for (Task output : tasks) { + System.out.println(count + "." + output); + count++; + } + } else { + System.out.println("There are no task in the list."); + } + } + + /** + * Checks if there is any task in the list before printing the list of tasks + * or a message that tells the user that there is no task containing that keyword. + * + * @param taskList List of tasks that has been stored if it contains the keyword + * @param size Size of the task list. + */ + public void printTaskFound(TaskList taskList, int size) { + if (size > 0) { + System.out.println("Here are the matching tasks in your list:"); + for (int count = 0; count < size; count++) { + System.out.println(count + "." + taskList.tasks.get(count)); + } + } else { + System.out.println("There are no task in the list containing the keyword."); + } + } + + public void printErrorMessage(String errorMessage) { + System.out.println(errorMessage); + } +} diff --git a/src/main/java/duke/exception/MissingInputException.java b/src/main/java/duke/exception/MissingInputException.java new file mode 100644 index 000000000..a27348daa --- /dev/null +++ b/src/main/java/duke/exception/MissingInputException.java @@ -0,0 +1,4 @@ +package duke.exception; + +public class MissingInputException extends DukeException{ +} diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index d103ac77a..fb2d672d5 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -4,37 +4,21 @@ public class Task { protected String description; protected boolean isDone; - /** - * Represents the function that will create a Task object according to the user input - * and mark the task as not done initially when being created. - * - * @param description Description of the command that was given by the user. - */ protected Task(String description) { this.isDone = false; this.description = description; } - /** - * Represents the function that will mark the task as done. - * It will also inform the user that the task has been successfully marked as done. - */ public void markDone() { isDone = true; - System.out.println("Nice! I've marked this task as done:\n" + this); } - /** - * Represents the function that will mark the task as not done. - * It will also inform the user that the task has been successfully unmarked as done. - */ - public void umarkDone() { + public void unmarkDone() { isDone = false; - System.out.println("OK, I've marked this task as not done yet:\n" + this); } /** - * Represents the function that will determine if the keyword is found within the task, + * Determine if the keyword is found within the task. * * @param partialName Keyword that has to be found within the specified task in the list. * @return Returns true if the specified task in the list contains the keyword and false otherwise. @@ -47,11 +31,6 @@ protected String getDescription() { return description; } - /** - * Represents the function that will mark the Task as done - * - * @return Returns a string that reflects if the task is done. - */ protected String status(){ if(isDone) { return "X"; diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 2c0b55423..d1469520d 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,9 +1,9 @@ -Hello from - ____ _ -| _ \ _ _| | _____ + ____ _ +| _ \ _ _| | _____ | | | | | | | |/ / _ \ | |_| | |_| | < __/ |____/ \__,_|_|\_\___| + ____________________________________________________ Hello! I'm Duke What can I do for you? @@ -49,7 +49,7 @@ Here are the tasks in your list: 3.[E][ ] project meeting(from: Mon 2pm to: 4pm) ____________________________________________________ ____________________________________________________ -OK, I've marked this task as not done yet: +Nice! I've marked this task as not done yet: [T][ ] borrow book ____________________________________________________ ____________________________________________________ @@ -69,21 +69,19 @@ Here are the tasks in your list: 3.[E][X] project meeting(from: Mon 2pm to: 4pm) ____________________________________________________ ____________________________________________________ -The description of a todo cannot be empty. +Description of the command is empty. ____________________________________________________ ____________________________________________________ -Incomplete description or date of the deadline -Please input in the following format: deadline /by +Description of the command is empty. ____________________________________________________ ____________________________________________________ -Incomplete description or date of the event -Please input in the following format: event /from /to +Description of the command is empty. ____________________________________________________ ____________________________________________________ -Task to be marked is not a number. +Description of the command is empty. ____________________________________________________ ____________________________________________________ -Task to be unmarked is not a number. +Description of the command is empty. ____________________________________________________ ____________________________________________________ Noted. I've removed this task: From 62dee311f6e7ff20672485da736d367dd8b90d9b Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Sat, 18 Mar 2023 16:13:38 +0800 Subject: [PATCH 20/22] Add save file feature. --- src/main/java/Data.java | 73 +++++++++++++++++++++++++++ src/main/java/Duke.java | 22 +++++++- src/main/java/Parser.java | 6 ++- src/main/java/TaskList.java | 49 +++++++++++++++++- src/main/java/duke/task/Deadline.java | 5 ++ src/main/java/duke/task/Event.java | 5 ++ src/main/java/duke/task/Task.java | 17 ++++++- src/main/java/duke/task/Todo.java | 5 ++ 8 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 src/main/java/Data.java diff --git a/src/main/java/Data.java b/src/main/java/Data.java new file mode 100644 index 000000000..7efe27819 --- /dev/null +++ b/src/main/java/Data.java @@ -0,0 +1,73 @@ +import duke.task.Task; + +import java.io.File; +import java.io.FileWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; +import java.io.IOException; + +public class Data { + + protected String filePath; + protected String fileDirectory; + + + public Data(String filePath, String fileDirectory) { + this.filePath = filePath; + this.fileDirectory = fileDirectory; + } + + /** + * Checks if the directory exists and creates one if it does not. + * Then it will check if the file exists and creates one if it does not. + * + * @throws IOException if there is an error in creating the directory or file. + */ + public void checkFile() throws IOException { + File file = new File(filePath); + File directory = new File(fileDirectory); + if (!directory.isDirectory()) { + Path path = Paths.get(String.valueOf(directory)); + Files.createDirectories(path); + } + if (!file.isFile()) { + Files.createFile(Path.of(filePath)); + } + } + /** + * Reads in the file duke.txt and categorise into its respective task: Todo, Deadline or Event. + * + * @param taskList taskList contains the functions to read and create the tasks correspondingly + * @throws IOException if there is and error when reading in the file. + */ + public void readFile(TaskList taskList) throws IOException { + File file = new File(filePath); + Scanner scanner = new Scanner(file); + while (scanner.hasNextLine()) { + String input = scanner.nextLine(); + String[] substring = input.split("-|\\|"); + if (substring[0].trim().equals("T")) { + taskList.readTodoTask(substring[1].trim(), substring[2].trim()); + } else if (substring[0].trim().equals("D")) { + taskList.readDeadlineTask(substring[1].trim(), substring[2].trim(), substring[3].trim()); + } else if (substring[0].trim().equals("E")) { + taskList.readEventTask(substring[1].trim(), substring[2].trim(), substring[3].trim(), substring[4].trim()); + } + } + } + + /** + * Writes the updated of tasks in the ArrayList back into the file duke.txt + * + * @throws IOException if there is an error in writing the file. + */ + public void writeFile() throws IOException { + FileWriter fileWriter = new FileWriter(filePath); + for (Task task : TaskList.tasks) { + fileWriter.write(task.saveToFile()); + } + fileWriter.close(); + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 9e62e1baa..f4e3f6243 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -4,22 +4,38 @@ import duke.exception.InvalidInputException; import duke.exception.MissingInputException; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + public class Duke { protected UI ui = new UI(); protected TaskList taskList = new TaskList(); protected Parser parser = new Parser(); + protected Data data = new Data("data/duke.txt", "data"); - public void run() { + public void run(){ ui.printHelloMessage(); boolean proceedToNextCommand = true; - + try { + data.readFile(taskList); + } catch (FileNotFoundException e) { + try { + data.checkFile(); + } catch (IOException checkException) { + ui.printErrorMessage("File not found. I will be creating one for you."); + } + } catch (IOException readException) { + ui.printErrorMessage("I'm sorry, but there is an error in reading the file."); + } while (proceedToNextCommand) { String userInput = UI.readCommand(); ui.printDivider(); try { proceedToNextCommand = parser.executeCommand(userInput); if (userInput.equals("bye")) { + data.writeFile(); break; } } catch (CommandNotFoundException e) { @@ -32,6 +48,8 @@ public void run() { ui.printErrorMessage("Please check if the input is correct."); } catch (NumberFormatException e) { ui.printErrorMessage("Task number should be an integer."); + } catch (IOException e) { + ui.printErrorMessage("I'm sorry, but there is an error in writing the file."); } catch (DukeException e) { ui.printErrorMessage("Error..."); } diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 0def85bce..b6c4963f1 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -1,10 +1,14 @@ import duke.exception.CommandNotFoundException; import duke.exception.DukeException; +import java.io.IOException; + public class Parser { protected TaskList tasklist = new TaskList(); protected UI ui = new UI(); + protected Data data = new Data("data/duke.txt", "data"); + /** * Interprets the command that was given by the user input and execute the commands accordingly to its specified usage. * @@ -16,7 +20,7 @@ public boolean executeCommand(String userInput) throws DukeException { if (userInput.equals("bye")) { ui.printByeMessage(); return false; - } else if (userInput.equals("list")) { + } else if (userInput.equals("list")) { tasklist.showList(); } else if (userInput.startsWith("todo")) { tasklist.createTodo(userInput); diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index aa9511a50..509a0abf4 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -10,7 +10,7 @@ public class TaskList { - protected ArrayList tasks = new ArrayList<>(); + public static ArrayList tasks = new ArrayList<>(); protected UI ui = new UI(); @@ -75,7 +75,7 @@ protected void createTodo(String userInput) throws MissingInputException { } else { String description = userInput.substring(4).trim(); Todo todoTask = new Todo(description); - tasks.add(todoTask ); + tasks.add(todoTask); ui.printTaskAdded(todoTask, tasks.size()); } } @@ -171,4 +171,49 @@ protected void findTask(String userInput) throws MissingInputException { ui.printTaskFound(taskList, taskList.tasks.size()); } } + + /** + * Creates a Todo task based on the file being read in duke.txt + * + * @param taskStatus Input string of either "0" or "1" to determine if the task is done. + * @param taskDescription Input string that describes the task. + */ + public void readTodoTask(String taskStatus, String taskDescription) { + Todo existingTask = new Todo(taskDescription); + if (taskStatus.equals("1")) { + existingTask.markDone(); + } + tasks.add(existingTask); + } + + /** + * Creates a Deadline task based on the file being read in duke.txt + * + * @param taskStatus Input string of either "0" or "1" to determine if the task is done. + * @param taskDescription Input string that describes the task. + * @param byDate Input string that contains the date of the deadline task. + */ + public void readDeadlineTask(String taskStatus, String taskDescription, String byDate) { + Deadline existingTask = new Deadline(taskDescription, byDate); + if (taskStatus.equals("1")) { + existingTask.markDone(); + } + tasks.add(existingTask); + } + + /** + * Create an Event task based on the file being read in duke.txt + * + * @param taskStatus Input string of either "0" or "1" to determine if the task is done. + * @param taskDescription Input string that describes the task. + * @param fromDate Input string that contains the start date of the Event task. + * @param toDate Input string that contains the end date of the Event task. + */ + public void readEventTask(String taskStatus, String taskDescription, String fromDate, String toDate) { + Event existingTask = new Event(taskDescription, fromDate, toDate); + if (taskStatus.equals("1")) { + existingTask.markDone(); + } + tasks.add(existingTask); + } } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 5361fca98..98b1eba4f 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -17,4 +17,9 @@ public Deadline(String description, String by) { public String toString() { return "[D]" + super.toString() + "(by: " + by + ")"; } + + @Override + public String saveToFile() { + return "D | " + this.getStatus() + " | " + description + " | " + by + "\n"; + } } diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index c87593096..162ef3fc0 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -15,4 +15,9 @@ public Event(String description, String from, String to) { public String toString() { return "[E]" + super.toString() + "(from: " + from + " to: " + to + ")"; } + + @Override + public String saveToFile() { + return "E | " + this.getStatus() + " | " + description + " | " + from + "-" + to + "\n"; + } } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index fb2d672d5..f9f09f017 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -17,6 +17,7 @@ public void unmarkDone() { isDone = false; } + /** * Determine if the keyword is found within the task. * @@ -31,7 +32,7 @@ protected String getDescription() { return description; } - protected String status(){ + public String status(){ if(isDone) { return "X"; } @@ -40,6 +41,19 @@ protected String status(){ } } + public String getStatus(){ + if(isDone) { + return "1"; + } + else { + return "O"; + } + } + + public String saveToFile() { + return ""; + } + /** * Printing the Task that includes whether it is done * @@ -49,4 +63,5 @@ protected String status(){ public String toString() { return "[" + status() + "] " + getDescription(); } + } diff --git a/src/main/java/duke/task/Todo.java b/src/main/java/duke/task/Todo.java index 72b3e4b43..a5baf6851 100644 --- a/src/main/java/duke/task/Todo.java +++ b/src/main/java/duke/task/Todo.java @@ -16,4 +16,9 @@ public Todo(String description) { public String toString() { return "[T]" + super.toString(); } + + @Override + public String saveToFile() { + return "T | " + this.getStatus() + " | " + description + "\n"; + } } From 98a859b677546b1313957a22ccfca9a03f3e8b0b Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Sat, 18 Mar 2023 16:46:13 +0800 Subject: [PATCH 21/22] Minor refactoring of spelling --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 98d4b77a3..91854ce50 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,7 +4,7 @@ Duke is an application for managing task list via a **Command Line Interface (CL + [Quick Start](#quick-start) + [Features](#features) + [Create a ToDo task: `todo`](#create-a-todo-task--todo) - + [Create a Deadline task: `deadline`](#create-an-deadline-task--deadline) + + [Create a Deadline task: `deadline`](#create-a-deadline-task--deadline) + [Create an Event task: `event`](#create-an-event-task--event) + [Mark a task as done: `mark`](#mark-a-task-as-done--mark) + [Mark a task as not done: `unmark`](#mark-a-task-as-not-done--unmark) @@ -36,7 +36,7 @@ Got it. I've added this tasks: Now you have 1 tasks in the list. ____________________________________________________ ``` -#### Create an Deadline Task: `deadline` +#### Create a Deadline Task: `deadline` Adds a deadline task into the list that includes the date of the deadline. Format: `deadline TASK_NAME /by DATE` From eed248b025e28af27751d89468a9214c065fafe4 Mon Sep 17 00:00:00 2001 From: Sebastian Soewanto Date: Sat, 18 Mar 2023 17:53:33 +0800 Subject: [PATCH 22/22] Fix bug in find feature --- src/main/java/Duke.java | 1 - src/main/java/TaskList.java | 6 +++--- src/main/java/UI.java | 6 +++--- src/main/java/duke/task/Task.java | 1 - 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f4e3f6243..990cc4843 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -4,7 +4,6 @@ import duke.exception.InvalidInputException; import duke.exception.MissingInputException; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index 509a0abf4..a3791264f 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -162,13 +162,13 @@ protected void findTask(String userInput) throws MissingInputException { throw new MissingInputException(); } else { String keyword = userInput.substring(4).trim(); - TaskList taskList = new TaskList(); + ArrayList matchingTasks = new ArrayList<>(); for (Task task : tasks) { if (task.findMatch(keyword)) { - taskList.tasks.add(task); + matchingTasks.add(task); } } - ui.printTaskFound(taskList, taskList.tasks.size()); + ui.printTaskFound(matchingTasks, matchingTasks.size()); } } diff --git a/src/main/java/UI.java b/src/main/java/UI.java index bf72cec65..0fcb47c92 100644 --- a/src/main/java/UI.java +++ b/src/main/java/UI.java @@ -71,14 +71,14 @@ public void printList(ArrayList tasks, int size) { * Checks if there is any task in the list before printing the list of tasks * or a message that tells the user that there is no task containing that keyword. * - * @param taskList List of tasks that has been stored if it contains the keyword + * @param matchingTasks List of tasks that has been stored if it contains the keyword * @param size Size of the task list. */ - public void printTaskFound(TaskList taskList, int size) { + public void printTaskFound(ArrayList matchingTasks, int size) { if (size > 0) { System.out.println("Here are the matching tasks in your list:"); for (int count = 0; count < size; count++) { - System.out.println(count + "." + taskList.tasks.get(count)); + System.out.println(count + 1 + "." + matchingTasks.get(count)); } } else { System.out.println("There are no task in the list containing the keyword."); diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index f9f09f017..2561e265b 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -17,7 +17,6 @@ public void unmarkDone() { isDone = false; } - /** * Determine if the keyword is found within the task. *