From e1b96e264f551f9ef3547856bbd28250b8e42ae2 Mon Sep 17 00:00:00 2001 From: Tai Kah Kiang Date: Fri, 21 Jan 2022 16:06:22 +0800 Subject: [PATCH 01/34] add greeting --- src/main/java/Duke.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..144b574c2 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,5 +6,12 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + System.out.println("____________________________________________________________\n" + + "Hello! I'm Duke\n" + + "What can I do for you?\n" + + "____________________________________________________________ \n" + + "Bye. Hope to see you again soon!\n" + + "____________________________________________________________"); + } } From 9a8829044c4258c9a986846cc1e43f4ade4fec2d Mon Sep 17 00:00:00 2001 From: Tai Date: Thu, 27 Jan 2022 00:16:56 +0800 Subject: [PATCH 02/34] refactored level_0 --- src/main/java/Duke.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 144b574c2..8f7075e33 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,12 +6,14 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - System.out.println("____________________________________________________________\n" + - "Hello! I'm Duke\n" - + "What can I do for you?\n" + - "____________________________________________________________ \n" + - "Bye. Hope to see you again soon!\n" + - "____________________________________________________________"); + String breakLine = "____________________________________________________________"; + System.out.println(breakLine); + System.out.println( "Hello! I'm Duke"); + System.out.println( "What can I do for you?"); + System.out.println(breakLine); + System.out.println("Bye. Hope to see you again soon!"); + System.out.println(breakLine); + } } From f35f63daf2cf44011d2f5e7ece0aac955076e83a Mon Sep 17 00:00:00 2001 From: Tai Date: Thu, 27 Jan 2022 00:32:50 +0800 Subject: [PATCH 03/34] add greet echo exit, and abstracted out print with divider --- src/main/java/Duke.java | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 8f7075e33..be0c8363b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,13 @@ +import java.util.Scanner; public class Duke { + public static void printWithDivider(String stringWithinDivider) { + String breakLine = "\t____________________________________________________________"; + System.out.println(breakLine); + stringWithinDivider = stringWithinDivider.replace("\n", "\n\t"); + System.out.println("\t" + stringWithinDivider); + System.out.println(breakLine); + } + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -6,14 +15,19 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - String breakLine = "____________________________________________________________"; - System.out.println(breakLine); - System.out.println( "Hello! I'm Duke"); - System.out.println( "What can I do for you?"); - System.out.println(breakLine); - System.out.println("Bye. Hope to see you again soon!"); - System.out.println(breakLine); + printWithDivider("Hello! I'm Duke\nWhat can I do for you?"); + + //Level-1 + Scanner sc = new Scanner (System.in); + + String line = sc.nextLine(); + while (!line.equals("bye")){ + printWithDivider(line); + line = sc.nextLine(); + } + + printWithDivider("Bye. Hope to see you again soon!"); } } From ca00879d65eb6d39b555918601d6ca5e1429a831 Mon Sep 17 00:00:00 2001 From: Tai Date: Thu, 27 Jan 2022 00:58:50 +0800 Subject: [PATCH 04/34] add list ability using arraylist --- src/main/java/Duke.java | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index be0c8363b..72b0475e9 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,4 @@ +import java.util.ArrayList; import java.util.Scanner; public class Duke { public static void printWithDivider(String stringWithinDivider) { @@ -8,6 +9,20 @@ public static void printWithDivider(String stringWithinDivider) { System.out.println(breakLine); } + public static String numerateList(ArrayList list) { + String output = ""; + int number = 1 ; + for (String item : list) { + output += String.format("%d. %s", number, item); + if (number != list.size()) { + output += "\n"; + } + number ++; + } + return output; + } + + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -21,9 +36,20 @@ public static void main(String[] args) { //Level-1 Scanner sc = new Scanner (System.in); + ArrayList tasks = new ArrayList(); + int counter = 0; + String line = sc.nextLine(); + while (!line.equals("bye")){ - printWithDivider(line); + if (line.equals("list")) { + printWithDivider(numerateList(tasks)); + } + else { + printWithDivider("added: " + line); + tasks.add(line); + counter ++; + } line = sc.nextLine(); } From 212f360b9a948822a6e367538f09870fabaea5ca Mon Sep 17 00:00:00 2001 From: Tai Date: Thu, 27 Jan 2022 01:45:29 +0800 Subject: [PATCH 05/34] add toggle complete --- src/main/java/Duke.java | 57 +++++++++++++++++++++++++++++++-------- src/main/java/Task.class | Bin 0 -> 459 bytes src/main/java/Task.java | 26 ++++++++++++++++++ 3 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 src/main/java/Task.class create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 72b0475e9..72cb01fcb 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -9,11 +9,11 @@ public static void printWithDivider(String stringWithinDivider) { System.out.println(breakLine); } - public static String numerateList(ArrayList list) { + public static String numerateList(ArrayList list) { String output = ""; int number = 1 ; - for (String item : list) { - output += String.format("%d. %s", number, item); + for (Task item : list) { + output += String.format("%d. %s", number, item.toString()); if (number != list.size()) { output += "\n"; } @@ -22,8 +22,16 @@ public static String numerateList(ArrayList list) { return output; } + public static void markCompleted (Task task) { + task.setCompleted(true); + printWithDivider(task.toString()); + } + public static void unmarkCompleted (Task task) { + task.setCompleted(false); + printWithDivider(task.toString()); + } - public static void main(String[] args) { + public static void hello() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" @@ -32,24 +40,51 @@ public static void main(String[] args) { System.out.println("Hello from\n" + logo); printWithDivider("Hello! I'm Duke\nWhat can I do for you?"); + } + + public static void main(String[] args) { + + hello(); //Level-1 Scanner sc = new Scanner (System.in); - - ArrayList tasks = new ArrayList(); + ArrayList tasks = new ArrayList(); int counter = 0; String line = sc.nextLine(); + boolean sayByeAlready = false; - while (!line.equals("bye")){ - if (line.equals("list")) { - printWithDivider(numerateList(tasks)); + + while (!sayByeAlready){ + + int divider = line.indexOf(' '); + + String command = line; + String number = ""; + if (divider != -1) { + command = line.substring(0, divider); + number = line.substring(divider+1); } - else { + + switch(command) { + case "bye": + sayByeAlready = true; + break; + case "mark": + markCompleted(tasks.get(Integer.parseInt(number)-1)); + break; + case "unmark": + unmarkCompleted(tasks.get(Integer.parseInt(number)-1)); + break; + case "list" : + printWithDivider(numerateList(tasks)); + break; + default: printWithDivider("added: " + line); - tasks.add(line); + tasks.add(new Task(line)); counter ++; } + line = sc.nextLine(); } diff --git a/src/main/java/Task.class b/src/main/java/Task.class new file mode 100644 index 0000000000000000000000000000000000000000..39485d91747dd0ac22b6c1376e86f3d66d53d49a GIT binary patch literal 459 zcmZus%TB^j5IvW+ls*(he8+{Z3JY1f(6}LSMQ{Oe!LEfHa9f!%J-On?X zOs)hxHSeUe$5`faEFgvg0U> zo21F7oIjWO9|toSYE`$NF0zrlRa(gzg!6|By_^pho{utY9~C%s*CT=2YV*5jDn~ia zc%a3rOMHYE6&cJXC=ZX(=kJtRFk7v6h!++DTa4wRsYlzWGX96YqV&85)r1Oa8=%J1 h8q_6d2lZd*d9flLTPJTYRvWnNgYH)0zstIW#t#$9L_Poj literal 0 HcmV?d00001 diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..6f04b70e8 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,26 @@ +public class Task { + private String name; + private boolean isCompleted; + + public Task(String name) { + this.name = name; + this.isCompleted = false; + } + + public String getName() { + return this.name; + } + + public boolean isCompleted() { + return this.isCompleted; + } + + public void setCompleted(boolean completion) { + this.isCompleted = completion; + } + + public String toString() { + return String.format("[%s] %s", (this.isCompleted? "X" : " "), this.name); + } + +} From 47cce8f140de8138369674d7023fcdb318e5cccb Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 2 Feb 2022 18:29:46 +0800 Subject: [PATCH 06/34] add todo, deadline and event subclass (level 4) --- src/main/java/Deadline.java | 14 +++++++++ src/main/java/Duke.java | 58 +++++++++++++++++++++++++++++++------ src/main/java/Event.java | 14 +++++++++ src/main/java/ToDo.java | 10 +++++++ 4 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/ToDo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..8ede16a7e --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,14 @@ +public class Deadline extends Task{ + + private String deadline; + + public Deadline(String name, String deadline) { + super(name); + this.deadline= deadline; + } + + @Override + public String toString() { + return "[D]" + super.toString() + String.format("(%s)", this.deadline) ; + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 72cb01fcb..ce73a4b6e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,6 +1,10 @@ import java.util.ArrayList; import java.util.Scanner; public class Duke { + + private static ArrayList tasks = new ArrayList(); + private static int counter = 0; + public static void printWithDivider(String stringWithinDivider) { String breakLine = "\t____________________________________________________________"; System.out.println(breakLine); @@ -42,14 +46,46 @@ public static void hello() { printWithDivider("Hello! I'm Duke\nWhat can I do for you?"); } + public static void addTask(String type, String description) { + Task toBeAdded; + String taskName = description; + String addInfo = ""; + int divider = description.indexOf("/"); + if (divider != -1) { + taskName = description.substring(0,divider); + addInfo = description.substring(divider+1); + } + + switch (type) { + case "todo": + toBeAdded = new ToDo(taskName); + break; + case "deadline": + toBeAdded = new Deadline(taskName, addInfo); + break; + case "event": + toBeAdded = new Event(taskName, addInfo); + break; + default: + toBeAdded = new Task(taskName); + break; + + } + counter ++; + tasks.add(toBeAdded); + + printWithDivider("Got it. I've added this task:\n\t" + toBeAdded.toString() + + String.format("\nNow you have %d task%s in the list.", counter, counter>1 ? "s" : "")); + + } + public static void main(String[] args) { hello(); //Level-1 Scanner sc = new Scanner (System.in); - ArrayList tasks = new ArrayList(); - int counter = 0; + String line = sc.nextLine(); boolean sayByeAlready = false; @@ -60,10 +96,11 @@ public static void main(String[] args) { int divider = line.indexOf(' '); String command = line; - String number = ""; + String description = ""; + if (divider != -1) { command = line.substring(0, divider); - number = line.substring(divider+1); + description = line.substring(divider+1); } switch(command) { @@ -71,18 +108,21 @@ public static void main(String[] args) { sayByeAlready = true; break; case "mark": - markCompleted(tasks.get(Integer.parseInt(number)-1)); + markCompleted(tasks.get(Integer.parseInt(description)-1)); break; case "unmark": - unmarkCompleted(tasks.get(Integer.parseInt(number)-1)); + unmarkCompleted(tasks.get(Integer.parseInt(description)-1)); break; case "list" : printWithDivider(numerateList(tasks)); break; + case "todo": + case "deadline": + case "event": + addTask(command, description); + break; default: - printWithDivider("added: " + line); - tasks.add(new Task(line)); - counter ++; + addTask("task", line); } line = sc.nextLine(); diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..9824da517 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,14 @@ +public class Event extends Task{ + + private String time; + + public Event(String name, String time) { + super(name); + this.time= time; + } + + @Override + public String toString() { + return "[D]" + super.toString() + String.format("(%s)", this.time) ; + } +} diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java new file mode 100644 index 000000000..5652fa415 --- /dev/null +++ b/src/main/java/ToDo.java @@ -0,0 +1,10 @@ +public class ToDo extends Task{ + public ToDo(String name) { + super(name); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} From 85f8d739f8b9049f4602ad4cd5501566b6d5606d Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 2 Feb 2022 19:38:31 +0800 Subject: [PATCH 07/34] add automated testing --- text-ui-test/EXPECTED.TXT | 39 +++++++++++++++++++++++++++++++++++++++ text-ui-test/input.txt | 9 +++++++++ 2 files changed, 48 insertions(+) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..a6b9e710f 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -5,3 +5,42 @@ Hello from | |_| | |_| | < __/ |____/ \__,_|_|\_\___| + ____________________________________________________________ + Hello! I'm Duke + What can I do for you? + ____________________________________________________________ + ____________________________________________________________ + Got it. I've added this task: + [ ] read book + Now you have 1 task in the list. + ____________________________________________________________ + ____________________________________________________________ + [X] read book + ____________________________________________________________ + ____________________________________________________________ + [ ] read book + ____________________________________________________________ + ____________________________________________________________ + Got it. I've added this task: + [T][ ] go to gym + Now you have 2 tasks in the list. + ____________________________________________________________ + ____________________________________________________________ + Got it. I've added this task: + [D][ ] assignment 3 (by: Sunday) + Now you have 3 tasks in the list. + ____________________________________________________________ + ____________________________________________________________ + Got it. I've added this task: + [E][ ] spiderman's birthday (15th December) + Now you have 4 tasks in the list. + ____________________________________________________________ + ____________________________________________________________ + [D][X] assignment 3 (by: Sunday) + ____________________________________________________________ + ____________________________________________________________ + 1. [ ] read book + 2. [T][ ] go to gym + 3. [D][X] assignment 3 (by: Sunday) + 4. [E][ ] spiderman's birthday (15th December) + ____________________________________________________________ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..ef52c4f21 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,9 @@ +read book +mark 1 +unmark 1 +todo go to gym +deadline assignment 3 /by: Sunday +event spiderman's birthday /15th December +mark 3 +list +bye \ No newline at end of file From bebd84da358c0db128260d8d2546775965c89e54 Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 2 Feb 2022 20:29:21 +0800 Subject: [PATCH 08/34] abstract run function from main, clean up code, fix event toString typo --- src/main/java/Duke.java | 130 ++++++++++++++++++-------------------- src/main/java/Event.java | 2 +- text-ui-test/EXPECTED.TXT | 3 + 3 files changed, 67 insertions(+), 68 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ce73a4b6e..45e244879 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,9 +2,11 @@ import java.util.Scanner; public class Duke { + //fields private static ArrayList tasks = new ArrayList(); private static int counter = 0; + //utility methods public static void printWithDivider(String stringWithinDivider) { String breakLine = "\t____________________________________________________________"; System.out.println(breakLine); @@ -12,29 +14,20 @@ public static void printWithDivider(String stringWithinDivider) { System.out.println("\t" + stringWithinDivider); System.out.println(breakLine); } + public static ArrayList processInput(String line, String delimiter) { + ArrayList words = new ArrayList<>(); + int divider = line.indexOf(delimiter); - public static String numerateList(ArrayList list) { - String output = ""; - int number = 1 ; - for (Task item : list) { - output += String.format("%d. %s", number, item.toString()); - if (number != list.size()) { - output += "\n"; - } - number ++; - } - return output; - } + words.add(line); - public static void markCompleted (Task task) { - task.setCompleted(true); - printWithDivider(task.toString()); - } - public static void unmarkCompleted (Task task) { - task.setCompleted(false); - printWithDivider(task.toString()); + if (divider != -1) { + words.set(0, line.substring(0, divider)); + words.add(line.substring(divider+1)); + } + return words; } + //command methods public static void hello() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -45,16 +38,23 @@ public static void hello() { printWithDivider("Hello! I'm Duke\nWhat can I do for you?"); } - + public static String numerateList(ArrayList list) { + String output = ""; + int number = 1 ; + for (Task item : list) { + output += String.format("%d. %s", number, item.toString()); + if (number != list.size()) { + output += "\n"; + } + number ++; + } + return output; + } public static void addTask(String type, String description) { Task toBeAdded; - String taskName = description; - String addInfo = ""; - int divider = description.indexOf("/"); - if (divider != -1) { - taskName = description.substring(0,divider); - addInfo = description.substring(divider+1); - } + ArrayList words = processInput(description, "/"); + String taskName = words.get(0); + String addInfo = words.size() >= 2 ? words.get(1) : null; switch (type) { case "todo": @@ -78,56 +78,52 @@ public static void addTask(String type, String description) { + String.format("\nNow you have %d task%s in the list.", counter, counter>1 ? "s" : "")); } + public static void markCompleted (Task task) { + task.setCompleted(true); + printWithDivider(task.toString()); + } + public static void unmarkCompleted (Task task) { + task.setCompleted(false); + printWithDivider(task.toString()); + } + + //main + public static void run (String line) { + ArrayList words = processInput(line, " "); + String command = words.get(0); + String description = words.size()>= 2 ? words.get(1) : null; + + switch(command) { + case "mark": + markCompleted(tasks.get(Integer.parseInt(description)-1)); + break; + case "unmark": + unmarkCompleted(tasks.get(Integer.parseInt(description)-1)); + break; + case "list" : + printWithDivider(numerateList(tasks)); + break; + case "todo": + case "deadline": + case "event": + addTask(command, description); + break; + default: + addTask("task", line); + } + } public static void main(String[] args) { hello(); - //Level-1 Scanner sc = new Scanner (System.in); - - String line = sc.nextLine(); - boolean sayByeAlready = false; - - - while (!sayByeAlready){ - - int divider = line.indexOf(' '); - - String command = line; - String description = ""; - - if (divider != -1) { - command = line.substring(0, divider); - description = line.substring(divider+1); - } - - switch(command) { - case "bye": - sayByeAlready = true; - break; - case "mark": - markCompleted(tasks.get(Integer.parseInt(description)-1)); - break; - case "unmark": - unmarkCompleted(tasks.get(Integer.parseInt(description)-1)); - break; - case "list" : - printWithDivider(numerateList(tasks)); - break; - case "todo": - case "deadline": - case "event": - addTask(command, description); - break; - default: - addTask("task", line); - } + while (!line.equals("bye")){ + run(line); line = sc.nextLine(); } - printWithDivider("Bye. Hope to see you again soon!"); } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 9824da517..5b3d1e916 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -9,6 +9,6 @@ public Event(String name, String time) { @Override public String toString() { - return "[D]" + super.toString() + String.format("(%s)", this.time) ; + return "[E]" + super.toString() + String.format("(%s)", this.time) ; } } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index a6b9e710f..381ca976a 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -44,3 +44,6 @@ Hello from 3. [D][X] assignment 3 (by: Sunday) 4. [E][ ] spiderman's birthday (15th December) ____________________________________________________________ + ____________________________________________________________ + Bye. Hope to see you again soon! + ____________________________________________________________ From 66cb637ddbf1beae8d4f10840869963a03e3bdfc Mon Sep 17 00:00:00 2001 From: Tai Date: Tue, 8 Feb 2022 23:42:18 +0800 Subject: [PATCH 09/34] abstract out task related things to TaskManager class --- src/main/java/Duke.java | 90 ++++----------------------------- src/main/java/IOMethods.java | 25 +++++++++ src/main/java/Task.class | Bin 459 -> 0 bytes src/main/java/TaskManager.java | 75 +++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 80 deletions(-) create mode 100644 src/main/java/IOMethods.java delete mode 100644 src/main/java/Task.class create mode 100644 src/main/java/TaskManager.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 45e244879..cb4479f15 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,31 +3,9 @@ public class Duke { //fields - private static ArrayList tasks = new ArrayList(); - private static int counter = 0; + private static TaskManager taskManager = new TaskManager(); - //utility methods - public static void printWithDivider(String stringWithinDivider) { - String breakLine = "\t____________________________________________________________"; - System.out.println(breakLine); - stringWithinDivider = stringWithinDivider.replace("\n", "\n\t"); - System.out.println("\t" + stringWithinDivider); - System.out.println(breakLine); - } - public static ArrayList processInput(String line, String delimiter) { - ArrayList words = new ArrayList<>(); - int divider = line.indexOf(delimiter); - - words.add(line); - - if (divider != -1) { - words.set(0, line.substring(0, divider)); - words.add(line.substring(divider+1)); - } - return words; - } - - //command methods + //methods public static void hello() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -35,81 +13,33 @@ public static void hello() { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - - printWithDivider("Hello! I'm Duke\nWhat can I do for you?"); - } - public static String numerateList(ArrayList list) { - String output = ""; - int number = 1 ; - for (Task item : list) { - output += String.format("%d. %s", number, item.toString()); - if (number != list.size()) { - output += "\n"; - } - number ++; - } - return output; + IOMethods.printWithDivider("Hello! I'm Duke\nWhat can I do for you?"); } - public static void addTask(String type, String description) { - Task toBeAdded; - ArrayList words = processInput(description, "/"); - String taskName = words.get(0); - String addInfo = words.size() >= 2 ? words.get(1) : null; - - switch (type) { - case "todo": - toBeAdded = new ToDo(taskName); - break; - case "deadline": - toBeAdded = new Deadline(taskName, addInfo); - break; - case "event": - toBeAdded = new Event(taskName, addInfo); - break; - default: - toBeAdded = new Task(taskName); - break; - } - counter ++; - tasks.add(toBeAdded); - - printWithDivider("Got it. I've added this task:\n\t" + toBeAdded.toString() - + String.format("\nNow you have %d task%s in the list.", counter, counter>1 ? "s" : "")); - - } - public static void markCompleted (Task task) { - task.setCompleted(true); - printWithDivider(task.toString()); - } - public static void unmarkCompleted (Task task) { - task.setCompleted(false); - printWithDivider(task.toString()); - } //main public static void run (String line) { - ArrayList words = processInput(line, " "); + ArrayList words = IOMethods.splitToTwo(line, " "); String command = words.get(0); String description = words.size()>= 2 ? words.get(1) : null; switch(command) { case "mark": - markCompleted(tasks.get(Integer.parseInt(description)-1)); + taskManager.markCompleted(Integer.parseInt(description)); break; case "unmark": - unmarkCompleted(tasks.get(Integer.parseInt(description)-1)); + taskManager.unmarkCompleted(Integer.parseInt(description)); break; case "list" : - printWithDivider(numerateList(tasks)); + IOMethods.printWithDivider(taskManager.toString()); break; case "todo": case "deadline": case "event": - addTask(command, description); + taskManager.addTask(command, description); break; default: - addTask("task", line); + taskManager.addTask("task", line); } } @@ -124,7 +54,7 @@ public static void main(String[] args) { run(line); line = sc.nextLine(); } - printWithDivider("Bye. Hope to see you again soon!"); + IOMethods.printWithDivider("Bye. Hope to see you again soon!"); } } diff --git a/src/main/java/IOMethods.java b/src/main/java/IOMethods.java new file mode 100644 index 000000000..ac35450a4 --- /dev/null +++ b/src/main/java/IOMethods.java @@ -0,0 +1,25 @@ +import java.util.ArrayList; + +public class IOMethods { + public static ArrayList splitToTwo(String line, String delimiter) { + ArrayList words = new ArrayList<>(); + int divider = line.indexOf(delimiter); + + words.add(line); + + if (divider != -1) { + words.set(0, line.substring(0, divider)); + words.add(line.substring(divider+1)); + } + return words; + } + + public static void printWithDivider(String stringWithinDivider) { + String breakLine = "\t____________________________________________________________"; + System.out.println(breakLine); + stringWithinDivider = stringWithinDivider.replace("\n", "\n\t"); + System.out.println("\t" + stringWithinDivider); + System.out.println(breakLine); + } + +} diff --git a/src/main/java/Task.class b/src/main/java/Task.class deleted file mode 100644 index 39485d91747dd0ac22b6c1376e86f3d66d53d49a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 459 zcmZus%TB^j5IvW+ls*(he8+{Z3JY1f(6}LSMQ{Oe!LEfHa9f!%J-On?X zOs)hxHSeUe$5`faEFgvg0U> zo21F7oIjWO9|toSYE`$NF0zrlRa(gzg!6|By_^pho{utY9~C%s*CT=2YV*5jDn~ia zc%a3rOMHYE6&cJXC=ZX(=kJtRFk7v6h!++DTa4wRsYlzWGX96YqV&85)r1Oa8=%J1 h8q_6d2lZd*d9flLTPJTYRvWnNgYH)0zstIW#t#$9L_Poj diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java new file mode 100644 index 000000000..f58cb0490 --- /dev/null +++ b/src/main/java/TaskManager.java @@ -0,0 +1,75 @@ +import java.util.ArrayList; + +public class TaskManager { + private ArrayList tasks = new ArrayList<>(); + private int numOfTasks = 0; + + public TaskManager(){ + + } + + public int getNumOfTasks() { + return this.numOfTasks; + } + + public ArrayList getTasks() { + return this.tasks; + } + + public void addTask(String type, String description) { + Task toBeAdded; + ArrayList words = IOMethods.splitToTwo(description, "/"); + + String taskName = words.get(0); + String addInfo = words.size() >= 2 ? words.get(1) : null; + + switch (type) { + case "todo": + toBeAdded = new ToDo(taskName); + break; + case "deadline": + toBeAdded = new Deadline(taskName, addInfo); + break; + case "event": + toBeAdded = new Event(taskName, addInfo); + break; + default: + toBeAdded = new Task(taskName); + break; + + } + this.numOfTasks++; + this.tasks.add(toBeAdded); + + IOMethods.printWithDivider("Got it. I've added this task:\n\t" + toBeAdded.toString() + + String.format("\nNow you have %d task%s in the list.", this.numOfTasks, + this.numOfTasks > 1 ? "s" : "")); + + } + + public void markCompleted (int taskNumber) { + Task task = tasks.get(taskNumber -1); + task.setCompleted(true); + IOMethods.printWithDivider(task.toString()); + } + + public void unmarkCompleted (int taskNumber) { + Task task = tasks.get(taskNumber -1); + task.setCompleted(false); + IOMethods.printWithDivider(task.toString()); + } + + @Override + public String toString() { + String output = ""; + int number = 1 ; + for (Task item : this.tasks) { + output += String.format("%d. %s", number, item.toString()); + if (number != numOfTasks) { + output += "\n"; + } + number ++; + } + return output; + } +} From ff54efbe2e3d50da0d91df4cee7589e47b855054 Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 9 Feb 2022 16:31:45 +0800 Subject: [PATCH 10/34] add error handling --- src/main/java/Duke.java | 10 +++-- src/main/java/IOMethods.java | 68 ++++++++++++++++++++++++++++++++++ src/main/java/TaskManager.java | 22 ++++++++--- 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index cb4479f15..4ebdef83e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -18,11 +18,12 @@ public static void hello() { //main - public static void run (String line) { + public static void run(String line) { ArrayList words = IOMethods.splitToTwo(line, " "); String command = words.get(0); String description = words.size()>= 2 ? words.get(1) : null; + switch(command) { case "mark": taskManager.markCompleted(Integer.parseInt(description)); @@ -43,15 +44,18 @@ public static void run (String line) { } } + + public static void main(String[] args) { hello(); - Scanner sc = new Scanner (System.in); String line = sc.nextLine(); while (!line.equals("bye")){ - run(line); + if (IOMethods.errorHandler(line)) { + run(line); + } line = sc.nextLine(); } IOMethods.printWithDivider("Bye. Hope to see you again soon!"); diff --git a/src/main/java/IOMethods.java b/src/main/java/IOMethods.java index ac35450a4..ba8893f2f 100644 --- a/src/main/java/IOMethods.java +++ b/src/main/java/IOMethods.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.Arrays; public class IOMethods { public static ArrayList splitToTwo(String line, String delimiter) { @@ -22,4 +23,71 @@ public static void printWithDivider(String stringWithinDivider) { System.out.println(breakLine); } + public static boolean errorHandler(String input) { + String[] words = input.split(" "); + String command = words[0]; + + switch (command) { + case "todo": + case "event": + case "deadline": + String taskName = getNextWord(input, command); + if (taskName.equals("")) { + System.out.printf("%s requires a name\n", command); + return false; + } else { + if (command.equals("event") || command.equals("deadline")) { + int indexOfSlash = input.indexOf("/"); + String date = indexOfSlash == -1 ? "" : input.substring(indexOfSlash); + if (date.length() <= 1) { + System.out.printf("%s requires a valid date in the format taskName /date\n", command); + return false; + } + } + } + break; + + case "bye": + case "list": + if (!getNextWord(input, command).equals("")) { + System.out.println("Command not understood"); + return false; + } + break; + + case "mark": + case "unmark": + try { + Integer.parseInt(getNextWord(input, command)); + } catch (NumberFormatException e) { + System.out.println("Please enter a valid index for mark/unmark"); + return false; + } + break; + + default: + System.out.println("Command not understood"); + return false; + } + return true; + } + + public static String getNextWord(String line, String word) { + String nextWord = ""; + try { + int indexOfWord = line.indexOf(word); + String nextPart = line.substring(indexOfWord + word.length() + 1); + int indexOfSpace = nextPart.indexOf(" "); + if (indexOfSpace == -1) { + nextWord = nextPart; + } else { + nextWord = nextPart.substring(0, indexOfSpace); + } + } + + catch (IndexOutOfBoundsException e){ + + } + return nextWord; + } } diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index f58cb0490..6bf3063e9 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -48,15 +48,25 @@ public void addTask(String type, String description) { } public void markCompleted (int taskNumber) { - Task task = tasks.get(taskNumber -1); - task.setCompleted(true); - IOMethods.printWithDivider(task.toString()); + try { + Task task = tasks.get(taskNumber - 1); + task.setCompleted(true); + IOMethods.printWithDivider(task.toString()); + } + catch (IndexOutOfBoundsException e) { + System.out.println ("Index out of bounds!"); + } } public void unmarkCompleted (int taskNumber) { - Task task = tasks.get(taskNumber -1); - task.setCompleted(false); - IOMethods.printWithDivider(task.toString()); + try { + Task task = tasks.get(taskNumber - 1); + task.setCompleted(false); + IOMethods.printWithDivider(task.toString()); + } + catch (IndexOutOfBoundsException e) { + System.out.println ("Index out of bounds!"); + } } @Override From 1bfe6b68bf6d60dfea2bb1590daa371c21d1abfe Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 9 Feb 2022 18:31:06 +0800 Subject: [PATCH 11/34] add DukeException class --- src/main/java/Duke.java | 15 ++++++++++----- src/main/java/DukeException.java | 5 +++++ src/main/java/IOMethods.java | 24 ++++++++++++------------ src/main/java/TaskManager.java | 8 ++++---- 4 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4ebdef83e..da90382ec 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -18,7 +18,7 @@ public static void hello() { //main - public static void run(String line) { + public static void run(String line) throws DukeException { ArrayList words = IOMethods.splitToTwo(line, " "); String command = words.get(0); String description = words.size()>= 2 ? words.get(1) : null; @@ -44,8 +44,6 @@ public static void run(String line) { } } - - public static void main(String[] args) { hello(); @@ -53,10 +51,17 @@ public static void main(String[] args) { String line = sc.nextLine(); while (!line.equals("bye")){ - if (IOMethods.errorHandler(line)) { + try { + IOMethods.errorHandler(line); run(line); } - line = sc.nextLine(); + catch(DukeException e) { + System.out.println(e.toString()); + } + finally { + line = sc.nextLine(); + } + } IOMethods.printWithDivider("Bye. Hope to see you again soon!"); diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..adbe7ebd0 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,5 @@ +public class DukeException extends Exception{ + public DukeException(String message){ + super(message); + } +} diff --git a/src/main/java/IOMethods.java b/src/main/java/IOMethods.java index ba8893f2f..dd9fd1173 100644 --- a/src/main/java/IOMethods.java +++ b/src/main/java/IOMethods.java @@ -23,7 +23,7 @@ public static void printWithDivider(String stringWithinDivider) { System.out.println(breakLine); } - public static boolean errorHandler(String input) { + public static void errorHandler(String input) throws DukeException { String[] words = input.split(" "); String command = words[0]; @@ -33,15 +33,15 @@ public static boolean errorHandler(String input) { case "deadline": String taskName = getNextWord(input, command); if (taskName.equals("")) { - System.out.printf("%s requires a name\n", command); - return false; + String errorMsg = String.format("%s requires a name\n", command); + throw new DukeException(errorMsg); } else { if (command.equals("event") || command.equals("deadline")) { int indexOfSlash = input.indexOf("/"); String date = indexOfSlash == -1 ? "" : input.substring(indexOfSlash); if (date.length() <= 1) { - System.out.printf("%s requires a valid date in the format taskName /date\n", command); - return false; + String errorMsg = String.format("%s requires a valid date in the format taskName /date\n", command); + throw new DukeException(errorMsg); } } } @@ -50,8 +50,8 @@ public static boolean errorHandler(String input) { case "bye": case "list": if (!getNextWord(input, command).equals("")) { - System.out.println("Command not understood"); - return false; + String errorMsg = String.format("Command not understood"); + throw new DukeException(errorMsg); } break; @@ -60,16 +60,16 @@ public static boolean errorHandler(String input) { try { Integer.parseInt(getNextWord(input, command)); } catch (NumberFormatException e) { - System.out.println("Please enter a valid index for mark/unmark"); - return false; + String errorMsg = String.format("Please enter a valid index for mark/unmark"); + throw new DukeException(errorMsg); } break; default: - System.out.println("Command not understood"); - return false; + String errorMsg = String.format("Command not understood"); + throw new DukeException(errorMsg); } - return true; + return; } public static String getNextWord(String line, String word) { diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 6bf3063e9..981ed4e19 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -47,25 +47,25 @@ public void addTask(String type, String description) { } - public void markCompleted (int taskNumber) { + public void markCompleted (int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); task.setCompleted(true); IOMethods.printWithDivider(task.toString()); } catch (IndexOutOfBoundsException e) { - System.out.println ("Index out of bounds!"); + throw new DukeException("Index out of bounds!"); } } - public void unmarkCompleted (int taskNumber) { + public void unmarkCompleted (int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); task.setCompleted(false); IOMethods.printWithDivider(task.toString()); } catch (IndexOutOfBoundsException e) { - System.out.println ("Index out of bounds!"); + throw new DukeException("Index out of bounds!"); } } From 2e14be29fa175018bf4ba90e285d23e3f3654e7e Mon Sep 17 00:00:00 2001 From: Tai Date: Thu, 10 Feb 2022 00:08:20 +0800 Subject: [PATCH 12/34] add to packages --- src/main/java/Duke.java | 4 ++++ src/main/java/{ => duke/exception}/DukeException.java | 2 ++ src/main/java/{ => duke/iomethods}/IOMethods.java | 5 ++++- src/main/java/{ => duke/task}/Deadline.java | 6 +++++- src/main/java/{ => duke/task}/Event.java | 6 +++++- src/main/java/{ => duke/task}/Task.java | 2 ++ src/main/java/{ => duke/task}/TaskManager.java | 7 ++++++- src/main/java/{ => duke/task}/ToDo.java | 6 +++++- 8 files changed, 33 insertions(+), 5 deletions(-) rename src/main/java/{ => duke/exception}/DukeException.java (82%) rename src/main/java/{ => duke/iomethods}/IOMethods.java (98%) rename src/main/java/{ => duke/task}/Deadline.java (77%) rename src/main/java/{ => duke/task}/Event.java (76%) rename src/main/java/{ => duke/task}/Task.java (96%) rename src/main/java/{ => duke/task}/TaskManager.java (96%) rename src/main/java/{ => duke/task}/ToDo.java (66%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index da90382ec..f5e6f4421 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,7 @@ +import duke.exception.DukeException; +import duke.iomethods.IOMethods; +import duke.task.TaskManager; + import java.util.ArrayList; import java.util.Scanner; public class Duke { diff --git a/src/main/java/DukeException.java b/src/main/java/duke/exception/DukeException.java similarity index 82% rename from src/main/java/DukeException.java rename to src/main/java/duke/exception/DukeException.java index adbe7ebd0..45385f24a 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/duke/exception/DukeException.java @@ -1,3 +1,5 @@ +package duke.exception; + public class DukeException extends Exception{ public DukeException(String message){ super(message); diff --git a/src/main/java/IOMethods.java b/src/main/java/duke/iomethods/IOMethods.java similarity index 98% rename from src/main/java/IOMethods.java rename to src/main/java/duke/iomethods/IOMethods.java index dd9fd1173..23b1e1767 100644 --- a/src/main/java/IOMethods.java +++ b/src/main/java/duke/iomethods/IOMethods.java @@ -1,5 +1,8 @@ +package duke.iomethods; + +import duke.exception.DukeException; + import java.util.ArrayList; -import java.util.Arrays; public class IOMethods { public static ArrayList splitToTwo(String line, String delimiter) { diff --git a/src/main/java/Deadline.java b/src/main/java/duke/task/Deadline.java similarity index 77% rename from src/main/java/Deadline.java rename to src/main/java/duke/task/Deadline.java index 8ede16a7e..9da508207 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,4 +1,8 @@ -public class Deadline extends Task{ +package duke.task; + +import duke.task.Task; + +public class Deadline extends Task { private String deadline; diff --git a/src/main/java/Event.java b/src/main/java/duke/task/Event.java similarity index 76% rename from src/main/java/Event.java rename to src/main/java/duke/task/Event.java index 5b3d1e916..fe3bfaf95 100644 --- a/src/main/java/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,4 +1,8 @@ -public class Event extends Task{ +package duke.task; + +import duke.task.Task; + +public class Event extends Task { private String time; diff --git a/src/main/java/Task.java b/src/main/java/duke/task/Task.java similarity index 96% rename from src/main/java/Task.java rename to src/main/java/duke/task/Task.java index 6f04b70e8..2e469eea1 100644 --- a/src/main/java/Task.java +++ b/src/main/java/duke/task/Task.java @@ -1,3 +1,5 @@ +package duke.task; + public class Task { private String name; private boolean isCompleted; diff --git a/src/main/java/TaskManager.java b/src/main/java/duke/task/TaskManager.java similarity index 96% rename from src/main/java/TaskManager.java rename to src/main/java/duke/task/TaskManager.java index 981ed4e19..4acfb484a 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -1,3 +1,8 @@ +package duke.task; + +import duke.exception.DukeException; +import duke.iomethods.IOMethods; + import java.util.ArrayList; public class TaskManager { @@ -47,7 +52,7 @@ public void addTask(String type, String description) { } - public void markCompleted (int taskNumber) throws DukeException { + public void markCompleted (int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); task.setCompleted(true); diff --git a/src/main/java/ToDo.java b/src/main/java/duke/task/ToDo.java similarity index 66% rename from src/main/java/ToDo.java rename to src/main/java/duke/task/ToDo.java index 5652fa415..897e84865 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -1,4 +1,8 @@ -public class ToDo extends Task{ +package duke.task; + +import duke.task.Task; + +public class ToDo extends Task { public ToDo(String name) { super(name); } From 0a80df8944e65e7777530adcb5cbd82540057501 Mon Sep 17 00:00:00 2001 From: Tai Date: Sat, 12 Feb 2022 13:42:47 +0800 Subject: [PATCH 13/34] add delete task function --- src/main/java/Duke.java | 2 ++ src/main/java/duke/iomethods/IOMethods.java | 1 + src/main/java/duke/task/TaskManager.java | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f5e6f4421..811f6c74c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -43,6 +43,8 @@ public static void run(String line) throws DukeException { case "event": taskManager.addTask(command, description); break; + case "delete": + taskManager.deleteTask(Integer.parseInt(description)); default: taskManager.addTask("task", line); } diff --git a/src/main/java/duke/iomethods/IOMethods.java b/src/main/java/duke/iomethods/IOMethods.java index 23b1e1767..1cfa4e8b9 100644 --- a/src/main/java/duke/iomethods/IOMethods.java +++ b/src/main/java/duke/iomethods/IOMethods.java @@ -60,6 +60,7 @@ public static void errorHandler(String input) throws DukeException { case "mark": case "unmark": + case "delete": try { Integer.parseInt(getNextWord(input, command)); } catch (NumberFormatException e) { diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 4acfb484a..724c7ab78 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -52,6 +52,24 @@ public void addTask(String type, String description) { } + public void deleteTask(int taskNumber) throws DukeException{ + try { + Task task = tasks.get(taskNumber - 1); + tasks.remove(task); + this.numOfTasks--; + + IOMethods.printWithDivider("Noted. I've removed this task:\n\t " + task.toString() + + String.format("\nNow you have %d task%s in the list.", this.numOfTasks, + this.numOfTasks > 1 ? "s" : "")); + } + + catch (IndexOutOfBoundsException e) { + throw new DukeException("Index out of bounds!"); + } + + + } + public void markCompleted (int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); From 4732cce34d6baa87c10309d55a694a37acb37d7f Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 16 Feb 2022 04:14:03 +0800 Subject: [PATCH 14/34] add file writer and reader --- src/main/java/duke/fileHandler/FileHandler.java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/main/java/duke/fileHandler/FileHandler.java diff --git a/src/main/java/duke/fileHandler/FileHandler.java b/src/main/java/duke/fileHandler/FileHandler.java new file mode 100644 index 000000000..89a6a019f --- /dev/null +++ b/src/main/java/duke/fileHandler/FileHandler.java @@ -0,0 +1,2 @@ +package duke.FileHandler;public class FileHandler { +} From 676c6349ec51a6fdc9f247324936c32b710bc74c Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 16 Feb 2022 23:58:01 +0800 Subject: [PATCH 15/34] complete filereader --- src/main/java/Duke.java | 11 ++- .../java/duke/fileHandler/FileHandler.java | 69 ++++++++++++++++++- src/main/java/duke/task/TaskManager.java | 5 ++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f5e6f4421..18c568885 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,9 @@ import duke.exception.DukeException; import duke.iomethods.IOMethods; import duke.task.TaskManager; +import duke.fileHandler.FileHandler; +import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class Duke { @@ -48,12 +50,17 @@ public static void run(String line) throws DukeException { } } - public static void main(String[] args) { + public static void main(String[] args) throws IOException { + + FileHandler.setfilePath("./inputoroutputfile.txt"); + taskManager.setTasks(FileHandler.readFile()); hello(); Scanner sc = new Scanner (System.in); String line = sc.nextLine(); + + while (!line.equals("bye")){ try { IOMethods.errorHandler(line); @@ -67,6 +74,8 @@ public static void main(String[] args) { } } + + FileHandler.writeFile(taskManager.toString()); IOMethods.printWithDivider("Bye. Hope to see you again soon!"); } diff --git a/src/main/java/duke/fileHandler/FileHandler.java b/src/main/java/duke/fileHandler/FileHandler.java index 89a6a019f..828c509f5 100644 --- a/src/main/java/duke/fileHandler/FileHandler.java +++ b/src/main/java/duke/fileHandler/FileHandler.java @@ -1,2 +1,69 @@ -package duke.FileHandler;public class FileHandler { +package duke.fileHandler; + +import duke.task.Deadline; +import duke.task.Event; +import duke.task.Task; +import duke.task.ToDo; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class FileHandler { + private static String filePath; + + public static void setfilePath(String filePath) { + FileHandler.filePath= filePath; + } + + public static Task stringToTask(String input) { + int indexOfSpace = input.indexOf(" "); + String taskType = input.substring(indexOfSpace + 2, indexOfSpace+3); + String status = input.substring(indexOfSpace + 5, indexOfSpace + 6); + String nameAndDate = input.substring(indexOfSpace+ 7); + String name= "", date = ""; + if (!taskType.equals("T")) { + name = nameAndDate.substring(0, nameAndDate.indexOf("(")); + date = nameAndDate.substring(nameAndDate.indexOf("(") + 1, nameAndDate.indexOf(")")); + } + Task task = new Task(""); + switch (taskType) { + case "T": + task = new ToDo(nameAndDate); + break; + case "D": + task = new Deadline(name, date); + break; + case "E": + task = new Event(name, date); + break; + } + if (status.equals("X")) { + task.setCompleted(true); + } + return task; + } + + public static ArrayList readFile() throws IOException { + ArrayList tasks = new ArrayList<>(); + File input = new File(filePath); + if (input.createNewFile()) { + System.out.println("Create"); + } + Scanner s = new Scanner(input); + while (s.hasNext()) { + tasks.add(stringToTask(s.nextLine())); + } + return tasks; + } + + public static void writeFile(String textToAdd) throws IOException { + FileWriter output = new FileWriter(filePath); + + output.write(textToAdd); + output.close(); + } } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 4acfb484a..e23e07229 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -52,6 +52,11 @@ public void addTask(String type, String description) { } + public void setTasks(ArrayList tasks) { + this.tasks = tasks; + this.numOfTasks = tasks.size(); + } + public void markCompleted (int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); From 63ce6a096b2dace46b505d3485be266d8cf36b91 Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 2 Mar 2022 04:01:23 +0800 Subject: [PATCH 16/34] add more oop- parser, ui, command, storage --- IOfile.txt | 3 + src/main/java/Duke.java | 90 ++++------ src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/duke/command/AddCommand.java | 24 +++ src/main/java/duke/command/ByeCommand.java | 9 + src/main/java/duke/command/Command.java | 16 ++ src/main/java/duke/command/DeleteCommand.java | 19 +++ src/main/java/duke/command/ListCommand.java | 13 ++ src/main/java/duke/command/MarkCommand.java | 19 +++ src/main/java/duke/command/UnmarkCommand.java | 19 +++ .../java/duke/fileHandler/FileHandler.java | 69 -------- src/main/java/duke/iomethods/IOMethods.java | 97 ----------- src/main/java/duke/parser/Parser.java | 159 ++++++++++++++++++ src/main/java/duke/storage/Storage.java | 41 +++++ src/main/java/duke/task/TaskManager.java | 16 +- src/main/java/duke/ui/Ui.java | 41 +++++ 16 files changed, 402 insertions(+), 236 deletions(-) create mode 100644 IOfile.txt create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/duke/command/AddCommand.java create mode 100644 src/main/java/duke/command/ByeCommand.java create mode 100644 src/main/java/duke/command/Command.java create mode 100644 src/main/java/duke/command/DeleteCommand.java create mode 100644 src/main/java/duke/command/ListCommand.java create mode 100644 src/main/java/duke/command/MarkCommand.java create mode 100644 src/main/java/duke/command/UnmarkCommand.java delete mode 100644 src/main/java/duke/fileHandler/FileHandler.java delete mode 100644 src/main/java/duke/iomethods/IOMethods.java create mode 100644 src/main/java/duke/parser/Parser.java create mode 100644 src/main/java/duke/storage/Storage.java create mode 100644 src/main/java/duke/ui/Ui.java diff --git a/IOfile.txt b/IOfile.txt new file mode 100644 index 000000000..ac8e25b7a --- /dev/null +++ b/IOfile.txt @@ -0,0 +1,3 @@ +1. [T][ ] go toilet +2. [T][ ] oo +3. [T][ ] boom shakala \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 1ebafc046..ae7f0f2f7 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,10 @@ +import duke.command.Command; import duke.exception.DukeException; -import duke.iomethods.IOMethods; +import duke.parser.Parser; +import duke.ui.Ui; import duke.task.TaskManager; -import duke.fileHandler.FileHandler; +import duke.storage.Storage; +import duke.ui.Ui; import java.io.IOException; import java.util.ArrayList; @@ -9,76 +12,43 @@ public class Duke { //fields - private static TaskManager taskManager = new TaskManager(); + private TaskManager taskManager; + private Ui ui; + private Storage storage; //methods - public static void hello() { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - IOMethods.printWithDivider("Hello! I'm Duke\nWhat can I do for you?"); - } - - - //main - public static void run(String line) throws DukeException { - ArrayList words = IOMethods.splitToTwo(line, " "); - String command = words.get(0); - String description = words.size()>= 2 ? words.get(1) : null; - + public Duke(String filePath) { + ui = new Ui(); + storage = new Storage(filePath); + try { + taskManager = new TaskManager(); + taskManager.setTasks(storage.readFile()); + } + catch (IOException e) { - switch(command) { - case "mark": - taskManager.markCompleted(Integer.parseInt(description)); - break; - case "unmark": - taskManager.unmarkCompleted(Integer.parseInt(description)); - break; - case "list" : - IOMethods.printWithDivider(taskManager.toString()); - break; - case "todo": - case "deadline": - case "event": - taskManager.addTask(command, description); - break; - case "delete": - taskManager.deleteTask(Integer.parseInt(description)); - default: - taskManager.addTask("task", line); } } - public static void main(String[] args) throws IOException { - - FileHandler.setfilePath("./IOfile.txt"); - taskManager.setTasks(FileHandler.readFile()); - - hello(); - Scanner sc = new Scanner (System.in); - String line = sc.nextLine(); - - - - while (!line.equals("bye")){ + public void run() throws IOException { + ui.hello(); + boolean isExit = false; + while(!isExit) { try { - IOMethods.errorHandler(line); - run(line); + String fullCommand = ui.readCommand(); + Command c = Parser.parse(fullCommand); + c.execute(taskManager, ui, storage); + isExit = c.isExit(); } catch(DukeException e) { - System.out.println(e.toString()); - } - finally { - line = sc.nextLine(); + ui.showError(e); } - } + storage.writeFile(taskManager.toString()); + Ui.printWithDivider("Bye. Hope to see you again soon!"); - FileHandler.writeFile(taskManager.toString()); - IOMethods.printWithDivider("Bye. Hope to see you again soon!"); + } + public static void main(String[] args) throws IOException { + new Duke ("./IOfile.txt").run(); } } diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..d2ffd5b4d --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java new file mode 100644 index 000000000..e8d7e1dca --- /dev/null +++ b/src/main/java/duke/command/AddCommand.java @@ -0,0 +1,24 @@ +package duke.command; + +import duke.storage.Storage; +import duke.task.TaskManager; +import duke.ui.Ui; + +public class AddCommand extends Command{ + + private String commandType; + private String taskName; + private String addInfo; + + + public AddCommand(String commandType, String taskName, String addInfo) { + this.taskName = taskName; + this.commandType = commandType; + this.addInfo = addInfo; + } + + @Override + public void execute(TaskManager taskManager, Ui ui, Storage storage) { + taskManager.addTask(this.commandType, this.taskName, this.addInfo); + } +} diff --git a/src/main/java/duke/command/ByeCommand.java b/src/main/java/duke/command/ByeCommand.java new file mode 100644 index 000000000..a6993e289 --- /dev/null +++ b/src/main/java/duke/command/ByeCommand.java @@ -0,0 +1,9 @@ +package duke.command; + +public class ByeCommand extends Command{ + + @Override + public boolean isExit() { + return true; + } +} diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java new file mode 100644 index 000000000..fd3f7e6cb --- /dev/null +++ b/src/main/java/duke/command/Command.java @@ -0,0 +1,16 @@ +package duke.command; +import duke.exception.DukeException; +import duke.storage.Storage; +import duke.task.TaskManager; +import duke.ui.Ui; + +public abstract class Command { + + public void execute(TaskManager taskManager, Ui ui, Storage storage) throws DukeException { + + } + + public boolean isExit(){ + return false; + } +} diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java new file mode 100644 index 000000000..04698a12d --- /dev/null +++ b/src/main/java/duke/command/DeleteCommand.java @@ -0,0 +1,19 @@ +package duke.command; + +import duke.exception.DukeException; +import duke.storage.Storage; +import duke.task.TaskManager; +import duke.ui.Ui; + +public class DeleteCommand extends Command{ + + private int taskNumber; + public DeleteCommand(int taskNumber) { + this.taskNumber = taskNumber; + } + + @Override + public void execute(TaskManager taskManager, Ui ui, Storage storage) throws DukeException { + taskManager.deleteTask(this.taskNumber); + } +} diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java new file mode 100644 index 000000000..5e38c5298 --- /dev/null +++ b/src/main/java/duke/command/ListCommand.java @@ -0,0 +1,13 @@ +package duke.command; + +import duke.storage.Storage; +import duke.task.TaskManager; +import duke.ui.Ui; + +public class ListCommand extends Command{ + + @Override + public void execute(TaskManager taskManager, Ui ui, Storage storage) { + Ui.printWithDivider(taskManager.toString()); + } +} diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java new file mode 100644 index 000000000..9f6e4bffd --- /dev/null +++ b/src/main/java/duke/command/MarkCommand.java @@ -0,0 +1,19 @@ +package duke.command; +import duke.exception.DukeException; +import duke.storage.Storage; +import duke.task.TaskManager; +import duke.ui.Ui; + +public class MarkCommand extends Command { + + private int taskNumber; + + public MarkCommand(int taskNumber) { + this.taskNumber = taskNumber; + } + + @Override + public void execute(TaskManager taskManager, Ui ui, Storage storage) throws DukeException { + taskManager.markCompleted(this.taskNumber); + } +} \ No newline at end of file diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java new file mode 100644 index 000000000..ad5e7a4e8 --- /dev/null +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -0,0 +1,19 @@ +package duke.command; +import duke.exception.DukeException; +import duke.storage.Storage; +import duke.task.TaskManager; +import duke.ui.Ui; + +public class UnmarkCommand extends Command { + + private int taskNumber; + + public UnmarkCommand(int taskNumber) { + this.taskNumber = taskNumber; + } + + @Override + public void execute(TaskManager taskManager, Ui ui, Storage storage) throws DukeException { + taskManager.unmarkCompleted(this.taskNumber); + } +} \ No newline at end of file diff --git a/src/main/java/duke/fileHandler/FileHandler.java b/src/main/java/duke/fileHandler/FileHandler.java deleted file mode 100644 index 828c509f5..000000000 --- a/src/main/java/duke/fileHandler/FileHandler.java +++ /dev/null @@ -1,69 +0,0 @@ -package duke.fileHandler; - -import duke.task.Deadline; -import duke.task.Event; -import duke.task.Task; -import duke.task.ToDo; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Scanner; - -public class FileHandler { - private static String filePath; - - public static void setfilePath(String filePath) { - FileHandler.filePath= filePath; - } - - public static Task stringToTask(String input) { - int indexOfSpace = input.indexOf(" "); - String taskType = input.substring(indexOfSpace + 2, indexOfSpace+3); - String status = input.substring(indexOfSpace + 5, indexOfSpace + 6); - String nameAndDate = input.substring(indexOfSpace+ 7); - String name= "", date = ""; - if (!taskType.equals("T")) { - name = nameAndDate.substring(0, nameAndDate.indexOf("(")); - date = nameAndDate.substring(nameAndDate.indexOf("(") + 1, nameAndDate.indexOf(")")); - } - Task task = new Task(""); - switch (taskType) { - case "T": - task = new ToDo(nameAndDate); - break; - case "D": - task = new Deadline(name, date); - break; - case "E": - task = new Event(name, date); - break; - } - if (status.equals("X")) { - task.setCompleted(true); - } - return task; - } - - public static ArrayList readFile() throws IOException { - ArrayList tasks = new ArrayList<>(); - File input = new File(filePath); - if (input.createNewFile()) { - System.out.println("Create"); - } - Scanner s = new Scanner(input); - while (s.hasNext()) { - tasks.add(stringToTask(s.nextLine())); - } - return tasks; - } - - public static void writeFile(String textToAdd) throws IOException { - FileWriter output = new FileWriter(filePath); - - output.write(textToAdd); - output.close(); - } -} diff --git a/src/main/java/duke/iomethods/IOMethods.java b/src/main/java/duke/iomethods/IOMethods.java deleted file mode 100644 index 1cfa4e8b9..000000000 --- a/src/main/java/duke/iomethods/IOMethods.java +++ /dev/null @@ -1,97 +0,0 @@ -package duke.iomethods; - -import duke.exception.DukeException; - -import java.util.ArrayList; - -public class IOMethods { - public static ArrayList splitToTwo(String line, String delimiter) { - ArrayList words = new ArrayList<>(); - int divider = line.indexOf(delimiter); - - words.add(line); - - if (divider != -1) { - words.set(0, line.substring(0, divider)); - words.add(line.substring(divider+1)); - } - return words; - } - - public static void printWithDivider(String stringWithinDivider) { - String breakLine = "\t____________________________________________________________"; - System.out.println(breakLine); - stringWithinDivider = stringWithinDivider.replace("\n", "\n\t"); - System.out.println("\t" + stringWithinDivider); - System.out.println(breakLine); - } - - public static void errorHandler(String input) throws DukeException { - String[] words = input.split(" "); - String command = words[0]; - - switch (command) { - case "todo": - case "event": - case "deadline": - String taskName = getNextWord(input, command); - if (taskName.equals("")) { - String errorMsg = String.format("%s requires a name\n", command); - throw new DukeException(errorMsg); - } else { - if (command.equals("event") || command.equals("deadline")) { - int indexOfSlash = input.indexOf("/"); - String date = indexOfSlash == -1 ? "" : input.substring(indexOfSlash); - if (date.length() <= 1) { - String errorMsg = String.format("%s requires a valid date in the format taskName /date\n", command); - throw new DukeException(errorMsg); - } - } - } - break; - - case "bye": - case "list": - if (!getNextWord(input, command).equals("")) { - String errorMsg = String.format("Command not understood"); - throw new DukeException(errorMsg); - } - break; - - case "mark": - case "unmark": - case "delete": - try { - Integer.parseInt(getNextWord(input, command)); - } catch (NumberFormatException e) { - String errorMsg = String.format("Please enter a valid index for mark/unmark"); - throw new DukeException(errorMsg); - } - break; - - default: - String errorMsg = String.format("Command not understood"); - throw new DukeException(errorMsg); - } - return; - } - - public static String getNextWord(String line, String word) { - String nextWord = ""; - try { - int indexOfWord = line.indexOf(word); - String nextPart = line.substring(indexOfWord + word.length() + 1); - int indexOfSpace = nextPart.indexOf(" "); - if (indexOfSpace == -1) { - nextWord = nextPart; - } else { - nextWord = nextPart.substring(0, indexOfSpace); - } - } - - catch (IndexOutOfBoundsException e){ - - } - return nextWord; - } -} diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java new file mode 100644 index 000000000..1c050cf88 --- /dev/null +++ b/src/main/java/duke/parser/Parser.java @@ -0,0 +1,159 @@ +package duke.parser; + +import duke.command.*; +import duke.exception.DukeException; +import duke.task.Deadline; +import duke.task.Event; +import duke.task.Task; +import duke.task.ToDo; +import duke.ui.Ui; + +import java.util.ArrayList; + +public class Parser { + private static ArrayList splitToTwo(String line, String delimiter) { + ArrayList words = new ArrayList<>(); + int divider = line.indexOf(delimiter); + + words.add(line); + + if (divider != -1) { + words.set(0, line.substring(0, divider)); + words.add(line.substring(divider+1)); + } + return words; + } + + private static String getNextWord(String line, String word) { + String nextWord = ""; + try { + int indexOfWord = line.indexOf(word); + String nextPart = line.substring(indexOfWord + word.length() + 1); + int indexOfSpace = nextPart.indexOf(" "); + if (indexOfSpace == -1) { + nextWord = nextPart; + } else { + nextWord = nextPart.substring(0, indexOfSpace); + } + } + + catch (IndexOutOfBoundsException e){ + + } + return nextWord; + } + + private static void errorCheck(String input) throws DukeException { + String[] words = input.split(" "); + String command = words[0]; + + switch (command) { + case "todo": + case "event": + case "deadline": + String taskName = getNextWord(input, command); + if (taskName.equals("")) { + String errorMsg = String.format("%s requires a name\n", command); + throw new DukeException(errorMsg); + } else { + if (command.equals("event") || command.equals("deadline")) { + int indexOfSlash = input.indexOf("/"); + String date = indexOfSlash == -1 ? "" : input.substring(indexOfSlash); + if (date.length() <= 1) { + String errorMsg = String.format("%s requires a valid date in the format taskName /date\n", command); + throw new DukeException(errorMsg); + } + } + } + break; + + case "bye": + case "list": + if (!getNextWord(input, command).equals("")) { + String errorMsg = String.format("Command not understood"); + throw new DukeException(errorMsg); + } + break; + + case "mark": + case "unmark": + case "delete": + try { + Integer.parseInt(getNextWord(input, command)); + } catch (NumberFormatException e) { + String errorMsg = String.format("Please enter a valid index for mark/unmark"); + throw new DukeException(errorMsg); + } + break; + + default: + String errorMsg = String.format("Command not understood"); + throw new DukeException(errorMsg); + } + return; + } + + public static Command parse(String fullCommand) throws DukeException { + + try { + errorCheck(fullCommand); + ArrayList words = splitToTwo(fullCommand, " "); + String command = words.get(0); + String description = words.size() >= 2 ? words.get(1) : null; + + switch (command) { + case "mark": + return new MarkCommand(Integer.parseInt(description)); + case "unmark": + return new UnmarkCommand(Integer.parseInt(description)); + case "list": + return new ListCommand(); + + case "todo": + case "deadline": + case "event": + ArrayList description_split = Parser.splitToTwo(description, "/"); + String taskName = description_split.get(0); + String addInfo = words.size() >= 2 ? words.get(1) : null; + return new AddCommand(command, taskName, addInfo); + case "delete": + return new DeleteCommand(Integer.parseInt(description)); + case "bye": + return new ByeCommand(); + default: + return null; + } + } + catch(DukeException e) { + throw e; + } + } + + public static Task parseToTask(String input) { + int indexOfSpace = input.indexOf(" "); + String taskType = input.substring(indexOfSpace + 2, indexOfSpace+3); + String status = input.substring(indexOfSpace + 5, indexOfSpace + 6); + String nameAndDate = input.substring(indexOfSpace+ 7); + String name= "", date = ""; + if (!taskType.equals("T")) { + name = nameAndDate.substring(0, nameAndDate.indexOf("(")); + date = nameAndDate.substring(nameAndDate.indexOf("(") + 1, nameAndDate.indexOf(")")); + } + Task task = new Task(""); + switch (taskType) { + case "T": + task = new ToDo(nameAndDate); + break; + case "D": + task = new Deadline(name, date); + break; + case "E": + task = new Event(name, date); + break; + } + if (status.equals("X")) { + task.setCompleted(true); + } + return task; + } + } diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java new file mode 100644 index 000000000..b7fb93597 --- /dev/null +++ b/src/main/java/duke/storage/Storage.java @@ -0,0 +1,41 @@ +package duke.storage; + +import duke.parser.Parser; +import duke.task.Deadline; +import duke.task.Event; +import duke.task.Task; +import duke.task.ToDo; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + private String filePath; + + public Storage(String filePath) { + this.filePath = filePath; + } + + public ArrayList readFile() throws IOException { + ArrayList tasks = new ArrayList<>(); + File input = new File(filePath); + if (input.createNewFile()) { + System.out.println("Create"); + } + Scanner s = new Scanner(input); + while (s.hasNext()) { + tasks.add(Parser.parseToTask(s.nextLine())); + } + return tasks; + } + + public void writeFile(String textToAdd) throws IOException { + FileWriter output = new FileWriter(this.filePath); + + output.write(textToAdd); + output.close(); + } +} diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 26e858fcd..1903005aa 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -1,7 +1,7 @@ package duke.task; import duke.exception.DukeException; -import duke.iomethods.IOMethods; +import duke.ui.Ui; import java.util.ArrayList; @@ -20,12 +20,8 @@ public ArrayList getTasks() { return this.tasks; } - public void addTask(String type, String description) { + public void addTask(String type, String taskName, String addInfo) { Task toBeAdded; - ArrayList words = IOMethods.splitToTwo(description, "/"); - - String taskName = words.get(0); - String addInfo = words.size() >= 2 ? words.get(1) : null; switch (type) { case "todo": @@ -45,7 +41,7 @@ public void addTask(String type, String description) { this.numOfTasks++; this.tasks.add(toBeAdded); - IOMethods.printWithDivider("Got it. I've added this task:\n\t" + toBeAdded.toString() + Ui.printWithDivider("Got it. I've added this task:\n\t" + toBeAdded.toString() + String.format("\nNow you have %d task%s in the list.", this.numOfTasks, this.numOfTasks > 1 ? "s" : "")); @@ -57,7 +53,7 @@ public void deleteTask(int taskNumber) throws DukeException { tasks.remove(task); this.numOfTasks--; - IOMethods.printWithDivider("Noted. I've removed this task:\n\t " + task.toString() + Ui.printWithDivider("Noted. I've removed this task:\n\t " + task.toString() + String.format("\nNow you have %d task%s in the list.", this.numOfTasks, this.numOfTasks > 1 ? "s" : "")); } catch (IndexOutOfBoundsException e) { @@ -75,7 +71,7 @@ public void markCompleted (int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); task.setCompleted(true); - IOMethods.printWithDivider(task.toString()); + Ui.printWithDivider(task.toString()); } catch (IndexOutOfBoundsException e) { throw new DukeException("Index out of bounds!"); @@ -86,7 +82,7 @@ public void unmarkCompleted (int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); task.setCompleted(false); - IOMethods.printWithDivider(task.toString()); + Ui.printWithDivider(task.toString()); } catch (IndexOutOfBoundsException e) { throw new DukeException("Index out of bounds!"); diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java new file mode 100644 index 000000000..40d0282b6 --- /dev/null +++ b/src/main/java/duke/ui/Ui.java @@ -0,0 +1,41 @@ +package duke.ui; + +import duke.exception.DukeException; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Ui { + private Scanner sc; + + public Ui () { + this.sc = new Scanner(System.in); + } + + public static void printWithDivider(String stringWithinDivider) { + String breakLine = "\t____________________________________________________________"; + System.out.println(breakLine); + stringWithinDivider = stringWithinDivider.replace("\n", "\n\t"); + System.out.println("\t" + stringWithinDivider); + System.out.println(breakLine); + } + + public void hello() { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); + Ui.printWithDivider("Hello! I'm Duke\nWhat can I do for you?"); + } + + public String readCommand() { + String line = sc.nextLine(); + return line; + } + + public void showError(DukeException e) { + System.out.println(e.toString()); + } +} From f64e5e9bcc4aee241399aabdf4faaf554765619b Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 2 Mar 2022 14:11:09 +0800 Subject: [PATCH 17/34] change deadline of Deadline to by, time of Event to on --- src/main/java/duke/task/Deadline.java | 8 ++++---- src/main/java/duke/task/Event.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 9da508207..8517cf63c 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -4,15 +4,15 @@ public class Deadline extends Task { - private String deadline; + private String by; - public Deadline(String name, String deadline) { + public Deadline(String name, String by) { super(name); - this.deadline= deadline; + this.by= by; } @Override public String toString() { - return "[D]" + super.toString() + String.format("(%s)", this.deadline) ; + return "[D]" + super.toString() + String.format("(%s)", this.by) ; } } diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index fe3bfaf95..06be257c5 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -4,15 +4,15 @@ public class Event extends Task { - private String time; + private String on; - public Event(String name, String time) { + public Event(String name, String on) { super(name); - this.time= time; + this.on= on; } @Override public String toString() { - return "[E]" + super.toString() + String.format("(%s)", this.time) ; + return "[E]" + super.toString() + String.format("(%s)", this.on) ; } } From 6fdb78af71dc85e80de0cc7f5bbbe02b6a4d7779 Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 2 Mar 2022 15:00:48 +0800 Subject: [PATCH 18/34] add date/time --- src/main/java/duke/parser/Parser.java | 3 ++- src/main/java/duke/task/Deadline.java | 15 ++++++++++----- src/main/java/duke/task/Event.java | 2 +- src/main/java/duke/task/TaskManager.java | 1 + 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 1c050cf88..29d2a63ac 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -59,6 +59,7 @@ private static void errorCheck(String input) throws DukeException { if (command.equals("event") || command.equals("deadline")) { int indexOfSlash = input.indexOf("/"); String date = indexOfSlash == -1 ? "" : input.substring(indexOfSlash); + date = Parser.splitToTwo(date, " ").get; if (date.length() <= 1) { String errorMsg = String.format("%s requires a valid date in the format taskName /date\n", command); throw new DukeException(errorMsg); @@ -108,13 +109,13 @@ public static Command parse(String fullCommand) throws DukeException { return new UnmarkCommand(Integer.parseInt(description)); case "list": return new ListCommand(); - case "todo": case "deadline": case "event": ArrayList description_split = Parser.splitToTwo(description, "/"); String taskName = description_split.get(0); String addInfo = words.size() >= 2 ? words.get(1) : null; + addInfo = addInfo != null ? Parser.splitToTwo(addInfo, " ").get(1) : null; return new AddCommand(command, taskName, addInfo); case "delete": return new DeleteCommand(Integer.parseInt(description)); diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 8517cf63c..0af8c0089 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,18 +1,23 @@ package duke.task; - -import duke.task.Task; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; public class Deadline extends Task { - private String by; + private LocalDateTime by; + public Deadline(String name, String by) { super(name); - this.by= by; + this.by = LocalDateTime.parse(by); } + @Override public String toString() { - return "[D]" + super.toString() + String.format("(%s)", this.by) ; + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"); + String by = this.by.format(formatter); + return "[D]" + super.toString() + String.format("(by: %s)", by) ; } } diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 06be257c5..a9617b1a7 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -13,6 +13,6 @@ public Event(String name, String on) { @Override public String toString() { - return "[E]" + super.toString() + String.format("(%s)", this.on) ; + return "[E]" + super.toString() + String.format("(on: %s)", this.on) ; } } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 1903005aa..dcb7c89a7 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -47,6 +47,7 @@ public void addTask(String type, String taskName, String addInfo) { } + public void deleteTask(int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); From f304d13abc163665134ac1ade54e86ce6332b9c1 Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 2 Mar 2022 15:29:16 +0800 Subject: [PATCH 19/34] add find functionality --- src/main/java/duke/command/FindCommand.java | 28 +++++++++++++++++++++ src/main/java/duke/parser/Parser.java | 13 ++++++++-- src/main/java/duke/task/TaskManager.java | 13 +++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/main/java/duke/command/FindCommand.java diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java new file mode 100644 index 000000000..e81c9f5f6 --- /dev/null +++ b/src/main/java/duke/command/FindCommand.java @@ -0,0 +1,28 @@ +package duke.command; + +import duke.storage.Storage; +import duke.task.TaskManager; +import duke.ui.Ui; + +public class FindCommand extends Command{ + + private String keyword; + + + public FindCommand(String keyword) { + this.keyword = keyword; + } + + @Override + public void execute(TaskManager taskManager, Ui ui, Storage storage) { + TaskManager relatedTaskManager = taskManager.findTask(this.keyword); + if (relatedTaskManager.getNumOfTasks() == 0) { + Ui.printWithDivider("Sorry, task(s) containing the word " + this.keyword +" not found."); + } + else { + Ui.printWithDivider( "Here are the matching tasks in your list: \n"+ + relatedTaskManager); + } + + } +} diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 1c050cf88..b26f07d18 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -74,7 +74,13 @@ private static void errorCheck(String input) throws DukeException { throw new DukeException(errorMsg); } break; - + case "find": + String keyword = getNextWord(input, command); + if (keyword.equals("")) { + String errorMsg = String.format("%s requires a word\n", command); + throw new DukeException(errorMsg); + } + break; case "mark": case "unmark": case "delete": @@ -86,6 +92,7 @@ private static void errorCheck(String input) throws DukeException { } break; + default: String errorMsg = String.format("Command not understood"); throw new DukeException(errorMsg); @@ -114,10 +121,12 @@ public static Command parse(String fullCommand) throws DukeException { case "event": ArrayList description_split = Parser.splitToTwo(description, "/"); String taskName = description_split.get(0); - String addInfo = words.size() >= 2 ? words.get(1) : null; + String addInfo = description_split.size() >= 2 ? description_split.get(1) : null; return new AddCommand(command, taskName, addInfo); case "delete": return new DeleteCommand(Integer.parseInt(description)); + case "find": + return new FindCommand(description); case "bye": return new ByeCommand(); default: diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 1903005aa..2ad0ee8ed 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -61,7 +61,6 @@ public void deleteTask(int taskNumber) throws DukeException { } } - public void setTasks(ArrayList tasks) { this.tasks = tasks; this.numOfTasks = tasks.size(); @@ -89,6 +88,18 @@ public void unmarkCompleted (int taskNumber) throws DukeException { } } + public TaskManager findTask (String keyword) { + TaskManager relatedTaskManager = new TaskManager(); + ArrayList relatedTasks = new ArrayList<>(); + for (Task task : tasks) { + if (task.getName().contains(keyword)) { + relatedTasks.add(task); + } + } + relatedTaskManager.setTasks(relatedTasks); + return relatedTaskManager; + } + @Override public String toString() { String output = ""; From 318d04607821bc2ad6e4ec0f379bd02aac61ebf2 Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 2 Mar 2022 16:04:47 +0800 Subject: [PATCH 20/34] add date and time --- src/main/java/duke/parser/Parser.java | 8 ++--- src/main/java/duke/task/DatedTask.java | 38 ++++++++++++++++++++++++ src/main/java/duke/task/Deadline.java | 12 ++------ src/main/java/duke/task/Event.java | 9 ++---- src/main/java/duke/task/TaskManager.java | 2 +- 5 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 src/main/java/duke/task/DatedTask.java diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 29d2a63ac..d95ad6994 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -59,9 +59,9 @@ private static void errorCheck(String input) throws DukeException { if (command.equals("event") || command.equals("deadline")) { int indexOfSlash = input.indexOf("/"); String date = indexOfSlash == -1 ? "" : input.substring(indexOfSlash); - date = Parser.splitToTwo(date, " ").get; if (date.length() <= 1) { - String errorMsg = String.format("%s requires a valid date in the format taskName /date\n", command); + String errorMsg = String.format("%s requires a valid date in the format taskName /date" + + "date could be a string or in /dd-MM-yyyy hh:mm format\n", command); throw new DukeException(errorMsg); } } @@ -114,8 +114,8 @@ public static Command parse(String fullCommand) throws DukeException { case "event": ArrayList description_split = Parser.splitToTwo(description, "/"); String taskName = description_split.get(0); - String addInfo = words.size() >= 2 ? words.get(1) : null; - addInfo = addInfo != null ? Parser.splitToTwo(addInfo, " ").get(1) : null; + String addInfo = description_split.size() >= 2 ? description_split.get(1) : null; + return new AddCommand(command, taskName, addInfo); case "delete": return new DeleteCommand(Integer.parseInt(description)); diff --git a/src/main/java/duke/task/DatedTask.java b/src/main/java/duke/task/DatedTask.java new file mode 100644 index 000000000..648b17cf4 --- /dev/null +++ b/src/main/java/duke/task/DatedTask.java @@ -0,0 +1,38 @@ +package duke.task; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; + +public class DatedTask extends Task { + + private String DateTimeString; + private LocalDateTime RealDateTime; + + + public DatedTask(String name, String DateTimeString) { + super(name); + this.DateTimeString = DateTimeString; + try { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"); + this.RealDateTime = LocalDateTime.parse(DateTimeString, formatter); + } + catch (Exception e) { + } + + } + + public String getDateTimeString(){ + String DateTimeString = this.DateTimeString; + + if (this.RealDateTime != null) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm"); + DateTimeString = this.RealDateTime.format(formatter); + } + return DateTimeString; + } + + public LocalDate getDate() { + return this.RealDateTime.toLocalDate(); + } +} diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 0af8c0089..6637f8deb 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -3,21 +3,15 @@ import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; -public class Deadline extends Task { - - private LocalDateTime by; +public class Deadline extends DatedTask { public Deadline(String name, String by) { - super(name); - this.by = LocalDateTime.parse(by); + super(name, by); } - @Override public String toString() { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"); - String by = this.by.format(formatter); - return "[D]" + super.toString() + String.format("(by: %s)", by) ; + return "[D]" + super.toString() + String.format("(by: %s)", this.getDateTimeString()) ; } } diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index a9617b1a7..966663dd9 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,18 +1,15 @@ package duke.task; -import duke.task.Task; -public class Event extends Task { - private String on; +public class Event extends DatedTask { public Event(String name, String on) { - super(name); - this.on= on; + super(name, on); } @Override public String toString() { - return "[E]" + super.toString() + String.format("(on: %s)", this.on) ; + return "[E]" + super.toString() + String.format("(on: %s)", this.getDateTimeString()) ; } } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index dcb7c89a7..7e611bea5 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -36,8 +36,8 @@ public void addTask(String type, String taskName, String addInfo) { default: toBeAdded = new Task(taskName); break; - } + this.numOfTasks++; this.tasks.add(toBeAdded); From 921bb7fedcf5b03d0e8fdd76742bc63b4994ce2c Mon Sep 17 00:00:00 2001 From: Tai Date: Wed, 2 Mar 2022 16:34:36 +0800 Subject: [PATCH 21/34] add javadoc for taskmanager and storage --- src/main/java/Duke.java | 9 +++++ src/main/java/duke/storage/Storage.java | 10 +++++ src/main/java/duke/task/TaskManager.java | 47 ++++++++++++++++++++++-- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ae7f0f2f7..fb2884ce9 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -17,6 +17,11 @@ public class Duke { private Storage storage; //methods + + /** + * Creates a Duke Object + * @param filePath filePath of the input/output file + */ public Duke(String filePath) { ui = new Ui(); storage = new Storage(filePath); @@ -29,6 +34,10 @@ public Duke(String filePath) { } } + /** + * Runs the main function + * @throws IOException + */ public void run() throws IOException { ui.hello(); boolean isExit = false; diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index b7fb93597..3d28a8b2f 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -19,6 +19,11 @@ public Storage(String filePath) { this.filePath = filePath; } + /** + * Reads input and returns it as a task list + * @return TaskList from input file + * @throws IOException If no input file is found + */ public ArrayList readFile() throws IOException { ArrayList tasks = new ArrayList<>(); File input = new File(filePath); @@ -32,6 +37,11 @@ public ArrayList readFile() throws IOException { return tasks; } + /** + * Saves task list into a ile + * @param textToAdd text to be written + * @throws IOException if file not found + */ public void writeFile(String textToAdd) throws IOException { FileWriter output = new FileWriter(this.filePath); diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index d7772b0b2..5b186cc2a 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -12,14 +12,21 @@ public class TaskManager { public TaskManager(){ } + /** + * + * @return Number of tasks + */ public int getNumOfTasks() { return this.numOfTasks; } - public ArrayList getTasks() { - return this.tasks; - } - + /** + * Adds a task into the task list. + * + * @param type Type of command (todo, event or deadline) + * @param taskName Task name + * @param addInfo Any additional info (date or time) + */ public void addTask(String type, String taskName, String addInfo) { Task toBeAdded; @@ -48,6 +55,12 @@ public void addTask(String type, String taskName, String addInfo) { } + /** + * Deletes a task from the task list. + * + * @param taskNumber Task number of the task to be deleted (as seen from list) + * @throws DukeException Throws exception if task number invalid + */ public void deleteTask(int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); @@ -62,11 +75,22 @@ public void deleteTask(int taskNumber) throws DukeException { } } + /** + * Replace existing task list with a new task list. + * + * @param tasks Replacing task list. + */ public void setTasks(ArrayList tasks) { this.tasks = tasks; this.numOfTasks = tasks.size(); } + /** + * Mark task as complete. + * + * @param taskNumber Task number of the task to be deleted (as seen from list) + * @throws DukeException Throws exception if task number invalid + */ public void markCompleted (int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); @@ -78,6 +102,12 @@ public void markCompleted (int taskNumber) throws DukeException { } } + /** + * Mark task as incomplete. + * + * @param taskNumber Task number of the task to be deleted (as seen from list) + * @throws DukeException Throws exception if task number invalid + */ public void unmarkCompleted (int taskNumber) throws DukeException { try { Task task = tasks.get(taskNumber - 1); @@ -89,6 +119,11 @@ public void unmarkCompleted (int taskNumber) throws DukeException { } } + /** + * Find tasks containing keyword in its name + * @param keyword Keyword in task name + * @return TaskManager containing all the related tasks + */ public TaskManager findTask (String keyword) { TaskManager relatedTaskManager = new TaskManager(); ArrayList relatedTasks = new ArrayList<>(); @@ -101,6 +136,10 @@ public TaskManager findTask (String keyword) { return relatedTaskManager; } + /** + * Return the task list (number listed) + * @return Task list + */ @Override public String toString() { String output = ""; From 550f81211796d1412552f58e7f59db60dd076c69 Mon Sep 17 00:00:00 2001 From: kktai1512 <77670583+kktai1512@users.noreply.github.com> Date: Wed, 2 Mar 2022 17:03:03 +0800 Subject: [PATCH 22/34] Update README.md --- README.md | 83 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 8715d4d91..c714ceb1d 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,59 @@ -# Duke project template - -This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. - -## Setting up in Intellij - -Prerequisites: JDK 11, update Intellij to the most recent version. - -1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) -1. Open the project into Intellij as follows: - 1. Click `Open`. - 1. Select the project directory, and click `OK`. - 1. If there are any further prompts, accept the defaults. -1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
- In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: - ``` - Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| - ``` + +# Welcome to Duke! +Duke is a CLI-based todo application. +# Getting started +After running the JAR file, an output file named "Duke.txt" will be created in the same directory of the JAR. Outputs will be saved into the file before the program exits, and the file will be reread the next time Duke runs. + +# Commands +## Add +There are three types of tasks to be added. Namely, todo, deadline and event. +### Todo + + To add a todo, command should be typed in the format "*todo taskname*" + Example of a valid input: + + + todo get groceries + +### Event / Deadline + + To add an event or a deadline, command should be typed in the format "event taskname /dd-MM-yyyy HH:mm" or "event taskname /additional info" + + :white_check_mark: Example of a valid input: + + + event Mark's birthday /14-02-2021 07:00 + event Mark's birthday /three days from now + deadline CS2113T submission /03-02-21 00:00 + :x: Example of a invalid input: + + deadline hello / + event / + + ## Delete + To delete a task, type in "delete" followed by task number. + + + delete 2 + + ## List + To list tasks, simply type "list" + + + list + + ## Mark/Unmark + To mark the task as completed, type in "mark" followed by task number. + + + mark 3 + + ## Find + To find task(s) containing a specific keyword, type in "find" followed by the keyword. + + find birthday + + ## Bye +To exit the program, simply type "bye". The program terminates and the current state of the tasks is saved into "Duke.txt". + + bye From 04944b64db53f6a0b4834893302b1f8ecba3c73a Mon Sep 17 00:00:00 2001 From: Tai Date: Fri, 4 Mar 2022 07:30:08 +0800 Subject: [PATCH 23/34] add more javadocs --- src/main/java/duke/command/Command.java | 8 ++++++++ .../java/duke/exception/DukeException.java | 3 +++ src/main/java/duke/parser/Parser.java | 17 +++++++++++++++++ src/main/java/duke/storage/Storage.java | 5 +++++ src/main/java/duke/task/DatedTask.java | 7 ++++--- src/main/java/duke/task/Task.java | 16 ++++++++++++++++ src/main/java/duke/task/TaskManager.java | 7 +++++-- src/main/java/duke/ui/Ui.java | 18 ++++++++++++++++++ 8 files changed, 76 insertions(+), 5 deletions(-) diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java index fd3f7e6cb..526dc7073 100644 --- a/src/main/java/duke/command/Command.java +++ b/src/main/java/duke/command/Command.java @@ -6,6 +6,14 @@ public abstract class Command { + /** + * Executes the command + * + * @param taskManager TaskManager instance in charge of calling the correct method + * @param ui Ui instance in charge to print + * @param storage Storage instance for writing files + * @throws DukeException + */ public void execute(TaskManager taskManager, Ui ui, Storage storage) throws DukeException { } diff --git a/src/main/java/duke/exception/DukeException.java b/src/main/java/duke/exception/DukeException.java index 45385f24a..3fde37e19 100644 --- a/src/main/java/duke/exception/DukeException.java +++ b/src/main/java/duke/exception/DukeException.java @@ -1,5 +1,8 @@ package duke.exception; +/** + * Exception for anything related to Duke + */ public class DukeException extends Exception{ public DukeException(String message){ super(message); diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 3d2248fb0..b194a0b8f 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -43,6 +43,11 @@ private static String getNextWord(String line, String word) { return nextWord; } + /** + * Checks input command for any error + * @param input input command to be checked + * @throws DukeException if there is an error + */ private static void errorCheck(String input) throws DukeException { String[] words = input.split(" "); String command = words[0]; @@ -101,6 +106,13 @@ private static void errorCheck(String input) throws DukeException { return; } + /** + * Parses the input command and return the corresponding command. It first calls errorCheck(fullCommand) + * to check for error, if there is no error, it returns the correct command. + * @param fullCommand input command + * @return Command object corresponding to the input command + * @throws DukeException if there is an error + */ public static Command parse(String fullCommand) throws DukeException { try { @@ -138,6 +150,11 @@ public static Command parse(String fullCommand) throws DukeException { } } + /** + * Parses string into task. Used by Storage to parse input file into task list. + * @param input String to be parsed + * @return task object correspond to the String + */ public static Task parseToTask(String input) { int indexOfSpace = input.indexOf(" "); String taskType = input.substring(indexOfSpace + 2, indexOfSpace+3); diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index 3d28a8b2f..914969852 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -15,6 +15,11 @@ public class Storage { private String filePath; + /** + * Creates a storage to read and write file from filepath + * + * @param filePath path of input/output file + */ public Storage(String filePath) { this.filePath = filePath; } diff --git a/src/main/java/duke/task/DatedTask.java b/src/main/java/duke/task/DatedTask.java index 648b17cf4..d7584da5f 100644 --- a/src/main/java/duke/task/DatedTask.java +++ b/src/main/java/duke/task/DatedTask.java @@ -22,6 +22,10 @@ public DatedTask(String name, String DateTimeString) { } + /** + * Get date and time in string + * @return date and time + */ public String getDateTimeString(){ String DateTimeString = this.DateTimeString; @@ -32,7 +36,4 @@ public String getDateTimeString(){ return DateTimeString; } - public LocalDate getDate() { - return this.RealDateTime.toLocalDate(); - } } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 2e469eea1..14daa3973 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -4,11 +4,19 @@ public class Task { private String name; private boolean isCompleted; + /** + * Creates a task + * @param name name of task + */ public Task(String name) { this.name = name; this.isCompleted = false; } + /** + * Returns name of task + * @return task name + */ public String getName() { return this.name; } @@ -17,10 +25,18 @@ public boolean isCompleted() { return this.isCompleted; } + /** + * Mark task as complete/incomplete + * @param completion completion status of task, true as completed, false as not completed + */ public void setCompleted(boolean completion) { this.isCompleted = completion; } + /** + * Returns a string representation of the task + * @return task in the format "[x] task name" (x for completed, " " for incomplete) + */ public String toString() { return String.format("[%s] %s", (this.isCompleted? "X" : " "), this.name); } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 5b186cc2a..5dd821db2 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -9,6 +9,9 @@ public class TaskManager { private ArrayList tasks = new ArrayList<>(); private int numOfTasks = 0; + /** + * Class to manage the tasks + */ public TaskManager(){ } @@ -86,7 +89,7 @@ public void setTasks(ArrayList tasks) { } /** - * Mark task as complete. + * Marks task as complete. * * @param taskNumber Task number of the task to be deleted (as seen from list) * @throws DukeException Throws exception if task number invalid @@ -103,7 +106,7 @@ public void markCompleted (int taskNumber) throws DukeException { } /** - * Mark task as incomplete. + * Marks task as incomplete. * * @param taskNumber Task number of the task to be deleted (as seen from list) * @throws DukeException Throws exception if task number invalid diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 40d0282b6..50e828d7e 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -12,6 +12,11 @@ public Ui () { this.sc = new Scanner(System.in); } + /** + * Prints String wrapped inside two divider lines. + * + * @param stringWithinDivider String to be wrapped + */ public static void printWithDivider(String stringWithinDivider) { String breakLine = "\t____________________________________________________________"; System.out.println(breakLine); @@ -20,6 +25,9 @@ public static void printWithDivider(String stringWithinDivider) { System.out.println(breakLine); } + /** + * Greets the user + */ public void hello() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -30,11 +38,21 @@ public void hello() { Ui.printWithDivider("Hello! I'm Duke\nWhat can I do for you?"); } + /** + * Reads input typed in by user + * + * @return input command as string + */ public String readCommand() { String line = sc.nextLine(); return line; } + /** + * Prints the error message + * + * @param e error + */ public void showError(DukeException e) { System.out.println(e.toString()); } From b7d0ed663ae18b2598b07ba1f34abde13ec20cd7 Mon Sep 17 00:00:00 2001 From: Tai Date: Fri, 4 Mar 2022 08:40:11 +0800 Subject: [PATCH 24/34] change to autosave everytine tasks has changed --- src/main/java/Duke.java | 8 ++-- src/main/java/duke/command/AddCommand.java | 4 +- src/main/java/duke/command/DeleteCommand.java | 2 + src/main/java/duke/command/MarkCommand.java | 1 + src/main/java/duke/command/UnmarkCommand.java | 1 + src/main/java/duke/storage/Storage.java | 44 ++++++++++++------- 6 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index fb2884ce9..ad7103952 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -29,8 +29,8 @@ public Duke(String filePath) { taskManager = new TaskManager(); taskManager.setTasks(storage.readFile()); } - catch (IOException e) { - + catch (DukeException e) { + ui.showError(e); } } @@ -52,12 +52,12 @@ public void run() throws IOException { ui.showError(e); } } - storage.writeFile(taskManager.toString()); + Ui.printWithDivider("Bye. Hope to see you again soon!"); } public static void main(String[] args) throws IOException { - new Duke ("./IOfile.txt").run(); + new Duke ("./Duke.txt").run(); } } diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java index e8d7e1dca..658622b30 100644 --- a/src/main/java/duke/command/AddCommand.java +++ b/src/main/java/duke/command/AddCommand.java @@ -1,5 +1,6 @@ package duke.command; +import duke.exception.DukeException; import duke.storage.Storage; import duke.task.TaskManager; import duke.ui.Ui; @@ -18,7 +19,8 @@ public AddCommand(String commandType, String taskName, String addInfo) { } @Override - public void execute(TaskManager taskManager, Ui ui, Storage storage) { + public void execute(TaskManager taskManager, Ui ui, Storage storage) throws DukeException { taskManager.addTask(this.commandType, this.taskName, this.addInfo); + storage.writeFile(taskManager.toString()); } } diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java index 04698a12d..ecc0ae48d 100644 --- a/src/main/java/duke/command/DeleteCommand.java +++ b/src/main/java/duke/command/DeleteCommand.java @@ -15,5 +15,7 @@ public DeleteCommand(int taskNumber) { @Override public void execute(TaskManager taskManager, Ui ui, Storage storage) throws DukeException { taskManager.deleteTask(this.taskNumber); + storage.writeFile(taskManager.toString()); + } } diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java index 9f6e4bffd..791916bfd 100644 --- a/src/main/java/duke/command/MarkCommand.java +++ b/src/main/java/duke/command/MarkCommand.java @@ -15,5 +15,6 @@ public MarkCommand(int taskNumber) { @Override public void execute(TaskManager taskManager, Ui ui, Storage storage) throws DukeException { taskManager.markCompleted(this.taskNumber); + storage.writeFile(taskManager.toString()); } } \ No newline at end of file diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java index ad5e7a4e8..103645d21 100644 --- a/src/main/java/duke/command/UnmarkCommand.java +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -15,5 +15,6 @@ public UnmarkCommand(int taskNumber) { @Override public void execute(TaskManager taskManager, Ui ui, Storage storage) throws DukeException { taskManager.unmarkCompleted(this.taskNumber); + storage.writeFile(taskManager.toString()); } } \ No newline at end of file diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index 914969852..d92208b24 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -1,10 +1,8 @@ package duke.storage; +import duke.exception.DukeException; import duke.parser.Parser; -import duke.task.Deadline; -import duke.task.Event; import duke.task.Task; -import duke.task.ToDo; import java.io.File; import java.io.FileWriter; @@ -13,7 +11,7 @@ import java.util.Scanner; public class Storage { - private String filePath; + private final String filePath; /** * Creates a storage to read and write file from filepath @@ -27,30 +25,42 @@ public Storage(String filePath) { /** * Reads input and returns it as a task list * @return TaskList from input file - * @throws IOException If no input file is found + * @throws DukeException If no input file is found */ - public ArrayList readFile() throws IOException { + public ArrayList readFile() throws DukeException { ArrayList tasks = new ArrayList<>(); File input = new File(filePath); - if (input.createNewFile()) { - System.out.println("Create"); + try { + //unused boolean + boolean hasCreatedFile = input.createNewFile(); + Scanner s = new Scanner(input); + while (s.hasNext()) { + tasks.add(Parser.parseToTask(s.nextLine())); + } + return tasks; } - Scanner s = new Scanner(input); - while (s.hasNext()) { - tasks.add(Parser.parseToTask(s.nextLine())); + catch (IOException e) { + String message = "Issue at file creation!"; + throw new DukeException(message); } - return tasks; + } /** * Saves task list into a ile * @param textToAdd text to be written - * @throws IOException if file not found + * @throws DukeException if file not found */ - public void writeFile(String textToAdd) throws IOException { - FileWriter output = new FileWriter(this.filePath); + public void writeFile(String textToAdd) throws DukeException { + try { + FileWriter output = new FileWriter(this.filePath); + output.write(textToAdd); + output.close(); + } + catch (IOException e) { + String message = "File writing issue" + throw new DukeException(message); + } - output.write(textToAdd); - output.close(); } } From b1e1f7eae4732e8d9fbf43928608cec303bc8899 Mon Sep 17 00:00:00 2001 From: Tai Date: Fri, 4 Mar 2022 16:47:25 +0800 Subject: [PATCH 25/34] change such that task doesnt accept nameless / wrong name --- Duke.txt | 0 IOfile.txt | 3 --- src/main/java/Duke.java | 6 ++++-- src/main/java/duke/parser/Parser.java | 27 +++++++++++++------------ src/main/java/duke/storage/Storage.java | 4 ++-- src/main/java/duke/task/Task.java | 5 +---- 6 files changed, 21 insertions(+), 24 deletions(-) create mode 100644 Duke.txt delete mode 100644 IOfile.txt diff --git a/Duke.txt b/Duke.txt new file mode 100644 index 000000000..e69de29bb diff --git a/IOfile.txt b/IOfile.txt deleted file mode 100644 index ac8e25b7a..000000000 --- a/IOfile.txt +++ /dev/null @@ -1,3 +0,0 @@ -1. [T][ ] go toilet -2. [T][ ] oo -3. [T][ ] boom shakala \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ad7103952..2bba535cf 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -45,8 +45,10 @@ public void run() throws IOException { try { String fullCommand = ui.readCommand(); Command c = Parser.parse(fullCommand); - c.execute(taskManager, ui, storage); - isExit = c.isExit(); + if (c != null){ + c.execute(taskManager, ui, storage); + isExit = c.isExit(); + } } catch(DukeException e) { ui.showError(e); diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index b194a0b8f..649e56f50 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -57,18 +57,18 @@ private static void errorCheck(String input) throws DukeException { case "event": case "deadline": String taskName = getNextWord(input, command); - if (taskName.equals("")) { - String errorMsg = String.format("%s requires a name\n", command); + // taskname has to contain at least one alphabet and no signs (slash or etc) + if (!taskName.matches("[A-Za-z0-9]+$")) { + String errorMsg = String.format("%s requires a alphanumeric name\n", command); throw new DukeException(errorMsg); - } else { - if (command.equals("event") || command.equals("deadline")) { - int indexOfSlash = input.indexOf("/"); - String date = indexOfSlash == -1 ? "" : input.substring(indexOfSlash); - if (date.length() <= 1) { - String errorMsg = String.format("%s requires a valid date in the format taskName /date" + - "date could be a string or in /dd-MM-yyyy hh:mm format\n", command); - throw new DukeException(errorMsg); - } + } + if (command.equals("event") || command.equals("deadline")) { + int indexOfSlash = input.indexOf("/"); + String date = indexOfSlash == -1 ? "" : input.substring(indexOfSlash); + if (date.length() <= 1) { + String errorMsg = String.format("%s requires a valid date in the format taskName /date" + + "date could be a string or in /dd-MM-yyyy hh:mm format\n", command); + throw new DukeException(errorMsg); } } break; @@ -119,7 +119,7 @@ public static Command parse(String fullCommand) throws DukeException { errorCheck(fullCommand); ArrayList words = splitToTwo(fullCommand, " "); String command = words.get(0); - String description = words.size() >= 2 ? words.get(1) : null; + String description = words.size() >= 2 ? words.get(1) : ""; switch (command) { case "mark": @@ -129,11 +129,12 @@ public static Command parse(String fullCommand) throws DukeException { case "list": return new ListCommand(); case "todo": + return new AddCommand(command, description, ""); case "deadline": case "event": ArrayList description_split = Parser.splitToTwo(description, "/"); String taskName = description_split.get(0); - String addInfo = description_split.size() >= 2 ? description_split.get(1) : null; + String addInfo = description_split.size() >= 2 ? description_split.get(1) : ""; return new AddCommand(command, taskName, addInfo); case "delete": return new DeleteCommand(Integer.parseInt(description)); diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index d92208b24..144b9eec3 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -11,7 +11,7 @@ import java.util.Scanner; public class Storage { - private final String filePath; + private String filePath; /** * Creates a storage to read and write file from filepath @@ -58,7 +58,7 @@ public void writeFile(String textToAdd) throws DukeException { output.close(); } catch (IOException e) { - String message = "File writing issue" + String message = "File writing issue"; throw new DukeException(message); } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 14daa3973..4ef93a6f1 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -8,7 +8,7 @@ public class Task { * Creates a task * @param name name of task */ - public Task(String name) { + public Task(String name) { this.name = name; this.isCompleted = false; } @@ -21,9 +21,6 @@ public String getName() { return this.name; } - public boolean isCompleted() { - return this.isCompleted; - } /** * Mark task as complete/incomplete From 52e23126bc91c176e55309cd63470720ffa5912d Mon Sep 17 00:00:00 2001 From: Tai Date: Fri, 4 Mar 2022 20:02:21 +0800 Subject: [PATCH 26/34] check for style --- checkstyle.xml | 403 ++++++++++++++++++ src/main/java/Duke.java | 21 +- src/main/java/duke/command/AddCommand.java | 9 +- src/main/java/duke/command/ByeCommand.java | 2 +- src/main/java/duke/command/DeleteCommand.java | 2 +- src/main/java/duke/command/FindCommand.java | 6 +- src/main/java/duke/command/ListCommand.java | 2 +- .../java/duke/exception/DukeException.java | 4 +- src/main/java/duke/parser/Parser.java | 100 ++--- src/main/java/duke/storage/Storage.java | 6 +- src/main/java/duke/task/DatedTask.java | 26 +- src/main/java/duke/task/Deadline.java | 5 +- src/main/java/duke/task/Event.java | 4 +- src/main/java/duke/task/Task.java | 4 +- src/main/java/duke/task/TaskManager.java | 14 +- src/main/java/duke/task/ToDo.java | 2 - 16 files changed, 502 insertions(+), 108 deletions(-) create mode 100644 checkstyle.xml diff --git a/checkstyle.xml b/checkstyle.xml new file mode 100644 index 000000000..4e2eb84de --- /dev/null +++ b/checkstyle.xml @@ -0,0 +1,403 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 2bba535cf..979a9df61 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,14 +1,12 @@ +import java.io.IOException; + import duke.command.Command; import duke.exception.DukeException; import duke.parser.Parser; -import duke.ui.Ui; -import duke.task.TaskManager; import duke.storage.Storage; +import duke.task.TaskManager; import duke.ui.Ui; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Scanner; public class Duke { //fields @@ -28,29 +26,26 @@ public Duke(String filePath) { try { taskManager = new TaskManager(); taskManager.setTasks(storage.readFile()); - } - catch (DukeException e) { + } catch (DukeException e) { ui.showError(e); } } /** * Runs the main function - * @throws IOException */ - public void run() throws IOException { + public void run() { ui.hello(); boolean isExit = false; - while(!isExit) { + while (!isExit) { try { String fullCommand = ui.readCommand(); Command c = Parser.parse(fullCommand); - if (c != null){ + if (c != null) { c.execute(taskManager, ui, storage); isExit = c.isExit(); } - } - catch(DukeException e) { + } catch (DukeException e) { ui.showError(e); } } diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java index 658622b30..fda2f76ab 100644 --- a/src/main/java/duke/command/AddCommand.java +++ b/src/main/java/duke/command/AddCommand.java @@ -5,13 +5,18 @@ import duke.task.TaskManager; import duke.ui.Ui; -public class AddCommand extends Command{ +public class AddCommand extends Command { private String commandType; private String taskName; private String addInfo; - + /** + * Command for "add" operations (add todo, event or deadline) + * @param commandType todo, event or deadline + * @param taskName name of task + * @param addInfo additional information + */ public AddCommand(String commandType, String taskName, String addInfo) { this.taskName = taskName; this.commandType = commandType; diff --git a/src/main/java/duke/command/ByeCommand.java b/src/main/java/duke/command/ByeCommand.java index a6993e289..8f9ad69cc 100644 --- a/src/main/java/duke/command/ByeCommand.java +++ b/src/main/java/duke/command/ByeCommand.java @@ -1,6 +1,6 @@ package duke.command; -public class ByeCommand extends Command{ +public class ByeCommand extends Command { @Override public boolean isExit() { diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java index ecc0ae48d..3b49870d3 100644 --- a/src/main/java/duke/command/DeleteCommand.java +++ b/src/main/java/duke/command/DeleteCommand.java @@ -5,7 +5,7 @@ import duke.task.TaskManager; import duke.ui.Ui; -public class DeleteCommand extends Command{ +public class DeleteCommand extends Command { private int taskNumber; public DeleteCommand(int taskNumber) { diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java index e81c9f5f6..391dfc7b5 100644 --- a/src/main/java/duke/command/FindCommand.java +++ b/src/main/java/duke/command/FindCommand.java @@ -4,7 +4,7 @@ import duke.task.TaskManager; import duke.ui.Ui; -public class FindCommand extends Command{ +public class FindCommand extends Command { private String keyword; @@ -20,8 +20,8 @@ public void execute(TaskManager taskManager, Ui ui, Storage storage) { Ui.printWithDivider("Sorry, task(s) containing the word " + this.keyword +" not found."); } else { - Ui.printWithDivider( "Here are the matching tasks in your list: \n"+ - relatedTaskManager); + Ui.printWithDivider( "Here are the matching tasks in your list: \n" + + relatedTaskManager); } } diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java index 5e38c5298..9da655b5a 100644 --- a/src/main/java/duke/command/ListCommand.java +++ b/src/main/java/duke/command/ListCommand.java @@ -4,7 +4,7 @@ import duke.task.TaskManager; import duke.ui.Ui; -public class ListCommand extends Command{ +public class ListCommand extends Command { @Override public void execute(TaskManager taskManager, Ui ui, Storage storage) { diff --git a/src/main/java/duke/exception/DukeException.java b/src/main/java/duke/exception/DukeException.java index 3fde37e19..722724230 100644 --- a/src/main/java/duke/exception/DukeException.java +++ b/src/main/java/duke/exception/DukeException.java @@ -3,8 +3,8 @@ /** * Exception for anything related to Duke */ -public class DukeException extends Exception{ - public DukeException(String message){ +public class DukeException extends Exception { + public DukeException(String message) { super(message); } } diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 649e56f50..f3a09dda8 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -1,4 +1,5 @@ package duke.parser; +import java.util.ArrayList; import duke.command.*; import duke.exception.DukeException; @@ -6,9 +7,9 @@ import duke.task.Event; import duke.task.Task; import duke.task.ToDo; -import duke.ui.Ui; -import java.util.ArrayList; + + public class Parser { private static ArrayList splitToTwo(String line, String delimiter) { @@ -19,7 +20,7 @@ private static ArrayList splitToTwo(String line, String delimiter) { if (divider != -1) { words.set(0, line.substring(0, divider)); - words.add(line.substring(divider+1)); + words.add(line.substring(divider + 1)); } return words; } @@ -35,10 +36,8 @@ private static String getNextWord(String line, String word) { } else { nextWord = nextPart.substring(0, indexOfSpace); } - } - - catch (IndexOutOfBoundsException e){ - + } catch (IndexOutOfBoundsException e) { + nextWord = ""; } return nextWord; } @@ -66,8 +65,8 @@ private static void errorCheck(String input) throws DukeException { int indexOfSlash = input.indexOf("/"); String date = indexOfSlash == -1 ? "" : input.substring(indexOfSlash); if (date.length() <= 1) { - String errorMsg = String.format("%s requires a valid date in the format taskName /date" + - "date could be a string or in /dd-MM-yyyy hh:mm format\n", command); + String errorMsg = String.format("%s requires a valid date in the format taskName /date" + + "date could be a string or in /dd-MM-yyyy hh:mm format\n", command); throw new DukeException(errorMsg); } } @@ -76,7 +75,7 @@ private static void errorCheck(String input) throws DukeException { case "bye": case "list": if (!getNextWord(input, command).equals("")) { - String errorMsg = String.format("Command not understood"); + String errorMsg = "Command not understood"; throw new DukeException(errorMsg); } break; @@ -93,63 +92,55 @@ private static void errorCheck(String input) throws DukeException { try { Integer.parseInt(getNextWord(input, command)); } catch (NumberFormatException e) { - String errorMsg = String.format("Please enter a valid index for mark/unmark"); + String errorMsg = "Please enter a valid index for mark/unmark"; throw new DukeException(errorMsg); } break; - - default: - String errorMsg = String.format("Command not understood"); + String errorMsg = "Command not understood"; throw new DukeException(errorMsg); } - return; } /** - * Parses the input command and return the corresponding command. It first calls errorCheck(fullCommand) - * to check for error, if there is no error, it returns the correct command. + * Parses the input command and return the corresponding command. * @param fullCommand input command * @return Command object corresponding to the input command * @throws DukeException if there is an error */ - public static Command parse(String fullCommand) throws DukeException { - try { - errorCheck(fullCommand); - ArrayList words = splitToTwo(fullCommand, " "); - String command = words.get(0); - String description = words.size() >= 2 ? words.get(1) : ""; + public static Command parse(String fullCommand) throws DukeException { + //checks for error before continuing, if there's error, an exception is thrown + errorCheck(fullCommand); + ArrayList words = splitToTwo(fullCommand, " "); + String command = words.get(0); + String description = words.size() >= 2 ? words.get(1) : ""; - switch (command) { - case "mark": - return new MarkCommand(Integer.parseInt(description)); - case "unmark": - return new UnmarkCommand(Integer.parseInt(description)); - case "list": - return new ListCommand(); - case "todo": - return new AddCommand(command, description, ""); - case "deadline": - case "event": - ArrayList description_split = Parser.splitToTwo(description, "/"); - String taskName = description_split.get(0); - String addInfo = description_split.size() >= 2 ? description_split.get(1) : ""; - return new AddCommand(command, taskName, addInfo); - case "delete": - return new DeleteCommand(Integer.parseInt(description)); - case "find": - return new FindCommand(description); - case "bye": - return new ByeCommand(); - default: - return null; - } - } - catch(DukeException e) { - throw e; - } + switch (command) { + case "mark": + return new MarkCommand(Integer.parseInt(description)); + case "unmark": + return new UnmarkCommand(Integer.parseInt(description)); + case "list": + return new ListCommand(); + case "todo": + return new AddCommand(command, description, ""); + case "deadline": + case "event": + ArrayList descriptionSplit = Parser.splitToTwo(description, "/"); + String taskName = descriptionSplit.get(0); + String addInfo = descriptionSplit.size() >= 2 ? descriptionSplit.get(1) : ""; + return new AddCommand(command, taskName, addInfo); + case "delete": + return new DeleteCommand(Integer.parseInt(description)); + case "find": + return new FindCommand(description); + case "bye": + return new ByeCommand(); + default: //fine to return null, as error check makes sure the commands are valid. + return null; } + } /** * Parses string into task. Used by Storage to parse input file into task list. @@ -160,8 +151,9 @@ public static Task parseToTask(String input) { int indexOfSpace = input.indexOf(" "); String taskType = input.substring(indexOfSpace + 2, indexOfSpace+3); String status = input.substring(indexOfSpace + 5, indexOfSpace + 6); - String nameAndDate = input.substring(indexOfSpace+ 7); - String name= "", date = ""; + String nameAndDate = input.substring(indexOfSpace + 7); + String name = ""; + String date = ""; if (!taskType.equals("T")) { name = nameAndDate.substring(0, nameAndDate.indexOf("(")); date = nameAndDate.substring(nameAndDate.indexOf("(") + 1, nameAndDate.indexOf(")")); @@ -177,6 +169,8 @@ public static Task parseToTask(String input) { case "E": task = new Event(name, date); break; + default: + break; } if (status.equals("X")) { task.setCompleted(true); diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index 144b9eec3..88ce6cba0 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -38,8 +38,7 @@ public ArrayList readFile() throws DukeException { tasks.add(Parser.parseToTask(s.nextLine())); } return tasks; - } - catch (IOException e) { + } catch (IOException e) { String message = "Issue at file creation!"; throw new DukeException(message); } @@ -56,8 +55,7 @@ public void writeFile(String textToAdd) throws DukeException { FileWriter output = new FileWriter(this.filePath); output.write(textToAdd); output.close(); - } - catch (IOException e) { + } catch (IOException e) { String message = "File writing issue"; throw new DukeException(message); } diff --git a/src/main/java/duke/task/DatedTask.java b/src/main/java/duke/task/DatedTask.java index d7584da5f..835f171bd 100644 --- a/src/main/java/duke/task/DatedTask.java +++ b/src/main/java/duke/task/DatedTask.java @@ -1,23 +1,27 @@ package duke.task; -import java.time.LocalDate; + import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; + public class DatedTask extends Task { private String DateTimeString; private LocalDateTime RealDateTime; - - public DatedTask(String name, String DateTimeString) { - super(name); + /** + * Task that has date/time attribute + * @param taskName name of task + * @param DateTimeString optional date/time in string format "dd-MM-yyyy HH:mm, "" if none + */ + public DatedTask(String taskName, String DateTimeString) { + super(taskName); this.DateTimeString = DateTimeString; try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"); this.RealDateTime = LocalDateTime.parse(DateTimeString, formatter); - } - catch (Exception e) { + } catch (Exception e) { + this.RealDateTime = null; } } @@ -26,14 +30,14 @@ public DatedTask(String name, String DateTimeString) { * Get date and time in string * @return date and time */ - public String getDateTimeString(){ - String DateTimeString = this.DateTimeString; + public String getDateTimeString() { + String dateTimeString = this.DateTimeString; if (this.RealDateTime != null) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm"); - DateTimeString = this.RealDateTime.format(formatter); + dateTimeString = this.RealDateTime.format(formatter); } - return DateTimeString; + return dateTimeString; } } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 6637f8deb..eac94b756 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,7 +1,4 @@ package duke.task; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; public class Deadline extends DatedTask { @@ -12,6 +9,6 @@ public Deadline(String name, String by) { @Override public String toString() { - return "[D]" + super.toString() + String.format("(by: %s)", this.getDateTimeString()) ; + return "[D]" + super.toString() + String.format("(by: %s)", this.getDateTimeString()); } } diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 966663dd9..cad879fb9 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,7 +1,5 @@ package duke.task; - - public class Event extends DatedTask { public Event(String name, String on) { @@ -10,6 +8,6 @@ public Event(String name, String on) { @Override public String toString() { - return "[E]" + super.toString() + String.format("(on: %s)", this.getDateTimeString()) ; + return "[E]" + super.toString() + String.format("(on: %s)", this.getDateTimeString()); } } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 4ef93a6f1..f1a85b7f9 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -8,7 +8,7 @@ public class Task { * Creates a task * @param name name of task */ - public Task(String name) { + public Task(String name) { this.name = name; this.isCompleted = false; } @@ -35,7 +35,7 @@ public void setCompleted(boolean completion) { * @return task in the format "[x] task name" (x for completed, " " for incomplete) */ public String toString() { - return String.format("[%s] %s", (this.isCompleted? "X" : " "), this.name); + return String.format("[%s] %s", (this.isCompleted ? "X" : " "), this.name); } } diff --git a/src/main/java/duke/task/TaskManager.java b/src/main/java/duke/task/TaskManager.java index 5dd821db2..838c1d019 100644 --- a/src/main/java/duke/task/TaskManager.java +++ b/src/main/java/duke/task/TaskManager.java @@ -1,9 +1,11 @@ package duke.task; +import java.util.ArrayList; + import duke.exception.DukeException; import duke.ui.Ui; -import java.util.ArrayList; + public class TaskManager { private ArrayList tasks = new ArrayList<>(); @@ -16,7 +18,7 @@ public TaskManager(){ } /** - * + * Returns number of tasks * @return Number of tasks */ public int getNumOfTasks() { @@ -59,7 +61,7 @@ public void addTask(String type, String taskName, String addInfo) { /** - * Deletes a task from the task list. + * * Deletes a task from the task list. * * @param taskNumber Task number of the task to be deleted (as seen from list) * @throws DukeException Throws exception if task number invalid @@ -79,7 +81,7 @@ public void deleteTask(int taskNumber) throws DukeException { } /** - * Replace existing task list with a new task list. + * Replaces existing task list with a new task list. * * @param tasks Replacing task list. */ @@ -146,13 +148,13 @@ public TaskManager findTask (String keyword) { @Override public String toString() { String output = ""; - int number = 1 ; + int number = 1; for (Task item : this.tasks) { output += String.format("%d. %s", number, item.toString()); if (number != numOfTasks) { output += "\n"; } - number ++; + number++; } return output; } diff --git a/src/main/java/duke/task/ToDo.java b/src/main/java/duke/task/ToDo.java index 897e84865..9924e4749 100644 --- a/src/main/java/duke/task/ToDo.java +++ b/src/main/java/duke/task/ToDo.java @@ -1,7 +1,5 @@ package duke.task; -import duke.task.Task; - public class ToDo extends Task { public ToDo(String name) { super(name); From 05026dc3a0ff3acece1515aaf9be1392f029d17f Mon Sep 17 00:00:00 2001 From: Tai Date: Fri, 4 Mar 2022 20:03:35 +0800 Subject: [PATCH 27/34] check for style --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index f69985ef1..d186d26b0 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ bin/ /text-ui-test/ACTUAL.txt text-ui-test/EXPECTED-UNIX.TXT + +checkstyle.xml \ No newline at end of file From d4a94ac3c20800f6dc77b020f7f31776af79e77a Mon Sep 17 00:00:00 2001 From: Tai Date: Fri, 4 Mar 2022 20:07:49 +0800 Subject: [PATCH 28/34] remove checkstyle --- Duke.txt | 0 checkstyle.xml | 403 ------------------------------------------------- 2 files changed, 403 deletions(-) delete mode 100644 Duke.txt delete mode 100644 checkstyle.xml diff --git a/Duke.txt b/Duke.txt deleted file mode 100644 index e69de29bb..000000000 diff --git a/checkstyle.xml b/checkstyle.xml deleted file mode 100644 index 4e2eb84de..000000000 --- a/checkstyle.xml +++ /dev/null @@ -1,403 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From d6bef1e05079a946b295a27d7b531831a67a217a Mon Sep 17 00:00:00 2001 From: Tai Date: Fri, 4 Mar 2022 20:30:26 +0800 Subject: [PATCH 29/34] allow non alphanumeric name, disallow / in taskName --- Duke.txt | 2 ++ src/main/java/duke/parser/Parser.java | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 Duke.txt diff --git a/Duke.txt b/Duke.txt new file mode 100644 index 000000000..adf781cdd --- /dev/null +++ b/Duke.txt @@ -0,0 +1,2 @@ +1. [T][ ] ` +2. [D][ ] ` (by: 99) \ No newline at end of file diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index f3a09dda8..8286c6b4f 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -56,9 +56,9 @@ private static void errorCheck(String input) throws DukeException { case "event": case "deadline": String taskName = getNextWord(input, command); - // taskname has to contain at least one alphabet and no signs (slash or etc) - if (!taskName.matches("[A-Za-z0-9]+$")) { - String errorMsg = String.format("%s requires a alphanumeric name\n", command); + // taskname cannot have "/" and must be at least size of 1 + if (taskName.contains("/") || taskName == "") { + String errorMsg = String.format("%s requires valid name (no `/` allowed) \n", command); throw new DukeException(errorMsg); } if (command.equals("event") || command.equals("deadline")) { From 424ec2424b573674cae4595716c6fbcbf0c429bb Mon Sep 17 00:00:00 2001 From: Tai Date: Fri, 4 Mar 2022 20:32:36 +0800 Subject: [PATCH 30/34] allow non alphanumeric name, disallow / in taskName --- src/main/java/duke/parser/Parser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 8286c6b4f..ae48243d9 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -56,7 +56,7 @@ private static void errorCheck(String input) throws DukeException { case "event": case "deadline": String taskName = getNextWord(input, command); - // taskname cannot have "/" and must be at least size of 1 + // taskName cannot have "/" and must be at least size of 1 if (taskName.contains("/") || taskName == "") { String errorMsg = String.format("%s requires valid name (no `/` allowed) \n", command); throw new DukeException(errorMsg); From 75b13f8d695aed9c5b235a47af6475606131a157 Mon Sep 17 00:00:00 2001 From: kktai1512 <77670583+kktai1512@users.noreply.github.com> Date: Fri, 4 Mar 2022 21:02:58 +0800 Subject: [PATCH 31/34] Update README.md --- README.md | 122 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 103 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index c714ceb1d..edf08fa90 100644 --- a/README.md +++ b/README.md @@ -1,59 +1,143 @@ + # Welcome to Duke! Duke is a CLI-based todo application. # Getting started -After running the JAR file, an output file named "Duke.txt" will be created in the same directory of the JAR. Outputs will be saved into the file before the program exits, and the file will be reread the next time Duke runs. +After running the JAR file, an output file named "Duke.txt" will be created in the same directory of the JAR. Output will be saved into the file before the program exits, and the file will be reread the next time Duke runs. + +To run the file, from the terminal, go to the same directory as your Duke.jar file, and run the command `java -jar Duke.jar` + + +# Features and usage +| Notation | Meaning | +|--|--| +| [T] | todo | +| [D] | deadline| +| [E] | event | +| [ ] | incomplete task +| [X] | completed task + + + -# Commands ## Add There are three types of tasks to be added. Namely, todo, deadline and event. ### Todo - To add a todo, command should be typed in the format "*todo taskname*" + To add a todo, command should be typed in the format + `todo (taskname)` Example of a valid input: - + `todo get groceries` + +Sample usage: + + todo buy groceries + ____________________________________________________________ + Got it. I've added this task: + [T][ ] buy groceries + Now you have 1 task in the list. + ____________________________________________________________ + - todo get groceries ### Event / Deadline - To add an event or a deadline, command should be typed in the format "event taskname /dd-MM-yyyy HH:mm" or "event taskname /additional info" + To add an event or a deadline, command should be typed in the format `event (taskname) /(dd-MM-yyyy HH:mm)` or + `event (taskname) /(additional info)` - :white_check_mark: Example of a valid input: + :white_check_mark: Sample usage with valid input: - event Mark's birthday /14-02-2021 07:00 - event Mark's birthday /three days from now - deadline CS2113T submission /03-02-21 00:00 + deadline cs2113t project /tomorrow + ____________________________________________________________ + Got it. I've added this task: + [D][ ] cs2113t project (by: tomorrow) + Now you have 2 tasks in the list. + ____________________________________________________________ + event Mark's birthday party /07-03-2022 22:00 + ____________________________________________________________ + + Got it. I've added this task: + [E][ ] Mark's birthday party (on: Mar 07 2022 22:00) + Now you have 3 tasks in the list. + + ____________________________________________________________ + + + :x: Example of a invalid input: deadline hello / event / - ## Delete - To delete a task, type in "delete" followed by task number. - - delete 2 + ## List - To list tasks, simply type "list" + To list tasks, simply type + `list` +Outcome: List of tasks. + + list + ____________________________________________________________ + 1. [T][ ] buy groceries + 2. [D][ ] cs2113t project (by: tomorrow) + 3. [E][ ] Mark's birthday party (on: Mar 07 2022 22:00) + ____________________________________________________________ + + ## Delete + To delete a task, type in the command `delete (task number)` + + + delete 2 + ____________________________________________________________ + Noted. I've removed this task: + [D][ ] cs2113t project (by: tomorrow) + Now you have 2 tasks in the list. + ____________________________________________________________ + + + ## Mark/Unmark - To mark the task as completed, type in "mark" followed by task number. + To mark the task as completed, type in `mark (task number)`. + Similarly, type in `unmark (task number)` to mark the task as incomplete. + - mark 3 + mark 1 + ____________________________________________________________ + [T][X] buy groceries + ____________________________________________________________ + unmark 1 + ____________________________________________________________ + [T][ ] buy groceries + ____________________________________________________________ ## Find - To find task(s) containing a specific keyword, type in "find" followed by the keyword. + To find task(s) containing a specific keyword, type in `find (keyword)` - find birthday + + + find groceries + ____________________________________________________________ + Here are the matching tasks in your list: + 1. [T][ ] buy groceries + ____________________________________________________________ ## Bye To exit the program, simply type "bye". The program terminates and the current state of the tasks is saved into "Duke.txt". + + bye + ____________________________________________________________ + Bye. Hope to see you again soon! + ____________________________________________________________ + + + + From 816230ce2d40adb64225e3109f34f51a165aab79 Mon Sep 17 00:00:00 2001 From: Tai Date: Fri, 4 Mar 2022 21:06:49 +0800 Subject: [PATCH 32/34] change readme.md --- README.md | 83 +++++++++++++++----------------------------------- docs/README.md | 62 +++++++++++++++++++++++++++---------- 2 files changed, 70 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index c714ceb1d..48ba0c52c 100644 --- a/README.md +++ b/README.md @@ -1,59 +1,24 @@ - -# Welcome to Duke! -Duke is a CLI-based todo application. -# Getting started -After running the JAR file, an output file named "Duke.txt" will be created in the same directory of the JAR. Outputs will be saved into the file before the program exits, and the file will be reread the next time Duke runs. - -# Commands -## Add -There are three types of tasks to be added. Namely, todo, deadline and event. -### Todo - - To add a todo, command should be typed in the format "*todo taskname*" - Example of a valid input: - - - todo get groceries - -### Event / Deadline - - To add an event or a deadline, command should be typed in the format "event taskname /dd-MM-yyyy HH:mm" or "event taskname /additional info" - - :white_check_mark: Example of a valid input: - - - event Mark's birthday /14-02-2021 07:00 - event Mark's birthday /three days from now - deadline CS2113T submission /03-02-21 00:00 - :x: Example of a invalid input: - - deadline hello / - event / - - ## Delete - To delete a task, type in "delete" followed by task number. - - - delete 2 - - ## List - To list tasks, simply type "list" - - - list - - ## Mark/Unmark - To mark the task as completed, type in "mark" followed by task number. - - - mark 3 - - ## Find - To find task(s) containing a specific keyword, type in "find" followed by the keyword. - - find birthday - - ## Bye -To exit the program, simply type "bye". The program terminates and the current state of the tasks is saved into "Duke.txt". - - bye +# Duke project template + +This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. + +## Setting up in Intellij + +Prerequisites: JDK 11, update Intellij to the most recent version. + +1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) +1. Open the project into Intellij as follows: + 1. Click `Open`. + 1. Select the project directory, and click `OK`. + 1. If there are any further prompts, accept the defaults. +1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
+ In the same dialog, set the **Project language level** field to the `SDK default` option. +3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: + ``` + Hello from + ____ _ + | _ \ _ _| | _____ + | | | | | | | |/ / _ \ + | |_| | |_| | < __/ + |____/ \__,_|_|\_\___| + ``` \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index 8077118eb..38079d308 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,59 @@ -# User Guide -## Features +# Welcome to Duke! +Duke is a CLI-based todo application. +# Getting started +After running the JAR file, an output file named "Duke.txt" will be created in the same directory of the JAR. Outputs will be saved into the file before the program exits, and the file will be reread the next time Duke runs. -### Feature-ABC +# Commands +## Add +There are three types of tasks to be added. Namely, todo, deadline and event. +### Todo -Description of the feature. +To add a todo, command should be typed in the format "*todo taskname*" +Example of a valid input: -### Feature-XYZ -Description of the feature. + todo get groceries -## Usage +### Event / Deadline -### `Keyword` - Describe action +To add an event or a deadline, command should be typed in the format "event taskname /dd-MM-yyyy HH:mm" or "event taskname /additional info" -Describe the action and its outcome. +:white_check_mark: Example of a valid input: -Example of usage: -`keyword (optional arguments)` + event Mark's birthday /14-02-2021 07:00 + event Mark's birthday /three days from now + deadline CS2113T submission /03-02-21 00:00 +:x: Example of a invalid input: -Expected outcome: + deadline hello / + event / -Description of the outcome. +## Delete +To delete a task, type in "delete" followed by task number. -``` -expected output -``` + + delete 2 + +## List +To list tasks, simply type "list" + + + list + +## Mark/Unmark +To mark the task as completed, type in "mark" followed by task number. + + + mark 3 + +## Find +To find task(s) containing a specific keyword, type in "find" followed by the keyword. + + find birthday + +## Bye +To exit the program, simply type "bye". The program terminates and the current state of the tasks is saved into "Duke.txt". + + bye From b2c7657443795a62fbd49f9bde5148e2f7914a81 Mon Sep 17 00:00:00 2001 From: kktai1512 <77670583+kktai1512@users.noreply.github.com> Date: Fri, 4 Mar 2022 22:30:20 +0800 Subject: [PATCH 33/34] Update README.md --- docs/README.md | 118 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 101 insertions(+), 17 deletions(-) diff --git a/docs/README.md b/docs/README.md index 38079d308..6213d8d9a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,59 +1,143 @@ + # Welcome to Duke! Duke is a CLI-based todo application. # Getting started -After running the JAR file, an output file named "Duke.txt" will be created in the same directory of the JAR. Outputs will be saved into the file before the program exits, and the file will be reread the next time Duke runs. +After running the JAR file, an output file named "Duke.txt" will be created in the same directory of the JAR. Output will be saved into the file before the program exits, and the file will be reread the next time Duke runs. + +To run the file, from the terminal, go to the same directory as your Duke.jar file, and run the command `java -jar Duke.jar` + + +# Features and usage +| Notation | Meaning | +|--|--| +| [T] | todo | +| [D] | deadline| +| [E] | event | +| [ ] | incomplete task +| [X] | completed task + + + -# Commands ## Add There are three types of tasks to be added. Namely, todo, deadline and event. ### Todo -To add a todo, command should be typed in the format "*todo taskname*" +To add a todo, command should be typed in the format +`todo (taskname)` Example of a valid input: +`todo get groceries` + +Sample usage: + + todo buy groceries + ____________________________________________________________ + Got it. I've added this task: + [T][ ] buy groceries + Now you have 1 task in the list. + ____________________________________________________________ - todo get groceries ### Event / Deadline -To add an event or a deadline, command should be typed in the format "event taskname /dd-MM-yyyy HH:mm" or "event taskname /additional info" +To add an event or a deadline, command should be typed in the format `event (taskname) /(dd-MM-yyyy HH:mm)` or +`event (taskname) /(additional info)` + +:white_check_mark: Sample usage with valid input: + + + deadline cs2113t project /tomorrow + ____________________________________________________________ + Got it. I've added this task: + [D][ ] cs2113t project (by: tomorrow) + Now you have 2 tasks in the list. + ____________________________________________________________ + event Mark's birthday party /07-03-2022 22:00 + ____________________________________________________________ + + Got it. I've added this task: + [E][ ] Mark's birthday party (on: Mar 07 2022 22:00) + Now you have 3 tasks in the list. + + ____________________________________________________________ -:white_check_mark: Example of a valid input: - event Mark's birthday /14-02-2021 07:00 - event Mark's birthday /three days from now - deadline CS2113T submission /03-02-21 00:00 :x: Example of a invalid input: deadline hello / event / -## Delete -To delete a task, type in "delete" followed by task number. - delete 2 ## List -To list tasks, simply type "list" +To list tasks, simply type +`list` + + +Outcome: List of tasks. list + ____________________________________________________________ + 1. [T][ ] buy groceries + 2. [D][ ] cs2113t project (by: tomorrow) + 3. [E][ ] Mark's birthday party (on: Mar 07 2022 22:00) + ____________________________________________________________ + +## Delete +To delete a task, type in the command `delete (task number)` + + + delete 2 + ____________________________________________________________ + Noted. I've removed this task: + [D][ ] cs2113t project (by: tomorrow) + Now you have 2 tasks in the list. + ____________________________________________________________ + + + ## Mark/Unmark -To mark the task as completed, type in "mark" followed by task number. +To mark the task as completed, type in `mark (task number)`. +Similarly, type in `unmark (task number)` to mark the task as incomplete. - mark 3 + + mark 1 + ____________________________________________________________ + [T][X] buy groceries + ____________________________________________________________ + unmark 1 + ____________________________________________________________ + [T][ ] buy groceries + ____________________________________________________________ ## Find -To find task(s) containing a specific keyword, type in "find" followed by the keyword. +To find task(s) containing a specific keyword, type in `find (keyword)` + - find birthday + + find groceries + ____________________________________________________________ + Here are the matching tasks in your list: + 1. [T][ ] buy groceries + ____________________________________________________________ ## Bye To exit the program, simply type "bye". The program terminates and the current state of the tasks is saved into "Duke.txt". + + bye + ____________________________________________________________ + Bye. Hope to see you again soon! + ____________________________________________________________ + + + + From c170780815282c66d57743892154e4a2c7872342 Mon Sep 17 00:00:00 2001 From: kktai1512 <77670583+kktai1512@users.noreply.github.com> Date: Fri, 4 Mar 2022 22:32:47 +0800 Subject: [PATCH 34/34] Update README.md --- docs/README.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/README.md b/docs/README.md index 6213d8d9a..5a1de4f53 100644 --- a/docs/README.md +++ b/docs/README.md @@ -56,11 +56,9 @@ To add an event or a deadline, command should be typed in the format `event (tas ____________________________________________________________ event Mark's birthday party /07-03-2022 22:00 ____________________________________________________________ - Got it. I've added this task: [E][ ] Mark's birthday party (on: Mar 07 2022 22:00) Now you have 3 tasks in the list. - ____________________________________________________________ @@ -74,12 +72,11 @@ To add an event or a deadline, command should be typed in the format `event (tas ## List -To list tasks, simply type +To list all the tasks, simply type `list` -Outcome: List of tasks. - +Sample usage: list ____________________________________________________________ @@ -91,6 +88,7 @@ Outcome: List of tasks. ## Delete To delete a task, type in the command `delete (task number)` +Sample usage: delete 2 ____________________________________________________________ @@ -106,7 +104,7 @@ To delete a task, type in the command `delete (task number)` To mark the task as completed, type in `mark (task number)`. Similarly, type in `unmark (task number)` to mark the task as incomplete. - +Sample usage: mark 1 ____________________________________________________________ @@ -120,7 +118,7 @@ Similarly, type in `unmark (task number)` to mark the task as incomplete. ## Find To find task(s) containing a specific keyword, type in `find (keyword)` - +Sample usage: find groceries ____________________________________________________________ @@ -131,7 +129,7 @@ To find task(s) containing a specific keyword, type in `find (keyword)` ## Bye To exit the program, simply type "bye". The program terminates and the current state of the tasks is saved into "Duke.txt". - +Sample usage: bye ____________________________________________________________