From da9144b7b99a8ce0e0ea327f06fb9478e908202a Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Sun, 27 Aug 2023 18:57:54 +0800 Subject: [PATCH 01/59] Rename bot and implement basic greet, exit --- src/main/java/Duke.java | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..d0effdb99 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,21 @@ +import java.util.Arrays; + public class Duke { + final private static String BOT_NAME = "Python"; + final private static int HORIZONTAL_LINE_LENGTH = 80; + + private static void printHorizontalLine() { + char[] horizontalLine = new char[HORIZONTAL_LINE_LENGTH]; + Arrays.fill(horizontalLine, '—'); + System.out.println(new String(horizontalLine)); + } + public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + printHorizontalLine(); + System.out.printf("Hello! I am a short Java Bot %s!\n", BOT_NAME); + System.out.println("What can I do for you?"); + printHorizontalLine(); + System.out.println("Bye. See you again when you run the program again!"); + printHorizontalLine(); } } From 75be7cef568c14b7d116010c030c1e2bf469e7ee Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 4 Sep 2023 20:32:00 +0800 Subject: [PATCH 02/59] Echo the user given command, exit when "bye" --- src/main/java/Duke.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index d0effdb99..b844ccebb 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,9 +1,12 @@ import java.util.Arrays; +import java.util.Scanner; public class Duke { final private static String BOT_NAME = "Python"; final private static int HORIZONTAL_LINE_LENGTH = 80; + final private static Scanner in = new Scanner(System.in); + private static void printHorizontalLine() { char[] horizontalLine = new char[HORIZONTAL_LINE_LENGTH]; Arrays.fill(horizontalLine, '—'); @@ -15,7 +18,17 @@ public static void main(String[] args) { System.out.printf("Hello! I am a short Java Bot %s!\n", BOT_NAME); System.out.println("What can I do for you?"); printHorizontalLine(); - System.out.println("Bye. See you again when you run the program again!"); - printHorizontalLine(); + + String inputCommand; + do { + inputCommand = in.nextLine(); + printHorizontalLine(); + if (inputCommand.equals("bye")) { + System.out.println("Bye. See you again when you run the program again!"); + } else { + System.out.println(inputCommand); + } + printHorizontalLine(); + } while(!inputCommand.equals("bye")); } } From afc511c819c8e3c670f78ccc7c0154c8111b68c6 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 4 Sep 2023 20:39:07 +0800 Subject: [PATCH 03/59] Add indentation to responses --- src/main/java/Duke.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b844ccebb..6c87ecf67 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -10,13 +10,13 @@ public class Duke { private static void printHorizontalLine() { char[] horizontalLine = new char[HORIZONTAL_LINE_LENGTH]; Arrays.fill(horizontalLine, '—'); - System.out.println(new String(horizontalLine)); + System.out.println("\t" + new String(horizontalLine)); } public static void main(String[] args) { printHorizontalLine(); - System.out.printf("Hello! I am a short Java Bot %s!\n", BOT_NAME); - System.out.println("What can I do for you?"); + System.out.printf("\tHello! I am a short Java Bot %s!\n", BOT_NAME); + System.out.println("\tWhat can I do for you?"); printHorizontalLine(); String inputCommand; @@ -24,9 +24,9 @@ public static void main(String[] args) { inputCommand = in.nextLine(); printHorizontalLine(); if (inputCommand.equals("bye")) { - System.out.println("Bye. See you again when you run the program again!"); + System.out.println("\tBye. See you again when you run the program again!"); } else { - System.out.println(inputCommand); + System.out.println("\t" + inputCommand); } printHorizontalLine(); } while(!inputCommand.equals("bye")); From 332b3f4c8a83495047bf2eb6784a3c4e1c10e292 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 4 Sep 2023 20:51:19 +0800 Subject: [PATCH 04/59] Add Python ASCII art --- src/main/java/Duke.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 6c87ecf67..8ea8dd669 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -5,6 +5,14 @@ public class Duke { final private static String BOT_NAME = "Python"; final private static int HORIZONTAL_LINE_LENGTH = 80; + final private static String pythonASCIIArt = + "\t ____ _ _ \n" + + "\t| _ \\ _ _| |_| |__ ___ _ __ \n" + + "\t| |_) | | | | __| _ \\ / _ \\| _ \\ \n" + + "\t| __/| |_| | |_| | | | (_) | | | |\n" + + "\t|_| \\__, |\\__|_| |_|\\___/|_| |_|\n" + + "\t |___/ "; + final private static Scanner in = new Scanner(System.in); private static void printHorizontalLine() { @@ -14,6 +22,7 @@ private static void printHorizontalLine() { } public static void main(String[] args) { + System.out.println(pythonASCIIArt); printHorizontalLine(); System.out.printf("\tHello! I am a short Java Bot %s!\n", BOT_NAME); System.out.println("\tWhat can I do for you?"); From 1ebb2bff661c6854e27e803dd3b803bb111f5d00 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 4 Sep 2023 21:11:04 +0800 Subject: [PATCH 05/59] Add Python emoji before response --- src/main/java/Duke.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 8ea8dd669..76e8af5cb 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -13,6 +13,7 @@ public class Duke { "\t|_| \\__, |\\__|_| |_|\\___/|_| |_|\n" + "\t |___/ "; + final private static String pythonEmoji = "\uD83D\uDC0D"; final private static Scanner in = new Scanner(System.in); private static void printHorizontalLine() { @@ -24,8 +25,8 @@ private static void printHorizontalLine() { public static void main(String[] args) { System.out.println(pythonASCIIArt); printHorizontalLine(); - System.out.printf("\tHello! I am a short Java Bot %s!\n", BOT_NAME); - System.out.println("\tWhat can I do for you?"); + System.out.printf("\t%s: Hello! I am a short Java Bot %s!\n", pythonEmoji, BOT_NAME); + System.out.printf("\t%s: What can I do for you?\n", pythonEmoji); printHorizontalLine(); String inputCommand; @@ -33,9 +34,9 @@ public static void main(String[] args) { inputCommand = in.nextLine(); printHorizontalLine(); if (inputCommand.equals("bye")) { - System.out.println("\tBye. See you again when you run the program again!"); + System.out.printf("\t%s: Bye. See you again when you run the program again!\n", pythonEmoji); } else { - System.out.println("\t" + inputCommand); + System.out.printf("\t%s: %s\n", pythonEmoji, inputCommand); } printHorizontalLine(); } while(!inputCommand.equals("bye")); From 111d8a1c586dc9c23c1fe929eec3cbb3df12c4e3 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 4 Sep 2023 21:39:58 +0800 Subject: [PATCH 06/59] Store commands and display if requested --- src/main/java/Duke.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 76e8af5cb..f0d888bb8 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,6 @@ +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Scanner; public class Duke { @@ -14,6 +16,8 @@ public class Duke { "\t |___/ "; final private static String pythonEmoji = "\uD83D\uDC0D"; + + final static private List commandsList = new ArrayList<>(); final private static Scanner in = new Scanner(System.in); private static void printHorizontalLine() { @@ -33,10 +37,19 @@ public static void main(String[] args) { do { inputCommand = in.nextLine(); printHorizontalLine(); - if (inputCommand.equals("bye")) { + switch (inputCommand) { + case "bye": System.out.printf("\t%s: Bye. See you again when you run the program again!\n", pythonEmoji); - } else { + break; + case "list": + System.out.printf("\t%s: You have %d tasks to do!\n", pythonEmoji, commandsList.size()); + for(int taskNo = 1; taskNo <= commandsList.size(); taskNo++) { + System.out.printf("\t\t\t%d. %s\n", taskNo, commandsList.get(taskNo - 1)); + } + break; + default: System.out.printf("\t%s: %s\n", pythonEmoji, inputCommand); + commandsList.add(inputCommand); } printHorizontalLine(); } while(!inputCommand.equals("bye")); From 0093f07a68f6ef577d48c4fbabfd25c8a4a5a3c6 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 4 Sep 2023 23:12:56 +0800 Subject: [PATCH 07/59] Add task class --- src/main/java/Task.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..479b5b894 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,25 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + + public String getDescription() { + return description; + } + + public boolean isDone() { + return isDone; + } + + public void setDone(boolean done) { + isDone = done; + } +} From 8f17b2472e623bec0c3d9b91420070e468fa623c Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 4 Sep 2023 23:13:20 +0800 Subject: [PATCH 08/59] Allow mark and unmark of tasks --- src/main/java/Duke.java | 60 ++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f0d888bb8..c3b47fa2f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -17,7 +17,7 @@ public class Duke { final private static String pythonEmoji = "\uD83D\uDC0D"; - final static private List commandsList = new ArrayList<>(); + final static private List taskList = new ArrayList<>(); final private static Scanner in = new Scanner(System.in); private static void printHorizontalLine() { @@ -33,25 +33,67 @@ public static void main(String[] args) { System.out.printf("\t%s: What can I do for you?\n", pythonEmoji); printHorizontalLine(); - String inputCommand; + String inputLine; do { - inputCommand = in.nextLine(); + inputLine = in.nextLine(); + String inputCommand = inputLine.split(" ")[0]; printHorizontalLine(); switch (inputCommand) { case "bye": System.out.printf("\t%s: Bye. See you again when you run the program again!\n", pythonEmoji); break; case "list": - System.out.printf("\t%s: You have %d tasks to do!\n", pythonEmoji, commandsList.size()); - for(int taskNo = 1; taskNo <= commandsList.size(); taskNo++) { - System.out.printf("\t\t\t%d. %s\n", taskNo, commandsList.get(taskNo - 1)); + System.out.printf("\t%s: You have %d tasks to do!\n", pythonEmoji, taskList.size()); + for(int taskNo = 1; taskNo <= taskList.size(); taskNo++) { + System.out.printf("\t\t\t%d. [%s] %s\n", + taskNo, taskList.get(taskNo - 1).getStatusIcon(), + taskList.get(taskNo - 1).getDescription()); } break; + case "mark": { + int taskNo = Integer.parseInt(inputLine.split(" ")[1]); + if (taskNo > taskList.size()) { + System.out.printf("\t%s: Are you from the future?\n", pythonEmoji); + break; + } + if (taskList.get(taskNo - 1).isDone()) { + System.out.printf("\t%s: Are you from the past?\n", pythonEmoji); + System.out.printf("\t\tTask: \"%s\" is already done!!!\n", + taskList.get(taskNo - 1).getDescription()); + break; + } + taskList.get(taskNo - 1).setDone(true); + System.out.printf("\t%s: Good job completing the task!\n", pythonEmoji); + System.out.printf("\t\t\t[%s] %s\n", + taskList.get(taskNo - 1).getStatusIcon(), + taskList.get(taskNo - 1).getDescription()); + break; + } + case "unmark": { + int taskNo = Integer.parseInt(inputLine.split(" ")[1]); + if (taskNo > taskList.size()) { + System.out.printf("\t%s: Are you from the future?\n", pythonEmoji); + break; + } + if (!taskList.get(taskNo - 1).isDone()) { + System.out.printf("\t%s: Alas! Only the completed tasks can be unmarked!\n", pythonEmoji); + System.out.printf("\t\tTask: \"%s\" is already sitting idle. Get started...!!!\n", + taskList.get(taskNo - 1).getDescription()); + break; + } + taskList.get(taskNo - 1).setDone(false); + System.out.printf("\t%s: Its okay! To err is human! Unmarked!\n", pythonEmoji); + System.out.printf("\t\t\t[%s] %s\n", + taskList.get(taskNo - 1).getStatusIcon(), + taskList.get(taskNo - 1).getDescription()); + break; + } default: - System.out.printf("\t%s: %s\n", pythonEmoji, inputCommand); - commandsList.add(inputCommand); + System.out.printf("\t%s: %s\n", pythonEmoji, inputLine); + Task task = new Task(inputLine); + taskList.add(task); } printHorizontalLine(); - } while(!inputCommand.equals("bye")); + } while(!inputLine.equals("bye")); } } From 4cf5d1e44c98ae6ec8101b934b8dd2418107f9c0 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 4 Sep 2023 23:22:15 +0800 Subject: [PATCH 09/59] Rename taskList to tasks --- src/main/java/Duke.java | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index c3b47fa2f..987bf8aa3 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -17,7 +17,7 @@ public class Duke { final private static String pythonEmoji = "\uD83D\uDC0D"; - final static private List taskList = new ArrayList<>(); + final static private List tasks = new ArrayList<>(); final private static Scanner in = new Scanner(System.in); private static void printHorizontalLine() { @@ -43,55 +43,55 @@ public static void main(String[] args) { System.out.printf("\t%s: Bye. See you again when you run the program again!\n", pythonEmoji); break; case "list": - System.out.printf("\t%s: You have %d tasks to do!\n", pythonEmoji, taskList.size()); - for(int taskNo = 1; taskNo <= taskList.size(); taskNo++) { + System.out.printf("\t%s: You have %d tasks to do!\n", pythonEmoji, tasks.size()); + for(int taskNo = 1; taskNo <= tasks.size(); taskNo++) { System.out.printf("\t\t\t%d. [%s] %s\n", - taskNo, taskList.get(taskNo - 1).getStatusIcon(), - taskList.get(taskNo - 1).getDescription()); + taskNo, tasks.get(taskNo - 1).getStatusIcon(), + tasks.get(taskNo - 1).getDescription()); } break; case "mark": { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); - if (taskNo > taskList.size()) { + if (taskNo > tasks.size()) { System.out.printf("\t%s: Are you from the future?\n", pythonEmoji); break; } - if (taskList.get(taskNo - 1).isDone()) { + if (tasks.get(taskNo - 1).isDone()) { System.out.printf("\t%s: Are you from the past?\n", pythonEmoji); System.out.printf("\t\tTask: \"%s\" is already done!!!\n", - taskList.get(taskNo - 1).getDescription()); + tasks.get(taskNo - 1).getDescription()); break; } - taskList.get(taskNo - 1).setDone(true); + tasks.get(taskNo - 1).setDone(true); System.out.printf("\t%s: Good job completing the task!\n", pythonEmoji); System.out.printf("\t\t\t[%s] %s\n", - taskList.get(taskNo - 1).getStatusIcon(), - taskList.get(taskNo - 1).getDescription()); + tasks.get(taskNo - 1).getStatusIcon(), + tasks.get(taskNo - 1).getDescription()); break; } case "unmark": { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); - if (taskNo > taskList.size()) { + if (taskNo > tasks.size()) { System.out.printf("\t%s: Are you from the future?\n", pythonEmoji); break; } - if (!taskList.get(taskNo - 1).isDone()) { + if (!tasks.get(taskNo - 1).isDone()) { System.out.printf("\t%s: Alas! Only the completed tasks can be unmarked!\n", pythonEmoji); System.out.printf("\t\tTask: \"%s\" is already sitting idle. Get started...!!!\n", - taskList.get(taskNo - 1).getDescription()); + tasks.get(taskNo - 1).getDescription()); break; } - taskList.get(taskNo - 1).setDone(false); + tasks.get(taskNo - 1).setDone(false); System.out.printf("\t%s: Its okay! To err is human! Unmarked!\n", pythonEmoji); System.out.printf("\t\t\t[%s] %s\n", - taskList.get(taskNo - 1).getStatusIcon(), - taskList.get(taskNo - 1).getDescription()); + tasks.get(taskNo - 1).getStatusIcon(), + tasks.get(taskNo - 1).getDescription()); break; } default: System.out.printf("\t%s: %s\n", pythonEmoji, inputLine); Task task = new Task(inputLine); - taskList.add(task); + tasks.add(task); } printHorizontalLine(); } while(!inputLine.equals("bye")); From 7e8e63a2fd25eede0296a39021b0c6af9588b101 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 4 Sep 2023 23:39:52 +0800 Subject: [PATCH 10/59] Format and change var names to match Java standards --- src/main/java/Duke.java | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 987bf8aa3..b5eb9e619 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -7,14 +7,13 @@ public class Duke { final private static String BOT_NAME = "Python"; final private static int HORIZONTAL_LINE_LENGTH = 80; - final private static String pythonASCIIArt = - "\t ____ _ _ \n" + - "\t| _ \\ _ _| |_| |__ ___ _ __ \n" + - "\t| |_) | | | | __| _ \\ / _ \\| _ \\ \n" + - "\t| __/| |_| | |_| | | | (_) | | | |\n" + - "\t|_| \\__, |\\__|_| |_|\\___/|_| |_|\n" + - "\t |___/ "; - + final private static String pythonAsciiArt = + "\t ____ _ _ \n" + + "\t| _ \\ _ _| |_| |__ ___ _ __ \n" + + "\t| |_) | | | | __| _ \\ / _ \\| _ \\ \n" + + "\t| __/| |_| | |_| | | | (_) | | | |\n" + + "\t|_| \\__, |\\__|_| |_|\\___/|_| |_|\n" + + "\t |___/ "; final private static String pythonEmoji = "\uD83D\uDC0D"; final static private List tasks = new ArrayList<>(); @@ -25,9 +24,9 @@ private static void printHorizontalLine() { Arrays.fill(horizontalLine, '—'); System.out.println("\t" + new String(horizontalLine)); } - + public static void main(String[] args) { - System.out.println(pythonASCIIArt); + System.out.println(pythonAsciiArt); printHorizontalLine(); System.out.printf("\t%s: Hello! I am a short Java Bot %s!\n", pythonEmoji, BOT_NAME); System.out.printf("\t%s: What can I do for you?\n", pythonEmoji); @@ -44,9 +43,8 @@ public static void main(String[] args) { break; case "list": System.out.printf("\t%s: You have %d tasks to do!\n", pythonEmoji, tasks.size()); - for(int taskNo = 1; taskNo <= tasks.size(); taskNo++) { - System.out.printf("\t\t\t%d. [%s] %s\n", - taskNo, tasks.get(taskNo - 1).getStatusIcon(), + for (int taskNo = 1; taskNo <= tasks.size(); taskNo++) { + System.out.printf("\t\t\t%d. [%s] %s\n", taskNo, tasks.get(taskNo - 1).getStatusIcon(), tasks.get(taskNo - 1).getDescription()); } break; @@ -64,8 +62,7 @@ public static void main(String[] args) { } tasks.get(taskNo - 1).setDone(true); System.out.printf("\t%s: Good job completing the task!\n", pythonEmoji); - System.out.printf("\t\t\t[%s] %s\n", - tasks.get(taskNo - 1).getStatusIcon(), + System.out.printf("\t\t\t[%s] %s\n", tasks.get(taskNo - 1).getStatusIcon(), tasks.get(taskNo - 1).getDescription()); break; } @@ -83,8 +80,7 @@ public static void main(String[] args) { } tasks.get(taskNo - 1).setDone(false); System.out.printf("\t%s: Its okay! To err is human! Unmarked!\n", pythonEmoji); - System.out.printf("\t\t\t[%s] %s\n", - tasks.get(taskNo - 1).getStatusIcon(), + System.out.printf("\t\t\t[%s] %s\n", tasks.get(taskNo - 1).getStatusIcon(), tasks.get(taskNo - 1).getDescription()); break; } @@ -92,8 +88,9 @@ public static void main(String[] args) { System.out.printf("\t%s: %s\n", pythonEmoji, inputLine); Task task = new Task(inputLine); tasks.add(task); + break; } printHorizontalLine(); - } while(!inputLine.equals("bye")); + } while (!inputLine.equals("bye")); } } From 875d4bb566bde470f8c3ce3de91654d92b2ac073 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 7 Sep 2023 01:40:17 +0800 Subject: [PATCH 11/59] Add Todo class --- src/main/java/Task.java | 4 ++++ src/main/java/Todo.java | 10 ++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 479b5b894..c7c1682f3 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -11,6 +11,10 @@ public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + public String getTypeIcon() { + return " "; + } + public String getDescription() { return description; } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..ff5d1a5bd --- /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 getTypeIcon() { + return "T"; + } +} From dca8d876119dbceef20b97ac1bc6611e595abe36 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 10:48:34 +0800 Subject: [PATCH 12/59] Add toString method to task class --- src/main/java/Duke.java | 17 +++++++---------- src/main/java/Task.java | 9 +++++++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b5eb9e619..a31f28c8e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -44,8 +44,7 @@ public static void main(String[] args) { case "list": System.out.printf("\t%s: You have %d tasks to do!\n", pythonEmoji, tasks.size()); for (int taskNo = 1; taskNo <= tasks.size(); taskNo++) { - System.out.printf("\t\t\t%d. [%s] %s\n", taskNo, tasks.get(taskNo - 1).getStatusIcon(), - tasks.get(taskNo - 1).getDescription()); + System.out.printf("\t\t\t%d. %s\n", taskNo, tasks.get(taskNo - 1)); } break; case "mark": { @@ -56,14 +55,13 @@ public static void main(String[] args) { } if (tasks.get(taskNo - 1).isDone()) { System.out.printf("\t%s: Are you from the past?\n", pythonEmoji); - System.out.printf("\t\tTask: \"%s\" is already done!!!\n", - tasks.get(taskNo - 1).getDescription()); + System.out.printf("\t\tTask: %s\n is already done!!!\n", + tasks.get(taskNo - 1)); break; } tasks.get(taskNo - 1).setDone(true); System.out.printf("\t%s: Good job completing the task!\n", pythonEmoji); - System.out.printf("\t\t\t[%s] %s\n", tasks.get(taskNo - 1).getStatusIcon(), - tasks.get(taskNo - 1).getDescription()); + System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; } case "unmark": { @@ -74,14 +72,13 @@ public static void main(String[] args) { } if (!tasks.get(taskNo - 1).isDone()) { System.out.printf("\t%s: Alas! Only the completed tasks can be unmarked!\n", pythonEmoji); - System.out.printf("\t\tTask: \"%s\" is already sitting idle. Get started...!!!\n", - tasks.get(taskNo - 1).getDescription()); + System.out.printf("\t\tTask: %s\n is already sitting idle. Get started...!!!\n", + tasks.get(taskNo - 1)); break; } tasks.get(taskNo - 1).setDone(false); System.out.printf("\t%s: Its okay! To err is human! Unmarked!\n", pythonEmoji); - System.out.printf("\t\t\t[%s] %s\n", tasks.get(taskNo - 1).getStatusIcon(), - tasks.get(taskNo - 1).getDescription()); + System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; } default: diff --git a/src/main/java/Task.java b/src/main/java/Task.java index c7c1682f3..2234b73d9 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -8,11 +8,11 @@ public Task(String description) { } public String getStatusIcon() { - return (isDone ? "X" : " "); // mark done task with X + return (isDone ? "[X]" : "[ ]"); // mark done task with X } public String getTypeIcon() { - return " "; + return "[ ]"; } public String getDescription() { @@ -26,4 +26,9 @@ public boolean isDone() { public void setDone(boolean done) { isDone = done; } + + @Override + public String toString() { + return getTypeIcon() + getStatusIcon() + " " + description; + } } From bbb2a7541d2a88da4acb5865ad320547f5487dff Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 10:56:18 +0800 Subject: [PATCH 13/59] Implement Todo class --- src/main/java/Task.java | 2 +- src/main/java/Todo.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 2234b73d9..88a90fe0f 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -29,6 +29,6 @@ public void setDone(boolean done) { @Override public String toString() { - return getTypeIcon() + getStatusIcon() + " " + description; + return getTypeIcon() + getStatusIcon() + " " + getDescription(); } } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index ff5d1a5bd..e0c5e6867 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -1,10 +1,10 @@ -public class Todo extends Task{ +public class Todo extends Task { public Todo(String description) { super(description); } @Override public String getTypeIcon() { - return "T"; + return "[T]"; } } From a08b0389604916cd45eab699ef5749424504472e Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:00:07 +0800 Subject: [PATCH 14/59] Implement Deadline class --- src/main/java/Deadline.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/java/Deadline.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..3328bb208 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,13 @@ +public class Deadline extends Task { + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String getTypeIcon() { + return "[D]"; + } +} From d0b678e3570754e6d3bfd1b54f1567cd367befbb Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:08:03 +0800 Subject: [PATCH 15/59] Implement Deadline class toString method --- src/main/java/Deadline.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 3328bb208..5b672ed35 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -10,4 +10,13 @@ public Deadline(String description, String by) { public String getTypeIcon() { return "[D]"; } + + public String getBy() { + return by; + } + + @Override + public String toString() { + return super.toString() + " (by: " + getBy() + ")"; + } } From ec7a82d573bdb8e15a59b3e1b453f4988e507ff6 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:08:18 +0800 Subject: [PATCH 16/59] Implement Event class --- src/main/java/Event.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/Event.java diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..d5dfbfc10 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,26 @@ +public class Event extends Task { + protected String from, to; + + public Event(String description, String from, String to) { + super(description); + this.from = from; + this.to = to; + } + + public String getTypeIcon() { + return "[E]"; + } + + public String getFrom() { + return from; + } + + public String getTo() { + return to; + } + + @Override + public String toString() { + return super.toString() + " (from: " + getFrom() + " to: " + getTo() + ")"; + } +} From 0591c0fa24d85e2556ca6f1c7c4deddf252fb9dd Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:42:26 +0800 Subject: [PATCH 17/59] Integrate Todo, Deadline, Event class into chatbot --- src/main/java/Duke.java | 43 ++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a31f28c8e..c91f5da16 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -31,11 +31,10 @@ public static void main(String[] args) { System.out.printf("\t%s: Hello! I am a short Java Bot %s!\n", pythonEmoji, BOT_NAME); System.out.printf("\t%s: What can I do for you?\n", pythonEmoji); printHorizontalLine(); - String inputLine; do { inputLine = in.nextLine(); - String inputCommand = inputLine.split(" ")[0]; + String inputCommand = inputLine.split("\\s+")[0]; printHorizontalLine(); switch (inputCommand) { case "bye": @@ -55,7 +54,7 @@ public static void main(String[] args) { } if (tasks.get(taskNo - 1).isDone()) { System.out.printf("\t%s: Are you from the past?\n", pythonEmoji); - System.out.printf("\t\tTask: %s\n is already done!!!\n", + System.out.printf("\t\tTask: %s\n \t\t is already done!!!\n", tasks.get(taskNo - 1)); break; } @@ -72,7 +71,7 @@ public static void main(String[] args) { } if (!tasks.get(taskNo - 1).isDone()) { System.out.printf("\t%s: Alas! Only the completed tasks can be unmarked!\n", pythonEmoji); - System.out.printf("\t\tTask: %s\n is already sitting idle. Get started...!!!\n", + System.out.printf("\t\tTask: %s\n \t\tis already sitting idle. Get started...!!!\n", tasks.get(taskNo - 1)); break; } @@ -81,10 +80,40 @@ public static void main(String[] args) { System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; } + case "todo": { + System.out.printf("\t%s: %s\n", pythonEmoji, "New Todo! You have added this todo:"); + final String todoDescription = inputLine.split(" ", 2)[1]; + Todo todo = new Todo(todoDescription); + tasks.add(todo); + System.out.printf("\t\t\t %s\n", todo); + System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); + break; + } + case "deadline": { + System.out.printf("\t%s: %s\n", pythonEmoji, "New Deadline! You have added this deadline:"); + final String deadlineDetails = inputLine.split(" ", 2)[1]; + final String deadlineDescription = deadlineDetails.split(" /by ")[0]; + final String deadlineBy = deadlineDetails.split(" /by ")[1]; + Deadline deadline = new Deadline(deadlineDescription, deadlineBy); + tasks.add(deadline); + System.out.printf("\t\t\t %s\n", deadline); + System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); + break; + } + case "event": { + System.out.printf("\t%s: %s\n", pythonEmoji, "New Event! You have added this event:"); + final String eventDetails = inputLine.split(" ", 2)[1]; + final String eventDescription = eventDetails.split(" /from | /to ", 3)[0]; + final String eventFrom = eventDetails.split(" /from | /to ", 3)[1]; + final String eventTo = eventDetails.split(" /from | /to ", 3)[2]; + Event event = new Event(eventDescription, eventFrom, eventTo); + tasks.add(event); + System.out.printf("\t\t\t %s\n", event); + System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); + break; + } default: - System.out.printf("\t%s: %s\n", pythonEmoji, inputLine); - Task task = new Task(inputLine); - tasks.add(task); + System.out.printf("\t%s: %s\n", pythonEmoji, "I cannot understand the command!"); break; } printHorizontalLine(); From 846bfbf4dc9121420d39e7fa73e272001c06905e Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 12:43:16 +0800 Subject: [PATCH 18/59] Remove trailing spaces from Ascii art --- src/main/java/Duke.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index c91f5da16..572ed599b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -8,12 +8,12 @@ public class Duke { final private static int HORIZONTAL_LINE_LENGTH = 80; final private static String pythonAsciiArt = - "\t ____ _ _ \n" + - "\t| _ \\ _ _| |_| |__ ___ _ __ \n" + - "\t| |_) | | | | __| _ \\ / _ \\| _ \\ \n" + + "\t ____ _ _\n" + + "\t| _ \\ _ _| |_| |__ ___ _ __\n" + + "\t| |_) | | | | __| _ \\ / _ \\| _ \\\n" + "\t| __/| |_| | |_| | | | (_) | | | |\n" + "\t|_| \\__, |\\__|_| |_|\\___/|_| |_|\n" + - "\t |___/ "; + "\t |___/"; final private static String pythonEmoji = "\uD83D\uDC0D"; final static private List tasks = new ArrayList<>(); From dd172031905fc98d93dd47c50679ffb77f48268b Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 12:47:08 +0800 Subject: [PATCH 19/59] Add input and expected behaviour of the bot --- text-ui-test/EXPECTED.TXT | 124 +++++++++++++++++++++++++++++++++++--- text-ui-test/input.txt | 22 +++++++ 2 files changed, 139 insertions(+), 7 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..9143fa56b 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,117 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| - + ____ _ _ + | _ \ _ _| |_| |__ ___ _ __ + | |_) | | | | __| _ \ / _ \| _ \ + | __/| |_| | |_| | | | (_) | | | | + |_| \__, |\__|_| |_|\___/|_| |_| + |___/ + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Hello! I am a short Java Bot Python! + 🐍: What can I do for you? + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: New Todo! You have added this todo: + [T][ ] borrow book + You have 1 tasks in total! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: New Todo! You have added this todo: + [T][ ] borrow cycle + You have 2 tasks in total! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: New Todo! You have added this todo: + [T][ ] cycle + You have 3 tasks in total! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: New Deadline! You have added this deadline: + [D][ ] return book (by: Sunday) + You have 4 tasks in total! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: New Event! You have added this event: + [E][ ] project meeting (from: Mon 2pm to: 4pm) + You have 5 tasks in total! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: New Event! You have added this event: + [E][ ] group study (from: 1pm to to: 2pm) + You have 6 tasks in total! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: You have 6 tasks to do! + 1. [T][ ] borrow book + 2. [T][ ] borrow cycle + 3. [T][ ] cycle + 4. [D][ ] return book (by: Sunday) + 5. [E][ ] project meeting (from: Mon 2pm to: 4pm) + 6. [E][ ] group study (from: 1pm to to: 2pm) + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Good job completing the task! + [T][X] borrow book + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Are you from the past? + Task: [T][X] borrow book + is already done!!! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Its okay! To err is human! Unmarked! + [T][ ] borrow book + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Alas! Only the completed tasks can be unmarked! + Task: [T][ ] borrow book + is already sitting idle. Get started...!!! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: I cannot understand the command! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Good job completing the task! + [T][X] borrow cycle + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Good job completing the task! + [T][X] cycle + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Good job completing the task! + [D][X] return book (by: Sunday) + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: You have 6 tasks to do! + 1. [T][ ] borrow book + 2. [T][X] borrow cycle + 3. [T][X] cycle + 4. [D][X] return book (by: Sunday) + 5. [E][ ] project meeting (from: Mon 2pm to: 4pm) + 6. [E][ ] group study (from: 1pm to to: 2pm) + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Its okay! To err is human! Unmarked! + [T][ ] cycle + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Its okay! To err is human! Unmarked! + [D][ ] return book (by: Sunday) + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: You have 6 tasks to do! + 1. [T][ ] borrow book + 2. [T][X] borrow cycle + 3. [T][ ] cycle + 4. [D][ ] return book (by: Sunday) + 5. [E][ ] project meeting (from: Mon 2pm to: 4pm) + 6. [E][ ] group study (from: 1pm to to: 2pm) + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Are you from the future? + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Are you from the future? + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Bye. See you again when you run the program again! + ———————————————————————————————————————————————————————————————————————————————— diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..94832e9a3 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,22 @@ +todo borrow book +todo borrow cycle +todo cycle +deadline return book /by Sunday +event project meeting /from Mon 2pm /to 4pm +event group study /from 1pm to /to 2pm +list +mark 1 +mark 1 +unmark 1 +unmark 1 +undo +mark 2 +mark 3 +mark 4 +list +unmark 3 +unmark 4 +list +mark 7 +unmark 7 +bye \ No newline at end of file From 16606ac3ebe0f29266335376118c3b9890782d72 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 12:54:31 +0800 Subject: [PATCH 20/59] Use all capitals for constant variables --- src/main/java/Duke.java | 34 +++++++++++++++++----------------- text-ui-test/runtest.sh | 0 2 files changed, 17 insertions(+), 17 deletions(-) mode change 100644 => 100755 text-ui-test/runtest.sh diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 572ed599b..1ce784d50 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -7,14 +7,14 @@ public class Duke { final private static String BOT_NAME = "Python"; final private static int HORIZONTAL_LINE_LENGTH = 80; - final private static String pythonAsciiArt = + final private static String PYTHON_ASCII_ART = "\t ____ _ _\n" + "\t| _ \\ _ _| |_| |__ ___ _ __\n" + "\t| |_) | | | | __| _ \\ / _ \\| _ \\\n" + "\t| __/| |_| | |_| | | | (_) | | | |\n" + "\t|_| \\__, |\\__|_| |_|\\___/|_| |_|\n" + "\t |___/"; - final private static String pythonEmoji = "\uD83D\uDC0D"; + final private static String PYTHON_EMOJI = "\uD83D\uDC0D"; final static private List tasks = new ArrayList<>(); final private static Scanner in = new Scanner(System.in); @@ -26,10 +26,10 @@ private static void printHorizontalLine() { } public static void main(String[] args) { - System.out.println(pythonAsciiArt); + System.out.println(PYTHON_ASCII_ART); printHorizontalLine(); - System.out.printf("\t%s: Hello! I am a short Java Bot %s!\n", pythonEmoji, BOT_NAME); - System.out.printf("\t%s: What can I do for you?\n", pythonEmoji); + System.out.printf("\t%s: Hello! I am a short Java Bot %s!\n", PYTHON_EMOJI, BOT_NAME); + System.out.printf("\t%s: What can I do for you?\n", PYTHON_EMOJI); printHorizontalLine(); String inputLine; do { @@ -38,10 +38,10 @@ public static void main(String[] args) { printHorizontalLine(); switch (inputCommand) { case "bye": - System.out.printf("\t%s: Bye. See you again when you run the program again!\n", pythonEmoji); + System.out.printf("\t%s: Bye. See you again when you run the program again!\n", PYTHON_EMOJI); break; case "list": - System.out.printf("\t%s: You have %d tasks to do!\n", pythonEmoji, tasks.size()); + System.out.printf("\t%s: You have %d tasks to do!\n", PYTHON_EMOJI, tasks.size()); for (int taskNo = 1; taskNo <= tasks.size(); taskNo++) { System.out.printf("\t\t\t%d. %s\n", taskNo, tasks.get(taskNo - 1)); } @@ -49,39 +49,39 @@ public static void main(String[] args) { case "mark": { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); if (taskNo > tasks.size()) { - System.out.printf("\t%s: Are you from the future?\n", pythonEmoji); + System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; } if (tasks.get(taskNo - 1).isDone()) { - System.out.printf("\t%s: Are you from the past?\n", pythonEmoji); + System.out.printf("\t%s: Are you from the past?\n", PYTHON_EMOJI); System.out.printf("\t\tTask: %s\n \t\t is already done!!!\n", tasks.get(taskNo - 1)); break; } tasks.get(taskNo - 1).setDone(true); - System.out.printf("\t%s: Good job completing the task!\n", pythonEmoji); + System.out.printf("\t%s: Good job completing the task!\n", PYTHON_EMOJI); System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; } case "unmark": { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); if (taskNo > tasks.size()) { - System.out.printf("\t%s: Are you from the future?\n", pythonEmoji); + System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; } if (!tasks.get(taskNo - 1).isDone()) { - System.out.printf("\t%s: Alas! Only the completed tasks can be unmarked!\n", pythonEmoji); + System.out.printf("\t%s: Alas! Only the completed tasks can be unmarked!\n", PYTHON_EMOJI); System.out.printf("\t\tTask: %s\n \t\tis already sitting idle. Get started...!!!\n", tasks.get(taskNo - 1)); break; } tasks.get(taskNo - 1).setDone(false); - System.out.printf("\t%s: Its okay! To err is human! Unmarked!\n", pythonEmoji); + System.out.printf("\t%s: Its okay! To err is human! Unmarked!\n", PYTHON_EMOJI); System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; } case "todo": { - System.out.printf("\t%s: %s\n", pythonEmoji, "New Todo! You have added this todo:"); + System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Todo! You have added this todo:"); final String todoDescription = inputLine.split(" ", 2)[1]; Todo todo = new Todo(todoDescription); tasks.add(todo); @@ -90,7 +90,7 @@ public static void main(String[] args) { break; } case "deadline": { - System.out.printf("\t%s: %s\n", pythonEmoji, "New Deadline! You have added this deadline:"); + System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Deadline! You have added this deadline:"); final String deadlineDetails = inputLine.split(" ", 2)[1]; final String deadlineDescription = deadlineDetails.split(" /by ")[0]; final String deadlineBy = deadlineDetails.split(" /by ")[1]; @@ -101,7 +101,7 @@ public static void main(String[] args) { break; } case "event": { - System.out.printf("\t%s: %s\n", pythonEmoji, "New Event! You have added this event:"); + System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Event! You have added this event:"); final String eventDetails = inputLine.split(" ", 2)[1]; final String eventDescription = eventDetails.split(" /from | /to ", 3)[0]; final String eventFrom = eventDetails.split(" /from | /to ", 3)[1]; @@ -113,7 +113,7 @@ public static void main(String[] args) { break; } default: - System.out.printf("\t%s: %s\n", pythonEmoji, "I cannot understand the command!"); + System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "I cannot understand the command!"); break; } printHorizontalLine(); diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh old mode 100644 new mode 100755 From 28473814a0061a08b47555bd106b49b2e5db04e1 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 13:00:01 +0800 Subject: [PATCH 21/59] Change method name to getDone --- src/main/java/Duke.java | 4 ++-- src/main/java/Task.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 1ce784d50..101918170 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -52,7 +52,7 @@ public static void main(String[] args) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; } - if (tasks.get(taskNo - 1).isDone()) { + if (tasks.get(taskNo - 1).getDone()) { System.out.printf("\t%s: Are you from the past?\n", PYTHON_EMOJI); System.out.printf("\t\tTask: %s\n \t\t is already done!!!\n", tasks.get(taskNo - 1)); @@ -69,7 +69,7 @@ public static void main(String[] args) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; } - if (!tasks.get(taskNo - 1).isDone()) { + if (!tasks.get(taskNo - 1).getDone()) { System.out.printf("\t%s: Alas! Only the completed tasks can be unmarked!\n", PYTHON_EMOJI); System.out.printf("\t\tTask: %s\n \t\tis already sitting idle. Get started...!!!\n", tasks.get(taskNo - 1)); diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 88a90fe0f..7ea2ef85a 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -19,7 +19,7 @@ public String getDescription() { return description; } - public boolean isDone() { + public boolean getDone() { return isDone; } From 2ee7cc7eb7c709a7e8a488c20ec633dea35ae45d Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 13:10:44 +0800 Subject: [PATCH 22/59] Use constant variables for command names --- src/main/java/Duke.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 101918170..0303f170a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -18,6 +18,13 @@ public class Duke { final static private List tasks = new ArrayList<>(); final private static Scanner in = new Scanner(System.in); + public static final String COMMAND_BYE = "bye"; + public static final String COMMAND_LIST = "list"; + public static final String COMMAND_MARK = "mark"; + public static final String COMMAND_UNMARK = "unmark"; + public static final String COMMAND_TODO = "todo"; + public static final String COMMAND_DEADLINE = "deadline"; + public static final String COMMAND_EVENT = "event"; private static void printHorizontalLine() { char[] horizontalLine = new char[HORIZONTAL_LINE_LENGTH]; @@ -37,16 +44,16 @@ public static void main(String[] args) { String inputCommand = inputLine.split("\\s+")[0]; printHorizontalLine(); switch (inputCommand) { - case "bye": + case COMMAND_BYE: System.out.printf("\t%s: Bye. See you again when you run the program again!\n", PYTHON_EMOJI); break; - case "list": + case COMMAND_LIST: System.out.printf("\t%s: You have %d tasks to do!\n", PYTHON_EMOJI, tasks.size()); for (int taskNo = 1; taskNo <= tasks.size(); taskNo++) { System.out.printf("\t\t\t%d. %s\n", taskNo, tasks.get(taskNo - 1)); } break; - case "mark": { + case COMMAND_MARK: { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); if (taskNo > tasks.size()) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); @@ -63,7 +70,7 @@ public static void main(String[] args) { System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; } - case "unmark": { + case COMMAND_UNMARK: { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); if (taskNo > tasks.size()) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); @@ -80,7 +87,7 @@ public static void main(String[] args) { System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; } - case "todo": { + case COMMAND_TODO: { System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Todo! You have added this todo:"); final String todoDescription = inputLine.split(" ", 2)[1]; Todo todo = new Todo(todoDescription); @@ -89,7 +96,7 @@ public static void main(String[] args) { System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); break; } - case "deadline": { + case COMMAND_DEADLINE: { System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Deadline! You have added this deadline:"); final String deadlineDetails = inputLine.split(" ", 2)[1]; final String deadlineDescription = deadlineDetails.split(" /by ")[0]; @@ -100,7 +107,7 @@ public static void main(String[] args) { System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); break; } - case "event": { + case COMMAND_EVENT: { System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Event! You have added this event:"); final String eventDetails = inputLine.split(" ", 2)[1]; final String eventDescription = eventDetails.split(" /from | /to ", 3)[0]; @@ -117,6 +124,6 @@ public static void main(String[] args) { break; } printHorizontalLine(); - } while (!inputLine.equals("bye")); + } while (!inputLine.equals(COMMAND_BYE)); } } From edcb49980f532e785d7f4758b0bcda0206dc4742 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 13:19:03 +0800 Subject: [PATCH 23/59] Use newlines to keep coherent blocks together --- src/main/java/Duke.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 0303f170a..4bfcb76b0 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -5,8 +5,6 @@ public class Duke { final private static String BOT_NAME = "Python"; - final private static int HORIZONTAL_LINE_LENGTH = 80; - final private static String PYTHON_ASCII_ART = "\t ____ _ _\n" + "\t| _ \\ _ _| |_| |__ ___ _ __\n" + @@ -17,7 +15,9 @@ public class Duke { final private static String PYTHON_EMOJI = "\uD83D\uDC0D"; final static private List tasks = new ArrayList<>(); + final private static Scanner in = new Scanner(System.in); + public static final String COMMAND_BYE = "bye"; public static final String COMMAND_LIST = "list"; public static final String COMMAND_MARK = "mark"; @@ -26,6 +26,7 @@ public class Duke { public static final String COMMAND_DEADLINE = "deadline"; public static final String COMMAND_EVENT = "event"; + final private static int HORIZONTAL_LINE_LENGTH = 80; private static void printHorizontalLine() { char[] horizontalLine = new char[HORIZONTAL_LINE_LENGTH]; Arrays.fill(horizontalLine, '—'); @@ -38,11 +39,13 @@ public static void main(String[] args) { System.out.printf("\t%s: Hello! I am a short Java Bot %s!\n", PYTHON_EMOJI, BOT_NAME); System.out.printf("\t%s: What can I do for you?\n", PYTHON_EMOJI); printHorizontalLine(); + String inputLine; do { inputLine = in.nextLine(); String inputCommand = inputLine.split("\\s+")[0]; printHorizontalLine(); + switch (inputCommand) { case COMMAND_BYE: System.out.printf("\t%s: Bye. See you again when you run the program again!\n", PYTHON_EMOJI); @@ -55,6 +58,7 @@ public static void main(String[] args) { break; case COMMAND_MARK: { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); + if (taskNo > tasks.size()) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; @@ -65,13 +69,16 @@ public static void main(String[] args) { tasks.get(taskNo - 1)); break; } + tasks.get(taskNo - 1).setDone(true); + System.out.printf("\t%s: Good job completing the task!\n", PYTHON_EMOJI); System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; } case COMMAND_UNMARK: { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); + if (taskNo > tasks.size()) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; @@ -82,7 +89,9 @@ public static void main(String[] args) { tasks.get(taskNo - 1)); break; } + tasks.get(taskNo - 1).setDone(false); + System.out.printf("\t%s: Its okay! To err is human! Unmarked!\n", PYTHON_EMOJI); System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; @@ -90,8 +99,10 @@ public static void main(String[] args) { case COMMAND_TODO: { System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Todo! You have added this todo:"); final String todoDescription = inputLine.split(" ", 2)[1]; + Todo todo = new Todo(todoDescription); tasks.add(todo); + System.out.printf("\t\t\t %s\n", todo); System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); break; @@ -101,8 +112,10 @@ public static void main(String[] args) { final String deadlineDetails = inputLine.split(" ", 2)[1]; final String deadlineDescription = deadlineDetails.split(" /by ")[0]; final String deadlineBy = deadlineDetails.split(" /by ")[1]; + Deadline deadline = new Deadline(deadlineDescription, deadlineBy); tasks.add(deadline); + System.out.printf("\t\t\t %s\n", deadline); System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); break; @@ -113,8 +126,10 @@ public static void main(String[] args) { final String eventDescription = eventDetails.split(" /from | /to ", 3)[0]; final String eventFrom = eventDetails.split(" /from | /to ", 3)[1]; final String eventTo = eventDetails.split(" /from | /to ", 3)[2]; + Event event = new Event(eventDescription, eventFrom, eventTo); tasks.add(event); + System.out.printf("\t\t\t %s\n", event); System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); break; From 8235ebc08579229c4f8d439bfd62d821ec999f78 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 13:25:33 +0800 Subject: [PATCH 24/59] Add some comments --- src/main/java/Duke.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4bfcb76b0..db450cc3b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -43,6 +43,8 @@ public static void main(String[] args) { String inputLine; do { inputLine = in.nextLine(); + + // Trim extra whitespace characters between words while splitting String inputCommand = inputLine.split("\\s+")[0]; printHorizontalLine(); @@ -59,6 +61,7 @@ public static void main(String[] args) { case COMMAND_MARK: { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); + // Handle unintended usage if (taskNo > tasks.size()) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; @@ -79,6 +82,7 @@ public static void main(String[] args) { case COMMAND_UNMARK: { int taskNo = Integer.parseInt(inputLine.split(" ")[1]); + // Handle unintended usage if (taskNo > tasks.size()) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; From 16258c40332287488392949d3ee8ae7fe7d6c3df Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 14:40:19 +0800 Subject: [PATCH 25/59] Make Task class abstract --- src/main/java/Task.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 7ea2ef85a..06c84883f 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,4 +1,4 @@ -public class Task { +public abstract class Task { protected String description; protected boolean isDone; @@ -11,9 +11,7 @@ public String getStatusIcon() { return (isDone ? "[X]" : "[ ]"); // mark done task with X } - public String getTypeIcon() { - return "[ ]"; - } + public abstract String getTypeIcon(); public String getDescription() { return description; From 54707ec1465814ae2e40d78ca2185fdb8cf89055 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 15:22:05 +0800 Subject: [PATCH 26/59] Catch exceptions and respond for unexpected input --- src/main/java/Duke.java | 80 +++++++++++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index db450cc3b..65526393e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -59,7 +59,14 @@ public static void main(String[] args) { } break; case COMMAND_MARK: { - int taskNo = Integer.parseInt(inputLine.split(" ")[1]); + int taskNo; + try { + taskNo = Integer.parseInt(inputLine.split(" ")[1]); + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + System.out.println("\t" + COMMAND_MARK + " command must be followed by an integer (task" + + " id)!"); + break; + } // Handle unintended usage if (taskNo > tasks.size()) { @@ -80,7 +87,14 @@ public static void main(String[] args) { break; } case COMMAND_UNMARK: { - int taskNo = Integer.parseInt(inputLine.split(" ")[1]); + int taskNo; + try { + taskNo = Integer.parseInt(inputLine.split(" ")[1]); + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + System.out.println("\t" + COMMAND_MARK + " command must be followed by an integer (task" + + " id)."); + break; + } // Handle unintended usage if (taskNo > tasks.size()) { @@ -101,8 +115,16 @@ public static void main(String[] args) { break; } case COMMAND_TODO: { + String todoDescription; + try { + todoDescription = inputLine.split(" ", 2)[1]; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("\t" + COMMAND_TODO + " command must be followed by a task " + + "description"); + break; + } + System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Todo! You have added this todo:"); - final String todoDescription = inputLine.split(" ", 2)[1]; Todo todo = new Todo(todoDescription); tasks.add(todo); @@ -112,10 +134,24 @@ public static void main(String[] args) { break; } case COMMAND_DEADLINE: { + String deadlineDetails, deadlineDescription, deadlineBy; + try { + deadlineDetails = inputLine.split(" ", 2)[1]; + deadlineDescription = deadlineDetails.split(" /by ")[0]; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("\t" + COMMAND_DEADLINE + " command must be followed by a task " + + "description!"); + break; + } + try { + deadlineBy = deadlineDetails.split(" /by ")[1]; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("\t" + COMMAND_DEADLINE + " command must have /by clause followed by " + + "time its due!"); + break; + } + System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Deadline! You have added this deadline:"); - final String deadlineDetails = inputLine.split(" ", 2)[1]; - final String deadlineDescription = deadlineDetails.split(" /by ")[0]; - final String deadlineBy = deadlineDetails.split(" /by ")[1]; Deadline deadline = new Deadline(deadlineDescription, deadlineBy); tasks.add(deadline); @@ -125,11 +161,31 @@ public static void main(String[] args) { break; } case COMMAND_EVENT: { + String eventDetails, eventDescription, eventFrom, eventTo; + try { + eventDetails = inputLine.split(" ", 2)[1]; + eventDescription = eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[0]; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("\t" + COMMAND_EVENT + " command must be followed by a task " + + "description!"); + break; + } + try { + eventFrom = eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[1]; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("\t" + COMMAND_EVENT + " command must have /from clause followed by " + + "time it starts!"); + break; + } + try { + eventTo = eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[2]; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("\t" + COMMAND_EVENT + " command must have /to clause followed by " + + "time it ends!"); + break; + } + System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Event! You have added this event:"); - final String eventDetails = inputLine.split(" ", 2)[1]; - final String eventDescription = eventDetails.split(" /from | /to ", 3)[0]; - final String eventFrom = eventDetails.split(" /from | /to ", 3)[1]; - final String eventTo = eventDetails.split(" /from | /to ", 3)[2]; Event event = new Event(eventDescription, eventFrom, eventTo); tasks.add(event); @@ -139,6 +195,10 @@ public static void main(String[] args) { break; } default: + if (inputLine.isEmpty()) { + System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "Nothing for me?"); + break; + } System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "I cannot understand the command!"); break; } From c60023af08ecd867aeea12d8cca8765e062ebe98 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 15:28:34 +0800 Subject: [PATCH 27/59] Fix typo in Duke --- src/main/java/Duke.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 65526393e..36b71aa87 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -91,7 +91,7 @@ public static void main(String[] args) { try { taskNo = Integer.parseInt(inputLine.split(" ")[1]); } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_MARK + " command must be followed by an integer (task" + + System.out.println("\t" + COMMAND_UNMARK + " command must be followed by an integer (task" + " id)."); break; } @@ -120,7 +120,7 @@ public static void main(String[] args) { todoDescription = inputLine.split(" ", 2)[1]; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("\t" + COMMAND_TODO + " command must be followed by a task " + - "description"); + "description!"); break; } From 652ef28ae0d7dce05a04dff7cb254bd582ae7771 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 14 Sep 2023 15:29:01 +0800 Subject: [PATCH 28/59] Update input.txt and EXPECTED.TXT --- text-ui-test/EXPECTED.TXT | 63 ++++++++++++++++++++++++++++++++++++--- text-ui-test/input.txt | 15 ++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 9143fa56b..232a8745b 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -48,6 +48,9 @@ 6. [E][ ] group study (from: 1pm to to: 2pm) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— + 🐍: Nothing for me? + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— 🐍: Good job completing the task! [T][X] borrow book ———————————————————————————————————————————————————————————————————————————————— @@ -69,6 +72,18 @@ 🐍: I cannot understand the command! ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— + mark command must be followed by an integer (task id)! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + mark command must be followed by an integer (task id)! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + unmark command must be followed by an integer (task id). + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + unmark command must be followed by an integer (task id). + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— 🐍: Good job completing the task! [T][X] borrow cycle ———————————————————————————————————————————————————————————————————————————————— @@ -81,13 +96,49 @@ [D][X] return book (by: Sunday) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— - 🐍: You have 6 tasks to do! + todo command must be followed by a task description! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + deadline command must be followed by a task description! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + event command must be followed by a task description! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + deadline command must have /by clause followed by time its due! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + deadline command must have /by clause followed by time its due! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: New Deadline! You have added this deadline: + [D][ ] return book (by: 2 June) + You have 7 tasks in total! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + event command must have /from clause followed by time it starts! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + event command must have /to clause followed by time it ends! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + event command must have /to clause followed by time it ends! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: New Event! You have added this event: + [E][ ] OOP Lecture (from: 12pm to: 2pm) + You have 8 tasks in total! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: You have 8 tasks to do! 1. [T][ ] borrow book 2. [T][X] borrow cycle 3. [T][X] cycle 4. [D][X] return book (by: Sunday) 5. [E][ ] project meeting (from: Mon 2pm to: 4pm) 6. [E][ ] group study (from: 1pm to to: 2pm) + 7. [D][ ] return book (by: 2 June) + 8. [E][ ] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— 🐍: Its okay! To err is human! Unmarked! @@ -98,19 +149,23 @@ [D][ ] return book (by: Sunday) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— - 🐍: You have 6 tasks to do! + 🐍: You have 8 tasks to do! 1. [T][ ] borrow book 2. [T][X] borrow cycle 3. [T][ ] cycle 4. [D][ ] return book (by: Sunday) 5. [E][ ] project meeting (from: Mon 2pm to: 4pm) 6. [E][ ] group study (from: 1pm to to: 2pm) + 7. [D][ ] return book (by: 2 June) + 8. [E][ ] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— - 🐍: Are you from the future? + 🐍: Good job completing the task! + [D][X] return book (by: 2 June) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— - 🐍: Are you from the future? + 🐍: Its okay! To err is human! Unmarked! + [D][ ] return book (by: 2 June) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— 🐍: Bye. See you again when you run the program again! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 94832e9a3..246df2ce6 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -5,14 +5,29 @@ deadline return book /by Sunday event project meeting /from Mon 2pm /to 4pm event group study /from 1pm to /to 2pm list + mark 1 mark 1 unmark 1 unmark 1 undo +mark a +mark +unmark c +unmark mark 2 mark 3 mark 4 +todo +deadline +event +deadline return book +deadline return book /by +deadline return book /by 2 June +event OOP Lecture +event OOP Lecture /from 12pm +event OOP Lecture /from /to 2pm +event OOP Lecture /from 12pm /to 2pm list unmark 3 unmark 4 From a45995bd4217676c375b018fa1b7b90d2b1dc764 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 13:35:35 +0800 Subject: [PATCH 29/59] Rename Duke to Python --- src/main/java/{Duke.java => Python.java} | 2 +- text-ui-test/runtest.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/java/{Duke.java => Python.java} (99%) diff --git a/src/main/java/Duke.java b/src/main/java/Python.java similarity index 99% rename from src/main/java/Duke.java rename to src/main/java/Python.java index 36b71aa87..090d36a36 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Python.java @@ -3,7 +3,7 @@ import java.util.List; import java.util.Scanner; -public class Duke { +public class Python { final private static String BOT_NAME = "Python"; final private static String PYTHON_ASCII_ART = "\t ____ _ _\n" + diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index c9ec87003..3f7371b54 100755 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -20,7 +20,7 @@ then fi # run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ../bin Duke < input.txt > ACTUAL.TXT +java -classpath ../bin Python < input.txt > ACTUAL.TXT # convert to UNIX format cp EXPECTED.TXT EXPECTED-UNIX.TXT From a35adecfd3d81891faefa00a4a909c9e0c11feb7 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 13:41:39 +0800 Subject: [PATCH 30/59] Organize files into packages --- src/main/java/{ => Python}/Python.java | 7 +++++++ src/main/java/{ => Python/Task}/Deadline.java | 2 ++ src/main/java/{ => Python/Task}/Event.java | 2 ++ src/main/java/{ => Python/Task}/Task.java | 2 ++ src/main/java/{ => Python/Task}/Todo.java | 2 ++ 5 files changed, 15 insertions(+) rename src/main/java/{ => Python}/Python.java (98%) rename src/main/java/{ => Python/Task}/Deadline.java (94%) rename src/main/java/{ => Python/Task}/Event.java (95%) rename src/main/java/{ => Python/Task}/Task.java (96%) rename src/main/java/{ => Python/Task}/Todo.java (89%) diff --git a/src/main/java/Python.java b/src/main/java/Python/Python.java similarity index 98% rename from src/main/java/Python.java rename to src/main/java/Python/Python.java index 090d36a36..00c6b712f 100644 --- a/src/main/java/Python.java +++ b/src/main/java/Python/Python.java @@ -1,3 +1,10 @@ +package Python; + +import Python.Task.Deadline; +import Python.Task.Event; +import Python.Task.Task; +import Python.Task.Todo; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; diff --git a/src/main/java/Deadline.java b/src/main/java/Python/Task/Deadline.java similarity index 94% rename from src/main/java/Deadline.java rename to src/main/java/Python/Task/Deadline.java index 5b672ed35..40d1012e0 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Python/Task/Deadline.java @@ -1,3 +1,5 @@ +package Python.Task; + public class Deadline extends Task { protected String by; diff --git a/src/main/java/Event.java b/src/main/java/Python/Task/Event.java similarity index 95% rename from src/main/java/Event.java rename to src/main/java/Python/Task/Event.java index d5dfbfc10..0b429e5ca 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Python/Task/Event.java @@ -1,3 +1,5 @@ +package Python.Task; + public class Event extends Task { protected String from, to; diff --git a/src/main/java/Task.java b/src/main/java/Python/Task/Task.java similarity index 96% rename from src/main/java/Task.java rename to src/main/java/Python/Task/Task.java index 06c84883f..1f301e238 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Python/Task/Task.java @@ -1,3 +1,5 @@ +package Python.Task; + public abstract class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/Todo.java b/src/main/java/Python/Task/Todo.java similarity index 89% rename from src/main/java/Todo.java rename to src/main/java/Python/Task/Todo.java index e0c5e6867..b97acad9c 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Python/Task/Todo.java @@ -1,3 +1,5 @@ +package Python.Task; + public class Todo extends Task { public Todo(String description) { super(description); From 6ce1ada103ccd5ebe552ccdb54de2e432c403944 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 13:42:05 +0800 Subject: [PATCH 31/59] Update the path in runtest.sh --- text-ui-test/runtest.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index 3f7371b54..0a8c6cc1f 100755 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -13,7 +13,7 @@ then fi # compile the code into the bin folder, terminates if error occurred -if ! javac -cp ../src/main/java -Xlint:none -d ../bin ../src/main/java/*.java +if ! javac -cp ../src/main/java -Xlint:none -d ../bin ../src/main/java/python/*.java ../src/main/java/python/task/*.java then echo "********** BUILD FAILURE **********" exit 1 From 96f5be8de37a0836c52de605c16c11dcf00072d4 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 14:01:26 +0800 Subject: [PATCH 32/59] Use String.repeat instead of Arrays.fill --- src/main/java/Python/Python.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/Python/Python.java b/src/main/java/Python/Python.java index 00c6b712f..6638967c4 100644 --- a/src/main/java/Python/Python.java +++ b/src/main/java/Python/Python.java @@ -35,9 +35,8 @@ public class Python { final private static int HORIZONTAL_LINE_LENGTH = 80; private static void printHorizontalLine() { - char[] horizontalLine = new char[HORIZONTAL_LINE_LENGTH]; - Arrays.fill(horizontalLine, '—'); - System.out.println("\t" + new String(horizontalLine)); + String horizontalLine = "-".repeat(HORIZONTAL_LINE_LENGTH); + System.out.println("\t" + horizontalLine); } public static void main(String[] args) { From 938cdb0c6760a69a9d391b17f822871646462271 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 14:08:29 +0800 Subject: [PATCH 33/59] Change method name from getDone() to isDone() --- src/main/java/Python/Python.java | 5 ++--- src/main/java/Python/Task/Task.java | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/Python/Python.java b/src/main/java/Python/Python.java index 6638967c4..59fdd8af2 100644 --- a/src/main/java/Python/Python.java +++ b/src/main/java/Python/Python.java @@ -6,7 +6,6 @@ import Python.Task.Todo; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Scanner; @@ -79,7 +78,7 @@ public static void main(String[] args) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; } - if (tasks.get(taskNo - 1).getDone()) { + if (tasks.get(taskNo - 1).isDone()) { System.out.printf("\t%s: Are you from the past?\n", PYTHON_EMOJI); System.out.printf("\t\tTask: %s\n \t\t is already done!!!\n", tasks.get(taskNo - 1)); @@ -107,7 +106,7 @@ public static void main(String[] args) { System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); break; } - if (!tasks.get(taskNo - 1).getDone()) { + if (!tasks.get(taskNo - 1).isDone()) { System.out.printf("\t%s: Alas! Only the completed tasks can be unmarked!\n", PYTHON_EMOJI); System.out.printf("\t\tTask: %s\n \t\tis already sitting idle. Get started...!!!\n", tasks.get(taskNo - 1)); diff --git a/src/main/java/Python/Task/Task.java b/src/main/java/Python/Task/Task.java index 1f301e238..e036e8982 100644 --- a/src/main/java/Python/Task/Task.java +++ b/src/main/java/Python/Task/Task.java @@ -10,7 +10,7 @@ public Task(String description) { } public String getStatusIcon() { - return (isDone ? "[X]" : "[ ]"); // mark done task with X + return (isDone() ? "[X]" : "[ ]"); // mark done task with X } public abstract String getTypeIcon(); @@ -19,7 +19,7 @@ public String getDescription() { return description; } - public boolean getDone() { + public boolean isDone() { return isDone; } From 085f7ec9010bb7dc39db6e1ec3446e2b9d6faa9f Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 14:15:14 +0800 Subject: [PATCH 34/59] Use Em Dash for horizontal line --- src/main/java/Python/Python.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Python/Python.java b/src/main/java/Python/Python.java index 59fdd8af2..8440af64e 100644 --- a/src/main/java/Python/Python.java +++ b/src/main/java/Python/Python.java @@ -34,7 +34,7 @@ public class Python { final private static int HORIZONTAL_LINE_LENGTH = 80; private static void printHorizontalLine() { - String horizontalLine = "-".repeat(HORIZONTAL_LINE_LENGTH); + String horizontalLine = "—".repeat(HORIZONTAL_LINE_LENGTH); System.out.println("\t" + horizontalLine); } From cabb00de92a3cad6497356babcacb7c10de73e8a Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 14:15:58 +0800 Subject: [PATCH 35/59] Fix path in runtest.sh --- text-ui-test/runtest.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index 0a8c6cc1f..338b41235 100755 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -20,7 +20,7 @@ then fi # run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ../bin Python < input.txt > ACTUAL.TXT +java -classpath ../bin Python.Python < input.txt > ACTUAL.TXT # convert to UNIX format cp EXPECTED.TXT EXPECTED-UNIX.TXT From 8dd6c036ca9e1140d10bca6443071f3f0f8ea6e7 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 21:34:59 +0800 Subject: [PATCH 36/59] Add support for deleting tasks --- src/main/java/Python/Python.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/Python/Python.java b/src/main/java/Python/Python.java index 8440af64e..e840ddc5c 100644 --- a/src/main/java/Python/Python.java +++ b/src/main/java/Python/Python.java @@ -28,6 +28,7 @@ public class Python { public static final String COMMAND_LIST = "list"; public static final String COMMAND_MARK = "mark"; public static final String COMMAND_UNMARK = "unmark"; + public static final String COMMAND_DELETE = "delete"; public static final String COMMAND_TODO = "todo"; public static final String COMMAND_DEADLINE = "deadline"; public static final String COMMAND_EVENT = "event"; @@ -119,6 +120,29 @@ public static void main(String[] args) { System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); break; } + case COMMAND_DELETE: { + int taskNo; + try { + taskNo = Integer.parseInt(inputLine.split(" ")[1]); + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + System.out.println("\t" + COMMAND_DELETE + " command must be followed by an integer (task" + + " id)."); + break; + } + + // Handle unintended usage + if (taskNo > tasks.size()) { + System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); + System.out.printf("\t%s: You have %d tasks only!\n", PYTHON_EMOJI, tasks.size()); + break; + } + + System.out.printf("\t%s: Okay. Deleting this task...!\n", PYTHON_EMOJI); + System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); + tasks.remove(taskNo - 1); + System.out.printf("\t%s: Now, you have %d tasks!\n", PYTHON_EMOJI, tasks.size()); + break; + } case COMMAND_TODO: { String todoDescription; try { From 0960bfb14db92f8236dbe61203faedda066d8c22 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 21:37:02 +0800 Subject: [PATCH 37/59] Add tests for delete feature --- text-ui-test/EXPECTED.TXT | 23 +++++++++++++++++++++-- text-ui-test/input.txt | 3 +++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 232a8745b..1f9dbbb37 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -160,12 +160,31 @@ 8. [E][ ] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— + 🐍: Okay. Deleting this task...! + [T][X] borrow cycle + 🐍: Now, you have 7 tasks! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Are you from the future? + 🐍: You have 7 tasks only! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: You have 7 tasks to do! + 1. [T][ ] borrow book + 2. [T][ ] cycle + 3. [D][ ] return book (by: Sunday) + 4. [E][ ] project meeting (from: Mon 2pm to: 4pm) + 5. [E][ ] group study (from: 1pm to to: 2pm) + 6. [D][ ] return book (by: 2 June) + 7. [E][ ] OOP Lecture (from: 12pm to: 2pm) + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— 🐍: Good job completing the task! - [D][X] return book (by: 2 June) + [E][X] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— 🐍: Its okay! To err is human! Unmarked! - [D][ ] return book (by: 2 June) + [E][ ] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— 🐍: Bye. See you again when you run the program again! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 246df2ce6..4e117df32 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -32,6 +32,9 @@ list unmark 3 unmark 4 list +delete 2 +delete 10 +list mark 7 unmark 7 bye \ No newline at end of file From 1d7a30b1430b05b62b9b14aa80f52cc856c0b0b8 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 22:48:05 +0800 Subject: [PATCH 38/59] Implement task details format method for file saving --- src/main/java/Python/Task/Deadline.java | 5 +++++ src/main/java/Python/Task/Event.java | 5 +++++ src/main/java/Python/Task/Task.java | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/src/main/java/Python/Task/Deadline.java b/src/main/java/Python/Task/Deadline.java index 40d1012e0..4f6570507 100644 --- a/src/main/java/Python/Task/Deadline.java +++ b/src/main/java/Python/Task/Deadline.java @@ -21,4 +21,9 @@ public String getBy() { public String toString() { return super.toString() + " (by: " + getBy() + ")"; } + + @Override + public String toDiskSaveFormat() { + return super.toDiskSaveFormat() + " | " + getBy(); + } } diff --git a/src/main/java/Python/Task/Event.java b/src/main/java/Python/Task/Event.java index 0b429e5ca..bf9ebb617 100644 --- a/src/main/java/Python/Task/Event.java +++ b/src/main/java/Python/Task/Event.java @@ -25,4 +25,9 @@ public String getTo() { public String toString() { return super.toString() + " (from: " + getFrom() + " to: " + getTo() + ")"; } + + @Override + public String toDiskSaveFormat() { + return super.toDiskSaveFormat() + " | " + getFrom() + " | " + getTo(); + } } diff --git a/src/main/java/Python/Task/Task.java b/src/main/java/Python/Task/Task.java index e036e8982..70ff06c49 100644 --- a/src/main/java/Python/Task/Task.java +++ b/src/main/java/Python/Task/Task.java @@ -31,4 +31,8 @@ public void setDone(boolean done) { public String toString() { return getTypeIcon() + getStatusIcon() + " " + getDescription(); } + + public String toDiskSaveFormat() { + return getTypeIcon() + " | " + getStatusIcon() + " | " + getDescription(); + } } From 4ea6ed55af8d063871b2b289e93308c2c59424ec Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 22:49:11 +0800 Subject: [PATCH 39/59] Save tasks after each change to task lists --- src/main/java/Python/Python.java | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/main/java/Python/Python.java b/src/main/java/Python/Python.java index 8440af64e..7b9d93cb7 100644 --- a/src/main/java/Python/Python.java +++ b/src/main/java/Python/Python.java @@ -8,6 +8,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Scanner; +import java.io.File; +import java.io.IOException; +import java.io.FileWriter; public class Python { final private static String BOT_NAME = "Python"; @@ -32,12 +35,59 @@ public class Python { public static final String COMMAND_DEADLINE = "deadline"; public static final String COMMAND_EVENT = "event"; + public static final String FILENAME_TASKS_LIST = "python.txt"; + public static final String DIR_TASKS_LIST = "./data/"; + final private static int HORIZONTAL_LINE_LENGTH = 80; private static void printHorizontalLine() { String horizontalLine = "—".repeat(HORIZONTAL_LINE_LENGTH); System.out.println("\t" + horizontalLine); } + private static void writeToFile(File file, String textToAdd) throws IOException { + FileWriter fw = new FileWriter(file); + fw.write(textToAdd); + fw.close(); + } + + private static void saveTasksToDisk() { + File directory = new File(DIR_TASKS_LIST); + File file = new File(directory, FILENAME_TASKS_LIST); + + // Check if directory exists, if not, create it + if (!directory.exists()) { + if (!directory.mkdirs()) { + System.out.printf("\t%s: Unable to save the tasks.\n", PYTHON_EMOJI); + System.out.printf("\t%s: Failed to create the directory: %s.\n", PYTHON_EMOJI, + DIR_TASKS_LIST); + return; + } + } + + try { + if (file.createNewFile()) { + System.out.printf("\t%s: Created file to store tasks: %s.\n", PYTHON_EMOJI, + DIR_TASKS_LIST + FILENAME_TASKS_LIST); + } + } catch (IOException e) { + e.printStackTrace(); + return; + } + + StringBuilder contentFile = new StringBuilder(); + for (Task task : tasks) { + String taskDetails = task.toDiskSaveFormat() + System.lineSeparator(); + contentFile.append(taskDetails); + } + + try { + writeToFile(file, contentFile.toString()); + } catch (IOException e) { + System.out.printf("\t%s: An error occurred while writing to the file: %s.\n", PYTHON_EMOJI, + e.getMessage()); + } + } + public static void main(String[] args) { System.out.println(PYTHON_ASCII_ART); printHorizontalLine(); @@ -89,6 +139,7 @@ public static void main(String[] args) { System.out.printf("\t%s: Good job completing the task!\n", PYTHON_EMOJI); System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); + saveTasksToDisk(); break; } case COMMAND_UNMARK: { @@ -117,6 +168,7 @@ public static void main(String[] args) { System.out.printf("\t%s: Its okay! To err is human! Unmarked!\n", PYTHON_EMOJI); System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); + saveTasksToDisk(); break; } case COMMAND_TODO: { @@ -136,6 +188,7 @@ public static void main(String[] args) { System.out.printf("\t\t\t %s\n", todo); System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); + saveTasksToDisk(); break; } case COMMAND_DEADLINE: { @@ -163,6 +216,7 @@ public static void main(String[] args) { System.out.printf("\t\t\t %s\n", deadline); System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); + saveTasksToDisk(); break; } case COMMAND_EVENT: { @@ -197,6 +251,7 @@ public static void main(String[] args) { System.out.printf("\t\t\t %s\n", event); System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); + saveTasksToDisk(); break; } default: From 3c1dc3c79145142795b462eefad088ea92fca930 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 23:55:37 +0800 Subject: [PATCH 40/59] Use type icon constant variable --- src/main/java/Python/Task/Deadline.java | 4 +++- src/main/java/Python/Task/Event.java | 3 ++- src/main/java/Python/Task/Todo.java | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/Python/Task/Deadline.java b/src/main/java/Python/Task/Deadline.java index 4f6570507..350ce805d 100644 --- a/src/main/java/Python/Task/Deadline.java +++ b/src/main/java/Python/Task/Deadline.java @@ -3,6 +3,8 @@ public class Deadline extends Task { protected String by; + final static public String TYPE_ICON = "[D]"; + public Deadline(String description, String by) { super(description); this.by = by; @@ -10,7 +12,7 @@ public Deadline(String description, String by) { @Override public String getTypeIcon() { - return "[D]"; + return TYPE_ICON; } public String getBy() { diff --git a/src/main/java/Python/Task/Event.java b/src/main/java/Python/Task/Event.java index bf9ebb617..179b80ada 100644 --- a/src/main/java/Python/Task/Event.java +++ b/src/main/java/Python/Task/Event.java @@ -2,6 +2,7 @@ public class Event extends Task { protected String from, to; + final static public String TYPE_ICON = "[E]"; public Event(String description, String from, String to) { super(description); @@ -10,7 +11,7 @@ public Event(String description, String from, String to) { } public String getTypeIcon() { - return "[E]"; + return TYPE_ICON; } public String getFrom() { diff --git a/src/main/java/Python/Task/Todo.java b/src/main/java/Python/Task/Todo.java index b97acad9c..8cb2a55d8 100644 --- a/src/main/java/Python/Task/Todo.java +++ b/src/main/java/Python/Task/Todo.java @@ -1,12 +1,15 @@ package Python.Task; public class Todo extends Task { + + final static public String TYPE_ICON = "[T]"; + public Todo(String description) { super(description); } @Override public String getTypeIcon() { - return "[T]"; + return TYPE_ICON; } } From 9141ff68884d98190e91938942d5e185fed35f84 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Mon, 18 Sep 2023 23:56:49 +0800 Subject: [PATCH 41/59] Add support for loading tasks from file --- src/main/java/Python/Python.java | 51 +++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/java/Python/Python.java b/src/main/java/Python/Python.java index 7b9d93cb7..431ba01e5 100644 --- a/src/main/java/Python/Python.java +++ b/src/main/java/Python/Python.java @@ -10,6 +10,7 @@ import java.util.Scanner; import java.io.File; import java.io.IOException; +import java.io.FileNotFoundException; import java.io.FileWriter; public class Python { @@ -88,10 +89,58 @@ private static void saveTasksToDisk() { } } + private static void loadTasksFromDisk() { + File directory = new File(DIR_TASKS_LIST); + File file = new File(directory, FILENAME_TASKS_LIST); + + Scanner s; + try { + s = new Scanner(file); + } catch (FileNotFoundException e) { + return; + } + + while (s.hasNext()) { + String[] taskDetails = s.nextLine().split(" \\| "); + String typeIcon = taskDetails[0]; + if (typeIcon.equals(Todo.TYPE_ICON)) { + String description = taskDetails[2]; + String isDone = taskDetails[1]; + Todo todo = new Todo(description); + if (isDone.equals("[X]")) { + todo.setDone(true); + } + tasks.add(todo); + } else if (typeIcon.equals(Deadline.TYPE_ICON)) { + String description = taskDetails[2]; + String isDone = taskDetails[1]; + String by = taskDetails[3]; + Deadline deadline = new Deadline(description, by); + if (isDone.equals("[X]")) { + deadline.setDone(true); + } + tasks.add(deadline); + } else { + String description = taskDetails[2]; + String isDone = taskDetails[1]; + String from = taskDetails[3]; + String to = taskDetails[4]; + Event event = new Event(description, from, to); + if (isDone.equals("[X]")) { + event.setDone(true); + } + tasks.add(event); + } + } + } + + public static void main(String[] args) { System.out.println(PYTHON_ASCII_ART); printHorizontalLine(); System.out.printf("\t%s: Hello! I am a short Java Bot %s!\n", PYTHON_EMOJI, BOT_NAME); + loadTasksFromDisk(); + System.out.printf("\t%s: You currently have %d tasks!\n", PYTHON_EMOJI, tasks.size()); System.out.printf("\t%s: What can I do for you?\n", PYTHON_EMOJI); printHorizontalLine(); @@ -108,7 +157,7 @@ public static void main(String[] args) { System.out.printf("\t%s: Bye. See you again when you run the program again!\n", PYTHON_EMOJI); break; case COMMAND_LIST: - System.out.printf("\t%s: You have %d tasks to do!\n", PYTHON_EMOJI, tasks.size()); + System.out.printf("\t%s: You have %d tasks!\n", PYTHON_EMOJI, tasks.size()); for (int taskNo = 1; taskNo <= tasks.size(); taskNo++) { System.out.printf("\t\t\t%d. %s\n", taskNo, tasks.get(taskNo - 1)); } From 6dbf98513bf860a01b9026e40103570938ef0b96 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Tue, 19 Sep 2023 00:07:49 +0800 Subject: [PATCH 42/59] Save tasks to file after delete command --- src/main/java/Python/Python.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/Python/Python.java b/src/main/java/Python/Python.java index f66740c14..c71638ec2 100644 --- a/src/main/java/Python/Python.java +++ b/src/main/java/Python/Python.java @@ -242,6 +242,7 @@ public static void main(String[] args) { System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); tasks.remove(taskNo - 1); System.out.printf("\t%s: Now, you have %d tasks!\n", PYTHON_EMOJI, tasks.size()); + saveTasksToDisk(); break; } case COMMAND_TODO: { From 5d81533cda29d2222e4467157a0d0b669bbc27f6 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Tue, 19 Sep 2023 00:10:11 +0800 Subject: [PATCH 43/59] Ignore data folder --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 2873e189e..397229206 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT + +# Data files +/data/ From 1e4f7843eb9fb4fafed32e37ad3b2319ca773408 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 5 Oct 2023 14:32:08 +0800 Subject: [PATCH 44/59] Use lower case for directory names --- src/main/java/{Python => python}/Python.java | 17 +++++++++++------ .../{Python/Task => python/task}/Deadline.java | 2 +- .../{Python/Task => python/task}/Event.java | 2 +- .../java/{Python/Task => python/task}/Task.java | 2 +- .../java/{Python/Task => python/task}/Todo.java | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) rename src/main/java/{Python => python}/Python.java (98%) rename src/main/java/{Python/Task => python/task}/Deadline.java (96%) rename src/main/java/{Python/Task => python/task}/Event.java (97%) rename src/main/java/{Python/Task => python/task}/Task.java (97%) rename src/main/java/{Python/Task => python/task}/Todo.java (91%) diff --git a/src/main/java/Python/Python.java b/src/main/java/python/Python.java similarity index 98% rename from src/main/java/Python/Python.java rename to src/main/java/python/Python.java index c71638ec2..43c848114 100644 --- a/src/main/java/Python/Python.java +++ b/src/main/java/python/Python.java @@ -1,9 +1,9 @@ -package Python; +package python; -import Python.Task.Deadline; -import Python.Task.Event; -import Python.Task.Task; -import Python.Task.Todo; +import python.task.Deadline; +import python.task.Event; +import python.task.Task; +import python.task.Todo; import java.util.ArrayList; import java.util.List; @@ -14,7 +14,7 @@ import java.io.FileWriter; public class Python { - final private static String BOT_NAME = "Python"; + final private static String BOT_NAME = "python"; final private static String PYTHON_ASCII_ART = "\t ____ _ _\n" + "\t| _ \\ _ _| |_| |__ ___ _ __\n" + @@ -333,8 +333,13 @@ public static void main(String[] args) { System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "Nothing for me?"); break; } + int a = 2; + while (a != 3) { + a++; + } System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "I cannot understand the command!"); break; + } printHorizontalLine(); } while (!inputLine.equals(COMMAND_BYE)); diff --git a/src/main/java/Python/Task/Deadline.java b/src/main/java/python/task/Deadline.java similarity index 96% rename from src/main/java/Python/Task/Deadline.java rename to src/main/java/python/task/Deadline.java index 350ce805d..98c8b48b5 100644 --- a/src/main/java/Python/Task/Deadline.java +++ b/src/main/java/python/task/Deadline.java @@ -1,4 +1,4 @@ -package Python.Task; +package python.task; public class Deadline extends Task { protected String by; diff --git a/src/main/java/Python/Task/Event.java b/src/main/java/python/task/Event.java similarity index 97% rename from src/main/java/Python/Task/Event.java rename to src/main/java/python/task/Event.java index 179b80ada..d3648412b 100644 --- a/src/main/java/Python/Task/Event.java +++ b/src/main/java/python/task/Event.java @@ -1,4 +1,4 @@ -package Python.Task; +package python.task; public class Event extends Task { protected String from, to; diff --git a/src/main/java/Python/Task/Task.java b/src/main/java/python/task/Task.java similarity index 97% rename from src/main/java/Python/Task/Task.java rename to src/main/java/python/task/Task.java index 70ff06c49..eaea5daee 100644 --- a/src/main/java/Python/Task/Task.java +++ b/src/main/java/python/task/Task.java @@ -1,4 +1,4 @@ -package Python.Task; +package python.task; public abstract class Task { protected String description; diff --git a/src/main/java/Python/Task/Todo.java b/src/main/java/python/task/Todo.java similarity index 91% rename from src/main/java/Python/Task/Todo.java rename to src/main/java/python/task/Todo.java index 8cb2a55d8..b43e34ed4 100644 --- a/src/main/java/Python/Task/Todo.java +++ b/src/main/java/python/task/Todo.java @@ -1,4 +1,4 @@ -package Python.Task; +package python.task; public class Todo extends Task { From e6fb5156cbdc0d9499ba2bc800785b032cbc8863 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Thu, 5 Oct 2023 14:32:36 +0800 Subject: [PATCH 45/59] Update text-ui-test input and expected text --- text-ui-test/EXPECTED.TXT | 47 +++++++++++++++++++++++++++++++++++---- text-ui-test/input.txt | 8 +++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 1f9dbbb37..de45879d9 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -6,6 +6,7 @@ |___/ ———————————————————————————————————————————————————————————————————————————————— 🐍: Hello! I am a short Java Bot Python! + 🐍: You currently have 0 tasks! 🐍: What can I do for you? ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— @@ -39,7 +40,7 @@ You have 6 tasks in total! ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— - 🐍: You have 6 tasks to do! + 🐍: You have 6 tasks! 1. [T][ ] borrow book 2. [T][ ] borrow cycle 3. [T][ ] cycle @@ -130,7 +131,7 @@ You have 8 tasks in total! ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— - 🐍: You have 8 tasks to do! + 🐍: You have 8 tasks! 1. [T][ ] borrow book 2. [T][X] borrow cycle 3. [T][X] cycle @@ -149,7 +150,7 @@ [D][ ] return book (by: Sunday) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— - 🐍: You have 8 tasks to do! + 🐍: You have 8 tasks! 1. [T][ ] borrow book 2. [T][X] borrow cycle 3. [T][ ] cycle @@ -169,7 +170,7 @@ 🐍: You have 7 tasks only! ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— - 🐍: You have 7 tasks to do! + 🐍: You have 7 tasks! 1. [T][ ] borrow book 2. [T][ ] cycle 3. [D][ ] return book (by: Sunday) @@ -187,5 +188,43 @@ [E][ ] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— + 🐍: Okay. Deleting this task...! + [T][ ] borrow book + 🐍: Now, you have 6 tasks! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Okay. Deleting this task...! + [T][ ] cycle + 🐍: Now, you have 5 tasks! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Okay. Deleting this task...! + [D][ ] return book (by: Sunday) + 🐍: Now, you have 4 tasks! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Okay. Deleting this task...! + [E][ ] project meeting (from: Mon 2pm to: 4pm) + 🐍: Now, you have 3 tasks! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Okay. Deleting this task...! + [E][ ] group study (from: 1pm to to: 2pm) + 🐍: Now, you have 2 tasks! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Okay. Deleting this task...! + [D][ ] return book (by: 2 June) + 🐍: Now, you have 1 tasks! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: Okay. Deleting this task...! + [E][ ] OOP Lecture (from: 12pm to: 2pm) + 🐍: Now, you have 0 tasks! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + 🐍: You have 0 tasks! + ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— 🐍: Bye. See you again when you run the program again! ———————————————————————————————————————————————————————————————————————————————— diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 4e117df32..5095ae969 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -37,4 +37,12 @@ delete 10 list mark 7 unmark 7 +delete 1 +delete 1 +delete 1 +delete 1 +delete 1 +delete 1 +delete 1 +list bye \ No newline at end of file From 19bf31a199fc2156d4661a2e61bdced2385630a6 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:53:21 +0800 Subject: [PATCH 46/59] Add Command class --- src/main/java/python/parser/Command.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/python/parser/Command.java diff --git a/src/main/java/python/parser/Command.java b/src/main/java/python/parser/Command.java new file mode 100644 index 000000000..4a1aefc09 --- /dev/null +++ b/src/main/java/python/parser/Command.java @@ -0,0 +1,16 @@ +package python.parser; + +public class Command { + public static final String COMMAND_BYE = "bye"; + public static final String COMMAND_LIST = "list"; + public static final String COMMAND_MARK = "mark"; + public static final String COMMAND_UNMARK = "unmark"; + public static final String COMMAND_DELETE = "delete"; + public static final String COMMAND_TODO = "todo"; + public static final String COMMAND_DEADLINE = "deadline"; + public static final String COMMAND_EVENT = "event"; + + public static boolean isCommandBye(String command) { + return (command.equals(COMMAND_BYE)); + } +} From d34035cf38900dae6687d8a28db069f3e0bb9f17 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:53:32 +0800 Subject: [PATCH 47/59] Add Message class --- src/main/java/python/ui/Message.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/python/ui/Message.java diff --git a/src/main/java/python/ui/Message.java b/src/main/java/python/ui/Message.java new file mode 100644 index 000000000..6c48bbe41 --- /dev/null +++ b/src/main/java/python/ui/Message.java @@ -0,0 +1,28 @@ +package python.ui; + +public class Message { + final static String MESSAGE_INTRODUCTION = "Hello! I am a Java Bot Python!"; + final static String MESSAGE_ASK = "What can I do for you?"; + final static String MESSAGE_BYE = "Bye. See you again when you run the program again!"; + final static String MESSAGE_INT_AFTER_COMMAND = "Command must be followed by an integer (task id)!"; + final static String MESSAGE_DESC_AFTER_COMMAND = "Command must be followed by a task description!!"; + final static String MESSAGE_TIME_AFTER_FROM_CLAUSE = + "Command must have /from clause followed by time it starts!"; + final static String MESSAGE_TIME_AFTER_TO_CLAUSE = + "Command must have /to clause followed by time it ends!"; + final static String MESSAGE_TIME_AFTER_BY_CLAUSE = + "Command must have /by clause followed by time its due!"; + final static String MESSAGE_FUTURE_JOKE = "Are you from the future?"; + final static String MESSAGE_PAST_JOKE = "Are you from the past?"; + final static String MESSAGE_TASK_ALREADY_DONE = "Task is already done!"; + final static String MESSAGE_CONGRATUALTE = "Good job completing the task!"; + final static String MESSAGE_UNMARK_TASK_JOKE = "Alas! Only the completed tasks can be unmarked!"; + final static String MESSAGE_TASK_IDLE = "Task is already sitting idle. Get started...!!!"; + final static String MESSAGE_MISTAKE_CONSOLATION = "Its okay! To err is human! Unmarked!"; + final static String MESSAGE_DELETE_CONFIRMATION = "Okay. Deleting this task...!"; + final static String MESSAGE_EMPTY_COMMAND_JOKE = "Nothing for me?"; + final static String MESSAGE_UNKNOWN_COMMAND = "I cannot understand the command!"; + final static String MESSAGE_NEW_TODO = "New Todo! You have added this todo:"; + final static String MESSAGE_NEW_DEADLINE = "New Deadline! You have added this deadline:"; + final static String MESSAGE_NEW_EVENT = "New Event! You have added this event:"; +} From e13479b18c1f12cc1ae10320700f5815b83598d2 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:53:57 +0800 Subject: [PATCH 48/59] Add Parser class --- src/main/java/python/parser/Parser.java | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/python/parser/Parser.java diff --git a/src/main/java/python/parser/Parser.java b/src/main/java/python/parser/Parser.java new file mode 100644 index 000000000..eaca6c578 --- /dev/null +++ b/src/main/java/python/parser/Parser.java @@ -0,0 +1,47 @@ +package python.parser; + +public class Parser { + public static String extractCommandFromInputLine(String inputLine) { + return inputLine.split("\\s+")[0]; + } + + public static String extractCommandDescFromInputLine(String inputLine) { + return inputLine.split(" ", 2)[1]; + } + + public static int extractTaskNoFromInputLine(String inputLine) { + return Integer.parseInt(inputLine.split(" ")[1]); + } + + public static String extractTodoDescFromInputLine(String inputLine) { + return inputLine.split(" ", 2)[1]; + } + + public static String extractDeadlineDetailsFromInputLine(String inputLine) { + return inputLine.split(" ", 2)[1]; + } + + public static String extractDeadlineDescFromDeadlineDetails(String deadlineDetails) { + return deadlineDetails.split(" /by ")[0]; + } + + public static String extractDeadlineByFromDeadlineDetails(String deadlineDetails) { + return deadlineDetails.split(" /by ")[1]; + } + + public static String extractEventDetailsFromInputLine(String inputLine) { + return inputLine.split(" ", 2)[1]; + } + + public static String extractEventDescFromEventDetails(String eventDetails) { + return eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[0]; + } + + public static String extractEventFromFromEventDetails(String eventDetails) { + return eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[1]; + } + + public static String extractEventToFromEventDetails(String eventDetails) { + return eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[2]; + } +} From e995cc5825330e5ed353d5703c308d54c752e1da Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:54:07 +0800 Subject: [PATCH 49/59] Add Exception class --- src/main/java/python/exception/PythonException.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/python/exception/PythonException.java diff --git a/src/main/java/python/exception/PythonException.java b/src/main/java/python/exception/PythonException.java new file mode 100644 index 000000000..4c6d6198f --- /dev/null +++ b/src/main/java/python/exception/PythonException.java @@ -0,0 +1,7 @@ +package python.exception; + +public class PythonException extends Exception { + public PythonException(String errorMessage) { + super(errorMessage); + } +} \ No newline at end of file From 7bbab3b662d0feefef5e950a844d1687afa6a480 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:54:19 +0800 Subject: [PATCH 50/59] Add TaskList class --- src/main/java/python/task/TaskList.java | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/main/java/python/task/TaskList.java diff --git a/src/main/java/python/task/TaskList.java b/src/main/java/python/task/TaskList.java new file mode 100644 index 000000000..17b1714f2 --- /dev/null +++ b/src/main/java/python/task/TaskList.java @@ -0,0 +1,37 @@ +package python.task; + +import java.util.ArrayList; +import java.util.List; + +public class TaskList { + final static private List tasks = new ArrayList<>(); + + public static Task getTask(int taskNo) { + return tasks.get(taskNo); + } + + public static List getTasks() { + return tasks; + } + + public static int getNumberOfTasks() { + return tasks.size(); + } + + public static void markTask(int taskNo) { + tasks.get(taskNo).setDone(true); + } + + public static void unmarkTask(int taskNo) { + tasks.get(taskNo).setDone(false); + } + + public static void deleteTask(int taskNo) { + tasks.remove(taskNo); + } + + public static void addTask(Task task) { + tasks.add(task); + } + +} \ No newline at end of file From 4a3c6b723928622580971de0ff9ffadf2af76f1a Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:54:51 +0800 Subject: [PATCH 51/59] Add Ui class --- src/main/java/python/ui/Ui.java | 273 ++++++++++++++++++++++++++++++++ 1 file changed, 273 insertions(+) create mode 100644 src/main/java/python/ui/Ui.java diff --git a/src/main/java/python/ui/Ui.java b/src/main/java/python/ui/Ui.java new file mode 100644 index 000000000..ff0635041 --- /dev/null +++ b/src/main/java/python/ui/Ui.java @@ -0,0 +1,273 @@ +package python.ui; + +import python.exception.PythonException; +import python.parser.Command; +import python.parser.Parser; +import python.task.Deadline; +import python.task.Event; +import python.task.TaskList; +import python.task.Todo; + +import java.util.Scanner; + +public class Ui { + private static final Scanner in = new Scanner(System.in); + final private static String PYTHON_EMOJI = "\uD83D\uDC0D"; + final private static int HORIZONTAL_LINE_LENGTH = 80; + private String inputLine; + private String inputCommand; + + final private static String PYTHON_ASCII_ART = + "\t ____ _ _\n" + + "\t| _ \\ _ _| |_| |__ ___ _ __\n" + + "\t| |_) | | | | __| _ \\ / _ \\| _ \\\n" + + "\t| __/| |_| | |_| | | | (_) | | | |\n" + + "\t|_| \\__, |\\__|_| |_|\\___/|_| |_|\n" + + "\t |___/"; + + private static void printHorizontalLine() { + String horizontalLine = "—".repeat(HORIZONTAL_LINE_LENGTH); + System.out.println("\t" + horizontalLine); + } + + private static void addEmojiAndPrint(String message) { + System.out.printf("\t%s: %s\n", PYTHON_EMOJI, message); + } + + public void welcomeUser() { + System.out.println(PYTHON_ASCII_ART); + printHorizontalLine(); + addEmojiAndPrint(Message.MESSAGE_INTRODUCTION); + displayTaskCount(); + addEmojiAndPrint(Message.MESSAGE_ASK); + printHorizontalLine(); + } + + public void greetGoodBye() { + addEmojiAndPrint(Message.MESSAGE_BYE); + } + + public void displayTaskCount() { + addEmojiAndPrint("You have " + TaskList.getNumberOfTasks() + " tasks!"); + } + + public void displayTasks() { + for (int taskNo = 0; taskNo < TaskList.getNumberOfTasks(); taskNo++) { + System.out.printf("\t\t\t%d. %s\n", taskNo + 1, TaskList.getTask(taskNo)); + } + } + + public void getUserInput() { + this.inputLine = in.nextLine(); + } + + public void displayException(Exception e) { + System.out.println("\tError: " + e.getMessage()); + } + + public void parseLine() { + this.inputCommand = Parser.extractCommandFromInputLine(inputLine); + } + + private void handleByeCommand() { + greetGoodBye(); + } + + private void handleListCommand() { + displayTaskCount(); + displayTasks(); + } + + private void handleMarkCommand() throws PythonException { + int taskNo; + try { + taskNo = Parser.extractTaskNoFromInputLine(inputLine) - 1; + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_INT_AFTER_COMMAND); + } + + // Handle unintended usage + if (taskNo >= TaskList.getNumberOfTasks()) { + addEmojiAndPrint(Message.MESSAGE_FUTURE_JOKE); + return; + } + if (TaskList.getTask(taskNo).isDone()) { + addEmojiAndPrint(Message.MESSAGE_PAST_JOKE); + addEmojiAndPrint(TaskList.getTask(taskNo).toString()); + addEmojiAndPrint(Message.MESSAGE_TASK_ALREADY_DONE); + return; + } + + TaskList.markTask(taskNo); + addEmojiAndPrint(Message.MESSAGE_CONGRATUALTE); + addEmojiAndPrint(TaskList.getTask(taskNo).toString()); + } + + private void handleUnmarkCommand() throws PythonException { + int taskNo; + try { + taskNo = Parser.extractTaskNoFromInputLine(inputLine) - 1; + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_INT_AFTER_COMMAND); + } + + // Handle unintended usage + if (taskNo >= TaskList.getNumberOfTasks()) { + addEmojiAndPrint(Message.MESSAGE_FUTURE_JOKE); + return; + } + if (!TaskList.getTask(taskNo).isDone()) { + addEmojiAndPrint(Message.MESSAGE_UNMARK_TASK_JOKE); + addEmojiAndPrint(TaskList.getTask(taskNo).toString()); + addEmojiAndPrint(Message.MESSAGE_TASK_IDLE); + return; + } + + TaskList.unmarkTask(taskNo); + + addEmojiAndPrint(Message.MESSAGE_MISTAKE_CONSOLATION); + addEmojiAndPrint(TaskList.getTask(taskNo).toString()); + } + + private void handleDeleteCommand() throws PythonException { + int taskNo; + + try { + taskNo = Parser.extractTaskNoFromInputLine(inputLine) - 1; + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_INT_AFTER_COMMAND); + } + + // Handle unintended usage + if (taskNo >= TaskList.getNumberOfTasks()) { + addEmojiAndPrint(Message.MESSAGE_FUTURE_JOKE); + displayTaskCount(); + return; + } + + addEmojiAndPrint(Message.MESSAGE_DELETE_CONFIRMATION); + addEmojiAndPrint(TaskList.getTask(taskNo).toString()); + TaskList.deleteTask(taskNo); + displayTaskCount(); + } + + + private void handleTodoCommand() throws PythonException { + String todoDescription; + try { + todoDescription = Parser.extractTodoDescFromInputLine(inputLine); + } catch (ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_DESC_AFTER_COMMAND); + } + + addEmojiAndPrint(Message.MESSAGE_NEW_TODO); + + Todo todo = new Todo(todoDescription); + + TaskList.addTask(todo); + + addEmojiAndPrint(todo.toString()); + displayTaskCount(); + } + + + private void handleDeadlineCommand() throws PythonException { + String deadlineDetails, deadlineDescription, deadlineBy; + try { + deadlineDetails = Parser.extractDeadlineDetailsFromInputLine(inputLine); + deadlineDescription = Parser.extractDeadlineDescFromDeadlineDetails(deadlineDetails); + } catch (ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_DESC_AFTER_COMMAND); + } + try { + deadlineBy = Parser.extractDeadlineByFromDeadlineDetails(deadlineDetails); + } catch (ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_TIME_AFTER_BY_CLAUSE); + } + + addEmojiAndPrint(Message.MESSAGE_NEW_DEADLINE); + + Deadline deadline = new Deadline(deadlineDescription, deadlineBy); + TaskList.addTask(deadline); + + addEmojiAndPrint(deadline.toString()); + displayTaskCount(); + } + + private void handleEventCommand() throws PythonException { + String eventDetails, eventDescription, eventFrom, eventTo; + try { + eventDetails = Parser.extractEventDetailsFromInputLine(inputLine); + eventDescription = Parser.extractEventDescFromEventDetails(eventDetails); + } catch (ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_DESC_AFTER_COMMAND); + } + + try { + eventFrom = Parser.extractEventFromFromEventDetails(eventDetails); + } catch (ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_TIME_AFTER_FROM_CLAUSE); + } + + try { + eventTo = Parser.extractEventToFromEventDetails(eventDetails); + } catch (ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_TIME_AFTER_TO_CLAUSE); + } + + addEmojiAndPrint(Message.MESSAGE_NEW_EVENT); + + Event event = new Event(eventDescription, eventFrom, eventTo); + TaskList.addTask(event); + + addEmojiAndPrint(event.toString()); + displayTaskCount(); + } + + private void handleUnknownCommand() throws PythonException { + if (inputLine.isEmpty()) { + addEmojiAndPrint(Message.MESSAGE_EMPTY_COMMAND_JOKE); + return; + } + addEmojiAndPrint(Message.MESSAGE_UNKNOWN_COMMAND); + } + + + public void executeLine() throws PythonException { + printHorizontalLine(); + switch (inputCommand) { + case Command.COMMAND_BYE: + handleByeCommand(); + break; + case Command.COMMAND_LIST: + handleListCommand(); + break; + case Command.COMMAND_MARK: + handleMarkCommand(); + break; + case Command.COMMAND_UNMARK: + handleUnmarkCommand(); + break; + case Command.COMMAND_DELETE: + handleDeleteCommand(); + break; + case Command.COMMAND_TODO: + handleTodoCommand(); + break; + case Command.COMMAND_DEADLINE: + handleDeadlineCommand(); + break; + case Command.COMMAND_EVENT: + handleEventCommand(); + break; + default: + handleUnknownCommand(); + break; + } + } + + public boolean isExit() { + return Command.isCommandBye(inputCommand); + } + +} From c84f7dc97a2b66e23ecc0d96de03467fbf886ca5 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:55:20 +0800 Subject: [PATCH 52/59] Refactor Python.java for more oop --- src/main/java/python/Python.java | 350 ++----------------------------- 1 file changed, 12 insertions(+), 338 deletions(-) diff --git a/src/main/java/python/Python.java b/src/main/java/python/Python.java index 43c848114..61c19d953 100644 --- a/src/main/java/python/Python.java +++ b/src/main/java/python/Python.java @@ -1,347 +1,21 @@ package python; -import python.task.Deadline; -import python.task.Event; -import python.task.Task; -import python.task.Todo; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; -import java.io.File; -import java.io.IOException; -import java.io.FileNotFoundException; -import java.io.FileWriter; +import python.exception.PythonException; +import python.ui.Ui; public class Python { - final private static String BOT_NAME = "python"; - final private static String PYTHON_ASCII_ART = - "\t ____ _ _\n" + - "\t| _ \\ _ _| |_| |__ ___ _ __\n" + - "\t| |_) | | | | __| _ \\ / _ \\| _ \\\n" + - "\t| __/| |_| | |_| | | | (_) | | | |\n" + - "\t|_| \\__, |\\__|_| |_|\\___/|_| |_|\n" + - "\t |___/"; - final private static String PYTHON_EMOJI = "\uD83D\uDC0D"; - - final static private List tasks = new ArrayList<>(); - - final private static Scanner in = new Scanner(System.in); - - public static final String COMMAND_BYE = "bye"; - public static final String COMMAND_LIST = "list"; - public static final String COMMAND_MARK = "mark"; - public static final String COMMAND_UNMARK = "unmark"; - public static final String COMMAND_DELETE = "delete"; - public static final String COMMAND_TODO = "todo"; - public static final String COMMAND_DEADLINE = "deadline"; - public static final String COMMAND_EVENT = "event"; - - public static final String FILENAME_TASKS_LIST = "python.txt"; - public static final String DIR_TASKS_LIST = "./data/"; - - final private static int HORIZONTAL_LINE_LENGTH = 80; - private static void printHorizontalLine() { - String horizontalLine = "—".repeat(HORIZONTAL_LINE_LENGTH); - System.out.println("\t" + horizontalLine); - } - - private static void writeToFile(File file, String textToAdd) throws IOException { - FileWriter fw = new FileWriter(file); - fw.write(textToAdd); - fw.close(); - } - - private static void saveTasksToDisk() { - File directory = new File(DIR_TASKS_LIST); - File file = new File(directory, FILENAME_TASKS_LIST); - - // Check if directory exists, if not, create it - if (!directory.exists()) { - if (!directory.mkdirs()) { - System.out.printf("\t%s: Unable to save the tasks.\n", PYTHON_EMOJI); - System.out.printf("\t%s: Failed to create the directory: %s.\n", PYTHON_EMOJI, - DIR_TASKS_LIST); - return; - } - } - - try { - if (file.createNewFile()) { - System.out.printf("\t%s: Created file to store tasks: %s.\n", PYTHON_EMOJI, - DIR_TASKS_LIST + FILENAME_TASKS_LIST); - } - } catch (IOException e) { - e.printStackTrace(); - return; - } - - StringBuilder contentFile = new StringBuilder(); - for (Task task : tasks) { - String taskDetails = task.toDiskSaveFormat() + System.lineSeparator(); - contentFile.append(taskDetails); - } - - try { - writeToFile(file, contentFile.toString()); - } catch (IOException e) { - System.out.printf("\t%s: An error occurred while writing to the file: %s.\n", PYTHON_EMOJI, - e.getMessage()); - } - } - - private static void loadTasksFromDisk() { - File directory = new File(DIR_TASKS_LIST); - File file = new File(directory, FILENAME_TASKS_LIST); - - Scanner s; - try { - s = new Scanner(file); - } catch (FileNotFoundException e) { - return; - } - - while (s.hasNext()) { - String[] taskDetails = s.nextLine().split(" \\| "); - String typeIcon = taskDetails[0]; - if (typeIcon.equals(Todo.TYPE_ICON)) { - String description = taskDetails[2]; - String isDone = taskDetails[1]; - Todo todo = new Todo(description); - if (isDone.equals("[X]")) { - todo.setDone(true); - } - tasks.add(todo); - } else if (typeIcon.equals(Deadline.TYPE_ICON)) { - String description = taskDetails[2]; - String isDone = taskDetails[1]; - String by = taskDetails[3]; - Deadline deadline = new Deadline(description, by); - if (isDone.equals("[X]")) { - deadline.setDone(true); - } - tasks.add(deadline); - } else { - String description = taskDetails[2]; - String isDone = taskDetails[1]; - String from = taskDetails[3]; - String to = taskDetails[4]; - Event event = new Event(description, from, to); - if (isDone.equals("[X]")) { - event.setDone(true); - } - tasks.add(event); - } - } - } - + static final private Ui ui = new Ui(); public static void main(String[] args) { - System.out.println(PYTHON_ASCII_ART); - printHorizontalLine(); - System.out.printf("\t%s: Hello! I am a short Java Bot %s!\n", PYTHON_EMOJI, BOT_NAME); - loadTasksFromDisk(); - System.out.printf("\t%s: You currently have %d tasks!\n", PYTHON_EMOJI, tasks.size()); - System.out.printf("\t%s: What can I do for you?\n", PYTHON_EMOJI); - printHorizontalLine(); - - String inputLine; + ui.welcomeUser(); do { - inputLine = in.nextLine(); - - // Trim extra whitespace characters between words while splitting - String inputCommand = inputLine.split("\\s+")[0]; - printHorizontalLine(); - - switch (inputCommand) { - case COMMAND_BYE: - System.out.printf("\t%s: Bye. See you again when you run the program again!\n", PYTHON_EMOJI); - break; - case COMMAND_LIST: - System.out.printf("\t%s: You have %d tasks!\n", PYTHON_EMOJI, tasks.size()); - for (int taskNo = 1; taskNo <= tasks.size(); taskNo++) { - System.out.printf("\t\t\t%d. %s\n", taskNo, tasks.get(taskNo - 1)); - } - break; - case COMMAND_MARK: { - int taskNo; - try { - taskNo = Integer.parseInt(inputLine.split(" ")[1]); - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_MARK + " command must be followed by an integer (task" + - " id)!"); - break; - } - - // Handle unintended usage - if (taskNo > tasks.size()) { - System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); - break; - } - if (tasks.get(taskNo - 1).isDone()) { - System.out.printf("\t%s: Are you from the past?\n", PYTHON_EMOJI); - System.out.printf("\t\tTask: %s\n \t\t is already done!!!\n", - tasks.get(taskNo - 1)); - break; - } - - tasks.get(taskNo - 1).setDone(true); - - System.out.printf("\t%s: Good job completing the task!\n", PYTHON_EMOJI); - System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); - saveTasksToDisk(); - break; - } - case COMMAND_UNMARK: { - int taskNo; - try { - taskNo = Integer.parseInt(inputLine.split(" ")[1]); - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_UNMARK + " command must be followed by an integer (task" + - " id)."); - break; - } - - // Handle unintended usage - if (taskNo > tasks.size()) { - System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); - break; - } - if (!tasks.get(taskNo - 1).isDone()) { - System.out.printf("\t%s: Alas! Only the completed tasks can be unmarked!\n", PYTHON_EMOJI); - System.out.printf("\t\tTask: %s\n \t\tis already sitting idle. Get started...!!!\n", - tasks.get(taskNo - 1)); - break; - } - - tasks.get(taskNo - 1).setDone(false); - - System.out.printf("\t%s: Its okay! To err is human! Unmarked!\n", PYTHON_EMOJI); - System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); - saveTasksToDisk(); - break; - } - case COMMAND_DELETE: { - int taskNo; - try { - taskNo = Integer.parseInt(inputLine.split(" ")[1]); - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_DELETE + " command must be followed by an integer (task" + - " id)."); - break; - } - - // Handle unintended usage - if (taskNo > tasks.size()) { - System.out.printf("\t%s: Are you from the future?\n", PYTHON_EMOJI); - System.out.printf("\t%s: You have %d tasks only!\n", PYTHON_EMOJI, tasks.size()); - break; - } - - System.out.printf("\t%s: Okay. Deleting this task...!\n", PYTHON_EMOJI); - System.out.printf("\t\t\t %s\n", tasks.get(taskNo - 1)); - tasks.remove(taskNo - 1); - System.out.printf("\t%s: Now, you have %d tasks!\n", PYTHON_EMOJI, tasks.size()); - saveTasksToDisk(); - break; - } - case COMMAND_TODO: { - String todoDescription; - try { - todoDescription = inputLine.split(" ", 2)[1]; - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_TODO + " command must be followed by a task " + - "description!"); - break; - } - - System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Todo! You have added this todo:"); - - Todo todo = new Todo(todoDescription); - tasks.add(todo); - - System.out.printf("\t\t\t %s\n", todo); - System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); - saveTasksToDisk(); - break; - } - case COMMAND_DEADLINE: { - String deadlineDetails, deadlineDescription, deadlineBy; - try { - deadlineDetails = inputLine.split(" ", 2)[1]; - deadlineDescription = deadlineDetails.split(" /by ")[0]; - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_DEADLINE + " command must be followed by a task " + - "description!"); - break; - } - try { - deadlineBy = deadlineDetails.split(" /by ")[1]; - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_DEADLINE + " command must have /by clause followed by " + - "time its due!"); - break; - } - - System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Deadline! You have added this deadline:"); - - Deadline deadline = new Deadline(deadlineDescription, deadlineBy); - tasks.add(deadline); - - System.out.printf("\t\t\t %s\n", deadline); - System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); - saveTasksToDisk(); - break; - } - case COMMAND_EVENT: { - String eventDetails, eventDescription, eventFrom, eventTo; - try { - eventDetails = inputLine.split(" ", 2)[1]; - eventDescription = eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[0]; - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_EVENT + " command must be followed by a task " + - "description!"); - break; - } - try { - eventFrom = eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[1]; - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_EVENT + " command must have /from clause followed by " + - "time it starts!"); - break; - } - try { - eventTo = eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[2]; - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("\t" + COMMAND_EVENT + " command must have /to clause followed by " + - "time it ends!"); - break; - } - - System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "New Event! You have added this event:"); - - Event event = new Event(eventDescription, eventFrom, eventTo); - tasks.add(event); - - System.out.printf("\t\t\t %s\n", event); - System.out.printf("\t\tYou have %d tasks in total!\n", tasks.size()); - saveTasksToDisk(); - break; - } - default: - if (inputLine.isEmpty()) { - System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "Nothing for me?"); - break; - } - int a = 2; - while (a != 3) { - a++; - } - System.out.printf("\t%s: %s\n", PYTHON_EMOJI, "I cannot understand the command!"); - break; - - } - printHorizontalLine(); - } while (!inputLine.equals(COMMAND_BYE)); + ui.getUserInput(); + try { + ui.parseLine(); + ui.executeLine(); + } catch (PythonException e) { + ui.displayException(e); + } + } while(!ui.isExit()); } } From dd0cf106e7003cfcf1e62e506a2fbdf85ef63c2e Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:56:48 +0800 Subject: [PATCH 53/59] Update text-ui-testing --- text-ui-test/EXPECTED.TXT | 168 ++++++++++++++------------------------ text-ui-test/runtest.sh | 4 +- 2 files changed, 62 insertions(+), 110 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index de45879d9..3631c6c30 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -5,40 +5,34 @@ |_| \__, |\__|_| |_|\___/|_| |_| |___/ ———————————————————————————————————————————————————————————————————————————————— - 🐍: Hello! I am a short Java Bot Python! - 🐍: You currently have 0 tasks! + 🐍: Hello! I am a Java Bot Python! + 🐍: You have 0 tasks! 🐍: What can I do for you? ———————————————————————————————————————————————————————————————————————————————— ———————————————————————————————————————————————————————————————————————————————— 🐍: New Todo! You have added this todo: - [T][ ] borrow book - You have 1 tasks in total! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][ ] borrow book + 🐍: You have 1 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: New Todo! You have added this todo: - [T][ ] borrow cycle - You have 2 tasks in total! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][ ] borrow cycle + 🐍: You have 2 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: New Todo! You have added this todo: - [T][ ] cycle - You have 3 tasks in total! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][ ] cycle + 🐍: You have 3 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: New Deadline! You have added this deadline: - [D][ ] return book (by: Sunday) - You have 4 tasks in total! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [D][ ] return book (by: Sunday) + 🐍: You have 4 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: New Event! You have added this event: - [E][ ] project meeting (from: Mon 2pm to: 4pm) - You have 5 tasks in total! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [E][ ] project meeting (from: Mon 2pm to: 4pm) + 🐍: You have 5 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: New Event! You have added this event: - [E][ ] group study (from: 1pm to to: 2pm) - You have 6 tasks in total! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [E][ ] group study (from: 1pm to to: 2pm) + 🐍: You have 6 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: You have 6 tasks! 1. [T][ ] borrow book @@ -48,88 +42,64 @@ 5. [E][ ] project meeting (from: Mon 2pm to: 4pm) 6. [E][ ] group study (from: 1pm to to: 2pm) ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— 🐍: Nothing for me? ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— 🐍: Good job completing the task! - [T][X] borrow book - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][X] borrow book ———————————————————————————————————————————————————————————————————————————————— 🐍: Are you from the past? - Task: [T][X] borrow book - is already done!!! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][X] borrow book + 🐍: Task is already done! ———————————————————————————————————————————————————————————————————————————————— 🐍: Its okay! To err is human! Unmarked! - [T][ ] borrow book - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][ ] borrow book ———————————————————————————————————————————————————————————————————————————————— 🐍: Alas! Only the completed tasks can be unmarked! - Task: [T][ ] borrow book - is already sitting idle. Get started...!!! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][ ] borrow book + 🐍: Task is already sitting idle. Get started...!!! ———————————————————————————————————————————————————————————————————————————————— 🐍: I cannot understand the command! ———————————————————————————————————————————————————————————————————————————————— + Error: Command must be followed by an integer (task id)! ———————————————————————————————————————————————————————————————————————————————— - mark command must be followed by an integer (task id)! + Error: Command must be followed by an integer (task id)! ———————————————————————————————————————————————————————————————————————————————— + Error: Command must be followed by an integer (task id)! ———————————————————————————————————————————————————————————————————————————————— - mark command must be followed by an integer (task id)! - ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— - unmark command must be followed by an integer (task id). - ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— - unmark command must be followed by an integer (task id). - ———————————————————————————————————————————————————————————————————————————————— + Error: Command must be followed by an integer (task id)! ———————————————————————————————————————————————————————————————————————————————— 🐍: Good job completing the task! - [T][X] borrow cycle - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][X] borrow cycle ———————————————————————————————————————————————————————————————————————————————— 🐍: Good job completing the task! - [T][X] cycle - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][X] cycle ———————————————————————————————————————————————————————————————————————————————— 🐍: Good job completing the task! - [D][X] return book (by: Sunday) + 🐍: [D][X] return book (by: Sunday) ———————————————————————————————————————————————————————————————————————————————— + Error: Command must be followed by a task description!! ———————————————————————————————————————————————————————————————————————————————— - todo command must be followed by a task description! + Error: Command must be followed by a task description!! ———————————————————————————————————————————————————————————————————————————————— + Error: Command must be followed by a task description!! ———————————————————————————————————————————————————————————————————————————————— - deadline command must be followed by a task description! - ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— - event command must be followed by a task description! - ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— - deadline command must have /by clause followed by time its due! - ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— - deadline command must have /by clause followed by time its due! + Error: Command must have /by clause followed by time its due! ———————————————————————————————————————————————————————————————————————————————— + Error: Command must have /by clause followed by time its due! ———————————————————————————————————————————————————————————————————————————————— 🐍: New Deadline! You have added this deadline: - [D][ ] return book (by: 2 June) - You have 7 tasks in total! - ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— - event command must have /from clause followed by time it starts! - ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— - event command must have /to clause followed by time it ends! + 🐍: [D][ ] return book (by: 2 June) + 🐍: You have 7 tasks! ———————————————————————————————————————————————————————————————————————————————— + Error: Command must have /from clause followed by time it starts! ———————————————————————————————————————————————————————————————————————————————— - event command must have /to clause followed by time it ends! + Error: Command must have /to clause followed by time it ends! ———————————————————————————————————————————————————————————————————————————————— + Error: Command must have /to clause followed by time it ends! ———————————————————————————————————————————————————————————————————————————————— 🐍: New Event! You have added this event: - [E][ ] OOP Lecture (from: 12pm to: 2pm) - You have 8 tasks in total! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [E][ ] OOP Lecture (from: 12pm to: 2pm) + 🐍: You have 8 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: You have 8 tasks! 1. [T][ ] borrow book @@ -141,14 +111,11 @@ 7. [D][ ] return book (by: 2 June) 8. [E][ ] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— 🐍: Its okay! To err is human! Unmarked! - [T][ ] cycle - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][ ] cycle ———————————————————————————————————————————————————————————————————————————————— 🐍: Its okay! To err is human! Unmarked! - [D][ ] return book (by: Sunday) - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [D][ ] return book (by: Sunday) ———————————————————————————————————————————————————————————————————————————————— 🐍: You have 8 tasks! 1. [T][ ] borrow book @@ -160,15 +127,12 @@ 7. [D][ ] return book (by: 2 June) 8. [E][ ] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— 🐍: Okay. Deleting this task...! - [T][X] borrow cycle - 🐍: Now, you have 7 tasks! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][X] borrow cycle + 🐍: You have 7 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: Are you from the future? - 🐍: You have 7 tasks only! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: You have 7 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: You have 7 tasks! 1. [T][ ] borrow book @@ -179,52 +143,40 @@ 6. [D][ ] return book (by: 2 June) 7. [E][ ] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— 🐍: Good job completing the task! - [E][X] OOP Lecture (from: 12pm to: 2pm) - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [E][X] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— 🐍: Its okay! To err is human! Unmarked! - [E][ ] OOP Lecture (from: 12pm to: 2pm) - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [E][ ] OOP Lecture (from: 12pm to: 2pm) ———————————————————————————————————————————————————————————————————————————————— 🐍: Okay. Deleting this task...! - [T][ ] borrow book - 🐍: Now, you have 6 tasks! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][ ] borrow book + 🐍: You have 6 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: Okay. Deleting this task...! - [T][ ] cycle - 🐍: Now, you have 5 tasks! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [T][ ] cycle + 🐍: You have 5 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: Okay. Deleting this task...! - [D][ ] return book (by: Sunday) - 🐍: Now, you have 4 tasks! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [D][ ] return book (by: Sunday) + 🐍: You have 4 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: Okay. Deleting this task...! - [E][ ] project meeting (from: Mon 2pm to: 4pm) - 🐍: Now, you have 3 tasks! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [E][ ] project meeting (from: Mon 2pm to: 4pm) + 🐍: You have 3 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: Okay. Deleting this task...! - [E][ ] group study (from: 1pm to to: 2pm) - 🐍: Now, you have 2 tasks! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [E][ ] group study (from: 1pm to to: 2pm) + 🐍: You have 2 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: Okay. Deleting this task...! - [D][ ] return book (by: 2 June) - 🐍: Now, you have 1 tasks! - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [D][ ] return book (by: 2 June) + 🐍: You have 1 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: Okay. Deleting this task...! - [E][ ] OOP Lecture (from: 12pm to: 2pm) - 🐍: Now, you have 0 tasks! - ———————————————————————————————————————————————————————————————————————————————— - ———————————————————————————————————————————————————————————————————————————————— + 🐍: [E][ ] OOP Lecture (from: 12pm to: 2pm) 🐍: You have 0 tasks! ———————————————————————————————————————————————————————————————————————————————— + 🐍: You have 0 tasks! ———————————————————————————————————————————————————————————————————————————————— 🐍: Bye. See you again when you run the program again! - ———————————————————————————————————————————————————————————————————————————————— diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index 338b41235..632867c6a 100755 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -13,14 +13,14 @@ then fi # compile the code into the bin folder, terminates if error occurred -if ! javac -cp ../src/main/java -Xlint:none -d ../bin ../src/main/java/python/*.java ../src/main/java/python/task/*.java +if ! javac -cp ../src/main/java -Xlint:none -d ../bin ../src/main/java/python/*.java ../src/main/java/python/task/*.java ../src/main/java/python/ui/*.java ../src/main/java/python/exception/*.java ../src/main/java/python/parser/*.java then echo "********** BUILD FAILURE **********" exit 1 fi # run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ../bin Python.Python < input.txt > ACTUAL.TXT +java -classpath ../bin python.Python < input.txt > ACTUAL.TXT # convert to UNIX format cp EXPECTED.TXT EXPECTED-UNIX.TXT From ae0c242c419a038858f2cfd5aee7cb5c3acf4d96 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:22:35 +0800 Subject: [PATCH 54/59] Add support for finding tasks --- src/main/java/python/parser/Command.java | 1 + src/main/java/python/parser/Parser.java | 4 ++++ src/main/java/python/task/TaskList.java | 9 +++++++ src/main/java/python/ui/Message.java | 3 +++ src/main/java/python/ui/Ui.java | 30 ++++++++++++++++++++---- text-ui-test/EXPECTED.TXT | 9 +++++++ text-ui-test/input.txt | 3 +++ 7 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/main/java/python/parser/Command.java b/src/main/java/python/parser/Command.java index 4a1aefc09..15d5042ac 100644 --- a/src/main/java/python/parser/Command.java +++ b/src/main/java/python/parser/Command.java @@ -9,6 +9,7 @@ public class Command { public static final String COMMAND_TODO = "todo"; public static final String COMMAND_DEADLINE = "deadline"; public static final String COMMAND_EVENT = "event"; + public static final String COMMAND_FIND = "find"; public static boolean isCommandBye(String command) { return (command.equals(COMMAND_BYE)); diff --git a/src/main/java/python/parser/Parser.java b/src/main/java/python/parser/Parser.java index eaca6c578..1fa0a7711 100644 --- a/src/main/java/python/parser/Parser.java +++ b/src/main/java/python/parser/Parser.java @@ -13,6 +13,10 @@ public static int extractTaskNoFromInputLine(String inputLine) { return Integer.parseInt(inputLine.split(" ")[1]); } + public static String extractKeywordFromInputLine(String inputLine) { + return inputLine.split(" ")[1]; + } + public static String extractTodoDescFromInputLine(String inputLine) { return inputLine.split(" ", 2)[1]; } diff --git a/src/main/java/python/task/TaskList.java b/src/main/java/python/task/TaskList.java index 17b1714f2..cf400bcde 100644 --- a/src/main/java/python/task/TaskList.java +++ b/src/main/java/python/task/TaskList.java @@ -34,4 +34,13 @@ public static void addTask(Task task) { tasks.add(task); } + public static List findTask(String keyword) { + List matchedTasks = new ArrayList<>(); + for (int taskNo = 0; taskNo < getNumberOfTasks(); taskNo++) { + if (getTask(taskNo).getDescription().contains(keyword)) { + matchedTasks.add(getTask(taskNo)); + } + } + return matchedTasks; + } } \ No newline at end of file diff --git a/src/main/java/python/ui/Message.java b/src/main/java/python/ui/Message.java index 6c48bbe41..4fb48a0e5 100644 --- a/src/main/java/python/ui/Message.java +++ b/src/main/java/python/ui/Message.java @@ -5,6 +5,7 @@ public class Message { final static String MESSAGE_ASK = "What can I do for you?"; final static String MESSAGE_BYE = "Bye. See you again when you run the program again!"; final static String MESSAGE_INT_AFTER_COMMAND = "Command must be followed by an integer (task id)!"; + final static String MESSAGE_KEYWORD_AFTER_COMMAND = "Command must be followed by a keyword!"; final static String MESSAGE_DESC_AFTER_COMMAND = "Command must be followed by a task description!!"; final static String MESSAGE_TIME_AFTER_FROM_CLAUSE = "Command must have /from clause followed by time it starts!"; @@ -25,4 +26,6 @@ public class Message { final static String MESSAGE_NEW_TODO = "New Todo! You have added this todo:"; final static String MESSAGE_NEW_DEADLINE = "New Deadline! You have added this deadline:"; final static String MESSAGE_NEW_EVENT = "New Event! You have added this event:"; + final static String MESSAGE_NO_MATCH = "No matching tasks found!"; + final static String MESSAGE_MATCHES_FOUND = "Here are the matching tasks:"; } diff --git a/src/main/java/python/ui/Ui.java b/src/main/java/python/ui/Ui.java index ff0635041..5c2ffc619 100644 --- a/src/main/java/python/ui/Ui.java +++ b/src/main/java/python/ui/Ui.java @@ -7,7 +7,9 @@ import python.task.Event; import python.task.TaskList; import python.task.Todo; +import python.task.Task; +import java.util.List; import java.util.Scanner; public class Ui { @@ -51,9 +53,9 @@ public void displayTaskCount() { addEmojiAndPrint("You have " + TaskList.getNumberOfTasks() + " tasks!"); } - public void displayTasks() { - for (int taskNo = 0; taskNo < TaskList.getNumberOfTasks(); taskNo++) { - System.out.printf("\t\t\t%d. %s\n", taskNo + 1, TaskList.getTask(taskNo)); + public void displayTasks(List tasks) { + for (int taskNo = 0; taskNo < tasks.size(); taskNo++) { + System.out.printf("\t\t\t%d. %s\n", taskNo + 1, tasks.get(taskNo)); } } @@ -75,7 +77,7 @@ private void handleByeCommand() { private void handleListCommand() { displayTaskCount(); - displayTasks(); + displayTasks(TaskList.getTasks()); } private void handleMarkCommand() throws PythonException { @@ -224,6 +226,23 @@ private void handleEventCommand() throws PythonException { displayTaskCount(); } + private void handleFindCommand() throws PythonException { + String keyword; + try { + keyword = Parser.extractKeywordFromInputLine(inputLine); + } catch (ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_KEYWORD_AFTER_COMMAND); + } + + List matchedTasks = TaskList.findTask(keyword); + + if (matchedTasks.isEmpty()) addEmojiAndPrint(Message.MESSAGE_NO_MATCH); + else { + addEmojiAndPrint(Message.MESSAGE_MATCHES_FOUND); + displayTasks(matchedTasks); + } + } + private void handleUnknownCommand() throws PythonException { if (inputLine.isEmpty()) { addEmojiAndPrint(Message.MESSAGE_EMPTY_COMMAND_JOKE); @@ -260,6 +279,9 @@ public void executeLine() throws PythonException { case Command.COMMAND_EVENT: handleEventCommand(); break; + case Command.COMMAND_FIND: + handleFindCommand(); + break; default: handleUnknownCommand(); break; diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 3631c6c30..7df3c0b5c 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -101,6 +101,15 @@ 🐍: [E][ ] OOP Lecture (from: 12pm to: 2pm) 🐍: You have 8 tasks! ———————————————————————————————————————————————————————————————————————————————— + 🐍: Here are the matching tasks: + 1. [T][ ] borrow book + 2. [D][X] return book (by: Sunday) + 3. [D][ ] return book (by: 2 June) + ———————————————————————————————————————————————————————————————————————————————— + Error: Command must be followed by a keyword! + ———————————————————————————————————————————————————————————————————————————————— + 🐍: No matching tasks found! + ———————————————————————————————————————————————————————————————————————————————— 🐍: You have 8 tasks! 1. [T][ ] borrow book 2. [T][X] borrow cycle diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 5095ae969..bcbbb8412 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -28,6 +28,9 @@ event OOP Lecture event OOP Lecture /from 12pm event OOP Lecture /from /to 2pm event OOP Lecture /from 12pm /to 2pm +find book +find +find exam list unmark 3 unmark 4 From dd55af9b92d9af92a8dcee26aaed213988f07e55 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:25:24 +0800 Subject: [PATCH 55/59] Add manifest.mf --- src/main/java/META-INF/MANIFEST.MF | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..b8735a8ae --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: python.Python + From 1dab8c7a3ee6e755578ed77a716dc20548fdac58 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:30:26 +0800 Subject: [PATCH 56/59] Add JavaDoc comments --- src/main/java/python/Python.java | 1 - src/main/java/python/parser/Command.java | 9 +++ src/main/java/python/parser/Parser.java | 75 ++++++++++++++++++++++++ src/main/java/python/task/Deadline.java | 34 ++++++++--- src/main/java/python/task/Event.java | 39 +++++++++--- src/main/java/python/task/Task.java | 46 +++++++++++++-- src/main/java/python/task/TaskList.java | 46 +++++++++++++++ src/main/java/python/task/Todo.java | 15 ++++- src/main/java/python/ui/Message.java | 3 + src/main/java/python/ui/Ui.java | 35 ++++++----- 10 files changed, 266 insertions(+), 37 deletions(-) diff --git a/src/main/java/python/Python.java b/src/main/java/python/Python.java index 61c19d953..c7eaf8897 100644 --- a/src/main/java/python/Python.java +++ b/src/main/java/python/Python.java @@ -5,7 +5,6 @@ public class Python { static final private Ui ui = new Ui(); - public static void main(String[] args) { ui.welcomeUser(); do { diff --git a/src/main/java/python/parser/Command.java b/src/main/java/python/parser/Command.java index 15d5042ac..54e5d982f 100644 --- a/src/main/java/python/parser/Command.java +++ b/src/main/java/python/parser/Command.java @@ -1,5 +1,8 @@ package python.parser; +/** + * Defines the commands expected from the user + */ public class Command { public static final String COMMAND_BYE = "bye"; public static final String COMMAND_LIST = "list"; @@ -11,6 +14,12 @@ public class Command { public static final String COMMAND_EVENT = "event"; public static final String COMMAND_FIND = "find"; + /** + * Returns whether the given command is "bye" command or not + * + * @param command The command to check + * @return Returns whether the given command is "bye" command or not + */ public static boolean isCommandBye(String command) { return (command.equals(COMMAND_BYE)); } diff --git a/src/main/java/python/parser/Parser.java b/src/main/java/python/parser/Parser.java index 1fa0a7711..16b717f1f 100644 --- a/src/main/java/python/parser/Parser.java +++ b/src/main/java/python/parser/Parser.java @@ -1,50 +1,125 @@ package python.parser; +/** + * Contains utility functions for parsing of raw input given by the user. All classes here are static + */ public class Parser { + /** + * Returns the first word as command + * + * @param inputLine The raw user input + * @return Returns the first word as command + */ public static String extractCommandFromInputLine(String inputLine) { return inputLine.split("\\s+")[0]; } + /** + * Returns the rest of the sentence excluding the first word + * + * @param inputLine The raw user input + * @return Returns the rest of the sentence excluding the first word + */ public static String extractCommandDescFromInputLine(String inputLine) { return inputLine.split(" ", 2)[1]; } + /** + * Returns the second word as integer + * + * @param inputLine The raw user input + * @return Returns the second word as integer + */ public static int extractTaskNoFromInputLine(String inputLine) { return Integer.parseInt(inputLine.split(" ")[1]); } + /** + * Returns the second word as keyword + * + * @param inputLine The raw user input + * @return Returns the second word as keyword + */ public static String extractKeywordFromInputLine(String inputLine) { return inputLine.split(" ")[1]; } + /** + * Returns the rest of the sentence excluding the first word + * + * @param inputLine The raw user input + * @return Returns the rest of the sentence excluding the first word + */ public static String extractTodoDescFromInputLine(String inputLine) { return inputLine.split(" ", 2)[1]; } + /** + * Returns the rest of the sentence excluding the first word + * + * @param inputLine The raw user input + * @return Returns the rest of the sentence excluding the first word + */ public static String extractDeadlineDetailsFromInputLine(String inputLine) { return inputLine.split(" ", 2)[1]; } + /** + * Returns the text after the first word + * + * @param deadlineDetails The deadline details + * @return Returns the text after the first word + */ public static String extractDeadlineDescFromDeadlineDetails(String deadlineDetails) { return deadlineDetails.split(" /by ")[0]; } + /** + * Returns the text after the /by command + * + * @param deadlineDetails The deadline details + * @return Returns the text after the /by command + */ public static String extractDeadlineByFromDeadlineDetails(String deadlineDetails) { return deadlineDetails.split(" /by ")[1]; } + /** + * Returns the rest of the sentence excluding the first word + * + * @param inputLine The raw user input + * @return Returns the rest of the sentence excluding the first word + */ public static String extractEventDetailsFromInputLine(String inputLine) { return inputLine.split(" ", 2)[1]; } + /** + * Returns the sentence before /from + * + * @param eventDetails The event details + * @return Returns the sentence before /from + */ public static String extractEventDescFromEventDetails(String eventDetails) { return eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[0]; } + /** + * Returns the sentence after /from and before /to + * + * @param eventDetails The event details + * @return Returns the sentence after /from and before /to + */ public static String extractEventFromFromEventDetails(String eventDetails) { return eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[1]; } + /** + * Returns the sentence after /to + * + * @param eventDetails The event details + * @return Returns the sentence after /to + */ public static String extractEventToFromEventDetails(String eventDetails) { return eventDetails.split("\\s+/from\\s+|\\s+/to\\s+", 3)[2]; } diff --git a/src/main/java/python/task/Deadline.java b/src/main/java/python/task/Deadline.java index 98c8b48b5..5df1342a1 100644 --- a/src/main/java/python/task/Deadline.java +++ b/src/main/java/python/task/Deadline.java @@ -1,31 +1,49 @@ package python.task; +/** + * Represents a deadline object using due date + */ public class Deadline extends Task { - protected String by; - - final static public String TYPE_ICON = "[D]"; + final static private String TYPE_ICON = "[D]"; + private String by; + /** + * Constructs Deadline object + * + * @param description The description of the task + * @param by The due datetime + */ public Deadline(String description, String by) { super(description); this.by = by; } + /** + * Returns the type icon of deadline + * + * @return Returns the type icon of deadline + */ @Override public String getTypeIcon() { return TYPE_ICON; } + /** + * Returns the due datetime of deadline + * + * @return Returns the due datetime of deadline + */ public String getBy() { return by; } + /** + * Returns a human-readable string format + * + * @return Returns a human-readable string format + */ @Override public String toString() { return super.toString() + " (by: " + getBy() + ")"; } - - @Override - public String toDiskSaveFormat() { - return super.toDiskSaveFormat() + " | " + getBy(); - } } diff --git a/src/main/java/python/task/Event.java b/src/main/java/python/task/Event.java index d3648412b..9f13e3b31 100644 --- a/src/main/java/python/task/Event.java +++ b/src/main/java/python/task/Event.java @@ -1,34 +1,59 @@ package python.task; +/** + * Represents a event object using start and end datetime + */ public class Event extends Task { - protected String from, to; - final static public String TYPE_ICON = "[E]"; + final static private String TYPE_ICON = "[E]"; + private String from, to; + /** + * Constructs Event object + * + * @param description The description of the task + * @param from The start datetime of the task + * @param to The end datetime of the task + */ public Event(String description, String from, String to) { super(description); this.from = from; this.to = to; } + /** + * Returns the type icon of event + * + * @return Returns the type icon of event + */ public String getTypeIcon() { return TYPE_ICON; } + /** + * Returns the start datetime of the event + * + * @return Returns the start datetime of the event + */ public String getFrom() { return from; } + /** + * Returns the end datetime of the event + * + * @return Returns the end datetime of the event + */ public String getTo() { return to; } + /** + * Returns a human-readable string format + * + * @return Returns a human-readable string format + */ @Override public String toString() { return super.toString() + " (from: " + getFrom() + " to: " + getTo() + ")"; } - - @Override - public String toDiskSaveFormat() { - return super.toDiskSaveFormat() + " | " + getFrom() + " | " + getTo(); - } } diff --git a/src/main/java/python/task/Task.java b/src/main/java/python/task/Task.java index eaea5daee..ac6f05cb1 100644 --- a/src/main/java/python/task/Task.java +++ b/src/main/java/python/task/Task.java @@ -1,38 +1,72 @@ package python.task; +/** + * Represents a abstract Task object + */ public abstract class Task { - protected String description; - protected boolean isDone; + private String description; + private boolean isDone; + /** + * Constructs Task object + * + * @param description The description of the task + */ public Task(String description) { this.description = description; this.isDone = false; } + /** + * Returns the status icon of task + * + * @return Returns the status icon of task + */ public String getStatusIcon() { return (isDone() ? "[X]" : "[ ]"); // mark done task with X } + /** + * Returns the type icon of task + * + * @return Returns the type icon of task + */ public abstract String getTypeIcon(); + /** + * Returns the description of the task + * + * @return Returns the description of the task + */ public String getDescription() { return description; } + /** + * Returns whether the is completed or not + * + * @return Returns whether the is completed or not + */ public boolean isDone() { return isDone; } + /** + * Sets the status of completion of the task + * + * @param done The status to be set + */ public void setDone(boolean done) { isDone = done; } + /** + * Returns a human-readable string format + * + * @return Returns a human-readable string format + */ @Override public String toString() { return getTypeIcon() + getStatusIcon() + " " + getDescription(); } - - public String toDiskSaveFormat() { - return getTypeIcon() + " | " + getStatusIcon() + " | " + getDescription(); - } } diff --git a/src/main/java/python/task/TaskList.java b/src/main/java/python/task/TaskList.java index cf400bcde..61ef84f1e 100644 --- a/src/main/java/python/task/TaskList.java +++ b/src/main/java/python/task/TaskList.java @@ -3,37 +3,83 @@ import java.util.ArrayList; import java.util.List; +/** + * Encapsulates the functionality of a collection of tasks + */ public class TaskList { final static private List tasks = new ArrayList<>(); + /** + * Returns a task on the given index + * + * @param taskNo Index of the task + * @return Task at the index + */ public static Task getTask(int taskNo) { return tasks.get(taskNo); } + /** + * Returns all the tasks as a List + * + * @return Returns all the tasks as a List + */ public static List getTasks() { return tasks; } + /** + * Returns the number of tasks + * + * @return Returns the number of tasks + */ public static int getNumberOfTasks() { return tasks.size(); } + /** + * Marks a task + * + * @param taskNo The index of the task + */ public static void markTask(int taskNo) { tasks.get(taskNo).setDone(true); } + /** + * Unmarks a task + * + * @param taskNo The index of the task + */ public static void unmarkTask(int taskNo) { tasks.get(taskNo).setDone(false); } + /** + * Deletes a task + * + * @param taskNo The index of the task + */ public static void deleteTask(int taskNo) { tasks.remove(taskNo); } + /** + * Adds a task + * + * @param task The task to be added + */ public static void addTask(Task task) { tasks.add(task); } + /** + * Returns a List of Task of the matching tasks. If a task description + * contains the keyword, it is considered a match. + * + * @param keyword The keyword to be searched + * @return Returns a List of Task of the matching tasks + */ public static List findTask(String keyword) { List matchedTasks = new ArrayList<>(); for (int taskNo = 0; taskNo < getNumberOfTasks(); taskNo++) { diff --git a/src/main/java/python/task/Todo.java b/src/main/java/python/task/Todo.java index b43e34ed4..a1c4e40b0 100644 --- a/src/main/java/python/task/Todo.java +++ b/src/main/java/python/task/Todo.java @@ -1,13 +1,26 @@ package python.task; +/** + * Represents a Todo object with a description + */ public class Todo extends Task { - final static public String TYPE_ICON = "[T]"; + final static private String TYPE_ICON = "[T]"; + /** + * Constructs Todo object + * + * @param description The description of the task + */ public Todo(String description) { super(description); } + /** + * Returns the type icon of todo + * + * @return Returns the type icon of todo + */ @Override public String getTypeIcon() { return TYPE_ICON; diff --git a/src/main/java/python/ui/Message.java b/src/main/java/python/ui/Message.java index 4fb48a0e5..fd0849495 100644 --- a/src/main/java/python/ui/Message.java +++ b/src/main/java/python/ui/Message.java @@ -1,5 +1,8 @@ package python.ui; +/** + * Defines string literals for the chatbot responses. + */ public class Message { final static String MESSAGE_INTRODUCTION = "Hello! I am a Java Bot Python!"; final static String MESSAGE_ASK = "What can I do for you?"; diff --git a/src/main/java/python/ui/Ui.java b/src/main/java/python/ui/Ui.java index 5c2ffc619..2fa39124a 100644 --- a/src/main/java/python/ui/Ui.java +++ b/src/main/java/python/ui/Ui.java @@ -3,22 +3,20 @@ import python.exception.PythonException; import python.parser.Command; import python.parser.Parser; -import python.task.Deadline; -import python.task.Event; -import python.task.TaskList; -import python.task.Todo; -import python.task.Task; +import python.task.*; import java.util.List; import java.util.Scanner; +/** + * Controls the user-chatbot interactions + */ public class Ui { private static final Scanner in = new Scanner(System.in); final private static String PYTHON_EMOJI = "\uD83D\uDC0D"; final private static int HORIZONTAL_LINE_LENGTH = 80; private String inputLine; private String inputCommand; - final private static String PYTHON_ASCII_ART = "\t ____ _ _\n" + "\t| _ \\ _ _| |_| |__ ___ _ __\n" + @@ -36,6 +34,9 @@ private static void addEmojiAndPrint(String message) { System.out.printf("\t%s: %s\n", PYTHON_EMOJI, message); } + /** + * Welcomes user with some predefined messages + */ public void welcomeUser() { System.out.println(PYTHON_ASCII_ART); printHorizontalLine(); @@ -45,24 +46,32 @@ public void welcomeUser() { printHorizontalLine(); } - public void greetGoodBye() { + private void greetGoodBye() { addEmojiAndPrint(Message.MESSAGE_BYE); } - public void displayTaskCount() { + private void displayTaskCount() { addEmojiAndPrint("You have " + TaskList.getNumberOfTasks() + " tasks!"); } - public void displayTasks(List tasks) { + private void displayTasks(List tasks) { for (int taskNo = 0; taskNo < tasks.size(); taskNo++) { System.out.printf("\t\t\t%d. %s\n", taskNo + 1, tasks.get(taskNo)); } } + /** + * Gets the user given raw input + */ public void getUserInput() { this.inputLine = in.nextLine(); } + /** + * Displays the error messages + * + * @param e The error message + */ public void displayException(Exception e) { System.out.println("\tError: " + e.getMessage()); } @@ -153,7 +162,6 @@ private void handleDeleteCommand() throws PythonException { displayTaskCount(); } - private void handleTodoCommand() throws PythonException { String todoDescription; try { @@ -172,7 +180,6 @@ private void handleTodoCommand() throws PythonException { displayTaskCount(); } - private void handleDeadlineCommand() throws PythonException { String deadlineDetails, deadlineDescription, deadlineBy; try { @@ -236,8 +243,9 @@ private void handleFindCommand() throws PythonException { List matchedTasks = TaskList.findTask(keyword); - if (matchedTasks.isEmpty()) addEmojiAndPrint(Message.MESSAGE_NO_MATCH); - else { + if (matchedTasks.isEmpty()) { + addEmojiAndPrint(Message.MESSAGE_NO_MATCH); + } else { addEmojiAndPrint(Message.MESSAGE_MATCHES_FOUND); displayTasks(matchedTasks); } @@ -251,7 +259,6 @@ private void handleUnknownCommand() throws PythonException { addEmojiAndPrint(Message.MESSAGE_UNKNOWN_COMMAND); } - public void executeLine() throws PythonException { printHorizontalLine(); switch (inputCommand) { From f61a1b9aa5dbc9dd2bcb05de98359e7662e12e17 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:13:19 +0800 Subject: [PATCH 57/59] Add user guide --- docs/README.md | 109 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 15 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..b9eeaa290 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,108 @@ -# User Guide +# Python User Guide + +**Python** task manager will be a blessing for users with fast typing speed. You can +add three types of tasks, mark tasks as completed, delete tasks, and if necessary +find tasks using a keyword! ## Features +Python comes with the following core features: +* [Add Todo](#add-todo) +* [Add Deadline](#add-deadline) +* [Add Event](#add-event) +* [Mark Task](#mark-task) +* [Unmark Task](#unmark-task) +* [Delete Task](#delete-task) +* [Find Tasks](#find-tasks) +* [List Tasks](#list-tasks) +* [Exit](#exit-program) + +### Add Todo +Format: +`todo [todo description]` + +Example of usage: +`todo buy groceries` + +Add a simple todo! + +### Add Deadline + +Format: +`deadline [deadline description] /by [due date]` + +Example of usage: +`deadline lab 2 /by friday night` + +Add a deadline with due date! + + +### Add Event + +Format: +`event [event description] /from [start date] /to [end date]` + +Example of usage: +`event math exam /from monday 2pm /to monday 4pm` + +Add an event to keep track of both start and end times of an activity! + +### Mark Task + +Format: +`mark [index of the task - 1 based]` + +Example of usage: +`mark 1` + +Mark the task at index as done! + +### Unmark Task + +Format: +`unmark [index of the task - 1 based]` + +Example of usage: +`unmark 1` + +Unmark the task at index as not done yet! + +### Delete Task + +Format: +`delete [index of the task - 1 based]` + +Example of usage: +`delete 1` + +Delete the task at the index! + +### Find Tasks -### Feature-ABC +Format: +`find [keyword]` -Description of the feature. +Example of usage: +`find exam` -### Feature-XYZ +List the tasks that contains the keyword in the description! -Description of the feature. +### List Tasks -## Usage +Format: +`list` -### `Keyword` - Describe action +Example of usage: +`list` -Describe the action and its outcome. +List all the tasks along with completion status and task type! -Example of usage: +### Exit Program -`keyword (optional arguments)` +Format: +`bye` -Expected outcome: +Example of usage: +`bye` -Description of the outcome. +Greet Python 'bye' to exit the program! -``` -expected output -``` From 4d7bdfb48c7de259aa91d3bdf676093f4a4df590 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:25:57 +0800 Subject: [PATCH 58/59] Reorder and fix formatting --- src/main/java/python/Python.java | 3 +- src/main/java/python/ui/Message.java | 4 +-- src/main/java/python/ui/Ui.java | 48 ++++++++++++++-------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/main/java/python/Python.java b/src/main/java/python/Python.java index c7eaf8897..a1a476184 100644 --- a/src/main/java/python/Python.java +++ b/src/main/java/python/Python.java @@ -5,6 +5,7 @@ public class Python { static final private Ui ui = new Ui(); + public static void main(String[] args) { ui.welcomeUser(); do { @@ -15,6 +16,6 @@ public static void main(String[] args) { } catch (PythonException e) { ui.displayException(e); } - } while(!ui.isExit()); + } while (!ui.isExit()); } } diff --git a/src/main/java/python/ui/Message.java b/src/main/java/python/ui/Message.java index fd0849495..53bb71f9a 100644 --- a/src/main/java/python/ui/Message.java +++ b/src/main/java/python/ui/Message.java @@ -18,13 +18,13 @@ public class Message { "Command must have /by clause followed by time its due!"; final static String MESSAGE_FUTURE_JOKE = "Are you from the future?"; final static String MESSAGE_PAST_JOKE = "Are you from the past?"; + final static String MESSAGE_UNMARK_TASK_JOKE = "Alas! Only the completed tasks can be unmarked!"; + final static String MESSAGE_EMPTY_COMMAND_JOKE = "Nothing for me?"; final static String MESSAGE_TASK_ALREADY_DONE = "Task is already done!"; final static String MESSAGE_CONGRATUALTE = "Good job completing the task!"; - final static String MESSAGE_UNMARK_TASK_JOKE = "Alas! Only the completed tasks can be unmarked!"; final static String MESSAGE_TASK_IDLE = "Task is already sitting idle. Get started...!!!"; final static String MESSAGE_MISTAKE_CONSOLATION = "Its okay! To err is human! Unmarked!"; final static String MESSAGE_DELETE_CONFIRMATION = "Okay. Deleting this task...!"; - final static String MESSAGE_EMPTY_COMMAND_JOKE = "Nothing for me?"; final static String MESSAGE_UNKNOWN_COMMAND = "I cannot understand the command!"; final static String MESSAGE_NEW_TODO = "New Todo! You have added this todo:"; final static String MESSAGE_NEW_DEADLINE = "New Deadline! You have added this deadline:"; diff --git a/src/main/java/python/ui/Ui.java b/src/main/java/python/ui/Ui.java index 2fa39124a..41261af22 100644 --- a/src/main/java/python/ui/Ui.java +++ b/src/main/java/python/ui/Ui.java @@ -12,13 +12,13 @@ * Controls the user-chatbot interactions */ public class Ui { - private static final Scanner in = new Scanner(System.in); + private static final Scanner IN = new Scanner(System.in); final private static String PYTHON_EMOJI = "\uD83D\uDC0D"; final private static int HORIZONTAL_LINE_LENGTH = 80; private String inputLine; private String inputCommand; final private static String PYTHON_ASCII_ART = - "\t ____ _ _\n" + + "\t ____ _ _\n" + "\t| _ \\ _ _| |_| |__ ___ _ __\n" + "\t| |_) | | | | __| _ \\ / _ \\| _ \\\n" + "\t| __/| |_| | |_| | | | (_) | | | |\n" + @@ -60,26 +60,6 @@ private void displayTasks(List tasks) { } } - /** - * Gets the user given raw input - */ - public void getUserInput() { - this.inputLine = in.nextLine(); - } - - /** - * Displays the error messages - * - * @param e The error message - */ - public void displayException(Exception e) { - System.out.println("\tError: " + e.getMessage()); - } - - public void parseLine() { - this.inputCommand = Parser.extractCommandFromInputLine(inputLine); - } - private void handleByeCommand() { greetGoodBye(); } @@ -110,6 +90,7 @@ private void handleMarkCommand() throws PythonException { } TaskList.markTask(taskNo); + addEmojiAndPrint(Message.MESSAGE_CONGRATUALTE); addEmojiAndPrint(TaskList.getTask(taskNo).toString()); } @@ -158,6 +139,7 @@ private void handleDeleteCommand() throws PythonException { addEmojiAndPrint(Message.MESSAGE_DELETE_CONFIRMATION); addEmojiAndPrint(TaskList.getTask(taskNo).toString()); + TaskList.deleteTask(taskNo); displayTaskCount(); } @@ -169,11 +151,9 @@ private void handleTodoCommand() throws PythonException { } catch (ArrayIndexOutOfBoundsException e) { throw new PythonException(Message.MESSAGE_DESC_AFTER_COMMAND); } - addEmojiAndPrint(Message.MESSAGE_NEW_TODO); Todo todo = new Todo(todoDescription); - TaskList.addTask(todo); addEmojiAndPrint(todo.toString()); @@ -259,6 +239,26 @@ private void handleUnknownCommand() throws PythonException { addEmojiAndPrint(Message.MESSAGE_UNKNOWN_COMMAND); } + /** + * Gets the user given raw input + */ + public void getUserInput() { + this.inputLine = IN.nextLine(); + } + + /** + * Displays the error messages + * + * @param e The error message + */ + public void displayException(Exception e) { + System.out.println("\tError: " + e.getMessage()); + } + + public void parseLine() { + this.inputCommand = Parser.extractCommandFromInputLine(inputLine); + } + public void executeLine() throws PythonException { printHorizontalLine(); switch (inputCommand) { From 32183698fcc290b2e39a6c02f748325c0b95dc29 Mon Sep 17 00:00:00 2001 From: nihalzp <81457724+nihalzp@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:31:19 +0800 Subject: [PATCH 59/59] Do not print task count at start --- src/main/java/python/ui/Ui.java | 1 - text-ui-test/EXPECTED.TXT | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/python/ui/Ui.java b/src/main/java/python/ui/Ui.java index 41261af22..d74cf563e 100644 --- a/src/main/java/python/ui/Ui.java +++ b/src/main/java/python/ui/Ui.java @@ -41,7 +41,6 @@ public void welcomeUser() { System.out.println(PYTHON_ASCII_ART); printHorizontalLine(); addEmojiAndPrint(Message.MESSAGE_INTRODUCTION); - displayTaskCount(); addEmojiAndPrint(Message.MESSAGE_ASK); printHorizontalLine(); } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 7df3c0b5c..c034527aa 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -6,7 +6,6 @@ |___/ ———————————————————————————————————————————————————————————————————————————————— 🐍: Hello! I am a Java Bot Python! - 🐍: You have 0 tasks! 🐍: What can I do for you? ———————————————————————————————————————————————————————————————————————————————— ————————————————————————————————————————————————————————————————————————————————