From ab76983039e218197b246d665c3cc6a16d3d435d Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 27 Jan 2023 17:18:46 +0800 Subject: [PATCH 01/26] Level-0 Increment, Greet and Exit message --- src/main/java/Duke.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..85ce925c4 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,5 +6,22 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + + greetUser(); + exit(); } + + public static void greetUser() { + System.out.println("____________________________________________________________"); + System.out.println("Hello! I'm Duke"); + System.out.println("What can I do for you?"); + } + + public static void exit() { + System.out.println("____________________________________________________________"); + System.out.println("Bye. Hope to see you again soon!"); + System.out.println("____________________________________________________________"); + + } + } From 66f05b6900771150d6db68a3cf3e7bd804b6beb0 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 27 Jan 2023 17:35:36 +0800 Subject: [PATCH 02/26] Level 1. Greet, Echo, Exit --- src/main/java/Duke.java | 46 +++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 85ce925c4..f82da9bb5 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,5 +1,19 @@ +import java.util.ArrayList; +import java.util.Scanner; + public class Duke { + + private ArrayList userInput; + + public Duke() { + this.userInput = new ArrayList<>(); + } + public static void main(String[] args) { + + Duke duke = new Duke(); + boolean isRun = true; + Scanner scan = new Scanner(System.in); String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" @@ -8,19 +22,39 @@ public static void main(String[] args) { System.out.println("Hello from\n" + logo); greetUser(); + + while (true) { + String input = scan.nextLine(); + duke.userInput.add(input); + if (input.equals("bye")) { + break; + } + echoCmd(input); + + + } exit(); } + public static void echoCmd(String cmd) { + System.out.println("\t____________________________________________________________"); + System.out.println("\t " + cmd); + System.out.println("\t____________________________________________________________"); + + } + public static void greetUser() { - System.out.println("____________________________________________________________"); - System.out.println("Hello! I'm Duke"); - System.out.println("What can I do for you?"); + System.out.println("\t____________________________________________________________"); + System.out.println("\tHello! I'm Duke"); + System.out.println("\tWhat can I do for you?"); + System.out.println("\t____________________________________________________________"); + } public static void exit() { - System.out.println("____________________________________________________________"); - System.out.println("Bye. Hope to see you again soon!"); - System.out.println("____________________________________________________________"); + System.out.println("\t____________________________________________________________"); + System.out.println("\tBye. Hope to see you again soon!"); + System.out.println("\t____________________________________________________________"); } From 326cf60fa2eef637b06df785ebb7e0b57e0faed3 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 27 Jan 2023 17:44:38 +0800 Subject: [PATCH 03/26] Level 2. Add, List --- src/main/java/Duke.java | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f82da9bb5..5e10032da 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,15 +3,14 @@ public class Duke { - private ArrayList userInput; - public Duke() { - this.userInput = new ArrayList<>(); + } public static void main(String[] args) { Duke duke = new Duke(); + ArrayList userInput = new ArrayList<>(); boolean isRun = true; Scanner scan = new Scanner(System.in); String logo = " ____ _ \n" @@ -25,17 +24,33 @@ public static void main(String[] args) { while (true) { String input = scan.nextLine(); - duke.userInput.add(input); if (input.equals("bye")) { break; + } else if (input.equals("list")) { + list(userInput); } - echoCmd(input); + //echoCmd(input); + addToList(input, userInput); } exit(); } + public static void addToList(String cmd, ArrayList userInput) { + System.out.println("\t____________________________________________________________"); + System.out.println("\tadded: " + cmd); + System.out.println("\t____________________________________________________________"); + userInput.add(cmd); + } + public static void list(ArrayList userInput) { + System.out.println("\t____________________________________________________________"); + for (int i = 0; i < userInput.size(); i++) { + System.out.println("\t" + (i + 1) + ". " + userInput.get(i)); + } + System.out.println("\t____________________________________________________________"); + + } public static void echoCmd(String cmd) { System.out.println("\t____________________________________________________________"); System.out.println("\t " + cmd); From e77c524ef058413c88cf204141c66392f709acb2 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Wed, 1 Feb 2023 01:11:51 +0800 Subject: [PATCH 04/26] LevevlLevel 3. Mark as Done --- src/main/java/Duke.java | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5e10032da..65f490130 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -26,27 +26,58 @@ public static void main(String[] args) { String input = scan.nextLine(); if (input.equals("bye")) { break; - } else if (input.equals("list")) { + } + if (input.equals("list")) { list(userInput); + } else if (input.length() >= 4 && input.substring(0, 4).equals("mark")) { + String[] tmpArr = input.split(" "); + markDone(Integer.parseInt(tmpArr[1]), userInput, true); + } else if (input.length() >= 6 && input.substring(0, 6).equals("unmark")) { + String[] tmpArr = input.split(" "); + markDone(Integer.parseInt(tmpArr[1]), userInput, false); + } else { + addToList(input, userInput); } //echoCmd(input); - addToList(input, userInput); } exit(); } + public static void markDone(int index, ArrayList userInput, boolean mark) { + if (mark) { + String org = userInput.get(index - 1); + String newStr = org.replace("[ ]", "[X]"); + System.out.println(newStr); + userInput.set(index - 1, newStr); + } else { + String org = userInput.get(index - 1); + String newStr = org.replace("[X]", "[ ]"); + System.out.println(newStr); + userInput.set(index - 1, newStr); + } + System.out.println("\t____________________________________________________________"); + if (mark) { + System.out.println("\t Nice! I've marked this task as done:"); + } else { + System.out.println("\t OK, I've marked this task as not done yet:"); + } + int idx = userInput.get(index - 1).indexOf("["); + System.out.println("\t" + " " + userInput.get(index - 1).substring(idx)); + System.out.println("\t____________________________________________________________"); + } public static void addToList(String cmd, ArrayList userInput) { System.out.println("\t____________________________________________________________"); System.out.println("\tadded: " + cmd); System.out.println("\t____________________________________________________________"); userInput.add(cmd); + userInput.set(userInput.size() - 1, userInput.size() + ". [ ] " + userInput.get(userInput.size() - 1)); } public static void list(ArrayList userInput) { System.out.println("\t____________________________________________________________"); for (int i = 0; i < userInput.size(); i++) { - System.out.println("\t" + (i + 1) + ". " + userInput.get(i)); + System.out.println("\t" + userInput.get(i)); } System.out.println("\t____________________________________________________________"); From 2cc013112056e2982c509ab5d17201a9928ecf32 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Wed, 1 Feb 2023 01:18:29 +0800 Subject: [PATCH 05/26] A-CodingStandard --- src/main/java/Duke.java | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 65f490130..4fea80b19 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -10,7 +10,7 @@ public Duke() { public static void main(String[] args) { Duke duke = new Duke(); - ArrayList userInput = new ArrayList<>(); + ArrayList userInputs = new ArrayList<>(); boolean isRun = true; Scanner scan = new Scanner(System.in); String logo = " ____ _ \n" @@ -28,15 +28,15 @@ public static void main(String[] args) { break; } if (input.equals("list")) { - list(userInput); + listOut(userInputs); } else if (input.length() >= 4 && input.substring(0, 4).equals("mark")) { String[] tmpArr = input.split(" "); - markDone(Integer.parseInt(tmpArr[1]), userInput, true); + markDone(Integer.parseInt(tmpArr[1]), userInputs, true); } else if (input.length() >= 6 && input.substring(0, 6).equals("unmark")) { String[] tmpArr = input.split(" "); - markDone(Integer.parseInt(tmpArr[1]), userInput, false); + markDone(Integer.parseInt(tmpArr[1]), userInputs, false); } else { - addToList(input, userInput); + addToList(input, userInputs); } //echoCmd(input); @@ -44,40 +44,40 @@ public static void main(String[] args) { } exit(); } - public static void markDone(int index, ArrayList userInput, boolean mark) { - if (mark) { - String org = userInput.get(index - 1); + public static void markDone(int index, ArrayList userInputs, boolean isMark) { + if (isMark) { + String org = userInputs.get(index - 1); String newStr = org.replace("[ ]", "[X]"); System.out.println(newStr); - userInput.set(index - 1, newStr); + userInputs.set(index - 1, newStr); } else { - String org = userInput.get(index - 1); + String org = userInputs.get(index - 1); String newStr = org.replace("[X]", "[ ]"); System.out.println(newStr); - userInput.set(index - 1, newStr); + userInputs.set(index - 1, newStr); } System.out.println("\t____________________________________________________________"); - if (mark) { + if (isMark) { System.out.println("\t Nice! I've marked this task as done:"); } else { System.out.println("\t OK, I've marked this task as not done yet:"); } - int idx = userInput.get(index - 1).indexOf("["); - System.out.println("\t" + " " + userInput.get(index - 1).substring(idx)); + int idx = userInputs.get(index - 1).indexOf("["); + System.out.println("\t" + " " + userInputs.get(index - 1).substring(idx)); System.out.println("\t____________________________________________________________"); } - public static void addToList(String cmd, ArrayList userInput) { + public static void addToList(String cmd, ArrayList userInputs) { System.out.println("\t____________________________________________________________"); System.out.println("\tadded: " + cmd); System.out.println("\t____________________________________________________________"); - userInput.add(cmd); - userInput.set(userInput.size() - 1, userInput.size() + ". [ ] " + userInput.get(userInput.size() - 1)); + userInputs.add(cmd); + userInputs.set(userInputs.size() - 1, userInputs.size() + ". [ ] " + userInputs.get(userInputs.size() - 1)); } - public static void list(ArrayList userInput) { + public static void listOut(ArrayList userInputs) { System.out.println("\t____________________________________________________________"); - for (int i = 0; i < userInput.size(); i++) { - System.out.println("\t" + userInput.get(i)); + for (int i = 0; i < userInputs.size(); i++) { + System.out.println("\t" + userInputs.get(i)); } System.out.println("\t____________________________________________________________"); From 489244b5f3e5c5236275a583fcea30bf6826afe0 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Feb 2023 04:26:24 +0800 Subject: [PATCH 06/26] Level 4. ToDos, Events, Deadlines --- src/main/java/Deadline.java | 14 +++++++ src/main/java/Duke.java | 83 ++++++++++++++++++++++--------------- src/main/java/Event.java | 17 ++++++++ src/main/java/Task.java | 25 +++++++++++ src/main/java/Todo.java | 10 +++++ 5 files changed, 116 insertions(+), 33 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/Task.java create mode 100644 src/main/java/Todo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..bc4660fde --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,14 @@ +public class Deadline extends Task { + + protected String by; + + public Deadline(String description, boolean isMark, String by) { + super(description, isMark); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + super.toString() + "(by:" + by + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4fea80b19..0b31b9ebe 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -11,6 +11,7 @@ public static void main(String[] args) { Duke duke = new Duke(); ArrayList userInputs = new ArrayList<>(); + ArrayList tasks = new ArrayList<>(); boolean isRun = true; Scanner scan = new Scanner(System.in); String logo = " ____ _ \n" @@ -23,61 +24,77 @@ public static void main(String[] args) { greetUser(); while (true) { + Task tsk = null; String input = scan.nextLine(); + if (input.equals("bye")) { break; } + if (input.contains("todo")) { + tsk = new Todo(input, false); + } else if (input.contains("event")) { + tsk = parseEvent(input); + } else if (input.contains("deadline")) { + tsk = parseDeadline(input); + } if (input.equals("list")) { - listOut(userInputs); + listOut(userInputs, tasks); } else if (input.length() >= 4 && input.substring(0, 4).equals("mark")) { String[] tmpArr = input.split(" "); - markDone(Integer.parseInt(tmpArr[1]), userInputs, true); + tasks.get(Integer.parseInt(tmpArr[1]) - 1).mark(); + System.out.println("\t____________________________________________________________"); + System.out.println("\tNice! I've marked this task as done:"); + System.out.println("\t " + tasks.get(Integer.parseInt(tmpArr[1]) - 1)); + System.out.println("\t____________________________________________________________"); + } else if (input.length() >= 6 && input.substring(0, 6).equals("unmark")) { String[] tmpArr = input.split(" "); - markDone(Integer.parseInt(tmpArr[1]), userInputs, false); + tasks.get(Integer.parseInt(tmpArr[1]) - 1).unMark(); + System.out.println("\t____________________________________________________________"); + System.out.println("\tNice! I've marked this task as done:"); + System.out.println("\t " + tasks.get(Integer.parseInt(tmpArr[1]) - 1)); + System.out.println("\t____________________________________________________________"); + } else { addToList(input, userInputs); - } - //echoCmd(input); - + tasks.add(tsk); + System.out.println("\t____________________________________________________________"); + System.out.println("\tGot it. I've added this task:"); + System.out.println("\t " + tsk.toString()); + System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); + System.out.println("\t____________________________________________________________"); + } } exit(); } - public static void markDone(int index, ArrayList userInputs, boolean isMark) { - if (isMark) { - String org = userInputs.get(index - 1); - String newStr = org.replace("[ ]", "[X]"); - System.out.println(newStr); - userInputs.set(index - 1, newStr); - } else { - String org = userInputs.get(index - 1); - String newStr = org.replace("[X]", "[ ]"); - System.out.println(newStr); - userInputs.set(index - 1, newStr); - } - System.out.println("\t____________________________________________________________"); - if (isMark) { - System.out.println("\t Nice! I've marked this task as done:"); - } else { - System.out.println("\t OK, I've marked this task as not done yet:"); - } - int idx = userInputs.get(index - 1).indexOf("["); - System.out.println("\t" + " " + userInputs.get(index - 1).substring(idx)); - System.out.println("\t____________________________________________________________"); +//deadline return book /by Sunday + public static Deadline parseDeadline(String input) { + int idx = input.indexOf("/by"); + String desc = input.substring(8, idx); + String by = input.substring(idx + 3); + return new Deadline(desc, false, by); } + public static Event parseEvent(String input) { + int idx = input.indexOf("/from"); + int idx1 = input.indexOf("/to"); + String desc = input.substring(5, idx); + String start = input.substring(idx + 5, idx1); + String end = input.substring(idx1 + 3, input.length()); + + return new Event(desc, false, start, end); + }//event project meeting /from Mon 2pm /to 4pm + public static void addToList(String cmd, ArrayList userInputs) { - System.out.println("\t____________________________________________________________"); - System.out.println("\tadded: " + cmd); - System.out.println("\t____________________________________________________________"); userInputs.add(cmd); userInputs.set(userInputs.size() - 1, userInputs.size() + ". [ ] " + userInputs.get(userInputs.size() - 1)); } - public static void listOut(ArrayList userInputs) { + public static void listOut(ArrayList userInputs, ArrayList tasks) { System.out.println("\t____________________________________________________________"); - for (int i = 0; i < userInputs.size(); i++) { - System.out.println("\t" + userInputs.get(i)); + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println("\t" + (i + 1) + "." + tasks.get(i)); } System.out.println("\t____________________________________________________________"); diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..d4b357e24 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,17 @@ +public class Event extends Task { + + private String start; + private String end; + + + public Event(String description, boolean isMark, String start, String end) { + super(description, isMark); + this.start = start; + this.end = end; + } + + @Override + public String toString() { + return "[E]" + super.toString() + "(from:" + this.start + "to:" + this.end + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..0fe7517b4 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,25 @@ +public class Task { + private String description; + private boolean isMark; + public Task(String inDescription, boolean isMark) { + this.description = inDescription; + this.isMark = isMark; + } + public void mark() { + this.isMark = true; + } + public void unMark() { + this.isMark = false; + } + public String getMarked() { + if (this.isMark) { + return "[X]"; + } else { + return "[ ]"; + } + } + + public String toString() { + return this.getMarked() + " " + this.description; + } +} diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..a7c8e3c1f --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,10 @@ +public class Todo extends Task { + public Todo(String description, boolean isMark) { + super(description, isMark); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} \ No newline at end of file From 9cc6ba3401367d8f78d1a863b927d1f2a3c3c716 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Feb 2023 04:29:34 +0800 Subject: [PATCH 07/26] A-CodeQuality --- src/main/java/Duke.java | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 0b31b9ebe..ceddd3944 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -7,12 +7,13 @@ public Duke() { } + /* + Main function that takes user input and interpets how to store and what to do with it + */ public static void main(String[] args) { - Duke duke = new Duke(); ArrayList userInputs = new ArrayList<>(); ArrayList tasks = new ArrayList<>(); - boolean isRun = true; Scanner scan = new Scanner(System.in); String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -68,13 +69,18 @@ public static void main(String[] args) { } exit(); } -//deadline return book /by Sunday + /* + This Returns the input as a Deadline object + */ public static Deadline parseDeadline(String input) { int idx = input.indexOf("/by"); String desc = input.substring(8, idx); String by = input.substring(idx + 3); return new Deadline(desc, false, by); } + /* + This Returns the input as a Event object + */ public static Event parseEvent(String input) { int idx = input.indexOf("/from"); int idx1 = input.indexOf("/to"); @@ -83,13 +89,17 @@ public static Event parseEvent(String input) { String end = input.substring(idx1 + 3, input.length()); return new Event(desc, false, start, end); - }//event project meeting /from Mon 2pm /to 4pm - + } + /* + This Adds the input to an input array for the ability to keep track of + */ public static void addToList(String cmd, ArrayList userInputs) { userInputs.add(cmd); userInputs.set(userInputs.size() - 1, userInputs.size() + ". [ ] " + userInputs.get(userInputs.size() - 1)); } - + /* + This method lists out the tasks in order + */ public static void listOut(ArrayList userInputs, ArrayList tasks) { System.out.println("\t____________________________________________________________"); System.out.println("Here are the tasks in your list:"); @@ -97,23 +107,19 @@ public static void listOut(ArrayList userInputs, ArrayList tasks) System.out.println("\t" + (i + 1) + "." + tasks.get(i)); } System.out.println("\t____________________________________________________________"); - - } - public static void echoCmd(String cmd) { - System.out.println("\t____________________________________________________________"); - System.out.println("\t " + cmd); - System.out.println("\t____________________________________________________________"); - } - + /* + Automated greet function + */ public static void greetUser() { System.out.println("\t____________________________________________________________"); System.out.println("\tHello! I'm Duke"); System.out.println("\tWhat can I do for you?"); System.out.println("\t____________________________________________________________"); - } - + /* + Exit message + */ public static void exit() { System.out.println("\t____________________________________________________________"); System.out.println("\tBye. Hope to see you again soon!"); From 9939b5645bc5251aeb2f245f98616fdc5fb0c3ff Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Wed, 8 Feb 2023 14:30:25 +0800 Subject: [PATCH 08/26] added class DukeException.java and switched from if statement structure to switch case --- src/main/java/Duke.java | 76 +++++++++++++++++++------------- src/main/java/DukeException.java | 3 ++ 2 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ceddd3944..67e46fc1a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -27,47 +27,61 @@ public static void main(String[] args) { while (true) { Task tsk = null; String input = scan.nextLine(); + String[] splitInput = input.split(" "); - if (input.equals("bye")) { - break; - } - if (input.contains("todo")) { + switch (splitInput[0]) { + case "bye": + exit(); + return; + case "todo": tsk = new Todo(input, false); - } else if (input.contains("event")) { + addToList(input, userInputs); + tasks.add(tsk); + addTaskPrint(tasks, tsk); + + break; + case "event": tsk = parseEvent(input); - } else if (input.contains("deadline")) { + addToList(input, userInputs); + tasks.add(tsk); + addTaskPrint(tasks, tsk); + + break; + case "deadline": tsk = parseDeadline(input); - } - if (input.equals("list")) { + addToList(input, userInputs); + tasks.add(tsk); + addTaskPrint(tasks, tsk); + break; + case "list": listOut(userInputs, tasks); - } else if (input.length() >= 4 && input.substring(0, 4).equals("mark")) { - String[] tmpArr = input.split(" "); - tasks.get(Integer.parseInt(tmpArr[1]) - 1).mark(); - System.out.println("\t____________________________________________________________"); - System.out.println("\tNice! I've marked this task as done:"); - System.out.println("\t " + tasks.get(Integer.parseInt(tmpArr[1]) - 1)); - System.out.println("\t____________________________________________________________"); - - } else if (input.length() >= 6 && input.substring(0, 6).equals("unmark")) { - String[] tmpArr = input.split(" "); - tasks.get(Integer.parseInt(tmpArr[1]) - 1).unMark(); + break; + case "mark": + tasks.get(Integer.parseInt(splitInput[1]) - 1).mark(); System.out.println("\t____________________________________________________________"); System.out.println("\tNice! I've marked this task as done:"); - System.out.println("\t " + tasks.get(Integer.parseInt(tmpArr[1]) - 1)); - System.out.println("\t____________________________________________________________"); - - } else { - addToList(input, userInputs); - tasks.add(tsk); + System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); + System.out.println("\t____________________________________________________________"); + break; + case "unmark": + tasks.get(Integer.parseInt(splitInput[1]) - 1).unMark(); System.out.println("\t____________________________________________________________"); - System.out.println("\tGot it. I've added this task:"); - System.out.println("\t " + tsk.toString()); - System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); + System.out.println("\tOK, I've marked this task as not done yet:"); + System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); System.out.println("\t____________________________________________________________"); - - } + break; + default: + break; + } } - exit(); + } + + public static void addTaskPrint(ArrayList tasks, Task tsk) { + System.out.println("\t____________________________________________________________"); + System.out.println("\tGot it. I've added this task:"); + System.out.println("\t " + tsk.toString()); + System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); + System.out.println("\t____________________________________________________________"); } /* This Returns the input as a Deadline object diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..fb8bea5b6 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,3 @@ +public class DukeException { + +} From c8898cc16d4e426828630555e6c23b517dd68619 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Thu, 9 Feb 2023 13:02:39 +0800 Subject: [PATCH 09/26] Added exception handling for todo and unknown command --- src/main/java/Deadline.java | 2 +- src/main/java/Duke.java | 41 +++++++++++++++++++++++------ src/main/java/DukeException.java | 2 +- src/main/java/DukeIOBException.java | 3 +++ src/main/java/Event.java | 2 +- src/main/java/Task.java | 5 +++- src/main/java/Todo.java | 3 ++- 7 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 src/main/java/DukeIOBException.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index bc4660fde..30518733c 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -2,7 +2,7 @@ public class Deadline extends Task { protected String by; - public Deadline(String description, boolean isMark, String by) { + public Deadline(String description, boolean isMark, String by) throws DukeException { super(description, isMark); this.by = by; } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 67e46fc1a..d549c5c0c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -34,10 +34,17 @@ public static void main(String[] args) { exit(); return; case "todo": - tsk = new Todo(input, false); - addToList(input, userInputs); - tasks.add(tsk); - addTaskPrint(tasks, tsk); + try { + tsk = new Todo(splitInput[1], false); + addToList(input, userInputs); + tasks.add(tsk); + addTaskPrint(tasks, tsk); + } catch (IndexOutOfBoundsException de) { + printExceptionMsg("todo", "description of a todo cannot be empty."); + } catch (DukeException de) { + + } + break; case "event": @@ -45,7 +52,6 @@ public static void main(String[] args) { addToList(input, userInputs); tasks.add(tsk); addTaskPrint(tasks, tsk); - break; case "deadline": tsk = parseDeadline(input); @@ -71,11 +77,19 @@ public static void main(String[] args) { System.out.println("\t____________________________________________________________"); break; default: + System.out.println("\t____________________________________________________________"); + System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");; + System.out.println("\t____________________________________________________________"); + break; } } } - + public static void printExceptionMsg(String task, String err) { + System.out.println("\t____________________________________________________________"); + System.out.println(" ☹ OOPS!!! The description of a todo cannot be empty."); + System.out.println("\t____________________________________________________________"); + } public static void addTaskPrint(ArrayList tasks, Task tsk) { System.out.println("\t____________________________________________________________"); System.out.println("\tGot it. I've added this task:"); @@ -90,7 +104,13 @@ public static Deadline parseDeadline(String input) { int idx = input.indexOf("/by"); String desc = input.substring(8, idx); String by = input.substring(idx + 3); - return new Deadline(desc, false, by); + Deadline tsk = null; + try { + tsk = new Deadline(desc, false, by); + } catch (DukeException de) { + + } + return tsk; } /* This Returns the input as a Event object @@ -101,8 +121,13 @@ public static Event parseEvent(String input) { String desc = input.substring(5, idx); String start = input.substring(idx + 5, idx1); String end = input.substring(idx1 + 3, input.length()); + Event tsk = null; + try { + tsk = new Event(desc, false, start, end); + } catch (DukeException de) { - return new Event(desc, false, start, end); + } + return tsk; } /* This Adds the input to an input array for the ability to keep track of diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java index fb8bea5b6..f88f4a6d9 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/DukeException.java @@ -1,3 +1,3 @@ -public class DukeException { +public class DukeException extends Throwable { } diff --git a/src/main/java/DukeIOBException.java b/src/main/java/DukeIOBException.java new file mode 100644 index 000000000..bedccd12c --- /dev/null +++ b/src/main/java/DukeIOBException.java @@ -0,0 +1,3 @@ +public class DukeIOBException extends java.lang.ArrayIndexOutOfBoundsException { + +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java index d4b357e24..05c915064 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -4,7 +4,7 @@ public class Event extends Task { private String end; - public Event(String description, boolean isMark, String start, String end) { + public Event(String description, boolean isMark, String start, String end) throws DukeException { super(description, isMark); this.start = start; this.end = end; diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 0fe7517b4..87f7454a0 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,7 +1,10 @@ public class Task { private String description; private boolean isMark; - public Task(String inDescription, boolean isMark) { + public Task(String inDescription, boolean isMark) throws DukeException { + if (inDescription.length() == 0) { + throw new DukeException(); + } this.description = inDescription; this.isMark = isMark; } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index a7c8e3c1f..c937b21f4 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -1,6 +1,7 @@ public class Todo extends Task { - public Todo(String description, boolean isMark) { + public Todo(String description, boolean isMark) throws DukeException { super(description, isMark); + } @Override From c233d69efbfe7584714b259121d350cb96f75703 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Thu, 9 Feb 2023 13:33:21 +0800 Subject: [PATCH 10/26] A-Packages --- src/main/java/{ => duke}/Duke.java | 10 ++++++++++ src/main/java/{ => dukeException}/DukeException.java | 2 ++ .../java/{ => dukeException}/DukeIOBException.java | 2 ++ src/main/java/{ => tasks}/Deadline.java | 4 ++++ src/main/java/{ => tasks}/Event.java | 4 ++++ src/main/java/{ => tasks}/Task.java | 4 ++++ src/main/java/{ => tasks}/Todo.java | 4 ++++ 7 files changed, 30 insertions(+) rename src/main/java/{ => duke}/Duke.java (97%) rename src/main/java/{ => dukeException}/DukeException.java (69%) rename src/main/java/{ => dukeException}/DukeIOBException.java (78%) rename src/main/java/{ => tasks}/Deadline.java (86%) rename src/main/java/{ => tasks}/Event.java (88%) rename src/main/java/{ => tasks}/Task.java (92%) rename src/main/java/{ => tasks}/Todo.java (82%) diff --git a/src/main/java/Duke.java b/src/main/java/duke/Duke.java similarity index 97% rename from src/main/java/Duke.java rename to src/main/java/duke/Duke.java index d549c5c0c..64de99b4b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,6 +1,16 @@ +package duke; + import java.util.ArrayList; import java.util.Scanner; +import tasks.Task; +import tasks.Deadline; +import tasks.Event; +import tasks.Todo; + +import dukeException.DukeException; +import dukeException.DukeIOBException; + public class Duke { public Duke() { diff --git a/src/main/java/DukeException.java b/src/main/java/dukeException/DukeException.java similarity index 69% rename from src/main/java/DukeException.java rename to src/main/java/dukeException/DukeException.java index f88f4a6d9..d6b3de535 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/dukeException/DukeException.java @@ -1,3 +1,5 @@ +package dukeException; + public class DukeException extends Throwable { } diff --git a/src/main/java/DukeIOBException.java b/src/main/java/dukeException/DukeIOBException.java similarity index 78% rename from src/main/java/DukeIOBException.java rename to src/main/java/dukeException/DukeIOBException.java index bedccd12c..9f8154a0a 100644 --- a/src/main/java/DukeIOBException.java +++ b/src/main/java/dukeException/DukeIOBException.java @@ -1,3 +1,5 @@ +package dukeException; + public class DukeIOBException extends java.lang.ArrayIndexOutOfBoundsException { } diff --git a/src/main/java/Deadline.java b/src/main/java/tasks/Deadline.java similarity index 86% rename from src/main/java/Deadline.java rename to src/main/java/tasks/Deadline.java index 30518733c..740f6759a 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/tasks/Deadline.java @@ -1,3 +1,7 @@ +package tasks; + +import dukeException.DukeException; + public class Deadline extends Task { protected String by; diff --git a/src/main/java/Event.java b/src/main/java/tasks/Event.java similarity index 88% rename from src/main/java/Event.java rename to src/main/java/tasks/Event.java index 05c915064..6245d4dca 100644 --- a/src/main/java/Event.java +++ b/src/main/java/tasks/Event.java @@ -1,3 +1,7 @@ +package tasks; + +import dukeException.DukeException; + public class Event extends Task { private String start; diff --git a/src/main/java/Task.java b/src/main/java/tasks/Task.java similarity index 92% rename from src/main/java/Task.java rename to src/main/java/tasks/Task.java index 87f7454a0..1f435fae6 100644 --- a/src/main/java/Task.java +++ b/src/main/java/tasks/Task.java @@ -1,3 +1,7 @@ +package tasks; + +import dukeException.DukeException; + public class Task { private String description; private boolean isMark; diff --git a/src/main/java/Todo.java b/src/main/java/tasks/Todo.java similarity index 82% rename from src/main/java/Todo.java rename to src/main/java/tasks/Todo.java index c937b21f4..81c611ba0 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/tasks/Todo.java @@ -1,3 +1,7 @@ +package tasks; + +import dukeException.DukeException; + public class Todo extends Task { public Todo(String description, boolean isMark) throws DukeException { super(description, isMark); From a760d2027a0a5a9debf3c5ce73f5226cbd73bdec Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Thu, 16 Feb 2023 01:46:45 +0800 Subject: [PATCH 11/26] Level 6. Delete Add support for deleting tasks from the list. And Code cleanup --- src/main/java/duke/Duke.java | 100 +++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 64de99b4b..032b0fdbf 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,5 +1,6 @@ package duke; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Scanner; @@ -12,7 +13,7 @@ import dukeException.DukeIOBException; public class Duke { - + static ArrayList tasks = new ArrayList<>(); public Duke() { } @@ -23,7 +24,6 @@ public Duke() { public static void main(String[] args) { ArrayList userInputs = new ArrayList<>(); - ArrayList tasks = new ArrayList<>(); Scanner scan = new Scanner(System.in); String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -35,64 +35,72 @@ public static void main(String[] args) { greetUser(); while (true) { - Task tsk = null; String input = scan.nextLine(); String[] splitInput = input.split(" "); - switch (splitInput[0]) { case "bye": exit(); return; case "todo": - try { - tsk = new Todo(splitInput[1], false); - addToList(input, userInputs); - tasks.add(tsk); - addTaskPrint(tasks, tsk); - } catch (IndexOutOfBoundsException de) { - printExceptionMsg("todo", "description of a todo cannot be empty."); - } catch (DukeException de) { - - } - - - break; + insertTodo(input); + break; case "event": - tsk = parseEvent(input); - addToList(input, userInputs); - tasks.add(tsk); - addTaskPrint(tasks, tsk); + insertEvent(input); break; - case "deadline": - tsk = parseDeadline(input); - addToList(input, userInputs); - tasks.add(tsk); - addTaskPrint(tasks, tsk); + case "deadline": + insertDeadline(input); break; - case "list": + case "list": listOut(userInputs, tasks); break; case "mark": - tasks.get(Integer.parseInt(splitInput[1]) - 1).mark(); - System.out.println("\t____________________________________________________________"); - System.out.println("\tNice! I've marked this task as done:"); - System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); - System.out.println("\t____________________________________________________________"); + markTask(splitInput); break; - case "unmark": - tasks.get(Integer.parseInt(splitInput[1]) - 1).unMark(); - System.out.println("\t____________________________________________________________"); - System.out.println("\tOK, I've marked this task as not done yet:"); - System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); - System.out.println("\t____________________________________________________________"); + case "unmark": + unMarkTask(splitInput); + break; + case "delete": + deleteTask(splitInput); break; default: System.out.println("\t____________________________________________________________"); System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");; System.out.println("\t____________________________________________________________"); - break; - } + } + + } + } + public static void deleteTask(String[] splitInput) { + String tmpTask = tasks.get(Integer.parseInt(splitInput[1]) - 1).toString(); + tasks.remove(tasks.get(Integer.parseInt(splitInput[1]) - 1)); + System.out.println("\tNoted. I've removed this task:"); + System.out.println("\t " + tmpTask); + System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); + } + public static void unMarkTask(String[] splitInput) { + tasks.get(Integer.parseInt(splitInput[1]) - 1).unMark(); + System.out.println("\t____________________________________________________________"); + System.out.println("\tOK, I've marked this task as not done yet:"); + System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); + System.out.println("\t____________________________________________________________"); + } + public static void markTask(String[] splitInput) { + tasks.get(Integer.parseInt(splitInput[1]) - 1).mark(); + System.out.println("\t____________________________________________________________"); + System.out.println("\tNice! I've marked this task as done:"); + System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); + System.out.println("\t____________________________________________________________"); + } + public static void insertTodo(String input) { + try { + Task tsk = new Todo(input.substring(5), false); + tasks.add(tsk); + addTaskPrint(tasks, tsk); + } catch (IndexOutOfBoundsException de) { + printExceptionMsg("todo", "description of a todo cannot be empty."); + } catch (DukeException de) { + } } public static void printExceptionMsg(String task, String err) { @@ -110,34 +118,36 @@ public static void addTaskPrint(ArrayList tasks, Task tsk) { /* This Returns the input as a Deadline object */ - public static Deadline parseDeadline(String input) { + public static void insertDeadline(String input) { int idx = input.indexOf("/by"); String desc = input.substring(8, idx); String by = input.substring(idx + 3); Deadline tsk = null; try { tsk = new Deadline(desc, false, by); + addTaskPrint(tasks, tsk); } catch (DukeException de) { } - return tsk; + tasks.add(tsk); } /* This Returns the input as a Event object */ - public static Event parseEvent(String input) { + public static void insertEvent(String input) { int idx = input.indexOf("/from"); int idx1 = input.indexOf("/to"); String desc = input.substring(5, idx); String start = input.substring(idx + 5, idx1); - String end = input.substring(idx1 + 3, input.length()); + String end = input.substring(idx1 + 3); Event tsk = null; try { tsk = new Event(desc, false, start, end); + addTaskPrint(tasks, tsk); } catch (DukeException de) { } - return tsk; + tasks.add(tsk); } /* This Adds the input to an input array for the ability to keep track of From 238fb8567f3b9f9c29c9ac5fa127866d869cbc12 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Thu, 16 Feb 2023 01:57:52 +0800 Subject: [PATCH 12/26] adding File Paths_1 --- src/main/java/duke/Duke.java | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 64de99b4b..1fb02d885 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,5 +1,9 @@ package duke; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Scanner; @@ -32,6 +36,22 @@ public static void main(String[] args) { + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + try { + Files.createDirectories(Path.of("./data")); + + File myObj = new File("./data/duke.txt"); + if (myObj.createNewFile()) { + System.out.println("File created: " + myObj.getName()); + } else { + ArrayList tmpStrTasks = new ArrayList<>(); + tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get("./data/duke.txt"), StandardCharsets.UTF_8); + + } + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + greetUser(); while (true) { @@ -95,6 +115,34 @@ public static void main(String[] args) { } } } + public static void readDukeText() { + File f = new File("./data/duke.txt"); + try { + Scanner s = new Scanner(f); + while (s.hasNext()) { + String line = s.nextLine(); + String[] text = line.split("\\|"); + if (line.startsWith("T")) { + Todo todo = new Todo(text[2]); + todo.setDone(!text[1].equals(" ")); + tasks.add(todo); + } else if (line.startsWith("D")) { + Deadline deadline = new Deadline(text[2], text[3]); + deadline.setDone(!text[1].equals(" ")); + tasks.add(deadline); + } else if (line.startsWith("E")) { + Event event = new Event(text[2], text[3], text[4]); + event.setDone(!text[1].equals(" ")); + tasks.add(event); + } + } + } catch (FileNotFoundException e) { + System.out.println("Something went wrong: " + e.getMessage()); + throw new RuntimeException(e); + } catch (NoSuchElementException e) { + System.out.println("Sorry no line found. "); + } + } public static void printExceptionMsg(String task, String err) { System.out.println("\t____________________________________________________________"); System.out.println(" ☹ OOPS!!! The description of a todo cannot be empty."); From acd898ae95c619d3cd5df2bb4d2dea2bcba72577 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Thu, 16 Feb 2023 02:26:18 +0800 Subject: [PATCH 13/26] Level 7. Save --- src/main/java/duke/Duke.java | 173 +++++++++++++++++++++-------------- 1 file changed, 102 insertions(+), 71 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 1fb02d885..278848def 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,9 +1,12 @@ package duke; import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Scanner; @@ -16,7 +19,7 @@ import dukeException.DukeIOBException; public class Duke { - + static ArrayList tasks = new ArrayList<>(); public Duke() { } @@ -27,7 +30,6 @@ public Duke() { public static void main(String[] args) { ArrayList userInputs = new ArrayList<>(); - ArrayList tasks = new ArrayList<>(); Scanner scan = new Scanner(System.in); String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -43,9 +45,7 @@ public static void main(String[] args) { if (myObj.createNewFile()) { System.out.println("File created: " + myObj.getName()); } else { - ArrayList tmpStrTasks = new ArrayList<>(); - tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get("./data/duke.txt"), StandardCharsets.UTF_8); - + readDukeText(); } } catch (IOException e) { System.out.println("An error occurred."); @@ -55,92 +55,121 @@ public static void main(String[] args) { greetUser(); while (true) { - Task tsk = null; String input = scan.nextLine(); String[] splitInput = input.split(" "); - + int taskSize = tasks.size(); switch (splitInput[0]) { case "bye": exit(); return; case "todo": - try { - tsk = new Todo(splitInput[1], false); - addToList(input, userInputs); - tasks.add(tsk); - addTaskPrint(tasks, tsk); - } catch (IndexOutOfBoundsException de) { - printExceptionMsg("todo", "description of a todo cannot be empty."); - } catch (DukeException de) { - - } - - - break; + insertTodo(input, false); + break; case "event": - tsk = parseEvent(input); - addToList(input, userInputs); - tasks.add(tsk); - addTaskPrint(tasks, tsk); + insertEvent(input, false); break; - case "deadline": - tsk = parseDeadline(input); - addToList(input, userInputs); - tasks.add(tsk); - addTaskPrint(tasks, tsk); + case "deadline": + insertDeadline(input, false); break; - case "list": + case "list": listOut(userInputs, tasks); break; case "mark": - tasks.get(Integer.parseInt(splitInput[1]) - 1).mark(); - System.out.println("\t____________________________________________________________"); - System.out.println("\tNice! I've marked this task as done:"); - System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); - System.out.println("\t____________________________________________________________"); + markTask(splitInput); break; - case "unmark": - tasks.get(Integer.parseInt(splitInput[1]) - 1).unMark(); - System.out.println("\t____________________________________________________________"); - System.out.println("\tOK, I've marked this task as not done yet:"); - System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); - System.out.println("\t____________________________________________________________"); + case "unmark": + unMarkTask(splitInput); + break; + case "delete": + deleteTask(splitInput); break; default: System.out.println("\t____________________________________________________________"); System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");; System.out.println("\t____________________________________________________________"); - break; - } + } + if (taskSize != tasks.size()) { + saveTasks(); + } + + } + } + public static void saveTasks() { + try { + FileWriter myWriter = new FileWriter("./data/duke.txt"); + myWriter.flush(); + for (Task task : tasks) { + myWriter.write(task.toString() + "\n"); + } + myWriter.close(); + System.out.println("Successfully wrote to the file."); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); } } public static void readDukeText() { - File f = new File("./data/duke.txt"); + try { - Scanner s = new Scanner(f); - while (s.hasNext()) { - String line = s.nextLine(); - String[] text = line.split("\\|"); - if (line.startsWith("T")) { - Todo todo = new Todo(text[2]); - todo.setDone(!text[1].equals(" ")); - tasks.add(todo); - } else if (line.startsWith("D")) { - Deadline deadline = new Deadline(text[2], text[3]); - deadline.setDone(!text[1].equals(" ")); - tasks.add(deadline); - } else if (line.startsWith("E")) { - Event event = new Event(text[2], text[3], text[4]); - event.setDone(!text[1].equals(" ")); - tasks.add(event); + ArrayList tmpStrTasks = new ArrayList<>(); + tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get("./data/duke.txt"), StandardCharsets.UTF_8); + for (int i = 0; i < tmpStrTasks.size(); i++) { + System.out.println(tmpStrTasks.get(i).substring(7)); + } + for (int i = 0; i < tmpStrTasks.size(); i++) { + String newTask = tmpStrTasks.get(i); + Task tsk = null; + boolean isMark = newTask.charAt(4) == 'X'; + switch (newTask.charAt(1)) { + case 'T': + insertTodo("todo " + newTask.substring(7), isMark); + break; + case 'D': + insertDeadline("deadline " + newTask.substring(7), isMark); + break; + case 'E': + insertEvent("event " + newTask.substring(7), isMark); + break; } + System.out.println(newTask); } - } catch (FileNotFoundException e) { - System.out.println("Something went wrong: " + e.getMessage()); - throw new RuntimeException(e); - } catch (NoSuchElementException e) { - System.out.println("Sorry no line found. "); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } + + public static void deleteTask(String[] splitInput) { + String tmpTask = tasks.get(Integer.parseInt(splitInput[1]) - 1).toString(); + tasks.remove(tasks.get(Integer.parseInt(splitInput[1]) - 1)); + System.out.println("\tNoted. I've removed this task:"); + System.out.println("\t " + tmpTask); + System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); + } + public static void unMarkTask(String[] splitInput) { + tasks.get(Integer.parseInt(splitInput[1]) - 1).unMark(); + System.out.println("\t____________________________________________________________"); + System.out.println("\tOK, I've marked this task as not done yet:"); + System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); + System.out.println("\t____________________________________________________________"); + } + public static void markTask(String[] splitInput) { + tasks.get(Integer.parseInt(splitInput[1]) - 1).mark(); + System.out.println("\t____________________________________________________________"); + System.out.println("\tNice! I've marked this task as done:"); + System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); + System.out.println("\t____________________________________________________________"); + } + public static void insertTodo(String input, boolean isMark) { + + try { + Task tsk = new Todo(input.substring(5), isMark); + tasks.add(tsk); + addTaskPrint(tasks, tsk); + } catch (IndexOutOfBoundsException de) { + printExceptionMsg("todo", "description of a todo cannot be empty."); + } catch (DukeException de) { + } } public static void printExceptionMsg(String task, String err) { @@ -158,34 +187,36 @@ public static void addTaskPrint(ArrayList tasks, Task tsk) { /* This Returns the input as a Deadline object */ - public static Deadline parseDeadline(String input) { + public static void insertDeadline(String input, boolean isMark) { int idx = input.indexOf("/by"); String desc = input.substring(8, idx); String by = input.substring(idx + 3); Deadline tsk = null; try { tsk = new Deadline(desc, false, by); + addTaskPrint(tasks, tsk); } catch (DukeException de) { } - return tsk; + tasks.add(tsk); } /* This Returns the input as a Event object */ - public static Event parseEvent(String input) { + public static void insertEvent(String input, boolean isMark) { int idx = input.indexOf("/from"); int idx1 = input.indexOf("/to"); String desc = input.substring(5, idx); String start = input.substring(idx + 5, idx1); - String end = input.substring(idx1 + 3, input.length()); + String end = input.substring(idx1 + 3); Event tsk = null; try { tsk = new Event(desc, false, start, end); + addTaskPrint(tasks, tsk); } catch (DukeException de) { } - return tsk; + tasks.add(tsk); } /* This Adds the input to an input array for the ability to keep track of @@ -224,4 +255,4 @@ public static void exit() { } -} +} \ No newline at end of file From 96b58bef129ac110d3133cdfa9c5c5144aea1702 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Thu, 16 Feb 2023 02:38:27 +0800 Subject: [PATCH 14/26] Changes for code readibility --- src/main/java/duke/Duke.java | 35 +++-------------------------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index becd0807e..0b99e64e9 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,6 +1,5 @@ package duke; -<<<<<<< HEAD import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -8,9 +7,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -======= import java.lang.reflect.Array; ->>>>>>> branch-Level-6 import java.util.ArrayList; import java.util.Scanner; @@ -23,7 +20,7 @@ import dukeException.DukeIOBException; public class Duke { - + static ArrayList tasks = new ArrayList(); public Duke() { } @@ -62,16 +59,12 @@ public static void main(String[] args) { while (true) { String input = scan.nextLine(); String[] splitInput = input.split(" "); -<<<<<<< HEAD int taskSize = tasks.size(); -======= ->>>>>>> branch-Level-6 switch (splitInput[0]) { case "bye": exit(); return; case "todo": -<<<<<<< HEAD insertTodo(input, false); break; case "event": @@ -79,15 +72,12 @@ public static void main(String[] args) { break; case "deadline": insertDeadline(input, false); -======= - insertTodo(input); break; case "event": insertEvent(input); break; case "deadline": insertDeadline(input); ->>>>>>> branch-Level-6 break; case "list": listOut(userInputs, tasks); @@ -107,7 +97,6 @@ public static void main(String[] args) { System.out.println("\t____________________________________________________________"); break; } -<<<<<<< HEAD if (taskSize != tasks.size()) { saveTasks(); } @@ -158,11 +147,7 @@ public static void readDukeText() { } } -======= - } - } ->>>>>>> branch-Level-6 public static void deleteTask(String[] splitInput) { String tmpTask = tasks.get(Integer.parseInt(splitInput[1]) - 1).toString(); tasks.remove(tasks.get(Integer.parseInt(splitInput[1]) - 1)); @@ -184,16 +169,10 @@ public static void markTask(String[] splitInput) { System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); System.out.println("\t____________________________________________________________"); } -<<<<<<< HEAD public static void insertTodo(String input, boolean isMark) { try { Task tsk = new Todo(input.substring(5), isMark); -======= - public static void insertTodo(String input) { - try { - Task tsk = new Todo(input.substring(5), false); ->>>>>>> branch-Level-6 tasks.add(tsk); addTaskPrint(tasks, tsk); } catch (IndexOutOfBoundsException de) { @@ -217,17 +196,13 @@ public static void addTaskPrint(ArrayList tasks, Task tsk) { /* This Returns the input as a Deadline object */ -<<<<<<< HEAD public static void insertDeadline(String input, boolean isMark) { -======= - public static void insertDeadline(String input) { ->>>>>>> branch-Level-6 int idx = input.indexOf("/by"); String desc = input.substring(8, idx); String by = input.substring(idx + 3); Deadline tsk = null; try { - tsk = new Deadline(desc, false, by); + tsk = new Deadline(desc, isMark, by); addTaskPrint(tasks, tsk); } catch (DukeException de) { @@ -237,11 +212,7 @@ public static void insertDeadline(String input) { /* This Returns the input as a Event object */ -<<<<<<< HEAD public static void insertEvent(String input, boolean isMark) { -======= - public static void insertEvent(String input) { ->>>>>>> branch-Level-6 int idx = input.indexOf("/from"); int idx1 = input.indexOf("/to"); String desc = input.substring(5, idx); @@ -249,7 +220,7 @@ public static void insertEvent(String input) { String end = input.substring(idx1 + 3); Event tsk = null; try { - tsk = new Event(desc, false, start, end); + tsk = new Event(desc, isMark, start, end); addTaskPrint(tasks, tsk); } catch (DukeException de) { From 804bf2e6fdedc260d584e72d7c527c618d1fbe24 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Thu, 16 Feb 2023 02:39:39 +0800 Subject: [PATCH 15/26] Merge Conflicts with branch 6 and 7 --- src/main/java/duke/Duke.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 0b99e64e9..e1fee8dc0 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -73,12 +73,6 @@ public static void main(String[] args) { case "deadline": insertDeadline(input, false); break; - case "event": - insertEvent(input); - break; - case "deadline": - insertDeadline(input); - break; case "list": listOut(userInputs, tasks); break; From d9dc7c1e2952ca44d222cba60387d5ca0a43a122 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Thu, 16 Feb 2023 02:42:32 +0800 Subject: [PATCH 16/26] Code Cleanup --- src/main/java/duke/Duke.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index e1fee8dc0..90588c930 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -74,13 +74,15 @@ public static void main(String[] args) { insertDeadline(input, false); break; case "list": - listOut(userInputs, tasks); + listOut(); break; case "mark": markTask(splitInput); + saveTasks(); break; case "unmark": unMarkTask(splitInput); + saveTasks(); break; case "delete": deleteTask(splitInput); @@ -231,7 +233,7 @@ public static void addToList(String cmd, ArrayList userInputs) { /* This method lists out the tasks in order */ - public static void listOut(ArrayList userInputs, ArrayList tasks) { + public static void listOut() { System.out.println("\t____________________________________________________________"); System.out.println("Here are the tasks in your list:"); for (int i = 0; i < tasks.size(); i++) { From 99237ca3831e02769922411eb1580e7d7e92b8ce Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Mar 2023 03:49:05 +0800 Subject: [PATCH 17/26] A-MoreOOP --- src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/duke/Commands/Command.java | 32 ++ .../java/duke/Commands/DeadLineCommand.java | 27 + .../java/duke/Commands/DeleteCommand.java | 25 + src/main/java/duke/Commands/EventCommand.java | 28 + src/main/java/duke/Commands/ExitCommand.java | 14 + src/main/java/duke/Commands/ListCommand.java | 16 + src/main/java/duke/Commands/MarkCommand.java | 22 + src/main/java/duke/Commands/TempDuke.java | 43 ++ src/main/java/duke/Commands/ToDoCommand.java | 21 + .../java/duke/Commands/UnMarkCommand.java | 21 + src/main/java/duke/Duke.java | 531 ++++++++++-------- src/main/java/duke/Parser.java | 93 +++ src/main/java/duke/Storage.java | 96 ++++ src/main/java/duke/TaskList.java | 28 + src/main/java/duke/Ui.java | 40 ++ src/main/java/tasks/Deadline.java | 4 + src/main/java/tasks/Event.java | 6 + src/main/java/tasks/Task.java | 3 + 19 files changed, 805 insertions(+), 248 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/duke/Commands/Command.java create mode 100644 src/main/java/duke/Commands/DeadLineCommand.java create mode 100644 src/main/java/duke/Commands/DeleteCommand.java create mode 100644 src/main/java/duke/Commands/EventCommand.java create mode 100644 src/main/java/duke/Commands/ExitCommand.java create mode 100644 src/main/java/duke/Commands/ListCommand.java create mode 100644 src/main/java/duke/Commands/MarkCommand.java create mode 100644 src/main/java/duke/Commands/TempDuke.java create mode 100644 src/main/java/duke/Commands/ToDoCommand.java create mode 100644 src/main/java/duke/Commands/UnMarkCommand.java create mode 100644 src/main/java/duke/Parser.java create mode 100644 src/main/java/duke/Storage.java create mode 100644 src/main/java/duke/TaskList.java create mode 100644 src/main/java/duke/Ui.java diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..2c9a9745c --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.Duke + diff --git a/src/main/java/duke/Commands/Command.java b/src/main/java/duke/Commands/Command.java new file mode 100644 index 000000000..40895b78e --- /dev/null +++ b/src/main/java/duke/Commands/Command.java @@ -0,0 +1,32 @@ +package duke.Commands; + +import duke.Storage; +import duke.TaskList; +import dukeException.DukeException; +import tasks.Task; + +import static duke.Duke.LINE_SPACING; + +public class Command { + protected TaskList tasks = new TaskList(); + protected boolean isExit = false; + public static void addTaskPrint(TaskList tasks, Task tsk) { + System.out.println(LINE_SPACING); + System.out.println("\tGot it. I've added this task:"); + System.out.println("\t " + tsk.toString()); + System.out.println("\tNow you have " + tasks.getSize() + " tasks in the list."); + System.out.println(LINE_SPACING); + Storage.saveTasks(tasks); + + } + public Task getTask(int idx) { + return tasks.getTask(idx); + } + public void cmd() throws DukeException { + System.out.println("Please enter in a valid command"); + } + + public boolean isExit() { + return isExit; + } +} diff --git a/src/main/java/duke/Commands/DeadLineCommand.java b/src/main/java/duke/Commands/DeadLineCommand.java new file mode 100644 index 000000000..4840cf720 --- /dev/null +++ b/src/main/java/duke/Commands/DeadLineCommand.java @@ -0,0 +1,27 @@ +package duke.Commands; + +import duke.Storage; +import dukeException.DukeException; +import tasks.Deadline; + +public class DeadLineCommand extends Command { + private Deadline deadline; + public static final String COMMAND_WORD = "deadline"; + private String desc; + private boolean isMark; + private String by; + + public DeadLineCommand(String desc, boolean isMark, String by) { + this.desc = desc; + this.isMark = isMark; + this.by = by; + } + public void cmd() throws DukeException { + + Deadline dL = new Deadline(this.desc, this.isMark, this.by); + tasks.add(dL); + addTaskPrint(tasks, dL); + Storage.saveTasks(tasks); + + } +} diff --git a/src/main/java/duke/Commands/DeleteCommand.java b/src/main/java/duke/Commands/DeleteCommand.java new file mode 100644 index 000000000..bb9b4dc20 --- /dev/null +++ b/src/main/java/duke/Commands/DeleteCommand.java @@ -0,0 +1,25 @@ +package duke.Commands; + +import duke.Storage; +import tasks.Deadline; +import tasks.Task; + +public class DeleteCommand extends Command { + public static final String COMMAND_WORD = "delete"; + private int dIdx; + public DeleteCommand(int dIdx) { + this.dIdx = dIdx; + } + public void cmd(int dIdx) { + try { + Task tmp = tasks.getTask(dIdx); + tasks.delete(dIdx); + System.out.println("\tNoted. I've removed this task:"); + System.out.println("\t " + tmp); + System.out.println("\tNow you have " + tasks.getSize() + " tasks in the list."); + Storage.saveTasks(tasks); + } catch (IndexOutOfBoundsException ioe) { + System.out.println("OOPS! Item does not exist, try a different index"); + } + } +} diff --git a/src/main/java/duke/Commands/EventCommand.java b/src/main/java/duke/Commands/EventCommand.java new file mode 100644 index 000000000..6e94f0dad --- /dev/null +++ b/src/main/java/duke/Commands/EventCommand.java @@ -0,0 +1,28 @@ +package duke.Commands; + +import duke.Storage; +import dukeException.DukeException; +import tasks.Deadline; +import tasks.Event; + +public class EventCommand extends Command { + private Event event; + public static final String COMMAND_WORD = "event"; + private String desc; + private boolean isMark; + private String start; + private String end; + public EventCommand(String desc, boolean isMark, String start, String end) { + this.desc = desc; + this.isMark = isMark; + this.start = start; + this.end = end; + } + public void cmd() throws DukeException { + + Event event = new Event(this.desc, this.isMark, this.start, this.end); + tasks.add(event); + addTaskPrint(tasks, event); + Storage.saveTasks(tasks); + } +} diff --git a/src/main/java/duke/Commands/ExitCommand.java b/src/main/java/duke/Commands/ExitCommand.java new file mode 100644 index 000000000..aeb8489ca --- /dev/null +++ b/src/main/java/duke/Commands/ExitCommand.java @@ -0,0 +1,14 @@ +package duke.Commands; + +import static duke.Duke.LINE_SPACING; + +public class ExitCommand extends Command { + public static final String COMMAND_WORD = "exit"; + public void cmd() { + System.out.println(LINE_SPACING); + System.out.println("\tBye. Hope to see you again soon!"); + System.out.println(LINE_SPACING); + isExit = true; + } + +} diff --git a/src/main/java/duke/Commands/ListCommand.java b/src/main/java/duke/Commands/ListCommand.java new file mode 100644 index 000000000..40541cf48 --- /dev/null +++ b/src/main/java/duke/Commands/ListCommand.java @@ -0,0 +1,16 @@ +package duke.Commands; + +import static duke.Duke.LINE_SPACING; + +public class ListCommand extends Command{ + public static final String COMMAND_WORD = "list"; + public void cmd() { + System.out.println(LINE_SPACING); + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < tasks.getSize(); i++) { + System.out.println("\t" + (i + 1) + "." + tasks.getTask(i)); + } + System.out.println(LINE_SPACING); + } + +} diff --git a/src/main/java/duke/Commands/MarkCommand.java b/src/main/java/duke/Commands/MarkCommand.java new file mode 100644 index 000000000..85149c21e --- /dev/null +++ b/src/main/java/duke/Commands/MarkCommand.java @@ -0,0 +1,22 @@ +package duke.Commands; + +import duke.Storage; + +import static duke.Duke.LINE_SPACING; + +public class MarkCommand extends Command{ + public static final String COMMAND_WORD = "mark"; + private int idx; + public MarkCommand(int idx) { + this.idx = idx; + } + public void cmd() { + tasks.getTask(this.idx).mark(); + System.out.println(LINE_SPACING); + System.out.println("\tNice! I've marked this task as done:"); + System.out.println("\t " + tasks.getTask(this.idx)); + System.out.println(LINE_SPACING); + Storage.saveTasks(tasks); + } + +} diff --git a/src/main/java/duke/Commands/TempDuke.java b/src/main/java/duke/Commands/TempDuke.java new file mode 100644 index 000000000..f3d5d649e --- /dev/null +++ b/src/main/java/duke/Commands/TempDuke.java @@ -0,0 +1,43 @@ +package duke.Commands; + +import duke.Parser; +import duke.Storage; +import duke.TaskList; +import duke.Ui; +import dukeException.DukeException; + +public class TempDuke extends Command{ + + private Storage storage; + public static final String LINE_SPACING = "\t____________________________________________________________"; + + private Ui ui; + + public TempDuke(String filePath) { + ui = new Ui(); + storage = new Storage(tasks); + } + + public void run() { + ui.greetUser(); + boolean isExit = false; + while (!isExit) { + try { + String fullCommand = ui.readCommand(); + ui.showLine(); // show the divider line ("_______") + Command c = Parser.parse(fullCommand); + c.cmd(); + isExit = c.isExit(); + } catch (DukeException e) { + ui.showError(e.getMessage()); + } finally { + ui.showLine(); + } + } + } + + public static void main(String[] args) { + + new TempDuke("./data/duke.txt").run(); + } +} diff --git a/src/main/java/duke/Commands/ToDoCommand.java b/src/main/java/duke/Commands/ToDoCommand.java new file mode 100644 index 000000000..b6ce884f4 --- /dev/null +++ b/src/main/java/duke/Commands/ToDoCommand.java @@ -0,0 +1,21 @@ +package duke.Commands; + +import duke.Storage; +import dukeException.DukeException; +import tasks.Todo; + +public class ToDoCommand extends Command { + public static final String COMMAND_WORD = "todo"; + private String desc; + private boolean isMark; + public ToDoCommand(String desc, boolean isMark) { + this.desc = desc; + this.isMark = isMark; + } + public void cmd() throws DukeException { + Todo todo = new Todo(this.desc, this.isMark); + tasks.add(todo); + addTaskPrint(tasks, todo); + Storage.saveTasks(tasks); + } +} diff --git a/src/main/java/duke/Commands/UnMarkCommand.java b/src/main/java/duke/Commands/UnMarkCommand.java new file mode 100644 index 000000000..7f23146c5 --- /dev/null +++ b/src/main/java/duke/Commands/UnMarkCommand.java @@ -0,0 +1,21 @@ +package duke.Commands; + +import duke.Storage; + +import static duke.Duke.LINE_SPACING; + +public class UnMarkCommand extends Command { + public static final String COMMAND_WORD = "unmark"; + private int idx; + public UnMarkCommand(int idx) { + this.idx = idx; + } + public void cmd() { + tasks.getTask(this.idx).unMark(); + System.out.println(LINE_SPACING); + System.out.println("\tOK, I've marked this task as not done yet:"); + System.out.println("\t " + tasks.getTask(this.idx)); + System.out.println(LINE_SPACING); + Storage.saveTasks(tasks); + } +} diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 90588c930..a3e0e2b51 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,263 +1,298 @@ -package duke; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.Scanner; +//package duke; +// +//import java.io.File; +//import java.io.FileWriter; +//import java.io.IOException; +//import java.nio.charset.StandardCharsets; +//import java.nio.file.Files; +//import java.nio.file.Path; +//import java.nio.file.Paths; +//import java.util.ArrayList; +// +//import tasks.Task; +//import tasks.Deadline; +//import tasks.Event; +//import tasks.Todo; +// +//import dukeException.DukeException; +// +//public class Duke { +// static ArrayList tasks = new ArrayList(); +// public static final String LINE_SPACING = "\t____________________________________________________________"; +// public static void run() { +// Ui userUi = new Ui(); +// userUi.greetUser(); +// +// } +// /* +// Main function that takes user input and interpets how to store and what to do with it +// */ +// public static void main(String[] args) { +// +// Ui userUi = new Ui(); +// userUi.greetUser(); +// try { +// Files.createDirectories(Path.of("./data")); +// +// File myObj = new File("./data/duke.txt"); +// if (myObj.createNewFile()) { +// System.out.println("File created: " + myObj.getName()); +// } else { +// readDukeText(); +// } +// } catch (IOException e) { +// System.out.println("An error occurred."); +// e.printStackTrace(); +// } +// +// greetUser(); +// +// while (true) { +// +// String input = userUi.readInput(); +// String[] splitInput = input.split(" "); +// int taskSize = tasks.size(); +// switch (splitInput[0]) { +// case "bye": +// exit(); +// return; +// case "todo": +// insertTodo(input, false); +// break; +// case "event": +// insertEvent(input, false); +// break; +// case "deadline": +// insertDeadline(input, false); +// break; +// case "list": +// listOut(); +// break; +// case "mark": +// markTask(splitInput); +// saveTasks(); +// break; +// case "unmark": +// unMarkTask(splitInput); +// saveTasks(); +// break; +// case "delete": +// deleteTask(splitInput); +// break; +// default: +// System.out.println(LINE_SPACING); +// System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");; +// System.out.println(LINE_SPACING); +// break; +// } +// if (taskSize != tasks.size()) { +// saveTasks(); +// } +// } +// } +// public static void saveTasks() { +// try { +// FileWriter myWriter = new FileWriter("./data/duke.txt"); +// myWriter.flush(); +// for (Task task : tasks) { +// myWriter.write(task.toString() + "\n"); +// } +// myWriter.close(); +// System.out.println("Successfully wrote to the file."); +// } catch (IOException e) { +// System.out.println("An error occurred."); +// e.printStackTrace(); +// } +// } +// public static void readDukeText() { +// +// try { +// ArrayList tmpStrTasks = new ArrayList<>(); +// tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get("./data/duke.txt"), StandardCharsets.UTF_8); +// for (int i = 0; i < tmpStrTasks.size(); i++) { +// System.out.println(tmpStrTasks.get(i).substring(7)); +// } +// for (int i = 0; i < tmpStrTasks.size(); i++) { +// String newTask = tmpStrTasks.get(i); +// Task tsk = null; +// boolean isMark = newTask.charAt(4) == 'X'; +// switch (newTask.charAt(1)) { +// case 'T': +// insertTodo("todo " + newTask.substring(7), isMark); +// break; +// case 'D': +// insertDeadline("deadline " + newTask.substring(7), isMark); +// break; +// case 'E': +// insertEvent("event " + newTask.substring(7), isMark); +// break; +// } +// System.out.println(newTask); +// } +// } catch (IOException ioe) { +// ioe.printStackTrace(); +// } +// } +// +// +// public static void deleteTask(String[] splitInput) { +// String tmpTask = tasks.get(Integer.parseInt(splitInput[1]) - 1).toString(); +// tasks.remove(tasks.get(Integer.parseInt(splitInput[1]) - 1)); +// System.out.println("\tNoted. I've removed this task:"); +// System.out.println("\t " + tmpTask); +// System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); +// } +// public static void unMarkTask(String[] splitInput) { +// tasks.get(Integer.parseInt(splitInput[1]) - 1).unMark(); +// System.out.println(LINE_SPACING); +// System.out.println("\tOK, I've marked this task as not done yet:"); +// System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); +// System.out.println(LINE_SPACING); +// } +// public static void markTask(String[] splitInput) { +// tasks.get(Integer.parseInt(splitInput[1]) - 1).mark(); +// System.out.println(LINE_SPACING); +// System.out.println("\tNice! I've marked this task as done:"); +// System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); +// System.out.println(LINE_SPACING); +// } +// public static void insertTodo(String input, boolean isMark) { +// +// try { +// Task tsk = new Todo(input.substring(5), isMark); +// tasks.add(tsk); +// addTaskPrint(tasks, tsk); +// } catch (IndexOutOfBoundsException de) { +// printExceptionMsg("todo", "description of a todo cannot be empty."); +// } catch (DukeException de) { +// +// } +// } +// public static void printExceptionMsg(String task, String err) { +// System.out.println(LINE_SPACING); +// System.out.println(" ☹ OOPS!!! The description of a todo cannot be empty."); +// System.out.println(LINE_SPACING); +// } +// public static void addTaskPrint(ArrayList tasks, Task tsk) { +// System.out.println(LINE_SPACING); +// System.out.println("\tGot it. I've added this task:"); +// System.out.println("\t " + tsk.toString()); +// System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); +// System.out.println(LINE_SPACING); +// } +// /* +// This Returns the input as a Deadline object +// */ +// public static void insertDeadline(String input, boolean isMark) { +// int idx = input.indexOf("/by"); +// String desc = input.substring(8, idx); +// String by = input.substring(idx + 3); +// Deadline tsk = null; +// try { +// tsk = new Deadline(desc, isMark, by); +// tasks.add(tsk); +// addTaskPrint(tasks, tsk); +// } catch (DukeException de) { +// +// } +// } +// /* +// This Returns the input as a Event object +// */ +// public static void insertEvent(String input, boolean isMark) { +// int idx = input.indexOf("/from"); +// int idx1 = input.indexOf("/to"); +// String desc = input.substring(5, idx); +// String start = input.substring(idx + 5, idx1); +// String end = input.substring(idx1 + 3); +// Event tsk = null; +// try { +// tsk = new Event(desc, isMark, start, end); +// tasks.add(tsk); +// addTaskPrint(tasks, tsk); +// } catch (DukeException de) { +// +// } +// } +// /* +// This Adds the input to an input array for the ability to keep track of +// */ +// public static void addToList(String cmd, ArrayList userInputs) { +// userInputs.add(cmd); +// userInputs.set(userInputs.size() - 1, userInputs.size() + ". [ ] " + userInputs.get(userInputs.size() - 1)); +// } +// /* +// This method lists out the tasks in order +// */ +// public static void listOut() { +// System.out.println(LINE_SPACING); +// System.out.println("Here are the tasks in your list:"); +// for (int i = 0; i < tasks.size(); i++) { +// System.out.println("\t" + (i + 1) + "." + tasks.get(i)); +// } +// System.out.println(LINE_SPACING); +// } +// /* +// Automated greet function +// */ +// public static void greetUser() { +// System.out.println(LINE_SPACING); +// System.out.println("\tHello! I'm Duke"); +// System.out.println("\tWhat can I do for you?"); +// System.out.println(LINE_SPACING); +// } +// /* +// Exit message +// */ +// public static void exit() { +// System.out.println(LINE_SPACING); +// System.out.println("\tBye. Hope to see you again soon!"); +// System.out.println(LINE_SPACING); +// } +// +//} -import tasks.Task; -import tasks.Deadline; -import tasks.Event; -import tasks.Todo; +package duke; +import duke.Commands.Command; +import duke.Parser; +import duke.Storage; +import duke.TaskList; +import duke.Ui; import dukeException.DukeException; -import dukeException.DukeIOBException; -public class Duke { - static ArrayList tasks = new ArrayList(); - public Duke() { +public class Duke extends Command { - } - - /* - Main function that takes user input and interpets how to store and what to do with it - */ - public static void main(String[] args) { - - ArrayList userInputs = new ArrayList<>(); - ArrayList tasks = new ArrayList<>(); - Scanner scan = new Scanner(System.in); - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - - try { - Files.createDirectories(Path.of("./data")); - - File myObj = new File("./data/duke.txt"); - if (myObj.createNewFile()) { - System.out.println("File created: " + myObj.getName()); - } else { - readDukeText(); - } - } catch (IOException e) { - System.out.println("An error occurred."); - e.printStackTrace(); - } + private Storage storage; + public static final String LINE_SPACING = "\t____________________________________________________________"; - greetUser(); + private Ui ui; - while (true) { - String input = scan.nextLine(); - String[] splitInput = input.split(" "); - int taskSize = tasks.size(); - switch (splitInput[0]) { - case "bye": - exit(); - return; - case "todo": - insertTodo(input, false); - break; - case "event": - insertEvent(input, false); - break; - case "deadline": - insertDeadline(input, false); - break; - case "list": - listOut(); - break; - case "mark": - markTask(splitInput); - saveTasks(); - break; - case "unmark": - unMarkTask(splitInput); - saveTasks(); - break; - case "delete": - deleteTask(splitInput); - break; - default: - System.out.println("\t____________________________________________________________"); - System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");; - System.out.println("\t____________________________________________________________"); - break; - } - if (taskSize != tasks.size()) { - saveTasks(); - } - - } - } - public static void saveTasks() { - try { - FileWriter myWriter = new FileWriter("./data/duke.txt"); - myWriter.flush(); - for (Task task : tasks) { - myWriter.write(task.toString() + "\n"); - } - myWriter.close(); - System.out.println("Successfully wrote to the file."); - } catch (IOException e) { - System.out.println("An error occurred."); - e.printStackTrace(); - } + public Duke(String filePath) { + ui = new Ui(); + storage = new Storage(tasks); } - public static void readDukeText() { - try { - ArrayList tmpStrTasks = new ArrayList<>(); - tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get("./data/duke.txt"), StandardCharsets.UTF_8); - for (int i = 0; i < tmpStrTasks.size(); i++) { - System.out.println(tmpStrTasks.get(i).substring(7)); - } - for (int i = 0; i < tmpStrTasks.size(); i++) { - String newTask = tmpStrTasks.get(i); - Task tsk = null; - boolean isMark = newTask.charAt(4) == 'X'; - switch (newTask.charAt(1)) { - case 'T': - insertTodo("todo " + newTask.substring(7), isMark); - break; - case 'D': - insertDeadline("deadline " + newTask.substring(7), isMark); - break; - case 'E': - insertEvent("event " + newTask.substring(7), isMark); - break; - } - System.out.println(newTask); + public void run() { + ui.greetUser(); + boolean isExit = false; + while (!isExit) { + try { + String fullCommand = ui.readCommand(); + ui.showLine(); // show the divider line ("_______") + Command c = Parser.parse(fullCommand); + c.cmd(); + isExit = c.isExit(); + } catch (DukeException e) { + ui.showError(e.getMessage()); + } finally { + ui.showLine(); } - } catch (IOException ioe) { - ioe.printStackTrace(); } } + public static void main(String[] args) { - public static void deleteTask(String[] splitInput) { - String tmpTask = tasks.get(Integer.parseInt(splitInput[1]) - 1).toString(); - tasks.remove(tasks.get(Integer.parseInt(splitInput[1]) - 1)); - System.out.println("\tNoted. I've removed this task:"); - System.out.println("\t " + tmpTask); - System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); - } - public static void unMarkTask(String[] splitInput) { - tasks.get(Integer.parseInt(splitInput[1]) - 1).unMark(); - System.out.println("\t____________________________________________________________"); - System.out.println("\tOK, I've marked this task as not done yet:"); - System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); - System.out.println("\t____________________________________________________________"); - } - public static void markTask(String[] splitInput) { - tasks.get(Integer.parseInt(splitInput[1]) - 1).mark(); - System.out.println("\t____________________________________________________________"); - System.out.println("\tNice! I've marked this task as done:"); - System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); - System.out.println("\t____________________________________________________________"); - } - public static void insertTodo(String input, boolean isMark) { - - try { - Task tsk = new Todo(input.substring(5), isMark); - tasks.add(tsk); - addTaskPrint(tasks, tsk); - } catch (IndexOutOfBoundsException de) { - printExceptionMsg("todo", "description of a todo cannot be empty."); - } catch (DukeException de) { - - } - } - public static void printExceptionMsg(String task, String err) { - System.out.println("\t____________________________________________________________"); - System.out.println(" ☹ OOPS!!! The description of a todo cannot be empty."); - System.out.println("\t____________________________________________________________"); - } - public static void addTaskPrint(ArrayList tasks, Task tsk) { - System.out.println("\t____________________________________________________________"); - System.out.println("\tGot it. I've added this task:"); - System.out.println("\t " + tsk.toString()); - System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); - System.out.println("\t____________________________________________________________"); - } - /* - This Returns the input as a Deadline object - */ - public static void insertDeadline(String input, boolean isMark) { - int idx = input.indexOf("/by"); - String desc = input.substring(8, idx); - String by = input.substring(idx + 3); - Deadline tsk = null; - try { - tsk = new Deadline(desc, isMark, by); - addTaskPrint(tasks, tsk); - } catch (DukeException de) { - - } - tasks.add(tsk); - } - /* - This Returns the input as a Event object - */ - public static void insertEvent(String input, boolean isMark) { - int idx = input.indexOf("/from"); - int idx1 = input.indexOf("/to"); - String desc = input.substring(5, idx); - String start = input.substring(idx + 5, idx1); - String end = input.substring(idx1 + 3); - Event tsk = null; - try { - tsk = new Event(desc, isMark, start, end); - addTaskPrint(tasks, tsk); - } catch (DukeException de) { - - } - tasks.add(tsk); - } - /* - This Adds the input to an input array for the ability to keep track of - */ - public static void addToList(String cmd, ArrayList userInputs) { - userInputs.add(cmd); - userInputs.set(userInputs.size() - 1, userInputs.size() + ". [ ] " + userInputs.get(userInputs.size() - 1)); - } - /* - This method lists out the tasks in order - */ - public static void listOut() { - System.out.println("\t____________________________________________________________"); - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < tasks.size(); i++) { - System.out.println("\t" + (i + 1) + "." + tasks.get(i)); - } - System.out.println("\t____________________________________________________________"); - } - /* - Automated greet function - */ - public static void greetUser() { - System.out.println("\t____________________________________________________________"); - System.out.println("\tHello! I'm Duke"); - System.out.println("\tWhat can I do for you?"); - System.out.println("\t____________________________________________________________"); - } - /* - Exit message - */ - public static void exit() { - System.out.println("\t____________________________________________________________"); - System.out.println("\tBye. Hope to see you again soon!"); - System.out.println("\t____________________________________________________________"); - + new Duke("./data/duke.txt").run(); } - -} \ No newline at end of file +} diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 000000000..b7031a161 --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,93 @@ +package duke; + +import duke.Commands.*; +import dukeException.DukeException; +import tasks.Deadline; +import tasks.Event; + +public class Parser { + + public static Command parse(String command) { + + String[] cmds = command.split(" ", 2); + String cmd = cmds[0]; + Command tmpCommand = null; + boolean isParse = true; + System.out.println(cmd); + switch (cmd) { + case DeadLineCommand.COMMAND_WORD: + tmpCommand = parseDeadline(cmds[1]); + break; + case DeleteCommand.COMMAND_WORD: + tmpCommand = parseDelete(cmds[1]); + break; + case EventCommand.COMMAND_WORD: + tmpCommand = parseEvent(cmds[1]); + break; + case ExitCommand.COMMAND_WORD: + tmpCommand = parseExit(); + break; + case ListCommand.COMMAND_WORD: + tmpCommand = parseList(); + break; + case MarkCommand.COMMAND_WORD: + tmpCommand = parseMark(cmds[1]); + break; + case UnMarkCommand.COMMAND_WORD: + tmpCommand = parseUnMark(cmds[1]); + break; + + case ToDoCommand.COMMAND_WORD: + System.out.println(cmds[1]); + tmpCommand = parseToDo(cmds[1]); + break; + default: + Command c = new Command(); + return c; + } + + return tmpCommand; + + } + public static Command parseToDo(String input) { + return new ToDoCommand(input, false); + } + public static Command parseDeadline(String input) { + int idx = input.indexOf("/by"); + System.out.println(input); + String desc = input.substring(0, idx); + String by = input.substring(idx + 3); + Deadline tsk = null; + return new DeadLineCommand(desc, false, by); + } + + public static Command parseDelete(String input) { + int dIdx = Integer.parseInt(input); + return new DeleteCommand(dIdx-1); + } + public static Command parseEvent(String input) { + int idx = input.indexOf("/from"); + int idx1 = input.indexOf("/to"); + String desc = input.substring(0, idx); + String start = input.substring(idx + 5, idx1); + String end = input.substring(idx1 + 3); + return new EventCommand(desc, false, start, end); + } + public static Command parseExit() { + return new ExitCommand(); + } + public static Command parseList() { + return new ListCommand(); + } + public static Command parseMark(String input) { + int dIdx = Integer.parseInt(input); + return new MarkCommand(dIdx-1); + } + public static Command parseUnMark(String input) { + int dIdx = Integer.parseInt(input); + return new UnMarkCommand(dIdx-1); + } + + + +} diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java new file mode 100644 index 000000000..024b8b07e --- /dev/null +++ b/src/main/java/duke/Storage.java @@ -0,0 +1,96 @@ +package duke; + +import dukeException.DukeException; +import tasks.Deadline; +import tasks.Event; +import tasks.Task; +import tasks.Todo; + +import java.io.FileWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; + +public class Storage { + + + public Storage(TaskList tasks) { + readFile(tasks); + } + + + public static void saveTasks(TaskList tasks) { + + Storage.writeFile(tasks); + } + + public static void writeFile(TaskList tasks) { + try { + FileWriter myWriter = new FileWriter("./data/duke.txt"); + myWriter.flush(); + String strTask = ""; + for (int i = 0; i < tasks.getSize(); i++) { +// myWriter.write(tasks.getTask(i).toString() + "\n"); + if (tasks.getTask(i).toString().contains("[T]")) { + strTask = "todoSplitFactString" + tasks.getTask(i).getDescription() + + "SplitFactString" + tasks.getTask(i).getMarked(); + } else if (tasks.getTask(i).toString().contains("[D]")) { + Deadline dl = (Deadline) tasks.getTask(i); + strTask = "deadlineSplitFactString" + dl.getDescription() + + "SplitFactString" + dl.getMarked() + + "SplitFactString" + dl.getBy(); + } else if (tasks.getTask(i).toString().contains("[E]")) { + Event event = (Event) tasks.getTask(i); + strTask = "eventSplitFactString" + event.getDescription() + + "SplitFactString" + event.getMarked() + + "SplitFactString" + event.getStart() + + "SplitFactString" + event.getEnd(); + } + strTask += "\n"; + myWriter.write(strTask); + } + myWriter.close(); + System.out.println("Successfully wrote to the file."); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + } + public static void readFile(TaskList tasks) { + try { + ArrayList tmpStrTasks = new ArrayList<>(); + tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get("./data/duke.txt"), StandardCharsets.UTF_8); + + for (int i = 0; i < tmpStrTasks.size(); i++) { + String[] strTask = tmpStrTasks.get(i).split("SplitFactString"); + + boolean isMark = false; + if (tmpStrTasks.get(i).contains("[X]")) { + isMark = true; + } + switch (strTask[0]) { + case "todo": + Todo todo = new Todo(strTask[1], isMark); + tasks.add(todo); + +// insertTodo("todo " + newTask.substring(7), isMark); + break; + case "deadline": + Deadline dL = new Deadline(strTask[1], isMark, strTask[3]); + tasks.add(dL); +// insertDeadline("deadline " + newTask.substring(7), isMark); + break; + case "event": + + Event event = new Event(strTask[1], isMark, strTask[3], strTask[4]); +// insertEvent("event " + newTask.substring(7), isMark); + break; + } + } + } catch (IOException | DukeException ioe) { + ioe.printStackTrace(); + } + } +} diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java new file mode 100644 index 000000000..4340b4653 --- /dev/null +++ b/src/main/java/duke/TaskList.java @@ -0,0 +1,28 @@ +package duke; + +import tasks.Task; +import java.util.ArrayList; + +public class TaskList { + private static ArrayList tasks = new ArrayList(); + private static int size = 0; + public TaskList() { + + } + public Task getTask(int idx) { + return tasks.get(idx); + } + public void add(Task task) { + tasks.add(task); + size++; + } + public void delete(int idx) { + tasks.remove(idx); + size--; + } + public int getSize() { + return size; + } + + +} diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java new file mode 100644 index 000000000..57d59365a --- /dev/null +++ b/src/main/java/duke/Ui.java @@ -0,0 +1,40 @@ +package duke; + +import java.util.Scanner; + +public class Ui { + private final String LINE_SPACING = "\t____________________________________________________________"; + private Scanner scan; + public Ui() { + scan = new Scanner(System.in); + } + public void showLine() { + System.out.println(LINE_SPACING); + } + public void greetUser() { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); + + System.out.println(LINE_SPACING); + System.out.println("\tHello! I'm Duke"); + System.out.println("\tWhat can I do for you?"); + System.out.println(LINE_SPACING); + } + + public String readCommand() { + String input = scan.nextLine(); + return input; + } + + public void showError(String message) { + + } + + public void showLoadingError() { + + } +} diff --git a/src/main/java/tasks/Deadline.java b/src/main/java/tasks/Deadline.java index 740f6759a..94e7784d9 100644 --- a/src/main/java/tasks/Deadline.java +++ b/src/main/java/tasks/Deadline.java @@ -10,6 +10,10 @@ public Deadline(String description, boolean isMark, String by) throws DukeExcept super(description, isMark); this.by = by; } + public String getBy() { + return this.by; + } + @Override public String toString() { diff --git a/src/main/java/tasks/Event.java b/src/main/java/tasks/Event.java index 6245d4dca..bb8c32c11 100644 --- a/src/main/java/tasks/Event.java +++ b/src/main/java/tasks/Event.java @@ -13,6 +13,12 @@ public Event(String description, boolean isMark, String start, String end) throw this.start = start; this.end = end; } + public String getStart() { + return this.start; + } + public String getEnd() { + return this.end; + } @Override public String toString() { diff --git a/src/main/java/tasks/Task.java b/src/main/java/tasks/Task.java index 1f435fae6..af6c6532e 100644 --- a/src/main/java/tasks/Task.java +++ b/src/main/java/tasks/Task.java @@ -25,6 +25,9 @@ public String getMarked() { return "[ ]"; } } + public String getDescription() { + return this.description; + } public String toString() { return this.getMarked() + " " + this.description; From f9bb7fe161ebaca7697b4db07461bdbabc55fa92 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:18:54 +0800 Subject: [PATCH 18/26] Code Cleanup --- src/main/java/duke/Commands/Command.java | 3 -- .../java/duke/Commands/DeleteCommand.java | 8 ++-- src/main/java/duke/Commands/ListCommand.java | 2 - src/main/java/duke/Commands/MarkCommand.java | 4 +- src/main/java/duke/Commands/TempDuke.java | 43 ------------------- .../java/duke/Commands/UnMarkCommand.java | 2 - src/main/java/duke/Duke.java | 2 +- src/main/java/duke/Parser.java | 10 ++--- src/main/java/duke/Storage.java | 3 +- 9 files changed, 12 insertions(+), 65 deletions(-) delete mode 100644 src/main/java/duke/Commands/TempDuke.java diff --git a/src/main/java/duke/Commands/Command.java b/src/main/java/duke/Commands/Command.java index 40895b78e..d4172f681 100644 --- a/src/main/java/duke/Commands/Command.java +++ b/src/main/java/duke/Commands/Command.java @@ -15,9 +15,6 @@ public static void addTaskPrint(TaskList tasks, Task tsk) { System.out.println("\tGot it. I've added this task:"); System.out.println("\t " + tsk.toString()); System.out.println("\tNow you have " + tasks.getSize() + " tasks in the list."); - System.out.println(LINE_SPACING); - Storage.saveTasks(tasks); - } public Task getTask(int idx) { return tasks.getTask(idx); diff --git a/src/main/java/duke/Commands/DeleteCommand.java b/src/main/java/duke/Commands/DeleteCommand.java index bb9b4dc20..2c54353e3 100644 --- a/src/main/java/duke/Commands/DeleteCommand.java +++ b/src/main/java/duke/Commands/DeleteCommand.java @@ -10,12 +10,12 @@ public class DeleteCommand extends Command { public DeleteCommand(int dIdx) { this.dIdx = dIdx; } - public void cmd(int dIdx) { + public void cmd() { try { - Task tmp = tasks.getTask(dIdx); - tasks.delete(dIdx); + Task tmp = tasks.getTask(this.dIdx); + tasks.delete(this.dIdx); System.out.println("\tNoted. I've removed this task:"); - System.out.println("\t " + tmp); + System.out.println("\t " + tmp.getDescription()); System.out.println("\tNow you have " + tasks.getSize() + " tasks in the list."); Storage.saveTasks(tasks); } catch (IndexOutOfBoundsException ioe) { diff --git a/src/main/java/duke/Commands/ListCommand.java b/src/main/java/duke/Commands/ListCommand.java index 40541cf48..8eab34974 100644 --- a/src/main/java/duke/Commands/ListCommand.java +++ b/src/main/java/duke/Commands/ListCommand.java @@ -5,12 +5,10 @@ public class ListCommand extends Command{ public static final String COMMAND_WORD = "list"; public void cmd() { - System.out.println(LINE_SPACING); System.out.println("Here are the tasks in your list:"); for (int i = 0; i < tasks.getSize(); i++) { System.out.println("\t" + (i + 1) + "." + tasks.getTask(i)); } - System.out.println(LINE_SPACING); } } diff --git a/src/main/java/duke/Commands/MarkCommand.java b/src/main/java/duke/Commands/MarkCommand.java index 85149c21e..f49fe99dd 100644 --- a/src/main/java/duke/Commands/MarkCommand.java +++ b/src/main/java/duke/Commands/MarkCommand.java @@ -12,10 +12,8 @@ public MarkCommand(int idx) { } public void cmd() { tasks.getTask(this.idx).mark(); - System.out.println(LINE_SPACING); - System.out.println("\tNice! I've marked this task as done:"); + System.out.println("\tNice! I'velis marked this task as done:"); System.out.println("\t " + tasks.getTask(this.idx)); - System.out.println(LINE_SPACING); Storage.saveTasks(tasks); } diff --git a/src/main/java/duke/Commands/TempDuke.java b/src/main/java/duke/Commands/TempDuke.java deleted file mode 100644 index f3d5d649e..000000000 --- a/src/main/java/duke/Commands/TempDuke.java +++ /dev/null @@ -1,43 +0,0 @@ -package duke.Commands; - -import duke.Parser; -import duke.Storage; -import duke.TaskList; -import duke.Ui; -import dukeException.DukeException; - -public class TempDuke extends Command{ - - private Storage storage; - public static final String LINE_SPACING = "\t____________________________________________________________"; - - private Ui ui; - - public TempDuke(String filePath) { - ui = new Ui(); - storage = new Storage(tasks); - } - - public void run() { - ui.greetUser(); - boolean isExit = false; - while (!isExit) { - try { - String fullCommand = ui.readCommand(); - ui.showLine(); // show the divider line ("_______") - Command c = Parser.parse(fullCommand); - c.cmd(); - isExit = c.isExit(); - } catch (DukeException e) { - ui.showError(e.getMessage()); - } finally { - ui.showLine(); - } - } - } - - public static void main(String[] args) { - - new TempDuke("./data/duke.txt").run(); - } -} diff --git a/src/main/java/duke/Commands/UnMarkCommand.java b/src/main/java/duke/Commands/UnMarkCommand.java index 7f23146c5..b83814d6b 100644 --- a/src/main/java/duke/Commands/UnMarkCommand.java +++ b/src/main/java/duke/Commands/UnMarkCommand.java @@ -12,10 +12,8 @@ public UnMarkCommand(int idx) { } public void cmd() { tasks.getTask(this.idx).unMark(); - System.out.println(LINE_SPACING); System.out.println("\tOK, I've marked this task as not done yet:"); System.out.println("\t " + tasks.getTask(this.idx)); - System.out.println(LINE_SPACING); Storage.saveTasks(tasks); } } diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index a3e0e2b51..ac67291a3 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -266,7 +266,7 @@ public class Duke extends Command { private Storage storage; public static final String LINE_SPACING = "\t____________________________________________________________"; - private Ui ui; + private final Ui ui; public Duke(String filePath) { ui = new Ui(); diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index b7031a161..8f017cb4a 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -13,14 +13,14 @@ public static Command parse(String command) { String cmd = cmds[0]; Command tmpCommand = null; boolean isParse = true; - System.out.println(cmd); + switch (cmd) { case DeadLineCommand.COMMAND_WORD: tmpCommand = parseDeadline(cmds[1]); break; case DeleteCommand.COMMAND_WORD: - tmpCommand = parseDelete(cmds[1]); - break; + tmpCommand = parseDelete(cmds[1]); + break; case EventCommand.COMMAND_WORD: tmpCommand = parseEvent(cmds[1]); break; @@ -36,14 +36,12 @@ public static Command parse(String command) { case UnMarkCommand.COMMAND_WORD: tmpCommand = parseUnMark(cmds[1]); break; - case ToDoCommand.COMMAND_WORD: System.out.println(cmds[1]); tmpCommand = parseToDo(cmds[1]); break; default: - Command c = new Command(); - return c; + return new Command(); } return tmpCommand; diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 024b8b07e..64b40f283 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -22,7 +22,8 @@ public Storage(TaskList tasks) { public static void saveTasks(TaskList tasks) { - + Ui ui = new Ui(); + ui.showLine(); Storage.writeFile(tasks); } From 73543ab5236ff31ec80b8e85a42f67ed2710b3b8 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:44:13 +0800 Subject: [PATCH 19/26] Level 9. Find and code clean up --- src/main/java/duke/Commands/FindCommand.java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/main/java/duke/Commands/FindCommand.java diff --git a/src/main/java/duke/Commands/FindCommand.java b/src/main/java/duke/Commands/FindCommand.java new file mode 100644 index 000000000..217d38fde --- /dev/null +++ b/src/main/java/duke/Commands/FindCommand.java @@ -0,0 +1,2 @@ +package duke.Commands;public class FindCommand { +} From cef26a2ee1b85a75a452e8c827fd5ea30fbce7df Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:46:17 +0800 Subject: [PATCH 20/26] Code Cleanup --- src/main/java/duke/Commands/FindCommand.java | 27 +++++++++- src/main/java/duke/Parser.java | 55 +++++++++++++------- 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/src/main/java/duke/Commands/FindCommand.java b/src/main/java/duke/Commands/FindCommand.java index 217d38fde..e362449d0 100644 --- a/src/main/java/duke/Commands/FindCommand.java +++ b/src/main/java/duke/Commands/FindCommand.java @@ -1,2 +1,27 @@ -package duke.Commands;public class FindCommand { +package duke.Commands; + +import duke.Ui; + +public class FindCommand extends Command { + public static final String COMMAND_WORD = "find"; + private String term; + public FindCommand(String term) { + this.term = term; + } + public void cmd() { + Ui ui = new Ui(); + ui.showLine(); + if (tasks.getSize() == 0) { + System.out.println("There are no tasks to search."); + } else { + System.out.println("\t Here are the matching tasks in your list:\n"); + for (int i = 0; i < tasks.getSize(); i++) { + if (tasks.getTask(i).getDescription().contains(this.term)) { + System.out.println(tasks.getTask(i)); + } + } + } + + } + } diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index 8f017cb4a..fa3fff2e6 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -13,33 +13,37 @@ public static Command parse(String command) { String cmd = cmds[0]; Command tmpCommand = null; boolean isParse = true; - + System.out.println("Here " + cmd); switch (cmd) { case DeadLineCommand.COMMAND_WORD: - tmpCommand = parseDeadline(cmds[1]); - break; + tmpCommand = parseDeadline(cmds[1]); + break; case DeleteCommand.COMMAND_WORD: tmpCommand = parseDelete(cmds[1]); break; case EventCommand.COMMAND_WORD: - tmpCommand = parseEvent(cmds[1]); - break; + tmpCommand = parseEvent(cmds[1]); + break; case ExitCommand.COMMAND_WORD: - tmpCommand = parseExit(); - break; + tmpCommand = parseExit(); + break; case ListCommand.COMMAND_WORD: - tmpCommand = parseList(); - break; + tmpCommand = parseList(); + break; case MarkCommand.COMMAND_WORD: - tmpCommand = parseMark(cmds[1]); - break; + tmpCommand = parseMark(cmds[1]); + break; case UnMarkCommand.COMMAND_WORD: - tmpCommand = parseUnMark(cmds[1]); - break; + tmpCommand = parseUnMark(cmds[1]); + break; case ToDoCommand.COMMAND_WORD: - System.out.println(cmds[1]); - tmpCommand = parseToDo(cmds[1]); - break; + tmpCommand = parseToDo(cmds[1]); + break; + case FindCommand.COMMAND_WORD: + System.out.println("Here in caseFind"); + + tmpCommand = parseFind(cmds[1]); + break; default: return new Command(); } @@ -47,9 +51,16 @@ public static Command parse(String command) { return tmpCommand; } + + public static Command parseFind(String input) { + System.out.println("Here in parseFind"); + return new FindCommand(input); + } + public static Command parseToDo(String input) { return new ToDoCommand(input, false); } + public static Command parseDeadline(String input) { int idx = input.indexOf("/by"); System.out.println(input); @@ -61,8 +72,9 @@ public static Command parseDeadline(String input) { public static Command parseDelete(String input) { int dIdx = Integer.parseInt(input); - return new DeleteCommand(dIdx-1); + return new DeleteCommand(dIdx - 1); } + public static Command parseEvent(String input) { int idx = input.indexOf("/from"); int idx1 = input.indexOf("/to"); @@ -71,21 +83,24 @@ public static Command parseEvent(String input) { String end = input.substring(idx1 + 3); return new EventCommand(desc, false, start, end); } + public static Command parseExit() { return new ExitCommand(); } + public static Command parseList() { return new ListCommand(); } + public static Command parseMark(String input) { int dIdx = Integer.parseInt(input); - return new MarkCommand(dIdx-1); + return new MarkCommand(dIdx - 1); } + public static Command parseUnMark(String input) { int dIdx = Integer.parseInt(input); - return new UnMarkCommand(dIdx-1); + return new UnMarkCommand(dIdx - 1); } - } From 5886c71eb006fc5f308ee15a6151227349750767 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Mar 2023 17:34:49 +0800 Subject: [PATCH 21/26] =?UTF-8?q?A-JavaDoc=20=E2=86=B3=20Add=20JavaDoc=20c?= =?UTF-8?q?omments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add JavaDoc comments to the code. --- src/main/java/duke/Commands/Command.java | 37 ++- .../java/duke/Commands/DeadLineCommand.java | 33 ++ .../java/duke/Commands/DeleteCommand.java | 23 ++ src/main/java/duke/Commands/EventCommand.java | 51 +++- src/main/java/duke/Commands/ExitCommand.java | 12 + src/main/java/duke/Commands/FindCommand.java | 21 +- src/main/java/duke/Commands/ListCommand.java | 14 +- src/main/java/duke/Commands/MarkCommand.java | 22 +- src/main/java/duke/Commands/ToDoCommand.java | 28 ++ .../java/duke/Commands/UnMarkCommand.java | 22 +- src/main/java/duke/Duke.java | 285 ++---------------- src/main/java/duke/Parser.java | 62 +++- src/main/java/duke/Storage.java | 53 ++-- src/main/java/duke/TaskList.java | 30 +- src/main/java/duke/Ui.java | 35 ++- 15 files changed, 430 insertions(+), 298 deletions(-) diff --git a/src/main/java/duke/Commands/Command.java b/src/main/java/duke/Commands/Command.java index d4172f681..a74c15be6 100644 --- a/src/main/java/duke/Commands/Command.java +++ b/src/main/java/duke/Commands/Command.java @@ -1,28 +1,63 @@ package duke.Commands; -import duke.Storage; import duke.TaskList; import dukeException.DukeException; import tasks.Task; import static duke.Duke.LINE_SPACING; +/** + * The Command class is an abstract class that serves as the base class for all types of commands in Duke. + */ public class Command { + + /** + * The list of tasks in Duke. + */ protected TaskList tasks = new TaskList(); + + /** + * A boolean flag that indicates whether the program should exit. + */ protected boolean isExit = false; + + /** + * Prints a message indicating that a task has been added to the task list. + * + * @param tasks The list of tasks in Duke. + * @param tsk The task that was added to the list. + */ public static void addTaskPrint(TaskList tasks, Task tsk) { System.out.println(LINE_SPACING); System.out.println("\tGot it. I've added this task:"); System.out.println("\t " + tsk.toString()); System.out.println("\tNow you have " + tasks.getSize() + " tasks in the list."); } + + /** + * Retrieves the task at the specified index in the list of tasks. + * + * @param idx The index of the task to retrieve. + * @return The task at the specified index. + */ public Task getTask(int idx) { return tasks.getTask(idx); } + + /** + * Executes the command. + * + * @throws DukeException If an error occurs while executing the command. + */ public void cmd() throws DukeException { System.out.println("Please enter in a valid command"); } + /** + * Returns a boolean flag indicating whether the program should exit. + * + * @return A boolean flag indicating whether the program should exit. + */ public boolean isExit() { return isExit; } diff --git a/src/main/java/duke/Commands/DeadLineCommand.java b/src/main/java/duke/Commands/DeadLineCommand.java index 4840cf720..b1454570a 100644 --- a/src/main/java/duke/Commands/DeadLineCommand.java +++ b/src/main/java/duke/Commands/DeadLineCommand.java @@ -5,17 +5,50 @@ import tasks.Deadline; public class DeadLineCommand extends Command { + /** + * The Deadline object to be added to the list of tasks. + */ private Deadline deadline; + + /** + * The command word for a DeadlineCommand object. + */ public static final String COMMAND_WORD = "deadline"; + + /** + * The description of the Deadline task. + */ private String desc; + + /** + * A boolean flag indicating whether the Deadline task is marked as done. + */ private boolean isMark; + + /** + * The deadline of the Deadline task. + */ private String by; + /** + * Constructs a DeadlineCommand object with the specified description, mark status, and deadline. + * + * @param desc The description of the Deadline task. + * @param isMark A boolean flag indicating whether the Deadline task is marked as done. + * @param by The deadline of the Deadline task. + */ public DeadLineCommand(String desc, boolean isMark, String by) { this.desc = desc; this.isMark = isMark; this.by = by; } + + /** + * Executes the DeadlineCommand by adding a new Deadline task to the list of tasks, printing a message to confirm + * the addition, and saving the tasks to the storage file. + * + * @throws DukeException If an error occurs while executing the command. + */ public void cmd() throws DukeException { Deadline dL = new Deadline(this.desc, this.isMark, this.by); diff --git a/src/main/java/duke/Commands/DeleteCommand.java b/src/main/java/duke/Commands/DeleteCommand.java index 2c54353e3..718dbf07f 100644 --- a/src/main/java/duke/Commands/DeleteCommand.java +++ b/src/main/java/duke/Commands/DeleteCommand.java @@ -4,12 +4,35 @@ import tasks.Deadline; import tasks.Task; +/* + * Subclass of Command for delete + */ public class DeleteCommand extends Command { + /** + * The keyword associated with the DeleteCommand. + */ public static final String COMMAND_WORD = "delete"; + + /** + * The index of the task to be deleted. + */ private int dIdx; + + /** + * Constructs a DeleteCommand object with the specified index of the task to be deleted. + * + * @param dIdx The index of the task to be deleted. + */ public DeleteCommand(int dIdx) { this.dIdx = dIdx; } + + /** + * Executes the DeleteCommand by removing the specified task from the task list. + * Also prints out a message indicating the task that was removed and the current number of tasks in the list. + * + * @throws IndexOutOfBoundsException if the specified index is out of bounds. + */ public void cmd() { try { Task tmp = tasks.getTask(this.dIdx); diff --git a/src/main/java/duke/Commands/EventCommand.java b/src/main/java/duke/Commands/EventCommand.java index 6e94f0dad..3d236e9d2 100644 --- a/src/main/java/duke/Commands/EventCommand.java +++ b/src/main/java/duke/Commands/EventCommand.java @@ -5,19 +5,62 @@ import tasks.Deadline; import tasks.Event; +/** + * Represents an EventCommand that is used to add an event to the task list. + * Inherits from the Command class. + */ public class EventCommand extends Command { + /** + * The Event object associated with the EventCommand. + */ private Event event; + + /** + * The keyword associated with the EventCommand. + */ public static final String COMMAND_WORD = "event"; - private String desc; - private boolean isMark; - private String start; - private String end; + + /** + * The description of the event. + */ + private final String desc; + + /** + * The completion status of the event. + */ + private final boolean isMark; + + /** + * The start time of the event. + */ + private final String start; + + /** + * The end time of the event. + */ + private final String end; + + /** + * Constructs an EventCommand object with the specified description, completion status, start and end times of the event. + * + * @param desc The description of the event. + * @param isMark The completion status of the event. + * @param start The start time of the event. + * @param end The end time of the event. + */ public EventCommand(String desc, boolean isMark, String start, String end) { this.desc = desc; this.isMark = isMark; this.start = start; this.end = end; } + + /** + * Executes the EventCommand by creating a new Event object with the specified description, completion status, start and end times, + * adding it to the task list, and printing out a message indicating that the event has been added to the list. + * + * @throws DukeException if there is an error creating the Event object. + */ public void cmd() throws DukeException { Event event = new Event(this.desc, this.isMark, this.start, this.end); diff --git a/src/main/java/duke/Commands/ExitCommand.java b/src/main/java/duke/Commands/ExitCommand.java index aeb8489ca..05f830b47 100644 --- a/src/main/java/duke/Commands/ExitCommand.java +++ b/src/main/java/duke/Commands/ExitCommand.java @@ -1,9 +1,21 @@ package duke.Commands; +/* + * ThRepresents the Exit Command that is used to terminate the program + * + */ + import static duke.Duke.LINE_SPACING; public class ExitCommand extends Command { + /** + * The keyword associated with the ExitCommand. + */ public static final String COMMAND_WORD = "exit"; + + /** + * Executes the ExitCommand by printing a farewell message and setting the isExit flag to true. + */ public void cmd() { System.out.println(LINE_SPACING); System.out.println("\tBye. Hope to see you again soon!"); diff --git a/src/main/java/duke/Commands/FindCommand.java b/src/main/java/duke/Commands/FindCommand.java index e362449d0..48a382ba3 100644 --- a/src/main/java/duke/Commands/FindCommand.java +++ b/src/main/java/duke/Commands/FindCommand.java @@ -2,12 +2,31 @@ import duke.Ui; +/* + * Represents the find command that extends the Command class*/ public class FindCommand extends Command { + /** + * The keyword associated with the FindCommand. + */ public static final String COMMAND_WORD = "find"; - private String term; + + /** + * The keyword to search for. + */ + private final String term; + + /** + * Constructs a FindCommand object with the specified keyword to search for. + * + * @param term The keyword to search for. + */ public FindCommand(String term) { this.term = term; } + + /** + * Executes the FindCommand by searching for tasks in the task list that contain the specified keyword and printing them out. + */ public void cmd() { Ui ui = new Ui(); ui.showLine(); diff --git a/src/main/java/duke/Commands/ListCommand.java b/src/main/java/duke/Commands/ListCommand.java index 8eab34974..f36c30b73 100644 --- a/src/main/java/duke/Commands/ListCommand.java +++ b/src/main/java/duke/Commands/ListCommand.java @@ -2,8 +2,20 @@ import static duke.Duke.LINE_SPACING; -public class ListCommand extends Command{ +/* + * Rerepsents a ListCommand that is sued to display all tasks + * inherits from Command + * + */ +public class ListCommand extends Command { + /** + * The keyword associated with the ListCommand. + */ public static final String COMMAND_WORD = "list"; + + /** + * Executes the ListCommand by displaying all tasks in the task list. + */ public void cmd() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < tasks.getSize(); i++) { diff --git a/src/main/java/duke/Commands/MarkCommand.java b/src/main/java/duke/Commands/MarkCommand.java index f49fe99dd..fa11c9d20 100644 --- a/src/main/java/duke/Commands/MarkCommand.java +++ b/src/main/java/duke/Commands/MarkCommand.java @@ -4,15 +4,33 @@ import static duke.Duke.LINE_SPACING; -public class MarkCommand extends Command{ +public class MarkCommand extends Command { + /** + * The keyword associated with the MarkCommand. + */ public static final String COMMAND_WORD = "mark"; + + /** + * The index of the task to be marked. + */ private int idx; + + /** + * Constructs a MarkCommand object with the specified index. + * + * @param idx The index of the task to be marked. + */ public MarkCommand(int idx) { this.idx = idx; } + + /** + * Executes the MarkCommand by marking the specified task as done. + * Saves the updated task list to the storage file. + */ public void cmd() { tasks.getTask(this.idx).mark(); - System.out.println("\tNice! I'velis marked this task as done:"); + System.out.println("\tNice! I've marked this task as done:"); System.out.println("\t " + tasks.getTask(this.idx)); Storage.saveTasks(tasks); } diff --git a/src/main/java/duke/Commands/ToDoCommand.java b/src/main/java/duke/Commands/ToDoCommand.java index b6ce884f4..9dcff5ae5 100644 --- a/src/main/java/duke/Commands/ToDoCommand.java +++ b/src/main/java/duke/Commands/ToDoCommand.java @@ -4,14 +4,42 @@ import dukeException.DukeException; import tasks.Todo; +/* + * Represents adding a todo task + * extends Command + * */ public class ToDoCommand extends Command { + /** + * The string representing the command word for this command. + */ public static final String COMMAND_WORD = "todo"; + + /** + * The description of the new Todo task. + */ private String desc; + + /** + * The completion status of the new Todo task. + */ private boolean isMark; + + /** + * Constructs a new ToDoCommand object. + * + * @param desc The description of the new Todo task. + * @param isMark The completion status of the new Todo task. + */ public ToDoCommand(String desc, boolean isMark) { this.desc = desc; this.isMark = isMark; } + + /** + * Executes the ToDoCommand to add a new Todo task to the task list and save the changes. + * + * @throws DukeException If there is an error adding the new Todo task to the task list. + */ public void cmd() throws DukeException { Todo todo = new Todo(this.desc, this.isMark); tasks.add(todo); diff --git a/src/main/java/duke/Commands/UnMarkCommand.java b/src/main/java/duke/Commands/UnMarkCommand.java index b83814d6b..88e936651 100644 --- a/src/main/java/duke/Commands/UnMarkCommand.java +++ b/src/main/java/duke/Commands/UnMarkCommand.java @@ -4,12 +4,32 @@ import static duke.Duke.LINE_SPACING; +/* + * Represents a Command to unmar a task + * inherits from Command + * */ public class UnMarkCommand extends Command { + /* + * The keyword that denotes the command to unmark a task*/ public static final String COMMAND_WORD = "unmark"; - private int idx; + /* + * The index to unmark + * */ + private final int idx; + + /** + * Creates an instance of UnMarkCommand. + * + * @param idx Index of the Task in the TaskList to be unmarked. + */ public UnMarkCommand(int idx) { this.idx = idx; } + + /** + * Unmarks a Task in the TaskList as not done yet. + * Prints the Task that has been unmarked and saves the updated TaskList to the storage file. + */ public void cmd() { tasks.getTask(this.idx).unMark(); System.out.println("\tOK, I've marked this task as not done yet:"); diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index ac67291a3..f08944ac1 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,257 +1,3 @@ -//package duke; -// -//import java.io.File; -//import java.io.FileWriter; -//import java.io.IOException; -//import java.nio.charset.StandardCharsets; -//import java.nio.file.Files; -//import java.nio.file.Path; -//import java.nio.file.Paths; -//import java.util.ArrayList; -// -//import tasks.Task; -//import tasks.Deadline; -//import tasks.Event; -//import tasks.Todo; -// -//import dukeException.DukeException; -// -//public class Duke { -// static ArrayList tasks = new ArrayList(); -// public static final String LINE_SPACING = "\t____________________________________________________________"; -// public static void run() { -// Ui userUi = new Ui(); -// userUi.greetUser(); -// -// } -// /* -// Main function that takes user input and interpets how to store and what to do with it -// */ -// public static void main(String[] args) { -// -// Ui userUi = new Ui(); -// userUi.greetUser(); -// try { -// Files.createDirectories(Path.of("./data")); -// -// File myObj = new File("./data/duke.txt"); -// if (myObj.createNewFile()) { -// System.out.println("File created: " + myObj.getName()); -// } else { -// readDukeText(); -// } -// } catch (IOException e) { -// System.out.println("An error occurred."); -// e.printStackTrace(); -// } -// -// greetUser(); -// -// while (true) { -// -// String input = userUi.readInput(); -// String[] splitInput = input.split(" "); -// int taskSize = tasks.size(); -// switch (splitInput[0]) { -// case "bye": -// exit(); -// return; -// case "todo": -// insertTodo(input, false); -// break; -// case "event": -// insertEvent(input, false); -// break; -// case "deadline": -// insertDeadline(input, false); -// break; -// case "list": -// listOut(); -// break; -// case "mark": -// markTask(splitInput); -// saveTasks(); -// break; -// case "unmark": -// unMarkTask(splitInput); -// saveTasks(); -// break; -// case "delete": -// deleteTask(splitInput); -// break; -// default: -// System.out.println(LINE_SPACING); -// System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");; -// System.out.println(LINE_SPACING); -// break; -// } -// if (taskSize != tasks.size()) { -// saveTasks(); -// } -// } -// } -// public static void saveTasks() { -// try { -// FileWriter myWriter = new FileWriter("./data/duke.txt"); -// myWriter.flush(); -// for (Task task : tasks) { -// myWriter.write(task.toString() + "\n"); -// } -// myWriter.close(); -// System.out.println("Successfully wrote to the file."); -// } catch (IOException e) { -// System.out.println("An error occurred."); -// e.printStackTrace(); -// } -// } -// public static void readDukeText() { -// -// try { -// ArrayList tmpStrTasks = new ArrayList<>(); -// tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get("./data/duke.txt"), StandardCharsets.UTF_8); -// for (int i = 0; i < tmpStrTasks.size(); i++) { -// System.out.println(tmpStrTasks.get(i).substring(7)); -// } -// for (int i = 0; i < tmpStrTasks.size(); i++) { -// String newTask = tmpStrTasks.get(i); -// Task tsk = null; -// boolean isMark = newTask.charAt(4) == 'X'; -// switch (newTask.charAt(1)) { -// case 'T': -// insertTodo("todo " + newTask.substring(7), isMark); -// break; -// case 'D': -// insertDeadline("deadline " + newTask.substring(7), isMark); -// break; -// case 'E': -// insertEvent("event " + newTask.substring(7), isMark); -// break; -// } -// System.out.println(newTask); -// } -// } catch (IOException ioe) { -// ioe.printStackTrace(); -// } -// } -// -// -// public static void deleteTask(String[] splitInput) { -// String tmpTask = tasks.get(Integer.parseInt(splitInput[1]) - 1).toString(); -// tasks.remove(tasks.get(Integer.parseInt(splitInput[1]) - 1)); -// System.out.println("\tNoted. I've removed this task:"); -// System.out.println("\t " + tmpTask); -// System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); -// } -// public static void unMarkTask(String[] splitInput) { -// tasks.get(Integer.parseInt(splitInput[1]) - 1).unMark(); -// System.out.println(LINE_SPACING); -// System.out.println("\tOK, I've marked this task as not done yet:"); -// System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); -// System.out.println(LINE_SPACING); -// } -// public static void markTask(String[] splitInput) { -// tasks.get(Integer.parseInt(splitInput[1]) - 1).mark(); -// System.out.println(LINE_SPACING); -// System.out.println("\tNice! I've marked this task as done:"); -// System.out.println("\t " + tasks.get(Integer.parseInt(splitInput[1]) - 1)); -// System.out.println(LINE_SPACING); -// } -// public static void insertTodo(String input, boolean isMark) { -// -// try { -// Task tsk = new Todo(input.substring(5), isMark); -// tasks.add(tsk); -// addTaskPrint(tasks, tsk); -// } catch (IndexOutOfBoundsException de) { -// printExceptionMsg("todo", "description of a todo cannot be empty."); -// } catch (DukeException de) { -// -// } -// } -// public static void printExceptionMsg(String task, String err) { -// System.out.println(LINE_SPACING); -// System.out.println(" ☹ OOPS!!! The description of a todo cannot be empty."); -// System.out.println(LINE_SPACING); -// } -// public static void addTaskPrint(ArrayList tasks, Task tsk) { -// System.out.println(LINE_SPACING); -// System.out.println("\tGot it. I've added this task:"); -// System.out.println("\t " + tsk.toString()); -// System.out.println("\tNow you have " + tasks.size() + " tasks in the list."); -// System.out.println(LINE_SPACING); -// } -// /* -// This Returns the input as a Deadline object -// */ -// public static void insertDeadline(String input, boolean isMark) { -// int idx = input.indexOf("/by"); -// String desc = input.substring(8, idx); -// String by = input.substring(idx + 3); -// Deadline tsk = null; -// try { -// tsk = new Deadline(desc, isMark, by); -// tasks.add(tsk); -// addTaskPrint(tasks, tsk); -// } catch (DukeException de) { -// -// } -// } -// /* -// This Returns the input as a Event object -// */ -// public static void insertEvent(String input, boolean isMark) { -// int idx = input.indexOf("/from"); -// int idx1 = input.indexOf("/to"); -// String desc = input.substring(5, idx); -// String start = input.substring(idx + 5, idx1); -// String end = input.substring(idx1 + 3); -// Event tsk = null; -// try { -// tsk = new Event(desc, isMark, start, end); -// tasks.add(tsk); -// addTaskPrint(tasks, tsk); -// } catch (DukeException de) { -// -// } -// } -// /* -// This Adds the input to an input array for the ability to keep track of -// */ -// public static void addToList(String cmd, ArrayList userInputs) { -// userInputs.add(cmd); -// userInputs.set(userInputs.size() - 1, userInputs.size() + ". [ ] " + userInputs.get(userInputs.size() - 1)); -// } -// /* -// This method lists out the tasks in order -// */ -// public static void listOut() { -// System.out.println(LINE_SPACING); -// System.out.println("Here are the tasks in your list:"); -// for (int i = 0; i < tasks.size(); i++) { -// System.out.println("\t" + (i + 1) + "." + tasks.get(i)); -// } -// System.out.println(LINE_SPACING); -// } -// /* -// Automated greet function -// */ -// public static void greetUser() { -// System.out.println(LINE_SPACING); -// System.out.println("\tHello! I'm Duke"); -// System.out.println("\tWhat can I do for you?"); -// System.out.println(LINE_SPACING); -// } -// /* -// Exit message -// */ -// public static void exit() { -// System.out.println(LINE_SPACING); -// System.out.println("\tBye. Hope to see you again soon!"); -// System.out.println(LINE_SPACING); -// } -// -//} - package duke; import duke.Commands.Command; @@ -261,18 +7,37 @@ import duke.Ui; import dukeException.DukeException; +/** + * Duke is a personal assistant chatbot that helps a user keep track of various tasks. + * The user can input tasks such as todo, deadline, and event, and Duke will manage them by adding, deleting, or marking + * them as done. Duke can also list all the tasks and find tasks that match certain keywords. Duke is able to save the + * tasks in a file and load them when the user runs the program again. + */ public class Duke extends Command { - private Storage storage; - public static final String LINE_SPACING = "\t____________________________________________________________"; + /** + * A constant string that represents the line spacing used in the output. + */ + public static final String LINE_SPACING = "\t____________________________________________________________"; + private Storage storage; private final Ui ui; + /** + * Creates a Duke object and initializes it with a Ui object and a Storage object. + * The storage object is created using the specified file path. + * + * @param filePath The file path of the file that stores the task list. + */ public Duke(String filePath) { ui = new Ui(); storage = new Storage(tasks); } + /** + * The main run loop of Duke. Displays a greeting message to the user and repeatedly prompts for user input, + * parsing the input into a command and executing it. The loop continues until the user types the "bye" command. + */ public void run() { ui.greetUser(); boolean isExit = false; @@ -284,15 +49,19 @@ public void run() { c.cmd(); isExit = c.isExit(); } catch (DukeException e) { - ui.showError(e.getMessage()); + System.out.println("error interrpetting input"); } finally { ui.showLine(); } } } + /** + * The entry point of the Duke application. + * + * @param args The command line arguments. + */ public static void main(String[] args) { - new Duke("./data/duke.txt").run(); } } diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index fa3fff2e6..a0d3280d1 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -5,8 +5,16 @@ import tasks.Deadline; import tasks.Event; +/** + * The Parser class is responsible for parsing user input and generating the appropriate commands for Duke to execute. + */ public class Parser { - + /** + * Parses the given command and generates the appropriate command for Duke to execute. + * + * @param command the command to parse + * @return the appropriate command for Duke to execute + */ public static Command parse(String command) { String[] cmds = command.split(" ", 2); @@ -52,15 +60,33 @@ public static Command parse(String command) { } + /** + * Parses the user input and generates a FindCommand for Duke to execute. + * + * @param input the user input + * @return a FindCommand for Duke to execute + */ public static Command parseFind(String input) { System.out.println("Here in parseFind"); return new FindCommand(input); } + /** + * Parses the user input and generates a ToDoCommand for Duke to execute. + * + * @param input the user input + * @return a ToDoCommand for Duke to execute + */ public static Command parseToDo(String input) { return new ToDoCommand(input, false); } + /** + * Parses the user input and generates a DeadLineCommand for Duke to execute. + * + * @param input the user input + * @return a DeadLineCommand for Duke to execute + */ public static Command parseDeadline(String input) { int idx = input.indexOf("/by"); System.out.println(input); @@ -70,11 +96,23 @@ public static Command parseDeadline(String input) { return new DeadLineCommand(desc, false, by); } + /** + * Parses the user input and generates a DeleteCommand for Duke to execute. + * + * @param input the user input + * @return a DeleteCommand for Duke to execute + */ public static Command parseDelete(String input) { int dIdx = Integer.parseInt(input); return new DeleteCommand(dIdx - 1); } + /** + * Parses the user input and generates an EventCommand for Duke to execute. + * + * @param input the user input + * @return an EventCommand for Duke to execute + */ public static Command parseEvent(String input) { int idx = input.indexOf("/from"); int idx1 = input.indexOf("/to"); @@ -84,19 +122,41 @@ public static Command parseEvent(String input) { return new EventCommand(desc, false, start, end); } + /** + * Parses the "exit" command and returns an instance of ExitCommand. + * + * @return An instance of ExitCommand. + */ public static Command parseExit() { return new ExitCommand(); } + /** + * Parses the "list" command and returns an instance of ListCommand. + * + * @return An instance of ListCommand. + */ public static Command parseList() { return new ListCommand(); } + /** + * Parses the "mark" command and returns an instance of MarkCommand. + * + * @param input The user input containing the index of the task to mark. + * @return An instance of MarkCommand. + */ public static Command parseMark(String input) { int dIdx = Integer.parseInt(input); return new MarkCommand(dIdx - 1); } + /** + * Parses the "unmark" command and returns an instance of UnMarkCommand. + * + * @param input The user input containing the index of the task to unmark. + * @return An instance of UnMarkCommand. + */ public static Command parseUnMark(String input) { int dIdx = Integer.parseInt(input); return new UnMarkCommand(dIdx - 1); diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 64b40f283..7c4f5e4c8 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -15,18 +15,32 @@ public class Storage { - + /** + * Constructor for Storage class. + * Reads file and populates TaskList object. + * + * @param tasks The TaskList object to be populated from the file. + */ public Storage(TaskList tasks) { readFile(tasks); } - + /** + * Writes the tasks in the TaskList object to a file. + * + * @param tasks The TaskList object to be written to a file. + */ public static void saveTasks(TaskList tasks) { Ui ui = new Ui(); ui.showLine(); Storage.writeFile(tasks); } + /** + * Writes the given TaskList object to a file. + * + * @param tasks The TaskList object to be written to a file. + */ public static void writeFile(TaskList tasks) { try { FileWriter myWriter = new FileWriter("./data/duke.txt"); @@ -59,35 +73,34 @@ public static void writeFile(TaskList tasks) { e.printStackTrace(); } } + + /** + * Reads from the file and populates the TaskList object. + * + * @param tasks The TaskList object to be populated from the file. + */ public static void readFile(TaskList tasks) { try { ArrayList tmpStrTasks = new ArrayList<>(); tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get("./data/duke.txt"), StandardCharsets.UTF_8); - for (int i = 0; i < tmpStrTasks.size(); i++) { String[] strTask = tmpStrTasks.get(i).split("SplitFactString"); - boolean isMark = false; if (tmpStrTasks.get(i).contains("[X]")) { isMark = true; } switch (strTask[0]) { - case "todo": - Todo todo = new Todo(strTask[1], isMark); - tasks.add(todo); - -// insertTodo("todo " + newTask.substring(7), isMark); - break; - case "deadline": - Deadline dL = new Deadline(strTask[1], isMark, strTask[3]); - tasks.add(dL); -// insertDeadline("deadline " + newTask.substring(7), isMark); - break; - case "event": - - Event event = new Event(strTask[1], isMark, strTask[3], strTask[4]); -// insertEvent("event " + newTask.substring(7), isMark); - break; + case "todo": + Todo todo = new Todo(strTask[1], isMark); + tasks.add(todo); + break; + case "deadline": + Deadline dL = new Deadline(strTask[1], isMark, strTask[3]); + tasks.add(dL); + break; + case "event": + Event event = new Event(strTask[1], isMark, strTask[3], strTask[4]); + break; } } } catch (IOException | DukeException ioe) { diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 4340b4653..a09b8ad0b 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -1,25 +1,53 @@ package duke; import tasks.Task; + import java.util.ArrayList; public class TaskList { private static ArrayList tasks = new ArrayList(); private static int size = 0; + + /** + * Constructs a new TaskList object. + */ public TaskList() { - + } + + /** + * Retrieves the task at the specified index. + * + * @param idx The index of the task to retrieve. + * @return The task at the specified index. + */ public Task getTask(int idx) { return tasks.get(idx); } + + /** + * Adds a new task to the task list. + * + * @param task The task to add to the task list. + */ public void add(Task task) { tasks.add(task); size++; } + + /** + * Deletes the task at the specified index. + * + * @param idx The index of the task to delete + */ public void delete(int idx) { tasks.remove(idx); size--; } + + /* + * returns the size of the list tasks + */ public int getSize() { return size; } diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index 57d59365a..2766cbead 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -2,15 +2,38 @@ import java.util.Scanner; +/* + * This class deals with user interaction + * displaying text and reading in text + * */ public class Ui { + /* + * a constant used for readability + */ private final String LINE_SPACING = "\t____________________________________________________________"; + /* + * A scanner for reading in user input + */ private Scanner scan; + + /* + * Initialized the scanner in the constructor for user input + * + */ public Ui() { scan = new Scanner(System.in); } + + /* + * Displays the line constant in the console + */ public void showLine() { System.out.println(LINE_SPACING); } + + /* + * Displays the intro message in a formatted way + */ public void greetUser() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -25,16 +48,12 @@ public void greetUser() { System.out.println(LINE_SPACING); } + /* + * reads in input + * @returns String of user input + */ public String readCommand() { String input = scan.nextLine(); return input; } - - public void showError(String message) { - - } - - public void showLoadingError() { - - } } From c12849d293ac802982f8e29e2d3f2840630e2a05 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Mar 2023 18:10:07 +0800 Subject: [PATCH 22/26] A-UserGuide --- docs/README.md | 241 ++++++++++++++++++++++- src/main/java/duke/Commands/Command.java | 1 - src/main/java/duke/Parser.java | 4 - 3 files changed, 233 insertions(+), 13 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..dda028d5e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,28 +2,253 @@ ## Features -### Feature-ABC +### Feature-todo -Description of the feature. +You can use this to create a new todo task with a description. -### Feature-XYZ +### Feature-deadline -Description of the feature. +You can use this to create a new deadline task with a description and a due by date. + +### Feature-event + +You can use this to create a new event task with a description and a /from time and a /to time. + +### Feature-list + +You can use this to list out all of the tasks, which will display if in order of and if it is marked as done. + +### Feature-mark + +You can use this feature to mark a task as done for good up-keeping. + +### Feature-unmark + +You can use this feature to un-mark a task as done for good up-keeping. + +### Feature-delete + +You can use this feature to remove a task from the list. + +### Feature-find + +You can use this feature to list out all of the tasks that match with a specified term. + +### Feature-Automatic Saving + +All modifications using the aforementioned features will be saved to your device in a text file automatically. ## Usage -### `Keyword` - Describe action +### `Keyword` - todo -Describe the action and its outcome. +Add a general task without a time frame. Example of usage: -`keyword (optional arguments)` +`todo take out the trash` + +Expected outcome: +``` + ____________________________________________________________ +todo take out the trash + ____________________________________________________________ + Got it. I've added this task: + [T][ ] take out the trash + Now you have 1 tasks in the list. + ____________________________________________________________ +Successfully wrote to the file. + ____________________________________________________________ +``` + +Description of the outcome. +``` + Reads in the todo and stores it in a list and saves to the file. +``` + +### `Keyword` - deadline + +Add a task with a deadlione. + +Example of usage: + +`deadline take out the trash /by 7pm` + +Expected outcome: +``` + ____________________________________________________________ +deadline take out the trash /by 7pm + ____________________________________________________________ +take out the trash /by 7pm + Got it. I've added this task: + [D][ ] take out the trash (by: 7pm) + Now you have 2 tasks in the list. + ____________________________________________________________ +Successfully wrote to the file. + ____________________________________________________________ + +``` + +Description of the outcome. +``` + Reads in the deadline and stores it in a list and saves to the file. +``` + +### `Keyword` - event + +Add a task with a deadlione. + +Example of usage: + +`event take out the trash /from 8am /to 10am` + +Expected outcome: +``` + ____________________________________________________________ + Got it. I've added this task: + [E][ ] take out the trash (from: 8am to: 10am) + Now you have 3 tasks in the list. + ____________________________________________________________ +Successfully wrote to the file. + ____________________________________________________________ + +``` + +Description of the outcome. +``` + Reads in the event and stores it in a list and saves to the file. +``` + +### `Keyword` - list + +Lists out all the entered in tasks and tasks loaded in at start. + +Example of usage: + +`list` + +Expected outcome: +``` +list + ____________________________________________________________ +Here are the tasks in your list: + 1.[T][ ] take out the trash + 2.[D][ ] take out the trash (by: 7pm) + 3.[E][ ] take out the trash (from: 8am to: 10am) + ____________________________________________________________ + +``` + +Description of the outcome. +``` + Lists out all the taskst with their descriptions, times, and if they are marked done or not. +``` + +### `Keyword` - mark + +Mark a task as done. + +Example of usage: + +`mark 3` Expected outcome: +``` +mark 3 + ____________________________________________________________ + Nice! I've marked this task as done: + [E][X] take out the trash (from: 8am to: 10am) + ____________________________________________________________ +Successfully wrote to the file. + ____________________________________________________________ + +``` Description of the outcome. +``` + Marked the task as done and displayed the marked task now denoted with '[X]' +``` +### `Keyword` - unmark + +unmark a task as not done. +Example of usage: + +`unmark 3` + +Expected outcome: +``` +unmark 3 + ____________________________________________________________ + OK, I've marked this task as not done yet: + [E][ ] take out the trash (from: 8am to: 10am) + ____________________________________________________________ +Successfully wrote to the file. + ____________________________________________________________ + +``` + +Description of the outcome. +``` + Marked the task as NOT done and displayed the marked task now denoted with '[ ]' ``` -expected output + +### `Keyword` - delete + +removes a task from the list from a specified index + +Example of usage: + +`delete 3` + +Expected outcome: +``` +delete 3 + ____________________________________________________________ + Noted. I've removed this task: + take out the trash + Now you have 2 tasks in the list. + ____________________________________________________________ +Successfully wrote to the file. + ____________________________________________________________ + +``` + +Description of the outcome. +``` + Deletes the specified task from the list. Echos what task it was. Resaves to the file because it was now changed. +``` + +### `Keyword` - find + +lists out all the tasks that match a specified term. + +Example of usage: + +`find trash` + +Expected outcome: +``` +list + ____________________________________________________________ +Here are the tasks in your list: + 1.[T][ ] take out the trash + 2.[D][ ] take out the trash (by: 7pm) + 3.[T][ ] find a book + ____________________________________________________________ +find trash + ____________________________________________________________ + ____________________________________________________________ + Here are the matching tasks in your list: + +[T][ ] take out the trash +[D][ ] take out the trash (by: 7pm) + ____________________________________________________________ + + +``` + +Description of the outcome. ``` + Finds all the tasks that match the specified query and lists them out. +``` \ No newline at end of file diff --git a/src/main/java/duke/Commands/Command.java b/src/main/java/duke/Commands/Command.java index a74c15be6..c8fa7176c 100644 --- a/src/main/java/duke/Commands/Command.java +++ b/src/main/java/duke/Commands/Command.java @@ -28,7 +28,6 @@ public class Command { * @param tsk The task that was added to the list. */ public static void addTaskPrint(TaskList tasks, Task tsk) { - System.out.println(LINE_SPACING); System.out.println("\tGot it. I've added this task:"); System.out.println("\t " + tsk.toString()); System.out.println("\tNow you have " + tasks.getSize() + " tasks in the list."); diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index a0d3280d1..54e3f37f5 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -21,7 +21,6 @@ public static Command parse(String command) { String cmd = cmds[0]; Command tmpCommand = null; boolean isParse = true; - System.out.println("Here " + cmd); switch (cmd) { case DeadLineCommand.COMMAND_WORD: tmpCommand = parseDeadline(cmds[1]); @@ -48,8 +47,6 @@ public static Command parse(String command) { tmpCommand = parseToDo(cmds[1]); break; case FindCommand.COMMAND_WORD: - System.out.println("Here in caseFind"); - tmpCommand = parseFind(cmds[1]); break; default: @@ -67,7 +64,6 @@ public static Command parse(String command) { * @return a FindCommand for Duke to execute */ public static Command parseFind(String input) { - System.out.println("Here in parseFind"); return new FindCommand(input); } From eee98c89d5bbcfcbdbb2226ee93d186db8545100 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Mar 2023 21:04:45 +0800 Subject: [PATCH 23/26] Code clean-up and error handling better --- src/main/java/duke/Commands/Command.java | 2 +- .../java/duke/Commands/DeadLineCommand.java | 2 - src/main/java/duke/Parser.java | 38 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/main/java/duke/Commands/Command.java b/src/main/java/duke/Commands/Command.java index c8fa7176c..8d8780c8c 100644 --- a/src/main/java/duke/Commands/Command.java +++ b/src/main/java/duke/Commands/Command.java @@ -49,7 +49,7 @@ public Task getTask(int idx) { * @throws DukeException If an error occurs while executing the command. */ public void cmd() throws DukeException { - System.out.println("Please enter in a valid command"); + System.out.println("Please enter in a valid command."); } /** diff --git a/src/main/java/duke/Commands/DeadLineCommand.java b/src/main/java/duke/Commands/DeadLineCommand.java index b1454570a..53f55e5fc 100644 --- a/src/main/java/duke/Commands/DeadLineCommand.java +++ b/src/main/java/duke/Commands/DeadLineCommand.java @@ -50,11 +50,9 @@ public DeadLineCommand(String desc, boolean isMark, String by) { * @throws DukeException If an error occurs while executing the command. */ public void cmd() throws DukeException { - Deadline dL = new Deadline(this.desc, this.isMark, this.by); tasks.add(dL); addTaskPrint(tasks, dL); Storage.saveTasks(tasks); - } } diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index 54e3f37f5..25bc62f23 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -74,6 +74,10 @@ public static Command parseFind(String input) { * @return a ToDoCommand for Duke to execute */ public static Command parseToDo(String input) { + if (input.equals("")) { + System.out.println("Todo description is empty :("); + return new Command(); + } return new ToDoCommand(input, false); } @@ -85,10 +89,17 @@ public static Command parseToDo(String input) { */ public static Command parseDeadline(String input) { int idx = input.indexOf("/by"); + if (idx == -1) { + System.out.println("Deadline by time is empty :("); + return new Command(); + } System.out.println(input); String desc = input.substring(0, idx); String by = input.substring(idx + 3); Deadline tsk = null; + if (desc.equals("")) { + System.out.println("Deadline description is empty :("); + } return new DeadLineCommand(desc, false, by); } @@ -99,6 +110,10 @@ public static Command parseDeadline(String input) { * @return a DeleteCommand for Duke to execute */ public static Command parseDelete(String input) { + if (input.equals("")) { + System.out.println("No valid index found"); + return new Command(); + } int dIdx = Integer.parseInt(input); return new DeleteCommand(dIdx - 1); } @@ -110,11 +125,26 @@ public static Command parseDelete(String input) { * @return an EventCommand for Duke to execute */ public static Command parseEvent(String input) { + if (input.equals("")) { + return new Command(); + } int idx = input.indexOf("/from"); + if (idx == -1) { + System.out.println("Event from time is empty :("); + return new Command(); + } int idx1 = input.indexOf("/to"); + if (idx1 == -1) { + System.out.println("Event to time is empty :("); + return new Command(); + } String desc = input.substring(0, idx); String start = input.substring(idx + 5, idx1); String end = input.substring(idx1 + 3); + if (desc.equals("")) { + System.out.println("Event description is empty :("); + return new Command(); + } return new EventCommand(desc, false, start, end); } @@ -143,6 +173,10 @@ public static Command parseList() { * @return An instance of MarkCommand. */ public static Command parseMark(String input) { + if (input.equals("")) { + System.out.println("No valid index found"); + return new Command(); + } int dIdx = Integer.parseInt(input); return new MarkCommand(dIdx - 1); } @@ -154,6 +188,10 @@ public static Command parseMark(String input) { * @return An instance of UnMarkCommand. */ public static Command parseUnMark(String input) { + if (input.equals("")) { + System.out.println("No valid index found"); + return new Command(); + } int dIdx = Integer.parseInt(input); return new UnMarkCommand(dIdx - 1); } From 1b6cb39fbd8845f2598f5ee2302356f873a059b6 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Fri, 3 Mar 2023 21:25:36 +0800 Subject: [PATCH 24/26] Coding Standard Upkeep --- src/main/java/duke/Commands/Command.java | 5 +++-- src/main/java/duke/Commands/DeadLineCommand.java | 6 ++++-- src/main/java/duke/Commands/DeleteCommand.java | 11 +++++++---- src/main/java/duke/Commands/EventCommand.java | 9 ++++++--- src/main/java/duke/Commands/ExitCommand.java | 1 - src/main/java/duke/Commands/FindCommand.java | 3 ++- src/main/java/duke/Commands/ListCommand.java | 1 - src/main/java/duke/Commands/ToDoCommand.java | 2 +- src/main/java/duke/Commands/UnMarkCommand.java | 8 ++++---- src/main/java/duke/Duke.java | 4 ++-- src/main/java/duke/Parser.java | 6 ++++++ src/main/java/duke/Storage.java | 4 ++-- src/main/java/duke/TaskList.java | 2 +- 13 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/main/java/duke/Commands/Command.java b/src/main/java/duke/Commands/Command.java index 8d8780c8c..213d14f65 100644 --- a/src/main/java/duke/Commands/Command.java +++ b/src/main/java/duke/Commands/Command.java @@ -7,7 +7,8 @@ import static duke.Duke.LINE_SPACING; /** - * The Command class is an abstract class that serves as the base class for all types of commands in Duke. + * The Command class is an abstract class that serves + * as the base class for all types of commands in Duke. */ public class Command { @@ -57,7 +58,7 @@ public void cmd() throws DukeException { * * @return A boolean flag indicating whether the program should exit. */ - public boolean isExit() { + public boolean getExit() { return isExit; } } diff --git a/src/main/java/duke/Commands/DeadLineCommand.java b/src/main/java/duke/Commands/DeadLineCommand.java index 53f55e5fc..19e064d66 100644 --- a/src/main/java/duke/Commands/DeadLineCommand.java +++ b/src/main/java/duke/Commands/DeadLineCommand.java @@ -31,7 +31,8 @@ public class DeadLineCommand extends Command { private String by; /** - * Constructs a DeadlineCommand object with the specified description, mark status, and deadline. + * Constructs a DeadlineCommand object with the specified + * description, mark status, and deadline. * * @param desc The description of the Deadline task. * @param isMark A boolean flag indicating whether the Deadline task is marked as done. @@ -44,7 +45,8 @@ public DeadLineCommand(String desc, boolean isMark, String by) { } /** - * Executes the DeadlineCommand by adding a new Deadline task to the list of tasks, printing a message to confirm + * Executes the DeadlineCommand by adding a new Deadline + * task to the list of tasks, printing a message to confirm * the addition, and saving the tasks to the storage file. * * @throws DukeException If an error occurs while executing the command. diff --git a/src/main/java/duke/Commands/DeleteCommand.java b/src/main/java/duke/Commands/DeleteCommand.java index 718dbf07f..f4bc132fb 100644 --- a/src/main/java/duke/Commands/DeleteCommand.java +++ b/src/main/java/duke/Commands/DeleteCommand.java @@ -4,7 +4,7 @@ import tasks.Deadline; import tasks.Task; -/* +/** * Subclass of Command for delete */ public class DeleteCommand extends Command { @@ -19,7 +19,8 @@ public class DeleteCommand extends Command { private int dIdx; /** - * Constructs a DeleteCommand object with the specified index of the task to be deleted. + * Constructs a DeleteCommand object with the specified + * index of the task to be deleted. * * @param dIdx The index of the task to be deleted. */ @@ -28,8 +29,10 @@ public DeleteCommand(int dIdx) { } /** - * Executes the DeleteCommand by removing the specified task from the task list. - * Also prints out a message indicating the task that was removed and the current number of tasks in the list. + * Executes the DeleteCommand by removing the specified task + * from the task list. + * Also prints out a message indicating the task that + * was removed and the current number of tasks in the list. * * @throws IndexOutOfBoundsException if the specified index is out of bounds. */ diff --git a/src/main/java/duke/Commands/EventCommand.java b/src/main/java/duke/Commands/EventCommand.java index 3d236e9d2..1e56e1c19 100644 --- a/src/main/java/duke/Commands/EventCommand.java +++ b/src/main/java/duke/Commands/EventCommand.java @@ -41,7 +41,8 @@ public class EventCommand extends Command { private final String end; /** - * Constructs an EventCommand object with the specified description, completion status, start and end times of the event. + * Constructs an EventCommand object with the specified description, + * completion status, start and end times of the event. * * @param desc The description of the event. * @param isMark The completion status of the event. @@ -56,8 +57,10 @@ public EventCommand(String desc, boolean isMark, String start, String end) { } /** - * Executes the EventCommand by creating a new Event object with the specified description, completion status, start and end times, - * adding it to the task list, and printing out a message indicating that the event has been added to the list. + * Executes the EventCommand by creating a new Event object with the + * specified description, completion status, start and end times, + * adding it to the task list, and printing out a message + * indicating that the event has been added to the list. * * @throws DukeException if there is an error creating the Event object. */ diff --git a/src/main/java/duke/Commands/ExitCommand.java b/src/main/java/duke/Commands/ExitCommand.java index 05f830b47..197e08562 100644 --- a/src/main/java/duke/Commands/ExitCommand.java +++ b/src/main/java/duke/Commands/ExitCommand.java @@ -2,7 +2,6 @@ /* * ThRepresents the Exit Command that is used to terminate the program - * */ import static duke.Duke.LINE_SPACING; diff --git a/src/main/java/duke/Commands/FindCommand.java b/src/main/java/duke/Commands/FindCommand.java index 48a382ba3..3704a9ba0 100644 --- a/src/main/java/duke/Commands/FindCommand.java +++ b/src/main/java/duke/Commands/FindCommand.java @@ -25,7 +25,8 @@ public FindCommand(String term) { } /** - * Executes the FindCommand by searching for tasks in the task list that contain the specified keyword and printing them out. + * Executes the FindCommand by searching for tasks in the task list + * that contain the specified keyword and printing them out. */ public void cmd() { Ui ui = new Ui(); diff --git a/src/main/java/duke/Commands/ListCommand.java b/src/main/java/duke/Commands/ListCommand.java index f36c30b73..d2dafdd2a 100644 --- a/src/main/java/duke/Commands/ListCommand.java +++ b/src/main/java/duke/Commands/ListCommand.java @@ -5,7 +5,6 @@ /* * Rerepsents a ListCommand that is sued to display all tasks * inherits from Command - * */ public class ListCommand extends Command { /** diff --git a/src/main/java/duke/Commands/ToDoCommand.java b/src/main/java/duke/Commands/ToDoCommand.java index 9dcff5ae5..983d11325 100644 --- a/src/main/java/duke/Commands/ToDoCommand.java +++ b/src/main/java/duke/Commands/ToDoCommand.java @@ -7,7 +7,7 @@ /* * Represents adding a todo task * extends Command - * */ + */ public class ToDoCommand extends Command { /** * The string representing the command word for this command. diff --git a/src/main/java/duke/Commands/UnMarkCommand.java b/src/main/java/duke/Commands/UnMarkCommand.java index 88e936651..524ccba40 100644 --- a/src/main/java/duke/Commands/UnMarkCommand.java +++ b/src/main/java/duke/Commands/UnMarkCommand.java @@ -4,12 +4,12 @@ import static duke.Duke.LINE_SPACING; -/* +/** * Represents a Command to unmar a task - * inherits from Command - * */ + * inherits from Command. + */ public class UnMarkCommand extends Command { - /* + /** * The keyword that denotes the command to unmark a task*/ public static final String COMMAND_WORD = "unmark"; /* diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index f08944ac1..b58fec03b 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -44,10 +44,10 @@ public void run() { while (!isExit) { try { String fullCommand = ui.readCommand(); - ui.showLine(); // show the divider line ("_______") + ui.showLine(); Command c = Parser.parse(fullCommand); c.cmd(); - isExit = c.isExit(); + isExit = c.getExit(); } catch (DukeException e) { System.out.println("error interrpetting input"); } finally { diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index 25bc62f23..91a1778ef 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -20,6 +20,12 @@ public static Command parse(String command) { String[] cmds = command.split(" ", 2); String cmd = cmds[0]; Command tmpCommand = null; + if ((!cmd.equals(ExitCommand.COMMAND_WORD) && + cmd.equals(ListCommand.COMMAND_WORD) == false) && + cmds.length == 1) { + System.out.print("Malformed input, "); + return new Command(); + } boolean isParse = true; switch (cmd) { case DeadLineCommand.COMMAND_WORD: diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 7c4f5e4c8..19a1d95a0 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -47,7 +47,6 @@ public static void writeFile(TaskList tasks) { myWriter.flush(); String strTask = ""; for (int i = 0; i < tasks.getSize(); i++) { -// myWriter.write(tasks.getTask(i).toString() + "\n"); if (tasks.getTask(i).toString().contains("[T]")) { strTask = "todoSplitFactString" + tasks.getTask(i).getDescription() + "SplitFactString" + tasks.getTask(i).getMarked(); @@ -81,8 +80,9 @@ public static void writeFile(TaskList tasks) { */ public static void readFile(TaskList tasks) { try { + String filePath = "./data/duke.txt"; ArrayList tmpStrTasks = new ArrayList<>(); - tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get("./data/duke.txt"), StandardCharsets.UTF_8); + tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8); for (int i = 0; i < tmpStrTasks.size(); i++) { String[] strTask = tmpStrTasks.get(i).split("SplitFactString"); boolean isMark = false; diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index a09b8ad0b..78682d3ab 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -45,7 +45,7 @@ public void delete(int idx) { size--; } - /* + /** * returns the size of the list tasks */ public int getSize() { From c222d8e1f780efd0d64a5f3675e23f7e2494b229 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Sat, 4 Mar 2023 23:36:26 +0800 Subject: [PATCH 25/26] Commiting for jar related issues - made a change in code so that I can process commit --- src/main/java/duke/Storage.java | 62 ++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 19a1d95a0..cda8d9604 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -6,10 +6,12 @@ import tasks.Task; import tasks.Todo; +import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -80,31 +82,43 @@ public static void writeFile(TaskList tasks) { */ public static void readFile(TaskList tasks) { try { - String filePath = "./data/duke.txt"; - ArrayList tmpStrTasks = new ArrayList<>(); - tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8); - for (int i = 0; i < tmpStrTasks.size(); i++) { - String[] strTask = tmpStrTasks.get(i).split("SplitFactString"); - boolean isMark = false; - if (tmpStrTasks.get(i).contains("[X]")) { - isMark = true; - } - switch (strTask[0]) { - case "todo": - Todo todo = new Todo(strTask[1], isMark); - tasks.add(todo); - break; - case "deadline": - Deadline dL = new Deadline(strTask[1], isMark, strTask[3]); - tasks.add(dL); - break; - case "event": - Event event = new Event(strTask[1], isMark, strTask[3], strTask[4]); - break; + Files.createDirectories(Path.of("./data")); + File myObj = new File("./data/duke.txt"); + + if (myObj.createNewFile()) { + System.out.println("File created: " + myObj.getName()); + } else { + } + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("An error occurred."); + try { + String filePath = "./data/duke.txt"; + ArrayList tmpStrTasks = new ArrayList<>(); + tmpStrTasks = (ArrayList) Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8); + for (int i = 0; i < tmpStrTasks.size(); i++) { + String[] strTask = tmpStrTasks.get(i).split("SplitFactString"); + boolean isMark = false; + if (tmpStrTasks.get(i).contains("[X]")) { + isMark = true; + } + switch (strTask[0]) { + case "todo": + Todo todo = new Todo(strTask[1], isMark); + tasks.add(todo); + break; + case "deadline": + Deadline dL = new Deadline(strTask[1], isMark, strTask[3]); + tasks.add(dL); + break; + case "event": + Event event = new Event(strTask[1], isMark, strTask[3], strTask[4]); + break; + } } + } catch (IOException | DukeException ioe) { + ioe.printStackTrace(); } - } catch (IOException | DukeException ioe) { - ioe.printStackTrace(); } } -} From 660cda3e71bd8d2d3e4b3c637cce2ac5ffa6d4d7 Mon Sep 17 00:00:00 2001 From: Liam Van <74201538+SpeciLiam@users.noreply.github.com> Date: Sat, 4 Mar 2023 23:36:47 +0800 Subject: [PATCH 26/26] Commiting for jar related issues - made a change in code so that I can process commit --- src/main/java/duke/Storage.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index cda8d9604..9ed37ab81 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -87,7 +87,6 @@ public static void readFile(TaskList tasks) { if (myObj.createNewFile()) { System.out.println("File created: " + myObj.getName()); - } else { } } catch (IOException e) { e.printStackTrace();