From 8d473bdbc41680ca00ab7ccc71ec14d36cd78b05 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Wed, 7 Feb 2024 21:35:21 +0800 Subject: [PATCH 01/36] Increment Level 1 --- src/main/java/GermaBot.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/java/GermaBot.java diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java new file mode 100644 index 000000000..2654ed6f8 --- /dev/null +++ b/src/main/java/GermaBot.java @@ -0,0 +1,13 @@ +public class GermaBot { + public static void main(String[] args) { + String WelcomeMessage = "____________________ \n" + + "Hello! GermaBot here! \n" + + "What may I do for you this fine day? \n" + + "____________________"; + String GoodbyeMessage = "____________________ \n" + + "Thanks for using me! Hope you again soon~! \n" + + "____________________"; + System.out.println(WelcomeMessage); + System.out.println(GoodbyeMessage); + } +} From 902153b79926aa5a50a1e22b32abd9adcb78c6e0 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Wed, 7 Feb 2024 21:35:35 +0800 Subject: [PATCH 02/36] no message --- src/main/java/Duke.java | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/main/java/Duke.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334c..000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} From 37892d1d0c8ac738ee37e3ca2d4d062fabcf40ff Mon Sep 17 00:00:00 2001 From: Fureimi Date: Wed, 7 Feb 2024 21:57:32 +0800 Subject: [PATCH 03/36] Level 1-1 --- src/main/java/GermaBot.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java index 2654ed6f8..8ef9c90ae 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/GermaBot.java @@ -1,13 +1,24 @@ +import java.util.Scanner; + public class GermaBot { public static void main(String[] args) { String WelcomeMessage = "____________________ \n" + "Hello! GermaBot here! \n" + "What may I do for you this fine day? \n" + "____________________"; + System.out.println(WelcomeMessage); + while (true) { + String echo; + Scanner in = new Scanner(System.in); + echo = in.nextLine(); + if (echo.equals("bye")) { + break; + } + System.out.println(echo); + } String GoodbyeMessage = "____________________ \n" + "Thanks for using me! Hope you again soon~! \n" + "____________________"; - System.out.println(WelcomeMessage); System.out.println(GoodbyeMessage); } } From 8c29b77a76419b37f33b84262965d5ba702e1b98 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Wed, 7 Feb 2024 22:53:39 +0800 Subject: [PATCH 04/36] Level-2 --- src/main/java/GermaBot.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java index 8ef9c90ae..a01c00ed2 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/GermaBot.java @@ -7,6 +7,8 @@ public static void main(String[] args) { + "What may I do for you this fine day? \n" + "____________________"; System.out.println(WelcomeMessage); + String[] toDoList = new String[100]; + int counter = 0; while (true) { String echo; Scanner in = new Scanner(System.in); @@ -14,11 +16,25 @@ public static void main(String[] args) { if (echo.equals("bye")) { break; } - System.out.println(echo); + if (echo.equals("list")) { + int i = 1; + for (String s : toDoList) { + if (s == null) { + break; + } + System.out.println(i + ". " + s); + i++; + } + } + else { + toDoList[counter] = echo; + System.out.println("Added: " + echo + " to the list!"); + counter += 1; + } } String GoodbyeMessage = "____________________ \n" + "Thanks for using me! Hope you again soon~! \n" + "____________________"; System.out.println(GoodbyeMessage); } -} + } From 373d959045a372a2c5217b8c4c5ba54ed8434661 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Thu, 8 Feb 2024 10:17:50 +0800 Subject: [PATCH 05/36] Level 3 --- src/main/java/GermaBot.java | 39 +++++++++++++++++++++++++------------ src/main/java/Task.java | 29 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java index a01c00ed2..feca153bf 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/GermaBot.java @@ -1,13 +1,17 @@ import java.util.Scanner; public class GermaBot { + public static int getIdx(String input) { + return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; + } + public static void main(String[] args) { String WelcomeMessage = "____________________ \n" + "Hello! GermaBot here! \n" + "What may I do for you this fine day? \n" + "____________________"; System.out.println(WelcomeMessage); - String[] toDoList = new String[100]; + Task[] toDoList = new Task[100]; int counter = 0; while (true) { String echo; @@ -17,19 +21,30 @@ public static void main(String[] args) { break; } if (echo.equals("list")) { - int i = 1; - for (String s : toDoList) { - if (s == null) { + int printCounter = 1; + System.out.println("Gotcha! Here are your tasks:"); + for (int j = 0; j < counter; j++) { + if (toDoList[j] == null) { break; } - System.out.println(i + ". " + s); - i++; + System.out.println(printCounter + ". [" + toDoList[j].getStatusIcon() + "] " + toDoList[j].getDescription()); + printCounter++; } - } - else { - toDoList[counter] = echo; - System.out.println("Added: " + echo + " to the list!"); - counter += 1; + } else if (echo.contains("unmark")) { + int idx = getIdx(echo); + toDoList[idx].setDone(false); + System.out.println("Aww, not done? Okay, I'll mark this task as undone: " + + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); + } else if (echo.contains("mark")) { + int idx = getIdx(echo); + toDoList[idx].setDone(true);t + System.out.println("Good job! I'll mark this task as done: " + + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); + } else { + Task t = new Task(echo); + toDoList[counter] = t; + System.out.println("Added '" + echo + "' to the list!"); + counter++; } } String GoodbyeMessage = "____________________ \n" @@ -37,4 +52,4 @@ public static void main(String[] args) { + "____________________"; System.out.println(GoodbyeMessage); } - } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..f3fc74768 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,29 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public boolean isDone() { + return isDone; + } + + public void setDone(boolean done) { + isDone = done; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } +} From 6da7d59fe448403e0e6eb1757ca05d1e087a0e30 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Thu, 8 Feb 2024 10:24:07 +0800 Subject: [PATCH 06/36] Level 3 --- src/main/java/GermaBot.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java index feca153bf..b11193157 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/GermaBot.java @@ -37,12 +37,13 @@ public static void main(String[] args) { + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); } else if (echo.contains("mark")) { int idx = getIdx(echo); - toDoList[idx].setDone(true);t + toDoList[idx].setDone(true); System.out.println("Good job! I'll mark this task as done: " + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); } else { Task t = new Task(echo); toDoList[counter] = t; + toDoList[counter].setDescription(echo); System.out.println("Added '" + echo + "' to the list!"); counter++; } From b8325175200aaa011be398891eaf486faf4e1cd9 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Wed, 14 Feb 2024 20:25:16 +0800 Subject: [PATCH 07/36] Level 4 --- src/main/java/Deadline.java | 22 ++++++++++ src/main/java/Event.java | 32 ++++++++++++++ src/main/java/GermaBot.java | 84 +++++++++++++++++++++++++++++-------- src/main/java/Task.java | 5 +++ src/main/java/ToDo.java | 10 +++++ 5 files changed, 135 insertions(+), 18 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/ToDo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..7c913e6be --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,22 @@ +public class Deadline extends Task { + + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + public String getBy() { + return by; + } + + public void setBy(String date) { + this.by = date; + } + + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + by + ")"; + } +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..de38c737d --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,32 @@ +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; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; + } +} diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java index b11193157..e13876ce5 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/GermaBot.java @@ -1,51 +1,99 @@ import java.util.Scanner; public class GermaBot { + static int counter = 0; + static Task[] toDoList = new Task[100]; public static int getIdx(String input) { return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; } + public static void echo(String input) { + if (input.contains("todo")) { + System.out.println("Gotcha! Added '" + input + "' to your To Do List!"); + } + else if (input.contains("deadline")) { + System.out.println("Gotcha! Added '" + input + "' to your To Do List!" + + " You have to finish this by a certain time, so be reminded!"); + } + else if (input.contains("event")) { + System.out.println("Gotcha! Added '" + input + "' to your To Do List!" + + " This happens and ends at a specific time, so remember to check your calender! (Or ask me!)"); + } + else { + System.out.println("Uhh.. I'm sorry but I'm not quite sure what '" + input + "' means..." + + " Remember to include the Task Type in front of the input!!"); + } + } + + public static void createTask(String input) { + if (input.contains("todo")) { + toDoList[counter] = new ToDo(input.substring(input.indexOf("todo ") + 5)); + counter++; + echo(input); + } + else if (input.contains("deadline")) { + String description = input.replaceFirst("deadline ", ""); + int idxOfEndDate = description.indexOf("/"); + String Date = description.substring( idxOfEndDate + 4); + toDoList[counter] = new Deadline(description.substring(0, idxOfEndDate - 1), Date); + counter++; + echo(input); + } + else if(input.contains("event")) { + String description = input.replaceFirst("event ", ""); + int idxOfFrom = description.indexOf("/from"); + int idxOfBy = description.indexOf("/to"); + String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); + String endDate = description.substring(idxOfBy + 4); + toDoList[counter] = new Event(description.substring(0, idxOfFrom - 1), startDate, endDate); + counter++; + echo(input); + } else { + echo(input); + } + /*Task t = new Task(input); + toDoList[counter] = t; + toDoList[counter].setDescription(input); + System.out.println("Added '" + input + "' to your To Do List!"); + counter++;*/ + } + public static void main(String[] args) { String WelcomeMessage = "____________________ \n" + "Hello! GermaBot here! \n" + "What may I do for you this fine day? \n" + "____________________"; System.out.println(WelcomeMessage); - Task[] toDoList = new Task[100]; - int counter = 0; while (true) { - String echo; + String input; Scanner in = new Scanner(System.in); - echo = in.nextLine(); - if (echo.equals("bye")) { + input = in.nextLine(); + if (input.equals("bye")) { break; } - if (echo.equals("list")) { + if (input.equals("list")) { int printCounter = 1; System.out.println("Gotcha! Here are your tasks:"); - for (int j = 0; j < counter; j++) { - if (toDoList[j] == null) { + for (int i = 0; i < counter; i++) { + if (toDoList[i] == null) { break; } - System.out.println(printCounter + ". [" + toDoList[j].getStatusIcon() + "] " + toDoList[j].getDescription()); + /*System.out.println(printCounter + ". [" + toDoList[j].getStatusIcon() + "] " + toDoList[j].getDescription());*/ + System.out.println(printCounter + ". " + toDoList[i].toString()); printCounter++; } - } else if (echo.contains("unmark")) { - int idx = getIdx(echo); + } else if (input.contains("unmark")) { + int idx = getIdx(input); toDoList[idx].setDone(false); System.out.println("Aww, not done? Okay, I'll mark this task as undone: " + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); - } else if (echo.contains("mark")) { - int idx = getIdx(echo); + } else if (input.contains("mark")) { + int idx = getIdx(input); toDoList[idx].setDone(true); System.out.println("Good job! I'll mark this task as done: " + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); } else { - Task t = new Task(echo); - toDoList[counter] = t; - toDoList[counter].setDescription(echo); - System.out.println("Added '" + echo + "' to the list!"); - counter++; + createTask(input); } } String GoodbyeMessage = "____________________ \n" diff --git a/src/main/java/Task.java b/src/main/java/Task.java index f3fc74768..611f6ea5b 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -26,4 +26,9 @@ public void setDone(boolean done) { public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + + @Override + public String toString() { + return "[" + this.getStatusIcon() + "] " + this.getDescription(); + } } diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java new file mode 100644 index 000000000..ee72ef43c --- /dev/null +++ b/src/main/java/ToDo.java @@ -0,0 +1,10 @@ +public class ToDo extends Task { + public ToDo(String description) { + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} From ab3c2a20e98164705de9a2eb38850dfc1e62c5c2 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Wed, 14 Feb 2024 20:39:55 +0800 Subject: [PATCH 08/36] A-CodeQuality --- src/main/java/GermaBot.java | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java index e13876ce5..81b93bc46 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/GermaBot.java @@ -2,6 +2,7 @@ public class GermaBot { static int counter = 0; + static final String LINE= "____________________________________________"; static Task[] toDoList = new Task[100]; public static int getIdx(String input) { return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; @@ -9,15 +10,27 @@ public static int getIdx(String input) { public static void echo(String input) { if (input.contains("todo")) { - System.out.println("Gotcha! Added '" + input + "' to your To Do List!"); + String toDoTask = input.substring(input.indexOf("todo ") + 5); + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); } else if (input.contains("deadline")) { - System.out.println("Gotcha! Added '" + input + "' to your To Do List!" + - " You have to finish this by a certain time, so be reminded!"); + String description = input.replaceFirst("deadline ", ""); + int idxOfEndDate = description.indexOf("/"); + String toDoTask = description.substring(0, idxOfEndDate - 1); + String Date = description.substring(idxOfEndDate + 4); + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " You have to finish this by " + Date + ", so be reminded!"); } else if (input.contains("event")) { - System.out.println("Gotcha! Added '" + input + "' to your To Do List!" + - " This happens and ends at a specific time, so remember to check your calender! (Or ask me!)"); + String description = input.replaceFirst("event ", ""); + int idxOfFrom = description.indexOf("/from"); + int idxOfBy = description.indexOf("/to"); + String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); + String endDate = description.substring(idxOfBy + 4); + String toDoTask = description.substring(0, idxOfFrom - 1); + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + + " on your calender! (Or ask me!)"); } else { System.out.println("Uhh.. I'm sorry but I'm not quite sure what '" + input + "' means..." + @@ -59,11 +72,11 @@ else if(input.contains("event")) { } public static void main(String[] args) { - String WelcomeMessage = "____________________ \n" - + "Hello! GermaBot here! \n" - + "What may I do for you this fine day? \n" - + "____________________"; + String WelcomeMessage = "Hello! GermaBot here! \n" + + "What may I do for you this fine day?"; + System.out.println(LINE); System.out.println(WelcomeMessage); + System.out.println(LINE); while (true) { String input; Scanner in = new Scanner(System.in); @@ -96,9 +109,9 @@ public static void main(String[] args) { createTask(input); } } - String GoodbyeMessage = "____________________ \n" - + "Thanks for using me! Hope you again soon~! \n" - + "____________________"; + String GoodbyeMessage = "Thanks for using me! Hope you again soon~!"; + System.out.println(LINE); System.out.println(GoodbyeMessage); + System.out.println(LINE); } } From a5e25f406ce53003fa6bd1548f3c1787f99982bf Mon Sep 17 00:00:00 2001 From: Fureimi Date: Thu, 15 Feb 2024 14:46:45 +0800 Subject: [PATCH 09/36] Some clean up --- src/main/java/GermaBot.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java index 81b93bc46..41ef88ac1 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/GermaBot.java @@ -64,11 +64,6 @@ else if(input.contains("event")) { } else { echo(input); } - /*Task t = new Task(input); - toDoList[counter] = t; - toDoList[counter].setDescription(input); - System.out.println("Added '" + input + "' to your To Do List!"); - counter++;*/ } public static void main(String[] args) { @@ -91,7 +86,6 @@ public static void main(String[] args) { if (toDoList[i] == null) { break; } - /*System.out.println(printCounter + ". [" + toDoList[j].getStatusIcon() + "] " + toDoList[j].getDescription());*/ System.out.println(printCounter + ". " + toDoList[i].toString()); printCounter++; } @@ -109,7 +103,7 @@ public static void main(String[] args) { createTask(input); } } - String GoodbyeMessage = "Thanks for using me! Hope you again soon~!"; + String GoodbyeMessage = "Thanks for using me! Hope to see you again soon~!"; System.out.println(LINE); System.out.println(GoodbyeMessage); System.out.println(LINE); From c771a0759656dc10544b9b074a6011de1a4e38c8 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Sun, 18 Feb 2024 17:21:22 +0800 Subject: [PATCH 10/36] Level 5 --- src/main/java/EmptyTaskException.java | 2 + src/main/java/GermaBot.java | 127 ++++++++++++------- src/main/java/MissingDeadlineException.java | 2 + src/main/java/MissingStartDateException.java | 2 + src/main/java/UnknownInputException.java | 2 + 5 files changed, 90 insertions(+), 45 deletions(-) create mode 100644 src/main/java/EmptyTaskException.java create mode 100644 src/main/java/MissingDeadlineException.java create mode 100644 src/main/java/MissingStartDateException.java create mode 100644 src/main/java/UnknownInputException.java diff --git a/src/main/java/EmptyTaskException.java b/src/main/java/EmptyTaskException.java new file mode 100644 index 000000000..daa526f32 --- /dev/null +++ b/src/main/java/EmptyTaskException.java @@ -0,0 +1,2 @@ +public class EmptyTaskException extends Exception{ +} diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java index 41ef88ac1..2e0c4d601 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/GermaBot.java @@ -8,61 +8,93 @@ public static int getIdx(String input) { return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; } - public static void echo(String input) { - if (input.contains("todo")) { - String toDoTask = input.substring(input.indexOf("todo ") + 5); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); + public static void createTodo(String input) throws EmptyTaskException { + String toDoTask = input.substring(input.indexOf("todo ") + 5); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); } - else if (input.contains("deadline")) { - String description = input.replaceFirst("deadline ", ""); - int idxOfEndDate = description.indexOf("/"); - String toDoTask = description.substring(0, idxOfEndDate - 1); - String Date = description.substring(idxOfEndDate + 4); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + - " You have to finish this by " + Date + ", so be reminded!"); + toDoList[counter] = new ToDo(toDoTask); + counter++; + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); + } + + public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException { + String description = input.replaceFirst("deadline ", ""); + int idxOfEndDate = description.indexOf("/"); + if (idxOfEndDate == -1) { + throw new MissingDeadlineException(); } - else if (input.contains("event")) { - String description = input.replaceFirst("event ", ""); - int idxOfFrom = description.indexOf("/from"); - int idxOfBy = description.indexOf("/to"); - String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); - String endDate = description.substring(idxOfBy + 4); - String toDoTask = description.substring(0, idxOfFrom - 1); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + - " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + - " on your calender! (Or ask me!)"); + String date = description.substring( idxOfEndDate + 4); + String toDoTask = description.substring(0, idxOfEndDate - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList[counter] = new Deadline(description.substring(0, idxOfEndDate - 1), date); + counter++; + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " You have to finish this by " + date + ", so be reminded!"); + } + + public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, + MissingStartDateException { + String description = input.replaceFirst("event ", ""); + int idxOfFrom = description.indexOf("/from"); + if (idxOfFrom == -1) { + throw new EmptyTaskException(); + } + int idxOfBy = description.indexOf("/to"); + if (idxOfBy == -1) { + throw new MissingDeadlineException(); } - else { - System.out.println("Uhh.. I'm sorry but I'm not quite sure what '" + input + "' means..." + - " Remember to include the Task Type in front of the input!!"); + String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); + if (startDate.isBlank()) { + throw new MissingStartDateException(); } + String endDate = description.substring(idxOfBy + 4); + if (endDate.isBlank()) { + throw new MissingDeadlineException(); + } + String toDoTask = description.substring(0, idxOfFrom - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList[counter] = new Event(description.substring(0, idxOfFrom - 1), startDate, endDate); + counter++; + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + + " on your calender! (Or ask me!)"); } - public static void createTask(String input) { + public static void createTask(String input) throws UnknownInputException{ if (input.contains("todo")) { - toDoList[counter] = new ToDo(input.substring(input.indexOf("todo ") + 5)); - counter++; - echo(input); + try { + createTodo(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } } else if (input.contains("deadline")) { - String description = input.replaceFirst("deadline ", ""); - int idxOfEndDate = description.indexOf("/"); - String Date = description.substring( idxOfEndDate + 4); - toDoList[counter] = new Deadline(description.substring(0, idxOfEndDate - 1), Date); - counter++; - echo(input); + try { + createDeadline(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (MissingDeadlineException e) { + System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + } + } - else if(input.contains("event")) { - String description = input.replaceFirst("event ", ""); - int idxOfFrom = description.indexOf("/from"); - int idxOfBy = description.indexOf("/to"); - String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); - String endDate = description.substring(idxOfBy + 4); - toDoList[counter] = new Event(description.substring(0, idxOfFrom - 1), startDate, endDate); - counter++; - echo(input); + else if (input.contains("event")) { + try { + createEvent(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (MissingStartDateException e) { + System.out.println("Uh oh, you did not specify a start time! Let's try again!"); + } catch (MissingDeadlineException e) { + System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + } } else { - echo(input); + throw new UnknownInputException(); } } @@ -100,7 +132,12 @@ public static void main(String[] args) { System.out.println("Good job! I'll mark this task as done: " + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); } else { - createTask(input); + try { + createTask(input); + } catch (UnknownInputException e){ + System.out.println("Uhh.. I'm sorry but I'm not quite sure what '" + input + "' means..." + + " Remember to include the Task Type in front of the input!!"); + } } } String GoodbyeMessage = "Thanks for using me! Hope to see you again soon~!"; diff --git a/src/main/java/MissingDeadlineException.java b/src/main/java/MissingDeadlineException.java new file mode 100644 index 000000000..fcf99eb8c --- /dev/null +++ b/src/main/java/MissingDeadlineException.java @@ -0,0 +1,2 @@ +public class MissingDeadlineException extends Exception{ +} diff --git a/src/main/java/MissingStartDateException.java b/src/main/java/MissingStartDateException.java new file mode 100644 index 000000000..294f71630 --- /dev/null +++ b/src/main/java/MissingStartDateException.java @@ -0,0 +1,2 @@ +public class MissingStartDateException extends Exception{ +} diff --git a/src/main/java/UnknownInputException.java b/src/main/java/UnknownInputException.java new file mode 100644 index 000000000..ee928be8f --- /dev/null +++ b/src/main/java/UnknownInputException.java @@ -0,0 +1,2 @@ +public class UnknownInputException extends Exception{ +} From 068ec723b3a2d2de4a08d02ab09b5153267c8a13 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Sun, 18 Feb 2024 20:40:49 +0800 Subject: [PATCH 11/36] A-Packages with some exceptions refining --- .../{ => Exceptions}/EmptyTaskException.java | 2 + .../MissingDeadlineException.java | 2 + .../MissingStartDateException.java | 2 + .../UnknownInputException.java | 2 + src/main/java/{ => MainRuntime}/GermaBot.java | 37 ++++++++++++++----- src/main/java/{ => Tasks}/Deadline.java | 2 + src/main/java/{ => Tasks}/Event.java | 4 ++ src/main/java/{ => Tasks}/Task.java | 2 + src/main/java/{ => Tasks}/ToDo.java | 4 ++ 9 files changed, 48 insertions(+), 9 deletions(-) rename src/main/java/{ => Exceptions}/EmptyTaskException.java (71%) rename src/main/java/{ => Exceptions}/MissingDeadlineException.java (73%) rename src/main/java/{ => Exceptions}/MissingStartDateException.java (74%) rename src/main/java/{ => Exceptions}/UnknownInputException.java (72%) rename src/main/java/{ => MainRuntime}/GermaBot.java (84%) rename src/main/java/{ => Tasks}/Deadline.java (96%) rename src/main/java/{ => Tasks}/Event.java (94%) rename src/main/java/{ => Tasks}/Task.java (97%) rename src/main/java/{ => Tasks}/ToDo.java (84%) diff --git a/src/main/java/EmptyTaskException.java b/src/main/java/Exceptions/EmptyTaskException.java similarity index 71% rename from src/main/java/EmptyTaskException.java rename to src/main/java/Exceptions/EmptyTaskException.java index daa526f32..bc2cd15ab 100644 --- a/src/main/java/EmptyTaskException.java +++ b/src/main/java/Exceptions/EmptyTaskException.java @@ -1,2 +1,4 @@ +package Exceptions; + public class EmptyTaskException extends Exception{ } diff --git a/src/main/java/MissingDeadlineException.java b/src/main/java/Exceptions/MissingDeadlineException.java similarity index 73% rename from src/main/java/MissingDeadlineException.java rename to src/main/java/Exceptions/MissingDeadlineException.java index fcf99eb8c..c32f8ac8e 100644 --- a/src/main/java/MissingDeadlineException.java +++ b/src/main/java/Exceptions/MissingDeadlineException.java @@ -1,2 +1,4 @@ +package Exceptions; + public class MissingDeadlineException extends Exception{ } diff --git a/src/main/java/MissingStartDateException.java b/src/main/java/Exceptions/MissingStartDateException.java similarity index 74% rename from src/main/java/MissingStartDateException.java rename to src/main/java/Exceptions/MissingStartDateException.java index 294f71630..13d6b8714 100644 --- a/src/main/java/MissingStartDateException.java +++ b/src/main/java/Exceptions/MissingStartDateException.java @@ -1,2 +1,4 @@ +package Exceptions; + public class MissingStartDateException extends Exception{ } diff --git a/src/main/java/UnknownInputException.java b/src/main/java/Exceptions/UnknownInputException.java similarity index 72% rename from src/main/java/UnknownInputException.java rename to src/main/java/Exceptions/UnknownInputException.java index ee928be8f..634325351 100644 --- a/src/main/java/UnknownInputException.java +++ b/src/main/java/Exceptions/UnknownInputException.java @@ -1,2 +1,4 @@ +package Exceptions; + public class UnknownInputException extends Exception{ } diff --git a/src/main/java/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java similarity index 84% rename from src/main/java/GermaBot.java rename to src/main/java/MainRuntime/GermaBot.java index 2e0c4d601..0a82a47c5 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -1,3 +1,14 @@ +package MainRuntime; + +import Exceptions.EmptyTaskException; +import Exceptions.MissingDeadlineException; +import Exceptions.MissingStartDateException; +import Exceptions.UnknownInputException; +import Tasks.Deadline; +import Tasks.Event; +import Tasks.Task; +import Tasks.ToDo; + import java.util.Scanner; public class GermaBot { @@ -20,6 +31,9 @@ public static void createTodo(String input) throws EmptyTaskException { public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException { String description = input.replaceFirst("deadline ", ""); + if (description.equals("deadline")) { + throw new EmptyTaskException(); + } int idxOfEndDate = description.indexOf("/"); if (idxOfEndDate == -1) { throw new MissingDeadlineException(); @@ -65,7 +79,7 @@ public static void createEvent(String input) throws EmptyTaskException, MissingD " on your calender! (Or ask me!)"); } - public static void createTask(String input) throws UnknownInputException{ + public static void createTask(String input) throws UnknownInputException { if (input.contains("todo")) { try { createTodo(input); @@ -99,7 +113,7 @@ else if (input.contains("event")) { } public static void main(String[] args) { - String WelcomeMessage = "Hello! GermaBot here! \n" + String WelcomeMessage = "Hello! MainRuntime.GermaBot here! \n" + "What may I do for you this fine day?"; System.out.println(LINE); System.out.println(WelcomeMessage); @@ -112,14 +126,19 @@ public static void main(String[] args) { break; } if (input.equals("list")) { - int printCounter = 1; - System.out.println("Gotcha! Here are your tasks:"); - for (int i = 0; i < counter; i++) { - if (toDoList[i] == null) { - break; + if (toDoList[0] == null) { + System.out.println("Umm... You haven't added any Tasks yet... Let's try adding " + + "some now!"); + } else { + int printCounter = 1; + System.out.println("Gotcha! Here are your tasks:"); + for (int i = 0; i < counter; i++) { + if (toDoList[i] == null) { + break; + } + System.out.println(printCounter + ". " + toDoList[i].toString()); + printCounter++; } - System.out.println(printCounter + ". " + toDoList[i].toString()); - printCounter++; } } else if (input.contains("unmark")) { int idx = getIdx(input); diff --git a/src/main/java/Deadline.java b/src/main/java/Tasks/Deadline.java similarity index 96% rename from src/main/java/Deadline.java rename to src/main/java/Tasks/Deadline.java index 7c913e6be..c0470b7a9 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Tasks/Deadline.java @@ -1,3 +1,5 @@ +package Tasks; + public class Deadline extends Task { protected String by; diff --git a/src/main/java/Event.java b/src/main/java/Tasks/Event.java similarity index 94% rename from src/main/java/Event.java rename to src/main/java/Tasks/Event.java index de38c737d..b55f26b02 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Tasks/Event.java @@ -1,3 +1,7 @@ +package Tasks; + +import Tasks.Task; + public class Event extends Task { protected String from; diff --git a/src/main/java/Task.java b/src/main/java/Tasks/Task.java similarity index 97% rename from src/main/java/Task.java rename to src/main/java/Tasks/Task.java index 611f6ea5b..beb238781 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Tasks/Task.java @@ -1,3 +1,5 @@ +package Tasks; + public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/ToDo.java b/src/main/java/Tasks/ToDo.java similarity index 84% rename from src/main/java/ToDo.java rename to src/main/java/Tasks/ToDo.java index ee72ef43c..8ac8b949c 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/Tasks/ToDo.java @@ -1,3 +1,7 @@ +package Tasks; + +import Tasks.Task; + public class ToDo extends Task { public ToDo(String description) { super(description); From a0124d81f31e7081b72ee79be1aae46f54a578d7 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Sun, 25 Feb 2024 17:01:04 +0800 Subject: [PATCH 12/36] Added Array List Class --- src/main/java/META-INF/MANIFEST.MF | 3 +++ src/main/java/MainRuntime/GermaBot.java | 29 ++++++++++++------------- 2 files changed, 17 insertions(+), 15 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..f6ace6ee9 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: MainRuntime.GermaBot + diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 0a82a47c5..e98268119 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -10,11 +10,12 @@ import Tasks.ToDo; import java.util.Scanner; +import java.util.ArrayList; public class GermaBot { static int counter = 0; static final String LINE= "____________________________________________"; - static Task[] toDoList = new Task[100]; + static ArrayList toDoList = new ArrayList<>(); public static int getIdx(String input) { return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; } @@ -24,7 +25,7 @@ public static void createTodo(String input) throws EmptyTaskException { if (toDoTask.isBlank()) { throw new EmptyTaskException(); } - toDoList[counter] = new ToDo(toDoTask); + toDoList.add(new ToDo(toDoTask)); counter++; System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); } @@ -43,7 +44,7 @@ public static void createDeadline(String input) throws EmptyTaskException, Missi if (toDoTask.isBlank()) { throw new EmptyTaskException(); } - toDoList[counter] = new Deadline(description.substring(0, idxOfEndDate - 1), date); + toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); counter++; System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + " You have to finish this by " + date + ", so be reminded!"); @@ -72,7 +73,7 @@ public static void createEvent(String input) throws EmptyTaskException, MissingD if (toDoTask.isBlank()) { throw new EmptyTaskException(); } - toDoList[counter] = new Event(description.substring(0, idxOfFrom - 1), startDate, endDate); + toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); counter++; System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + @@ -113,7 +114,7 @@ else if (input.contains("event")) { } public static void main(String[] args) { - String WelcomeMessage = "Hello! MainRuntime.GermaBot here! \n" + String WelcomeMessage = "Hello! GermaBot here! \n" + "What may I do for you this fine day?"; System.out.println(LINE); System.out.println(WelcomeMessage); @@ -126,35 +127,33 @@ public static void main(String[] args) { break; } if (input.equals("list")) { - if (toDoList[0] == null) { + if (toDoList.isEmpty()) { System.out.println("Umm... You haven't added any Tasks yet... Let's try adding " + "some now!"); } else { - int printCounter = 1; System.out.println("Gotcha! Here are your tasks:"); for (int i = 0; i < counter; i++) { - if (toDoList[i] == null) { + if (toDoList.get(i) == null) { break; } - System.out.println(printCounter + ". " + toDoList[i].toString()); - printCounter++; + System.out.println(i + 1 + ". " + toDoList.get(i)); } } } else if (input.contains("unmark")) { int idx = getIdx(input); - toDoList[idx].setDone(false); + toDoList.get(idx).setDone(false); System.out.println("Aww, not done? Okay, I'll mark this task as undone: " - + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); + + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); } else if (input.contains("mark")) { int idx = getIdx(input); - toDoList[idx].setDone(true); + toDoList.get(idx).setDone(true); System.out.println("Good job! I'll mark this task as done: " - + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); + + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); } else { try { createTask(input); } catch (UnknownInputException e){ - System.out.println("Uhh.. I'm sorry but I'm not quite sure what '" + input + "' means..." + + System.out.println("Uhh.. I'm sorry but I'm not quite sure what in the world '" + input + "' means..." + " Remember to include the Task Type in front of the input!!"); } } From 91efd3ca4d99eee7db821f6dec0f6ad9eb8d1b48 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Sun, 25 Feb 2024 17:29:45 +0800 Subject: [PATCH 13/36] Added delete function to the bot --- .../Exceptions/TaskNotFoundException.java | 4 +++ src/main/java/MainRuntime/GermaBot.java | 33 +++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 src/main/java/Exceptions/TaskNotFoundException.java diff --git a/src/main/java/Exceptions/TaskNotFoundException.java b/src/main/java/Exceptions/TaskNotFoundException.java new file mode 100644 index 000000000..3002f95d9 --- /dev/null +++ b/src/main/java/Exceptions/TaskNotFoundException.java @@ -0,0 +1,4 @@ +package Exceptions; + +public class TaskNotFoundException extends Exception{ +} diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index e98268119..7d729da75 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -1,9 +1,6 @@ package MainRuntime; -import Exceptions.EmptyTaskException; -import Exceptions.MissingDeadlineException; -import Exceptions.MissingStartDateException; -import Exceptions.UnknownInputException; +import Exceptions.*; import Tasks.Deadline; import Tasks.Event; import Tasks.Task; @@ -80,15 +77,37 @@ public static void createEvent(String input) throws EmptyTaskException, MissingD " on your calender! (Or ask me!)"); } + public static void deleteTask(String input) throws TaskNotFoundException { + int idxToDelete = getIdx(input); + if (idxToDelete >= counter) { + throw new TaskNotFoundException(); + } else { + System.out.println("Okay! I've removed this task from your To Do List:"); + System.out.println(toDoList.get(idxToDelete)); + toDoList.remove(idxToDelete); + System.out.println("Now you have, let me count... " + toDoList.size() + " items left in your " + + "To Do List!"); + counter--; + } + } + public static void createTask(String input) throws UnknownInputException { - if (input.contains("todo")) { + if (input.startsWith("delete")) { + try { + deleteTask(input); + } catch (TaskNotFoundException e) { + System.out.println("Sorry but... There is no task number " + (getIdx(input) + 1) + "... " + + "Let's try again!"); + } + } + else if (input.startsWith("todo")) { try { createTodo(input); } catch (EmptyTaskException e) { System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); } } - else if (input.contains("deadline")) { + else if (input.startsWith("deadline")) { try { createDeadline(input); } catch (EmptyTaskException e) { @@ -98,7 +117,7 @@ else if (input.contains("deadline")) { } } - else if (input.contains("event")) { + else if (input.startsWith("event")) { try { createEvent(input); } catch (EmptyTaskException e) { From 4ceb8bcd5e76e2b16a6659dc0d4bcae5200f22eb Mon Sep 17 00:00:00 2001 From: Fureimi Date: Mon, 26 Feb 2024 00:02:59 +0800 Subject: [PATCH 14/36] added file reading and writing along with some touch ups --- src/data/GermaBotData.txt | 3 + src/main/java/DataHandling/SaveData.java | 30 +++++ .../java/Exceptions/FileReadException.java | 4 + .../java/Exceptions/LoadFileException.java | 4 + src/main/java/MainRuntime/GermaBot.java | 115 ++++++++++++++++-- 5 files changed, 144 insertions(+), 12 deletions(-) create mode 100644 src/data/GermaBotData.txt create mode 100644 src/main/java/DataHandling/SaveData.java create mode 100644 src/main/java/Exceptions/FileReadException.java create mode 100644 src/main/java/Exceptions/LoadFileException.java diff --git a/src/data/GermaBotData.txt b/src/data/GermaBotData.txt new file mode 100644 index 000000000..0739da9ee --- /dev/null +++ b/src/data/GermaBotData.txt @@ -0,0 +1,3 @@ +T | 1 | Read a book +D | 0 | Finish IP | tmr +E | 1 | Do NOT drop out (IMPOSSIBLE) | now | end of semester diff --git a/src/main/java/DataHandling/SaveData.java b/src/main/java/DataHandling/SaveData.java new file mode 100644 index 000000000..d22ca1d42 --- /dev/null +++ b/src/main/java/DataHandling/SaveData.java @@ -0,0 +1,30 @@ +package DataHandling; + +import Tasks.*; + +import java.io.FileWriter; +import java.io.IOException; + +public class SaveData { + private static final String filePath = "src/data/GermaBotData.txt"; + + public static void addTodoToFile(ToDo toDoTask, char taskType) throws IOException { + FileWriter writer = new FileWriter(filePath, true); + writer.write("T | " + (toDoTask.isDone() ? "1" : "0") + " | " + toDoTask.getDescription() + System.lineSeparator()); + writer.close(); + } + + public static void addDeadlineToFile(Deadline deadlineTask, char taskType) throws IOException { + FileWriter writer = new FileWriter(filePath, true); + writer.write("D | " + (deadlineTask.isDone() ? "1" : "0") + " | " + deadlineTask.getDescription() + + " | " + deadlineTask.getBy() + System.lineSeparator()); + writer.close(); + } + + public static void addEventToFile(Event eventTask, char taskType) throws IOException { + FileWriter writer = new FileWriter(filePath, true); + writer.write("E | " + (eventTask.isDone() ? "1" : "0") + " | " + eventTask.getDescription() + + " | " + eventTask.getFrom() + " | " + eventTask.getTo() + System.lineSeparator()); + writer.close(); + } +} diff --git a/src/main/java/Exceptions/FileReadException.java b/src/main/java/Exceptions/FileReadException.java new file mode 100644 index 000000000..8a7adda56 --- /dev/null +++ b/src/main/java/Exceptions/FileReadException.java @@ -0,0 +1,4 @@ +package Exceptions; + +public class FileReadException extends Exception{ +} diff --git a/src/main/java/Exceptions/LoadFileException.java b/src/main/java/Exceptions/LoadFileException.java new file mode 100644 index 000000000..2af6e102f --- /dev/null +++ b/src/main/java/Exceptions/LoadFileException.java @@ -0,0 +1,4 @@ +package Exceptions; + +public class LoadFileException extends Exception { +} diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 7d729da75..8ca94b967 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -1,33 +1,107 @@ package MainRuntime; import Exceptions.*; -import Tasks.Deadline; -import Tasks.Event; -import Tasks.Task; -import Tasks.ToDo; +import Tasks.*; +import javax.lang.model.type.NullType; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; +import java.io.File; public class GermaBot { static int counter = 0; static final String LINE= "____________________________________________"; + static final String WELCOME_MESSAGE = "Hello! GermaBot here! \n" + + "Let me load your saved To Do List first..."; + static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; static ArrayList toDoList = new ArrayList<>(); + + public static void loadToDo(String description) throws LoadFileException { + if (description.isEmpty()) { + throw new LoadFileException(); + } + toDoList.add(new ToDo(description)); + } + + public static void loadDeadline(String description) throws LoadFileException { + String[] deadlineDescription = description.split("\\|"); + String task = deadlineDescription[0].trim(); + String by = deadlineDescription[1].trim(); + if (task.isBlank() || by.isBlank()) { + throw new LoadFileException(); + } + toDoList.add(new Deadline(task, by)); + } + + public static void loadEvent(String description) throws LoadFileException { + String[] eventDescription = description.split("\\|"); + String task = eventDescription[0].trim(); + String from = eventDescription[1].trim(); + String by = eventDescription[2].trim(); + if (task.isBlank() || from.isBlank() || by.isBlank()) { + throw new LoadFileException(); + } + toDoList.add(new Event(task, from, by)); + } + + public static void loadFile () throws FileNotFoundException, FileReadException { + File data = new File("src/data/GermaBotData.txt"); + Scanner fileInput = new Scanner(data); + do { + String task = fileInput.nextLine(); + if (task.trim().isEmpty()) { + throw new FileReadException(); + } + counter++; + char type = task.charAt(0); + String description = task.substring(8); + boolean isCompleted = task.charAt(4) != '0'; + if (type == 'T') { + try { + loadToDo(description); + } catch (LoadFileException e) { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + } else if (type == 'D') { + try { + loadDeadline(description); + } catch (LoadFileException e) { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + } else if (type == 'E') { + try { + loadEvent(description); + } catch (LoadFileException e) { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + } else { + break; + } + int idxToMark = toDoList.size() - 1; + toDoList.get(idxToMark).setDone(isCompleted); + } while (fileInput.hasNext()); + } public static int getIdx(String input) { return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; } - public static void createTodo(String input) throws EmptyTaskException { + public static void createTodo(String input) throws EmptyTaskException, IOException { String toDoTask = input.substring(input.indexOf("todo ") + 5); if (toDoTask.isBlank()) { throw new EmptyTaskException(); } toDoList.add(new ToDo(toDoTask)); counter++; + DataHandling.SaveData.addTodoToFile(new ToDo(toDoTask), 'T'); System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); } - public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException { + public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException, IOException { String description = input.replaceFirst("deadline ", ""); if (description.equals("deadline")) { throw new EmptyTaskException(); @@ -43,12 +117,13 @@ public static void createDeadline(String input) throws EmptyTaskException, Missi } toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); counter++; + DataHandling.SaveData.addDeadlineToFile(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + " You have to finish this by " + date + ", so be reminded!"); } public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, - MissingStartDateException { + MissingStartDateException, IOException { String description = input.replaceFirst("event ", ""); int idxOfFrom = description.indexOf("/from"); if (idxOfFrom == -1) { @@ -72,6 +147,7 @@ public static void createEvent(String input) throws EmptyTaskException, MissingD } toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); counter++; + DataHandling.SaveData.addEventToFile(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + " on your calender! (Or ask me!)"); @@ -105,6 +181,8 @@ else if (input.startsWith("todo")) { createTodo(input); } catch (EmptyTaskException e) { System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (IOException e) { + System.out.println("Uh oh, I could not save that to the file..."); } } else if (input.startsWith("deadline")) { @@ -114,6 +192,8 @@ else if (input.startsWith("deadline")) { System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); } catch (MissingDeadlineException e) { System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + } catch (IOException e) { + System.out.println("Uh oh, I could not save that to the file..."); } } @@ -126,6 +206,8 @@ else if (input.startsWith("event")) { System.out.println("Uh oh, you did not specify a start time! Let's try again!"); } catch (MissingDeadlineException e) { System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + } catch (IOException e) { + System.out.println("Uh oh, I could not save that to the file..."); } } else { throw new UnknownInputException(); @@ -133,10 +215,20 @@ else if (input.startsWith("event")) { } public static void main(String[] args) { - String WelcomeMessage = "Hello! GermaBot here! \n" - + "What may I do for you this fine day?"; System.out.println(LINE); - System.out.println(WelcomeMessage); + System.out.println(WELCOME_MESSAGE); + try { + loadFile(); + System.out.println("Loading complete!"); + } catch (FileNotFoundException e) { + System.out.println("Uh oh, there is no saved file yet! Please create a GermaBotsData.txt " + + "file under './src/data/GermaBotData'!!\n" + + "Because there is no saved file found, I will start with an empty list!"); + } catch (FileReadException e) { + System.out.println("Uh oh, there seems to be an issue with the save file...\n" + + "Because there is an error with the save file, I will start with an empty list!"); + } + System.out.println("What may I do for you this fine day?"); System.out.println(LINE); while (true) { String input; @@ -177,9 +269,8 @@ public static void main(String[] args) { } } } - String GoodbyeMessage = "Thanks for using me! Hope to see you again soon~!"; System.out.println(LINE); - System.out.println(GoodbyeMessage); + System.out.println(GOODBYE_MESSAGE); System.out.println(LINE); } } From fd754ce8366cba96e1a3e3173cc4759e07d15ba6 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Mon, 26 Feb 2024 00:10:02 +0800 Subject: [PATCH 15/36] made some changes according to TA comments --- src/main/java/MainRuntime/GermaBot.java | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 8ca94b967..0d496ef44 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -3,7 +3,6 @@ import Exceptions.*; import Tasks.*; -import javax.lang.model.type.NullType; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; @@ -110,7 +109,7 @@ public static void createDeadline(String input) throws EmptyTaskException, Missi if (idxOfEndDate == -1) { throw new MissingDeadlineException(); } - String date = description.substring( idxOfEndDate + 4); + String date = description.substring(idxOfEndDate + 4); String toDoTask = description.substring(0, idxOfEndDate - 1); if (toDoTask.isBlank()) { throw new EmptyTaskException(); @@ -195,7 +194,6 @@ else if (input.startsWith("deadline")) { } catch (IOException e) { System.out.println("Uh oh, I could not save that to the file..."); } - } else if (input.startsWith("event")) { try { @@ -214,6 +212,16 @@ else if (input.startsWith("event")) { } } + public static void printList() { + System.out.println("Gotcha! Here are your tasks:"); + for (int i = 0; i < counter; i++) { + if (toDoList.get(i) == null) { + break; + } + System.out.println(i + 1 + ". " + toDoList.get(i)); + } + } + public static void main(String[] args) { System.out.println(LINE); System.out.println(WELCOME_MESSAGE); @@ -242,14 +250,8 @@ public static void main(String[] args) { System.out.println("Umm... You haven't added any Tasks yet... Let's try adding " + "some now!"); } else { - System.out.println("Gotcha! Here are your tasks:"); - for (int i = 0; i < counter; i++) { - if (toDoList.get(i) == null) { - break; - } - System.out.println(i + 1 + ". " + toDoList.get(i)); + printList(); } - } } else if (input.contains("unmark")) { int idx = getIdx(input); toDoList.get(idx).setDone(false); From 4b7773ecb79f13b26268b2d8b6acd7cb2d42bfa2 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Mon, 26 Feb 2024 00:14:35 +0800 Subject: [PATCH 16/36] removed some magic numbers --- src/data/GermaBotData.txt | 1 + src/main/java/MainRuntime/GermaBot.java | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/data/GermaBotData.txt b/src/data/GermaBotData.txt index 0739da9ee..2f22d3776 100644 --- a/src/data/GermaBotData.txt +++ b/src/data/GermaBotData.txt @@ -1,3 +1,4 @@ T | 1 | Read a book D | 0 | Finish IP | tmr E | 1 | Do NOT drop out (IMPOSSIBLE) | now | end of semester +D | 0 | test | tomorrow diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 0d496ef44..7159e689d 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -90,7 +90,8 @@ public static int getIdx(String input) { } public static void createTodo(String input) throws EmptyTaskException, IOException { - String toDoTask = input.substring(input.indexOf("todo ") + 5); + int idxBetweenTodoAndDescription = 5; + String toDoTask = input.substring(input.indexOf("todo ") + idxBetweenTodoAndDescription); if (toDoTask.isBlank()) { throw new EmptyTaskException(); } @@ -109,7 +110,8 @@ public static void createDeadline(String input) throws EmptyTaskException, Missi if (idxOfEndDate == -1) { throw new MissingDeadlineException(); } - String date = description.substring(idxOfEndDate + 4); + int idxToDateFromEndDate = 4; + String date = description.substring(idxOfEndDate + idxToDateFromEndDate); String toDoTask = description.substring(0, idxOfEndDate - 1); if (toDoTask.isBlank()) { throw new EmptyTaskException(); @@ -132,11 +134,13 @@ public static void createEvent(String input) throws EmptyTaskException, MissingD if (idxOfBy == -1) { throw new MissingDeadlineException(); } - String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); + int idxFromFromToStartDate = 6; + String startDate = description.substring(idxOfFrom + idxFromFromToStartDate, idxOfBy - 1); if (startDate.isBlank()) { throw new MissingStartDateException(); } - String endDate = description.substring(idxOfBy + 4); + int idxFromByToEndDate = 4; + String endDate = description.substring(idxOfBy + idxFromByToEndDate); if (endDate.isBlank()) { throw new MissingDeadlineException(); } From 2e7a416f49e6f262f6501626886b40a3cfa1852c Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Tue, 5 Mar 2024 15:39:14 +0800 Subject: [PATCH 17/36] Add UI class for future use --- src/data/GermaBotData.txt | 1 + src/main/java/MainRuntime/GermaBot.java | 7 ++++--- src/main/java/UiOutputs.java | 6 ++++++ 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 src/main/java/UiOutputs.java diff --git a/src/data/GermaBotData.txt b/src/data/GermaBotData.txt index 2f22d3776..03a1ee652 100644 --- a/src/data/GermaBotData.txt +++ b/src/data/GermaBotData.txt @@ -2,3 +2,4 @@ T | 1 | Read a book D | 0 | Finish IP | tmr E | 1 | Do NOT drop out (IMPOSSIBLE) | now | end of semester D | 0 | test | tomorrow +T | 0 | Cry diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 7159e689d..3211abf51 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -2,6 +2,7 @@ import Exceptions.*; import Tasks.*; +import DataHandling.SaveData; import java.io.FileNotFoundException; import java.io.IOException; @@ -97,7 +98,7 @@ public static void createTodo(String input) throws EmptyTaskException, IOExcepti } toDoList.add(new ToDo(toDoTask)); counter++; - DataHandling.SaveData.addTodoToFile(new ToDo(toDoTask), 'T'); + SaveData.addTodoToFile(new ToDo(toDoTask), 'T'); System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); } @@ -118,7 +119,7 @@ public static void createDeadline(String input) throws EmptyTaskException, Missi } toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); counter++; - DataHandling.SaveData.addDeadlineToFile(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); + SaveData.addDeadlineToFile(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + " You have to finish this by " + date + ", so be reminded!"); } @@ -150,7 +151,7 @@ public static void createEvent(String input) throws EmptyTaskException, MissingD } toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); counter++; - DataHandling.SaveData.addEventToFile(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); + SaveData.addEventToFile(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + " on your calender! (Or ask me!)"); diff --git a/src/main/java/UiOutputs.java b/src/main/java/UiOutputs.java new file mode 100644 index 000000000..f333f0377 --- /dev/null +++ b/src/main/java/UiOutputs.java @@ -0,0 +1,6 @@ +public class UiOutputs { + static final String LINE= "____________________________________________"; + static final String WELCOME_MESSAGE = "Hello! GermaBot here! \n" + + "Let me load your saved To Do List first..."; + static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; +} From 3d32a68cbf7d358147a7222a4bbddd4e65f9af69 Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Wed, 6 Mar 2024 17:37:41 +0800 Subject: [PATCH 18/36] upload for PC use --- src/main/java/DataHandling/LoadData.java | 89 +++++++++++++++++++++ src/main/java/GermaBot/Logic.java | 15 ++++ src/main/java/{ => GermaBot}/UiOutputs.java | 7 ++ src/main/java/MainRuntime/GermaBot.java | 9 ++- 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/main/java/DataHandling/LoadData.java create mode 100644 src/main/java/GermaBot/Logic.java rename src/main/java/{ => GermaBot}/UiOutputs.java (68%) diff --git a/src/main/java/DataHandling/LoadData.java b/src/main/java/DataHandling/LoadData.java new file mode 100644 index 000000000..e5db8e73b --- /dev/null +++ b/src/main/java/DataHandling/LoadData.java @@ -0,0 +1,89 @@ +package DataHandling; + +import Exceptions.FileReadException; +import Exceptions.LoadFileException; +import Tasks.*; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Scanner; +import java.util.ArrayList; +import java.io.File; + +public class LoadData { + static ArrayList toDoList = new ArrayList<>(); + static int counter = 0; + + public static void loadToDo(String description) throws LoadFileException { + if (description.isEmpty()) { + throw new LoadFileException(); + } + toDoList.add(new ToDo(description)); + } + + public static void loadDeadline(String description) throws LoadFileException { + String[] deadlineDescription = description.split("\\|"); + String task = deadlineDescription[0].trim(); + String by = deadlineDescription[1].trim(); + if (task.isBlank() || by.isBlank()) { + throw new LoadFileException(); + } + toDoList.add(new Deadline(task, by)); + } + + public static void loadEvent(String description) throws LoadFileException { + String[] eventDescription = description.split("\\|"); + String task = eventDescription[0].trim(); + String from = eventDescription[1].trim(); + String by = eventDescription[2].trim(); + if (task.isBlank() || from.isBlank() || by.isBlank()) { + throw new LoadFileException(); + } + toDoList.add(new Event(task, from, by)); + } + + public static ArrayList loadFile() throws FileNotFoundException, FileReadException { + File data = new File("src/data/GermaBotData.txt"); + Scanner fileInput = new Scanner(data); + do { + String task = fileInput.nextLine(); + if (task.trim().isEmpty()) { + throw new FileReadException(); + } + counter++; + char type = task.charAt(0); + String description = task.substring(8); + boolean isCompleted = task.charAt(4) != '0'; + if (type == 'T') { + try { + loadToDo(description); + } catch (LoadFileException e) { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + } else if (type == 'D') { + try { + loadDeadline(description); + } catch (LoadFileException e) { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + } else if (type == 'E') { + try { + loadEvent(description); + } catch (LoadFileException e) { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + } else { + break; + } + int idxToMark = toDoList.size() - 1; + toDoList.get(idxToMark).setDone(isCompleted); + } while (fileInput.hasNext()); + fileInput.close(); + return toDoList; + } +} diff --git a/src/main/java/GermaBot/Logic.java b/src/main/java/GermaBot/Logic.java new file mode 100644 index 000000000..6903688e9 --- /dev/null +++ b/src/main/java/GermaBot/Logic.java @@ -0,0 +1,15 @@ +package GermaBot; + +import Exceptions.*; +import Tasks.*; +import DataHandling.SaveData; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Scanner; +import java.util.ArrayList; +import java.io.File; + +public class Logic { + +} diff --git a/src/main/java/UiOutputs.java b/src/main/java/GermaBot/UiOutputs.java similarity index 68% rename from src/main/java/UiOutputs.java rename to src/main/java/GermaBot/UiOutputs.java index f333f0377..ddb7dd288 100644 --- a/src/main/java/UiOutputs.java +++ b/src/main/java/GermaBot/UiOutputs.java @@ -1,6 +1,13 @@ +package GermaBot; + public class UiOutputs { static final String LINE= "____________________________________________"; static final String WELCOME_MESSAGE = "Hello! GermaBot here! \n" + "Let me load your saved To Do List first..."; static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; + + public static void printWelcomeMessage() { + System.out.println(LINE); + System.out.println(WELCOME_MESSAGE); + } } diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 3211abf51..2df56c564 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -2,7 +2,8 @@ import Exceptions.*; import Tasks.*; -import DataHandling.SaveData; +import DataHandling.*; +import GermaBot.*; import java.io.FileNotFoundException; import java.io.IOException; @@ -227,11 +228,15 @@ public static void printList() { } } + public void run() { + UiOutputs.printWelcomeMessage(); + } + public static void main(String[] args) { System.out.println(LINE); System.out.println(WELCOME_MESSAGE); try { - loadFile(); + ArrayList toDoList = LoadData.loadFile(); System.out.println("Loading complete!"); } catch (FileNotFoundException e) { System.out.println("Uh oh, there is no saved file yet! Please create a GermaBotsData.txt " + From abec695ff7e5045e9f128d135428b68248ba690b Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Wed, 6 Mar 2024 20:53:27 +0800 Subject: [PATCH 19/36] add UI class and replaced body code with UI class --- src/data/GermaBotData.txt | 1 + src/main/java/DataHandling/LoadData.java | 16 ++- src/main/java/GermaBot/Logic.java | 4 + src/main/java/GermaBot/TaskManager.java | 20 ++++ src/main/java/GermaBot/UI.java | 130 +++++++++++++++++++++++ src/main/java/GermaBot/UiOutputs.java | 13 --- src/main/java/MainRuntime/GermaBot.java | 95 ++++++----------- 7 files changed, 193 insertions(+), 86 deletions(-) create mode 100644 src/main/java/GermaBot/TaskManager.java create mode 100644 src/main/java/GermaBot/UI.java delete mode 100644 src/main/java/GermaBot/UiOutputs.java diff --git a/src/data/GermaBotData.txt b/src/data/GermaBotData.txt index 03a1ee652..7c77e8429 100644 --- a/src/data/GermaBotData.txt +++ b/src/data/GermaBotData.txt @@ -3,3 +3,4 @@ D | 0 | Finish IP | tmr E | 1 | Do NOT drop out (IMPOSSIBLE) | now | end of semester D | 0 | test | tomorrow T | 0 | Cry +T | 0 | Sing diff --git a/src/main/java/DataHandling/LoadData.java b/src/main/java/DataHandling/LoadData.java index e5db8e73b..6da05ce88 100644 --- a/src/main/java/DataHandling/LoadData.java +++ b/src/main/java/DataHandling/LoadData.java @@ -2,12 +2,10 @@ import Exceptions.FileReadException; import Exceptions.LoadFileException; +import GermaBot.UI; import Tasks.*; -import java.io.FileWriter; -import java.io.IOException; import java.io.FileNotFoundException; -import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; import java.io.File; @@ -60,22 +58,19 @@ public static ArrayList loadFile() throws FileNotFoundException, FileReadE try { loadToDo(description); } catch (LoadFileException e) { - System.out.println("Oh no, There was an error loading your save file! Please check" + - " to make sure it follows the format..."); + UI.printLoadFileException(); } } else if (type == 'D') { try { loadDeadline(description); } catch (LoadFileException e) { - System.out.println("Oh no, There was an error loading your save file! Please check" + - " to make sure it follows the format..."); + UI.printLoadFileException(); } } else if (type == 'E') { try { loadEvent(description); } catch (LoadFileException e) { - System.out.println("Oh no, There was an error loading your save file! Please check" + - " to make sure it follows the format..."); + UI.printLoadFileException(); } } else { break; @@ -86,4 +81,7 @@ public static ArrayList loadFile() throws FileNotFoundException, FileReadE fileInput.close(); return toDoList; } + public static int getCounter() { + return counter; + } } diff --git a/src/main/java/GermaBot/Logic.java b/src/main/java/GermaBot/Logic.java index 6903688e9..2c4912b85 100644 --- a/src/main/java/GermaBot/Logic.java +++ b/src/main/java/GermaBot/Logic.java @@ -12,4 +12,8 @@ public class Logic { + public static int getIdx(String input) { + return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; + } + } diff --git a/src/main/java/GermaBot/TaskManager.java b/src/main/java/GermaBot/TaskManager.java new file mode 100644 index 000000000..e2f4b6eaa --- /dev/null +++ b/src/main/java/GermaBot/TaskManager.java @@ -0,0 +1,20 @@ +package GermaBot; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +import Exceptions.*; +import Tasks.*; + +public class TaskManager { + private String filePath; + + public TaskManager(String filePath) { + this.filePath = filePath; + } + +} diff --git a/src/main/java/GermaBot/UI.java b/src/main/java/GermaBot/UI.java new file mode 100644 index 000000000..dd73497d4 --- /dev/null +++ b/src/main/java/GermaBot/UI.java @@ -0,0 +1,130 @@ +package GermaBot; + +import Tasks.Task; + +import java.util.ArrayList; +import java.util.Scanner; + +public class UI { + static final String LINE= "____________________________________________"; + static final String WELCOME_MESSAGE = "Hei...! GermaBot here! \n" + + "Let me load your saved To Do List first..."; + static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; + static final String LOAD_COMPLETE = "Loading complete!"; + + private static Scanner scanner; + + public UI() { + scanner = new Scanner(System.in); + } + + public static String getInput() { + return scanner.nextLine(); + } + + public static void printWelcomeMessage() { + System.out.println(LINE); + System.out.println(WELCOME_MESSAGE); + } + + public static void printLoadComplete() { + + System.out.println(LOAD_COMPLETE); + } + + public static void printFileNotFoundException() { + System.out.println("Uh oh, there is no saved file yet! Please create a GermaBotsData.txt " + + "file under './src/data/GermaBotData'!!\n" + + "Because there is no saved file found, I will start with an empty list!"); + } + + public static void printFileReadException() { + System.out.println("Uh oh, there seems to be an issue with the save file...\n" + + "Because there is an error with the save file, I will start with an empty list!"); + } + + public static void printLoadFileException() { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + + public static void printPostLoadingMessage() { + System.out.println("What may I do for you this fine day?"); + System.out.println(LINE); + } + + public static void printByeMessage() { + System.out.println(LINE); + System.out.println(GOODBYE_MESSAGE); + System.out.println(LINE); + } + + public static void printListEmptyMessage() { + System.out.println("Umm... You haven't added any Tasks yet... Let's try adding " + + "some now!"); + } + + public static void printMarkUndone(int idx, ArrayList toDoList) { + System.out.println("Aww, not done? Okay, I'll mark this task as undone: " + + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); + } + + public static void printMarkDone(int idx, ArrayList toDoList) { + System.out.println("Good job! I'll mark this task as done: " + + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); + } + + public static void printUnknownInputException(String input) { + System.out.println("Uhh.. I'm sorry but I'm not quite sure what in the world '" + input + "' means..." + + " Remember to include the Task' Type in front of the input!!"); + } + + public static void printTaskList(ArrayList toDoList) { + System.out.println("Gotcha! Here are your tasks:"); + for (int i = 0; i < toDoList.size(); i++) { + System.out.println(i + 1 + ". " + toDoList.get(i)); + } + } + + public static void printIOException() { + System.out.println("Uh oh, I could not save that to the file..."); + } + + public static void printEmptyTaskException() { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } + + public static void printMissingStartDateException() { + System.out.println("Uh oh, you did not specify a start time! Let's try again!"); + } + + public static void printMissingDeadlineException() { + System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + } + + public static void printTaskNotFoundException(int idx) { + System.out.println("Sorry but... There is no task number " + (idx + 1) + "... " + + "Let's try again!"); + } + + public static void deleteTask(ArrayList toDoList, int idxToDelete) { + System.out.println("Okay! I've removed this task from your To Do List:"); + System.out.println(toDoList.get(idxToDelete)); + toDoList.remove(idxToDelete); + System.out.println("Now you have, let me count... " + toDoList.size() + " items left in your " + + "To Do List!"); + } + + public static void printCreateEventMessage(String toDoTask, String startDate, String endDate) { + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + + " on your calender! (Or ask me!)"); + } + + public static void printCreateDeadlineMessage(String toDoTask, String date) { + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " You have to finish this by " + date + ", so be reminded!"); + } +} + + diff --git a/src/main/java/GermaBot/UiOutputs.java b/src/main/java/GermaBot/UiOutputs.java deleted file mode 100644 index ddb7dd288..000000000 --- a/src/main/java/GermaBot/UiOutputs.java +++ /dev/null @@ -1,13 +0,0 @@ -package GermaBot; - -public class UiOutputs { - static final String LINE= "____________________________________________"; - static final String WELCOME_MESSAGE = "Hello! GermaBot here! \n" - + "Let me load your saved To Do List first..."; - static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; - - public static void printWelcomeMessage() { - System.out.println(LINE); - System.out.println(WELCOME_MESSAGE); - } -} diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 2df56c564..6c2c1af53 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -13,10 +13,6 @@ public class GermaBot { static int counter = 0; - static final String LINE= "____________________________________________"; - static final String WELCOME_MESSAGE = "Hello! GermaBot here! \n" - + "Let me load your saved To Do List first..."; - static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; static ArrayList toDoList = new ArrayList<>(); public static void loadToDo(String description) throws LoadFileException { @@ -63,22 +59,19 @@ public static void loadFile () throws FileNotFoundException, FileReadException { try { loadToDo(description); } catch (LoadFileException e) { - System.out.println("Oh no, There was an error loading your save file! Please check" + - " to make sure it follows the format..."); + UI.printLoadFileException(); } } else if (type == 'D') { try { loadDeadline(description); } catch (LoadFileException e) { - System.out.println("Oh no, There was an error loading your save file! Please check" + - " to make sure it follows the format..."); + UI.printLoadFileException(); } } else if (type == 'E') { try { loadEvent(description); } catch (LoadFileException e) { - System.out.println("Oh no, There was an error loading your save file! Please check" + - " to make sure it follows the format..."); + UI.printLoadFileException(); } } else { break; @@ -121,8 +114,7 @@ public static void createDeadline(String input) throws EmptyTaskException, Missi toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); counter++; SaveData.addDeadlineToFile(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + - " You have to finish this by " + date + ", so be reminded!"); + UI.printCreateDeadlineMessage(toDoTask, date); } public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, @@ -153,9 +145,7 @@ public static void createEvent(String input) throws EmptyTaskException, MissingD toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); counter++; SaveData.addEventToFile(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + - " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + - " on your calender! (Or ask me!)"); + UI.printCreateEventMessage(toDoTask, startDate, endDate); } public static void deleteTask(String input) throws TaskNotFoundException { @@ -163,11 +153,7 @@ public static void deleteTask(String input) throws TaskNotFoundException { if (idxToDelete >= counter) { throw new TaskNotFoundException(); } else { - System.out.println("Okay! I've removed this task from your To Do List:"); - System.out.println(toDoList.get(idxToDelete)); - toDoList.remove(idxToDelete); - System.out.println("Now you have, let me count... " + toDoList.size() + " items left in your " + - "To Do List!"); + UI.deleteTask(toDoList, idxToDelete); counter--; } } @@ -177,77 +163,64 @@ public static void createTask(String input) throws UnknownInputException { try { deleteTask(input); } catch (TaskNotFoundException e) { - System.out.println("Sorry but... There is no task number " + (getIdx(input) + 1) + "... " + - "Let's try again!"); + int idx = getIdx(input); + UI.printTaskNotFoundException(idx); } } else if (input.startsWith("todo")) { try { createTodo(input); } catch (EmptyTaskException e) { - System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + UI.printEmptyTaskException(); } catch (IOException e) { - System.out.println("Uh oh, I could not save that to the file..."); + UI.printIOException(); } } else if (input.startsWith("deadline")) { try { createDeadline(input); } catch (EmptyTaskException e) { - System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + UI.printEmptyTaskException(); } catch (MissingDeadlineException e) { - System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + UI.printMissingDeadlineException(); } catch (IOException e) { - System.out.println("Uh oh, I could not save that to the file..."); + UI.printIOException(); } } else if (input.startsWith("event")) { try { createEvent(input); } catch (EmptyTaskException e) { - System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + UI.printEmptyTaskException(); } catch (MissingStartDateException e) { - System.out.println("Uh oh, you did not specify a start time! Let's try again!"); + UI.printMissingStartDateException(); } catch (MissingDeadlineException e) { - System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + UI.printMissingDeadlineException(); } catch (IOException e) { - System.out.println("Uh oh, I could not save that to the file..."); + UI.printIOException(); } } else { throw new UnknownInputException(); } } - public static void printList() { - System.out.println("Gotcha! Here are your tasks:"); - for (int i = 0; i < counter; i++) { - if (toDoList.get(i) == null) { - break; - } - System.out.println(i + 1 + ". " + toDoList.get(i)); - } - } public void run() { - UiOutputs.printWelcomeMessage(); + UI.printWelcomeMessage(); } public static void main(String[] args) { - System.out.println(LINE); - System.out.println(WELCOME_MESSAGE); + UI.printWelcomeMessage(); try { - ArrayList toDoList = LoadData.loadFile(); - System.out.println("Loading complete!"); + toDoList = LoadData.loadFile(); + counter = LoadData.getCounter(); + UI.printLoadComplete(); } catch (FileNotFoundException e) { - System.out.println("Uh oh, there is no saved file yet! Please create a GermaBotsData.txt " + - "file under './src/data/GermaBotData'!!\n" - + "Because there is no saved file found, I will start with an empty list!"); + UI.printFileNotFoundException(); } catch (FileReadException e) { - System.out.println("Uh oh, there seems to be an issue with the save file...\n" + - "Because there is an error with the save file, I will start with an empty list!"); + UI.printFileReadException(); } - System.out.println("What may I do for you this fine day?"); - System.out.println(LINE); + UI.printPostLoadingMessage(); while (true) { String input; Scanner in = new Scanner(System.in); @@ -257,32 +230,26 @@ public static void main(String[] args) { } if (input.equals("list")) { if (toDoList.isEmpty()) { - System.out.println("Umm... You haven't added any Tasks yet... Let's try adding " + - "some now!"); + UI.printListEmptyMessage(); } else { - printList(); + UI.printTaskList(toDoList); } } else if (input.contains("unmark")) { int idx = getIdx(input); toDoList.get(idx).setDone(false); - System.out.println("Aww, not done? Okay, I'll mark this task as undone: " - + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); + UI.printMarkUndone(idx, toDoList); } else if (input.contains("mark")) { int idx = getIdx(input); toDoList.get(idx).setDone(true); - System.out.println("Good job! I'll mark this task as done: " - + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); + UI.printMarkDone(idx, toDoList); } else { try { createTask(input); } catch (UnknownInputException e){ - System.out.println("Uhh.. I'm sorry but I'm not quite sure what in the world '" + input + "' means..." + - " Remember to include the Task Type in front of the input!!"); + UI.printUnknownInputException(input); } } } - System.out.println(LINE); - System.out.println(GOODBYE_MESSAGE); - System.out.println(LINE); + UI.printByeMessage(); } } From 5b96b6dd1d187b4cb5d5913d46fabc3c0ea40c41 Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Wed, 6 Mar 2024 22:23:07 +0800 Subject: [PATCH 20/36] add logic class and refactor code --- src/data/GermaBotData.txt | 3 +- src/main/java/GermaBot/Logic.java | 141 ++++++++++++++ src/main/java/GermaBot/TaskManager.java | 6 +- src/main/java/GermaBot/UI.java | 17 +- src/main/java/MainRuntime/GermaBot.java | 238 ++---------------------- src/main/java/Tasks/Task.java | 9 + 6 files changed, 176 insertions(+), 238 deletions(-) diff --git a/src/data/GermaBotData.txt b/src/data/GermaBotData.txt index 7c77e8429..9c26a8c3e 100644 --- a/src/data/GermaBotData.txt +++ b/src/data/GermaBotData.txt @@ -2,5 +2,4 @@ T | 1 | Read a book D | 0 | Finish IP | tmr E | 1 | Do NOT drop out (IMPOSSIBLE) | now | end of semester D | 0 | test | tomorrow -T | 0 | Cry -T | 0 | Sing + diff --git a/src/main/java/GermaBot/Logic.java b/src/main/java/GermaBot/Logic.java index 2c4912b85..bd94df63a 100644 --- a/src/main/java/GermaBot/Logic.java +++ b/src/main/java/GermaBot/Logic.java @@ -16,4 +16,145 @@ public static int getIdx(String input) { return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; } + public static void createTodo(ArrayList toDoList, String input) throws EmptyTaskException, IOException { + int idxBetweenTodoAndDescription = 5; + String toDoTask = input.substring(input.indexOf("todo ") + idxBetweenTodoAndDescription); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList.add(new ToDo(toDoTask)); + Task.setNoOfTask(Task.getNoOfTask() + 1); + SaveData.addTodoToFile(new ToDo(toDoTask), 'T'); + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); + } + + public static void createDeadline(ArrayList toDoList, String input) throws EmptyTaskException, MissingDeadlineException, IOException { + String description = input.replaceFirst("deadline ", ""); + if (description.equals("deadline")) { + throw new EmptyTaskException(); + } + int idxOfEndDate = description.indexOf("/"); + if (idxOfEndDate == -1) { + throw new MissingDeadlineException(); + } + int idxToDateFromEndDate = 4; + String date = description.substring(idxOfEndDate + idxToDateFromEndDate); + String toDoTask = description.substring(0, idxOfEndDate - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); + Task.setNoOfTask(Task.getNoOfTask() + 1); + SaveData.addDeadlineToFile(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); + UI.printCreateDeadlineMessage(toDoTask, date); + } + + public static void createEvent(ArrayList toDoList, String input) throws EmptyTaskException, MissingDeadlineException, + MissingStartDateException, IOException { + String description = input.replaceFirst("event ", ""); + int idxOfFrom = description.indexOf("/from"); + if (idxOfFrom == -1) { + throw new EmptyTaskException(); + } + int idxOfBy = description.indexOf("/to"); + if (idxOfBy == -1) { + throw new MissingDeadlineException(); + } + int idxFromFromToStartDate = 6; + String startDate = description.substring(idxOfFrom + idxFromFromToStartDate, idxOfBy - 1); + if (startDate.isBlank()) { + throw new MissingStartDateException(); + } + int idxFromByToEndDate = 4; + String endDate = description.substring(idxOfBy + idxFromByToEndDate); + if (endDate.isBlank()) { + throw new MissingDeadlineException(); + } + String toDoTask = description.substring(0, idxOfFrom - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); + Task.setNoOfTask(Task.getNoOfTask() + 1); + SaveData.addEventToFile(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); + UI.printCreateEventMessage(toDoTask, startDate, endDate); + } + + public static void deleteTask(ArrayList toDoList, String input) throws TaskNotFoundException { + int idxToDelete = getIdx(input); + if (idxToDelete >= Task.getNoOfTask()) { + throw new TaskNotFoundException(); + } else { + UI.deleteTask(toDoList, idxToDelete); + Task.setNoOfTask(Task.getNoOfTask() - 1);; + } + } + + public static void createTask(ArrayList toDoList, String input) { + if (input.startsWith("delete")) { + try { + deleteTask(toDoList, input); + } catch (TaskNotFoundException e) { + int idx = getIdx(input); + UI.printTaskNotFoundException(idx); + } + } + else if (input.startsWith("todo")) { + try { + createTodo(toDoList, input); + } catch (EmptyTaskException e) { + UI.printEmptyTaskException(); + } catch (IOException e) { + UI.printIOException(); + } + } + else if (input.startsWith("deadline")) { + try { + createDeadline(toDoList, input); + } catch (EmptyTaskException e) { + UI.printEmptyTaskException(); + } catch (MissingDeadlineException e) { + UI.printMissingDeadlineException(); + } catch (IOException e) { + UI.printIOException(); + } + } + else if (input.startsWith("event")) { + try { + createEvent(toDoList, input); + } catch (EmptyTaskException e) { + UI.printEmptyTaskException(); + } catch (MissingStartDateException e) { + UI.printMissingStartDateException(); + } catch (MissingDeadlineException e) { + UI.printMissingDeadlineException(); + } catch (IOException e) { + UI.printIOException(); + } + } + } + + public static void readCommand(ArrayList toDoList, String input) throws UnknownInputException { + if (input.startsWith("delete") || input.startsWith("todo") || input.startsWith("deadline") || input.startsWith("event")) { + createTask(toDoList, input); + } + if (input.equals("list")) { + if (toDoList.isEmpty()) { + UI.printListEmptyMessage(); + } else { + UI.printTaskList(toDoList); + } + } else if (input.contains("unmark")) { + int idx = getIdx(input); + toDoList.get(idx).setDone(false); + UI.printMarkUndone(idx, toDoList); + } else if (input.contains("mark")) { + int idx = getIdx(input); + toDoList.get(idx).setDone(true); + UI.printMarkDone(idx, toDoList); + } + else { + throw new UnknownInputException(); + } + } } diff --git a/src/main/java/GermaBot/TaskManager.java b/src/main/java/GermaBot/TaskManager.java index e2f4b6eaa..832a60fce 100644 --- a/src/main/java/GermaBot/TaskManager.java +++ b/src/main/java/GermaBot/TaskManager.java @@ -11,10 +11,10 @@ import Tasks.*; public class TaskManager { - private String filePath; + private final ArrayList tasks = new ArrayList<>(); + + public TaskManager() { - public TaskManager(String filePath) { - this.filePath = filePath; } } diff --git a/src/main/java/GermaBot/UI.java b/src/main/java/GermaBot/UI.java index dd73497d4..daac749e6 100644 --- a/src/main/java/GermaBot/UI.java +++ b/src/main/java/GermaBot/UI.java @@ -12,33 +12,32 @@ public class UI { static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; static final String LOAD_COMPLETE = "Loading complete!"; - private static Scanner scanner; + private static final Scanner scanner = new Scanner(System.in);; public UI() { - scanner = new Scanner(System.in); } - public static String getInput() { + public String getInput() { return scanner.nextLine(); } - public static void printWelcomeMessage() { + public void printWelcomeMessage() { System.out.println(LINE); System.out.println(WELCOME_MESSAGE); } - public static void printLoadComplete() { + public void printLoadComplete() { System.out.println(LOAD_COMPLETE); } - public static void printFileNotFoundException() { + public void printFileNotFoundException() { System.out.println("Uh oh, there is no saved file yet! Please create a GermaBotsData.txt " + "file under './src/data/GermaBotData'!!\n" + "Because there is no saved file found, I will start with an empty list!"); } - public static void printFileReadException() { + public void printFileReadException() { System.out.println("Uh oh, there seems to be an issue with the save file...\n" + "Because there is an error with the save file, I will start with an empty list!"); } @@ -53,7 +52,7 @@ public static void printPostLoadingMessage() { System.out.println(LINE); } - public static void printByeMessage() { + public void printByeMessage() { System.out.println(LINE); System.out.println(GOODBYE_MESSAGE); System.out.println(LINE); @@ -74,7 +73,7 @@ public static void printMarkDone(int idx, ArrayList toDoList) { + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); } - public static void printUnknownInputException(String input) { + public void printUnknownInputException(String input) { System.out.println("Uhh.. I'm sorry but I'm not quite sure what in the world '" + input + "' means..." + " Remember to include the Task' Type in front of the input!!"); } diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 6c2c1af53..f4004d100 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -6,250 +6,40 @@ import GermaBot.*; import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Scanner; import java.util.ArrayList; -import java.io.File; public class GermaBot { - static int counter = 0; static ArrayList toDoList = new ArrayList<>(); - - public static void loadToDo(String description) throws LoadFileException { - if (description.isEmpty()) { - throw new LoadFileException(); - } - toDoList.add(new ToDo(description)); - } - - public static void loadDeadline(String description) throws LoadFileException { - String[] deadlineDescription = description.split("\\|"); - String task = deadlineDescription[0].trim(); - String by = deadlineDescription[1].trim(); - if (task.isBlank() || by.isBlank()) { - throw new LoadFileException(); - } - toDoList.add(new Deadline(task, by)); - } - - public static void loadEvent(String description) throws LoadFileException { - String[] eventDescription = description.split("\\|"); - String task = eventDescription[0].trim(); - String from = eventDescription[1].trim(); - String by = eventDescription[2].trim(); - if (task.isBlank() || from.isBlank() || by.isBlank()) { - throw new LoadFileException(); - } - toDoList.add(new Event(task, from, by)); - } - - public static void loadFile () throws FileNotFoundException, FileReadException { - File data = new File("src/data/GermaBotData.txt"); - Scanner fileInput = new Scanner(data); - do { - String task = fileInput.nextLine(); - if (task.trim().isEmpty()) { - throw new FileReadException(); - } - counter++; - char type = task.charAt(0); - String description = task.substring(8); - boolean isCompleted = task.charAt(4) != '0'; - if (type == 'T') { - try { - loadToDo(description); - } catch (LoadFileException e) { - UI.printLoadFileException(); - } - } else if (type == 'D') { - try { - loadDeadline(description); - } catch (LoadFileException e) { - UI.printLoadFileException(); - } - } else if (type == 'E') { - try { - loadEvent(description); - } catch (LoadFileException e) { - UI.printLoadFileException(); - } - } else { - break; - } - int idxToMark = toDoList.size() - 1; - toDoList.get(idxToMark).setDone(isCompleted); - } while (fileInput.hasNext()); - } - public static int getIdx(String input) { - return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; - } - - public static void createTodo(String input) throws EmptyTaskException, IOException { - int idxBetweenTodoAndDescription = 5; - String toDoTask = input.substring(input.indexOf("todo ") + idxBetweenTodoAndDescription); - if (toDoTask.isBlank()) { - throw new EmptyTaskException(); - } - toDoList.add(new ToDo(toDoTask)); - counter++; - SaveData.addTodoToFile(new ToDo(toDoTask), 'T'); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); - } - - public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException, IOException { - String description = input.replaceFirst("deadline ", ""); - if (description.equals("deadline")) { - throw new EmptyTaskException(); - } - int idxOfEndDate = description.indexOf("/"); - if (idxOfEndDate == -1) { - throw new MissingDeadlineException(); - } - int idxToDateFromEndDate = 4; - String date = description.substring(idxOfEndDate + idxToDateFromEndDate); - String toDoTask = description.substring(0, idxOfEndDate - 1); - if (toDoTask.isBlank()) { - throw new EmptyTaskException(); - } - toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); - counter++; - SaveData.addDeadlineToFile(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); - UI.printCreateDeadlineMessage(toDoTask, date); - } - - public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, - MissingStartDateException, IOException { - String description = input.replaceFirst("event ", ""); - int idxOfFrom = description.indexOf("/from"); - if (idxOfFrom == -1) { - throw new EmptyTaskException(); - } - int idxOfBy = description.indexOf("/to"); - if (idxOfBy == -1) { - throw new MissingDeadlineException(); - } - int idxFromFromToStartDate = 6; - String startDate = description.substring(idxOfFrom + idxFromFromToStartDate, idxOfBy - 1); - if (startDate.isBlank()) { - throw new MissingStartDateException(); - } - int idxFromByToEndDate = 4; - String endDate = description.substring(idxOfBy + idxFromByToEndDate); - if (endDate.isBlank()) { - throw new MissingDeadlineException(); - } - String toDoTask = description.substring(0, idxOfFrom - 1); - if (toDoTask.isBlank()) { - throw new EmptyTaskException(); - } - toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); - counter++; - SaveData.addEventToFile(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); - UI.printCreateEventMessage(toDoTask, startDate, endDate); - } - - public static void deleteTask(String input) throws TaskNotFoundException { - int idxToDelete = getIdx(input); - if (idxToDelete >= counter) { - throw new TaskNotFoundException(); - } else { - UI.deleteTask(toDoList, idxToDelete); - counter--; - } - } - - public static void createTask(String input) throws UnknownInputException { - if (input.startsWith("delete")) { - try { - deleteTask(input); - } catch (TaskNotFoundException e) { - int idx = getIdx(input); - UI.printTaskNotFoundException(idx); - } - } - else if (input.startsWith("todo")) { - try { - createTodo(input); - } catch (EmptyTaskException e) { - UI.printEmptyTaskException(); - } catch (IOException e) { - UI.printIOException(); - } - } - else if (input.startsWith("deadline")) { - try { - createDeadline(input); - } catch (EmptyTaskException e) { - UI.printEmptyTaskException(); - } catch (MissingDeadlineException e) { - UI.printMissingDeadlineException(); - } catch (IOException e) { - UI.printIOException(); - } - } - else if (input.startsWith("event")) { - try { - createEvent(input); - } catch (EmptyTaskException e) { - UI.printEmptyTaskException(); - } catch (MissingStartDateException e) { - UI.printMissingStartDateException(); - } catch (MissingDeadlineException e) { - UI.printMissingDeadlineException(); - } catch (IOException e) { - UI.printIOException(); - } - } else { - throw new UnknownInputException(); - } - } - + private static UI ui; public void run() { - UI.printWelcomeMessage(); + ui.printWelcomeMessage(); } public static void main(String[] args) { - UI.printWelcomeMessage(); + ui = new UI(); + ui.printWelcomeMessage(); try { toDoList = LoadData.loadFile(); - counter = LoadData.getCounter(); - UI.printLoadComplete(); + Task.setNoOfTask(LoadData.getCounter()); + ui.printLoadComplete(); } catch (FileNotFoundException e) { - UI.printFileNotFoundException(); + ui.printFileNotFoundException(); } catch (FileReadException e) { - UI.printFileReadException(); + ui.printFileReadException(); } UI.printPostLoadingMessage(); while (true) { - String input; - Scanner in = new Scanner(System.in); - input = in.nextLine(); + String input = ui.getInput(); if (input.equals("bye")) { break; } - if (input.equals("list")) { - if (toDoList.isEmpty()) { - UI.printListEmptyMessage(); - } else { - UI.printTaskList(toDoList); - } - } else if (input.contains("unmark")) { - int idx = getIdx(input); - toDoList.get(idx).setDone(false); - UI.printMarkUndone(idx, toDoList); - } else if (input.contains("mark")) { - int idx = getIdx(input); - toDoList.get(idx).setDone(true); - UI.printMarkDone(idx, toDoList); - } else { - try { - createTask(input); - } catch (UnknownInputException e){ - UI.printUnknownInputException(input); - } + try { + Logic.readCommand(toDoList, input); + } catch (UnknownInputException e) { + ui.printUnknownInputException(input); } } - UI.printByeMessage(); + ui.printByeMessage(); } } diff --git a/src/main/java/Tasks/Task.java b/src/main/java/Tasks/Task.java index beb238781..49169ea1b 100644 --- a/src/main/java/Tasks/Task.java +++ b/src/main/java/Tasks/Task.java @@ -3,6 +3,7 @@ public class Task { protected String description; protected boolean isDone; + protected static int counter = 0; public Task(String description) { this.description = description; @@ -29,6 +30,14 @@ public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + public static int getNoOfTask() { + return counter; + } + + public static void setNoOfTask(int numberOfTasks) { + counter = numberOfTasks; + } + @Override public String toString() { return "[" + this.getStatusIcon() + "] " + this.getDescription(); From 2bfc69395c45cfba17bf5aed61876e644a9cde81 Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Thu, 7 Mar 2024 02:33:10 +0800 Subject: [PATCH 21/36] add storage class and make main code more OOP --- src/main/java/GermaBot/Logic.java | 6 ++-- src/main/java/GermaBot/Storage.java | 38 +++++++++++++++++++++++++ src/main/java/GermaBot/TaskManager.java | 1 - src/main/java/GermaBot/UI.java | 2 +- src/main/java/MainRuntime/GermaBot.java | 19 ++++++++----- 5 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 src/main/java/GermaBot/Storage.java diff --git a/src/main/java/GermaBot/Logic.java b/src/main/java/GermaBot/Logic.java index bd94df63a..e28b862d0 100644 --- a/src/main/java/GermaBot/Logic.java +++ b/src/main/java/GermaBot/Logic.java @@ -24,7 +24,7 @@ public static void createTodo(ArrayList toDoList, String input) throws Emp } toDoList.add(new ToDo(toDoTask)); Task.setNoOfTask(Task.getNoOfTask() + 1); - SaveData.addTodoToFile(new ToDo(toDoTask), 'T'); + Storage.saveTodo(new ToDo(toDoTask), 'T'); System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); } @@ -45,7 +45,7 @@ public static void createDeadline(ArrayList toDoList, String input) throws } toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); Task.setNoOfTask(Task.getNoOfTask() + 1); - SaveData.addDeadlineToFile(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); + Storage.saveDeadline(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); UI.printCreateDeadlineMessage(toDoTask, date); } @@ -76,7 +76,7 @@ public static void createEvent(ArrayList toDoList, String input) throws Em } toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); Task.setNoOfTask(Task.getNoOfTask() + 1); - SaveData.addEventToFile(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); + Storage.saveEvent(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); UI.printCreateEventMessage(toDoTask, startDate, endDate); } diff --git a/src/main/java/GermaBot/Storage.java b/src/main/java/GermaBot/Storage.java new file mode 100644 index 000000000..8d05eab94 --- /dev/null +++ b/src/main/java/GermaBot/Storage.java @@ -0,0 +1,38 @@ +package GermaBot; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; + +import DataHandling.*; +import Exceptions.FileReadException; +import Tasks.*; + +public class Storage { + private File dataFile; + + public Storage() { + } + + public int getNumOfTask() { + return LoadData.getCounter(); + } + + public ArrayList loadFile() throws FileNotFoundException, FileReadException { + return LoadData.loadFile(); + } + + public static void saveTodo(ToDo toDoTask, char taskType) throws IOException { + SaveData.addTodoToFile(toDoTask, taskType); + } + + public static void saveDeadline(Deadline deadlineTask, char taskType) throws IOException { + SaveData.addDeadlineToFile(deadlineTask, taskType); + } + + public static void saveEvent(Event eventTask, char taskType) throws IOException { + SaveData.addEventToFile(eventTask, taskType); + } + +} diff --git a/src/main/java/GermaBot/TaskManager.java b/src/main/java/GermaBot/TaskManager.java index 832a60fce..d7c34717d 100644 --- a/src/main/java/GermaBot/TaskManager.java +++ b/src/main/java/GermaBot/TaskManager.java @@ -14,7 +14,6 @@ public class TaskManager { private final ArrayList tasks = new ArrayList<>(); public TaskManager() { - } } diff --git a/src/main/java/GermaBot/UI.java b/src/main/java/GermaBot/UI.java index daac749e6..03021c4aa 100644 --- a/src/main/java/GermaBot/UI.java +++ b/src/main/java/GermaBot/UI.java @@ -47,7 +47,7 @@ public static void printLoadFileException() { " to make sure it follows the format..."); } - public static void printPostLoadingMessage() { + public void printPostLoadingMessage() { System.out.println("What may I do for you this fine day?"); System.out.println(LINE); } diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index f4004d100..334fb7e00 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -11,16 +11,14 @@ public class GermaBot { static ArrayList toDoList = new ArrayList<>(); private static UI ui; + private static Storage storage; - public void run() { - ui.printWelcomeMessage(); - } - - public static void main(String[] args) { + public GermaBot() { + storage = new Storage(); ui = new UI(); ui.printWelcomeMessage(); try { - toDoList = LoadData.loadFile(); + toDoList = storage.loadFile(); Task.setNoOfTask(LoadData.getCounter()); ui.printLoadComplete(); } catch (FileNotFoundException e) { @@ -28,7 +26,10 @@ public static void main(String[] args) { } catch (FileReadException e) { ui.printFileReadException(); } - UI.printPostLoadingMessage(); + ui.printPostLoadingMessage(); + } + + public void run() { while (true) { String input = ui.getInput(); if (input.equals("bye")) { @@ -42,4 +43,8 @@ public static void main(String[] args) { } ui.printByeMessage(); } + + public static void main(String[] args) { + new GermaBot().run(); + } } From c447112bfae4026146e914a58ca582a025b9181c Mon Sep 17 00:00:00 2001 From: Fureimi Date: Thu, 7 Mar 2024 15:56:29 +0800 Subject: [PATCH 22/36] add TaskManager class and more OOP --- src/data/GermaBotData.txt | 1 - src/main/java/GermaBot/Logic.java | 30 ++++++++++++------------- src/main/java/GermaBot/TaskManager.java | 20 +++++++++++++++++ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/data/GermaBotData.txt b/src/data/GermaBotData.txt index 9c26a8c3e..2f22d3776 100644 --- a/src/data/GermaBotData.txt +++ b/src/data/GermaBotData.txt @@ -2,4 +2,3 @@ T | 1 | Read a book D | 0 | Finish IP | tmr E | 1 | Do NOT drop out (IMPOSSIBLE) | now | end of semester D | 0 | test | tomorrow - diff --git a/src/main/java/GermaBot/Logic.java b/src/main/java/GermaBot/Logic.java index e28b862d0..a59b5a1b0 100644 --- a/src/main/java/GermaBot/Logic.java +++ b/src/main/java/GermaBot/Logic.java @@ -12,6 +12,8 @@ public class Logic { + public static TaskManager taskManager = new TaskManager(); + public static int getIdx(String input) { return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; } @@ -22,7 +24,7 @@ public static void createTodo(ArrayList toDoList, String input) throws Emp if (toDoTask.isBlank()) { throw new EmptyTaskException(); } - toDoList.add(new ToDo(toDoTask)); + taskManager.addTodo(toDoList, toDoTask); Task.setNoOfTask(Task.getNoOfTask() + 1); Storage.saveTodo(new ToDo(toDoTask), 'T'); System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); @@ -43,7 +45,7 @@ public static void createDeadline(ArrayList toDoList, String input) throws if (toDoTask.isBlank()) { throw new EmptyTaskException(); } - toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); + taskManager.addDeadline(toDoList, description, idxOfEndDate, date); Task.setNoOfTask(Task.getNoOfTask() + 1); Storage.saveDeadline(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); UI.printCreateDeadlineMessage(toDoTask, date); @@ -74,7 +76,7 @@ public static void createEvent(ArrayList toDoList, String input) throws Em if (toDoTask.isBlank()) { throw new EmptyTaskException(); } - toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); + taskManager.addEvent(toDoList, description, idxOfFrom, startDate, endDate); Task.setNoOfTask(Task.getNoOfTask() + 1); Storage.saveEvent(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); UI.printCreateEventMessage(toDoTask, startDate, endDate); @@ -86,7 +88,8 @@ public static void deleteTask(ArrayList toDoList, String input) throws Tas throw new TaskNotFoundException(); } else { UI.deleteTask(toDoList, idxToDelete); - Task.setNoOfTask(Task.getNoOfTask() - 1);; + Task.setNoOfTask(Task.getNoOfTask() - 1); + ; } } @@ -98,8 +101,7 @@ public static void createTask(ArrayList toDoList, String input) { int idx = getIdx(input); UI.printTaskNotFoundException(idx); } - } - else if (input.startsWith("todo")) { + } else if (input.startsWith("todo")) { try { createTodo(toDoList, input); } catch (EmptyTaskException e) { @@ -107,8 +109,7 @@ else if (input.startsWith("todo")) { } catch (IOException e) { UI.printIOException(); } - } - else if (input.startsWith("deadline")) { + } else if (input.startsWith("deadline")) { try { createDeadline(toDoList, input); } catch (EmptyTaskException e) { @@ -118,8 +119,7 @@ else if (input.startsWith("deadline")) { } catch (IOException e) { UI.printIOException(); } - } - else if (input.startsWith("event")) { + } else if (input.startsWith("event")) { try { createEvent(toDoList, input); } catch (EmptyTaskException e) { @@ -137,8 +137,7 @@ else if (input.startsWith("event")) { public static void readCommand(ArrayList toDoList, String input) throws UnknownInputException { if (input.startsWith("delete") || input.startsWith("todo") || input.startsWith("deadline") || input.startsWith("event")) { createTask(toDoList, input); - } - if (input.equals("list")) { + } else if (input.equals("list")) { if (toDoList.isEmpty()) { UI.printListEmptyMessage(); } else { @@ -146,14 +145,13 @@ public static void readCommand(ArrayList toDoList, String input) throws Un } } else if (input.contains("unmark")) { int idx = getIdx(input); - toDoList.get(idx).setDone(false); + taskManager.markTaskUndone(toDoList, idx); UI.printMarkUndone(idx, toDoList); } else if (input.contains("mark")) { int idx = getIdx(input); - toDoList.get(idx).setDone(true); + taskManager.markTaskDone(toDoList, idx); UI.printMarkDone(idx, toDoList); - } - else { + } else { throw new UnknownInputException(); } } diff --git a/src/main/java/GermaBot/TaskManager.java b/src/main/java/GermaBot/TaskManager.java index d7c34717d..2acda4408 100644 --- a/src/main/java/GermaBot/TaskManager.java +++ b/src/main/java/GermaBot/TaskManager.java @@ -16,4 +16,24 @@ public class TaskManager { public TaskManager() { } + public void markTaskDone(ArrayList toDoList, int idx) { + toDoList.get(idx).setDone(true); + } + + public void markTaskUndone(ArrayList toDoList, int idx) { + toDoList.get(idx).setDone(false); + } + + public void addTodo(ArrayList toDoList, String toDoTask) { + toDoList.add(new ToDo(toDoTask)); + } + + public void addDeadline(ArrayList toDoList, String description, int idxOfEndDate, String date) { + toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); + } + + public void addEvent(ArrayList toDoList, String description, int idxOfFrom, String startDate, String endDate) { + toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); + } + } From f55da6dabe3a9cc2ff969afbf5728915a8e67581 Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Thu, 7 Mar 2024 21:17:52 +0800 Subject: [PATCH 23/36] add find function and create new IllegalArgumentException --- src/data/GermaBotData.txt | 1 + src/main/java/GermaBot/Logic.java | 85 +++++++++++++++++++++++-------- src/main/java/GermaBot/UI.java | 16 ++++++ 3 files changed, 82 insertions(+), 20 deletions(-) diff --git a/src/data/GermaBotData.txt b/src/data/GermaBotData.txt index 2f22d3776..b12cd99db 100644 --- a/src/data/GermaBotData.txt +++ b/src/data/GermaBotData.txt @@ -2,3 +2,4 @@ T | 1 | Read a book D | 0 | Finish IP | tmr E | 1 | Do NOT drop out (IMPOSSIBLE) | now | end of semester D | 0 | test | tomorrow +T | 0 | test diff --git a/src/main/java/GermaBot/Logic.java b/src/main/java/GermaBot/Logic.java index a59b5a1b0..1274f71af 100644 --- a/src/main/java/GermaBot/Logic.java +++ b/src/main/java/GermaBot/Logic.java @@ -14,10 +14,15 @@ public class Logic { public static TaskManager taskManager = new TaskManager(); - public static int getIdx(String input) { - return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; + public static int getIdx(String input) throws IllegalArgumentException { + int spaceIndex = input.indexOf(" "); + if (spaceIndex == -1 || spaceIndex == input.length() - 1) { + throw new IllegalArgumentException(); + } + return Integer.parseInt(input.substring(spaceIndex + 1)) - 1; } + public static void createTodo(ArrayList toDoList, String input) throws EmptyTaskException, IOException { int idxBetweenTodoAndDescription = 5; String toDoTask = input.substring(input.indexOf("todo ") + idxBetweenTodoAndDescription); @@ -83,13 +88,16 @@ public static void createEvent(ArrayList toDoList, String input) throws Em } public static void deleteTask(ArrayList toDoList, String input) throws TaskNotFoundException { - int idxToDelete = getIdx(input); - if (idxToDelete >= Task.getNoOfTask()) { - throw new TaskNotFoundException(); - } else { - UI.deleteTask(toDoList, idxToDelete); - Task.setNoOfTask(Task.getNoOfTask() - 1); - ; + try { + int idxToDelete = getIdx(input); + if (idxToDelete >= Task.getNoOfTask()) { + throw new TaskNotFoundException(); + } else { + UI.deleteTask(toDoList, idxToDelete); + Task.setNoOfTask(Task.getNoOfTask() - 1); + } + } catch (IllegalArgumentException e) { + UI.printIllegalArgumentException(); } } @@ -98,8 +106,12 @@ public static void createTask(ArrayList toDoList, String input) { try { deleteTask(toDoList, input); } catch (TaskNotFoundException e) { - int idx = getIdx(input); - UI.printTaskNotFoundException(idx); + try { + int idx = getIdx(input); + UI.printTaskNotFoundException(idx); + } catch (IllegalArgumentException f) { + UI.printIllegalArgumentException(); + } } } else if (input.startsWith("todo")) { try { @@ -134,23 +146,56 @@ public static void createTask(ArrayList toDoList, String input) { } } + public static void findTask(ArrayList toDoList, String input) { + int counter = 1; + int idxBetweenFindAndDescription = 5; + String taskToFind = input.substring(input.indexOf("find ") + idxBetweenFindAndDescription); + if (taskToFind.isBlank()) { + throw new IllegalArgumentException(); + } + UI.printFindingMessage(taskToFind); + for (int i = 0; i < Task.getNoOfTask(); i++) { + if (toDoList.get(i).getDescription().contains(taskToFind)) { + UI.printTask(toDoList, counter, i); + counter++; + } + } + if (counter == 1) { + UI.printTaskNotFoundMessage(taskToFind); + } + } + public static void readCommand(ArrayList toDoList, String input) throws UnknownInputException { if (input.startsWith("delete") || input.startsWith("todo") || input.startsWith("deadline") || input.startsWith("event")) { createTask(toDoList, input); - } else if (input.equals("list")) { + } else if (input.startsWith("list")) { if (toDoList.isEmpty()) { UI.printListEmptyMessage(); } else { UI.printTaskList(toDoList); } - } else if (input.contains("unmark")) { - int idx = getIdx(input); - taskManager.markTaskUndone(toDoList, idx); - UI.printMarkUndone(idx, toDoList); - } else if (input.contains("mark")) { - int idx = getIdx(input); - taskManager.markTaskDone(toDoList, idx); - UI.printMarkDone(idx, toDoList); + } else if (input.startsWith("unmark")) { + try { + int idx = getIdx(input); + taskManager.markTaskUndone(toDoList, idx); + UI.printMarkUndone(idx, toDoList); + } catch (IllegalArgumentException e) { + UI.printIllegalArgumentException(); + } + } else if (input.startsWith("mark")) { + try { + int idx = getIdx(input); + taskManager.markTaskDone(toDoList, idx); + UI.printMarkDone(idx, toDoList); + } catch (IllegalArgumentException e) { + UI.printIllegalArgumentException(); + } + } else if (input.startsWith("find")) { + try { + findTask(toDoList, input); + } catch (IllegalArgumentException e) { + UI.printIllegalArgumentException(); + } } else { throw new UnknownInputException(); } diff --git a/src/main/java/GermaBot/UI.java b/src/main/java/GermaBot/UI.java index 03021c4aa..f988a0a62 100644 --- a/src/main/java/GermaBot/UI.java +++ b/src/main/java/GermaBot/UI.java @@ -124,6 +124,22 @@ public static void printCreateDeadlineMessage(String toDoTask, String date) { System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + " You have to finish this by " + date + ", so be reminded!"); } + + public static void printTask(ArrayList toDoList, int counter, int i) { + System.out.println(counter + ". " + toDoList.get(i)); + } + + public static void printTaskNotFoundMessage(String input) { + System.out.println("Uhm... I'm sorry but, I could not find any tasks containing '" + input + "'... Maybe try again?"); + } + + public static void printIllegalArgumentException() { + System.out.println("Uh oh, you did not provide a task number! Please try again!"); + } + + public static void printFindingMessage(String input) { + System.out.println("Gotcha! Finding tasks containing '" + input + "'..."); + } } From e69231ebd5594d89cd249e798fba4db0aeb913bd Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Fri, 8 Mar 2024 01:46:34 +0800 Subject: [PATCH 24/36] add javadoc comments to classes --- src/main/java/DataHandling/LoadData.java | 34 ++++++++++++ src/main/java/DataHandling/SaveData.java | 24 +++++++++ src/main/java/GermaBot/Logic.java | 66 +++++++++++++++++++++++- src/main/java/GermaBot/Storage.java | 31 +++++++++-- src/main/java/GermaBot/TaskManager.java | 46 ++++++++++++++--- src/main/java/GermaBot/UI.java | 16 ++++++ src/main/java/MainRuntime/GermaBot.java | 9 ++++ src/main/java/Tasks/Deadline.java | 3 ++ src/main/java/Tasks/Event.java | 3 ++ src/main/java/Tasks/Task.java | 3 ++ src/main/java/Tasks/ToDo.java | 3 ++ 11 files changed, 224 insertions(+), 14 deletions(-) diff --git a/src/main/java/DataHandling/LoadData.java b/src/main/java/DataHandling/LoadData.java index 6da05ce88..c6e7c1370 100644 --- a/src/main/java/DataHandling/LoadData.java +++ b/src/main/java/DataHandling/LoadData.java @@ -10,10 +10,19 @@ import java.util.ArrayList; import java.io.File; +/** + * The LoadData class handles loading tasks from a file into the program. + */ public class LoadData { static ArrayList toDoList = new ArrayList<>(); static int counter = 0; + /** + * Loads a ToDo task from a string description and adds it to the task list. + * + * @param description The description of the ToDo task. + * @throws LoadFileException If the description is empty. + */ public static void loadToDo(String description) throws LoadFileException { if (description.isEmpty()) { throw new LoadFileException(); @@ -21,6 +30,12 @@ public static void loadToDo(String description) throws LoadFileException { toDoList.add(new ToDo(description)); } + /** + * Loads a Deadline task from a string description and adds it to the task list. + * + * @param description The description of the Deadline task, including the deadline date. + * @throws LoadFileException If the description or deadline date is empty or invalid. + */ public static void loadDeadline(String description) throws LoadFileException { String[] deadlineDescription = description.split("\\|"); String task = deadlineDescription[0].trim(); @@ -31,6 +46,12 @@ public static void loadDeadline(String description) throws LoadFileException { toDoList.add(new Deadline(task, by)); } + /** + * Loads an Event task from a string description and adds it to the task list. + * + * @param description The description of the Event task, including the start and end dates. + * @throws LoadFileException If the description or dates are empty or invalid. + */ public static void loadEvent(String description) throws LoadFileException { String[] eventDescription = description.split("\\|"); String task = eventDescription[0].trim(); @@ -42,6 +63,13 @@ public static void loadEvent(String description) throws LoadFileException { toDoList.add(new Event(task, from, by)); } + /** + * Loads tasks from a file into the program. + * + * @return An ArrayList of tasks loaded from the file. + * @throws FileNotFoundException If the file is not found. + * @throws FileReadException If there is an error reading the file, such as if the file is in an invalid format. + */ public static ArrayList loadFile() throws FileNotFoundException, FileReadException { File data = new File("src/data/GermaBotData.txt"); Scanner fileInput = new Scanner(data); @@ -81,6 +109,12 @@ public static ArrayList loadFile() throws FileNotFoundException, FileReadE fileInput.close(); return toDoList; } + + /** + * Returns the number of tasks loaded from the file. + * + * @return The number of tasks. + */ public static int getCounter() { return counter; } diff --git a/src/main/java/DataHandling/SaveData.java b/src/main/java/DataHandling/SaveData.java index d22ca1d42..f68daa705 100644 --- a/src/main/java/DataHandling/SaveData.java +++ b/src/main/java/DataHandling/SaveData.java @@ -5,15 +5,32 @@ import java.io.FileWriter; import java.io.IOException; +/** + * The SaveData class handles saving tasks to a file. + */ public class SaveData { private static final String filePath = "src/data/GermaBotData.txt"; + /** + * Saves a ToDo task to the file. + * + * @param toDoTask The ToDo task to be saved. + * @param taskType The type of the task ('T' for ToDo). + * @throws IOException If there is an error writing to the file. + */ public static void addTodoToFile(ToDo toDoTask, char taskType) throws IOException { FileWriter writer = new FileWriter(filePath, true); writer.write("T | " + (toDoTask.isDone() ? "1" : "0") + " | " + toDoTask.getDescription() + System.lineSeparator()); writer.close(); } + /** + * Saves a Deadline task to the file. + * + * @param deadlineTask The Deadline task to be saved. + * @param taskType The type of the task ('D' for Deadline). + * @throws IOException If there is an error writing to the file. + */ public static void addDeadlineToFile(Deadline deadlineTask, char taskType) throws IOException { FileWriter writer = new FileWriter(filePath, true); writer.write("D | " + (deadlineTask.isDone() ? "1" : "0") + " | " + deadlineTask.getDescription() + @@ -21,6 +38,13 @@ public static void addDeadlineToFile(Deadline deadlineTask, char taskType) throw writer.close(); } + /** + * Saves an Event task to the file. + * + * @param eventTask The Event task to be saved. + * @param taskType The type of the task ('E' for Event). + * @throws IOException If there is an error writing to the file. + */ public static void addEventToFile(Event eventTask, char taskType) throws IOException { FileWriter writer = new FileWriter(filePath, true); writer.write("E | " + (eventTask.isDone() ? "1" : "0") + " | " + eventTask.getDescription() + diff --git a/src/main/java/GermaBot/Logic.java b/src/main/java/GermaBot/Logic.java index 1274f71af..e22ee059a 100644 --- a/src/main/java/GermaBot/Logic.java +++ b/src/main/java/GermaBot/Logic.java @@ -10,10 +10,20 @@ import java.util.ArrayList; import java.io.File; + +/** + * Contains the main logic for handling user commands in the GermaBot application. + */ public class Logic { public static TaskManager taskManager = new TaskManager(); + /** + * Returns the index of a task in the arraylist that is specified in the input. + * @param input The user input. + * @return The index parsed from the input. + * @throws IllegalArgumentException If the input format is invalid, such as if there is no index provided. + */ public static int getIdx(String input) throws IllegalArgumentException { int spaceIndex = input.indexOf(" "); if (spaceIndex == -1 || spaceIndex == input.length() - 1) { @@ -22,7 +32,14 @@ public static int getIdx(String input) throws IllegalArgumentException { return Integer.parseInt(input.substring(spaceIndex + 1)) - 1; } - + /** + * Creates a new ToDo task based on user input and adds it to the task list. + * + * @param toDoList The list of tasks in the form of an ArrayList. + * @param input The user input containing the task description. + * @throws EmptyTaskException If the task description is empty. + * @throws IOException If there is an error saving the task to the file. + */ public static void createTodo(ArrayList toDoList, String input) throws EmptyTaskException, IOException { int idxBetweenTodoAndDescription = 5; String toDoTask = input.substring(input.indexOf("todo ") + idxBetweenTodoAndDescription); @@ -32,9 +49,18 @@ public static void createTodo(ArrayList toDoList, String input) throws Emp taskManager.addTodo(toDoList, toDoTask); Task.setNoOfTask(Task.getNoOfTask() + 1); Storage.saveTodo(new ToDo(toDoTask), 'T'); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); + UI.printCreateTodoMessage(toDoTask); } + /** + * Creates a new Deadline task based on user input and adds it to the task list. + * + * @param toDoList The list of tasks in the form of an ArrayList. + * @param input The user input containing the task description and deadline. + * @throws EmptyTaskException If the task description is empty. + * @throws MissingDeadlineException If the deadline is missing. + * @throws IOException If there is an error saving the task to the file. + */ public static void createDeadline(ArrayList toDoList, String input) throws EmptyTaskException, MissingDeadlineException, IOException { String description = input.replaceFirst("deadline ", ""); if (description.equals("deadline")) { @@ -56,6 +82,16 @@ public static void createDeadline(ArrayList toDoList, String input) throws UI.printCreateDeadlineMessage(toDoTask, date); } + /** + * Creates a new Event task based on user input and adds it to the task list. + * + * @param toDoList The list of tasks in the form of an ArrayList. + * @param input The user input containing the task description, start date, and end date. + * @throws EmptyTaskException If the task description is empty. + * @throws MissingDeadlineException If the end date is missing. + * @throws MissingStartDateException If the start date is missing. + * @throws IOException If there is an error saving the task to the file. + */ public static void createEvent(ArrayList toDoList, String input) throws EmptyTaskException, MissingDeadlineException, MissingStartDateException, IOException { String description = input.replaceFirst("event ", ""); @@ -87,6 +123,13 @@ public static void createEvent(ArrayList toDoList, String input) throws Em UI.printCreateEventMessage(toDoTask, startDate, endDate); } + /** + * Deletes a task from the task list based on the index specified in the user input. + * + * @param toDoList The list of tasks in the form of an ArrayList. + * @param input The user input containing the index of the task to be deleted. + * @throws TaskNotFoundException If the specified task index is not found. + */ public static void deleteTask(ArrayList toDoList, String input) throws TaskNotFoundException { try { int idxToDelete = getIdx(input); @@ -101,6 +144,12 @@ public static void deleteTask(ArrayList toDoList, String input) throws Tas } } + /** + * Processes user input to create, delete, or manage tasks. + * + * @param toDoList The list of tasks in the form of an ArrayList. + * @param input The user input command. + */ public static void createTask(ArrayList toDoList, String input) { if (input.startsWith("delete")) { try { @@ -146,6 +195,12 @@ public static void createTask(ArrayList toDoList, String input) { } } + /** + * Searches for tasks in the task list that match the keyword specified in the user input. + * + * @param toDoList The list of tasks in the form of an ArrayList. + * @param input The user input containing the search keyword. + */ public static void findTask(ArrayList toDoList, String input) { int counter = 1; int idxBetweenFindAndDescription = 5; @@ -165,6 +220,13 @@ public static void findTask(ArrayList toDoList, String input) { } } + /** + * Interprets and executes the command specified in the user input. + * + * @param toDoList The list of tasks in the form of an ArrayList. + * @param input The user input command. + * @throws UnknownInputException If the input command is not recognized. + */ public static void readCommand(ArrayList toDoList, String input) throws UnknownInputException { if (input.startsWith("delete") || input.startsWith("todo") || input.startsWith("deadline") || input.startsWith("event")) { createTask(toDoList, input); diff --git a/src/main/java/GermaBot/Storage.java b/src/main/java/GermaBot/Storage.java index 8d05eab94..be0c07df8 100644 --- a/src/main/java/GermaBot/Storage.java +++ b/src/main/java/GermaBot/Storage.java @@ -9,28 +9,51 @@ import Exceptions.FileReadException; import Tasks.*; +/** + * Handles storage operations for the GermaBot application. + */ public class Storage { private File dataFile; public Storage() { } - public int getNumOfTask() { - return LoadData.getCounter(); - } - + /** + * Loads tasks from a file. + * @return The list of tasks loaded from the file. + * @throws FileNotFoundException If the file is not found. + * @throws FileReadException If there is an error reading the file. + */ public ArrayList loadFile() throws FileNotFoundException, FileReadException { return LoadData.loadFile(); } + /** + * Saves a todo task to the file. + * @param toDoTask The todo task to save to the file. + * @param taskType The type of task (T for todo, D for deadline, E for event). + * @throws IOException If an I/O error occurs. + */ public static void saveTodo(ToDo toDoTask, char taskType) throws IOException { SaveData.addTodoToFile(toDoTask, taskType); } + /** + * Saves a deadline task to the file. + * @param deadlineTask The deadline task to save to the file. + * @param taskType The type of task (T for todo, D for deadline, E for event). + * @throws IOException If an I/O error occurs. + */ public static void saveDeadline(Deadline deadlineTask, char taskType) throws IOException { SaveData.addDeadlineToFile(deadlineTask, taskType); } + /** + * Saves an event task to the file. + * @param eventTask The event task to save to the file. + * @param taskType The type of task (T for todo, D for deadline, E for event). + * @throws IOException If an I/O error occurs. + */ public static void saveEvent(Event eventTask, char taskType) throws IOException { SaveData.addEventToFile(eventTask, taskType); } diff --git a/src/main/java/GermaBot/TaskManager.java b/src/main/java/GermaBot/TaskManager.java index 2acda4408..520a1fbe3 100644 --- a/src/main/java/GermaBot/TaskManager.java +++ b/src/main/java/GermaBot/TaskManager.java @@ -1,37 +1,67 @@ package GermaBot; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Scanner; +import Tasks.Deadline; +import Tasks.Event; +import Tasks.Task; +import Tasks.ToDo; -import Exceptions.*; -import Tasks.*; +import java.util.ArrayList; +/** + * Manages tasks in the GermaBot application. + */ public class TaskManager { private final ArrayList tasks = new ArrayList<>(); public TaskManager() { } + /** + * Marks a task as done. + * @param toDoList The list of tasks in the form of an ArrayList. + * @param idx The index of the task to mark as done. + */ public void markTaskDone(ArrayList toDoList, int idx) { toDoList.get(idx).setDone(true); } + /** + * Marks a task as undone. + * @param toDoList The list of tasks in the form of an ArrayList. + * @param idx The index of the task to mark as undone. + */ public void markTaskUndone(ArrayList toDoList, int idx) { toDoList.get(idx).setDone(false); } + /** + * Adds a todo task to the list of tasks. + * @param toDoList The list of tasks in the form of an ArrayList. + * @param toDoTask The description of the todo task. + */ public void addTodo(ArrayList toDoList, String toDoTask) { toDoList.add(new ToDo(toDoTask)); } + /** + * Adds a deadline task to the list of tasks. + * @param toDoList The list of tasks in the form of an ArrayList. + * @param description The description of the deadline task. + * @param idxOfEndDate The index of the end date in the String array of the description. + * @param date The end date of the deadline task. + */ public void addDeadline(ArrayList toDoList, String description, int idxOfEndDate, String date) { toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); } + /** + * Adds an event task to the list of tasks. + * @param toDoList The list of tasks in the form of an ArrayList. + * @param description The description of the event task. + * @param idxOfFrom The index of the from date in the String array of the description. + * @param startDate The start date of the event task. + * @param endDate The end date of the event task. + */ public void addEvent(ArrayList toDoList, String description, int idxOfFrom, String startDate, String endDate) { toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); } diff --git a/src/main/java/GermaBot/UI.java b/src/main/java/GermaBot/UI.java index f988a0a62..6ce025f85 100644 --- a/src/main/java/GermaBot/UI.java +++ b/src/main/java/GermaBot/UI.java @@ -5,6 +5,9 @@ import java.util.ArrayList; import java.util.Scanner; +/** + * Represents a class object for printing to standard output, as well as reading input. + */ public class UI { static final String LINE= "____________________________________________"; static final String WELCOME_MESSAGE = "Hei...! GermaBot here! \n" @@ -78,6 +81,10 @@ public void printUnknownInputException(String input) { " Remember to include the Task' Type in front of the input!!"); } + /** + * Prints the list of tasks currently stored in the toDoList. + * @param toDoList The list of tasks in the form of an ArrayList. + */ public static void printTaskList(ArrayList toDoList) { System.out.println("Gotcha! Here are your tasks:"); for (int i = 0; i < toDoList.size(); i++) { @@ -106,6 +113,11 @@ public static void printTaskNotFoundException(int idx) { "Let's try again!"); } + /** + * Deletes a task to be removed from the task list. + * @param toDoList the list of tasks in the form of an ArrayList. + * @param idxToDelete is the index of the task that is going to be deleted. + */ public static void deleteTask(ArrayList toDoList, int idxToDelete) { System.out.println("Okay! I've removed this task from your To Do List:"); System.out.println(toDoList.get(idxToDelete)); @@ -114,6 +126,10 @@ public static void deleteTask(ArrayList toDoList, int idxToDelete) { "To Do List!"); } + public static void printCreateTodoMessage(String toDoTask) { + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); + } + public static void printCreateEventMessage(String toDoTask, String startDate, String endDate) { System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 334fb7e00..79d2fe666 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -8,11 +8,17 @@ import java.io.FileNotFoundException; import java.util.ArrayList; +/** + * Main class for the GermaBot application. + */ public class GermaBot { static ArrayList toDoList = new ArrayList<>(); private static UI ui; private static Storage storage; + /** + * Constructor for GermaBot class. Initializes the user interface and storage manager, loads tasks from storage, and prints welcome messages. + */ public GermaBot() { storage = new Storage(); ui = new UI(); @@ -29,6 +35,9 @@ public GermaBot() { ui.printPostLoadingMessage(); } + /** + * Main method to run the GermaBot application. Continuously reads user input, processes it and prints the result until the user types "bye". + */ public void run() { while (true) { String input = ui.getInput(); diff --git a/src/main/java/Tasks/Deadline.java b/src/main/java/Tasks/Deadline.java index c0470b7a9..af37edcc0 100644 --- a/src/main/java/Tasks/Deadline.java +++ b/src/main/java/Tasks/Deadline.java @@ -1,5 +1,8 @@ package Tasks; +/** + * Represents a class for deadline tasks. + */ public class Deadline extends Task { protected String by; diff --git a/src/main/java/Tasks/Event.java b/src/main/java/Tasks/Event.java index b55f26b02..d0d4c7748 100644 --- a/src/main/java/Tasks/Event.java +++ b/src/main/java/Tasks/Event.java @@ -2,6 +2,9 @@ import Tasks.Task; +/** + * Represents a class for event tasks. + */ public class Event extends Task { protected String from; diff --git a/src/main/java/Tasks/Task.java b/src/main/java/Tasks/Task.java index 49169ea1b..b9bf82f8d 100644 --- a/src/main/java/Tasks/Task.java +++ b/src/main/java/Tasks/Task.java @@ -1,5 +1,8 @@ package Tasks; +/** + * Represents a Task object that can be used as a parent class for deadline, event and todo type classes. + */ public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/Tasks/ToDo.java b/src/main/java/Tasks/ToDo.java index 8ac8b949c..b4caac87b 100644 --- a/src/main/java/Tasks/ToDo.java +++ b/src/main/java/Tasks/ToDo.java @@ -2,6 +2,9 @@ import Tasks.Task; +/** + * Represents a class for todo tasks. + */ public class ToDo extends Task { public ToDo(String description) { super(description); From 48ebd3d98b2e7d557d9ae03bc879ede974f89159 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Fri, 8 Mar 2024 15:07:29 +0800 Subject: [PATCH 25/36] add user guide --- docs/README.md | 113 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 101 insertions(+), 12 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..15ad692c0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,118 @@ # User Guide +GermaBot is a task list bot. You can add, delete, find and list the stored tasks. GermaBot recognizes 3 different type +of tasks: Todo tasks, deadline tasks and event tasks. +GermaBot is also capable of saving files to your local hard disk, to the `/src/GermaBotData.txt` file. + ## Features -### Feature-ABC +> [!NOTE] +> ## Notes about the command format. +> - Words in `UPPER_CASE` are the parameters to be supplied by the user. +> - Extra parameters that do not take in any parameters (such as `list` and `bye`) will be ignored. +> e.g. if the input is `list 123`, it will be interpreted as `list`. + + +### Adding Todo tasks : `todo` +Adds a todo task to the task list. + +Format: `todo TASK_NAME` + +### Adding Deadline tasks : `deadline` +Adds a deadline task to the task list. + +Format: `deadline TASK_NAME /by DEADLINE` + +Example: +``` +deadline Read a book /by 4th July +``` + +### Adding Event tasks : `event` +Adds an event task to the task list. + +Format: `event TASK_NAME /from STARTDATE /to ENDDATE` + +Example: +``` +event Dad's party /from 6pm /to 12am +``` + +### Listing all tasks : `list` +Shows the list of the current tasks, including the details of the task. + +Example input: +``` +list +``` +Example output: +``` +Gotcha! Here are your tasks: +1. [T][X] Read a book +2. [D][ ] Finish IP (by: 23rd November) +3. [E][X] Dad's birthday party (from: 6pm to: 12am) +``` + +### Deleting tasks : `delete` +Deletes a task from the task list. -Description of the feature. +Format: `delete TASK_NUMBER` -### Feature-XYZ +Example: +``` +delete 2 +``` +Expected output: +``` +Okay! I've removed this task from your To Do List: +[D][X] Finish IP (by: tmr) +Now you have, let me count... 4 items left in your To Do List! +``` -Description of the feature. +### Finding tasks : `find` +Finds a task containing certain keywords. -## Usage +Format: `find KEYWORDS` -### `Keyword` - Describe action +Example: +``` +find book +``` +Expected output: +``` +Gotcha! Finding tasks containing 'book'... +1. [T][X] Read a book +``` -Describe the action and its outcome. +### Marking tasks as done : `mark` +Marks a task as done. -Example of usage: +Format: `mark TASK_NUMBER` -`keyword (optional arguments)` +Example: +``` +mark 2 +``` +Expected output: +``` +Good job! I'll mark this task as done: [X] Finish IP +``` -Expected outcome: +### Marking tasks as undone : `unmark` +Marks a task as undone. -Description of the outcome. +Format: `unmark TASK_NUMBER` +Example ``` -expected output +unmark 1 ``` +Expected output: +``` +Aww, not done? Okay, I'll mark this task as undone: [ ] Read a book +``` + +### Exiting the program: `bye` +Exits the program. + +Format: `bye` From fc09fdc84c144d6560b18a856b1693f042a6d020 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Fri, 8 Mar 2024 15:10:02 +0800 Subject: [PATCH 26/36] do minor changes to readme formatting --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 15ad692c0..2d7e22a5d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,4 @@ -# User Guide +# GermaBot User Guide GermaBot is a task list bot. You can add, delete, find and list the stored tasks. GermaBot recognizes 3 different type of tasks: Todo tasks, deadline tasks and event tasks. From 6bd73375594b780be64ecb7a645e6d67417f7876 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Fri, 8 Mar 2024 15:15:26 +0800 Subject: [PATCH 27/36] do minor changes to README --- docs/README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/README.md b/docs/README.md index 2d7e22a5d..8bae30f17 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,6 +12,7 @@ GermaBot is also capable of saving files to your local hard disk, to the `/src/G > - Extra parameters that do not take in any parameters (such as `list` and `bye`) will be ignored. > e.g. if the input is `list 123`, it will be interpreted as `list`. + ### Adding Todo tasks : `todo` Adds a todo task to the task list. @@ -62,7 +63,7 @@ Example: ``` delete 2 ``` -Expected output: +Example output: ``` Okay! I've removed this task from your To Do List: [D][X] Finish IP (by: tmr) @@ -78,7 +79,7 @@ Example: ``` find book ``` -Expected output: +Example output: ``` Gotcha! Finding tasks containing 'book'... 1. [T][X] Read a book @@ -93,7 +94,7 @@ Example: ``` mark 2 ``` -Expected output: +Example output: ``` Good job! I'll mark this task as done: [X] Finish IP ``` @@ -103,11 +104,11 @@ Marks a task as undone. Format: `unmark TASK_NUMBER` -Example +Example: ``` unmark 1 ``` -Expected output: +Example output: ``` Aww, not done? Okay, I'll mark this task as undone: [ ] Read a book ``` From 09bebe3c7cd18cee5d7dbea1878833dafd6d5ef8 Mon Sep 17 00:00:00 2001 From: Fureimi Date: Fri, 8 Mar 2024 15:44:49 +0800 Subject: [PATCH 28/36] change notes in readme --- docs/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 8bae30f17..901c124cf 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,6 @@ GermaBot is also capable of saving files to your local hard disk, to the `/src/G ## Features -> [!NOTE] > ## Notes about the command format. > - Words in `UPPER_CASE` are the parameters to be supplied by the user. > - Extra parameters that do not take in any parameters (such as `list` and `bye`) will be ignored. From 710731161c57e6a55df4ff8969d49c53f2577cfd Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Fri, 8 Mar 2024 18:45:02 +0800 Subject: [PATCH 29/36] change ui and readme errors --- docs/README.md | 2 +- src/main/java/GermaBot/UI.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 901c124cf..d405a0c0d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ GermaBot is a task list bot. You can add, delete, find and list the stored tasks. GermaBot recognizes 3 different type of tasks: Todo tasks, deadline tasks and event tasks. -GermaBot is also capable of saving files to your local hard disk, to the `/src/GermaBotData.txt` file. +GermaBot is also capable of saving files to your local hard disk, to the `/src/data/GermaBotData.txt` file. ## Features diff --git a/src/main/java/GermaBot/UI.java b/src/main/java/GermaBot/UI.java index 6ce025f85..5e14d1bd9 100644 --- a/src/main/java/GermaBot/UI.java +++ b/src/main/java/GermaBot/UI.java @@ -12,7 +12,7 @@ public class UI { static final String LINE= "____________________________________________"; static final String WELCOME_MESSAGE = "Hei...! GermaBot here! \n" + "Let me load your saved To Do List first..."; - static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; + static final String GOODBYE_MESSAGE = "Thanks you for using me!!!! See you soon~! ^^"; static final String LOAD_COMPLETE = "Loading complete!"; private static final Scanner scanner = new Scanner(System.in);; From 6c21a23699199969d700711ac52d7fc013c96059 Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Fri, 8 Mar 2024 21:44:42 +0800 Subject: [PATCH 30/36] clean up importing --- src/main/java/GermaBot/Logic.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/GermaBot/Logic.java b/src/main/java/GermaBot/Logic.java index e22ee059a..0892f8e69 100644 --- a/src/main/java/GermaBot/Logic.java +++ b/src/main/java/GermaBot/Logic.java @@ -1,14 +1,13 @@ package GermaBot; import Exceptions.*; -import Tasks.*; -import DataHandling.SaveData; +import Tasks.Deadline; +import Tasks.Event; +import Tasks.Task; +import Tasks.ToDo; -import java.io.FileNotFoundException; import java.io.IOException; -import java.util.Scanner; import java.util.ArrayList; -import java.io.File; /** From f462c5008e274708e84b1e9186b8155f22685b39 Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Fri, 8 Mar 2024 21:45:46 +0800 Subject: [PATCH 31/36] add error handling --- src/main/java/EmptyTaskException.java | 2 + src/main/java/GermaBot.java | 148 +++++++++++++++++++ src/main/java/MissingDeadlineException.java | 2 + src/main/java/MissingStartDateException.java | 2 + src/main/java/UnknownInputException.java | 2 + 5 files changed, 156 insertions(+) create mode 100644 src/main/java/EmptyTaskException.java create mode 100644 src/main/java/GermaBot.java create mode 100644 src/main/java/MissingDeadlineException.java create mode 100644 src/main/java/MissingStartDateException.java create mode 100644 src/main/java/UnknownInputException.java diff --git a/src/main/java/EmptyTaskException.java b/src/main/java/EmptyTaskException.java new file mode 100644 index 000000000..daa526f32 --- /dev/null +++ b/src/main/java/EmptyTaskException.java @@ -0,0 +1,2 @@ +public class EmptyTaskException extends Exception{ +} diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java new file mode 100644 index 000000000..2e0c4d601 --- /dev/null +++ b/src/main/java/GermaBot.java @@ -0,0 +1,148 @@ +import java.util.Scanner; + +public class GermaBot { + static int counter = 0; + static final String LINE= "____________________________________________"; + static Task[] toDoList = new Task[100]; + public static int getIdx(String input) { + return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; + } + + public static void createTodo(String input) throws EmptyTaskException { + String toDoTask = input.substring(input.indexOf("todo ") + 5); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList[counter] = new ToDo(toDoTask); + counter++; + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); + } + + public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException { + String description = input.replaceFirst("deadline ", ""); + int idxOfEndDate = description.indexOf("/"); + if (idxOfEndDate == -1) { + throw new MissingDeadlineException(); + } + String date = description.substring( idxOfEndDate + 4); + String toDoTask = description.substring(0, idxOfEndDate - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList[counter] = new Deadline(description.substring(0, idxOfEndDate - 1), date); + counter++; + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " You have to finish this by " + date + ", so be reminded!"); + } + + public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, + MissingStartDateException { + String description = input.replaceFirst("event ", ""); + int idxOfFrom = description.indexOf("/from"); + if (idxOfFrom == -1) { + throw new EmptyTaskException(); + } + int idxOfBy = description.indexOf("/to"); + if (idxOfBy == -1) { + throw new MissingDeadlineException(); + } + String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); + if (startDate.isBlank()) { + throw new MissingStartDateException(); + } + String endDate = description.substring(idxOfBy + 4); + if (endDate.isBlank()) { + throw new MissingDeadlineException(); + } + String toDoTask = description.substring(0, idxOfFrom - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList[counter] = new Event(description.substring(0, idxOfFrom - 1), startDate, endDate); + counter++; + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + + " on your calender! (Or ask me!)"); + } + + public static void createTask(String input) throws UnknownInputException{ + if (input.contains("todo")) { + try { + createTodo(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } + } + else if (input.contains("deadline")) { + try { + createDeadline(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (MissingDeadlineException e) { + System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + } + + } + else if (input.contains("event")) { + try { + createEvent(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (MissingStartDateException e) { + System.out.println("Uh oh, you did not specify a start time! Let's try again!"); + } catch (MissingDeadlineException e) { + System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + } + } else { + throw new UnknownInputException(); + } + } + + public static void main(String[] args) { + String WelcomeMessage = "Hello! GermaBot here! \n" + + "What may I do for you this fine day?"; + System.out.println(LINE); + System.out.println(WelcomeMessage); + System.out.println(LINE); + while (true) { + String input; + Scanner in = new Scanner(System.in); + input = in.nextLine(); + if (input.equals("bye")) { + break; + } + if (input.equals("list")) { + int printCounter = 1; + System.out.println("Gotcha! Here are your tasks:"); + for (int i = 0; i < counter; i++) { + if (toDoList[i] == null) { + break; + } + System.out.println(printCounter + ". " + toDoList[i].toString()); + printCounter++; + } + } else if (input.contains("unmark")) { + int idx = getIdx(input); + toDoList[idx].setDone(false); + System.out.println("Aww, not done? Okay, I'll mark this task as undone: " + + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); + } else if (input.contains("mark")) { + int idx = getIdx(input); + toDoList[idx].setDone(true); + System.out.println("Good job! I'll mark this task as done: " + + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); + } else { + try { + createTask(input); + } catch (UnknownInputException e){ + System.out.println("Uhh.. I'm sorry but I'm not quite sure what '" + input + "' means..." + + " Remember to include the Task Type in front of the input!!"); + } + } + } + String GoodbyeMessage = "Thanks for using me! Hope to see you again soon~!"; + System.out.println(LINE); + System.out.println(GoodbyeMessage); + System.out.println(LINE); + } +} diff --git a/src/main/java/MissingDeadlineException.java b/src/main/java/MissingDeadlineException.java new file mode 100644 index 000000000..fcf99eb8c --- /dev/null +++ b/src/main/java/MissingDeadlineException.java @@ -0,0 +1,2 @@ +public class MissingDeadlineException extends Exception{ +} diff --git a/src/main/java/MissingStartDateException.java b/src/main/java/MissingStartDateException.java new file mode 100644 index 000000000..294f71630 --- /dev/null +++ b/src/main/java/MissingStartDateException.java @@ -0,0 +1,2 @@ +public class MissingStartDateException extends Exception{ +} diff --git a/src/main/java/UnknownInputException.java b/src/main/java/UnknownInputException.java new file mode 100644 index 000000000..ee928be8f --- /dev/null +++ b/src/main/java/UnknownInputException.java @@ -0,0 +1,2 @@ +public class UnknownInputException extends Exception{ +} From 9b642bcb0b9ed7f873f2485e8d3aaa2ecbfab5a1 Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Fri, 8 Mar 2024 21:47:35 +0800 Subject: [PATCH 32/36] import Tasks class --- src/main/java/GermaBot.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java index 2e0c4d601..a8039d4ed 100644 --- a/src/main/java/GermaBot.java +++ b/src/main/java/GermaBot.java @@ -1,4 +1,5 @@ import java.util.Scanner; +import Tasks.*; public class GermaBot { static int counter = 0; From 0e69b9e16f5855a20ee5d2e7b4bdc3ef1a5375e5 Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Fri, 8 Mar 2024 22:02:13 +0800 Subject: [PATCH 33/36] delete extra classes --- src/main/java/EmptyTaskException.java | 2 - src/main/java/GermaBot.java | 149 ------------------- src/main/java/MissingDeadlineException.java | 2 - src/main/java/MissingStartDateException.java | 2 - src/main/java/UnknownInputException.java | 2 - 5 files changed, 157 deletions(-) delete mode 100644 src/main/java/EmptyTaskException.java delete mode 100644 src/main/java/GermaBot.java delete mode 100644 src/main/java/MissingDeadlineException.java delete mode 100644 src/main/java/MissingStartDateException.java delete mode 100644 src/main/java/UnknownInputException.java diff --git a/src/main/java/EmptyTaskException.java b/src/main/java/EmptyTaskException.java deleted file mode 100644 index daa526f32..000000000 --- a/src/main/java/EmptyTaskException.java +++ /dev/null @@ -1,2 +0,0 @@ -public class EmptyTaskException extends Exception{ -} diff --git a/src/main/java/GermaBot.java b/src/main/java/GermaBot.java deleted file mode 100644 index a8039d4ed..000000000 --- a/src/main/java/GermaBot.java +++ /dev/null @@ -1,149 +0,0 @@ -import java.util.Scanner; -import Tasks.*; - -public class GermaBot { - static int counter = 0; - static final String LINE= "____________________________________________"; - static Task[] toDoList = new Task[100]; - public static int getIdx(String input) { - return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; - } - - public static void createTodo(String input) throws EmptyTaskException { - String toDoTask = input.substring(input.indexOf("todo ") + 5); - if (toDoTask.isBlank()) { - throw new EmptyTaskException(); - } - toDoList[counter] = new ToDo(toDoTask); - counter++; - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); - } - - public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException { - String description = input.replaceFirst("deadline ", ""); - int idxOfEndDate = description.indexOf("/"); - if (idxOfEndDate == -1) { - throw new MissingDeadlineException(); - } - String date = description.substring( idxOfEndDate + 4); - String toDoTask = description.substring(0, idxOfEndDate - 1); - if (toDoTask.isBlank()) { - throw new EmptyTaskException(); - } - toDoList[counter] = new Deadline(description.substring(0, idxOfEndDate - 1), date); - counter++; - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + - " You have to finish this by " + date + ", so be reminded!"); - } - - public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, - MissingStartDateException { - String description = input.replaceFirst("event ", ""); - int idxOfFrom = description.indexOf("/from"); - if (idxOfFrom == -1) { - throw new EmptyTaskException(); - } - int idxOfBy = description.indexOf("/to"); - if (idxOfBy == -1) { - throw new MissingDeadlineException(); - } - String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); - if (startDate.isBlank()) { - throw new MissingStartDateException(); - } - String endDate = description.substring(idxOfBy + 4); - if (endDate.isBlank()) { - throw new MissingDeadlineException(); - } - String toDoTask = description.substring(0, idxOfFrom - 1); - if (toDoTask.isBlank()) { - throw new EmptyTaskException(); - } - toDoList[counter] = new Event(description.substring(0, idxOfFrom - 1), startDate, endDate); - counter++; - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + - " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + - " on your calender! (Or ask me!)"); - } - - public static void createTask(String input) throws UnknownInputException{ - if (input.contains("todo")) { - try { - createTodo(input); - } catch (EmptyTaskException e) { - System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); - } - } - else if (input.contains("deadline")) { - try { - createDeadline(input); - } catch (EmptyTaskException e) { - System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); - } catch (MissingDeadlineException e) { - System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); - } - - } - else if (input.contains("event")) { - try { - createEvent(input); - } catch (EmptyTaskException e) { - System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); - } catch (MissingStartDateException e) { - System.out.println("Uh oh, you did not specify a start time! Let's try again!"); - } catch (MissingDeadlineException e) { - System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); - } - } else { - throw new UnknownInputException(); - } - } - - public static void main(String[] args) { - String WelcomeMessage = "Hello! GermaBot here! \n" - + "What may I do for you this fine day?"; - System.out.println(LINE); - System.out.println(WelcomeMessage); - System.out.println(LINE); - while (true) { - String input; - Scanner in = new Scanner(System.in); - input = in.nextLine(); - if (input.equals("bye")) { - break; - } - if (input.equals("list")) { - int printCounter = 1; - System.out.println("Gotcha! Here are your tasks:"); - for (int i = 0; i < counter; i++) { - if (toDoList[i] == null) { - break; - } - System.out.println(printCounter + ". " + toDoList[i].toString()); - printCounter++; - } - } else if (input.contains("unmark")) { - int idx = getIdx(input); - toDoList[idx].setDone(false); - System.out.println("Aww, not done? Okay, I'll mark this task as undone: " - + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); - } else if (input.contains("mark")) { - int idx = getIdx(input); - toDoList[idx].setDone(true); - System.out.println("Good job! I'll mark this task as done: " - + "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); - } else { - try { - createTask(input); - } catch (UnknownInputException e){ - System.out.println("Uhh.. I'm sorry but I'm not quite sure what '" + input + "' means..." + - " Remember to include the Task Type in front of the input!!"); - } - } - } - String GoodbyeMessage = "Thanks for using me! Hope to see you again soon~!"; - System.out.println(LINE); - System.out.println(GoodbyeMessage); - System.out.println(LINE); - } -} diff --git a/src/main/java/MissingDeadlineException.java b/src/main/java/MissingDeadlineException.java deleted file mode 100644 index fcf99eb8c..000000000 --- a/src/main/java/MissingDeadlineException.java +++ /dev/null @@ -1,2 +0,0 @@ -public class MissingDeadlineException extends Exception{ -} diff --git a/src/main/java/MissingStartDateException.java b/src/main/java/MissingStartDateException.java deleted file mode 100644 index 294f71630..000000000 --- a/src/main/java/MissingStartDateException.java +++ /dev/null @@ -1,2 +0,0 @@ -public class MissingStartDateException extends Exception{ -} diff --git a/src/main/java/UnknownInputException.java b/src/main/java/UnknownInputException.java deleted file mode 100644 index ee928be8f..000000000 --- a/src/main/java/UnknownInputException.java +++ /dev/null @@ -1,2 +0,0 @@ -public class UnknownInputException extends Exception{ -} From d3581b78afb9be8939420e2e228e5e811f48e06b Mon Sep 17 00:00:00 2001 From: Fureimi Date: Sun, 25 Feb 2024 17:29:45 +0800 Subject: [PATCH 34/36] add delete --- src/main/java/MainRuntime/GermaBot.java | 206 +++++++++++++++++++----- 1 file changed, 166 insertions(+), 40 deletions(-) diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 79d2fe666..7d729da75 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -1,59 +1,185 @@ package MainRuntime; import Exceptions.*; -import Tasks.*; -import DataHandling.*; -import GermaBot.*; +import Tasks.Deadline; +import Tasks.Event; +import Tasks.Task; +import Tasks.ToDo; -import java.io.FileNotFoundException; +import java.util.Scanner; import java.util.ArrayList; -/** - * Main class for the GermaBot application. - */ public class GermaBot { + static int counter = 0; + static final String LINE= "____________________________________________"; static ArrayList toDoList = new ArrayList<>(); - private static UI ui; - private static Storage storage; - - /** - * Constructor for GermaBot class. Initializes the user interface and storage manager, loads tasks from storage, and prints welcome messages. - */ - public GermaBot() { - storage = new Storage(); - ui = new UI(); - ui.printWelcomeMessage(); - try { - toDoList = storage.loadFile(); - Task.setNoOfTask(LoadData.getCounter()); - ui.printLoadComplete(); - } catch (FileNotFoundException e) { - ui.printFileNotFoundException(); - } catch (FileReadException e) { - ui.printFileReadException(); - } - ui.printPostLoadingMessage(); + public static int getIdx(String input) { + return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; } - /** - * Main method to run the GermaBot application. Continuously reads user input, processes it and prints the result until the user types "bye". - */ - public void run() { - while (true) { - String input = ui.getInput(); - if (input.equals("bye")) { - break; + public static void createTodo(String input) throws EmptyTaskException { + String toDoTask = input.substring(input.indexOf("todo ") + 5); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList.add(new ToDo(toDoTask)); + counter++; + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); + } + + public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException { + String description = input.replaceFirst("deadline ", ""); + if (description.equals("deadline")) { + throw new EmptyTaskException(); + } + int idxOfEndDate = description.indexOf("/"); + if (idxOfEndDate == -1) { + throw new MissingDeadlineException(); + } + String date = description.substring( idxOfEndDate + 4); + String toDoTask = description.substring(0, idxOfEndDate - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); + counter++; + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " You have to finish this by " + date + ", so be reminded!"); + } + + public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, + MissingStartDateException { + String description = input.replaceFirst("event ", ""); + int idxOfFrom = description.indexOf("/from"); + if (idxOfFrom == -1) { + throw new EmptyTaskException(); + } + int idxOfBy = description.indexOf("/to"); + if (idxOfBy == -1) { + throw new MissingDeadlineException(); + } + String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); + if (startDate.isBlank()) { + throw new MissingStartDateException(); + } + String endDate = description.substring(idxOfBy + 4); + if (endDate.isBlank()) { + throw new MissingDeadlineException(); + } + String toDoTask = description.substring(0, idxOfFrom - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); + counter++; + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + + " on your calender! (Or ask me!)"); + } + + public static void deleteTask(String input) throws TaskNotFoundException { + int idxToDelete = getIdx(input); + if (idxToDelete >= counter) { + throw new TaskNotFoundException(); + } else { + System.out.println("Okay! I've removed this task from your To Do List:"); + System.out.println(toDoList.get(idxToDelete)); + toDoList.remove(idxToDelete); + System.out.println("Now you have, let me count... " + toDoList.size() + " items left in your " + + "To Do List!"); + counter--; + } + } + + public static void createTask(String input) throws UnknownInputException { + if (input.startsWith("delete")) { + try { + deleteTask(input); + } catch (TaskNotFoundException e) { + System.out.println("Sorry but... There is no task number " + (getIdx(input) + 1) + "... " + + "Let's try again!"); + } + } + else if (input.startsWith("todo")) { + try { + createTodo(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } + } + else if (input.startsWith("deadline")) { + try { + createDeadline(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (MissingDeadlineException e) { + System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); } + + } + else if (input.startsWith("event")) { try { - Logic.readCommand(toDoList, input); - } catch (UnknownInputException e) { - ui.printUnknownInputException(input); + createEvent(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (MissingStartDateException e) { + System.out.println("Uh oh, you did not specify a start time! Let's try again!"); + } catch (MissingDeadlineException e) { + System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); } + } else { + throw new UnknownInputException(); } - ui.printByeMessage(); } public static void main(String[] args) { - new GermaBot().run(); + String WelcomeMessage = "Hello! GermaBot here! \n" + + "What may I do for you this fine day?"; + System.out.println(LINE); + System.out.println(WelcomeMessage); + System.out.println(LINE); + while (true) { + String input; + Scanner in = new Scanner(System.in); + input = in.nextLine(); + if (input.equals("bye")) { + break; + } + if (input.equals("list")) { + if (toDoList.isEmpty()) { + System.out.println("Umm... You haven't added any Tasks yet... Let's try adding " + + "some now!"); + } else { + System.out.println("Gotcha! Here are your tasks:"); + for (int i = 0; i < counter; i++) { + if (toDoList.get(i) == null) { + break; + } + System.out.println(i + 1 + ". " + toDoList.get(i)); + } + } + } else if (input.contains("unmark")) { + int idx = getIdx(input); + toDoList.get(idx).setDone(false); + System.out.println("Aww, not done? Okay, I'll mark this task as undone: " + + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); + } else if (input.contains("mark")) { + int idx = getIdx(input); + toDoList.get(idx).setDone(true); + System.out.println("Good job! I'll mark this task as done: " + + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); + } else { + try { + createTask(input); + } catch (UnknownInputException e){ + System.out.println("Uhh.. I'm sorry but I'm not quite sure what in the world '" + input + "' means..." + + " Remember to include the Task Type in front of the input!!"); + } + } + } + String GoodbyeMessage = "Thanks for using me! Hope to see you again soon~!"; + System.out.println(LINE); + System.out.println(GoodbyeMessage); + System.out.println(LINE); } } From b0ff90acb06ac2d96a34197245d3008a2198e77a Mon Sep 17 00:00:00 2001 From: Fureimi Date: Mon, 26 Feb 2024 00:02:59 +0800 Subject: [PATCH 35/36] add save and loading --- src/data/GermaBotData.txt | 3 + src/main/java/DataHandling/SaveData.java | 24 -- src/main/java/MainRuntime/GermaBot.java | 289 ++++++++++++++++++++--- 3 files changed, 256 insertions(+), 60 deletions(-) diff --git a/src/data/GermaBotData.txt b/src/data/GermaBotData.txt index b12cd99db..ca588acb3 100644 --- a/src/data/GermaBotData.txt +++ b/src/data/GermaBotData.txt @@ -1,5 +1,8 @@ T | 1 | Read a book D | 0 | Finish IP | tmr E | 1 | Do NOT drop out (IMPOSSIBLE) | now | end of semester +<<<<<<< HEAD D | 0 | test | tomorrow T | 0 | test +======= +>>>>>>> 4ceb8bc (added file reading and writing along with some touch ups) diff --git a/src/main/java/DataHandling/SaveData.java b/src/main/java/DataHandling/SaveData.java index f68daa705..d22ca1d42 100644 --- a/src/main/java/DataHandling/SaveData.java +++ b/src/main/java/DataHandling/SaveData.java @@ -5,32 +5,15 @@ import java.io.FileWriter; import java.io.IOException; -/** - * The SaveData class handles saving tasks to a file. - */ public class SaveData { private static final String filePath = "src/data/GermaBotData.txt"; - /** - * Saves a ToDo task to the file. - * - * @param toDoTask The ToDo task to be saved. - * @param taskType The type of the task ('T' for ToDo). - * @throws IOException If there is an error writing to the file. - */ public static void addTodoToFile(ToDo toDoTask, char taskType) throws IOException { FileWriter writer = new FileWriter(filePath, true); writer.write("T | " + (toDoTask.isDone() ? "1" : "0") + " | " + toDoTask.getDescription() + System.lineSeparator()); writer.close(); } - /** - * Saves a Deadline task to the file. - * - * @param deadlineTask The Deadline task to be saved. - * @param taskType The type of the task ('D' for Deadline). - * @throws IOException If there is an error writing to the file. - */ public static void addDeadlineToFile(Deadline deadlineTask, char taskType) throws IOException { FileWriter writer = new FileWriter(filePath, true); writer.write("D | " + (deadlineTask.isDone() ? "1" : "0") + " | " + deadlineTask.getDescription() + @@ -38,13 +21,6 @@ public static void addDeadlineToFile(Deadline deadlineTask, char taskType) throw writer.close(); } - /** - * Saves an Event task to the file. - * - * @param eventTask The Event task to be saved. - * @param taskType The type of the task ('E' for Event). - * @throws IOException If there is an error writing to the file. - */ public static void addEventToFile(Event eventTask, char taskType) throws IOException { FileWriter writer = new FileWriter(filePath, true); writer.write("E | " + (eventTask.isDone() ? "1" : "0") + " | " + eventTask.getDescription() + diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index 79d2fe666..c09075277 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -2,58 +2,275 @@ import Exceptions.*; import Tasks.*; -import DataHandling.*; -import GermaBot.*; +import javax.lang.model.type.NullType; import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Scanner; import java.util.ArrayList; +import java.io.File; -/** - * Main class for the GermaBot application. - */ public class GermaBot { + static int counter = 0; + static final String LINE= "____________________________________________"; + static final String WELCOME_MESSAGE = "Hello! GermaBot here! \n" + + "Let me load your saved To Do List first..."; + static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; static ArrayList toDoList = new ArrayList<>(); - private static UI ui; - private static Storage storage; - - /** - * Constructor for GermaBot class. Initializes the user interface and storage manager, loads tasks from storage, and prints welcome messages. - */ - public GermaBot() { - storage = new Storage(); - ui = new UI(); - ui.printWelcomeMessage(); - try { - toDoList = storage.loadFile(); - Task.setNoOfTask(LoadData.getCounter()); - ui.printLoadComplete(); - } catch (FileNotFoundException e) { - ui.printFileNotFoundException(); - } catch (FileReadException e) { - ui.printFileReadException(); + + public static void loadToDo(String description) throws LoadFileException { + if (description.isEmpty()) { + throw new LoadFileException(); } - ui.printPostLoadingMessage(); + toDoList.add(new ToDo(description)); } - /** - * Main method to run the GermaBot application. Continuously reads user input, processes it and prints the result until the user types "bye". - */ - public void run() { - while (true) { - String input = ui.getInput(); - if (input.equals("bye")) { + public static void loadDeadline(String description) throws LoadFileException { + String[] deadlineDescription = description.split("\\|"); + String task = deadlineDescription[0].trim(); + String by = deadlineDescription[1].trim(); + if (task.isBlank() || by.isBlank()) { + throw new LoadFileException(); + } + toDoList.add(new Deadline(task, by)); + } + + public static void loadEvent(String description) throws LoadFileException { + String[] eventDescription = description.split("\\|"); + String task = eventDescription[0].trim(); + String from = eventDescription[1].trim(); + String by = eventDescription[2].trim(); + if (task.isBlank() || from.isBlank() || by.isBlank()) { + throw new LoadFileException(); + } + toDoList.add(new Event(task, from, by)); + } + + public static void loadFile () throws FileNotFoundException, FileReadException { + File data = new File("src/data/GermaBotData.txt"); + Scanner fileInput = new Scanner(data); + do { + String task = fileInput.nextLine(); + if (task.trim().isEmpty()) { + throw new FileReadException(); + } + counter++; + char type = task.charAt(0); + String description = task.substring(8); + boolean isCompleted = task.charAt(4) != '0'; + if (type == 'T') { + try { + loadToDo(description); + } catch (LoadFileException e) { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + } else if (type == 'D') { + try { + loadDeadline(description); + } catch (LoadFileException e) { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + } else if (type == 'E') { + try { + loadEvent(description); + } catch (LoadFileException e) { + System.out.println("Oh no, There was an error loading your save file! Please check" + + " to make sure it follows the format..."); + } + } else { break; } + int idxToMark = toDoList.size() - 1; + toDoList.get(idxToMark).setDone(isCompleted); + } while (fileInput.hasNext()); + } + public static int getIdx(String input) { + return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; + } + + public static void createTodo(String input) throws EmptyTaskException, IOException { + String toDoTask = input.substring(input.indexOf("todo ") + 5); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList.add(new ToDo(toDoTask)); + counter++; + DataHandling.SaveData.addTodoToFile(new ToDo(toDoTask), 'T'); + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); + } + + public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException, IOException { + String description = input.replaceFirst("deadline ", ""); + if (description.equals("deadline")) { + throw new EmptyTaskException(); + } + int idxOfEndDate = description.indexOf("/"); + if (idxOfEndDate == -1) { + throw new MissingDeadlineException(); + } + String date = description.substring( idxOfEndDate + 4); + String toDoTask = description.substring(0, idxOfEndDate - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); + counter++; + DataHandling.SaveData.addDeadlineToFile(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " You have to finish this by " + date + ", so be reminded!"); + } + + public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, + MissingStartDateException, IOException { + String description = input.replaceFirst("event ", ""); + int idxOfFrom = description.indexOf("/from"); + if (idxOfFrom == -1) { + throw new EmptyTaskException(); + } + int idxOfBy = description.indexOf("/to"); + if (idxOfBy == -1) { + throw new MissingDeadlineException(); + } + String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); + if (startDate.isBlank()) { + throw new MissingStartDateException(); + } + String endDate = description.substring(idxOfBy + 4); + if (endDate.isBlank()) { + throw new MissingDeadlineException(); + } + String toDoTask = description.substring(0, idxOfFrom - 1); + if (toDoTask.isBlank()) { + throw new EmptyTaskException(); + } + toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); + counter++; + DataHandling.SaveData.addEventToFile(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); + System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + + " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + + " on your calender! (Or ask me!)"); + } + + public static void deleteTask(String input) throws TaskNotFoundException { + int idxToDelete = getIdx(input); + if (idxToDelete >= counter) { + throw new TaskNotFoundException(); + } else { + System.out.println("Okay! I've removed this task from your To Do List:"); + System.out.println(toDoList.get(idxToDelete)); + toDoList.remove(idxToDelete); + System.out.println("Now you have, let me count... " + toDoList.size() + " items left in your " + + "To Do List!"); + counter--; + } + } + + public static void createTask(String input) throws UnknownInputException { + if (input.startsWith("delete")) { + try { + deleteTask(input); + } catch (TaskNotFoundException e) { + System.out.println("Sorry but... There is no task number " + (getIdx(input) + 1) + "... " + + "Let's try again!"); + } + } + else if (input.startsWith("todo")) { try { - Logic.readCommand(toDoList, input); - } catch (UnknownInputException e) { - ui.printUnknownInputException(input); + createTodo(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (IOException e) { + System.out.println("Uh oh, I could not save that to the file..."); } } - ui.printByeMessage(); + else if (input.startsWith("deadline")) { + try { + createDeadline(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (MissingDeadlineException e) { + System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + } catch (IOException e) { + System.out.println("Uh oh, I could not save that to the file..."); + } + + } + else if (input.startsWith("event")) { + try { + createEvent(input); + } catch (EmptyTaskException e) { + System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); + } catch (MissingStartDateException e) { + System.out.println("Uh oh, you did not specify a start time! Let's try again!"); + } catch (MissingDeadlineException e) { + System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); + } catch (IOException e) { + System.out.println("Uh oh, I could not save that to the file..."); + } + } else { + throw new UnknownInputException(); + } } public static void main(String[] args) { - new GermaBot().run(); + System.out.println(LINE); + System.out.println(WELCOME_MESSAGE); + try { + loadFile(); + System.out.println("Loading complete!"); + } catch (FileNotFoundException e) { + System.out.println("Uh oh, there is no saved file yet! Please create a GermaBotsData.txt " + + "file under './src/data/GermaBotData'!!\n" + + "Because there is no saved file found, I will start with an empty list!"); + } catch (FileReadException e) { + System.out.println("Uh oh, there seems to be an issue with the save file...\n" + + "Because there is an error with the save file, I will start with an empty list!"); + } + System.out.println("What may I do for you this fine day?"); + System.out.println(LINE); + while (true) { + String input; + Scanner in = new Scanner(System.in); + input = in.nextLine(); + if (input.equals("bye")) { + break; + } + if (input.equals("list")) { + if (toDoList.isEmpty()) { + System.out.println("Umm... You haven't added any Tasks yet... Let's try adding " + + "some now!"); + } else { + System.out.println("Gotcha! Here are your tasks:"); + for (int i = 0; i < counter; i++) { + if (toDoList.get(i) == null) { + break; + } + System.out.println(i + 1 + ". " + toDoList.get(i)); + } + } + } else if (input.contains("unmark")) { + int idx = getIdx(input); + toDoList.get(idx).setDone(false); + System.out.println("Aww, not done? Okay, I'll mark this task as undone: " + + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); + } else if (input.contains("mark")) { + int idx = getIdx(input); + toDoList.get(idx).setDone(true); + System.out.println("Good job! I'll mark this task as done: " + + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); + } else { + try { + createTask(input); + } catch (UnknownInputException e){ + System.out.println("Uhh.. I'm sorry but I'm not quite sure what in the world '" + input + "' means..." + + " Remember to include the Task Type in front of the input!!"); + } + } + } + System.out.println(LINE); + System.out.println(GOODBYE_MESSAGE); + System.out.println(LINE); } } From a177300a61bfd7b7fbb4b9c51772b19a32b49c20 Mon Sep 17 00:00:00 2001 From: guanlinmin Date: Fri, 8 Mar 2024 22:30:49 +0800 Subject: [PATCH 36/36] final version of germabot (should be) --- src/data/GermaBotData.txt | 5 - src/main/java/DataHandling/SaveData.java | 24 ++ src/main/java/MainRuntime/GermaBot.java | 289 +++-------------------- 3 files changed, 60 insertions(+), 258 deletions(-) diff --git a/src/data/GermaBotData.txt b/src/data/GermaBotData.txt index ca588acb3..0739da9ee 100644 --- a/src/data/GermaBotData.txt +++ b/src/data/GermaBotData.txt @@ -1,8 +1,3 @@ T | 1 | Read a book D | 0 | Finish IP | tmr E | 1 | Do NOT drop out (IMPOSSIBLE) | now | end of semester -<<<<<<< HEAD -D | 0 | test | tomorrow -T | 0 | test -======= ->>>>>>> 4ceb8bc (added file reading and writing along with some touch ups) diff --git a/src/main/java/DataHandling/SaveData.java b/src/main/java/DataHandling/SaveData.java index d22ca1d42..f68daa705 100644 --- a/src/main/java/DataHandling/SaveData.java +++ b/src/main/java/DataHandling/SaveData.java @@ -5,15 +5,32 @@ import java.io.FileWriter; import java.io.IOException; +/** + * The SaveData class handles saving tasks to a file. + */ public class SaveData { private static final String filePath = "src/data/GermaBotData.txt"; + /** + * Saves a ToDo task to the file. + * + * @param toDoTask The ToDo task to be saved. + * @param taskType The type of the task ('T' for ToDo). + * @throws IOException If there is an error writing to the file. + */ public static void addTodoToFile(ToDo toDoTask, char taskType) throws IOException { FileWriter writer = new FileWriter(filePath, true); writer.write("T | " + (toDoTask.isDone() ? "1" : "0") + " | " + toDoTask.getDescription() + System.lineSeparator()); writer.close(); } + /** + * Saves a Deadline task to the file. + * + * @param deadlineTask The Deadline task to be saved. + * @param taskType The type of the task ('D' for Deadline). + * @throws IOException If there is an error writing to the file. + */ public static void addDeadlineToFile(Deadline deadlineTask, char taskType) throws IOException { FileWriter writer = new FileWriter(filePath, true); writer.write("D | " + (deadlineTask.isDone() ? "1" : "0") + " | " + deadlineTask.getDescription() + @@ -21,6 +38,13 @@ public static void addDeadlineToFile(Deadline deadlineTask, char taskType) throw writer.close(); } + /** + * Saves an Event task to the file. + * + * @param eventTask The Event task to be saved. + * @param taskType The type of the task ('E' for Event). + * @throws IOException If there is an error writing to the file. + */ public static void addEventToFile(Event eventTask, char taskType) throws IOException { FileWriter writer = new FileWriter(filePath, true); writer.write("E | " + (eventTask.isDone() ? "1" : "0") + " | " + eventTask.getDescription() + diff --git a/src/main/java/MainRuntime/GermaBot.java b/src/main/java/MainRuntime/GermaBot.java index c09075277..79d2fe666 100644 --- a/src/main/java/MainRuntime/GermaBot.java +++ b/src/main/java/MainRuntime/GermaBot.java @@ -2,275 +2,58 @@ import Exceptions.*; import Tasks.*; +import DataHandling.*; +import GermaBot.*; -import javax.lang.model.type.NullType; import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Scanner; import java.util.ArrayList; -import java.io.File; +/** + * Main class for the GermaBot application. + */ public class GermaBot { - static int counter = 0; - static final String LINE= "____________________________________________"; - static final String WELCOME_MESSAGE = "Hello! GermaBot here! \n" - + "Let me load your saved To Do List first..."; - static final String GOODBYE_MESSAGE = "Thanks for using me! Hope to see you again soon~!"; static ArrayList toDoList = new ArrayList<>(); - - public static void loadToDo(String description) throws LoadFileException { - if (description.isEmpty()) { - throw new LoadFileException(); - } - toDoList.add(new ToDo(description)); - } - - public static void loadDeadline(String description) throws LoadFileException { - String[] deadlineDescription = description.split("\\|"); - String task = deadlineDescription[0].trim(); - String by = deadlineDescription[1].trim(); - if (task.isBlank() || by.isBlank()) { - throw new LoadFileException(); - } - toDoList.add(new Deadline(task, by)); - } - - public static void loadEvent(String description) throws LoadFileException { - String[] eventDescription = description.split("\\|"); - String task = eventDescription[0].trim(); - String from = eventDescription[1].trim(); - String by = eventDescription[2].trim(); - if (task.isBlank() || from.isBlank() || by.isBlank()) { - throw new LoadFileException(); + private static UI ui; + private static Storage storage; + + /** + * Constructor for GermaBot class. Initializes the user interface and storage manager, loads tasks from storage, and prints welcome messages. + */ + public GermaBot() { + storage = new Storage(); + ui = new UI(); + ui.printWelcomeMessage(); + try { + toDoList = storage.loadFile(); + Task.setNoOfTask(LoadData.getCounter()); + ui.printLoadComplete(); + } catch (FileNotFoundException e) { + ui.printFileNotFoundException(); + } catch (FileReadException e) { + ui.printFileReadException(); } - toDoList.add(new Event(task, from, by)); + ui.printPostLoadingMessage(); } - public static void loadFile () throws FileNotFoundException, FileReadException { - File data = new File("src/data/GermaBotData.txt"); - Scanner fileInput = new Scanner(data); - do { - String task = fileInput.nextLine(); - if (task.trim().isEmpty()) { - throw new FileReadException(); - } - counter++; - char type = task.charAt(0); - String description = task.substring(8); - boolean isCompleted = task.charAt(4) != '0'; - if (type == 'T') { - try { - loadToDo(description); - } catch (LoadFileException e) { - System.out.println("Oh no, There was an error loading your save file! Please check" + - " to make sure it follows the format..."); - } - } else if (type == 'D') { - try { - loadDeadline(description); - } catch (LoadFileException e) { - System.out.println("Oh no, There was an error loading your save file! Please check" + - " to make sure it follows the format..."); - } - } else if (type == 'E') { - try { - loadEvent(description); - } catch (LoadFileException e) { - System.out.println("Oh no, There was an error loading your save file! Please check" + - " to make sure it follows the format..."); - } - } else { + /** + * Main method to run the GermaBot application. Continuously reads user input, processes it and prints the result until the user types "bye". + */ + public void run() { + while (true) { + String input = ui.getInput(); + if (input.equals("bye")) { break; } - int idxToMark = toDoList.size() - 1; - toDoList.get(idxToMark).setDone(isCompleted); - } while (fileInput.hasNext()); - } - public static int getIdx(String input) { - return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; - } - - public static void createTodo(String input) throws EmptyTaskException, IOException { - String toDoTask = input.substring(input.indexOf("todo ") + 5); - if (toDoTask.isBlank()) { - throw new EmptyTaskException(); - } - toDoList.add(new ToDo(toDoTask)); - counter++; - DataHandling.SaveData.addTodoToFile(new ToDo(toDoTask), 'T'); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); - } - - public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException, IOException { - String description = input.replaceFirst("deadline ", ""); - if (description.equals("deadline")) { - throw new EmptyTaskException(); - } - int idxOfEndDate = description.indexOf("/"); - if (idxOfEndDate == -1) { - throw new MissingDeadlineException(); - } - String date = description.substring( idxOfEndDate + 4); - String toDoTask = description.substring(0, idxOfEndDate - 1); - if (toDoTask.isBlank()) { - throw new EmptyTaskException(); - } - toDoList.add(new Deadline(description.substring(0, idxOfEndDate - 1), date)); - counter++; - DataHandling.SaveData.addDeadlineToFile(new Deadline(description.substring(0, idxOfEndDate - 1), date), 'D'); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + - " You have to finish this by " + date + ", so be reminded!"); - } - - public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, - MissingStartDateException, IOException { - String description = input.replaceFirst("event ", ""); - int idxOfFrom = description.indexOf("/from"); - if (idxOfFrom == -1) { - throw new EmptyTaskException(); - } - int idxOfBy = description.indexOf("/to"); - if (idxOfBy == -1) { - throw new MissingDeadlineException(); - } - String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); - if (startDate.isBlank()) { - throw new MissingStartDateException(); - } - String endDate = description.substring(idxOfBy + 4); - if (endDate.isBlank()) { - throw new MissingDeadlineException(); - } - String toDoTask = description.substring(0, idxOfFrom - 1); - if (toDoTask.isBlank()) { - throw new EmptyTaskException(); - } - toDoList.add(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate)); - counter++; - DataHandling.SaveData.addEventToFile(new Event(description.substring(0, idxOfFrom - 1), startDate, endDate), 'E'); - System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + - " This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + - " on your calender! (Or ask me!)"); - } - - public static void deleteTask(String input) throws TaskNotFoundException { - int idxToDelete = getIdx(input); - if (idxToDelete >= counter) { - throw new TaskNotFoundException(); - } else { - System.out.println("Okay! I've removed this task from your To Do List:"); - System.out.println(toDoList.get(idxToDelete)); - toDoList.remove(idxToDelete); - System.out.println("Now you have, let me count... " + toDoList.size() + " items left in your " + - "To Do List!"); - counter--; - } - } - - public static void createTask(String input) throws UnknownInputException { - if (input.startsWith("delete")) { - try { - deleteTask(input); - } catch (TaskNotFoundException e) { - System.out.println("Sorry but... There is no task number " + (getIdx(input) + 1) + "... " + - "Let's try again!"); - } - } - else if (input.startsWith("todo")) { try { - createTodo(input); - } catch (EmptyTaskException e) { - System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); - } catch (IOException e) { - System.out.println("Uh oh, I could not save that to the file..."); + Logic.readCommand(toDoList, input); + } catch (UnknownInputException e) { + ui.printUnknownInputException(input); } } - else if (input.startsWith("deadline")) { - try { - createDeadline(input); - } catch (EmptyTaskException e) { - System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); - } catch (MissingDeadlineException e) { - System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); - } catch (IOException e) { - System.out.println("Uh oh, I could not save that to the file..."); - } - - } - else if (input.startsWith("event")) { - try { - createEvent(input); - } catch (EmptyTaskException e) { - System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); - } catch (MissingStartDateException e) { - System.out.println("Uh oh, you did not specify a start time! Let's try again!"); - } catch (MissingDeadlineException e) { - System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); - } catch (IOException e) { - System.out.println("Uh oh, I could not save that to the file..."); - } - } else { - throw new UnknownInputException(); - } + ui.printByeMessage(); } public static void main(String[] args) { - System.out.println(LINE); - System.out.println(WELCOME_MESSAGE); - try { - loadFile(); - System.out.println("Loading complete!"); - } catch (FileNotFoundException e) { - System.out.println("Uh oh, there is no saved file yet! Please create a GermaBotsData.txt " + - "file under './src/data/GermaBotData'!!\n" - + "Because there is no saved file found, I will start with an empty list!"); - } catch (FileReadException e) { - System.out.println("Uh oh, there seems to be an issue with the save file...\n" + - "Because there is an error with the save file, I will start with an empty list!"); - } - System.out.println("What may I do for you this fine day?"); - System.out.println(LINE); - while (true) { - String input; - Scanner in = new Scanner(System.in); - input = in.nextLine(); - if (input.equals("bye")) { - break; - } - if (input.equals("list")) { - if (toDoList.isEmpty()) { - System.out.println("Umm... You haven't added any Tasks yet... Let's try adding " + - "some now!"); - } else { - System.out.println("Gotcha! Here are your tasks:"); - for (int i = 0; i < counter; i++) { - if (toDoList.get(i) == null) { - break; - } - System.out.println(i + 1 + ". " + toDoList.get(i)); - } - } - } else if (input.contains("unmark")) { - int idx = getIdx(input); - toDoList.get(idx).setDone(false); - System.out.println("Aww, not done? Okay, I'll mark this task as undone: " - + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); - } else if (input.contains("mark")) { - int idx = getIdx(input); - toDoList.get(idx).setDone(true); - System.out.println("Good job! I'll mark this task as done: " - + "[" + toDoList.get(idx).getStatusIcon() + "] " + toDoList.get(idx).getDescription()); - } else { - try { - createTask(input); - } catch (UnknownInputException e){ - System.out.println("Uhh.. I'm sorry but I'm not quite sure what in the world '" + input + "' means..." + - " Remember to include the Task Type in front of the input!!"); - } - } - } - System.out.println(LINE); - System.out.println(GOODBYE_MESSAGE); - System.out.println(LINE); + new GermaBot().run(); } }