From 948e367446d5263a0d3f1d8f5574add9bb967767 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Thu, 2 Feb 2023 00:38:32 +0800 Subject: [PATCH 01/21] Duke echoes what the user says --- src/main/java/Duke.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..e0402a544 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,7 @@ +import java.util.Scanner; + public class Duke { + public static String DIVIDER_LINE = "______________________________\n"; public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -6,5 +9,22 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + String greet = DIVIDER_LINE + + "Hello! I'm Duke\n" + + "What can i do for you\n" + + DIVIDER_LINE; + System.out.println(greet); + boolean shouldContinue = true; + Scanner in = new Scanner(System.in); + String echo; + while (shouldContinue) { + echo = in.nextLine(); + if (echo.equals("bye")) { + System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); + shouldContinue = false; + } else { + System.out.println(DIVIDER_LINE + echo + "\n" + DIVIDER_LINE); + } + } } } From ed23c9249224a0f62dc89c6831a81c19efd4e841 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Thu, 2 Feb 2023 02:44:10 +0800 Subject: [PATCH 02/21] add and list tasks --- src/main/java/Duke.java | 19 +++++++++++++++---- src/main/java/Task.java | 2 ++ 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index e0402a544..4dea39ec4 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,6 +2,7 @@ public class Duke { public static String DIVIDER_LINE = "______________________________\n"; + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -16,14 +17,24 @@ public static void main(String[] args) { System.out.println(greet); boolean shouldContinue = true; Scanner in = new Scanner(System.in); - String echo; + String[] tasks = new String[100]; + int taskCount = 0; + String action; while (shouldContinue) { - echo = in.nextLine(); - if (echo.equals("bye")) { + action = in.nextLine(); + if (action.equals("bye")) { System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); shouldContinue = false; + } else if (action.equals("list")) { + System.out.print(DIVIDER_LINE); + for (int i = 0; i < taskCount; i += 1) { + System.out.println(Integer.toString(i + 1) + ". " + tasks[i]); + } + System.out.println(DIVIDER_LINE); } else { - System.out.println(DIVIDER_LINE + echo + "\n" + DIVIDER_LINE); + tasks[taskCount] = action; + taskCount += 1; + System.out.println(DIVIDER_LINE + "added: " + action + "\n" + DIVIDER_LINE); } } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..4ec445449 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class Task { +} From 3443a1a05a9f4bd5a1b55ccb22126513d09ea9f3 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Thu, 2 Feb 2023 03:18:40 +0800 Subject: [PATCH 03/21] added "mark as done or undone" --- src/main/java/Duke.java | 26 +++++++++++++++++++++----- src/main/java/Task.java | 23 ++++++++++++++++++++++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4dea39ec4..2d1f3ab30 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -17,22 +17,38 @@ public static void main(String[] args) { System.out.println(greet); boolean shouldContinue = true; Scanner in = new Scanner(System.in); - String[] tasks = new String[100]; + //String[] tasks = new String[100]; int taskCount = 0; String action; + Task[] tasks = new Task[100]; while (shouldContinue) { action = in.nextLine(); if (action.equals("bye")) { System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); shouldContinue = false; - } else if (action.equals("list")) { + }else if (action.equals("list")) { System.out.print(DIVIDER_LINE); for (int i = 0; i < taskCount; i += 1) { - System.out.println(Integer.toString(i + 1) + ". " + tasks[i]); + System.out.println(Integer.toString(i + 1) + "." + "" +tasks[i].getStatusIcon() + + " " +tasks[i].description); } System.out.println(DIVIDER_LINE); - } else { - tasks[taskCount] = action; + }else if (action.startsWith("mark")){ + int dividerPos = action.indexOf(" "); + int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; + tasks[toBeMarked].markAsDone(); + System.out.println(DIVIDER_LINE + "Nice! I've marked this task as done:\n" + + tasks[toBeMarked].getStatusIcon() + " " + tasks[toBeMarked].description + + "\n" + DIVIDER_LINE); + }else if (action.startsWith("unmark")) { + int dividerPos = action.indexOf(" "); + int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; + tasks[toBeUnmarked].markAsUndone(); + System.out.println(DIVIDER_LINE + "Nice! I've marked this task as undone:\n" + + tasks[toBeUnmarked].getStatusIcon() + " " + tasks[toBeUnmarked].description + + "\n" + DIVIDER_LINE); + }else { + tasks[taskCount] = new Task(action); taskCount += 1; System.out.println(DIVIDER_LINE + "added: " + action + "\n" + DIVIDER_LINE); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 4ec445449..604bf49a9 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,2 +1,23 @@ -package PACKAGE_NAME;public class Task { +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "[X]" : "[ ]"); // mark done task with X + } + + public void markAsDone() { + this.isDone = true; + } + + public void markAsUndone() { + this.isDone = false; + } + + //... } From e45b3f9aa58ee6d447856c65a86c3daf5472dee8 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Wed, 8 Feb 2023 16:17:09 +0800 Subject: [PATCH 04/21] added level4 with deadline, todo and event --- src/main/java/Deadline.java | 18 ++++++++++++++++++ src/main/java/Duke.java | 34 ++++++++++++++++++++++++++-------- src/main/java/Event.java | 21 +++++++++++++++++++++ src/main/java/Task.java | 9 ++++++++- 4 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..0fca15644 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,18 @@ +public class Deadline extends Task{ + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String getTypeIcon() { + return "[D]"; + } + + @Override + public String toString(){ + return getTypeIcon() + super.getStatusIcon() +super.description + " (by: " + this.by + ")"; + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 2d1f3ab30..daf27a1e3 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -17,7 +17,6 @@ public static void main(String[] args) { System.out.println(greet); boolean shouldContinue = true; Scanner in = new Scanner(System.in); - //String[] tasks = new String[100]; int taskCount = 0; String action; Task[] tasks = new Task[100]; @@ -29,8 +28,7 @@ public static void main(String[] args) { }else if (action.equals("list")) { System.out.print(DIVIDER_LINE); for (int i = 0; i < taskCount; i += 1) { - System.out.println(Integer.toString(i + 1) + "." + "" +tasks[i].getStatusIcon() - + " " +tasks[i].description); + System.out.println(Integer.toString(i + 1) + ". " +tasks[i].toString()); } System.out.println(DIVIDER_LINE); }else if (action.startsWith("mark")){ @@ -38,8 +36,7 @@ public static void main(String[] args) { int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; tasks[toBeMarked].markAsDone(); System.out.println(DIVIDER_LINE + "Nice! I've marked this task as done:\n" - + tasks[toBeMarked].getStatusIcon() + " " + tasks[toBeMarked].description - + "\n" + DIVIDER_LINE); + + tasks[toBeMarked].toString() + "\n" + DIVIDER_LINE); }else if (action.startsWith("unmark")) { int dividerPos = action.indexOf(" "); int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; @@ -47,11 +44,32 @@ public static void main(String[] args) { System.out.println(DIVIDER_LINE + "Nice! I've marked this task as undone:\n" + tasks[toBeUnmarked].getStatusIcon() + " " + tasks[toBeUnmarked].description + "\n" + DIVIDER_LINE); - }else { - tasks[taskCount] = new Task(action); + }else if (action.startsWith("todo")){ + tasks[taskCount] = new Task(action.substring(5)); + System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + taskCount += 1; + printNumTask(taskCount); + }else if (action.startsWith("deadline")) { + int dividerPosition = action.indexOf("/by"); + tasks[taskCount] = new Deadline(action.substring(9,dividerPosition - 1), + action.substring(dividerPosition + 4)); + System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + taskCount += 1; + printNumTask(taskCount); + }else if (action.startsWith("event")) { + int dividerPosition1 = action.indexOf("/from"); + int dividerPosition2 = action.indexOf("/to"); + tasks[taskCount] = new Event(action.substring(6,dividerPosition1 - 1), + action.substring(dividerPosition1 + 6, dividerPosition2 - 1), + action.substring(dividerPosition2 + 4)); + System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); taskCount += 1; - System.out.println(DIVIDER_LINE + "added: " + action + "\n" + DIVIDER_LINE); + printNumTask(taskCount); } } } + + public static void printNumTask(int taskCount) { + System.out.println("Now you have " + taskCount + " tasks in the list"); + } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..9dbd63ea4 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,21 @@ +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 getTypeIcon() { + return "[E]"; + } + + @Override + public String toString(){ + return getTypeIcon() + super.getStatusIcon() + super.description + + " (from: " + this.from + " to: " + this.to + ")"; + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 604bf49a9..04904f02c 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -11,6 +11,10 @@ public String getStatusIcon() { return (isDone ? "[X]" : "[ ]"); // mark done task with X } + public String getTypeIcon() { + return "[T]"; + } + public void markAsDone() { this.isDone = true; } @@ -19,5 +23,8 @@ public void markAsUndone() { this.isDone = false; } - //... + public String toString() { + return getTypeIcon() + getStatusIcon() + description; + } + } From 266f1af741c9e1f0d44367c5b080b1c8e1aa161c Mon Sep 17 00:00:00 2001 From: sunkyan Date: Mon, 13 Feb 2023 16:53:01 +0800 Subject: [PATCH 05/21] handled exceptions --- src/main/java/Duke.java | 105 ++++++++++++++++++------------- src/main/java/DukeException.java | 4 ++ 2 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index daf27a1e3..03d5c2440 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,6 +2,8 @@ public class Duke { public static String DIVIDER_LINE = "______________________________\n"; + protected static boolean shouldContinue = true; + protected static int taskCount = 0; public static void main(String[] args) { String logo = " ____ _ \n" @@ -15,60 +17,73 @@ public static void main(String[] args) { + "What can i do for you\n" + DIVIDER_LINE; System.out.println(greet); - boolean shouldContinue = true; Scanner in = new Scanner(System.in); - int taskCount = 0; String action; Task[] tasks = new Task[100]; while (shouldContinue) { action = in.nextLine(); - if (action.equals("bye")) { - System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); - shouldContinue = false; - }else if (action.equals("list")) { - System.out.print(DIVIDER_LINE); - for (int i = 0; i < taskCount; i += 1) { - System.out.println(Integer.toString(i + 1) + ". " +tasks[i].toString()); - } - System.out.println(DIVIDER_LINE); - }else if (action.startsWith("mark")){ - int dividerPos = action.indexOf(" "); - int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; - tasks[toBeMarked].markAsDone(); - System.out.println(DIVIDER_LINE + "Nice! I've marked this task as done:\n" - + tasks[toBeMarked].toString() + "\n" + DIVIDER_LINE); - }else if (action.startsWith("unmark")) { - int dividerPos = action.indexOf(" "); - int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; - tasks[toBeUnmarked].markAsUndone(); - System.out.println(DIVIDER_LINE + "Nice! I've marked this task as undone:\n" - + tasks[toBeUnmarked].getStatusIcon() + " " + tasks[toBeUnmarked].description - + "\n" + DIVIDER_LINE); - }else if (action.startsWith("todo")){ - tasks[taskCount] = new Task(action.substring(5)); - System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); - taskCount += 1; - printNumTask(taskCount); - }else if (action.startsWith("deadline")) { - int dividerPosition = action.indexOf("/by"); - tasks[taskCount] = new Deadline(action.substring(9,dividerPosition - 1), - action.substring(dividerPosition + 4)); - System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); - taskCount += 1; - printNumTask(taskCount); - }else if (action.startsWith("event")) { - int dividerPosition1 = action.indexOf("/from"); - int dividerPosition2 = action.indexOf("/to"); - tasks[taskCount] = new Event(action.substring(6,dividerPosition1 - 1), - action.substring(dividerPosition1 + 6, dividerPosition2 - 1), - action.substring(dividerPosition2 + 4)); - System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); - taskCount += 1; - printNumTask(taskCount); + try { + tasks = handleAction(tasks, action); + } catch (DukeException e) { + System.out.println(DIVIDER_LINE + "Sorry! I don't know what that means.\n" + DIVIDER_LINE); + } catch (DukeException.TaskEmpty e) { + System.out.println(DIVIDER_LINE + "The content of the task cannot be empty!\n" + DIVIDER_LINE); } } } + public static Task[] handleAction(Task[] tasks, String action) throws DukeException, DukeException.TaskEmpty{ + if (action.equals("bye")) { + System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); + shouldContinue = false; + }else if (action.equals("list")) { + System.out.print(DIVIDER_LINE); + for (int i = 0; i < taskCount; i += 1) { + System.out.println(Integer.toString(i + 1) + ". " +tasks[i].toString()); + } + System.out.println(DIVIDER_LINE); + }else if (action.equals("todo") || action.equals("deadline") || action.equals("event")) { + throw new DukeException.TaskEmpty(); + }else if (action.startsWith("mark")){ + int dividerPos = action.indexOf(" "); + int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; + tasks[toBeMarked].markAsDone(); + System.out.println(DIVIDER_LINE + "Nice! I've marked this task as done:\n" + + tasks[toBeMarked].toString() + "\n" + DIVIDER_LINE); + }else if (action.startsWith("unmark")) { + int dividerPos = action.indexOf(" "); + int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; + tasks[toBeUnmarked].markAsUndone(); + System.out.println(DIVIDER_LINE + "Nice! I've marked this task as undone:\n" + + tasks[toBeUnmarked].getStatusIcon() + " " + tasks[toBeUnmarked].description + + "\n" + DIVIDER_LINE); + }else if (action.startsWith("todo")){ + tasks[taskCount] = new Task(action.substring(5)); + System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + taskCount += 1; + printNumTask(taskCount); + }else if (action.startsWith("deadline")) { + int dividerPosition = action.indexOf("/by"); + tasks[taskCount] = new Deadline(action.substring(9,dividerPosition - 1), + action.substring(dividerPosition + 4)); + System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + taskCount += 1; + printNumTask(taskCount); + }else if (action.startsWith("event")) { + int dividerPosition1 = action.indexOf("/from"); + int dividerPosition2 = action.indexOf("/to"); + tasks[taskCount] = new Event(action.substring(6,dividerPosition1 - 1), + action.substring(dividerPosition1 + 6, dividerPosition2 - 1), + action.substring(dividerPosition2 + 4)); + System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + taskCount += 1; + printNumTask(taskCount); + }else { + throw new DukeException(); + } + return tasks; + } + public static void printNumTask(int taskCount) { System.out.println("Now you have " + taskCount + " tasks in the list"); } diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..d1dc0a831 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,4 @@ +public class DukeException extends Exception{ + public static class TaskEmpty extends Exception{} + +} From 348e162b61515a234f4fdc1cfd9c93abe4100e31 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Mon, 13 Feb 2023 16:54:45 +0800 Subject: [PATCH 06/21] used exception --- src/main/java/DukeException.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java index d1dc0a831..fddfd7062 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/DukeException.java @@ -1,4 +1,3 @@ public class DukeException extends Exception{ public static class TaskEmpty extends Exception{} - } From 7a661ffef125e266dbf97a168c3d763817779a57 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Mon, 13 Feb 2023 16:59:30 +0800 Subject: [PATCH 07/21] added a package --- src/main/java/{ => duke}/Deadline.java | 2 ++ src/main/java/{ => duke}/Duke.java | 2 ++ src/main/java/{ => duke}/DukeException.java | 2 ++ src/main/java/{ => duke}/Event.java | 2 ++ src/main/java/{ => duke}/Task.java | 2 ++ 5 files changed, 10 insertions(+) rename src/main/java/{ => duke}/Deadline.java (96%) rename src/main/java/{ => duke}/Duke.java (99%) rename src/main/java/{ => duke}/DukeException.java (87%) rename src/main/java/{ => duke}/Event.java (97%) rename src/main/java/{ => duke}/Task.java (97%) diff --git a/src/main/java/Deadline.java b/src/main/java/duke/Deadline.java similarity index 96% rename from src/main/java/Deadline.java rename to src/main/java/duke/Deadline.java index 0fca15644..66640714a 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/duke/Deadline.java @@ -1,3 +1,5 @@ +package duke; + public class Deadline extends Task{ protected String by; diff --git a/src/main/java/Duke.java b/src/main/java/duke/Duke.java similarity index 99% rename from src/main/java/Duke.java rename to src/main/java/duke/Duke.java index 03d5c2440..cfe7c88db 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,3 +1,5 @@ +package duke; + import java.util.Scanner; public class Duke { diff --git a/src/main/java/DukeException.java b/src/main/java/duke/DukeException.java similarity index 87% rename from src/main/java/DukeException.java rename to src/main/java/duke/DukeException.java index fddfd7062..806d7b487 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/duke/DukeException.java @@ -1,3 +1,5 @@ +package duke; + public class DukeException extends Exception{ public static class TaskEmpty extends Exception{} } diff --git a/src/main/java/Event.java b/src/main/java/duke/Event.java similarity index 97% rename from src/main/java/Event.java rename to src/main/java/duke/Event.java index 9dbd63ea4..1088449d0 100644 --- a/src/main/java/Event.java +++ b/src/main/java/duke/Event.java @@ -1,3 +1,5 @@ +package duke; + public class Event extends Task{ protected String from; protected String to; diff --git a/src/main/java/Task.java b/src/main/java/duke/Task.java similarity index 97% rename from src/main/java/Task.java rename to src/main/java/duke/Task.java index 04904f02c..7f4eefc2e 100644 --- a/src/main/java/Task.java +++ b/src/main/java/duke/Task.java @@ -1,3 +1,5 @@ +package duke; + public class Task { protected String description; protected boolean isDone; From cade92134f1d91904ddafb36f0805d5634308223 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Mon, 13 Feb 2023 17:02:02 +0800 Subject: [PATCH 08/21] A-CodingStandard --- src/main/java/duke/Duke.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index cfe7c88db..53cc7f3f6 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -38,40 +38,40 @@ public static Task[] handleAction(Task[] tasks, String action) throws DukeExcept if (action.equals("bye")) { System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); shouldContinue = false; - }else if (action.equals("list")) { + } else if (action.equals("list")) { System.out.print(DIVIDER_LINE); for (int i = 0; i < taskCount; i += 1) { System.out.println(Integer.toString(i + 1) + ". " +tasks[i].toString()); } System.out.println(DIVIDER_LINE); - }else if (action.equals("todo") || action.equals("deadline") || action.equals("event")) { + } else if (action.equals("todo") || action.equals("deadline") || action.equals("event")) { throw new DukeException.TaskEmpty(); - }else if (action.startsWith("mark")){ + } else if (action.startsWith("mark")){ int dividerPos = action.indexOf(" "); int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; tasks[toBeMarked].markAsDone(); System.out.println(DIVIDER_LINE + "Nice! I've marked this task as done:\n" + tasks[toBeMarked].toString() + "\n" + DIVIDER_LINE); - }else if (action.startsWith("unmark")) { + } else if (action.startsWith("unmark")) { int dividerPos = action.indexOf(" "); int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; tasks[toBeUnmarked].markAsUndone(); System.out.println(DIVIDER_LINE + "Nice! I've marked this task as undone:\n" + tasks[toBeUnmarked].getStatusIcon() + " " + tasks[toBeUnmarked].description + "\n" + DIVIDER_LINE); - }else if (action.startsWith("todo")){ + } else if (action.startsWith("todo")){ tasks[taskCount] = new Task(action.substring(5)); System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); taskCount += 1; printNumTask(taskCount); - }else if (action.startsWith("deadline")) { + } else if (action.startsWith("deadline")) { int dividerPosition = action.indexOf("/by"); tasks[taskCount] = new Deadline(action.substring(9,dividerPosition - 1), action.substring(dividerPosition + 4)); System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); taskCount += 1; printNumTask(taskCount); - }else if (action.startsWith("event")) { + } else if (action.startsWith("event")) { int dividerPosition1 = action.indexOf("/from"); int dividerPosition2 = action.indexOf("/to"); tasks[taskCount] = new Event(action.substring(6,dividerPosition1 - 1), @@ -80,7 +80,7 @@ public static Task[] handleAction(Task[] tasks, String action) throws DukeExcept System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); taskCount += 1; printNumTask(taskCount); - }else { + } else { throw new DukeException(); } return tasks; From fa052ea797d56aecfd32e5a1621a63567dbbe598 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Mon, 13 Feb 2023 17:11:39 +0800 Subject: [PATCH 09/21] improved code quality --- src/main/java/duke/Duke.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 53cc7f3f6..43d4f5be1 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -50,25 +50,22 @@ public static Task[] handleAction(Task[] tasks, String action) throws DukeExcept int dividerPos = action.indexOf(" "); int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; tasks[toBeMarked].markAsDone(); - System.out.println(DIVIDER_LINE + "Nice! I've marked this task as done:\n" - + tasks[toBeMarked].toString() + "\n" + DIVIDER_LINE); + printMarked(tasks[toBeMarked], action.split(" ")[0]); } else if (action.startsWith("unmark")) { int dividerPos = action.indexOf(" "); int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; tasks[toBeUnmarked].markAsUndone(); - System.out.println(DIVIDER_LINE + "Nice! I've marked this task as undone:\n" - + tasks[toBeUnmarked].getStatusIcon() + " " + tasks[toBeUnmarked].description - + "\n" + DIVIDER_LINE); + printMarked(tasks[toBeUnmarked], action.split(" ")[0]); } else if (action.startsWith("todo")){ tasks[taskCount] = new Task(action.substring(5)); - System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + printAdded(tasks); taskCount += 1; printNumTask(taskCount); } else if (action.startsWith("deadline")) { int dividerPosition = action.indexOf("/by"); tasks[taskCount] = new Deadline(action.substring(9,dividerPosition - 1), action.substring(dividerPosition + 4)); - System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + printAdded(tasks); taskCount += 1; printNumTask(taskCount); } else if (action.startsWith("event")) { @@ -77,7 +74,7 @@ public static Task[] handleAction(Task[] tasks, String action) throws DukeExcept tasks[taskCount] = new Event(action.substring(6,dividerPosition1 - 1), action.substring(dividerPosition1 + 6, dividerPosition2 - 1), action.substring(dividerPosition2 + 4)); - System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + printAdded(tasks); taskCount += 1; printNumTask(taskCount); } else { @@ -86,6 +83,15 @@ public static Task[] handleAction(Task[] tasks, String action) throws DukeExcept return tasks; } + private static void printMarked(Task tasks, String action) { + System.out.println(DIVIDER_LINE + "Nice! I've marked this task as "+ (action.equals("mark") ? "done:" : "undone:") + + "\n" + tasks.toString() + "\n" + DIVIDER_LINE); + } + + private static void printAdded(Task[] tasks) { + System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + } + public static void printNumTask(int taskCount) { System.out.println("Now you have " + taskCount + " tasks in the list"); } From 080beee8f19146bd08d2b50fc5ed7a7e408b3bf3 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Mon, 13 Feb 2023 17:22:48 +0800 Subject: [PATCH 10/21] no message --- src/main/java/duke/Duke.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 43d4f5be1..81f3de55a 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -71,6 +71,7 @@ public static Task[] handleAction(Task[] tasks, String action) throws DukeExcept } else if (action.startsWith("event")) { int dividerPosition1 = action.indexOf("/from"); int dividerPosition2 = action.indexOf("/to"); + //extract the event details tasks[taskCount] = new Event(action.substring(6,dividerPosition1 - 1), action.substring(dividerPosition1 + 6, dividerPosition2 - 1), action.substring(dividerPosition2 + 4)); From 483db36fcbda018004b94a17580c78d30b6d5af4 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Mon, 13 Feb 2023 17:24:15 +0800 Subject: [PATCH 11/21] level 5 --- src/main/java/Duke.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 03d5c2440..4a4cc27e2 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -84,6 +84,7 @@ public static Task[] handleAction(Task[] tasks, String action) throws DukeExcept return tasks; } + public static void printNumTask(int taskCount) { System.out.println("Now you have " + taskCount + " tasks in the list"); } From 4392566b7b63d5ccf7bdf778e1b44a799598c7e8 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Wed, 15 Feb 2023 18:57:38 +0800 Subject: [PATCH 12/21] dummy commit --- src/main/java/duke/Duke.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 81f3de55a..b07dd4a28 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,6 +1,7 @@ package duke; import java.util.Scanner; +import java.util.ArrayList; public class Duke { public static String DIVIDER_LINE = "______________________________\n"; @@ -21,7 +22,7 @@ public static void main(String[] args) { System.out.println(greet); Scanner in = new Scanner(System.in); String action; - Task[] tasks = new Task[100]; + ArrayList tasks = new ArrayList<>(); while (shouldContinue) { action = in.nextLine(); try { @@ -34,7 +35,7 @@ public static void main(String[] args) { } } - public static Task[] handleAction(Task[] tasks, String action) throws DukeException, DukeException.TaskEmpty{ + public static ArrayList handleAction(ArrayList tasks, String action) throws DukeException, DukeException.TaskEmpty{ if (action.equals("bye")) { System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); shouldContinue = false; From 22f3a3f8c4faccd2a355b46183a95550046236c8 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Thu, 16 Feb 2023 10:14:03 +0800 Subject: [PATCH 13/21] added delete function --- src/main/java/duke/Duke.java | 43 ++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index b07dd4a28..2b815dfe7 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -42,7 +42,7 @@ public static ArrayList handleAction(ArrayList tasks, String action) } else if (action.equals("list")) { System.out.print(DIVIDER_LINE); for (int i = 0; i < taskCount; i += 1) { - System.out.println(Integer.toString(i + 1) + ". " +tasks[i].toString()); + System.out.println(Integer.toString(i + 1) + ". " +tasks.get(i).toString()); } System.out.println(DIVIDER_LINE); } else if (action.equals("todo") || action.equals("deadline") || action.equals("event")) { @@ -50,35 +50,46 @@ public static ArrayList handleAction(ArrayList tasks, String action) } else if (action.startsWith("mark")){ int dividerPos = action.indexOf(" "); int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; - tasks[toBeMarked].markAsDone(); - printMarked(tasks[toBeMarked], action.split(" ")[0]); + tasks.get(toBeMarked).markAsDone(); + printMarked(tasks.get(toBeMarked), action.split(" ")[0]); } else if (action.startsWith("unmark")) { int dividerPos = action.indexOf(" "); int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; - tasks[toBeUnmarked].markAsUndone(); - printMarked(tasks[toBeUnmarked], action.split(" ")[0]); + tasks.get(toBeUnmarked).markAsUndone(); + printMarked(tasks.get(toBeUnmarked), action.split(" ")[0]); + } else if (action.startsWith("delete")) { + int dividerPos = action.indexOf(" "); + int toBeDeleted = Integer.parseInt(action.substring(dividerPos + 1)) - 1; + Task removedTask = tasks.get(toBeDeleted); + tasks.remove(toBeDeleted); + taskCount -= 1; + printDeleted(removedTask); + printNumTask(); } else if (action.startsWith("todo")){ - tasks[taskCount] = new Task(action.substring(5)); + Task tempTask = new Task(action.substring(5)); + tasks.add(tempTask); printAdded(tasks); taskCount += 1; - printNumTask(taskCount); + printNumTask(); } else if (action.startsWith("deadline")) { int dividerPosition = action.indexOf("/by"); - tasks[taskCount] = new Deadline(action.substring(9,dividerPosition - 1), + Task tempTask = new Deadline(action.substring(9,dividerPosition - 1), action.substring(dividerPosition + 4)); + tasks.add(tempTask); printAdded(tasks); taskCount += 1; - printNumTask(taskCount); + printNumTask(); } else if (action.startsWith("event")) { int dividerPosition1 = action.indexOf("/from"); int dividerPosition2 = action.indexOf("/to"); //extract the event details - tasks[taskCount] = new Event(action.substring(6,dividerPosition1 - 1), + Task tempTask = new Event(action.substring(6,dividerPosition1 - 1), action.substring(dividerPosition1 + 6, dividerPosition2 - 1), action.substring(dividerPosition2 + 4)); + tasks.add(tempTask); printAdded(tasks); taskCount += 1; - printNumTask(taskCount); + printNumTask(); } else { throw new DukeException(); } @@ -90,11 +101,15 @@ private static void printMarked(Task tasks, String action) { + "\n" + tasks.toString() + "\n" + DIVIDER_LINE); } - private static void printAdded(Task[] tasks) { - System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + private static void printAdded(ArrayList tasks) { + System.out.println(DIVIDER_LINE + "added:\n" + tasks.get(taskCount).toString() + "\n" + DIVIDER_LINE); + } + + private static void printDeleted(Task deletedTask) { + System.out.println(DIVIDER_LINE + "deleted:\n" + deletedTask.toString() + "\n" + DIVIDER_LINE); } - public static void printNumTask(int taskCount) { + public static void printNumTask() { System.out.println("Now you have " + taskCount + " tasks in the list"); } } From 226cefae4d805e316d010bd43ce746625f396aa5 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Thu, 16 Feb 2023 11:01:36 +0800 Subject: [PATCH 14/21] dummy change --- src/main/java/duke/Duke.java | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index b07dd4a28..e84d6aa33 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -42,7 +42,7 @@ public static ArrayList handleAction(ArrayList tasks, String action) } else if (action.equals("list")) { System.out.print(DIVIDER_LINE); for (int i = 0; i < taskCount; i += 1) { - System.out.println(Integer.toString(i + 1) + ". " +tasks[i].toString()); + System.out.println(Integer.toString(i + 1) + ". " +tasks.get(i).toString()); } System.out.println(DIVIDER_LINE); } else if (action.equals("todo") || action.equals("deadline") || action.equals("event")) { @@ -50,22 +50,24 @@ public static ArrayList handleAction(ArrayList tasks, String action) } else if (action.startsWith("mark")){ int dividerPos = action.indexOf(" "); int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; - tasks[toBeMarked].markAsDone(); - printMarked(tasks[toBeMarked], action.split(" ")[0]); + tasks.get(toBeMarked).markAsDone(); + printMarked(tasks.get(toBeMarked), action.split(" ")[0]); } else if (action.startsWith("unmark")) { int dividerPos = action.indexOf(" "); int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; - tasks[toBeUnmarked].markAsUndone(); - printMarked(tasks[toBeUnmarked], action.split(" ")[0]); + tasks.get(toBeUnmarked).markAsUndone(); + printMarked(tasks.get(toBeUnmarked), action.split(" ")[0]); } else if (action.startsWith("todo")){ - tasks[taskCount] = new Task(action.substring(5)); + Task tempTask = new Task(action.substring(5)); + tasks.add(tempTask); printAdded(tasks); taskCount += 1; printNumTask(taskCount); } else if (action.startsWith("deadline")) { int dividerPosition = action.indexOf("/by"); - tasks[taskCount] = new Deadline(action.substring(9,dividerPosition - 1), + Task tempTask = new Deadline(action.substring(9,dividerPosition - 1), action.substring(dividerPosition + 4)); + tasks.add(tempTask); printAdded(tasks); taskCount += 1; printNumTask(taskCount); @@ -73,9 +75,10 @@ public static ArrayList handleAction(ArrayList tasks, String action) int dividerPosition1 = action.indexOf("/from"); int dividerPosition2 = action.indexOf("/to"); //extract the event details - tasks[taskCount] = new Event(action.substring(6,dividerPosition1 - 1), + Task tempTask = new Event(action.substring(6,dividerPosition1 - 1), action.substring(dividerPosition1 + 6, dividerPosition2 - 1), action.substring(dividerPosition2 + 4)); + tasks.add(tempTask); printAdded(tasks); taskCount += 1; printNumTask(taskCount); @@ -90,8 +93,8 @@ private static void printMarked(Task tasks, String action) { + "\n" + tasks.toString() + "\n" + DIVIDER_LINE); } - private static void printAdded(Task[] tasks) { - System.out.println(DIVIDER_LINE + "added:\n" + tasks[taskCount].toString() + "\n" + DIVIDER_LINE); + private static void printAdded(ArrayList tasks) { + System.out.println(DIVIDER_LINE + "added:\n" + tasks.get(taskCount).toString() + "\n" + DIVIDER_LINE); } public static void printNumTask(int taskCount) { From 1d2e54428d4ee91ab174edbbbdcd2322bda86f0f Mon Sep 17 00:00:00 2001 From: sunkyan Date: Fri, 3 Mar 2023 22:10:25 +0800 Subject: [PATCH 15/21] lvl 7 added save --- src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/duke/Duke.java | 67 ++++++++++++++++----- src/main/java/duke/TaskData.java | 93 ++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/duke/TaskData.java diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..2c9a9745c --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.Duke + diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index e84d6aa33..aaceae60b 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,5 +1,7 @@ package duke; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; @@ -9,6 +11,35 @@ public class Duke { protected static int taskCount = 0; public static void main(String[] args) { + greetUser(); + ArrayList tasks = new ArrayList<>(); + TaskData file = new TaskData("./duke.txt"); + tasks = loadData(tasks, file); + taskCount = tasks.size(); + Scanner in = new Scanner(System.in); + String action; + while (shouldContinue) { + action = in.nextLine(); + try { + tasks = handleAction(tasks, action, file); + } catch (DukeException e) { + System.out.println(DIVIDER_LINE + "Sorry! I don't know what that means.\n" + DIVIDER_LINE); + } catch (DukeException.TaskEmpty e) { + System.out.println(DIVIDER_LINE + "The content of the task cannot be empty!\n" + DIVIDER_LINE); + } + } + } + + private static ArrayList loadData(ArrayList tasks, TaskData file) { + try { + file.readData(tasks); + } catch (FileNotFoundException e) { + System.out.println("file not found!"); + } + return tasks; + } + + private static void greetUser() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" @@ -20,22 +51,27 @@ public static void main(String[] args) { + "What can i do for you\n" + DIVIDER_LINE; System.out.println(greet); - Scanner in = new Scanner(System.in); - String action; - ArrayList tasks = new ArrayList<>(); - while (shouldContinue) { - action = in.nextLine(); - try { - tasks = handleAction(tasks, action); - } catch (DukeException e) { - System.out.println(DIVIDER_LINE + "Sorry! I don't know what that means.\n" + DIVIDER_LINE); - } catch (DukeException.TaskEmpty e) { - System.out.println(DIVIDER_LINE + "The content of the task cannot be empty!\n" + DIVIDER_LINE); - } + } + + private static ArrayList addTaskToFile(ArrayList tasks, Task taskToAdd, TaskData file) { + try { + tasks = file.writeToFile(tasks, taskToAdd.toString()); + } catch (IOException e) { + System.out.println("IOException!"); } + return tasks; + } + + private static ArrayList updateFile(ArrayList tasks, TaskData file) { + try { + tasks = file.updateFile(tasks); + } catch (IOException e) { + System.out.println("IOException!"); + } + return tasks; } - public static ArrayList handleAction(ArrayList tasks, String action) throws DukeException, DukeException.TaskEmpty{ + public static ArrayList handleAction(ArrayList tasks, String action, TaskData file) throws DukeException, DukeException.TaskEmpty{ if (action.equals("bye")) { System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); shouldContinue = false; @@ -52,17 +88,20 @@ public static ArrayList handleAction(ArrayList tasks, String action) int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; tasks.get(toBeMarked).markAsDone(); printMarked(tasks.get(toBeMarked), action.split(" ")[0]); + updateFile(tasks, file); } else if (action.startsWith("unmark")) { int dividerPos = action.indexOf(" "); int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; tasks.get(toBeUnmarked).markAsUndone(); printMarked(tasks.get(toBeUnmarked), action.split(" ")[0]); + updateFile(tasks, file); } else if (action.startsWith("todo")){ Task tempTask = new Task(action.substring(5)); tasks.add(tempTask); printAdded(tasks); taskCount += 1; printNumTask(taskCount); + tasks = addTaskToFile(tasks, tempTask, file); } else if (action.startsWith("deadline")) { int dividerPosition = action.indexOf("/by"); Task tempTask = new Deadline(action.substring(9,dividerPosition - 1), @@ -71,6 +110,7 @@ public static ArrayList handleAction(ArrayList tasks, String action) printAdded(tasks); taskCount += 1; printNumTask(taskCount); + tasks = addTaskToFile(tasks, tempTask, file); } else if (action.startsWith("event")) { int dividerPosition1 = action.indexOf("/from"); int dividerPosition2 = action.indexOf("/to"); @@ -82,6 +122,7 @@ public static ArrayList handleAction(ArrayList tasks, String action) printAdded(tasks); taskCount += 1; printNumTask(taskCount); + tasks = addTaskToFile(tasks, tempTask, file); } else { throw new DukeException(); } diff --git a/src/main/java/duke/TaskData.java b/src/main/java/duke/TaskData.java new file mode 100644 index 000000000..819df797e --- /dev/null +++ b/src/main/java/duke/TaskData.java @@ -0,0 +1,93 @@ +package duke; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Scanner; +import java.util.concurrent.LinkedBlockingDeque; + +public class TaskData { + private File data; + private String fileName; + + public TaskData (String fileName) { + this.fileName = fileName; + data = new File(fileName); + } + +// public ArrayList loadData (ArrayList tasks) { +// try { +// readData(tasks); +// } catch (FileNotFoundException e){ +// System.out.println("file not found!"); +// } +// } + + public ArrayList readData (ArrayList tasks) throws FileNotFoundException { + if (!data.exists()) { + return tasks; + } + Scanner s = new Scanner(data); + String task; + String taskDescription; + while (s.hasNext()) { + task = s.nextLine(); + if (task.charAt(1) == 'T') { + taskDescription = task.substring(6); + Task tempTask = new Task(taskDescription); + if (task.charAt(4) == 'X') { + tempTask.markAsDone(); + } else { + tempTask.markAsUndone(); + } + tasks.add(tempTask); + } else if (task.charAt(1) == 'D') { + taskDescription = task.substring(6, task.indexOf("(by") - 2); + String by = task.substring(task.indexOf("(by") + 5, task.length() - 1); + Task tempTask = new Deadline(taskDescription, by); + if (task.charAt(4) == 'X') { + tempTask.markAsDone(); + } else { + tempTask.markAsUndone(); + } + tasks.add(tempTask); + } else { + taskDescription = task.substring(6, task.indexOf("(from") - 2); + String from = task.substring(task.indexOf("(from") + 7, task.indexOf("to") -1); + String to = task.substring(task.indexOf("to") + 4, task.length() - 1); + Task tempTask = new Event(taskDescription, from, to); + if (task.charAt(4) == 'X') { + tempTask.markAsDone(); + } else { + tempTask.markAsUndone(); + } + tasks.add(tempTask); + } + } + return tasks; + } + + public ArrayList writeToFile (ArrayList tasks,String task) throws IOException { + FileWriter fw = new FileWriter(fileName, true); + fw.write(task + System.lineSeparator()); + fw.close(); + return tasks; + } + + public ArrayList updateFile (ArrayList tasks) throws IOException { + FileWriter fw = new FileWriter(fileName); + fw.write(""); + fw.close(); + FileWriter fwAppend = new FileWriter(fileName, true); + for (Task t : tasks) { + fwAppend.write(t.toString() + System.lineSeparator()); + } + fwAppend.close(); + return tasks; + } + +} From 4cb33545d0e8bf008827a023c3ee5e5c7399f519 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Fri, 3 Mar 2023 22:47:54 +0800 Subject: [PATCH 16/21] added find function --- src/main/java/duke/Duke.java | 13 ++++++++++++- src/main/java/duke/TaskData.java | 12 +----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index e906d561a..ae4d9b098 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -141,7 +141,18 @@ public static ArrayList handleAction(ArrayList tasks, String action, printNumTask(); tasks = addTaskToFile(tasks, tempTask, file); - } else { + } else if (action.startsWith("find")) { + int dividerPos = action.indexOf(" "); + String taskToFind = action.substring(5); + System.out.println(DIVIDER_LINE + "Here are the matching tasks in your list:"); + for (Task t : tasks) { + if (t.description.contains(taskToFind)) { + System.out.println(t.toString()); + } + } + System.out.println(DIVIDER_LINE); + + } else { throw new DukeException(); } return tasks; diff --git a/src/main/java/duke/TaskData.java b/src/main/java/duke/TaskData.java index 2275b785b..555edb4c1 100644 --- a/src/main/java/duke/TaskData.java +++ b/src/main/java/duke/TaskData.java @@ -4,11 +4,9 @@ import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; import java.util.ArrayList; import java.util.Scanner; -import java.util.concurrent.LinkedBlockingDeque; + public class TaskData { private File data; @@ -19,14 +17,6 @@ public TaskData (String fileName) { data = new File(fileName); } -// public ArrayList loadData (ArrayList tasks) { -// try { -// readData(tasks); -// } catch (FileNotFoundException e){ -// System.out.println("file not found!"); -// } -// } - public ArrayList readData (ArrayList tasks) throws FileNotFoundException { if (!data.exists()) { return tasks; From 8e72009e2efe86db4bdd7b8f86aa2ac7a76379f5 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Fri, 3 Mar 2023 23:25:21 +0800 Subject: [PATCH 17/21] some comments --- src/main/java/duke/Duke.java | 35 +++++++++++++++++++++++++++++++- src/main/java/duke/TaskData.java | 26 +++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index ae4d9b098..f8ffa86bd 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -30,6 +30,14 @@ public static void main(String[] args) { } } + /** + * Returns updated tasks from file "/duke.txt" + * tasks is unchanged if there is no such file already existing and creates a file + * + * @param tasks all the tasks added + * @param file the file storing the tasks + * @return tasks + */ private static ArrayList loadData(ArrayList tasks, TaskData file) { try { file.readData(tasks); @@ -53,6 +61,14 @@ private static void greetUser() { System.out.println(greet); } + /** + * Appends a task to the file and returns the updated tasks + * + * @param tasks all the tasks added + * @param taskToAdd the task to append to the text file + * @param file the txt file storing the tasks + * @return tasks + */ private static ArrayList addTaskToFile(ArrayList tasks, Task taskToAdd, TaskData file) { try { tasks = file.writeToFile(tasks, taskToAdd.toString()); @@ -62,6 +78,13 @@ private static ArrayList addTaskToFile(ArrayList tasks, Task taskToA return tasks; } + /** + * updates the file entirely to the current tasks + * + * @param tasks all the tasks added + * @param file the txt file storing the tasks + * @return tasks + */ private static ArrayList updateFile(ArrayList tasks, TaskData file) { try { tasks = file.updateFile(tasks); @@ -71,6 +94,17 @@ private static ArrayList updateFile(ArrayList tasks, TaskData file) return tasks; } + /** + * Handles command(action) input by the user + * Returns updated tasks + * + * @param tasks all the tasks added + * @param action the input line by the user + * @param file the txt file storing the tasks + * @return tasks + * @throws DukeException when the command word is not recognised + * @throws DukeException.TaskEmpty when the content of task to be added is empty + */ public static ArrayList handleAction(ArrayList tasks, String action, TaskData file) throws DukeException, DukeException.TaskEmpty{ if (action.equals("bye")) { System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); @@ -163,7 +197,6 @@ private static void printMarked(Task tasks, String action) { + "\n" + tasks.toString() + "\n" + DIVIDER_LINE); } - private static void printAdded(ArrayList tasks) { System.out.println(DIVIDER_LINE + "added:\n" + tasks.get(taskCount).toString() + "\n" + DIVIDER_LINE); } diff --git a/src/main/java/duke/TaskData.java b/src/main/java/duke/TaskData.java index 555edb4c1..684c28671 100644 --- a/src/main/java/duke/TaskData.java +++ b/src/main/java/duke/TaskData.java @@ -7,7 +7,10 @@ import java.util.ArrayList; import java.util.Scanner; - +/** + * Represents a file storing data for duke. + * Manages the file + */ public class TaskData { private File data; private String fileName; @@ -17,6 +20,13 @@ public TaskData (String fileName) { data = new File(fileName); } + /** + * Reads data from a file and stores them inside ArrayList tasks + * + * @param tasks the ArrayList storing all the tasks + * @return tasks + * @throws FileNotFoundException when the file cannot be found + */ public ArrayList readData (ArrayList tasks) throws FileNotFoundException { if (!data.exists()) { return tasks; @@ -61,6 +71,13 @@ public ArrayList readData (ArrayList tasks) throws FileNotFoundExcep return tasks; } + /** + * Appends data of a task to the file + * @param tasks storing all the tasks + * @param task the task to be appended + * @return tasks + * @throws IOException when the file writer fails + */ public ArrayList writeToFile (ArrayList tasks,String task) throws IOException { FileWriter fw = new FileWriter(fileName, true); fw.write(task + System.lineSeparator()); @@ -68,6 +85,13 @@ public ArrayList writeToFile (ArrayList tasks,String task) throws IO return tasks; } + /** + * updates the file entirely based on the current tasks + * + * @param tasks the ArrayList storing all the tasks + * @return tasks + * @throws IOException when file writer fails + */ public ArrayList updateFile (ArrayList tasks) throws IOException { FileWriter fw = new FileWriter(fileName); fw.write(""); From 0d0c7ea480efd4bc483c23c921eeb2ad51ff939a Mon Sep 17 00:00:00 2001 From: sunkyan Date: Fri, 3 Mar 2023 23:43:10 +0800 Subject: [PATCH 18/21] Added JavaDoc --- src/main/java/duke/Deadline.java | 18 +++++++++++++++++ src/main/java/duke/Duke.java | 3 +-- src/main/java/duke/DukeException.java | 4 ++++ src/main/java/duke/Event.java | 19 ++++++++++++++++++ src/main/java/duke/Task.java | 28 +++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/main/java/duke/Deadline.java b/src/main/java/duke/Deadline.java index 66640714a..5feb3f434 100644 --- a/src/main/java/duke/Deadline.java +++ b/src/main/java/duke/Deadline.java @@ -1,18 +1,36 @@ package duke; +/** + * A subclass Deadline of Task + * Represents a Deadline added by the user and contains the deadline info "by" + */ public class Deadline extends Task{ protected String by; + /** + * Constructor to construct a Deadline + * @param description the details of the task + * @param by the time of the deadline + */ public Deadline(String description, String by) { super(description); this.by = by; } + /** + * gets the type of the task which is deadline + * @return a string representing the type ([D]) + */ @Override public String getTypeIcon() { return "[D]"; } + /** + * Returns all the information about the task + * + * @return a String with all the information about the task + */ @Override public String toString(){ return getTypeIcon() + super.getStatusIcon() +super.description + " (by: " + this.by + ")"; diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index f8ffa86bd..6cbc4d7dd 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -176,7 +176,6 @@ public static ArrayList handleAction(ArrayList tasks, String action, tasks = addTaskToFile(tasks, tempTask, file); } else if (action.startsWith("find")) { - int dividerPos = action.indexOf(" "); String taskToFind = action.substring(5); System.out.println(DIVIDER_LINE + "Here are the matching tasks in your list:"); for (Task t : tasks) { @@ -205,7 +204,7 @@ private static void printDeleted(Task deletedTask) { System.out.println(DIVIDER_LINE + "deleted:\n" + deletedTask.toString() + "\n" + DIVIDER_LINE); } - public static void printNumTask() { + private static void printNumTask() { System.out.println("Now you have " + taskCount + " tasks in the list"); } } diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java index 806d7b487..da73a2852 100644 --- a/src/main/java/duke/DukeException.java +++ b/src/main/java/duke/DukeException.java @@ -1,5 +1,9 @@ package duke; +/** + * DukeException is for command word not recognised by Duke + * TaskEmpty is when the content of the command word recognised is empty + */ public class DukeException extends Exception{ public static class TaskEmpty extends Exception{} } diff --git a/src/main/java/duke/Event.java b/src/main/java/duke/Event.java index 1088449d0..d68abfcdd 100644 --- a/src/main/java/duke/Event.java +++ b/src/main/java/duke/Event.java @@ -1,20 +1,39 @@ package duke; +/** + * A subclass Event of Task + * Represents an event for the user containing info for the time period of the event + */ public class Event extends Task{ protected String from; protected String to; + /** + * Constructor to construct an event + * @param description details of the event + * @param from starting time + * @param to ending time + */ public Event(String description, String from, String to){ super(description); this.from = from; this.to = to; } + /** + * gets the type of the task which is deadline + * @return a string representing the type ([E]) + */ @Override public String getTypeIcon() { return "[E]"; } + /** + * Returns all the information about the task + * + * @return a String with all the information about the task + */ @Override public String toString(){ return getTypeIcon() + super.getStatusIcon() + super.description diff --git a/src/main/java/duke/Task.java b/src/main/java/duke/Task.java index 7f4eefc2e..927b8f7c6 100644 --- a/src/main/java/duke/Task.java +++ b/src/main/java/duke/Task.java @@ -1,30 +1,58 @@ package duke; +/** + * Represents a task to be tracked by the user. + * a Task is a "todo" by default + */ public class Task { protected String description; protected boolean isDone; + /** + * Constructor to construct a Task + * Mark the task as undone by default + * @param description the details of the todo + */ public Task(String description) { this.description = description; this.isDone = false; } + /** + * get the completion status of the task (isDone) + * @return [X] if the task is done and [ ] otherwise + */ public String getStatusIcon() { return (isDone ? "[X]" : "[ ]"); // mark done task with X } + /** + * gets the icon of this task type + * @return [T] for task todo + */ public String getTypeIcon() { return "[T]"; } + /** + * mark the task as done + */ public void markAsDone() { this.isDone = true; } + /** + * mark the task as undone + */ public void markAsUndone() { this.isDone = false; } + /** + * Returns all the information about the task + * + * @return a String with all the information about the task + */ public String toString() { return getTypeIcon() + getStatusIcon() + description; } From 2940847c124af0f6f8cc0b87419983547e590415 Mon Sep 17 00:00:00 2001 From: sunkyan Date: Fri, 3 Mar 2023 23:47:44 +0800 Subject: [PATCH 19/21] more OOP --- src/main/java/duke/TaskData.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/duke/TaskData.java b/src/main/java/duke/TaskData.java index 684c28671..92a92b1fa 100644 --- a/src/main/java/duke/TaskData.java +++ b/src/main/java/duke/TaskData.java @@ -46,6 +46,7 @@ public ArrayList readData (ArrayList tasks) throws FileNotFoundExcep } tasks.add(tempTask); } else if (task.charAt(1) == 'D') { + //extracts info taskDescription = task.substring(6, task.indexOf("(by") - 1); String by = task.substring(task.indexOf("(by") + 5, task.length() - 1); Task tempTask = new Deadline(taskDescription, by); @@ -56,6 +57,7 @@ public ArrayList readData (ArrayList tasks) throws FileNotFoundExcep } tasks.add(tempTask); } else { + //extracts info taskDescription = task.substring(6, task.indexOf("(from") - 1); String from = task.substring(task.indexOf("(from") + 7, task.indexOf("to") -1); String to = task.substring(task.indexOf("to") + 4, task.length() - 1); From 8e45cf8644a202f01257d530b798317acccce45f Mon Sep 17 00:00:00 2001 From: skyanzy <88618401+skyanzy@users.noreply.github.com> Date: Sat, 4 Mar 2023 00:35:36 +0800 Subject: [PATCH 20/21] Update README.md --- docs/README.md | 131 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 120 insertions(+), 11 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..d758eacf2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,28 +2,137 @@ ## Features -### Feature-ABC +### Feature-Adding tasks -Description of the feature. +Duke can store your tasks to complete. -### Feature-XYZ +### Feature-Different types of tasks -Description of the feature. +Duke allows three different types of tasks: todo, deadline and event. + +### Feature-Mark a task as done/ undone + +Duke can change the completion status of a task. + +### Feature-Viewing tasks + +All the tasks and the corresponding type and completion status can be seen. + +### Feature-Viewing tasks + +Tasks can be deleted. + +### Feature-Storing information + +Duke can store your tasks locally when the programme is exited and loads the stored data whenever the programmed is run. ## Usage -### `Keyword` - Describe action +### command and details + +To use Duke, you need to tell it the command keyword and the details of the command. + +The format can be seen in the example below with the corresponding output. + +#### **Adding a task** + +Input the type of the task (todo, deadline, event) and follow the format given below: + +``` + todo study + + deadline submit assignments /by fri + + event lecture /from 4pm fri /to 6pm fri +``` + +**_Note: the "/" for deadline and event is very important for Duke to recognise the time/ duration!_** + +#### **Listing all the tasks** + +Simply enter list to see your tasks: + +``` +list +______________________________ +1. [T][ ]study +2. [D][ ]submit assignments (by: fri) +3. [E][ ]lecture (from: 4pm fri to: 6pm fri) +______________________________ +``` + +#### **Marking a task as done/ undone** -Describe the action and its outcome. +``` +mark 1 +``` +``` +______________________________ +Nice! I've marked this task as done: +[T][X]study +______________________________ +``` +``` +mark 2 +``` +``` +______________________________ +Nice! I've marked this task as done: +[D][X]submit assignments (by: fri) +______________________________ +``` + +``` +list +______________________________ +1. [T][X]study +2. [D][X]submit assignments (by: fri) +3. [E][ ]lecture (from: 4pm fri to: 6pm fri) +______________________________ +``` -Example of usage: +#### **Finding a task** -`keyword (optional arguments)` +``` +find study +______________________________ +Here are the matching tasks in your list: +[T][X]study +______________________________ +``` +``` +find submit +______________________________ +Here are the matching tasks in your list: +[D][X]submit assignments (by: fri) +______________________________ +``` -Expected outcome: +#### **Deleting a task** -Description of the outcome. +``` +list +______________________________ +1. [T][X]study +2. [D][X]submit assignments (by: fri) +3. [E][ ]lecture (from: 4pm fri to: 6pm fri) +______________________________ +``` +``` +delete 2 +______________________________ +deleted: +[D][X]submit assignments (by: fri) +______________________________ +Now you have 2 tasks in the list +``` ``` -expected output +list +______________________________ +1. [T][X]study +2. [E][ ]lecture (from: 4pm fri to: 6pm fri) +______________________________ ``` + + From 07ecaaab3f508281563722a89b1422c40dca507b Mon Sep 17 00:00:00 2001 From: sunkyan Date: Mon, 20 Mar 2023 23:54:22 +0800 Subject: [PATCH 21/21] Improved coding standard with better OOP. Handled more exceptions --- src/main/java/duke/Duke.java | 203 ++------------------------ src/main/java/duke/DukeException.java | 1 + src/main/java/duke/Parser.java | 104 +++++++++++++ src/main/java/duke/TaskData.java | 50 +++++++ src/main/java/duke/Ui.java | 125 ++++++++++++++++ 5 files changed, 295 insertions(+), 188 deletions(-) create mode 100644 src/main/java/duke/Parser.java create mode 100644 src/main/java/duke/Ui.java diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 6cbc4d7dd..7b8248161 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,210 +1,37 @@ package duke; -import java.io.FileNotFoundException; -import java.io.IOException; + import java.util.Scanner; import java.util.ArrayList; public class Duke { - public static String DIVIDER_LINE = "______________________________\n"; - protected static boolean shouldContinue = true; - protected static int taskCount = 0; + + public static boolean shouldContinue = true; + public static int taskCount = 0; public static void main(String[] args) { - greetUser(); + Ui.greetUser(); ArrayList tasks = new ArrayList<>(); TaskData file = new TaskData("./duke.txt"); - tasks = loadData(tasks, file); + tasks = file.loadData(tasks, file); taskCount = tasks.size(); Scanner in = new Scanner(System.in); String action; while (shouldContinue) { action = in.nextLine(); try { - tasks = handleAction(tasks, action, file); + tasks = Parser.handleAction(tasks, action, file); } catch (DukeException e) { - System.out.println(DIVIDER_LINE + "Sorry! I don't know what that means.\n" + DIVIDER_LINE); + Ui.printWrongCommand(); } catch (DukeException.TaskEmpty e) { - System.out.println(DIVIDER_LINE + "The content of the task cannot be empty!\n" + DIVIDER_LINE); - } - } - } - - /** - * Returns updated tasks from file "/duke.txt" - * tasks is unchanged if there is no such file already existing and creates a file - * - * @param tasks all the tasks added - * @param file the file storing the tasks - * @return tasks - */ - private static ArrayList loadData(ArrayList tasks, TaskData file) { - try { - file.readData(tasks); - } catch (FileNotFoundException e) { - System.out.println("file not found!"); - } - return tasks; - } - - private static void greetUser() { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - String greet = DIVIDER_LINE - + "Hello! I'm Duke\n" - + "What can i do for you\n" - + DIVIDER_LINE; - System.out.println(greet); - } - - /** - * Appends a task to the file and returns the updated tasks - * - * @param tasks all the tasks added - * @param taskToAdd the task to append to the text file - * @param file the txt file storing the tasks - * @return tasks - */ - private static ArrayList addTaskToFile(ArrayList tasks, Task taskToAdd, TaskData file) { - try { - tasks = file.writeToFile(tasks, taskToAdd.toString()); - } catch (IOException e) { - System.out.println("IOException!"); - } - return tasks; - } - - /** - * updates the file entirely to the current tasks - * - * @param tasks all the tasks added - * @param file the txt file storing the tasks - * @return tasks - */ - private static ArrayList updateFile(ArrayList tasks, TaskData file) { - try { - tasks = file.updateFile(tasks); - } catch (IOException e) { - System.out.println("IOException!"); - } - return tasks; - } - - /** - * Handles command(action) input by the user - * Returns updated tasks - * - * @param tasks all the tasks added - * @param action the input line by the user - * @param file the txt file storing the tasks - * @return tasks - * @throws DukeException when the command word is not recognised - * @throws DukeException.TaskEmpty when the content of task to be added is empty - */ - public static ArrayList handleAction(ArrayList tasks, String action, TaskData file) throws DukeException, DukeException.TaskEmpty{ - if (action.equals("bye")) { - System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); - shouldContinue = false; - - } else if (action.equals("list")) { - System.out.print(DIVIDER_LINE); - for (int i = 0; i < taskCount; i += 1) { - System.out.println(Integer.toString(i + 1) + ". " +tasks.get(i).toString()); + Ui.printEmptyContent(); + } catch (StringIndexOutOfBoundsException e) { + Ui.printWrongFormat(); + } catch (NumberFormatException e) { + Ui.printWrongNumber(); + } catch (IndexOutOfBoundsException e) { + Ui.printWrongNumber(); } - System.out.println(DIVIDER_LINE); - - } else if (action.equals("todo") || action.equals("deadline") || action.equals("event")) { - throw new DukeException.TaskEmpty(); - - } else if (action.startsWith("mark")){ - int dividerPos = action.indexOf(" "); - int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; - tasks.get(toBeMarked).markAsDone(); - printMarked(tasks.get(toBeMarked), action.split(" ")[0]); - updateFile(tasks, file); - - } else if (action.startsWith("unmark")) { - int dividerPos = action.indexOf(" "); - int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; - tasks.get(toBeUnmarked).markAsUndone(); - printMarked(tasks.get(toBeUnmarked), action.split(" ")[0]); - updateFile(tasks, file); - - } else if (action.startsWith("delete")) { - int dividerPos = action.indexOf(" "); - int toBeDeleted = Integer.parseInt(action.substring(dividerPos + 1)) - 1; - Task removedTask = tasks.get(toBeDeleted); - tasks.remove(toBeDeleted); - taskCount -= 1; - printDeleted(removedTask); - printNumTask(); - updateFile(tasks, file); - - } else if (action.startsWith("todo")){ - Task tempTask = new Task(action.substring(5)); - tasks.add(tempTask); - printAdded(tasks); - taskCount += 1; - printNumTask(); - tasks = addTaskToFile(tasks, tempTask, file); - - } else if (action.startsWith("deadline")) { - int dividerPosition = action.indexOf("/by"); - Task tempTask = new Deadline(action.substring(9,dividerPosition - 1), - action.substring(dividerPosition + 4)); - tasks.add(tempTask); - printAdded(tasks); - taskCount += 1; - printNumTask(); - tasks = addTaskToFile(tasks, tempTask, file); - - } else if (action.startsWith("event")) { - int dividerPosition1 = action.indexOf("/from"); - int dividerPosition2 = action.indexOf("/to"); - //extract the event details - Task tempTask = new Event(action.substring(6,dividerPosition1 - 1), - action.substring(dividerPosition1 + 6, dividerPosition2 - 1), - action.substring(dividerPosition2 + 4)); - tasks.add(tempTask); - printAdded(tasks); - taskCount += 1; - printNumTask(); - tasks = addTaskToFile(tasks, tempTask, file); - - } else if (action.startsWith("find")) { - String taskToFind = action.substring(5); - System.out.println(DIVIDER_LINE + "Here are the matching tasks in your list:"); - for (Task t : tasks) { - if (t.description.contains(taskToFind)) { - System.out.println(t.toString()); - } - } - System.out.println(DIVIDER_LINE); - - } else { - throw new DukeException(); } - return tasks; - } - - private static void printMarked(Task tasks, String action) { - System.out.println(DIVIDER_LINE + "Nice! I've marked this task as "+ (action.equals("mark") ? "done:" : "undone:") - + "\n" + tasks.toString() + "\n" + DIVIDER_LINE); - } - - private static void printAdded(ArrayList tasks) { - System.out.println(DIVIDER_LINE + "added:\n" + tasks.get(taskCount).toString() + "\n" + DIVIDER_LINE); - } - - private static void printDeleted(Task deletedTask) { - System.out.println(DIVIDER_LINE + "deleted:\n" + deletedTask.toString() + "\n" + DIVIDER_LINE); - } - - private static void printNumTask() { - System.out.println("Now you have " + taskCount + " tasks in the list"); } } diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java index da73a2852..920941def 100644 --- a/src/main/java/duke/DukeException.java +++ b/src/main/java/duke/DukeException.java @@ -6,4 +6,5 @@ */ public class DukeException extends Exception{ public static class TaskEmpty extends Exception{} + } diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 000000000..22f02a2ef --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,104 @@ +package duke; + +import java.util.ArrayList; + +/** + * Deals with making sense of user command + */ +public class Parser { + protected static int TODO_OFFSET = 5; + protected static int DEADLINE_OFFSET = 9; + protected static int BY_OFFSET = 4; + protected static int EVENT_OFFSET = 6; + protected static int FROM_OFFSET = 6; + protected static int TO_OFFSET = 4; + protected static int FIND_OFFSET = 5; + + /** + * Handles command(action) input by the user + * Returns updated tasks + * + * @param tasks all the tasks added + * @param action the input line by the user + * @param file the txt file storing the tasks + * @return tasks + * @throws DukeException when the command word is not recognised + * @throws DukeException.TaskEmpty when the content of task to be added is empty + */ + public static ArrayList handleAction(ArrayList tasks, String action, TaskData file + ) throws DukeException, DukeException.TaskEmpty{ + if (action.equals("bye")) { + Ui.exitProgram(); + Duke.shouldContinue = false; + + } else if (action.equals("list")) { + Ui.listTasks(tasks, Duke.taskCount); + + } else if (action.equals("todo") || action.equals("deadline") || action.equals("event")) { + throw new DukeException.TaskEmpty(); + + } else if (action.startsWith("mark")){ + int dividerPos = action.indexOf(" "); + int toBeMarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; + tasks.get(toBeMarked).markAsDone(); + Ui.printMarked(tasks.get(toBeMarked), action.split(" ")[0]); + file.updateFile(tasks, file); + + } else if (action.startsWith("unmark")) { + int dividerPos = action.indexOf(" "); + int toBeUnmarked = Integer.parseInt(action.substring(dividerPos + 1)) - 1; + tasks.get(toBeUnmarked).markAsUndone(); + Ui.printMarked(tasks.get(toBeUnmarked), action.split(" ")[0]); + file.updateFile(tasks, file); + + } else if (action.startsWith("delete")) { + int dividerPos = action.indexOf(" "); + int toBeDeleted = Integer.parseInt(action.substring(dividerPos + 1)) - 1; + Task removedTask = tasks.get(toBeDeleted); + tasks.remove(toBeDeleted); + Duke.taskCount -= 1; + Ui.printDeleted(removedTask); + Ui.printNumTask(Duke.taskCount); + file.updateFile(tasks, file); + + } else if (action.startsWith("todo")){ + Task tempTask = new Task(action.substring(TODO_OFFSET)); + tasks.add(tempTask); + Ui.printAdded(tasks, Duke.taskCount); + Duke.taskCount += 1; + Ui.printNumTask(Duke.taskCount); + tasks = file.addTaskToFile(tasks, tempTask, file); + + } else if (action.startsWith("deadline")) { + int dividerPosition = action.indexOf("/by"); + Task tempTask = new Deadline(action.substring(DEADLINE_OFFSET,dividerPosition - 1), + action.substring(dividerPosition + BY_OFFSET)); + tasks.add(tempTask); + Ui.printAdded(tasks, Duke.taskCount); + Duke.taskCount += 1; + Ui.printNumTask(Duke.taskCount); + tasks = file.addTaskToFile(tasks, tempTask, file); + + } else if (action.startsWith("event")) { + int dividerPosition1 = action.indexOf("/from"); + int dividerPosition2 = action.indexOf("/to"); + //extract the event details + Task tempTask = new Event(action.substring(EVENT_OFFSET,dividerPosition1 - 1), + action.substring(dividerPosition1 + FROM_OFFSET, dividerPosition2 - 1), + action.substring(dividerPosition2 + TO_OFFSET)); + tasks.add(tempTask); + Ui.printAdded(tasks, Duke.taskCount); + Duke.taskCount += 1; + Ui.printNumTask(Duke.taskCount); + tasks = file.addTaskToFile(tasks, tempTask, file); + + } else if (action.startsWith("find")) { + String taskToFind = action.substring(FIND_OFFSET); + Ui.printFound(tasks, taskToFind); + + } else { + throw new DukeException(); + } + return tasks; + } +} diff --git a/src/main/java/duke/TaskData.java b/src/main/java/duke/TaskData.java index 92a92b1fa..936202025 100644 --- a/src/main/java/duke/TaskData.java +++ b/src/main/java/duke/TaskData.java @@ -106,4 +106,54 @@ public ArrayList updateFile (ArrayList tasks) throws IOException { return tasks; } + /** + * Returns updated tasks from file "/duke.txt" + * tasks is unchanged if there is no such file already existing and creates a file + * + * @param tasks all the tasks added + * @param file the file storing the tasks + * @return tasks + */ + public static ArrayList loadData(ArrayList tasks, TaskData file) { + try { + file.readData(tasks); + } catch (FileNotFoundException e) { + System.out.println("file not found!"); + } + return tasks; + } + + /** + * Appends a task to the file and returns the updated tasks + * + * @param tasks all the tasks added + * @param taskToAdd the task to append to the text file + * @param file the txt file storing the tasks + * @return tasks + */ + public static ArrayList addTaskToFile(ArrayList tasks, Task taskToAdd, TaskData file) { + try { + tasks = file.writeToFile(tasks, taskToAdd.toString()); + } catch (IOException e) { + System.out.println("IOException!"); + } + return tasks; + } + + /** + * updates the file entirely to the current tasks + * + * @param tasks all the tasks added + * @param file the txt file storing the tasks + * @return tasks + */ + public static ArrayList updateFile(ArrayList tasks, TaskData file) { + try { + tasks = file.updateFile(tasks); + } catch (IOException e) { + System.out.println("IOException!"); + } + return tasks; + } + } diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java new file mode 100644 index 000000000..de0db5e68 --- /dev/null +++ b/src/main/java/duke/Ui.java @@ -0,0 +1,125 @@ +package duke; + +import java.util.ArrayList; + +/** + * Deals with interactions with the user + */ +public class Ui { + private static String DIVIDER_LINE = "______________________________\n"; + + /** + * Prints a message to greet the user + */ + public static void greetUser() { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); + String greet = DIVIDER_LINE + + "Hello! I'm Duke\n" + + "What can i do for you\n" + + DIVIDER_LINE; + System.out.println(greet); + } + + /** + * Prints a message to indicate exiting the programme + */ + public static void exitProgram() { + System.out.println(DIVIDER_LINE + "Bye. Hope to see you again soon!\n" + DIVIDER_LINE); + } + + /** + * List all the tasks stored + * @param tasks the Arraylist storing the tasks + * @param taskCount total number of tasks + */ + public static void listTasks(ArrayList tasks, int taskCount) { + System.out.print(DIVIDER_LINE); + for (int i = 0; i < taskCount; i += 1) { + System.out.println(Integer.toString(i + 1) + ". " +tasks.get(i).toString()); + } + System.out.println(DIVIDER_LINE); + } + + /** + * prints the results of finding the keyword + * @param tasks the Arraylist storing the tasks + * @param taskToFind the keyword to be found + */ + public static void printFound(ArrayList tasks, String taskToFind) { + System.out.println(DIVIDER_LINE + "Here are the matching tasks in your list:"); + for (Task t : tasks) { + if (t.description.contains(taskToFind)) { + System.out.println(t.toString()); + } + } + System.out.println(DIVIDER_LINE); + } + + /** + * prints the task just marked as done or undone + * @param tasks the Arraylist storing the tasks + * @param action the action to indicate mark or unmark + */ + public static void printMarked(Task tasks, String action) { + System.out.println(DIVIDER_LINE + "Nice! I've marked this task as "+ (action.equals("mark") ? "done:" : "undone:") + + "\n" + tasks.toString() + "\n" + DIVIDER_LINE); + } + + /** + * prints the added task details + * @param tasks the Arraylist storing the tasks + * @param taskCount the total number of tasks + */ + public static void printAdded(ArrayList tasks, int taskCount) { + System.out.println(DIVIDER_LINE + "added:\n" + tasks.get(taskCount).toString() + "\n" + DIVIDER_LINE); + } + + /** + * prints the task just deleted + * @param deletedTask the task that just got deleted + */ + public static void printDeleted(Task deletedTask) { + System.out.println(DIVIDER_LINE + "deleted:\n" + deletedTask.toString() + "\n" + DIVIDER_LINE); + } + + /** + * prints the total number of tasks + * @param taskCount total number of tasks + */ + public static void printNumTask(int taskCount) { + System.out.println("Now you have " + taskCount + " tasks in the list"); + } + + /** + * Print a message to indicate a wrong command word + */ + public static void printWrongCommand() { + System.out.println(DIVIDER_LINE + "Sorry! I don't know what that means.\n" + DIVIDER_LINE); + } + + /** + * prints a message to indicate empty content of a command + */ + public static void printEmptyContent() { + System.out.println(DIVIDER_LINE + "The content of the task cannot be empty!\n" + DIVIDER_LINE); + } + + /** + * prints a message to indicate wrong format for the command + */ + public static void printWrongFormat() { + System.out.println(DIVIDER_LINE + "Wrong format! Please try again\n" + DIVIDER_LINE); + } + + /** + * prints a message to indicate an invalid task number entered by the user + */ + public static void printWrongNumber() { + System.out.println(DIVIDER_LINE + "Please try again with a valid task number\n" + DIVIDER_LINE); + } +}