From cbf3cd2bb0ca39ee618e887877c84834947c0cd2 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 18 Aug 2021 10:49:05 +0800 Subject: [PATCH 01/33] Add Greet --- src/main/java/Duke.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..938d08927 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,5 +6,12 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + String divider = "________________________________________________________________"; + System.out.println(divider); + System.out.println("Hello! I'm Duke"); + System.out.println("What can I do for you?"); + System.out.println(divider); + System.out.println("Bye, hope to see you again soon!"); + System.out.println(divider); } } From 91ce339c1e1070426187f9c3e42362442111edff Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 25 Aug 2021 11:51:42 +0800 Subject: [PATCH 02/33] Add echo and method for printing output --- src/main/java/Duke.java | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 938d08927..a11d32c42 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,18 @@ +import java.util.Scanner; + public class Duke { + public static String divider = "___________________________________________________________"; + public static String indentation = " "; + + public static void printIndentationAndDivider(){ + System.out.print(indentation); + System.out.println(divider); + } + public static void printWordsWithIndentation(String words){ + System.out.print(indentation); + System.out.println(words); + } + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -6,12 +20,24 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - String divider = "________________________________________________________________"; - System.out.println(divider); - System.out.println("Hello! I'm Duke"); - System.out.println("What can I do for you?"); - System.out.println(divider); - System.out.println("Bye, hope to see you again soon!"); - System.out.println(divider); + printIndentationAndDivider(); + printWordsWithIndentation("Hello! I'm Duke"); + printWordsWithIndentation("What can I do for you? :D"); + printIndentationAndDivider(); + System.out.println(); + + Scanner in = new Scanner (System.in); + String input = in.nextLine(); + while(!input.equals("bye")){ + printIndentationAndDivider(); + printWordsWithIndentation(input); + printIndentationAndDivider(); + System.out.println(); + input = in.nextLine(); + } + + printIndentationAndDivider(); + printWordsWithIndentation("Bye, hope to see you again soon! :)"); + printIndentationAndDivider(); } } From e4f50de16bb08e1646e760b998da2047d025a9e3 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 25 Aug 2021 13:22:08 +0800 Subject: [PATCH 03/33] Add list keeping and list printing --- src/main/java/Duke.java | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a11d32c42..3dff301db 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,12 +3,15 @@ public class Duke { public static String divider = "___________________________________________________________"; public static String indentation = " "; + public static String[] input_list = new String[100]; + public static int index = 0; - public static void printIndentationAndDivider(){ + public static void printIndentationAndDivider() { System.out.print(indentation); System.out.println(divider); } - public static void printWordsWithIndentation(String words){ + + public static void printWordsWithIndentation(String words) { System.out.print(indentation); System.out.println(words); } @@ -26,13 +29,25 @@ public static void main(String[] args) { printIndentationAndDivider(); System.out.println(); - Scanner in = new Scanner (System.in); + Scanner in = new Scanner(System.in); String input = in.nextLine(); - while(!input.equals("bye")){ - printIndentationAndDivider(); - printWordsWithIndentation(input); - printIndentationAndDivider(); - System.out.println(); + while (!input.equals("bye")) { + if(input.equals("list")) { + printIndentationAndDivider(); + for (int i = 0; i < index; i++) { + printWordsWithIndentation(i + 1 + ". " + input_list[i]); + } + printIndentationAndDivider(); + System.out.println(); + } + else { + printIndentationAndDivider(); + printWordsWithIndentation("added: " + input); + printIndentationAndDivider(); + System.out.println(); + input_list[index] = input; + index++; + } input = in.nextLine(); } From 39dbc7c2edb1cb2d1058806b103bf5e413a58323 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 25 Aug 2021 14:15:00 +0800 Subject: [PATCH 04/33] Add Task class and Mark as Done feature --- src/main/java/Duke.java | 32 ++++++++++++++++++++++---------- src/main/java/Task.java | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3dff301db..2e98a4fe2 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,8 +3,7 @@ public class Duke { public static String divider = "___________________________________________________________"; public static String indentation = " "; - public static String[] input_list = new String[100]; - public static int index = 0; + public static Task[] taskList = new Task[100]; public static void printIndentationAndDivider() { System.out.print(indentation); @@ -24,7 +23,7 @@ public static void main(String[] args) { + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); printIndentationAndDivider(); - printWordsWithIndentation("Hello! I'm Duke"); + printWordsWithIndentation("Hello! I'm Duke, your friendly neighbourhood task manager"); printWordsWithIndentation("What can I do for you? :D"); printIndentationAndDivider(); System.out.println(); @@ -32,21 +31,34 @@ public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); while (!input.equals("bye")) { - if(input.equals("list")) { + switch(input.split(" ")[0]) { + case "list" : printIndentationAndDivider(); - for (int i = 0; i < index; i++) { - printWordsWithIndentation(i + 1 + ". " + input_list[i]); + for (int i = 0; i < Task.getTotalTasks(); i++) { + String description = taskList[i].getDescription(); + String statusIcon = taskList[i].getStatusIcon(); + printWordsWithIndentation(i + 1 + "." + "[" + statusIcon + "] " + description); } printIndentationAndDivider(); System.out.println(); - } - else { + break; + + case "done" : + int index = Integer.parseInt(input.split(" ")[1]) - 1; + taskList[index].markAsDone(); + printIndentationAndDivider(); + printWordsWithIndentation("You've completed the task! Well done!"); + printWordsWithIndentation("[" + taskList[index].getStatusIcon() + "] " + taskList[index].getDescription()); + printIndentationAndDivider(); + break; + default : printIndentationAndDivider(); printWordsWithIndentation("added: " + input); printIndentationAndDivider(); System.out.println(); - input_list[index] = input; - index++; + taskList[Task.getTotalTasks()] = new Task(input); + Task.setTotalTasks(Task.getTotalTasks() + 1); + break; } input = in.nextLine(); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..f41d1faa8 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,35 @@ +public class Task { + private boolean isDone; + private String description; + + private static int totalTasks = 0; + + public Task(String description){ + this.description = description; + isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); + } + + public void markAsDone() { + isDone = true; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public static int getTotalTasks() { + return totalTasks; + } + + public static void setTotalTasks(int totalTasks) { + Task.totalTasks = totalTasks; + } +} From b287fd98f3a1db6dfe3d184b4f9efb4ac4d7b6e4 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 25 Aug 2021 14:48:53 +0800 Subject: [PATCH 05/33] Change taskList to tasks --- src/main/java/Duke.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 2e98a4fe2..ce5c7e3f7 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,7 +3,7 @@ public class Duke { public static String divider = "___________________________________________________________"; public static String indentation = " "; - public static Task[] taskList = new Task[100]; + public static Task[] tasks = new Task[100]; public static void printIndentationAndDivider() { System.out.print(indentation); @@ -35,20 +35,19 @@ public static void main(String[] args) { case "list" : printIndentationAndDivider(); for (int i = 0; i < Task.getTotalTasks(); i++) { - String description = taskList[i].getDescription(); - String statusIcon = taskList[i].getStatusIcon(); + String description = tasks[i].getDescription(); + String statusIcon = tasks[i].getStatusIcon(); printWordsWithIndentation(i + 1 + "." + "[" + statusIcon + "] " + description); } printIndentationAndDivider(); System.out.println(); break; - case "done" : int index = Integer.parseInt(input.split(" ")[1]) - 1; - taskList[index].markAsDone(); + tasks[index].markAsDone(); printIndentationAndDivider(); printWordsWithIndentation("You've completed the task! Well done!"); - printWordsWithIndentation("[" + taskList[index].getStatusIcon() + "] " + taskList[index].getDescription()); + printWordsWithIndentation("[" + tasks[index].getStatusIcon() + "] " + tasks[index].getDescription()); printIndentationAndDivider(); break; default : @@ -56,7 +55,7 @@ public static void main(String[] args) { printWordsWithIndentation("added: " + input); printIndentationAndDivider(); System.out.println(); - taskList[Task.getTotalTasks()] = new Task(input); + tasks[Task.getTotalTasks()] = new Task(input); Task.setTotalTasks(Task.getTotalTasks() + 1); break; } From b6db0028d43963697e887939c43ec8d8d1671e0b Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Fri, 27 Aug 2021 11:43:35 +0800 Subject: [PATCH 06/33] Change constant to upper case --- src/main/java/Duke.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ce5c7e3f7..426275078 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,17 +1,17 @@ import java.util.Scanner; public class Duke { - public static String divider = "___________________________________________________________"; - public static String indentation = " "; + public static final String DIVIDER = "___________________________________________________________"; + public static final String INDENTATION = " "; public static Task[] tasks = new Task[100]; public static void printIndentationAndDivider() { - System.out.print(indentation); - System.out.println(divider); + System.out.print(INDENTATION); + System.out.println(DIVIDER); } public static void printWordsWithIndentation(String words) { - System.out.print(indentation); + System.out.print(INDENTATION); System.out.println(words); } From ab47db633a0c87be7f7eac933f6f055d99ceb90e Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 1 Sep 2021 21:49:36 +0800 Subject: [PATCH 07/33] Add Event, ToDo, Deadline class and TaskType enum. --- src/main/java/Deadline.java | 15 +++ src/main/java/Duke.java | 184 ++++++++++++++++++++++++++++-------- src/main/java/Event.java | 15 +++ src/main/java/Task.java | 20 ++-- src/main/java/TaskType.java | 3 + src/main/java/ToDo.java | 12 +++ 6 files changed, 200 insertions(+), 49 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/TaskType.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..0f87e5965 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,15 @@ +public class Deadline extends Task{ + public static final String BY = "by: "; + protected String date; + private static final String IDENTIFIER = "D"; + + public Deadline(String description,String date) { + super(description); + this.date = date; + } + + public String getStatusIconAndDescription() { + String icon = (isDone ? "X" : " "); + return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(BY + date); + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 426275078..e615f0611 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,8 +3,36 @@ public class Duke { public static final String DIVIDER = "___________________________________________________________"; public static final String INDENTATION = " "; + public static final String LOGO = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + public static final String HELLO_MESSAGE_2 = "Hello! I'm Duke, your friendly neighbourhood task manager"; + public static final String HELLO_MESSAGE_3 = "What can I do for you? :D"; + public static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; + public static final String ADDED_TO_LIST = "I've added this to your list :D"; + public static final String DEADLINE_PROMPT = " /by "; + public static final String EVENT_PROMPT = " /at "; + public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; + public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry, you are using me wrongly. Please type in a suitable command :)"; + public static final boolean IS_FINE = true; + public static final int TODO_STARTING_INDEX = 5; + public static final int DEADLINE_STARTING_INDEX = 9; + public static final int EVENT_STARTING_INDEX = 6; public static Task[] tasks = new Task[100]; + public static void main(String[] args) { + printStartingMessage(); + Scanner in = new Scanner(System.in); + String input = in.nextLine(); + while (!input.equals("bye")) { + processInput(input); + input = in.nextLine(); + } + printGoodbyeMessage(); + } + public static void printIndentationAndDivider() { System.out.print(INDENTATION); System.out.println(DIVIDER); @@ -15,55 +43,129 @@ public static void printWordsWithIndentation(String words) { System.out.println(words); } - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + private static void printGoodbyeMessage() { printIndentationAndDivider(); - printWordsWithIndentation("Hello! I'm Duke, your friendly neighbourhood task manager"); - printWordsWithIndentation("What can I do for you? :D"); + printWordsWithIndentation(GOODBYE_MESSAGE); + printIndentationAndDivider(); + } + + private static void processInput(String input) { + switch(input.split(" ")[0].toLowerCase()) { + case "list" : + executeListCase(); + break; + case "done" : + executeDoneCase(input); + break; + case "todo" : + executeTaskCase(input, TODO_STARTING_INDEX,TaskType.TODO); + break; + case "deadline" : + executeTaskCase(input, DEADLINE_STARTING_INDEX,TaskType.DEADLINE); + break; + case "event" : + executeTaskCase(input, EVENT_STARTING_INDEX,TaskType.EVENT); + break; + default : + printIncorrectInputMessage(); + } + } + + private static void printTaskMessage() { + printIndentationAndDivider(); + printWordsWithIndentation(ADDED_TO_LIST); + printWordsWithIndentation(tasks[Task.getTotalTasks() - 1].getStatusIconAndDescription()); + printWordsWithIndentation(notifyNumberOfTasks()); printIndentationAndDivider(); System.out.println(); + } - Scanner in = new Scanner(System.in); - String input = in.nextLine(); - while (!input.equals("bye")) { - switch(input.split(" ")[0]) { - case "list" : - printIndentationAndDivider(); - for (int i = 0; i < Task.getTotalTasks(); i++) { - String description = tasks[i].getDescription(); - String statusIcon = tasks[i].getStatusIcon(); - printWordsWithIndentation(i + 1 + "." + "[" + statusIcon + "] " + description); - } - printIndentationAndDivider(); - System.out.println(); - break; - case "done" : - int index = Integer.parseInt(input.split(" ")[1]) - 1; - tasks[index].markAsDone(); - printIndentationAndDivider(); - printWordsWithIndentation("You've completed the task! Well done!"); - printWordsWithIndentation("[" + tasks[index].getStatusIcon() + "] " + tasks[index].getDescription()); - printIndentationAndDivider(); - break; - default : - printIndentationAndDivider(); - printWordsWithIndentation("added: " + input); - printIndentationAndDivider(); - System.out.println(); - tasks[Task.getTotalTasks()] = new Task(input); - Task.setTotalTasks(Task.getTotalTasks() + 1); - break; + private static void printIncorrectInputMessage() { + printIndentationAndDivider(); + printWordsWithIndentation(TYPE_SUITABLE_COMMAND_MESSAGE); + printIndentationAndDivider(); + System.out.println(); + } + + private static void executeTaskCase(String input, int starting_index, TaskType type) { + String description = input.strip().substring(starting_index); + boolean isFine = addTask(description, type); + if(isFine) { + printTaskMessage(); + }else { + printIncorrectInputMessage(); + } + } + + private static String[] parseInputForDifferentTask(String input,TaskType type) { + String[] parsedOutput = {}; + switch(type) { + case TODO: + parsedOutput = new String[]{input}; + break; + + case DEADLINE: + parsedOutput = input.split(DEADLINE_PROMPT); + break; + + case EVENT: + parsedOutput = input.split(EVENT_PROMPT); + break; + } + return parsedOutput; + } + + private static boolean addTask(String input,TaskType type) { + String[] parsedOutput = parseInputForDifferentTask(input,type); + switch(type){ + case TODO: + tasks[Task.getTotalTasks()] = new ToDo(parsedOutput[0]); + break; + case EVENT: + if(parsedOutput.length < 2) { + return !IS_FINE; } - input = in.nextLine(); + tasks[Task.getTotalTasks()] = new Event(parsedOutput[0], parsedOutput[1]); + break; + case DEADLINE: + if(parsedOutput.length < 2) { + return !IS_FINE; + } + tasks[Task.getTotalTasks()] = new Deadline(parsedOutput[0], parsedOutput[1]); + break; + } + Task.setTotalTasks(Task.getTotalTasks() + 1); + return IS_FINE; + } + + private static String notifyNumberOfTasks() { + return "Now you have " + Task.getTotalTasks() + " task(s) in the list"; + } + + private static void executeDoneCase(String input) { + int index = Integer.parseInt(input.split(" ")[1]) - 1; + tasks[index].markAsDone(); + printIndentationAndDivider(); + printWordsWithIndentation(TASK_COMPLETED_MESSAGE); + printWordsWithIndentation(tasks[index].getStatusIconAndDescription()); + printIndentationAndDivider(); + } + + private static void executeListCase() { + printIndentationAndDivider(); + for (int i = 0; i < Task.getTotalTasks(); i++) { + printWordsWithIndentation(i + 1 + "." + tasks[i].getStatusIconAndDescription()); } + printIndentationAndDivider(); + System.out.println(); + } + private static void printStartingMessage() { + System.out.println("Hello from\n" + LOGO); printIndentationAndDivider(); - printWordsWithIndentation("Bye, hope to see you again soon! :)"); + printWordsWithIndentation(HELLO_MESSAGE_2); + printWordsWithIndentation(HELLO_MESSAGE_3); printIndentationAndDivider(); + System.out.println(); } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..32229ed9f --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,15 @@ +public class Event extends Task{ + public static final String AT = "at: "; + protected String dateAndTime; + private static final String IDENTIFIER = "E"; + + public Event(String description, String dateAndTime) { + super(description); + this.dateAndTime = dateAndTime; + } + + public String getStatusIconAndDescription() { + String icon = (isDone ? "X" : " "); + return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(AT + dateAndTime); + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index f41d1faa8..147d680e5 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,6 +1,6 @@ public class Task { - private boolean isDone; - private String description; + protected boolean isDone; + protected String description; private static int totalTasks = 0; @@ -9,18 +9,15 @@ public Task(String description){ isDone = false; } - public String getStatusIcon() { - return (isDone ? "X" : " "); + public String getStatusIconAndDescription() { + String icon = (isDone ? "X" : " "); + return addSquareBrackets(icon) + " " + description; } public void markAsDone() { isDone = true; } - public String getDescription() { - return description; - } - public void setDescription(String description) { this.description = description; } @@ -32,4 +29,11 @@ public static int getTotalTasks() { public static void setTotalTasks(int totalTasks) { Task.totalTasks = totalTasks; } + + protected static String addSquareBrackets(String s) { + return "[" + s + "]"; + } + protected static String addBrackets(String s) { + return "(" + s + ")"; + } } diff --git a/src/main/java/TaskType.java b/src/main/java/TaskType.java new file mode 100644 index 000000000..8831de1fa --- /dev/null +++ b/src/main/java/TaskType.java @@ -0,0 +1,3 @@ +public enum TaskType { + TODO,DEADLINE,EVENT; +} diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java new file mode 100644 index 000000000..1d52c4fc8 --- /dev/null +++ b/src/main/java/ToDo.java @@ -0,0 +1,12 @@ +public class ToDo extends Task{ + private static final String IDENTIFIER = "T"; + + public ToDo(String description) { + super(description); + } + + public String getStatusIconAndDescription() { + String icon = (isDone ? "X" : " "); + return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description; + } +} From 026be1016ee952ad2443db99230b7efc17d40b30 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 1 Sep 2021 22:30:47 +0800 Subject: [PATCH 08/33] Add text UI testing --- src/main/java/Duke.java | 19 ++++++++-------- text-ui-test/EXPECTED.TXT | 47 +++++++++++++++++++++++++++++++++++++++ text-ui-test/input.txt | 8 +++++++ 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index e615f0611..51ef821ce 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -43,6 +43,15 @@ public static void printWordsWithIndentation(String words) { System.out.println(words); } + private static void printStartingMessage() { + System.out.println("Hello from\n" + LOGO); + printIndentationAndDivider(); + printWordsWithIndentation(HELLO_MESSAGE_2); + printWordsWithIndentation(HELLO_MESSAGE_3); + printIndentationAndDivider(); + System.out.println(); + } + private static void printGoodbyeMessage() { printIndentationAndDivider(); printWordsWithIndentation(GOODBYE_MESSAGE); @@ -149,6 +158,7 @@ private static void executeDoneCase(String input) { printWordsWithIndentation(TASK_COMPLETED_MESSAGE); printWordsWithIndentation(tasks[index].getStatusIconAndDescription()); printIndentationAndDivider(); + System.out.println(); } private static void executeListCase() { @@ -159,13 +169,4 @@ private static void executeListCase() { printIndentationAndDivider(); System.out.println(); } - - private static void printStartingMessage() { - System.out.println("Hello from\n" + LOGO); - printIndentationAndDivider(); - printWordsWithIndentation(HELLO_MESSAGE_2); - printWordsWithIndentation(HELLO_MESSAGE_3); - printIndentationAndDivider(); - System.out.println(); - } } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..03eff38ca 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -5,3 +5,50 @@ Hello from | |_| | |_| | < __/ |____/ \__,_|_|\_\___| + ___________________________________________________________ + Hello! I'm Duke, your friendly neighbourhood task manager + What can I do for you? :D + ___________________________________________________________ + + ___________________________________________________________ + I've added this to your list :D + [T][ ] borrow book + Now you have 1 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + I've added this to your list :D + [D][ ] return book (by: Sunday) + Now you have 2 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + I've added this to your list :D + [E][ ] project meeting (at: 4th Sep 2-3pm) + Now you have 3 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + Sorry, you are using me wrongly. Please type in a suitable command :) + ___________________________________________________________ + + ___________________________________________________________ + 1.[T][ ] borrow book + 2.[D][ ] return book (by: Sunday) + 3.[E][ ] project meeting (at: 4th Sep 2-3pm) + ___________________________________________________________ + + ___________________________________________________________ + You've completed the task! Well done! + [T][X] borrow book + ___________________________________________________________ + + ___________________________________________________________ + 1.[T][X] borrow book + 2.[D][ ] return book (by: Sunday) + 3.[E][ ] project meeting (at: 4th Sep 2-3pm) + ___________________________________________________________ + + ___________________________________________________________ + Bye, hope to see you again soon! :) + ___________________________________________________________ \ No newline at end of file diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..57907cfac 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,8 @@ +todo borrow book +DeadLine return book /by Sunday +Event project meeting /at 4th Sep 2-3pm +DEADLINE /by Monday +list +done 1 +list +bye \ No newline at end of file From 5da062e46f43845afae6b1cc963e84153fba722c Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 1 Sep 2021 22:48:17 +0800 Subject: [PATCH 09/33] Update text ui test and done refactoring --- src/main/java/Duke.java | 18 +++++++++++++----- text-ui-test/EXPECTED.TXT | 4 ++++ text-ui-test/input.txt | 1 + 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 51ef821ce..6b9b5db01 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -12,8 +12,8 @@ public class Duke { public static final String HELLO_MESSAGE_3 = "What can I do for you? :D"; public static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; public static final String ADDED_TO_LIST = "I've added this to your list :D"; - public static final String DEADLINE_PROMPT = " /by "; - public static final String EVENT_PROMPT = " /at "; + public static final String DEADLINE_DESCRIPTION_AND_DATE_SPLITTER = " /by "; + public static final String EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER = " /at "; public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry, you are using me wrongly. Please type in a suitable command :)"; public static final boolean IS_FINE = true; @@ -114,11 +114,11 @@ private static String[] parseInputForDifferentTask(String input,TaskType type) { break; case DEADLINE: - parsedOutput = input.split(DEADLINE_PROMPT); + parsedOutput = input.split(DEADLINE_DESCRIPTION_AND_DATE_SPLITTER); break; case EVENT: - parsedOutput = input.split(EVENT_PROMPT); + parsedOutput = input.split(EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER); break; } return parsedOutput; @@ -152,11 +152,19 @@ private static String notifyNumberOfTasks() { } private static void executeDoneCase(String input) { + if(input.length() != 6) { + printIncorrectInputMessage(); + return; + } int index = Integer.parseInt(input.split(" ")[1]) - 1; tasks[index].markAsDone(); + printTaskCompletedMessage(tasks[index]); + } + + private static void printTaskCompletedMessage(Task task) { printIndentationAndDivider(); printWordsWithIndentation(TASK_COMPLETED_MESSAGE); - printWordsWithIndentation(tasks[index].getStatusIconAndDescription()); + printWordsWithIndentation(task.getStatusIconAndDescription()); printIndentationAndDivider(); System.out.println(); } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 03eff38ca..005929d15 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -32,6 +32,10 @@ Hello from Sorry, you are using me wrongly. Please type in a suitable command :) ___________________________________________________________ + ___________________________________________________________ + Sorry, you are using me wrongly. Please type in a suitable command :) + ___________________________________________________________ + ___________________________________________________________ 1.[T][ ] borrow book 2.[D][ ] return book (by: Sunday) diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 57907cfac..4c1a57a17 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -2,6 +2,7 @@ todo borrow book DeadLine return book /by Sunday Event project meeting /at 4th Sep 2-3pm DEADLINE /by Monday +done list done 1 list From 14a2865498c0e6b56ae7f59e1881b209e7051bb9 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 8 Sep 2021 18:04:54 +0800 Subject: [PATCH 10/33] Add exception handling and exception class --- src/main/java/Duke.java | 97 ++++++++++++++++++++++---------- src/main/java/DukeException.java | 2 + 2 files changed, 70 insertions(+), 29 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 6b9b5db01..a35d42f4b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -15,11 +15,18 @@ public class Duke { public static final String DEADLINE_DESCRIPTION_AND_DATE_SPLITTER = " /by "; public static final String EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER = " /at "; public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; - public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry, you are using me wrongly. Please type in a suitable command :)"; - public static final boolean IS_FINE = true; + public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry, I don't know what you mean. " + + "Please give me a suitable command :)"; public static final int TODO_STARTING_INDEX = 5; public static final int DEADLINE_STARTING_INDEX = 9; public static final int EVENT_STARTING_INDEX = 6; + public static final String PROMPT_TASK_DESCRIPTION = "Please tell me what do you need to do :)"; + public static final String INPUT_MORE_THAN_NUMBER_OF_TASKS = "Woah, you don't have THAT many things to do!"; + public static final String LIST_IS_EMPTY = "You're a free man :)"; + public static final String MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE = "Sorry," + + " you're missing some arguments, do type 'help' if you're unsure :)"; + public static final String PROMPT_TASK_NUMBER = "Please tell me which task you finished :)"; + public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1-100 :)"; public static Task[] tasks = new Task[100]; public static void main(String[] args) { @@ -60,23 +67,54 @@ private static void printGoodbyeMessage() { private static void processInput(String input) { switch(input.split(" ")[0].toLowerCase()) { - case "list" : + case "list": executeListCase(); break; - case "done" : - executeDoneCase(input); + case "done": + try { + executeDoneCase(input); + + } catch (NullPointerException error) { + printErrorMessage(INPUT_MORE_THAN_NUMBER_OF_TASKS); + + } catch (DukeException error) { + printErrorMessage(PROMPT_TASK_NUMBER); + + } catch (ArrayIndexOutOfBoundsException error) { + printErrorMessage(PROMPT_SENSIBLE_INDEX); + + } catch (NumberFormatException error) { + printErrorMessage(PROMPT_SENSIBLE_INDEX); + + } break; - case "todo" : - executeTaskCase(input, TODO_STARTING_INDEX,TaskType.TODO); + case "todo": + try { + executeTaskCase(input, TODO_STARTING_INDEX,TaskType.TODO); + + } catch (StringIndexOutOfBoundsException error) { + printErrorMessage(PROMPT_TASK_DESCRIPTION); + } break; - case "deadline" : - executeTaskCase(input, DEADLINE_STARTING_INDEX,TaskType.DEADLINE); + case "deadline": + try { + executeTaskCase(input, DEADLINE_STARTING_INDEX,TaskType.DEADLINE); + + } catch (StringIndexOutOfBoundsException error) { + printErrorMessage(PROMPT_TASK_DESCRIPTION); + + } break; - case "event" : - executeTaskCase(input, EVENT_STARTING_INDEX,TaskType.EVENT); + case "event": + try { + executeTaskCase(input, EVENT_STARTING_INDEX,TaskType.EVENT); + + } catch (StringIndexOutOfBoundsException error) { + printErrorMessage(PROMPT_TASK_DESCRIPTION); + } break; - default : - printIncorrectInputMessage(); + default: + printErrorMessage(TYPE_SUITABLE_COMMAND_MESSAGE); } } @@ -89,20 +127,21 @@ private static void printTaskMessage() { System.out.println(); } - private static void printIncorrectInputMessage() { + private static void printErrorMessage(String error_message) { printIndentationAndDivider(); - printWordsWithIndentation(TYPE_SUITABLE_COMMAND_MESSAGE); + printWordsWithIndentation(error_message); printIndentationAndDivider(); System.out.println(); } private static void executeTaskCase(String input, int starting_index, TaskType type) { String description = input.strip().substring(starting_index); - boolean isFine = addTask(description, type); - if(isFine) { + try { + addTask(description, type); printTaskMessage(); - }else { - printIncorrectInputMessage(); + + } catch (DukeException error) { + printErrorMessage(MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE); } } @@ -112,11 +151,9 @@ private static String[] parseInputForDifferentTask(String input,TaskType type) { case TODO: parsedOutput = new String[]{input}; break; - case DEADLINE: parsedOutput = input.split(DEADLINE_DESCRIPTION_AND_DATE_SPLITTER); break; - case EVENT: parsedOutput = input.split(EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER); break; @@ -124,7 +161,7 @@ private static String[] parseInputForDifferentTask(String input,TaskType type) { return parsedOutput; } - private static boolean addTask(String input,TaskType type) { + private static void addTask(String input,TaskType type) throws StringIndexOutOfBoundsException, DukeException { String[] parsedOutput = parseInputForDifferentTask(input,type); switch(type){ case TODO: @@ -132,29 +169,28 @@ private static boolean addTask(String input,TaskType type) { break; case EVENT: if(parsedOutput.length < 2) { - return !IS_FINE; + throw new DukeException(); } tasks[Task.getTotalTasks()] = new Event(parsedOutput[0], parsedOutput[1]); break; case DEADLINE: if(parsedOutput.length < 2) { - return !IS_FINE; + throw new DukeException(); } tasks[Task.getTotalTasks()] = new Deadline(parsedOutput[0], parsedOutput[1]); break; } Task.setTotalTasks(Task.getTotalTasks() + 1); - return IS_FINE; } private static String notifyNumberOfTasks() { return "Now you have " + Task.getTotalTasks() + " task(s) in the list"; } - private static void executeDoneCase(String input) { - if(input.length() != 6) { - printIncorrectInputMessage(); - return; + private static void executeDoneCase(String input) throws NullPointerException,DukeException, + ArrayIndexOutOfBoundsException, NumberFormatException{ + if(input.length() < 6) { + throw new DukeException(); } int index = Integer.parseInt(input.split(" ")[1]) - 1; tasks[index].markAsDone(); @@ -171,6 +207,9 @@ private static void printTaskCompletedMessage(Task task) { private static void executeListCase() { printIndentationAndDivider(); + if(Task.getTotalTasks() == 0) { + printWordsWithIndentation(LIST_IS_EMPTY); + } for (int i = 0; i < Task.getTotalTasks(); i++) { printWordsWithIndentation(i + 1 + "." + tasks[i].getStatusIconAndDescription()); } diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..83d92ee9b --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,2 @@ +public class DukeException extends Exception{ +} From 8d6b1e5c98b0677ab56c08201d59fd87e7862191 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 8 Sep 2021 18:05:41 +0800 Subject: [PATCH 11/33] Change input and expected text for ui test --- text-ui-test/EXPECTED.TXT | 16 ++++++++++++++-- text-ui-test/input.txt | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 005929d15..9d41433fc 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -29,11 +29,11 @@ Hello from ___________________________________________________________ ___________________________________________________________ - Sorry, you are using me wrongly. Please type in a suitable command :) + Sorry, you're missing some arguments, do type 'help' if you're unsure :) ___________________________________________________________ ___________________________________________________________ - Sorry, you are using me wrongly. Please type in a suitable command :) + Please tell me which task you finished :) ___________________________________________________________ ___________________________________________________________ @@ -53,6 +53,18 @@ Hello from 3.[E][ ] project meeting (at: 4th Sep 2-3pm) ___________________________________________________________ + ___________________________________________________________ + Please give a number between 1-100 :) + ___________________________________________________________ + + ___________________________________________________________ + Please give a number between 1-100 :) + ___________________________________________________________ + + ___________________________________________________________ + Woah, you don't have THAT many things to do! + ___________________________________________________________ + ___________________________________________________________ Bye, hope to see you again soon! :) ___________________________________________________________ \ No newline at end of file diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 4c1a57a17..6a5b84dd5 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -6,4 +6,7 @@ done list done 1 list +done - +done 0 +done 5 bye \ No newline at end of file From 42969c1277d2029aa2900bf4409a291263a04643 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 8 Sep 2021 18:17:53 +0800 Subject: [PATCH 12/33] Move classes into packages and update runtest.bat --- src/main/java/Duke.java | 4 +++- src/main/java/{ => task}/Deadline.java | 2 ++ src/main/java/{ => task}/Event.java | 2 ++ src/main/java/{ => task}/Task.java | 2 ++ src/main/java/{ => task}/TaskType.java | 2 ++ src/main/java/{ => task}/ToDo.java | 2 ++ text-ui-test/runtest.bat | 2 +- 7 files changed, 14 insertions(+), 2 deletions(-) rename src/main/java/{ => task}/Deadline.java (97%) rename src/main/java/{ => task}/Event.java (97%) rename src/main/java/{ => task}/Task.java (98%) rename src/main/java/{ => task}/TaskType.java (76%) rename src/main/java/{ => task}/ToDo.java (95%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a35d42f4b..540750ebc 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,5 @@ +import task.*; + import java.util.Scanner; public class Duke { @@ -90,7 +92,7 @@ private static void processInput(String input) { break; case "todo": try { - executeTaskCase(input, TODO_STARTING_INDEX,TaskType.TODO); + executeTaskCase(input, TODO_STARTING_INDEX, TaskType.TODO); } catch (StringIndexOutOfBoundsException error) { printErrorMessage(PROMPT_TASK_DESCRIPTION); diff --git a/src/main/java/Deadline.java b/src/main/java/task/Deadline.java similarity index 97% rename from src/main/java/Deadline.java rename to src/main/java/task/Deadline.java index 0f87e5965..af7240bea 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -1,3 +1,5 @@ +package task; + public class Deadline extends Task{ public static final String BY = "by: "; protected String date; diff --git a/src/main/java/Event.java b/src/main/java/task/Event.java similarity index 97% rename from src/main/java/Event.java rename to src/main/java/task/Event.java index 32229ed9f..b8b618f15 100644 --- a/src/main/java/Event.java +++ b/src/main/java/task/Event.java @@ -1,3 +1,5 @@ +package task; + public class Event extends Task{ public static final String AT = "at: "; protected String dateAndTime; diff --git a/src/main/java/Task.java b/src/main/java/task/Task.java similarity index 98% rename from src/main/java/Task.java rename to src/main/java/task/Task.java index 147d680e5..35dcae3a0 100644 --- a/src/main/java/Task.java +++ b/src/main/java/task/Task.java @@ -1,3 +1,5 @@ +package task; + public class Task { protected boolean isDone; protected String description; diff --git a/src/main/java/TaskType.java b/src/main/java/task/TaskType.java similarity index 76% rename from src/main/java/TaskType.java rename to src/main/java/task/TaskType.java index 8831de1fa..bd74dc8ff 100644 --- a/src/main/java/TaskType.java +++ b/src/main/java/task/TaskType.java @@ -1,3 +1,5 @@ +package task; + public enum TaskType { TODO,DEADLINE,EVENT; } diff --git a/src/main/java/ToDo.java b/src/main/java/task/ToDo.java similarity index 95% rename from src/main/java/ToDo.java rename to src/main/java/task/ToDo.java index 1d52c4fc8..dbe925971 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/task/ToDo.java @@ -1,3 +1,5 @@ +package task; + public class ToDo extends Task{ private static final String IDENTIFIER = "T"; diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..9ea7ba0a9 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,7 @@ REM delete output from previous run if exist ACTUAL.TXT del ACTUAL.TXT REM compile the code into the bin folder -javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java +javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\task\*.java ..\src\main\java\*.java IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 From 8493d644c5a4a48e5d53269df31e35d29e81d17e Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Fri, 10 Sep 2021 18:19:14 +0800 Subject: [PATCH 13/33] Add package duke and update readme and runtest.bat --- README.md | 2 +- src/main/java/{ => duke}/Duke.java | 8 +++++++- src/main/java/{ => duke}/DukeException.java | 2 ++ text-ui-test/runtest.bat | 4 ++-- 4 files changed, 12 insertions(+), 4 deletions(-) rename src/main/java/{ => duke}/Duke.java (98%) rename src/main/java/{ => duke}/DukeException.java (76%) diff --git a/README.md b/README.md index 8715d4d91..1a36bb905 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version. 1. If there are any further prompts, accept the defaults. 1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: +3. After that, locate the `src/main/java/duke/Duke.java` file, right-click it, and choose `Run duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from ____ _ diff --git a/src/main/java/Duke.java b/src/main/java/duke/Duke.java similarity index 98% rename from src/main/java/Duke.java rename to src/main/java/duke/Duke.java index 540750ebc..45e2cecd6 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,4 +1,10 @@ -import task.*; +package duke; + +import task.Deadline; +import task.Event; +import task.Task; +import task.TaskType; +import task.ToDo; import java.util.Scanner; diff --git a/src/main/java/DukeException.java b/src/main/java/duke/DukeException.java similarity index 76% rename from src/main/java/DukeException.java rename to src/main/java/duke/DukeException.java index 83d92ee9b..8c4a7a15f 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/duke/DukeException.java @@ -1,2 +1,4 @@ +package duke; + public class DukeException extends Exception{ } diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 9ea7ba0a9..a99301ad7 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,7 @@ REM delete output from previous run if exist ACTUAL.TXT del ACTUAL.TXT REM compile the code into the bin folder -javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\task\*.java ..\src\main\java\*.java +javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\duke\*.java ..\src\main\java\task\*.java IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Duke < input.txt > ACTUAL.TXT +java -classpath ..\bin duke.Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From 3dc210f08ab395a524a815b19379a5ef56709539 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 15 Sep 2021 12:54:42 +0800 Subject: [PATCH 14/33] Change array to array list, tidy up code, and update text ui test. --- src/main/java/duke/Duke.java | 66 ++++++++++++++++++++++++------------ src/main/java/task/Task.java | 4 --- text-ui-test/EXPECTED.TXT | 6 ++-- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 45e2cecd6..e39fbe71b 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -7,6 +7,7 @@ import task.ToDo; import java.util.Scanner; +import java.util.ArrayList; public class Duke { public static final String DIVIDER = "___________________________________________________________"; @@ -29,13 +30,15 @@ public class Duke { public static final int DEADLINE_STARTING_INDEX = 9; public static final int EVENT_STARTING_INDEX = 6; public static final String PROMPT_TASK_DESCRIPTION = "Please tell me what do you need to do :)"; - public static final String INPUT_MORE_THAN_NUMBER_OF_TASKS = "Woah, you don't have THAT many things to do!"; public static final String LIST_IS_EMPTY = "You're a free man :)"; public static final String MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE = "Sorry," + " you're missing some arguments, do type 'help' if you're unsure :)"; public static final String PROMPT_TASK_NUMBER = "Please tell me which task you finished :)"; - public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1-100 :)"; - public static Task[] tasks = new Task[100]; + public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1-"; + public static final int EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE = 2; + public static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 6; + public static final String DEFAULT_ERROR_MESSAGE = "Oops, something went wrong!"; + public static ArrayList tasks = new ArrayList<>(); public static void main(String[] args) { printStartingMessage(); @@ -82,17 +85,14 @@ private static void processInput(String input) { try { executeDoneCase(input); - } catch (NullPointerException error) { - printErrorMessage(INPUT_MORE_THAN_NUMBER_OF_TASKS); - } catch (DukeException error) { printErrorMessage(PROMPT_TASK_NUMBER); - } catch (ArrayIndexOutOfBoundsException error) { - printErrorMessage(PROMPT_SENSIBLE_INDEX); + } catch (IndexOutOfBoundsException error) { + printErrorMessage(getSensibleRange(Task.getTotalTasks())); } catch (NumberFormatException error) { - printErrorMessage(PROMPT_SENSIBLE_INDEX); + printErrorMessage(getSensibleRange(Task.getTotalTasks())); } break; @@ -102,6 +102,7 @@ private static void processInput(String input) { } catch (StringIndexOutOfBoundsException error) { printErrorMessage(PROMPT_TASK_DESCRIPTION); + } break; case "deadline": @@ -119,17 +120,19 @@ private static void processInput(String input) { } catch (StringIndexOutOfBoundsException error) { printErrorMessage(PROMPT_TASK_DESCRIPTION); + } break; default: printErrorMessage(TYPE_SUITABLE_COMMAND_MESSAGE); + } } private static void printTaskMessage() { printIndentationAndDivider(); printWordsWithIndentation(ADDED_TO_LIST); - printWordsWithIndentation(tasks[Task.getTotalTasks() - 1].getStatusIconAndDescription()); + printWordsWithIndentation(tasks.get(Task.getTotalTasks() - 1).getStatusIconAndDescription()); printWordsWithIndentation(notifyNumberOfTasks()); printIndentationAndDivider(); System.out.println(); @@ -150,6 +153,7 @@ private static void executeTaskCase(String input, int starting_index, TaskType t } catch (DukeException error) { printErrorMessage(MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE); + } } @@ -165,28 +169,35 @@ private static String[] parseInputForDifferentTask(String input,TaskType type) { case EVENT: parsedOutput = input.split(EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER); break; + default : + printErrorMessage(DEFAULT_ERROR_MESSAGE); + break; } return parsedOutput; } - private static void addTask(String input,TaskType type) throws StringIndexOutOfBoundsException, DukeException { + private static void addTask(String input,TaskType type) + throws StringIndexOutOfBoundsException, DukeException { String[] parsedOutput = parseInputForDifferentTask(input,type); switch(type){ case TODO: - tasks[Task.getTotalTasks()] = new ToDo(parsedOutput[0]); + tasks.add(new ToDo(parsedOutput[0])); break; case EVENT: - if(parsedOutput.length < 2) { + if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { throw new DukeException(); } - tasks[Task.getTotalTasks()] = new Event(parsedOutput[0], parsedOutput[1]); + tasks.add(new Event(parsedOutput[0], parsedOutput[1])); break; case DEADLINE: - if(parsedOutput.length < 2) { + if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { throw new DukeException(); } - tasks[Task.getTotalTasks()] = new Deadline(parsedOutput[0], parsedOutput[1]); + tasks.add(new Deadline(parsedOutput[0], parsedOutput[1])); break; + default : + printErrorMessage(DEFAULT_ERROR_MESSAGE); + return; } Task.setTotalTasks(Task.getTotalTasks() + 1); } @@ -195,14 +206,14 @@ private static String notifyNumberOfTasks() { return "Now you have " + Task.getTotalTasks() + " task(s) in the list"; } - private static void executeDoneCase(String input) throws NullPointerException,DukeException, - ArrayIndexOutOfBoundsException, NumberFormatException{ - if(input.length() < 6) { + private static void executeDoneCase(String input) throws DukeException, + IndexOutOfBoundsException,NumberFormatException { + if(input.length() < EXPECTED_LENGTH_FOR_DONE_INPUT) { throw new DukeException(); } int index = Integer.parseInt(input.split(" ")[1]) - 1; - tasks[index].markAsDone(); - printTaskCompletedMessage(tasks[index]); + tasks.get(index).markAsDone(); + printTaskCompletedMessage(tasks.get(index)); } private static void printTaskCompletedMessage(Task task) { @@ -219,9 +230,20 @@ private static void executeListCase() { printWordsWithIndentation(LIST_IS_EMPTY); } for (int i = 0; i < Task.getTotalTasks(); i++) { - printWordsWithIndentation(i + 1 + "." + tasks[i].getStatusIconAndDescription()); + printTask(i); } printIndentationAndDivider(); System.out.println(); } + + /** + *Prints out each task in a specific format + */ + private static void printTask(int i) { + printWordsWithIndentation(i + 1 + "." + tasks.get(i).getStatusIconAndDescription()); + } + + private static String getSensibleRange(int number) { + return PROMPT_SENSIBLE_INDEX + number + " :)"; + } } diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index 35dcae3a0..ebba8d73a 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -20,10 +20,6 @@ public void markAsDone() { isDone = true; } - public void setDescription(String description) { - this.description = description; - } - public static int getTotalTasks() { return totalTasks; } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 9d41433fc..5fe1e303b 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -54,15 +54,15 @@ Hello from ___________________________________________________________ ___________________________________________________________ - Please give a number between 1-100 :) + Please give a number between 1-3 :) ___________________________________________________________ ___________________________________________________________ - Please give a number between 1-100 :) + Please give a number between 1-3 :) ___________________________________________________________ ___________________________________________________________ - Woah, you don't have THAT many things to do! + Please give a number between 1-3 :) ___________________________________________________________ ___________________________________________________________ From a9902e734c2f07c126eb18bb2d87eb8ab68492f1 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 15 Sep 2021 14:29:46 +0800 Subject: [PATCH 15/33] Add delete task and update text ui test --- src/main/java/duke/Duke.java | 43 ++++++++++++++++++++++++++++++++++-- text-ui-test/EXPECTED.TXT | 43 ++++++++++++++++++++++++++++++++---- text-ui-test/input.txt | 7 ++++++ 3 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index e39fbe71b..a70f736b0 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -33,11 +33,12 @@ public class Duke { public static final String LIST_IS_EMPTY = "You're a free man :)"; public static final String MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE = "Sorry," + " you're missing some arguments, do type 'help' if you're unsure :)"; - public static final String PROMPT_TASK_NUMBER = "Please tell me which task you finished :)"; - public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1-"; + public static final String PROMPT_TASK_NUMBER = "Please tell me which task you want to select :)"; + public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1 and "; public static final int EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE = 2; public static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 6; public static final String DEFAULT_ERROR_MESSAGE = "Oops, something went wrong!"; + public static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 8; public static ArrayList tasks = new ArrayList<>(); public static void main(String[] args) { @@ -121,6 +122,21 @@ private static void processInput(String input) { } catch (StringIndexOutOfBoundsException error) { printErrorMessage(PROMPT_TASK_DESCRIPTION); + } + break; + case "delete": + try { + executeDeleteCase(input); + + } catch (DukeException error) { + printErrorMessage(PROMPT_TASK_NUMBER); + + } catch (IndexOutOfBoundsException error) { + printErrorMessage(getSensibleRange(Task.getTotalTasks())); + + } catch (NumberFormatException error) { + printErrorMessage(getSensibleRange(Task.getTotalTasks())); + } break; default: @@ -216,6 +232,16 @@ private static void executeDoneCase(String input) throws DukeException, printTaskCompletedMessage(tasks.get(index)); } + private static void executeDeleteCase(String input) throws DukeException, + IndexOutOfBoundsException,NumberFormatException { + if(input.length() < EXPECTED_LENGTH_FOR_DELETE_INPUT) { + throw new DukeException(); + } + int index = Integer.parseInt(input.split(" ")[1]) - 1; + printTaskDeletedMessage(tasks.get(index)); + tasks.remove(index); + } + private static void printTaskCompletedMessage(Task task) { printIndentationAndDivider(); printWordsWithIndentation(TASK_COMPLETED_MESSAGE); @@ -224,6 +250,16 @@ private static void printTaskCompletedMessage(Task task) { System.out.println(); } + private static void printTaskDeletedMessage(Task task) { + Task.setTotalTasks(Task.getTotalTasks() - 1); + printIndentationAndDivider(); + printWordsWithIndentation("Alright, I've removed this task:"); + printWordsWithIndentation(task.getStatusIconAndDescription()); + printWordsWithIndentation(notifyNumberOfTasks()); + printIndentationAndDivider(); + System.out.println(); + } + private static void executeListCase() { printIndentationAndDivider(); if(Task.getTotalTasks() == 0) { @@ -244,6 +280,9 @@ private static void printTask(int i) { } private static String getSensibleRange(int number) { + if(number < 1) { + return LIST_IS_EMPTY; + } return PROMPT_SENSIBLE_INDEX + number + " :)"; } } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 5fe1e303b..763d0a121 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -33,7 +33,7 @@ Hello from ___________________________________________________________ ___________________________________________________________ - Please tell me which task you finished :) + Please tell me which task you want to select :) ___________________________________________________________ ___________________________________________________________ @@ -54,15 +54,50 @@ Hello from ___________________________________________________________ ___________________________________________________________ - Please give a number between 1-3 :) + Please give a number between 1 and 3 :) ___________________________________________________________ ___________________________________________________________ - Please give a number between 1-3 :) + Please give a number between 1 and 3 :) ___________________________________________________________ ___________________________________________________________ - Please give a number between 1-3 :) + Please give a number between 1 and 3 :) + ___________________________________________________________ + + ___________________________________________________________ + Alright, I've removed this task: + [T][X] borrow book + Now you have 2 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + Please tell me which task you want to select :) + ___________________________________________________________ + + ___________________________________________________________ + Please give a number between 1 and 2 :) + ___________________________________________________________ + + ___________________________________________________________ + 1.[D][ ] return book (by: Sunday) + 2.[E][ ] project meeting (at: 4th Sep 2-3pm) + ___________________________________________________________ + + ___________________________________________________________ + Alright, I've removed this task: + [D][ ] return book (by: Sunday) + Now you have 1 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + Alright, I've removed this task: + [E][ ] project meeting (at: 4th Sep 2-3pm) + Now you have 0 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + You're a free man :) ___________________________________________________________ ___________________________________________________________ diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 6a5b84dd5..cc8592357 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -9,4 +9,11 @@ list done - done 0 done 5 +delete 1 +delete +delete 0 +list +delete 1 +delete 1 +list bye \ No newline at end of file From 7c89dda2cff162eda7aaa39cb049228f88523f7a Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 15 Sep 2021 20:19:08 +0800 Subject: [PATCH 16/33] Add save to file after termination and read from file when the program starts --- src/main/java/duke/Duke.java | 99 +++++++++++++++++++++++++++++++- src/main/java/task/Deadline.java | 5 ++ src/main/java/task/Event.java | 5 ++ src/main/java/task/Task.java | 6 +- src/main/java/task/ToDo.java | 5 ++ 5 files changed, 117 insertions(+), 3 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 45e2cecd6..3686e9005 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -6,7 +6,10 @@ import task.TaskType; import task.ToDo; +import java.io.FileWriter; +import java.io.IOException; import java.util.Scanner; +import java.io.File; public class Duke { public static final String DIVIDER = "___________________________________________________________"; @@ -35,19 +38,50 @@ public class Duke { " you're missing some arguments, do type 'help' if you're unsure :)"; public static final String PROMPT_TASK_NUMBER = "Please tell me which task you finished :)"; public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1-100 :)"; + public static final int EXPECTED_FILE_DEADLINE_AND_EVENT_LENGTH = 4; + public static final String FILE_NOT_CREATED = "Looks like you don't have a file for your tasks, let me create one now....."; + public static final String FILE_IS_EMPTY = "Looks like you don't have any task, you can start saving some now!"; + public static final String SEPARATOR = " | "; + public static final String UNEXPECTED_ERROR = "Oops,something unexpected happened"; + public static final String INCORRECT_FORMAT = "Oops, file format is incorrect. Please correct it!"; + public static final int EXPECTED_FILE_TODO_LENGTH = 3; public static Task[] tasks = new Task[100]; public static void main(String[] args) { printStartingMessage(); + getTaskFromFileMessage(); + initialFileProcessing(); Scanner in = new Scanner(System.in); String input = in.nextLine(); while (!input.equals("bye")) { processInput(input); input = in.nextLine(); } + finalFileProcessing(); printGoodbyeMessage(); } + private static void finalFileProcessing() { + try { + saveTasksToFile(); + } catch (IOException error){ + printErrorMessage(UNEXPECTED_ERROR); + System.exit(1); + } + } + + private static void initialFileProcessing() { + try { + loadTextFile(); + } catch (DukeException error) { + printErrorMessage(INCORRECT_FORMAT); + System.exit(1); + } catch (IOException error) { + printErrorMessage(UNEXPECTED_ERROR); + System.exit(1); + } + } + public static void printIndentationAndDivider() { System.out.print(INDENTATION); System.out.println(DIVIDER); @@ -169,7 +203,7 @@ private static String[] parseInputForDifferentTask(String input,TaskType type) { return parsedOutput; } - private static void addTask(String input,TaskType type) throws StringIndexOutOfBoundsException, DukeException { + private static void addTask(String input,TaskType type) throws StringIndexOutOfBoundsException, DukeException{ String[] parsedOutput = parseInputForDifferentTask(input,type); switch(type){ case TODO: @@ -224,4 +258,67 @@ private static void executeListCase() { printIndentationAndDivider(); System.out.println(); } + + private static void loadTextFile() throws IOException,DukeException { + File file = new File("duke.txt"); + if(file.createNewFile()) { + printErrorMessage(FILE_NOT_CREATED); + } + Scanner s = new Scanner(file); + if(!s.hasNext()) { + printErrorMessage(FILE_IS_EMPTY); + } + while (s.hasNext()) { + copyTasksToList(s); + Task.setTotalTasks(Task.getTotalTasks() + 1); + } + } + + private static void copyTasksToList(Scanner s) throws DukeException { + String[] parsedOutput = s.nextLine().split(" / "); + switch(parsedOutput[0]){ + case "T": + if(parsedOutput.length < EXPECTED_FILE_TODO_LENGTH) { + throw new DukeException(); + } + tasks[Task.getTotalTasks()] = new ToDo(parsedOutput[2]); + updateTaskStatus(parsedOutput[1]); + break; + case "E": + if(parsedOutput.length < EXPECTED_FILE_DEADLINE_AND_EVENT_LENGTH) { + throw new DukeException(); + } + tasks[Task.getTotalTasks()] = new Event(parsedOutput[2], parsedOutput[EXPECTED_FILE_TODO_LENGTH]); + updateTaskStatus(parsedOutput[1]); + break; + case "D": + if(parsedOutput.length < EXPECTED_FILE_DEADLINE_AND_EVENT_LENGTH) { + throw new DukeException(); + } + tasks[Task.getTotalTasks()] = new Deadline(parsedOutput[2], parsedOutput[EXPECTED_FILE_TODO_LENGTH]); + updateTaskStatus(parsedOutput[1]); + break; + } + } + + private static void getTaskFromFileMessage() { + printIndentationAndDivider(); + printWordsWithIndentation("Getting your tasks....."); + printIndentationAndDivider(); + System.out.println(); + } + + private static void updateTaskStatus(String done) { + if (done.equals("1")) { + tasks[Task.getTotalTasks()].markAsDone(); + } + } + + private static void saveTasksToFile() throws IOException{ + FileWriter fw = new FileWriter ("duke.txt"); + for (int i = 0; i < Task.getTotalTasks(); i++) { + fw.write(tasks[i].getStatusIconAndDescriptionForFile() + System.lineSeparator()); + } + fw.close(); + } } diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index af7240bea..361b92be3 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -14,4 +14,9 @@ public String getStatusIconAndDescription() { String icon = (isDone ? "X" : " "); return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(BY + date); } + + public String getStatusIconAndDescriptionForFile() { + String icon = (isDone ? "1" : "0"); + return IDENTIFIER + SEPARATOR + icon + SEPARATOR + description + SEPARATOR + date; + } } diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index b8b618f15..e3ec1fb03 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -14,4 +14,9 @@ public String getStatusIconAndDescription() { String icon = (isDone ? "X" : " "); return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(AT + dateAndTime); } + + public String getStatusIconAndDescriptionForFile() { + String icon = (isDone ? "1" : "0"); + return IDENTIFIER + SEPARATOR + icon + SEPARATOR + description + SEPARATOR + dateAndTime; + } } diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index 35dcae3a0..91487e120 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -1,6 +1,7 @@ package task; public class Task { + protected static final String SEPARATOR = " / "; protected boolean isDone; protected String description; @@ -20,8 +21,9 @@ public void markAsDone() { isDone = true; } - public void setDescription(String description) { - this.description = description; + public String getStatusIconAndDescriptionForFile() { + String icon = (isDone ? "1" : "0"); + return icon + SEPARATOR + description; } public static int getTotalTasks() { diff --git a/src/main/java/task/ToDo.java b/src/main/java/task/ToDo.java index dbe925971..65a188faf 100644 --- a/src/main/java/task/ToDo.java +++ b/src/main/java/task/ToDo.java @@ -11,4 +11,9 @@ public String getStatusIconAndDescription() { String icon = (isDone ? "X" : " "); return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description; } + + public String getStatusIconAndDescriptionForFile() { + String icon = (isDone ? "1" : "0"); + return IDENTIFIER + SEPARATOR + icon + SEPARATOR + description; + } } From 80a19f2029d4396b79d4ed834d81800b570ed89e Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 15 Sep 2021 21:58:28 +0800 Subject: [PATCH 17/33] Add help message and add jar file --- src/main/java/META-INF/MANIFEST.MF | 3 +++ src/main/java/duke/Duke.java | 30 ++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..2c9a9745c --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.Duke + diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index b1b1e6216..3ec02a89d 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -20,8 +20,8 @@ public class Duke { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - public static final String HELLO_MESSAGE_2 = "Hello! I'm Duke, your friendly neighbourhood task manager"; - public static final String HELLO_MESSAGE_3 = "What can I do for you? :D"; + public static final String HELLO_MESSAGE_2 = "Hello! I'm Duke, your friendly neighbourhood task manager\n" + + INDENTATION + "What can I do for you? :D\n" + INDENTATION + "(press help if you're not sure what to do!)"; public static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; public static final String ADDED_TO_LIST = "I've added this to your list :D"; public static final String DEADLINE_DESCRIPTION_AND_DATE_SPLITTER = " /by "; @@ -42,12 +42,25 @@ public class Duke { public static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 6; public static final String DEFAULT_ERROR_MESSAGE = "Oops, something went wrong!"; public static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 8; - public static final String FILE_NOT_CREATED = "Looks like you don't have a file for your tasks, let me create one now....."; + public static final String FILE_NOT_CREATED = "Looks like you don't have a file for your tasks, " + + "let me create one now....."; public static final String UNEXPECTED_ERROR = "Oops,something unexpected happened"; public static final String INCORRECT_FORMAT = "Oops, file format is incorrect. Please correct it!"; public static final String DONE = "Done!"; public static final String GETTING_TASK = "Getting your tasks....."; public static final String TASK_REMOVED = "Alright, I've removed this task:"; + public static final String HELP_MESSAGE = "list\n" +INDENTATION + + "- Shows you the list of tasks you have\n\n" +INDENTATION + + "todo (insert task description)\n" + INDENTATION + + "- Saves a general todo task\n\n" + INDENTATION + + "deadline (insert deadline description) /by (insert time)\n" + INDENTATION + + "- Saves a task with a given deadline\n\n" + INDENTATION + + "event (insert event description) /at (insert time interval)\n" + INDENTATION + + "- Saves an event happening at a certain time period\n\n" + INDENTATION + + "delete (insert number)\n" + INDENTATION + + "- Deletes a task with the corresponding number on the list\n\n" + INDENTATION + + "done (insert number)\n" + INDENTATION + + "- Marks the task with the corresponding number as done"; public static ArrayList tasks = new ArrayList<>(); public static void main(String[] args) { @@ -102,7 +115,6 @@ private static void printStartingMessage() { System.out.println("Hello from\n" + LOGO); printIndentationAndDivider(); printWordsWithIndentation(HELLO_MESSAGE_2); - printWordsWithIndentation(HELLO_MESSAGE_3); printIndentationAndDivider(); System.out.println(); } @@ -182,6 +194,9 @@ private static void processInput(String input) { } break; + case "help": + printHelpMessage(); + break; default: printMessage(TYPE_SUITABLE_COMMAND_MESSAGE); @@ -373,4 +388,11 @@ private static void saveTasksToFile() throws IOException{ } fw.close(); } + + private static void printHelpMessage() { + printIndentationAndDivider(); + printWordsWithIndentation(HELP_MESSAGE); + printIndentationAndDivider(); + System.out.println(); + } } From c2ec4137dc4a34996171c9ec12b9968a7b9a99fa Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Sat, 25 Sep 2021 16:03:22 +0800 Subject: [PATCH 18/33] Add classes for OOP with main code untouched --- src/main/java/commands/AddCommand.java | 42 ++ src/main/java/commands/Command.java | 14 + src/main/java/commands/CommandResult.java | 33 ++ src/main/java/commands/DeleteCommand.java | 39 ++ src/main/java/commands/DoneCommand.java | 23 + src/main/java/commands/ExitCommand.java | 10 + src/main/java/commands/HelpCommand.java | 22 + src/main/java/commands/IncorrectCommand.java | 10 + src/main/java/commands/ListCommand.java | 18 + src/main/java/commands/PrintOptions.java | 5 + src/main/java/constants/Constants.java | 9 + src/main/java/constants/Message.java | 50 +++ src/main/java/duke/DefaultException.java | 4 + src/main/java/duke/Duke.java | 436 ++++++------------- src/main/java/parser/Parser.java | 58 +++ src/main/java/storage/Storage.java | 62 +++ src/main/java/task/Deadline.java | 5 +- src/main/java/task/Event.java | 5 +- src/main/java/task/Task.java | 16 +- src/main/java/task/ToDo.java | 2 +- src/main/java/tasklist/TaskList.java | 28 ++ src/main/java/ui/Ui.java | 119 +++++ 22 files changed, 681 insertions(+), 329 deletions(-) create mode 100644 src/main/java/commands/AddCommand.java create mode 100644 src/main/java/commands/Command.java create mode 100644 src/main/java/commands/CommandResult.java create mode 100644 src/main/java/commands/DeleteCommand.java create mode 100644 src/main/java/commands/DoneCommand.java create mode 100644 src/main/java/commands/ExitCommand.java create mode 100644 src/main/java/commands/HelpCommand.java create mode 100644 src/main/java/commands/IncorrectCommand.java create mode 100644 src/main/java/commands/ListCommand.java create mode 100644 src/main/java/commands/PrintOptions.java create mode 100644 src/main/java/constants/Constants.java create mode 100644 src/main/java/constants/Message.java create mode 100644 src/main/java/duke/DefaultException.java create mode 100644 src/main/java/parser/Parser.java create mode 100644 src/main/java/storage/Storage.java create mode 100644 src/main/java/tasklist/TaskList.java create mode 100644 src/main/java/ui/Ui.java diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java new file mode 100644 index 000000000..c584f03ca --- /dev/null +++ b/src/main/java/commands/AddCommand.java @@ -0,0 +1,42 @@ +package commands; + +import duke.DefaultException; +import duke.DukeException; +import task.*; + +public class AddCommand extends Command{ + + private static final int EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE = 2; + public static final String ADDED_TO_LIST = "I've added this to your list :D"; + private final String[] parsedOutput; + private final TaskType type; + + public AddCommand(String[] parsedOutput, TaskType type) { + this.parsedOutput = parsedOutput; + this.type = type; + } + + public CommandResult execute() + throws StringIndexOutOfBoundsException, DukeException, DefaultException { + switch(type){ + case TODO: + tasks.addTask(new ToDo(parsedOutput[0])); + return new CommandResult(ADDED_TO_LIST,tasks,PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); + case EVENT: + if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { + throw new DukeException(); + } + tasks.addTask(new Event(parsedOutput[0], parsedOutput[1])); + return new CommandResult(ADDED_TO_LIST,tasks,PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); + case DEADLINE: + if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { + throw new DukeException(); + } + tasks.addTask(new Deadline(parsedOutput[0], parsedOutput[1])); + return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), + PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); + default : + throw new DefaultException(); + } + } +} diff --git a/src/main/java/commands/Command.java b/src/main/java/commands/Command.java new file mode 100644 index 000000000..adae808fe --- /dev/null +++ b/src/main/java/commands/Command.java @@ -0,0 +1,14 @@ +package commands; + +import duke.DefaultException; +import duke.DukeException; +import tasklist.TaskList; + +public abstract class Command { + protected TaskList tasks; + + public Command() { + this.tasks = new TaskList(); + } + public abstract CommandResult execute() throws DukeException, DefaultException; +} diff --git a/src/main/java/commands/CommandResult.java b/src/main/java/commands/CommandResult.java new file mode 100644 index 000000000..f1bcee789 --- /dev/null +++ b/src/main/java/commands/CommandResult.java @@ -0,0 +1,33 @@ +package commands; + +import task.Task; +import tasklist.TaskList; + +public class CommandResult{ + public final String feedbackToUser; + public final Task task; + public final TaskList tasks; + public final PrintOptions type; + + public CommandResult(String feedbackToUser, TaskList tasks,PrintOptions type) { + this.feedbackToUser = feedbackToUser; + this.task = null; + this.tasks = tasks; + this.type = type; + } + + public CommandResult(String feedbackToUser) { + this.feedbackToUser = feedbackToUser; + this.task = null; + this.tasks = null; + this.type = null; + } + + public CommandResult(String feedbackToUser, Task task,PrintOptions type) { + this.feedbackToUser = feedbackToUser; + this.task = task; + this.tasks = null; + this.type = type; + } + +} diff --git a/src/main/java/commands/DeleteCommand.java b/src/main/java/commands/DeleteCommand.java new file mode 100644 index 000000000..9f0900f27 --- /dev/null +++ b/src/main/java/commands/DeleteCommand.java @@ -0,0 +1,39 @@ +package commands; + +import duke.DukeException; +import task.Task; + +import static commands.ListCommand.LIST_IS_EMPTY; + +public class DeleteCommand extends Command{ + private static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 6; + public static final String TASK_REMOVED = "Alright, I've removed this task:"; + public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1 and "; + private String input; + + public DeleteCommand(String input) { + this.input = input; + } + + public CommandResult execute() throws DukeException, + NumberFormatException { + if(input.split(" ")[0].length() < EXPECTED_LENGTH_FOR_DELETE_INPUT) { + throw new DukeException(); + } + int index = Integer.parseInt(input.split(" ")[1]) - 1; + try { + Task temp = tasks.getTask(index); + tasks.deleteTask(index); + return new CommandResult(TASK_REMOVED,temp,"delete"); + } catch (IndexOutOfBoundsException error){ + return new CommandResult(getSensibleRange(Task.getTotalTasks())); + } + } + + public static String getSensibleRange(int number) { + if(number < 1) { + return LIST_IS_EMPTY; + } + return PROMPT_SENSIBLE_INDEX + number + " :)"; + } +} diff --git a/src/main/java/commands/DoneCommand.java b/src/main/java/commands/DoneCommand.java new file mode 100644 index 000000000..fa60a6890 --- /dev/null +++ b/src/main/java/commands/DoneCommand.java @@ -0,0 +1,23 @@ +package commands; + +import duke.DukeException; + +public class DoneCommand extends Command{ + private String input; + private static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 6; + private static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; + + public DoneCommand(String input) { + this.input = input; + } + + public CommandResult execute() throws DukeException, + IndexOutOfBoundsException,NumberFormatException { + if(input.length() < EXPECTED_LENGTH_FOR_DONE_INPUT) { + throw new DukeException(); + } + int index = Integer.parseInt(input.split(" ")[1]) - 1; + tasks.getTask(index).markAsDone(); + return new CommandResult(TASK_COMPLETED_MESSAGE,tasks.getTask(index),PrintOptions.ONLY_TASK); + } +} diff --git a/src/main/java/commands/ExitCommand.java b/src/main/java/commands/ExitCommand.java new file mode 100644 index 000000000..0402a72b9 --- /dev/null +++ b/src/main/java/commands/ExitCommand.java @@ -0,0 +1,10 @@ +package commands; + +public class ExitCommand extends Command{ + + public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; + + public CommandResult execute() { + return new CommandResult(GOODBYE_MESSAGE); + } +} diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java new file mode 100644 index 000000000..4bf6fc36d --- /dev/null +++ b/src/main/java/commands/HelpCommand.java @@ -0,0 +1,22 @@ +package commands; + +import static ui.Ui.INDENTATION; + +public class HelpCommand extends Command{ + private static final String HELP_MESSAGE = "list\n" +INDENTATION + + "- Shows you the list of tasks you have\n\n" +INDENTATION + + "todo (insert task description)\n" + INDENTATION + + "- Saves a general todo task\n\n" + INDENTATION + + "deadline (insert deadline description) /by (insert time)\n" + INDENTATION + + "- Saves a task with a given deadline\n\n" + INDENTATION + + "event (insert event description) /at (insert time interval)\n" + INDENTATION + + "- Saves an event happening at a certain time period\n\n" + INDENTATION + + "delete (insert number)\n" + INDENTATION + + "- Deletes a task with the corresponding number on the list\n\n" + INDENTATION + + "done (insert number)\n" + INDENTATION + + "- Marks the task with the corresponding number as done"; + + public CommandResult execute() { + return new CommandResult(HELP_MESSAGE); + } +} diff --git a/src/main/java/commands/IncorrectCommand.java b/src/main/java/commands/IncorrectCommand.java new file mode 100644 index 000000000..62e03ec81 --- /dev/null +++ b/src/main/java/commands/IncorrectCommand.java @@ -0,0 +1,10 @@ +package commands; + +public class IncorrectCommand extends Command{ + + public static final String DEFAULT_ERROR_MESSAGE = "Oops, something went wrong!"; + + public CommandResult execute() { + return new CommandResult(DEFAULT_ERROR_MESSAGE); + } +} diff --git a/src/main/java/commands/ListCommand.java b/src/main/java/commands/ListCommand.java new file mode 100644 index 000000000..e3a67d52c --- /dev/null +++ b/src/main/java/commands/ListCommand.java @@ -0,0 +1,18 @@ +package commands; + +import task.Task; + +public class ListCommand extends Command{ + + public static final String LIST_IS_EMPTY = "You're a free man :)"; + private static final String LIST_IS_NOT_EMPTY = "Here are the things you need to do : "; + + public CommandResult execute(){ + if(Task.getTotalTasks() == 0) { + return new CommandResult(LIST_IS_EMPTY); + } + else { + return new CommandResult(LIST_IS_NOT_EMPTY,tasks,PrintOptions.LIST); + } + } +} diff --git a/src/main/java/commands/PrintOptions.java b/src/main/java/commands/PrintOptions.java new file mode 100644 index 000000000..b541522fe --- /dev/null +++ b/src/main/java/commands/PrintOptions.java @@ -0,0 +1,5 @@ +package commands; + +public enum PrintOptions { + WITH_TASK_AND_NUMBER_OF_TASK,ONLY_TASK,LIST +} diff --git a/src/main/java/constants/Constants.java b/src/main/java/constants/Constants.java new file mode 100644 index 000000000..a346cfb0f --- /dev/null +++ b/src/main/java/constants/Constants.java @@ -0,0 +1,9 @@ +package constants; + +public class Constants { + public static final int TODO_STARTING_INDEX = 5; + public static final int DEADLINE_STARTING_INDEX = 9; + public static final int EVENT_STARTING_INDEX = 6; + public static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 6; + public static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 8; +} diff --git a/src/main/java/constants/Message.java b/src/main/java/constants/Message.java new file mode 100644 index 000000000..7d1518c3e --- /dev/null +++ b/src/main/java/constants/Message.java @@ -0,0 +1,50 @@ +package constants; + +public class Message { + + public static final String DIVIDER = "___________________________________________________________"; + public static final String INDENTATION = " "; + public static final String HELP_MESSAGE = "list\n" +INDENTATION + + "- Shows you the list of tasks you have\n\n" +INDENTATION + + "todo (insert task description)\n" + INDENTATION + + "- Saves a general todo task\n\n" + INDENTATION + + "deadline (insert deadline description) /by (insert time)\n" + INDENTATION + + "- Saves a task with a given deadline\n\n" + INDENTATION + + "event (insert event description) /at (insert time interval)\n" + INDENTATION + + "- Saves an event happening at a certain time period\n\n" + INDENTATION + + "delete (insert number)\n" + INDENTATION + + "- Deletes a task with the corresponding number on the list\n\n" + INDENTATION + + "done (insert number)\n" + INDENTATION + + "- Marks the task with the corresponding number as done"; + public static final String HELLO_MESSAGE_2 = "Hello! I'm Duke, your friendly neighbourhood task manager\n" + + INDENTATION + "What can I do for you? :D\n" + INDENTATION + "(type help if you're not sure what to do!)"; + public static final String LOGO = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + public static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; + public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; + public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry, I don't know what you mean. " + + "Please give me a suitable command :)"; + public static final String PROMPT_TASK_DESCRIPTION = "Please tell me what do you need to do :)"; + public static final String MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE = "Sorry," + + " you're missing some arguments, do type 'help' if you're unsure :)"; + public static final String PROMPT_TASK_NUMBER = "Please tell me which task you want to select :)"; + public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1 and "; + public static final String DEFAULT_ERROR_MESSAGE = "Oops, something went wrong!"; + public static final String FILE_NOT_CREATED = "Looks like you don't have a file for your tasks, " + + "let me create one now....."; + public static final String UNEXPECTED_ERROR = "Oops,something unexpected happened"; + public static final String INCORRECT_FORMAT = "Oops, file format is incorrect. Please correct it!"; + public static final String DONE = "Done!"; + public static final String GETTING_TASK = "Getting your tasks....."; + public static final String TASK_REMOVED = "Alright, I've removed this task:"; + + public static String getSensibleRange(int number) { + if(number < 1) { + return LIST_IS_EMPTY; + } + return PROMPT_SENSIBLE_INDEX + number + " :)"; + } +} diff --git a/src/main/java/duke/DefaultException.java b/src/main/java/duke/DefaultException.java new file mode 100644 index 000000000..b02068f4b --- /dev/null +++ b/src/main/java/duke/DefaultException.java @@ -0,0 +1,4 @@ +package duke; + +public class DefaultException extends Exception{ +} diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 3ec02a89d..edc84637f 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,398 +1,212 @@ package duke; -import task.Deadline; -import task.Event; +import constants.Constants; +import constants.Message; +import storage.Storage; import task.Task; import task.TaskType; -import task.ToDo; +import tasklist.TaskList; +import ui.Ui; -import java.io.FileWriter; import java.io.IOException; -import java.util.Scanner; -import java.util.ArrayList; -import java.io.File; public class Duke { - public static final String DIVIDER = "___________________________________________________________"; - public static final String INDENTATION = " "; - public static final String LOGO = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - public static final String HELLO_MESSAGE_2 = "Hello! I'm Duke, your friendly neighbourhood task manager\n" + - INDENTATION + "What can I do for you? :D\n" + INDENTATION + "(press help if you're not sure what to do!)"; - public static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; - public static final String ADDED_TO_LIST = "I've added this to your list :D"; - public static final String DEADLINE_DESCRIPTION_AND_DATE_SPLITTER = " /by "; - public static final String EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER = " /at "; - public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; - public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry, I don't know what you mean. " + - "Please give me a suitable command :)"; - public static final int TODO_STARTING_INDEX = 5; - public static final int DEADLINE_STARTING_INDEX = 9; - public static final int EVENT_STARTING_INDEX = 6; - public static final String PROMPT_TASK_DESCRIPTION = "Please tell me what do you need to do :)"; - public static final String LIST_IS_EMPTY = "You're a free man :)"; - public static final String MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE = "Sorry," + - " you're missing some arguments, do type 'help' if you're unsure :)"; - public static final String PROMPT_TASK_NUMBER = "Please tell me which task you want to select :)"; - public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1 and "; - public static final int EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE = 2; - public static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 6; - public static final String DEFAULT_ERROR_MESSAGE = "Oops, something went wrong!"; - public static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 8; - public static final String FILE_NOT_CREATED = "Looks like you don't have a file for your tasks, " + - "let me create one now....."; - public static final String UNEXPECTED_ERROR = "Oops,something unexpected happened"; - public static final String INCORRECT_FORMAT = "Oops, file format is incorrect. Please correct it!"; - public static final String DONE = "Done!"; - public static final String GETTING_TASK = "Getting your tasks....."; - public static final String TASK_REMOVED = "Alright, I've removed this task:"; - public static final String HELP_MESSAGE = "list\n" +INDENTATION + - "- Shows you the list of tasks you have\n\n" +INDENTATION + - "todo (insert task description)\n" + INDENTATION + - "- Saves a general todo task\n\n" + INDENTATION + - "deadline (insert deadline description) /by (insert time)\n" + INDENTATION + - "- Saves a task with a given deadline\n\n" + INDENTATION + - "event (insert event description) /at (insert time interval)\n" + INDENTATION + - "- Saves an event happening at a certain time period\n\n" + INDENTATION + - "delete (insert number)\n" + INDENTATION + - "- Deletes a task with the corresponding number on the list\n\n" + INDENTATION + - "done (insert number)\n" + INDENTATION + - "- Marks the task with the corresponding number as done"; - public static ArrayList tasks = new ArrayList<>(); - public static void main(String[] args) { - printStartingMessage(); - initialFileProcessing(); - Scanner in = new Scanner(System.in); - String input = in.nextLine(); - while (!input.equals("bye")) { - processInput(input); - input = in.nextLine(); - } - finalFileProcessing(); - printGoodbyeMessage(); - } - - private static void finalFileProcessing() { - try { - saveTasksToFile(); - } catch (IOException error){ - printMessage(UNEXPECTED_ERROR); - System.exit(1); - } - } + private Storage storage; + private Ui ui; + private TaskList tasks; - /** - * Reads the duke.txt file and saves it in the ArrayList "tasks" for modification - */ - private static void initialFileProcessing() { - printMessage(GETTING_TASK); + public Duke() { + ui = new Ui(); + storage = new Storage(); + tasks = new TaskList(); + ui.printMessage(Message.GETTING_TASK); try { - loadTextFile(); + storage.loadTextFile(tasks); + ui.printMessage(Message.DONE); } catch (IndexOutOfBoundsException error) { - printMessage(INCORRECT_FORMAT); + ui.printMessage(Message.INCORRECT_FORMAT); System.exit(1); + } catch (IOException error) { - printMessage(UNEXPECTED_ERROR); + ui.printMessage(Message.UNEXPECTED_ERROR); System.exit(1); - } - } - public static void printIndentationAndDivider() { - System.out.print(INDENTATION); - System.out.println(DIVIDER); + } catch (DukeException error) { + ui.printMessage(Message.FILE_NOT_CREATED); + ui.printMessage(Message.DONE); + + } } - public static void printWordsWithIndentation(String words) { - System.out.print(INDENTATION); - System.out.println(words); + public void run(){ + ui.printStartingMessage(); + while (true){ + String input = ui.getUserCommand(); + processInput(input); + } } - private static void printStartingMessage() { - System.out.println("Hello from\n" + LOGO); - printIndentationAndDivider(); - printWordsWithIndentation(HELLO_MESSAGE_2); - printIndentationAndDivider(); - System.out.println(); + public static void main(String[] args) { + new Duke().run(); } - private static void printGoodbyeMessage() { - printIndentationAndDivider(); - printWordsWithIndentation(GOODBYE_MESSAGE); - printIndentationAndDivider(); + private void finalFileProcessing() { + try { + storage.saveTasksToFile(tasks); + } catch (IOException error){ + ui.printMessage(Message.UNEXPECTED_ERROR); + System.exit(1); + } } /** + * Performs addition of different tasks, deletion of tasks, or list printing, depending on + * the first word. * * @param input + * input comes from the user * - * Performs addition of different tasks, deletion of tasks, or list printing, depending on - * the first word. */ - private static void processInput(String input) { + private void processInput(String input) { switch(input.split(" ")[0].toLowerCase()) { case "list": - executeListCase(); + runListCommand(); break; case "done": - try { - executeDoneCase(input); - - } catch (DukeException error) { - printMessage(PROMPT_TASK_NUMBER); - - } catch (IndexOutOfBoundsException error) { - printMessage(getSensibleRange(Task.getTotalTasks())); - - } catch (NumberFormatException error) { - printMessage(getSensibleRange(Task.getTotalTasks())); - - } + runDoneCommand(input); break; case "todo": - try { - executeTaskCase(input, TODO_STARTING_INDEX, TaskType.TODO); - - } catch (StringIndexOutOfBoundsException error) { - printMessage(PROMPT_TASK_DESCRIPTION); - - } + runToDoCommand(input); break; case "deadline": - try { - executeTaskCase(input, DEADLINE_STARTING_INDEX,TaskType.DEADLINE); - - } catch (StringIndexOutOfBoundsException error) { - printMessage(PROMPT_TASK_DESCRIPTION); - - } + runDeadlineCommand(input); break; case "event": - try { - executeTaskCase(input, EVENT_STARTING_INDEX,TaskType.EVENT); - - } catch (StringIndexOutOfBoundsException error) { - printMessage(PROMPT_TASK_DESCRIPTION); - - } + runEventCommand(input); break; case "delete": - try { - executeDeleteCase(input); - - } catch (DukeException error) { - printMessage(PROMPT_TASK_NUMBER); - - } catch (IndexOutOfBoundsException error) { - printMessage(getSensibleRange(Task.getTotalTasks())); - - } catch (NumberFormatException error) { - printMessage(getSensibleRange(Task.getTotalTasks())); - - } + runDeleteCommand(input); break; case "help": - printHelpMessage(); + ui.printHelpMessage(); break; + case "bye" : + finalFileProcessing(); + ui.printGoodbyeMessage(); + System.exit(0); default: - printMessage(TYPE_SUITABLE_COMMAND_MESSAGE); + ui.printMessage(Message.TYPE_SUITABLE_COMMAND_MESSAGE); } } - private static void printTaskMessage() { - printIndentationAndDivider(); - printWordsWithIndentation(ADDED_TO_LIST); - printWordsWithIndentation(tasks.get(Task.getTotalTasks() - 1).getStatusIconAndDescription()); - printWordsWithIndentation(notifyNumberOfTasks()); - printIndentationAndDivider(); - System.out.println(); - } - - private static void printMessage(String message) { - printIndentationAndDivider(); - printWordsWithIndentation(message); - printIndentationAndDivider(); - System.out.println(); - } - - private static void executeTaskCase(String input, int starting_index, TaskType type) { - String description = input.strip().substring(starting_index); + private void runDeleteCommand(String input) { try { - addTask(description, type); - printTaskMessage(); + executeDeleteCase(input); } catch (DukeException error) { - printMessage(MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE); + ui.printMessage(Message.PROMPT_TASK_NUMBER); - } - } + } catch (NumberFormatException error) { + ui.printMessage(Message.getSensibleRange(Task.getTotalTasks())); - private static String[] parseInputForDifferentTask(String input,TaskType type) { - String[] parsedOutput = {}; - switch(type) { - case TODO: - parsedOutput = new String[]{input}; - break; - case DEADLINE: - parsedOutput = input.split(DEADLINE_DESCRIPTION_AND_DATE_SPLITTER); - break; - case EVENT: - parsedOutput = input.split(EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER); - break; - default : - printMessage(DEFAULT_ERROR_MESSAGE); - break; } - return parsedOutput; } - private static void addTask(String input,TaskType type) - throws StringIndexOutOfBoundsException, DukeException { - String[] parsedOutput = parseInputForDifferentTask(input,type); - switch(type){ - case TODO: - tasks.add(new ToDo(parsedOutput[0])); - break; - case EVENT: - if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { - throw new DukeException(); - } - tasks.add(new Event(parsedOutput[0], parsedOutput[1])); - break; - case DEADLINE: - if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { - throw new DukeException(); - } - tasks.add(new Deadline(parsedOutput[0], parsedOutput[1])); - break; - default : - printMessage(DEFAULT_ERROR_MESSAGE); - return; - } - Task.setTotalTasks(Task.getTotalTasks() + 1); - } + private void runEventCommand(String input) { + try { + executeTaskCase(input, Constants.EVENT_STARTING_INDEX, TaskType.EVENT); - private static String notifyNumberOfTasks() { - return "Now you have " + Task.getTotalTasks() + " task(s) in the list"; - } + } catch (StringIndexOutOfBoundsException error) { + ui.printMessage(Message.PROMPT_TASK_DESCRIPTION); - private static void executeDoneCase(String input) throws DukeException, - IndexOutOfBoundsException,NumberFormatException { - if(input.length() < EXPECTED_LENGTH_FOR_DONE_INPUT) { - throw new DukeException(); } - int index = Integer.parseInt(input.split(" ")[1]) - 1; - tasks.get(index).markAsDone(); - printTaskCompletedMessage(tasks.get(index)); } - private static void executeDeleteCase(String input) throws DukeException, - IndexOutOfBoundsException,NumberFormatException { - if(input.length() < EXPECTED_LENGTH_FOR_DELETE_INPUT) { - throw new DukeException(); + private void runDeadlineCommand(String input) { + try { + executeTaskCase(input, Constants.DEADLINE_STARTING_INDEX, TaskType.DEADLINE); + + } catch (StringIndexOutOfBoundsException error) { + ui.printMessage(Message.PROMPT_TASK_DESCRIPTION); + } - int index = Integer.parseInt(input.split(" ")[1]) - 1; - printTaskDeletedMessage(tasks.get(index)); - tasks.remove(index); } - private static void printTaskCompletedMessage(Task task) { - printIndentationAndDivider(); - printWordsWithIndentation(TASK_COMPLETED_MESSAGE); - printWordsWithIndentation(task.getStatusIconAndDescription()); - printIndentationAndDivider(); - System.out.println(); - } + private void runToDoCommand(String input) { + try { + executeTaskCase(input, Constants.TODO_STARTING_INDEX, TaskType.TODO); - private static void printTaskDeletedMessage(Task task) { - Task.setTotalTasks(Task.getTotalTasks() - 1); - printIndentationAndDivider(); - printWordsWithIndentation(TASK_REMOVED); - printWordsWithIndentation(task.getStatusIconAndDescription()); - printWordsWithIndentation(notifyNumberOfTasks()); - printIndentationAndDivider(); - System.out.println(); - } + } catch (StringIndexOutOfBoundsException error) { + ui.printMessage(Message.PROMPT_TASK_DESCRIPTION); - private static void executeListCase() { - printIndentationAndDivider(); - if(Task.getTotalTasks() == 0) { - printWordsWithIndentation(LIST_IS_EMPTY); } - printTask(); - printIndentationAndDivider(); - System.out.println(); } - /** - * Prints the tasks based on the given format - */ - private static void printTask() { - for (int i = 0; i < Task.getTotalTasks(); i++) { - printWordsWithIndentation(i + 1 + "." + tasks.get(i).getStatusIconAndDescription()); - } - } + private void runDoneCommand(String input) { + try { + executeDoneCase(input); - private static String getSensibleRange(int number) { - if(number < 1) { - return LIST_IS_EMPTY; - } - return PROMPT_SENSIBLE_INDEX + number + " :)"; - } + } catch (DukeException error) { + ui.printMessage(Message.PROMPT_TASK_NUMBER); + + } catch (IndexOutOfBoundsException error) { + ui.printMessage(Message.getSensibleRange(Task.getTotalTasks())); + + } catch (NumberFormatException error) { + ui.printMessage(Message.getSensibleRange(Task.getTotalTasks())); - private static void loadTextFile() throws IOException { - File file = new File("duke.txt"); - if(file.createNewFile()) { - printMessage(FILE_NOT_CREATED); - } - printMessage(DONE); - Scanner s = new Scanner(file); - while (s.hasNext()) { - copyTasksToList(s); - Task.setTotalTasks(Task.getTotalTasks() + 1); } } - private static void copyTasksToList(Scanner s) throws IndexOutOfBoundsException { - String[] parsedOutput = s.nextLine().split(" / "); - switch(parsedOutput[0]){ - case "T": - tasks.add(new ToDo(parsedOutput[2])); - updateTaskStatus(parsedOutput[1]); - break; - case "E": - tasks.add(new Event(parsedOutput[2], parsedOutput[3])); - updateTaskStatus(parsedOutput[1]); - break; - case "D": - tasks.add(new Deadline(parsedOutput[2], parsedOutput[3])); - updateTaskStatus(parsedOutput[1]); - break; + private void executeTaskCase(String input, int starting_index, TaskType type) { + String description = input.strip().substring(starting_index); + try { + tasks.addTask(description, type); + ui.printAddTaskMessage(tasks); + + } catch (DukeException error) { + ui.printMessage(Message.MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE); + + } catch (DefaultException error) { + ui.printMessage(Message.DEFAULT_ERROR_MESSAGE); } } - private static void updateTaskStatus(String done) { - if (done.equals("1")) { - tasks.get(Task.getTotalTasks()).markAsDone(); + private void executeDoneCase(String input) throws DukeException, + IndexOutOfBoundsException,NumberFormatException { + if(input.length() < Constants.EXPECTED_LENGTH_FOR_DONE_INPUT) { + throw new DukeException(); } + int index = Integer.parseInt(input.split(" ")[1]) - 1; + tasks.getTask(index).markAsDone(); + ui.printTaskCompletedMessage(tasks.getTask(index)); } - private static void saveTasksToFile() throws IOException{ - FileWriter fw = new FileWriter ("duke.txt"); - for (int i = 0; i < Task.getTotalTasks(); i++) { - fw.write(tasks.get(i).getStatusIconAndDescriptionForFile() + System.lineSeparator()); + private void executeDeleteCase(String input) throws DukeException, + NumberFormatException { + if(input.length() < Constants.EXPECTED_LENGTH_FOR_DELETE_INPUT) { + throw new DukeException(); + } + int index = Integer.parseInt(input.split(" ")[1]) - 1; + try { + Task temp = tasks.getTask(index); + tasks.deleteTask(index); + ui.printTaskDeletedMessage(temp); + } catch (IndexOutOfBoundsException error){ + ui.printMessage(Message.getSensibleRange(Task.getTotalTasks())); } - fw.close(); } - private static void printHelpMessage() { - printIndentationAndDivider(); - printWordsWithIndentation(HELP_MESSAGE); - printIndentationAndDivider(); + private void runListCommand() { + ui.printIndentationAndDivider(); + if(Task.getTotalTasks() == 0) { + ui.printWordsWithIndentation(Message.LIST_IS_EMPTY); + } + ui.printTask(tasks); + ui.printIndentationAndDivider(); System.out.println(); } + } diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java new file mode 100644 index 000000000..c72451ad6 --- /dev/null +++ b/src/main/java/parser/Parser.java @@ -0,0 +1,58 @@ +package parser; + +import commands.*; +import task.TaskType; + +public class Parser { + + private static final String DEADLINE_DESCRIPTION_AND_DATE_SPLITTER = " /by "; + private static final String EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER = " /at "; + private static final int TODO_STARTING_INDEX = 5; + private static final int DEADLINE_STARTING_INDEX = 9; + private static final int EVENT_STARTING_INDEX = 6; + + public Command parseInputForDifferentTask(String input) { + String type = input.split(" ")[0].toLowerCase(); + switch(type) { + case "todo": + return prepareAddCommand(input,TaskType.TODO); + case "deadline": + return prepareAddCommand(input,TaskType.DEADLINE); + case "event": + return prepareAddCommand(input,TaskType.EVENT); + case "list": + return new ListCommand(); + case "done": + return new DoneCommand(input); + case "help": + return new HelpCommand(); + case "delete": + return new DeleteCommand(input); + case "bye": + return new ExitCommand(); + default : + return new IncorrectCommand(); + } + } + + private Command prepareAddCommand(String input,TaskType type) { + String description; + String parsedOutput[]; + switch(type){ + case TODO: + description = input.strip().substring(TODO_STARTING_INDEX); + parsedOutput = new String[]{description}; + return new AddCommand(parsedOutput,TaskType.TODO); + case EVENT: + description = input.strip().substring(EVENT_STARTING_INDEX); + parsedOutput = description.split(EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER); + return new AddCommand(parsedOutput,TaskType.EVENT); + case DEADLINE: + description = input.strip().substring(DEADLINE_STARTING_INDEX); + parsedOutput = description.split(DEADLINE_DESCRIPTION_AND_DATE_SPLITTER); + return new AddCommand(parsedOutput,TaskType.DEADLINE); + default: + return new IncorrectCommand(); + } + } +} diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java new file mode 100644 index 000000000..917e0f993 --- /dev/null +++ b/src/main/java/storage/Storage.java @@ -0,0 +1,62 @@ +package storage; + +import duke.DukeException; +import task.Deadline; +import task.Event; +import task.Task; +import task.ToDo; +import tasklist.TaskList; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Scanner; + +public class Storage { + + public Storage() {} + + public void loadTextFile(TaskList tasks) throws IOException, DukeException { + File file = new File("./duke.txt"); + if(file.createNewFile()) { + throw new DukeException(); + } + Scanner s = new Scanner(file); + while (s.hasNext()) { + addTaskFromFile(s,tasks); + Task.setTotalTasks(Task.getTotalTasks() + 1); + } + } + + public void saveTasksToFile(TaskList tasks) throws IOException{ + FileWriter fw = new FileWriter ("./duke.txt"); + for (int i = 0; i < Task.getTotalTasks(); i++) { + fw.write(tasks.getTask(i).getStatusIconAndDescriptionForFile() + System.lineSeparator()); + } + fw.close(); + } + + public void addTaskFromFile(Scanner s, TaskList tasks) throws IndexOutOfBoundsException { + String[] parsedOutput = s.nextLine().split(" / "); + switch(parsedOutput[0]){ + case "T": + tasks.addTask(new ToDo(parsedOutput[2])); + updateTaskStatus(parsedOutput[1],tasks); + break; + case "E": + tasks.addTask(new Event(parsedOutput[2], parsedOutput[3])); + updateTaskStatus(parsedOutput[1],tasks); + break; + case "D": + tasks.addTask(new Deadline(parsedOutput[2], parsedOutput[3])); + updateTaskStatus(parsedOutput[1],tasks); + break; + } + } + + private void updateTaskStatus(String done, TaskList tasks) { + if (done.equals("1")) { + tasks.getTask(Task.getTotalTasks()).markAsDone(); + } + } +} diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index 361b92be3..5818ae07f 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -6,13 +6,14 @@ public class Deadline extends Task{ private static final String IDENTIFIER = "D"; public Deadline(String description,String date) { - super(description); + this.description = description; this.date = date; } public String getStatusIconAndDescription() { String icon = (isDone ? "X" : " "); - return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(BY + date); + return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + + addBrackets(BY + date); } public String getStatusIconAndDescriptionForFile() { diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index e3ec1fb03..8b77bbddb 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -6,13 +6,14 @@ public class Event extends Task{ private static final String IDENTIFIER = "E"; public Event(String description, String dateAndTime) { - super(description); + this.description = description; this.dateAndTime = dateAndTime; } public String getStatusIconAndDescription() { String icon = (isDone ? "X" : " "); - return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(AT + dateAndTime); + return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + + addBrackets(AT + dateAndTime); } public String getStatusIconAndDescriptionForFile() { diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index 91487e120..b00db6eac 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -1,30 +1,20 @@ package task; -public class Task { +public abstract class Task { protected static final String SEPARATOR = " / "; protected boolean isDone; protected String description; private static int totalTasks = 0; - public Task(String description){ - this.description = description; - isDone = false; - } + public abstract String getStatusIconAndDescription(); - public String getStatusIconAndDescription() { - String icon = (isDone ? "X" : " "); - return addSquareBrackets(icon) + " " + description; - } public void markAsDone() { isDone = true; } - public String getStatusIconAndDescriptionForFile() { - String icon = (isDone ? "1" : "0"); - return icon + SEPARATOR + description; - } + public abstract String getStatusIconAndDescriptionForFile(); public static int getTotalTasks() { return totalTasks; diff --git a/src/main/java/task/ToDo.java b/src/main/java/task/ToDo.java index 65a188faf..378d5de52 100644 --- a/src/main/java/task/ToDo.java +++ b/src/main/java/task/ToDo.java @@ -4,7 +4,7 @@ public class ToDo extends Task{ private static final String IDENTIFIER = "T"; public ToDo(String description) { - super(description); + this.description = description; } public String getStatusIconAndDescription() { diff --git a/src/main/java/tasklist/TaskList.java b/src/main/java/tasklist/TaskList.java new file mode 100644 index 000000000..1ac4930bd --- /dev/null +++ b/src/main/java/tasklist/TaskList.java @@ -0,0 +1,28 @@ +package tasklist; + +import task.Task; + +import java.util.ArrayList; + +public class TaskList { + + private ArrayList tasks; + + public TaskList() { + this.tasks = new ArrayList<>(); + } + public void addTask(Task task) { + tasks.add(task); + Task.setTotalTasks(Task.getTotalTasks() + 1); + + } + + public void deleteTask(int index) throws IndexOutOfBoundsException { + tasks.remove(index); + Task.setTotalTasks(Task.getTotalTasks() - 1); + } + + public Task getTask(int index) { + return tasks.get(index); + } +} diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java new file mode 100644 index 000000000..6dcbfb127 --- /dev/null +++ b/src/main/java/ui/Ui.java @@ -0,0 +1,119 @@ +package ui; + +import commands.CommandResult; + +import constants.Message; +import task.Task; + +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Scanner; + + +public class Ui { + + private final Scanner in; + private final PrintStream out; + + public static final String INDENTATION = " "; + private static final Boolean NOTIFY_NUMBER_OF_TASK = true; + private static final Boolean PRINT_TASK = true; + + public Ui(){ + this(System.in,System.out); + } + + public Ui(InputStream in, PrintStream out){ + this.in = new Scanner(in); + this.out = out; + } + + public String getUserCommand() { + String input = in.nextLine(); + return input; + } + + public void printIndentationAndDivider() { + out.print(Message.INDENTATION); + out.println(Message.DIVIDER); + } + + public void printWordsWithIndentation(String words) { + out.print(Message.INDENTATION); + out.println(words); + } + + public void printStartingMessage() { + out.println("Hello from\n" + Message.LOGO); + printIndentationAndDivider(); + printWordsWithIndentation(Message.HELLO_MESSAGE_2); + printIndentationAndDivider(); + out.println(); + } + + public void printGoodbyeMessage() { + printIndentationAndDivider(); + printWordsWithIndentation(Message.GOODBYE_MESSAGE); + printIndentationAndDivider(); + } + + private void printTaskMessage(CommandResult result, Boolean notifyNumberOfTasks, Boolean printTask) { + printIndentationAndDivider(); + printWordsWithIndentation(result.feedbackToUser); + if(printTask) { + printWordsWithIndentation(result.task.getStatusIconAndDescription()); + } + if(notifyNumberOfTasks){ + printWordsWithIndentation(notifyNumberOfTasks()); + } + printIndentationAndDivider(); + System.out.println(); + } + + public void printMessage(CommandResult result) { + switch(result.type){ + case WITH_TASK_AND_NUMBER_OF_TASK: + printTaskMessage(result,NOTIFY_NUMBER_OF_TASK,PRINT_TASK); + break; + case ONLY_TASK: + printTaskMessage(result,!NOTIFY_NUMBER_OF_TASK,PRINT_TASK); + case LIST: + printList(result); + default: + printTaskMessage(result,!NOTIFY_NUMBER_OF_TASK,!PRINT_TASK); + } + } + + + + /** + * Prints the tasks based on the given format + */ + public void printTask(CommandResult result) { + for (int i = 0; i < Task.getTotalTasks(); i++) { + printWordsWithIndentation(i + 1 + "." + result.tasks.getTask(i).getStatusIconAndDescription()); + } + } + + public void printList(CommandResult result) { + printIndentationAndDivider(); + if(Task.getTotalTasks() == 0) { + printWordsWithIndentation(result.feedbackToUser); + } + printTask(result); + printIndentationAndDivider(); + System.out.println(); + } + + public void printHelpMessage() { + printIndentationAndDivider(); + printWordsWithIndentation(Message.HELP_MESSAGE); + printIndentationAndDivider(); + System.out.println(); + } + + private String notifyNumberOfTasks() { + int taskNumber = Task.getTotalTasks(); + return "Now you have " + taskNumber + " task(s) in the list"; + } +} From bcb0f124b63f032274a53e87e0bc58fb34fe56d2 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Sat, 25 Sep 2021 19:37:00 +0800 Subject: [PATCH 19/33] Update the code to have OOP style --- src/main/java/commands/AddCommand.java | 15 +- src/main/java/commands/Command.java | 3 + src/main/java/commands/CommandResult.java | 4 +- src/main/java/commands/DeleteCommand.java | 15 +- src/main/java/commands/DoneCommand.java | 11 +- src/main/java/commands/ExitCommand.java | 2 +- src/main/java/commands/HelpCommand.java | 2 +- src/main/java/commands/IncorrectCommand.java | 10 +- src/main/java/commands/ListCommand.java | 4 +- src/main/java/commands/PrintOptions.java | 2 +- src/main/java/constants/Message.java | 26 +-- src/main/java/duke/Duke.java | 182 +++---------------- src/main/java/parser/Parser.java | 25 ++- src/main/java/storage/Storage.java | 9 +- src/main/java/tasklist/TaskList.java | 1 + src/main/java/ui/Ui.java | 38 ++-- text-ui-test/EXPECTED.TXT | 17 +- text-ui-test/runtest.bat | 3 +- 18 files changed, 133 insertions(+), 236 deletions(-) diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java index c584f03ca..766889a58 100644 --- a/src/main/java/commands/AddCommand.java +++ b/src/main/java/commands/AddCommand.java @@ -7,6 +7,8 @@ public class AddCommand extends Command{ private static final int EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE = 2; + private static final String PROMPT_CORRECT_FORMAT = "Sorry, you're missing some arguments," + + " do type 'help' if you're unsure :)"; public static final String ADDED_TO_LIST = "I've added this to your list :D"; private final String[] parsedOutput; private final TaskType type; @@ -16,21 +18,22 @@ public AddCommand(String[] parsedOutput, TaskType type) { this.type = type; } - public CommandResult execute() - throws StringIndexOutOfBoundsException, DukeException, DefaultException { + public CommandResult execute() throws DefaultException { switch(type){ case TODO: tasks.addTask(new ToDo(parsedOutput[0])); - return new CommandResult(ADDED_TO_LIST,tasks,PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); + return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), + PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); case EVENT: if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { - throw new DukeException(); + return new CommandResult(PROMPT_CORRECT_FORMAT,PrintOptions.DEFAULT); } tasks.addTask(new Event(parsedOutput[0], parsedOutput[1])); - return new CommandResult(ADDED_TO_LIST,tasks,PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); + return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), + PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); case DEADLINE: if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { - throw new DukeException(); + return new CommandResult(PROMPT_CORRECT_FORMAT,PrintOptions.DEFAULT); } tasks.addTask(new Deadline(parsedOutput[0], parsedOutput[1])); return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), diff --git a/src/main/java/commands/Command.java b/src/main/java/commands/Command.java index adae808fe..00663a41c 100644 --- a/src/main/java/commands/Command.java +++ b/src/main/java/commands/Command.java @@ -11,4 +11,7 @@ public Command() { this.tasks = new TaskList(); } public abstract CommandResult execute() throws DukeException, DefaultException; + public void passList (TaskList tasks) { + this.tasks = tasks; + } } diff --git a/src/main/java/commands/CommandResult.java b/src/main/java/commands/CommandResult.java index f1bcee789..aa4409c8c 100644 --- a/src/main/java/commands/CommandResult.java +++ b/src/main/java/commands/CommandResult.java @@ -16,11 +16,11 @@ public CommandResult(String feedbackToUser, TaskList tasks,PrintOptions type) { this.type = type; } - public CommandResult(String feedbackToUser) { + public CommandResult(String feedbackToUser,PrintOptions type) { this.feedbackToUser = feedbackToUser; this.task = null; this.tasks = null; - this.type = null; + this.type = type; } public CommandResult(String feedbackToUser, Task task,PrintOptions type) { diff --git a/src/main/java/commands/DeleteCommand.java b/src/main/java/commands/DeleteCommand.java index 9f0900f27..38da31456 100644 --- a/src/main/java/commands/DeleteCommand.java +++ b/src/main/java/commands/DeleteCommand.java @@ -1,12 +1,12 @@ package commands; -import duke.DukeException; +import constants.Message; import task.Task; import static commands.ListCommand.LIST_IS_EMPTY; public class DeleteCommand extends Command{ - private static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 6; + private static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 2; public static final String TASK_REMOVED = "Alright, I've removed this task:"; public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1 and "; private String input; @@ -15,18 +15,17 @@ public DeleteCommand(String input) { this.input = input; } - public CommandResult execute() throws DukeException, - NumberFormatException { - if(input.split(" ")[0].length() < EXPECTED_LENGTH_FOR_DELETE_INPUT) { - throw new DukeException(); + public CommandResult execute() throws NumberFormatException { + if(input.split(" ").length < EXPECTED_LENGTH_FOR_DELETE_INPUT) { + return new CommandResult(Message.PROMPT_TASK_NUMBER,PrintOptions.DEFAULT); } int index = Integer.parseInt(input.split(" ")[1]) - 1; try { Task temp = tasks.getTask(index); tasks.deleteTask(index); - return new CommandResult(TASK_REMOVED,temp,"delete"); + return new CommandResult(TASK_REMOVED,temp,PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); } catch (IndexOutOfBoundsException error){ - return new CommandResult(getSensibleRange(Task.getTotalTasks())); + return new CommandResult(getSensibleRange(Task.getTotalTasks()),PrintOptions.DEFAULT); } } diff --git a/src/main/java/commands/DoneCommand.java b/src/main/java/commands/DoneCommand.java index fa60a6890..bc47a50f6 100644 --- a/src/main/java/commands/DoneCommand.java +++ b/src/main/java/commands/DoneCommand.java @@ -1,20 +1,19 @@ package commands; -import duke.DukeException; +import constants.Message; public class DoneCommand extends Command{ private String input; - private static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 6; + private static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 2; private static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; public DoneCommand(String input) { this.input = input; } - public CommandResult execute() throws DukeException, - IndexOutOfBoundsException,NumberFormatException { - if(input.length() < EXPECTED_LENGTH_FOR_DONE_INPUT) { - throw new DukeException(); + public CommandResult execute() throws IndexOutOfBoundsException,NumberFormatException { + if(input.split(" ").length < EXPECTED_LENGTH_FOR_DONE_INPUT) { + return new CommandResult(Message.PROMPT_TASK_NUMBER,PrintOptions.DEFAULT); } int index = Integer.parseInt(input.split(" ")[1]) - 1; tasks.getTask(index).markAsDone(); diff --git a/src/main/java/commands/ExitCommand.java b/src/main/java/commands/ExitCommand.java index 0402a72b9..dd788f201 100644 --- a/src/main/java/commands/ExitCommand.java +++ b/src/main/java/commands/ExitCommand.java @@ -5,6 +5,6 @@ public class ExitCommand extends Command{ public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; public CommandResult execute() { - return new CommandResult(GOODBYE_MESSAGE); + return new CommandResult(GOODBYE_MESSAGE,PrintOptions.DEFAULT); } } diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java index 4bf6fc36d..a800fc87d 100644 --- a/src/main/java/commands/HelpCommand.java +++ b/src/main/java/commands/HelpCommand.java @@ -17,6 +17,6 @@ public class HelpCommand extends Command{ "- Marks the task with the corresponding number as done"; public CommandResult execute() { - return new CommandResult(HELP_MESSAGE); + return new CommandResult(HELP_MESSAGE,PrintOptions.DEFAULT); } } diff --git a/src/main/java/commands/IncorrectCommand.java b/src/main/java/commands/IncorrectCommand.java index 62e03ec81..15b1d94d3 100644 --- a/src/main/java/commands/IncorrectCommand.java +++ b/src/main/java/commands/IncorrectCommand.java @@ -1,10 +1,16 @@ package commands; +import constants.Message; + public class IncorrectCommand extends Command{ - public static final String DEFAULT_ERROR_MESSAGE = "Oops, something went wrong!"; + private String message; + + public IncorrectCommand(String message) { + this.message = message; + } public CommandResult execute() { - return new CommandResult(DEFAULT_ERROR_MESSAGE); + return new CommandResult(message,PrintOptions.DEFAULT); } } diff --git a/src/main/java/commands/ListCommand.java b/src/main/java/commands/ListCommand.java index e3a67d52c..e8490c7a5 100644 --- a/src/main/java/commands/ListCommand.java +++ b/src/main/java/commands/ListCommand.java @@ -5,11 +5,11 @@ public class ListCommand extends Command{ public static final String LIST_IS_EMPTY = "You're a free man :)"; - private static final String LIST_IS_NOT_EMPTY = "Here are the things you need to do : "; + private static final String LIST_IS_NOT_EMPTY = "Here are the things you need to do :"; public CommandResult execute(){ if(Task.getTotalTasks() == 0) { - return new CommandResult(LIST_IS_EMPTY); + return new CommandResult(LIST_IS_EMPTY,PrintOptions.DEFAULT); } else { return new CommandResult(LIST_IS_NOT_EMPTY,tasks,PrintOptions.LIST); diff --git a/src/main/java/commands/PrintOptions.java b/src/main/java/commands/PrintOptions.java index b541522fe..d0583d9d6 100644 --- a/src/main/java/commands/PrintOptions.java +++ b/src/main/java/commands/PrintOptions.java @@ -1,5 +1,5 @@ package commands; public enum PrintOptions { - WITH_TASK_AND_NUMBER_OF_TASK,ONLY_TASK,LIST + WITH_TASK_AND_NUMBER_OF_TASK,ONLY_TASK,LIST,DEFAULT } diff --git a/src/main/java/constants/Message.java b/src/main/java/constants/Message.java index 7d1518c3e..f237c3ac1 100644 --- a/src/main/java/constants/Message.java +++ b/src/main/java/constants/Message.java @@ -1,21 +1,11 @@ package constants; +import static commands.ListCommand.LIST_IS_EMPTY; + public class Message { public static final String DIVIDER = "___________________________________________________________"; public static final String INDENTATION = " "; - public static final String HELP_MESSAGE = "list\n" +INDENTATION + - "- Shows you the list of tasks you have\n\n" +INDENTATION + - "todo (insert task description)\n" + INDENTATION + - "- Saves a general todo task\n\n" + INDENTATION + - "deadline (insert deadline description) /by (insert time)\n" + INDENTATION + - "- Saves a task with a given deadline\n\n" + INDENTATION + - "event (insert event description) /at (insert time interval)\n" + INDENTATION + - "- Saves an event happening at a certain time period\n\n" + INDENTATION + - "delete (insert number)\n" + INDENTATION + - "- Deletes a task with the corresponding number on the list\n\n" + INDENTATION + - "done (insert number)\n" + INDENTATION + - "- Marks the task with the corresponding number as done"; public static final String HELLO_MESSAGE_2 = "Hello! I'm Duke, your friendly neighbourhood task manager\n" + INDENTATION + "What can I do for you? :D\n" + INDENTATION + "(type help if you're not sure what to do!)"; public static final String LOGO = " ____ _ \n" @@ -23,23 +13,19 @@ public class Message { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - public static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; - public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; - public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry, I don't know what you mean. " + - "Please give me a suitable command :)"; public static final String PROMPT_TASK_DESCRIPTION = "Please tell me what do you need to do :)"; - public static final String MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE = "Sorry," + - " you're missing some arguments, do type 'help' if you're unsure :)"; + public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry," + + " I don't know what you mean, do type 'help' if you're unsure :)"; + public static final String IO_EXCEPTION_MESSAGE = "Oops, something unexpected happened while writing to file"; public static final String PROMPT_TASK_NUMBER = "Please tell me which task you want to select :)"; public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1 and "; public static final String DEFAULT_ERROR_MESSAGE = "Oops, something went wrong!"; public static final String FILE_NOT_CREATED = "Looks like you don't have a file for your tasks, " + "let me create one now....."; - public static final String UNEXPECTED_ERROR = "Oops,something unexpected happened"; public static final String INCORRECT_FORMAT = "Oops, file format is incorrect. Please correct it!"; public static final String DONE = "Done!"; public static final String GETTING_TASK = "Getting your tasks....."; - public static final String TASK_REMOVED = "Alright, I've removed this task:"; + public static final String PROMPT_NUMBER = "Please give me a number :)"; public static String getSensibleRange(int number) { if(number < 1) { diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index edc84637f..d8440d30f 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,10 +1,10 @@ package duke; -import constants.Constants; +import commands.*; import constants.Message; +import parser.Parser; import storage.Storage; import task.Task; -import task.TaskType; import tasklist.TaskList; import ui.Ui; @@ -12,14 +12,15 @@ public class Duke { - private Storage storage; - private Ui ui; - private TaskList tasks; + private final Storage storage; + private final Ui ui; + private final TaskList tasks; public Duke() { ui = new Ui(); storage = new Storage(); tasks = new TaskList(); + ui.printStartingMessage(); ui.printMessage(Message.GETTING_TASK); try { storage.loadTextFile(tasks); @@ -29,7 +30,7 @@ public Duke() { System.exit(1); } catch (IOException error) { - ui.printMessage(Message.UNEXPECTED_ERROR); + ui.printMessage(Message.DEFAULT_ERROR_MESSAGE); System.exit(1); } catch (DukeException error) { @@ -40,173 +41,48 @@ public Duke() { } public void run(){ - ui.printStartingMessage(); - while (true){ + Command command = null; + while (!(command instanceof ExitCommand)){ String input = ui.getUserCommand(); - processInput(input); + command = new Parser().parseInputForDifferentTask(input); + CommandResult result = executeCommand(command); + ui.showResult(result); } + exitProcess(); } - public static void main(String[] args) { - new Duke().run(); - } - - private void finalFileProcessing() { + private void exitProcess() { try { storage.saveTasksToFile(tasks); - } catch (IOException error){ - ui.printMessage(Message.UNEXPECTED_ERROR); - System.exit(1); - } - } - - /** - * Performs addition of different tasks, deletion of tasks, or list printing, depending on - * the first word. - * - * @param input - * input comes from the user - * - */ - private void processInput(String input) { - switch(input.split(" ")[0].toLowerCase()) { - case "list": - runListCommand(); - break; - case "done": - runDoneCommand(input); - break; - case "todo": - runToDoCommand(input); - break; - case "deadline": - runDeadlineCommand(input); - break; - case "event": - runEventCommand(input); - break; - case "delete": - runDeleteCommand(input); - break; - case "help": - ui.printHelpMessage(); - break; - case "bye" : - finalFileProcessing(); - ui.printGoodbyeMessage(); - System.exit(0); - default: - ui.printMessage(Message.TYPE_SUITABLE_COMMAND_MESSAGE); - - } - } - - private void runDeleteCommand(String input) { - try { - executeDeleteCase(input); - - } catch (DukeException error) { - ui.printMessage(Message.PROMPT_TASK_NUMBER); - - } catch (NumberFormatException error) { - ui.printMessage(Message.getSensibleRange(Task.getTotalTasks())); - - } - } - - private void runEventCommand(String input) { - try { - executeTaskCase(input, Constants.EVENT_STARTING_INDEX, TaskType.EVENT); - - } catch (StringIndexOutOfBoundsException error) { - ui.printMessage(Message.PROMPT_TASK_DESCRIPTION); - - } - } - - private void runDeadlineCommand(String input) { - try { - executeTaskCase(input, Constants.DEADLINE_STARTING_INDEX, TaskType.DEADLINE); - - } catch (StringIndexOutOfBoundsException error) { - ui.printMessage(Message.PROMPT_TASK_DESCRIPTION); - + } catch (IOException error) { + ui.printMessage(Message.IO_EXCEPTION_MESSAGE); } + System.exit(0); } - private void runToDoCommand(String input) { - try { - executeTaskCase(input, Constants.TODO_STARTING_INDEX, TaskType.TODO); - - } catch (StringIndexOutOfBoundsException error) { - ui.printMessage(Message.PROMPT_TASK_DESCRIPTION); - - } + public static void main(String[] args) { + new Duke().run(); } - private void runDoneCommand(String input) { + private CommandResult executeCommand(Command command) { + String errorMessage; try { - executeDoneCase(input); - - } catch (DukeException error) { - ui.printMessage(Message.PROMPT_TASK_NUMBER); + command.passList(tasks); + return command.execute(); } catch (IndexOutOfBoundsException error) { - ui.printMessage(Message.getSensibleRange(Task.getTotalTasks())); + errorMessage = Message.getSensibleRange(Task.getTotalTasks()); } catch (NumberFormatException error) { - ui.printMessage(Message.getSensibleRange(Task.getTotalTasks())); - - } - } - - private void executeTaskCase(String input, int starting_index, TaskType type) { - String description = input.strip().substring(starting_index); - try { - tasks.addTask(description, type); - ui.printAddTaskMessage(tasks); - - } catch (DukeException error) { - ui.printMessage(Message.MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE); + errorMessage = Message.PROMPT_NUMBER; } catch (DefaultException error) { - ui.printMessage(Message.DEFAULT_ERROR_MESSAGE); - } - } + errorMessage = Message.DEFAULT_ERROR_MESSAGE; - private void executeDoneCase(String input) throws DukeException, - IndexOutOfBoundsException,NumberFormatException { - if(input.length() < Constants.EXPECTED_LENGTH_FOR_DONE_INPUT) { - throw new DukeException(); - } - int index = Integer.parseInt(input.split(" ")[1]) - 1; - tasks.getTask(index).markAsDone(); - ui.printTaskCompletedMessage(tasks.getTask(index)); - } - - private void executeDeleteCase(String input) throws DukeException, - NumberFormatException { - if(input.length() < Constants.EXPECTED_LENGTH_FOR_DELETE_INPUT) { - throw new DukeException(); - } - int index = Integer.parseInt(input.split(" ")[1]) - 1; - try { - Task temp = tasks.getTask(index); - tasks.deleteTask(index); - ui.printTaskDeletedMessage(temp); - } catch (IndexOutOfBoundsException error){ - ui.printMessage(Message.getSensibleRange(Task.getTotalTasks())); - } - } + } catch (DukeException error) { + errorMessage = Message.TYPE_SUITABLE_COMMAND_MESSAGE; - private void runListCommand() { - ui.printIndentationAndDivider(); - if(Task.getTotalTasks() == 0) { - ui.printWordsWithIndentation(Message.LIST_IS_EMPTY); } - ui.printTask(tasks); - ui.printIndentationAndDivider(); - System.out.println(); + return new CommandResult(errorMessage, PrintOptions.DEFAULT); } - } diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index c72451ad6..cee4de9f0 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -1,6 +1,7 @@ package parser; import commands.*; +import constants.Message; import task.TaskType; public class Parser { @@ -15,11 +16,23 @@ public Command parseInputForDifferentTask(String input) { String type = input.split(" ")[0].toLowerCase(); switch(type) { case "todo": - return prepareAddCommand(input,TaskType.TODO); + try { + return prepareAddCommand(input,TaskType.TODO); + } catch (StringIndexOutOfBoundsException error) { + return new IncorrectCommand(Message.PROMPT_TASK_DESCRIPTION); + } case "deadline": - return prepareAddCommand(input,TaskType.DEADLINE); + try { + return prepareAddCommand(input,TaskType.DEADLINE); + } catch (StringIndexOutOfBoundsException error) { + return new IncorrectCommand(Message.PROMPT_TASK_DESCRIPTION); + } case "event": - return prepareAddCommand(input,TaskType.EVENT); + try { + return prepareAddCommand(input,TaskType.EVENT); + } catch (StringIndexOutOfBoundsException error) { + return new IncorrectCommand(Message.PROMPT_TASK_DESCRIPTION); + } case "list": return new ListCommand(); case "done": @@ -31,11 +44,11 @@ public Command parseInputForDifferentTask(String input) { case "bye": return new ExitCommand(); default : - return new IncorrectCommand(); + return new IncorrectCommand(Message.TYPE_SUITABLE_COMMAND_MESSAGE); } } - private Command prepareAddCommand(String input,TaskType type) { + private Command prepareAddCommand(String input,TaskType type) throws StringIndexOutOfBoundsException{ String description; String parsedOutput[]; switch(type){ @@ -52,7 +65,7 @@ private Command prepareAddCommand(String input,TaskType type) { parsedOutput = description.split(DEADLINE_DESCRIPTION_AND_DATE_SPLITTER); return new AddCommand(parsedOutput,TaskType.DEADLINE); default: - return new IncorrectCommand(); + return new IncorrectCommand(Message.DEFAULT_ERROR_MESSAGE); } } } diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index 917e0f993..b74637650 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -14,6 +14,8 @@ public class Storage { + private static final String SEPARATOR_FOR_FILE = " / "; + public Storage() {} public void loadTextFile(TaskList tasks) throws IOException, DukeException { @@ -24,7 +26,6 @@ public void loadTextFile(TaskList tasks) throws IOException, DukeException { Scanner s = new Scanner(file); while (s.hasNext()) { addTaskFromFile(s,tasks); - Task.setTotalTasks(Task.getTotalTasks() + 1); } } @@ -36,8 +37,8 @@ public void saveTasksToFile(TaskList tasks) throws IOException{ fw.close(); } - public void addTaskFromFile(Scanner s, TaskList tasks) throws IndexOutOfBoundsException { - String[] parsedOutput = s.nextLine().split(" / "); + private void addTaskFromFile(Scanner s, TaskList tasks) throws IndexOutOfBoundsException { + String[] parsedOutput = s.nextLine().split(SEPARATOR_FOR_FILE); switch(parsedOutput[0]){ case "T": tasks.addTask(new ToDo(parsedOutput[2])); @@ -56,7 +57,7 @@ public void addTaskFromFile(Scanner s, TaskList tasks) throws IndexOutOfBoundsEx private void updateTaskStatus(String done, TaskList tasks) { if (done.equals("1")) { - tasks.getTask(Task.getTotalTasks()).markAsDone(); + tasks.getTask(Task.getTotalTasks()-1).markAsDone(); } } } diff --git a/src/main/java/tasklist/TaskList.java b/src/main/java/tasklist/TaskList.java index 1ac4930bd..0682d1239 100644 --- a/src/main/java/tasklist/TaskList.java +++ b/src/main/java/tasklist/TaskList.java @@ -11,6 +11,7 @@ public class TaskList { public TaskList() { this.tasks = new ArrayList<>(); } + public void addTask(Task task) { tasks.add(task); Task.setTotalTasks(Task.getTotalTasks() + 1); diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index 6dcbfb127..a56a421b0 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -29,8 +29,7 @@ public Ui(InputStream in, PrintStream out){ } public String getUserCommand() { - String input = in.nextLine(); - return input; + return in.nextLine(); } public void printIndentationAndDivider() { @@ -51,13 +50,14 @@ public void printStartingMessage() { out.println(); } - public void printGoodbyeMessage() { + public void printMessage(String input) { printIndentationAndDivider(); - printWordsWithIndentation(Message.GOODBYE_MESSAGE); + printWordsWithIndentation(input); printIndentationAndDivider(); + System.out.println(); } - private void printTaskMessage(CommandResult result, Boolean notifyNumberOfTasks, Boolean printTask) { + private void printCommandResultMessage(CommandResult result, Boolean notifyNumberOfTasks, Boolean printTask) { printIndentationAndDivider(); printWordsWithIndentation(result.feedbackToUser); if(printTask) { @@ -70,17 +70,22 @@ private void printTaskMessage(CommandResult result, Boolean notifyNumberOfTasks, System.out.println(); } - public void printMessage(CommandResult result) { + public void showResult(CommandResult result) { switch(result.type){ case WITH_TASK_AND_NUMBER_OF_TASK: - printTaskMessage(result,NOTIFY_NUMBER_OF_TASK,PRINT_TASK); + printCommandResultMessage(result,NOTIFY_NUMBER_OF_TASK,PRINT_TASK); break; case ONLY_TASK: - printTaskMessage(result,!NOTIFY_NUMBER_OF_TASK,PRINT_TASK); + printCommandResultMessage(result,!NOTIFY_NUMBER_OF_TASK,PRINT_TASK); + break; case LIST: printList(result); + break; + case DEFAULT: + printCommandResultMessage(result,!NOTIFY_NUMBER_OF_TASK,!PRINT_TASK); + break; default: - printTaskMessage(result,!NOTIFY_NUMBER_OF_TASK,!PRINT_TASK); + printMessage(result.feedbackToUser); } } @@ -89,29 +94,20 @@ public void printMessage(CommandResult result) { /** * Prints the tasks based on the given format */ - public void printTask(CommandResult result) { + private void printTask(CommandResult result) { for (int i = 0; i < Task.getTotalTasks(); i++) { printWordsWithIndentation(i + 1 + "." + result.tasks.getTask(i).getStatusIconAndDescription()); } } - public void printList(CommandResult result) { + private void printList(CommandResult result) { printIndentationAndDivider(); - if(Task.getTotalTasks() == 0) { - printWordsWithIndentation(result.feedbackToUser); - } + printWordsWithIndentation(result.feedbackToUser); printTask(result); printIndentationAndDivider(); System.out.println(); } - public void printHelpMessage() { - printIndentationAndDivider(); - printWordsWithIndentation(Message.HELP_MESSAGE); - printIndentationAndDivider(); - System.out.println(); - } - private String notifyNumberOfTasks() { int taskNumber = Task.getTotalTasks(); return "Now you have " + taskNumber + " task(s) in the list"; diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 763d0a121..b3ddd0653 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -8,6 +8,15 @@ Hello from ___________________________________________________________ Hello! I'm Duke, your friendly neighbourhood task manager What can I do for you? :D + (type help if you're not sure what to do!) + ___________________________________________________________ + + ___________________________________________________________ + Getting your tasks..... + ___________________________________________________________ + + ___________________________________________________________ + Done! ___________________________________________________________ ___________________________________________________________ @@ -37,6 +46,7 @@ Hello from ___________________________________________________________ ___________________________________________________________ + Here are the things you need to do : 1.[T][ ] borrow book 2.[D][ ] return book (by: Sunday) 3.[E][ ] project meeting (at: 4th Sep 2-3pm) @@ -48,13 +58,14 @@ Hello from ___________________________________________________________ ___________________________________________________________ + Here are the things you need to do : 1.[T][X] borrow book 2.[D][ ] return book (by: Sunday) 3.[E][ ] project meeting (at: 4th Sep 2-3pm) ___________________________________________________________ ___________________________________________________________ - Please give a number between 1 and 3 :) + Please give me a number :) ___________________________________________________________ ___________________________________________________________ @@ -80,6 +91,7 @@ Hello from ___________________________________________________________ ___________________________________________________________ + Here are the things you need to do : 1.[D][ ] return book (by: Sunday) 2.[E][ ] project meeting (at: 4th Sep 2-3pm) ___________________________________________________________ @@ -102,4 +114,5 @@ Hello from ___________________________________________________________ Bye, hope to see you again soon! :) - ___________________________________________________________ \ No newline at end of file + ___________________________________________________________ + diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index a99301ad7..d2d47e6e5 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,8 @@ REM delete output from previous run if exist ACTUAL.TXT del ACTUAL.TXT REM compile the code into the bin folder -javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\duke\*.java ..\src\main\java\task\*.java +javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\duke\*.java ..\src\main\java\task\*.java ..\src\main\java\tasklist\*.java ..\src\main\java\ui\*.java ..\src\main\java\commands\*.java ..\src\main\java\constants\*.java ..\src\main\java\parser\*.java ..\src\main\java\storage\*.java + IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 From 10ef0498b7173b82b71b7f9e2f1a42b2269e558e Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Sun, 26 Sep 2021 15:38:21 +0800 Subject: [PATCH 20/33] Add date and time for event and deadline and some constants --- src/main/java/commands/AddCommand.java | 40 ++++++++-- src/main/java/commands/Command.java | 2 +- src/main/java/commands/DeleteCommand.java | 2 + src/main/java/commands/DoneCommand.java | 2 + src/main/java/commands/ExitCommand.java | 1 + src/main/java/commands/HelpCommand.java | 2 + src/main/java/commands/IncorrectCommand.java | 2 - src/main/java/commands/ListCommand.java | 56 ++++++++++++- src/main/java/commands/PrintOptions.java | 2 +- src/main/java/constants/Message.java | 1 - src/main/java/duke/Duke.java | 13 +++- src/main/java/parser/Parser.java | 82 ++++++++++++++++---- src/main/java/storage/Storage.java | 6 +- src/main/java/task/Deadline.java | 21 +++-- src/main/java/task/Event.java | 24 ++++-- src/main/java/tasklist/TaskList.java | 15 ++++ src/main/java/ui/Ui.java | 22 ++++-- 17 files changed, 240 insertions(+), 53 deletions(-) diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java index 766889a58..ff0ec768b 100644 --- a/src/main/java/commands/AddCommand.java +++ b/src/main/java/commands/AddCommand.java @@ -1,15 +1,26 @@ package commands; import duke.DefaultException; -import duke.DukeException; import task.*; +import java.time.LocalDateTime; +import java.time.format.DateTimeParseException; + public class AddCommand extends Command{ - private static final int EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE = 2; + private static final int EXPECTED_LENGTH_FOR_DEADLINE = 2; + private static final int EXPECTED_LENGTH_FOR_EVENT = 3; private static final String PROMPT_CORRECT_FORMAT = "Sorry, you're missing some arguments," + " do type 'help' if you're unsure :)"; + private static final String DATE_TIME_FORMAT_ERROR_FOR_DEADLINE = "Please write the date and time in the" + + " format : 'yyyy-mm-ddThh:mm:ss' :)"; + private static final String DATE_TIME_FORMAT_ERROR_FOR_EVENT = "Please write the date and time in the" + + " format : 'yyyy-mm-ddThh:mm:ss /to yyy-mm-ddThh:mm' :)"; public static final String ADDED_TO_LIST = "I've added this to your list :D"; + public static final String TODO_COMMAND = "todo"; + public static final String EVENT_COMMAND = "event"; + public static final String DEADLINE_COMMAND = "deadline"; + private final String[] parsedOutput; private final TaskType type; @@ -18,28 +29,43 @@ public AddCommand(String[] parsedOutput, TaskType type) { this.type = type; } - public CommandResult execute() throws DefaultException { + public CommandResult execute() throws DefaultException, DateTimeParseException { switch(type){ case TODO: tasks.addTask(new ToDo(parsedOutput[0])); return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); case EVENT: - if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { + if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT) { return new CommandResult(PROMPT_CORRECT_FORMAT,PrintOptions.DEFAULT); } - tasks.addTask(new Event(parsedOutput[0], parsedOutput[1])); + try { + tasks.addTask(new Event(parsedOutput[0], LocalDateTime.parse(parsedOutput[1]), + LocalDateTime.parse(parsedOutput[2]))); + + } catch (DateTimeParseException error) { + return new CommandResult(DATE_TIME_FORMAT_ERROR_FOR_EVENT,PrintOptions.DEFAULT); + + } return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); case DEADLINE: - if(parsedOutput.length < EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE) { + if(parsedOutput.length < EXPECTED_LENGTH_FOR_DEADLINE) { return new CommandResult(PROMPT_CORRECT_FORMAT,PrintOptions.DEFAULT); } - tasks.addTask(new Deadline(parsedOutput[0], parsedOutput[1])); + try { + tasks.addTask(new Deadline(parsedOutput[0], LocalDateTime.parse(parsedOutput[1]))); + + } catch (DateTimeParseException error) { + return new CommandResult(DATE_TIME_FORMAT_ERROR_FOR_DEADLINE,PrintOptions.DEFAULT); + + } return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); default : throw new DefaultException(); } } + + } diff --git a/src/main/java/commands/Command.java b/src/main/java/commands/Command.java index 00663a41c..f12bcf135 100644 --- a/src/main/java/commands/Command.java +++ b/src/main/java/commands/Command.java @@ -11,7 +11,7 @@ public Command() { this.tasks = new TaskList(); } public abstract CommandResult execute() throws DukeException, DefaultException; - public void passList (TaskList tasks) { + public void passTaskList(TaskList tasks) { this.tasks = tasks; } } diff --git a/src/main/java/commands/DeleteCommand.java b/src/main/java/commands/DeleteCommand.java index 38da31456..284f9bccf 100644 --- a/src/main/java/commands/DeleteCommand.java +++ b/src/main/java/commands/DeleteCommand.java @@ -6,7 +6,9 @@ import static commands.ListCommand.LIST_IS_EMPTY; public class DeleteCommand extends Command{ + private static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 2; + public static final String DELETE_COMMAND = "delete"; public static final String TASK_REMOVED = "Alright, I've removed this task:"; public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1 and "; private String input; diff --git a/src/main/java/commands/DoneCommand.java b/src/main/java/commands/DoneCommand.java index bc47a50f6..c07e348de 100644 --- a/src/main/java/commands/DoneCommand.java +++ b/src/main/java/commands/DoneCommand.java @@ -3,9 +3,11 @@ import constants.Message; public class DoneCommand extends Command{ + private String input; private static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 2; private static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; + public static final String DONE_COMMAND = "done"; public DoneCommand(String input) { this.input = input; diff --git a/src/main/java/commands/ExitCommand.java b/src/main/java/commands/ExitCommand.java index dd788f201..318492aed 100644 --- a/src/main/java/commands/ExitCommand.java +++ b/src/main/java/commands/ExitCommand.java @@ -3,6 +3,7 @@ public class ExitCommand extends Command{ public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; + public static final String EXIT_COMMAND = "bye"; public CommandResult execute() { return new CommandResult(GOODBYE_MESSAGE,PrintOptions.DEFAULT); diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java index a800fc87d..489da50d3 100644 --- a/src/main/java/commands/HelpCommand.java +++ b/src/main/java/commands/HelpCommand.java @@ -3,6 +3,8 @@ import static ui.Ui.INDENTATION; public class HelpCommand extends Command{ + + public static final String HELP_COMMAND = "help"; private static final String HELP_MESSAGE = "list\n" +INDENTATION + "- Shows you the list of tasks you have\n\n" +INDENTATION + "todo (insert task description)\n" + INDENTATION + diff --git a/src/main/java/commands/IncorrectCommand.java b/src/main/java/commands/IncorrectCommand.java index 15b1d94d3..27fd29a6c 100644 --- a/src/main/java/commands/IncorrectCommand.java +++ b/src/main/java/commands/IncorrectCommand.java @@ -1,7 +1,5 @@ package commands; -import constants.Message; - public class IncorrectCommand extends Command{ private String message; diff --git a/src/main/java/commands/ListCommand.java b/src/main/java/commands/ListCommand.java index e8490c7a5..157d8ef0d 100644 --- a/src/main/java/commands/ListCommand.java +++ b/src/main/java/commands/ListCommand.java @@ -1,18 +1,68 @@ package commands; +import task.Deadline; +import task.Event; import task.Task; +import tasklist.TaskList; + +import java.time.LocalDate; +import java.time.LocalDateTime; public class ListCommand extends Command{ + private final LocalDate dateTime; public static final String LIST_IS_EMPTY = "You're a free man :)"; private static final String LIST_IS_NOT_EMPTY = "Here are the things you need to do :"; + private static final String LIST_OF_SPECIFIC_DATE = "Here are the things you need to do on that day :"; + public static final String LIST_COMMAND = "list"; + public static final int NO_TASKS = 0; + + public ListCommand () { + super(); + this.dateTime = null; + } + + public ListCommand(LocalDate dateTime) { + super(); + this.dateTime = dateTime; + } public CommandResult execute(){ - if(Task.getTotalTasks() == 0) { + if(Task.getTotalTasks() == NO_TASKS) { return new CommandResult(LIST_IS_EMPTY,PrintOptions.DEFAULT); + + } else if (dateTime != null){ + TaskList tasksOfSpecificDate = getTasksOfSpecificDate(); + return new CommandResult(LIST_OF_SPECIFIC_DATE,tasksOfSpecificDate,PrintOptions.LIST_OF_SPECIFIC_DATE); } - else { - return new CommandResult(LIST_IS_NOT_EMPTY,tasks,PrintOptions.LIST); + return new CommandResult(LIST_IS_NOT_EMPTY,tasks,PrintOptions.LIST); + } + + private TaskList getTasksOfSpecificDate() { + TaskList tasksOfSpecificDate = new TaskList(); + int totalTasks = 0; + for (int i = 0; i tasks; + private int numberOfTasksOfSameDate; public TaskList() { this.tasks = new ArrayList<>(); + this.numberOfTasksOfSameDate = 0; + } + + public int getNumberOfTasksOfSameDate() { + return numberOfTasksOfSameDate; + } + + public void setNumberOfTaskForTheSameDate(int numberOfTasks) { + this.numberOfTasksOfSameDate = numberOfTasks; } public void addTask(Task task) { @@ -18,6 +28,11 @@ public void addTask(Task task) { } + public void addTaskForListOfSpecificDate(Task task) { + tasks.add(task); + + } + public void deleteTask(int index) throws IndexOutOfBoundsException { tasks.remove(index); Task.setTotalTasks(Task.getTotalTasks() - 1); diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index a56a421b0..6b2991c1f 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -2,6 +2,7 @@ import commands.CommandResult; +import commands.PrintOptions; import constants.Message; import task.Task; @@ -79,7 +80,10 @@ public void showResult(CommandResult result) { printCommandResultMessage(result,!NOTIFY_NUMBER_OF_TASK,PRINT_TASK); break; case LIST: - printList(result); + printList(result,PrintOptions.LIST); + break; + case LIST_OF_SPECIFIC_DATE: + printList(result,PrintOptions.LIST_OF_SPECIFIC_DATE); break; case DEFAULT: printCommandResultMessage(result,!NOTIFY_NUMBER_OF_TASK,!PRINT_TASK); @@ -94,16 +98,22 @@ public void showResult(CommandResult result) { /** * Prints the tasks based on the given format */ - private void printTask(CommandResult result) { - for (int i = 0; i < Task.getTotalTasks(); i++) { - printWordsWithIndentation(i + 1 + "." + result.tasks.getTask(i).getStatusIconAndDescription()); + private void printTask(CommandResult result,PrintOptions options) { + if(options == PrintOptions.LIST_OF_SPECIFIC_DATE) { + for (int i = 0; i < result.tasks.getNumberOfTasksOfSameDate(); i++) { + printWordsWithIndentation(i + 1 + "." + result.tasks.getTask(i).getStatusIconAndDescription()); + } + } else { + for (int i = 0; i < Task.getTotalTasks(); i++) { + printWordsWithIndentation(i + 1 + "." + result.tasks.getTask(i).getStatusIconAndDescription()); + } } } - private void printList(CommandResult result) { + private void printList(CommandResult result, PrintOptions options) { printIndentationAndDivider(); printWordsWithIndentation(result.feedbackToUser); - printTask(result); + printTask(result,options); printIndentationAndDivider(); System.out.println(); } From 861e928375f8ac3ddc581e4503675c0be499a89a Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Sun, 26 Sep 2021 17:15:22 +0800 Subject: [PATCH 21/33] Add find function to find tasks containining the description --- src/main/java/commands/FindCommand.java | 31 ++++++++++++++++++++++++ src/main/java/commands/PrintOptions.java | 2 +- src/main/java/parser/Parser.java | 15 ++++++++++-- src/main/java/task/Task.java | 3 +++ src/main/java/tasklist/TaskList.java | 13 ++++++++++ src/main/java/ui/Ui.java | 12 +++++++-- 6 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/main/java/commands/FindCommand.java diff --git a/src/main/java/commands/FindCommand.java b/src/main/java/commands/FindCommand.java new file mode 100644 index 000000000..cf642abab --- /dev/null +++ b/src/main/java/commands/FindCommand.java @@ -0,0 +1,31 @@ +package commands; + +import task.Task; +import tasklist.TaskList; + +public class FindCommand extends Command{ + + private final String input; + private static final String LIST_CONTAINING_STRING_MESSAGE = "Here are the matching tasks in your list : "; + private static final String NO_SIMILAR_WORDS = "There are no matching tasks :("; + + public FindCommand(String input) { + this.input = input; + } + public CommandResult execute() { + int totalTasks = 0; + TaskList taskContainingString = new TaskList(); + for (int i = 0; i < Task.getTotalTasks(); i++) { + if(tasks.getTask(i).getDescription().contains(input)){ + taskContainingString.addTaskForSpecificCases(tasks.getTask(i)); + totalTasks++; + } + } + taskContainingString.setNumberOfTask(totalTasks); + if(totalTasks == 0) { + return new CommandResult(NO_SIMILAR_WORDS,PrintOptions.DEFAULT); + } + return new CommandResult(LIST_CONTAINING_STRING_MESSAGE, + taskContainingString,PrintOptions.LIST_WITH_SPECIFIC_CONDITIONS); + } +} diff --git a/src/main/java/commands/PrintOptions.java b/src/main/java/commands/PrintOptions.java index d0583d9d6..f3bbb518a 100644 --- a/src/main/java/commands/PrintOptions.java +++ b/src/main/java/commands/PrintOptions.java @@ -1,5 +1,5 @@ package commands; public enum PrintOptions { - WITH_TASK_AND_NUMBER_OF_TASK,ONLY_TASK,LIST,DEFAULT + WITH_TASK_AND_NUMBER_OF_TASK,ONLY_TASK,LIST,DEFAULT,LIST_WITH_SPECIFIC_CONDITIONS } diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index cee4de9f0..8d33ae437 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -8,6 +8,9 @@ public class Parser { private static final String DEADLINE_DESCRIPTION_AND_DATE_SPLITTER = " /by "; private static final String EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER = " /at "; + private static final String FIND_FORMAT_ERROR = "Please type the find function in the format: " + + "'find (description)' :)"; + private static final int FIND_STARTING_INDEX = 5; private static final int TODO_STARTING_INDEX = 5; private static final int DEADLINE_STARTING_INDEX = 9; private static final int EVENT_STARTING_INDEX = 6; @@ -36,13 +39,21 @@ public Command parseInputForDifferentTask(String input) { case "list": return new ListCommand(); case "done": - return new DoneCommand(input); + return new DoneCommand(input.strip()); case "help": return new HelpCommand(); case "delete": - return new DeleteCommand(input); + return new DeleteCommand(input.strip()); case "bye": return new ExitCommand(); + case "find": + try { + return new FindCommand(input.strip().substring(FIND_STARTING_INDEX)); + + } catch (StringIndexOutOfBoundsException error) { + return new IncorrectCommand(FIND_FORMAT_ERROR); + + } default : return new IncorrectCommand(Message.TYPE_SUITABLE_COMMAND_MESSAGE); } diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index b00db6eac..bf25b1ff8 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -9,6 +9,9 @@ public abstract class Task { public abstract String getStatusIconAndDescription(); + public String getDescription() { + return description; + } public void markAsDone() { isDone = true; diff --git a/src/main/java/tasklist/TaskList.java b/src/main/java/tasklist/TaskList.java index 0682d1239..52949f66b 100644 --- a/src/main/java/tasklist/TaskList.java +++ b/src/main/java/tasklist/TaskList.java @@ -7,15 +7,28 @@ public class TaskList { private ArrayList tasks; + private int numberOfTasks; public TaskList() { this.tasks = new ArrayList<>(); + this.numberOfTasks = 0; + } + + public int getNumberOfTasks() { + return numberOfTasks; + } + + public void setNumberOfTask(int numberOfTasks) { + this.numberOfTasks = numberOfTasks; } public void addTask(Task task) { tasks.add(task); Task.setTotalTasks(Task.getTotalTasks() + 1); + } + public void addTaskForSpecificCases(Task task) { + tasks.add(task); } public void deleteTask(int index) throws IndexOutOfBoundsException { diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index a56a421b0..e24e2e01a 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -2,6 +2,7 @@ import commands.CommandResult; +import commands.PrintOptions; import constants.Message; import task.Task; @@ -78,6 +79,7 @@ public void showResult(CommandResult result) { case ONLY_TASK: printCommandResultMessage(result,!NOTIFY_NUMBER_OF_TASK,PRINT_TASK); break; + case LIST_WITH_SPECIFIC_CONDITIONS: case LIST: printList(result); break; @@ -95,8 +97,14 @@ public void showResult(CommandResult result) { * Prints the tasks based on the given format */ private void printTask(CommandResult result) { - for (int i = 0; i < Task.getTotalTasks(); i++) { - printWordsWithIndentation(i + 1 + "." + result.tasks.getTask(i).getStatusIconAndDescription()); + if(result.type == PrintOptions.LIST_WITH_SPECIFIC_CONDITIONS) { + for(int i = 0; i < result.tasks.getNumberOfTasks(); i++) { + printWordsWithIndentation(i + 1 + "." + result.tasks.getTask(i).getStatusIconAndDescription()); + } + } else { + for(int i = 0; i < Task.getTotalTasks(); i++) { + printWordsWithIndentation(i + 1 + "." + result.tasks.getTask(i).getStatusIconAndDescription()); + } } } From d76441c172f0cfa95c86033aa041249e8f0a5669 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Mon, 27 Sep 2021 14:41:04 +0800 Subject: [PATCH 22/33] Add javadocs to most classes and methods --- src/main/java/commands/AddCommand.java | 12 +++- src/main/java/commands/Command.java | 4 ++ src/main/java/commands/CommandResult.java | 37 ++++++++--- src/main/java/commands/DeleteCommand.java | 19 +++--- src/main/java/commands/DoneCommand.java | 12 +++- src/main/java/commands/ExitCommand.java | 8 +++ src/main/java/commands/HelpCommand.java | 8 +++ src/main/java/commands/IncorrectCommand.java | 10 ++- src/main/java/commands/ListCommand.java | 8 +++ src/main/java/commands/PrintOptions.java | 4 ++ src/main/java/constants/Constants.java | 9 --- src/main/java/constants/Message.java | 8 ++- src/main/java/duke/DefaultException.java | 3 + src/main/java/duke/Duke.java | 21 ++++++- src/main/java/duke/DukeException.java | 3 + src/main/java/parser/Parser.java | 31 ++++++++- src/main/java/storage/Storage.java | 28 +++++++++ src/main/java/task/Deadline.java | 6 ++ src/main/java/task/Event.java | 6 ++ src/main/java/task/Task.java | 14 +++++ src/main/java/task/TaskType.java | 3 + src/main/java/task/ToDo.java | 5 ++ src/main/java/tasklist/TaskList.java | 5 +- src/main/java/ui/Ui.java | 66 ++++++++++++++------ 24 files changed, 280 insertions(+), 50 deletions(-) delete mode 100644 src/main/java/constants/Constants.java diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java index 766889a58..26d28dc87 100644 --- a/src/main/java/commands/AddCommand.java +++ b/src/main/java/commands/AddCommand.java @@ -1,9 +1,11 @@ package commands; import duke.DefaultException; -import duke.DukeException; import task.*; +/** + * A Command class that contains methods for adding tasks to the ArrayList. + */ public class AddCommand extends Command{ private static final int EXPECTED_LENGTH_FOR_EVENT_AND_DEADLINE = 2; @@ -18,6 +20,14 @@ public AddCommand(String[] parsedOutput, TaskType type) { this.type = type; } + /** + * Creates a task object and adds it to the ArrayList depending on the type of task. + * + * @return A CommandResult that tells the Ui to print the status of the execution + * and the task added. + * @throws DefaultException Throws a DefaultException if something unexpected happens. + */ + @Override public CommandResult execute() throws DefaultException { switch(type){ case TODO: diff --git a/src/main/java/commands/Command.java b/src/main/java/commands/Command.java index 00663a41c..0e0bb71a9 100644 --- a/src/main/java/commands/Command.java +++ b/src/main/java/commands/Command.java @@ -4,6 +4,10 @@ import duke.DukeException; import tasklist.TaskList; + +/** + * An abstract class with the common methods and attributes for different types of Commands. + */ public abstract class Command { protected TaskList tasks; diff --git a/src/main/java/commands/CommandResult.java b/src/main/java/commands/CommandResult.java index aa4409c8c..db4a4dfb2 100644 --- a/src/main/java/commands/CommandResult.java +++ b/src/main/java/commands/CommandResult.java @@ -3,28 +3,51 @@ import task.Task; import tasklist.TaskList; +/** + * A class for passing the message from the execution of the Command to the Ui. + */ public class CommandResult{ - public final String feedbackToUser; + public final String message; public final Task task; public final TaskList tasks; public final PrintOptions type; - public CommandResult(String feedbackToUser, TaskList tasks,PrintOptions type) { - this.feedbackToUser = feedbackToUser; + /** + * Creates a CommandResult that allows the list of tasks to be shown to the user + * + * @param message Message to the user + * @param tasks The list of tasks + * @param type The options for printing in Ui.showResult + */ + public CommandResult(String message, TaskList tasks, PrintOptions type) { + this.message = message; this.task = null; this.tasks = tasks; this.type = type; } - public CommandResult(String feedbackToUser,PrintOptions type) { - this.feedbackToUser = feedbackToUser; + /** + * Creates a CommandResult that only needs to show a message to the user. + * + * @param message Message to the user + * @param type The options for printing in Ui.showResult + */ + public CommandResult(String message, PrintOptions type) { + this.message = message; this.task = null; this.tasks = null; this.type = type; } - public CommandResult(String feedbackToUser, Task task,PrintOptions type) { - this.feedbackToUser = feedbackToUser; + /** + * Creates a CommandResult that requires a deleted/added task to be shown to the user. + * + * @param message Message to the user + * @param task The task shown to the user + * @param type The options for printing in Ui.showResult + */ + public CommandResult(String message, Task task, PrintOptions type) { + this.message = message; this.task = task; this.tasks = null; this.type = type; diff --git a/src/main/java/commands/DeleteCommand.java b/src/main/java/commands/DeleteCommand.java index 38da31456..e4c120678 100644 --- a/src/main/java/commands/DeleteCommand.java +++ b/src/main/java/commands/DeleteCommand.java @@ -5,6 +5,9 @@ import static commands.ListCommand.LIST_IS_EMPTY; +/** + * A Command class that deletes a task in the ArrayList. + */ public class DeleteCommand extends Command{ private static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 2; public static final String TASK_REMOVED = "Alright, I've removed this task:"; @@ -15,6 +18,13 @@ public DeleteCommand(String input) { this.input = input; } + /** + * Deletes a task with a given index on the ArrayList + * @return A CommandResult that tells the Ui to print the status of execution + * and the deleted task. + * @throws NumberFormatException Throws an exception if the input given is not a number. + */ + @Override public CommandResult execute() throws NumberFormatException { if(input.split(" ").length < EXPECTED_LENGTH_FOR_DELETE_INPUT) { return new CommandResult(Message.PROMPT_TASK_NUMBER,PrintOptions.DEFAULT); @@ -25,14 +35,7 @@ public CommandResult execute() throws NumberFormatException { tasks.deleteTask(index); return new CommandResult(TASK_REMOVED,temp,PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); } catch (IndexOutOfBoundsException error){ - return new CommandResult(getSensibleRange(Task.getTotalTasks()),PrintOptions.DEFAULT); + return new CommandResult(Message.giveSensibleRange(Task.getTotalTasks()),PrintOptions.DEFAULT); } } - - public static String getSensibleRange(int number) { - if(number < 1) { - return LIST_IS_EMPTY; - } - return PROMPT_SENSIBLE_INDEX + number + " :)"; - } } diff --git a/src/main/java/commands/DoneCommand.java b/src/main/java/commands/DoneCommand.java index bc47a50f6..440fca1a6 100644 --- a/src/main/java/commands/DoneCommand.java +++ b/src/main/java/commands/DoneCommand.java @@ -2,6 +2,9 @@ import constants.Message; +/** + * A Command class that marks a task in the ArrayList as done + */ public class DoneCommand extends Command{ private String input; private static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 2; @@ -11,7 +14,14 @@ public DoneCommand(String input) { this.input = input; } - public CommandResult execute() throws IndexOutOfBoundsException,NumberFormatException { + /** + * Marks a task of a given index in the ArrayList as done + * @return A CommandResult that that tells the Ui to print the status of execution + * and the task that is done. + * @throws NumberFormatException Throws an exception if the input given is not a number. + */ + @Override + public CommandResult execute() throws NumberFormatException { if(input.split(" ").length < EXPECTED_LENGTH_FOR_DONE_INPUT) { return new CommandResult(Message.PROMPT_TASK_NUMBER,PrintOptions.DEFAULT); } diff --git a/src/main/java/commands/ExitCommand.java b/src/main/java/commands/ExitCommand.java index dd788f201..081e6c4a8 100644 --- a/src/main/java/commands/ExitCommand.java +++ b/src/main/java/commands/ExitCommand.java @@ -1,9 +1,17 @@ package commands; +/** + * A Command class that passes the exit message to the Ui + */ public class ExitCommand extends Command{ public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; + /** + * Sends the exit message to the Ui + * @return A CommandResult that tells the Ui to print the exit message. + */ + @Override public CommandResult execute() { return new CommandResult(GOODBYE_MESSAGE,PrintOptions.DEFAULT); } diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java index a800fc87d..dac23d548 100644 --- a/src/main/java/commands/HelpCommand.java +++ b/src/main/java/commands/HelpCommand.java @@ -2,6 +2,9 @@ import static ui.Ui.INDENTATION; +/** + * A Command class that send the help message to the Ui + */ public class HelpCommand extends Command{ private static final String HELP_MESSAGE = "list\n" +INDENTATION + "- Shows you the list of tasks you have\n\n" +INDENTATION + @@ -16,6 +19,11 @@ public class HelpCommand extends Command{ "done (insert number)\n" + INDENTATION + "- Marks the task with the corresponding number as done"; + /** + * Sends the help message to the Ui + * @return A CommandResult that tells the Ui to print the help message. + */ + @Override public CommandResult execute() { return new CommandResult(HELP_MESSAGE,PrintOptions.DEFAULT); } diff --git a/src/main/java/commands/IncorrectCommand.java b/src/main/java/commands/IncorrectCommand.java index 15b1d94d3..642cca23d 100644 --- a/src/main/java/commands/IncorrectCommand.java +++ b/src/main/java/commands/IncorrectCommand.java @@ -1,7 +1,8 @@ package commands; -import constants.Message; - +/** + * A Command Class that send an error message to the Ui + */ public class IncorrectCommand extends Command{ private String message; @@ -10,6 +11,11 @@ public IncorrectCommand(String message) { this.message = message; } + /** + * Sends the error message to the Ui + * @return A CommandResult that tells the Ui to print the error message. + */ + @Override public CommandResult execute() { return new CommandResult(message,PrintOptions.DEFAULT); } diff --git a/src/main/java/commands/ListCommand.java b/src/main/java/commands/ListCommand.java index e8490c7a5..67333ccf7 100644 --- a/src/main/java/commands/ListCommand.java +++ b/src/main/java/commands/ListCommand.java @@ -2,11 +2,19 @@ import task.Task; +/** + * A Command class that contains methods that asks the Ui to print out the ArrayList. + */ public class ListCommand extends Command{ public static final String LIST_IS_EMPTY = "You're a free man :)"; private static final String LIST_IS_NOT_EMPTY = "Here are the things you need to do :"; + /** + * Asks the Ui to print the list if it's not empty and inform the user if it's empty + * @return A CommandResult that is passed to the Ui to show the user the result. + */ + @Override public CommandResult execute(){ if(Task.getTotalTasks() == 0) { return new CommandResult(LIST_IS_EMPTY,PrintOptions.DEFAULT); diff --git a/src/main/java/commands/PrintOptions.java b/src/main/java/commands/PrintOptions.java index d0583d9d6..1a74051ca 100644 --- a/src/main/java/commands/PrintOptions.java +++ b/src/main/java/commands/PrintOptions.java @@ -1,5 +1,9 @@ package commands; +/** + * Used to reduce the number of methods in the Ui class by determining + * what needs to be printed. + */ public enum PrintOptions { WITH_TASK_AND_NUMBER_OF_TASK,ONLY_TASK,LIST,DEFAULT } diff --git a/src/main/java/constants/Constants.java b/src/main/java/constants/Constants.java deleted file mode 100644 index a346cfb0f..000000000 --- a/src/main/java/constants/Constants.java +++ /dev/null @@ -1,9 +0,0 @@ -package constants; - -public class Constants { - public static final int TODO_STARTING_INDEX = 5; - public static final int DEADLINE_STARTING_INDEX = 9; - public static final int EVENT_STARTING_INDEX = 6; - public static final int EXPECTED_LENGTH_FOR_DONE_INPUT = 6; - public static final int EXPECTED_LENGTH_FOR_DELETE_INPUT = 8; -} diff --git a/src/main/java/constants/Message.java b/src/main/java/constants/Message.java index f237c3ac1..4e255a2a2 100644 --- a/src/main/java/constants/Message.java +++ b/src/main/java/constants/Message.java @@ -27,7 +27,13 @@ public class Message { public static final String GETTING_TASK = "Getting your tasks....."; public static final String PROMPT_NUMBER = "Please give me a number :)"; - public static String getSensibleRange(int number) { + /** + * A message that asks the user to give a number between 1 and the total number of tasks. + * + * @param number Total number of tasks + * @return Message + */ + public static String giveSensibleRange(int number) { if(number < 1) { return LIST_IS_EMPTY; } diff --git a/src/main/java/duke/DefaultException.java b/src/main/java/duke/DefaultException.java index b02068f4b..99868f48d 100644 --- a/src/main/java/duke/DefaultException.java +++ b/src/main/java/duke/DefaultException.java @@ -1,4 +1,7 @@ package duke; +/** + * An Exception class created for unexpected errors while running the program. + */ public class DefaultException extends Exception{ } diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index d8440d30f..9369bcee8 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -10,12 +10,19 @@ import java.io.IOException; +/** + * The class that contains the main code for the execution of the Duke Program. + */ public class Duke { private final Storage storage; private final Ui ui; private final TaskList tasks; + /** + * Creates a Duke object to run the application. It also tries to find and load + * the text file duke.txt. + */ public Duke() { ui = new Ui(); storage = new Storage(); @@ -40,6 +47,11 @@ public Duke() { } } + /** + * Repeats the process of getting an input from the user, creating a Parser to parse + * the input, creating a Command object based on the input, then executing the command + * and showing the command output to the user. Stops when an ExitCommand is created. + */ public void run(){ Command command = null; while (!(command instanceof ExitCommand)){ @@ -64,6 +76,13 @@ public static void main(String[] args) { new Duke().run(); } + /** + * Passes the tasks to the command and tries to execute the command. Returns + * appropriate error messages if execution fails. + * + * @param command Command that needs to be executed. + * @return Message and actions from the execution of the command. + */ private CommandResult executeCommand(Command command) { String errorMessage; try { @@ -71,7 +90,7 @@ private CommandResult executeCommand(Command command) { return command.execute(); } catch (IndexOutOfBoundsException error) { - errorMessage = Message.getSensibleRange(Task.getTotalTasks()); + errorMessage = Message.giveSensibleRange(Task.getTotalTasks()); } catch (NumberFormatException error) { errorMessage = Message.PROMPT_NUMBER; diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java index 8c4a7a15f..bca1bbdc8 100644 --- a/src/main/java/duke/DukeException.java +++ b/src/main/java/duke/DukeException.java @@ -1,4 +1,7 @@ package duke; +/** + * An Exception class created for exceptions specific to duke. + */ public class DukeException extends Exception{ } diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index cee4de9f0..50e6375ce 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -1,9 +1,20 @@ package parser; -import commands.*; +import commands.AddCommand; +import commands.DoneCommand; +import commands.ListCommand; +import commands.IncorrectCommand; +import commands.HelpCommand; +import commands.ExitCommand; +import commands.DeleteCommand; +import commands.Command; + import constants.Message; import task.TaskType; +/** + * A class for parsing the input to determine the command to execute + */ public class Parser { private static final String DEADLINE_DESCRIPTION_AND_DATE_SPLITTER = " /by "; @@ -12,6 +23,15 @@ public class Parser { private static final int DEADLINE_STARTING_INDEX = 9; private static final int EVENT_STARTING_INDEX = 6; + /** + * Parses the input to determine the required Command object to be created. + * The command returned depends on the first word of the input before the + * first space. If the first word does not match any of the cases, a default + * error message is returned with the prompt to type 'help'. + * + * @param input Input from user + * @return Certain type of command depending on the input + */ public Command parseInputForDifferentTask(String input) { String type = input.split(" ")[0].toLowerCase(); switch(type) { @@ -48,6 +68,15 @@ public Command parseInputForDifferentTask(String input) { } } + /** + * Further parses the input to create a different AddCommand Object for different task + * types. Returns an error message if something unexpected happens. + * + * @param input Input from the user + * @param type Type of task + * @return AddCommand object with a different parsed Output depending on the type of task + * @throws StringIndexOutOfBoundsException If the format of the input is incorrect + */ private Command prepareAddCommand(String input,TaskType type) throws StringIndexOutOfBoundsException{ String description; String parsedOutput[]; diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index b74637650..fdd8d2325 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -12,12 +12,24 @@ import java.io.IOException; import java.util.Scanner; +/** + * A class that loads tasks to and saves tasks from the duke.txt file. + */ public class Storage { private static final String SEPARATOR_FOR_FILE = " / "; public Storage() {} + /** + * Loads the tasks from duke.txt file and creates a file if no file is found. + * + * @param tasks The ArrayList to store the tasks in + * @throws IOException Throws an exception if something unexpected happens + * when reading the file + * @throws DukeException Throws an exception to tell the user the file is not + * found and a new file is created + */ public void loadTextFile(TaskList tasks) throws IOException, DukeException { File file = new File("./duke.txt"); if(file.createNewFile()) { @@ -29,6 +41,13 @@ public void loadTextFile(TaskList tasks) throws IOException, DukeException { } } + /** + * Saves the tasks to the duke.txt file before the program stops + * + * @param tasks The ArrayList where the tasks are stored + * @throws IOException Throws an exception if something unexpected happens + * when writing to the file. + */ public void saveTasksToFile(TaskList tasks) throws IOException{ FileWriter fw = new FileWriter ("./duke.txt"); for (int i = 0; i < Task.getTotalTasks(); i++) { @@ -37,6 +56,15 @@ public void saveTasksToFile(TaskList tasks) throws IOException{ fw.close(); } + /** + * Adds the tasks from the file and their completion status into the ArrayList + * in different ways depending on the type of task. + * + * @param s the Scanner which is reading the file + * @param tasks The ArrayList of tasks + * @throws IndexOutOfBoundsException Throws an exception if the format of tasks + * saved on the files are different. + */ private void addTaskFromFile(Scanner s, TaskList tasks) throws IndexOutOfBoundsException { String[] parsedOutput = s.nextLine().split(SEPARATOR_FOR_FILE); switch(parsedOutput[0]){ diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index 5818ae07f..80473dd32 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -1,5 +1,9 @@ package task; +/** + * A Task class which contains the description of the task and date in which + * the task needs to be done. + */ public class Deadline extends Task{ public static final String BY = "by: "; protected String date; @@ -10,12 +14,14 @@ public Deadline(String description,String date) { this.date = date; } + @Override public String getStatusIconAndDescription() { String icon = (isDone ? "X" : " "); return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(BY + date); } + @Override public String getStatusIconAndDescriptionForFile() { String icon = (isDone ? "1" : "0"); return IDENTIFIER + SEPARATOR + icon + SEPARATOR + description + SEPARATOR + date; diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index 8b77bbddb..017ca2a01 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -1,5 +1,9 @@ package task; +/** + * A Task Class that saves the description of the task and the period of + * date and time in which the task needs to be done. + */ public class Event extends Task{ public static final String AT = "at: "; protected String dateAndTime; @@ -10,12 +14,14 @@ public Event(String description, String dateAndTime) { this.dateAndTime = dateAndTime; } + @Override public String getStatusIconAndDescription() { String icon = (isDone ? "X" : " "); return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(AT + dateAndTime); } + @Override public String getStatusIconAndDescriptionForFile() { String icon = (isDone ? "1" : "0"); return IDENTIFIER + SEPARATOR + icon + SEPARATOR + description + SEPARATOR + dateAndTime; diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index b00db6eac..be18cf562 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -1,5 +1,9 @@ package task; + +/** + * An abstract class with the common methods and attributes for different types of Tasks. + */ public abstract class Task { protected static final String SEPARATOR = " / "; protected boolean isDone; @@ -7,6 +11,11 @@ public abstract class Task { private static int totalTasks = 0; + /** + * Gets the completion status and description of task when printing it as a list + * + * @return the status and description of a task in a specific format + */ public abstract String getStatusIconAndDescription(); @@ -14,6 +23,11 @@ public void markAsDone() { isDone = true; } + /** + * Gets the completion status and description of the task when saving them to a file + * + * @return the status and description of a task in a specific format + */ public abstract String getStatusIconAndDescriptionForFile(); public static int getTotalTasks() { diff --git a/src/main/java/task/TaskType.java b/src/main/java/task/TaskType.java index bd74dc8ff..1d0aabfd9 100644 --- a/src/main/java/task/TaskType.java +++ b/src/main/java/task/TaskType.java @@ -1,5 +1,8 @@ package task; +/** + * To differentiate between the different types of tasks. + */ public enum TaskType { TODO,DEADLINE,EVENT; } diff --git a/src/main/java/task/ToDo.java b/src/main/java/task/ToDo.java index 378d5de52..e1c8fa4b1 100644 --- a/src/main/java/task/ToDo.java +++ b/src/main/java/task/ToDo.java @@ -1,5 +1,8 @@ package task; +/** + * A Task class that saves the description of the task. + */ public class ToDo extends Task{ private static final String IDENTIFIER = "T"; @@ -7,11 +10,13 @@ public ToDo(String description) { this.description = description; } + @Override public String getStatusIconAndDescription() { String icon = (isDone ? "X" : " "); return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description; } + @Override public String getStatusIconAndDescriptionForFile() { String icon = (isDone ? "1" : "0"); return IDENTIFIER + SEPARATOR + icon + SEPARATOR + description; diff --git a/src/main/java/tasklist/TaskList.java b/src/main/java/tasklist/TaskList.java index 0682d1239..dcd5c4642 100644 --- a/src/main/java/tasklist/TaskList.java +++ b/src/main/java/tasklist/TaskList.java @@ -4,6 +4,9 @@ import java.util.ArrayList; +/** + * A class that stores an ArrayList of tasks and the related methods for the ArrayList. + */ public class TaskList { private ArrayList tasks; @@ -23,7 +26,7 @@ public void deleteTask(int index) throws IndexOutOfBoundsException { Task.setTotalTasks(Task.getTotalTasks() - 1); } - public Task getTask(int index) { + public Task getTask(int index) throws IndexOutOfBoundsException{ return tasks.get(index); } } diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index a56a421b0..bb211ace0 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -6,60 +6,79 @@ import task.Task; import java.io.InputStream; -import java.io.PrintStream; import java.util.Scanner; - +/** + * A class with methods that interacts with the user by accepting inputs and printing results. + */ public class Ui { private final Scanner in; - private final PrintStream out; public static final String INDENTATION = " "; private static final Boolean NOTIFY_NUMBER_OF_TASK = true; private static final Boolean PRINT_TASK = true; public Ui(){ - this(System.in,System.out); + this(System.in); } - public Ui(InputStream in, PrintStream out){ + public Ui(InputStream in){ this.in = new Scanner(in); - this.out = out; } public String getUserCommand() { return in.nextLine(); } + /** + * Divides the outputs so that it looks neater + */ public void printIndentationAndDivider() { - out.print(Message.INDENTATION); - out.println(Message.DIVIDER); + System.out.print(Message.INDENTATION); + System.out.println(Message.DIVIDER); } public void printWordsWithIndentation(String words) { - out.print(Message.INDENTATION); - out.println(words); + System.out.print(Message.INDENTATION); + System.out.println(words); } + /** + * Prints the logo and the welcome message in the given format. + */ public void printStartingMessage() { - out.println("Hello from\n" + Message.LOGO); + System.out.println("Hello from\n" + Message.LOGO); printIndentationAndDivider(); printWordsWithIndentation(Message.HELLO_MESSAGE_2); printIndentationAndDivider(); - out.println(); + System.out.println(); } - public void printMessage(String input) { + /** + * Used to print messages to the user not issued by a CommandResult + * in the given format. + * + * @param message The message that needs to be printed + */ + public void printMessage(String message) { printIndentationAndDivider(); - printWordsWithIndentation(input); + printWordsWithIndentation(message); printIndentationAndDivider(); System.out.println(); } + /** + * Prints the result from the executed Commands in the given format. + * + * @param result CommandResult which contains the content to be printed. + * @param notifyNumberOfTasks Determines whether the number of tasks in + * the list needs to be printed. + * @param printTask Determines whether the selected task needs to be printed. + */ private void printCommandResultMessage(CommandResult result, Boolean notifyNumberOfTasks, Boolean printTask) { printIndentationAndDivider(); - printWordsWithIndentation(result.feedbackToUser); + printWordsWithIndentation(result.message); if(printTask) { printWordsWithIndentation(result.task.getStatusIconAndDescription()); } @@ -70,6 +89,12 @@ private void printCommandResultMessage(CommandResult result, Boolean notifyNumbe System.out.println(); } + /** + * Selects the type of function needed to show the result of different executed Commands + * This reduces the number of methods needed in the Ui class. + * + * @param result CommandResult which contains the content to be printed. + */ public void showResult(CommandResult result) { switch(result.type){ case WITH_TASK_AND_NUMBER_OF_TASK: @@ -85,14 +110,14 @@ public void showResult(CommandResult result) { printCommandResultMessage(result,!NOTIFY_NUMBER_OF_TASK,!PRINT_TASK); break; default: - printMessage(result.feedbackToUser); + printMessage(result.message); } } /** - * Prints the tasks based on the given format + * Prints the task's description with its completion status */ private void printTask(CommandResult result) { for (int i = 0; i < Task.getTotalTasks(); i++) { @@ -100,9 +125,14 @@ private void printTask(CommandResult result) { } } + /** + * Prints the list of tasks with the given format. + * + * @param result The CommandResult determines the message shown to the user + */ private void printList(CommandResult result) { printIndentationAndDivider(); - printWordsWithIndentation(result.feedbackToUser); + printWordsWithIndentation(result.message); printTask(result); printIndentationAndDivider(); System.out.println(); From 45dc0635761be80487440db3a78e29c2eafb01f4 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 14:52:14 +0800 Subject: [PATCH 23/33] Update ReadMe file --- README.md | 81 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 1a36bb905..03bc3b3ed 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,69 @@ -# Duke project template +# Duke -This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. +* Duke helps you record your tasks and manage them. +* It is a CLI program, where you type in commands to the program. +* If you forget or are unsure about Duke's functions, enter **help** to learn how to use them. -## Setting up in Intellij +## Functionalities -Prerequisites: JDK 11, update Intellij to the most recent version. +Here are a few things Duke can do + +1. Allows you to add different kinds of tasks such as ToDos, Deadlines and Events + * ToDo + * ToDos are tasks that can be done at any time + * Format : todo *description* + * Example : todo clean the house
+
+ * Deadline + * Deadlines are tasks that needs to be done before a specific time + * Format: deadline *description* /by *yyyy-MM-ddTHH:mm:ss* + * Example: deadline Homework /by 2021-10-01T23:59:59 + * yyyy represents the year, MM the month, dd the day, HH the hour, mm the minute and ss the second.
+
+ * Event + * Events are tasks that occurs at a specific time period + * Format: event *description* /at *yyyy-MM-ddTHH:mm:ss* /to *yyyy-MM-ddTHH:mm:ss* + * Example: event celebrate Mary's birthday /at 2021-09-30T22:00:00 /to 2021-09-30T23:00:00 + * Add the event start time before **/to** and the event end time after.
+
+2. Shows you the list of tasks you saved onto Duke and allows you to search for specific tasks + + * List + * Shows you the list of all the tasks you have + * Format: list
+
+ * Find + * Find a task with a specific description or date + * Format: find *description* **or** find /d *date* + * Example: find Homework, find /d 2021-09-30 + * **/d** tells Duke that you are searching for a task with a specific date.
+
+3. Allows you to mark a task as done + + * Done + * Marks the task of a specific number on the list as done + * Format: done *integer* + * Example: done 1 (This marks the first task on the list as done.)
+
+4. Allows you to remove a task from the list + + * Delete + * Deletes the task of a specific number on the list + * Format: delete *integer* + * Example: delete 1 (This deletes the first task on the list.)
+
+5. Loads the tasks from the "duke.txt" file when you start the program, if it exists. Creates the file to save your tasks if it doesn't.
+
+6. Automatically saves your tasks to a file "duke.txt" when you terminate the program.
+
+7. To terminate the program, type **bye**.
+
-1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) -1. Open the project into Intellij as follows: - 1. Click `Open`. - 1. Select the project directory, and click `OK`. - 1. If there are any further prompts, accept the defaults. -1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
- In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/duke/Duke.java` file, right-click it, and choose `Run duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| - ``` + ____ _ + | _ \ _ _| | _____ + | | | | | | | |/ / _ \ + | |_| | |_| | < __/ + |____/ \__,_|_|\_\___| + ``` \ No newline at end of file From 540a897d3540beb779722539034fdf92d0701c2e Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 14:57:17 +0800 Subject: [PATCH 24/33] Do a minor change on the ReadMe file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 03bc3b3ed..1e54596b6 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Here are a few things Duke can do
5. Loads the tasks from the "duke.txt" file when you start the program, if it exists. Creates the file to save your tasks if it doesn't.

-6. Automatically saves your tasks to a file "duke.txt" when you terminate the program.
+6. Automatically saves your tasks to the file "duke.txt" when you terminate the program.

7. To terminate the program, type **bye**.

From 31cc2eba79915aa585d94d0b7ea06c2306c8c4a8 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 15:03:51 +0800 Subject: [PATCH 25/33] Update ReadMe to correct GitHub Page ReadMe --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1e54596b6..8e981f104 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,14 @@ Here are a few things Duke can do * ToDo * ToDos are tasks that can be done at any time * Format : todo *description* - * Example : todo clean the house
-
+ * Example : todo clean the house + * Deadline * Deadlines are tasks that needs to be done before a specific time * Format: deadline *description* /by *yyyy-MM-ddTHH:mm:ss* * Example: deadline Homework /by 2021-10-01T23:59:59 - * yyyy represents the year, MM the month, dd the day, HH the hour, mm the minute and ss the second.
-
+ * yyyy represents the year, MM the month, dd the day, HH the hour, mm the minute and ss the second. + * Event * Events are tasks that occurs at a specific time period * Format: event *description* /at *yyyy-MM-ddTHH:mm:ss* /to *yyyy-MM-ddTHH:mm:ss* @@ -30,8 +30,8 @@ Here are a few things Duke can do * List * Shows you the list of all the tasks you have - * Format: list
-
+ * Format: list + * Find * Find a task with a specific description or date * Format: find *description* **or** find /d *date* From 6f942e9886c5920ee4c4073f42e92d71f6ed08f9 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 15:05:32 +0800 Subject: [PATCH 26/33] Correct GitHub Page ReadMe --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 8e981f104..a41522a8d 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,7 @@ Here are a few things Duke can do
6. Automatically saves your tasks to the file "duke.txt" when you terminate the program.

-7. To terminate the program, type **bye**.
-
+7. To terminate the program, type **bye**. ``` Hello from From 69ce26df84dafb2015bc7249b0d87f37acf93508 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 15:13:38 +0800 Subject: [PATCH 27/33] Correct GitHub Page not bolding words --- README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a41522a8d..366185a2d 100644 --- a/README.md +++ b/README.md @@ -6,22 +6,22 @@ ## Functionalities -Here are a few things Duke can do +Here are a few things Duke can do: 1. Allows you to add different kinds of tasks such as ToDos, Deadlines and Events * ToDo - * ToDos are tasks that can be done at any time + * ToDos are tasks that can be done at any time. * Format : todo *description* * Example : todo clean the house * Deadline - * Deadlines are tasks that needs to be done before a specific time + * Deadlines are tasks that needs to be done before a specific time. * Format: deadline *description* /by *yyyy-MM-ddTHH:mm:ss* * Example: deadline Homework /by 2021-10-01T23:59:59 * yyyy represents the year, MM the month, dd the day, HH the hour, mm the minute and ss the second. * Event - * Events are tasks that occurs at a specific time period + * Events are tasks that occurs at a specific time period. * Format: event *description* /at *yyyy-MM-ddTHH:mm:ss* /to *yyyy-MM-ddTHH:mm:ss* * Example: event celebrate Mary's birthday /at 2021-09-30T22:00:00 /to 2021-09-30T23:00:00 * Add the event start time before **/to** and the event end time after.
@@ -29,11 +29,11 @@ Here are a few things Duke can do 2. Shows you the list of tasks you saved onto Duke and allows you to search for specific tasks * List - * Shows you the list of all the tasks you have + * Shows you the list of all the tasks you have. * Format: list * Find - * Find a task with a specific description or date + * Find a task with a specific description or date. * Format: find *description* **or** find /d *date* * Example: find Homework, find /d 2021-09-30 * **/d** tells Duke that you are searching for a task with a specific date.
@@ -41,14 +41,14 @@ Here are a few things Duke can do 3. Allows you to mark a task as done * Done - * Marks the task of a specific number on the list as done + * Marks the task of a specific number on the list as done. * Format: done *integer* * Example: done 1 (This marks the first task on the list as done.)

4. Allows you to remove a task from the list * Delete - * Deletes the task of a specific number on the list + * Deletes the task of a specific number on the list. * Format: delete *integer* * Example: delete 1 (This deletes the first task on the list.)

@@ -56,7 +56,8 @@ Here are a few things Duke can do
6. Automatically saves your tasks to the file "duke.txt" when you terminate the program.

-7. To terminate the program, type **bye**. +7. To terminate the program, type **bye** .
+
``` Hello from From 086b7ecbc632b310e45ffd0b90aa6d336120030d Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 15:22:33 +0800 Subject: [PATCH 28/33] Correct GitHub Page not bolding words attempt 2. --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 366185a2d..de2cb4be7 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,7 @@ Here are a few things Duke can do:
6. Automatically saves your tasks to the file "duke.txt" when you terminate the program.

-7. To terminate the program, type **bye** .
-
+7. To terminate the program, type **bye** . ``` Hello from From 4663af30263eb118b4872e42b6d5b7bd3026d638 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 17:42:39 +0800 Subject: [PATCH 29/33] Update doc/ReadMe and revert the other ReadMe. Update help mesage --- README.md | 71 ++----- docs/README.md | 246 +++++++++++++++++++++++- src/main/java/commands/HelpCommand.java | 21 +- 3 files changed, 267 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index de2cb4be7..ab7d58d49 100644 --- a/README.md +++ b/README.md @@ -5,64 +5,27 @@ * If you forget or are unsure about Duke's functions, enter **help** to learn how to use them. ## Functionalities +# Duke project template -Here are a few things Duke can do: +This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. -1. Allows you to add different kinds of tasks such as ToDos, Deadlines and Events - * ToDo - * ToDos are tasks that can be done at any time. - * Format : todo *description* - * Example : todo clean the house +## Setting up in Intellij - * Deadline - * Deadlines are tasks that needs to be done before a specific time. - * Format: deadline *description* /by *yyyy-MM-ddTHH:mm:ss* - * Example: deadline Homework /by 2021-10-01T23:59:59 - * yyyy represents the year, MM the month, dd the day, HH the hour, mm the minute and ss the second. - - * Event - * Events are tasks that occurs at a specific time period. - * Format: event *description* /at *yyyy-MM-ddTHH:mm:ss* /to *yyyy-MM-ddTHH:mm:ss* - * Example: event celebrate Mary's birthday /at 2021-09-30T22:00:00 /to 2021-09-30T23:00:00 - * Add the event start time before **/to** and the event end time after.
-
-2. Shows you the list of tasks you saved onto Duke and allows you to search for specific tasks - - * List - * Shows you the list of all the tasks you have. - * Format: list - - * Find - * Find a task with a specific description or date. - * Format: find *description* **or** find /d *date* - * Example: find Homework, find /d 2021-09-30 - * **/d** tells Duke that you are searching for a task with a specific date.
-
-3. Allows you to mark a task as done - - * Done - * Marks the task of a specific number on the list as done. - * Format: done *integer* - * Example: done 1 (This marks the first task on the list as done.)
-
-4. Allows you to remove a task from the list - - * Delete - * Deletes the task of a specific number on the list. - * Format: delete *integer* - * Example: delete 1 (This deletes the first task on the list.)
-
-5. Loads the tasks from the "duke.txt" file when you start the program, if it exists. Creates the file to save your tasks if it doesn't.
-
-6. Automatically saves your tasks to the file "duke.txt" when you terminate the program.
-
-7. To terminate the program, type **bye** . +Prerequisites: JDK 11, update Intellij to the most recent version. +1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) +1. Open the project into Intellij as follows: + 1. Click `Open`. + 1. Select the project directory, and click `OK`. + 1. If there are any further prompts, accept the defaults. +1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
+ In the same dialog, set the **Project language level** field to the `SDK default` option. +3. After that, locate the `src/main/java/duke/Duke.java` file, right-click it, and choose `Run duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| + ____ _ + | _ \ _ _| | _____ + | | | | | | | |/ / _ \ + | |_| | |_| | < __/ + |____/ \__,_|_|\_\___| ``` \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index 8077118eb..4ca248320 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,28 +2,256 @@ ## Features -### Feature-ABC +### Add Task -Description of the feature. +Allows you to add different kinds of tasks such as ToDos, Deadlines and Events +* ToDo - A task that can be done anytime +* Deadline - A task that needs to be done before a certain time +* Events - A task that needs to be done at a certain period of time -### Feature-XYZ +### List -Description of the feature. +Shows you the list of tasks you have. + +### Find + +Allows you to find a task with a specific description or date. + +### Done + +Allows you to mark a task as done + +### Delete + +Allows you to remove a task from the list + +### Load + +Loads tasks from the "duke.txt" file if it exists. Creates a new "duke.txt" file if it does not + +### Save + +Saves the tasks to the "duke.txt" file when the program is terminated ## Usage -### `Keyword` - Describe action +### `todo` - todo *description* + +* Adds a ToDo task with its description to the list. +* If successful, the system will print out a success message, the task added, and the number of tasks in the list + +Example of usage: + +`todo read book` + +Expected outcome: + +* The success message is shown, along with the description of the task, the type of task added and the number of tasks in the list. + +``` + ___________________________________________________________ + I've added this to your list :D + [T][ ] read book + Now you have 1 task(s) in the list + ___________________________________________________________ +``` + +### `deadline` - deadline *description* /by *date* + +* Adds a Deadline task with its description and the date and time of the deadline to the list. +* If successful, the system will print out a success message, the task added, and the number of tasks in the list. +* The date of deadline is in the format of *yyyy-MM-ddTHH:mm* , where *yyyy* is the year, *MM* is the month, *dd* is the day, *HH* is the hour and *mm* is the minute. + +Example of usage: + +`deadline finish homework /by 2021-09-30T23:00` + +Expected outcome: + +* The success message is shown, along with the description of the task, date and time of the deadline, the type of task added, and the number of tasks in the list. + +``` + ___________________________________________________________ + I've added this to your list :D + [D][ ] finish homework (by: Sep 30 2021 23:00) + Now you have 2 task(s) in the list + ___________________________________________________________ +``` + + +### `event` - event *description* /at *start time* /to *end time* + +* Adds an Event task with its description and the date and time of the event to the list. +* If successful, the system will return a success message, the task added, and the number of tasks in the list. +* The date and time of the start of the event should be added before */to* and the date and time of the end of the event should be added after */to* +* The date of deadline is in the format of *yyyy-MM-ddTHH:mm* , where *yyyy* is the year, *MM* is the month, *dd* is the day, *HH* is the hour and *mm* is the minute. + +Example of usage: + +`event celebrate birthday /at 2021-09-30T00:00 /to 2021-09-30T02:00` + +Expected outcome: + +* The success message is shown, along with the description of the task, date and time of the event, the type of task added, and the number of tasks in the list. + +``` + ___________________________________________________________ + I've added this to your list :D + [E][ ] celebrate birthday (at: Sep 30 2021 00:00 to Sep 30 02:00) + Now you have 3 task(s) in the list + ___________________________________________________________ +``` + +### `list` - list + +* Displays the list of all tasks and their status of completion. + +Example of usage: + +`list` + +Expected outcome: + +* The list of tasks with their status of completion. +* If **X** appears in the second box, it means the task is completed. +* If the second box is empty, it means the task is not completed. + +``` + ___________________________________________________________ + Here are the things you need to do : + 1.[T][ ] read book + 2.[D][ ] finish homework (by: Sep 30 2021 23:00) + 3.[E][ ] celebrate birthday (at: Sep 30 2021 00:00 to Sep 30 02:00) + ___________________________________________________________ +``` + +### `find` - find *keyword* **or** find /d *date* + +* Searches through the list for tasks containing the keyword or with the same date, then displays all such tasks to the user if there is any. +* The date should be typed in the format of *yyyy-MM-dd* , where *yyyy* is the year, *MM* is the month and *dd* is the day + +Example of usage: + +1. `find read` +2. `find /d 2021-09-30` + +Expected outcome: + +1. A list of tasks containing the keyword given is shown to the user +2. A list of tasks with the same date is shown to the user + +``` + ___________________________________________________________ + Here are the matching tasks in your list : + 1.[T][ ] read book + ___________________________________________________________ + + ___________________________________________________________ + Here are the matching tasks in your list : + 1.[D][ ] finish homework (by: Sep 30 2021 23:00) + 2.[E][ ] celebrate birthday (at: Sep 30 2021 00:00 to Sep 30 02:00) + ___________________________________________________________ +``` + +### `done` - done *integer* + +* Marks the task with the specific number on the list as done. +* If successful, the system will print out the task marked as done and the task will have an **X** in the second box. + +Example of usage: + +`done 1` + +Expected outcome: + +* The first task in the list is marked as done. + +``` + ___________________________________________________________ + You've completed the task! Well done! + [T][X] read book + ___________________________________________________________ +``` + +### `delete` - delete *integer* + +* Deletes the task with the specific number on the list. +* If successful, the system will print out a success message, the deleted task, and the number of remaining tasks. + +Example of usage: + +`delete 1` + +Expected outcome: + +* The first task is deleted. The deleted task and the number of remaining tasks are shown. + +``` + ___________________________________________________________ + Alright, I've removed this task: + [T][X] read book + Now you have 2 task(s) in the list + ___________________________________________________________ +``` + +### `help` - help + +* Asks Duke to show the list of commands. + +Example of usage: + +`help` + +Expected outcome: +* The list of all the commands is printed out. + +``` + ___________________________________________________________ + list + - Shows you the list of tasks you have + + todo (description) + - Saves a general todo task + + deadline (description) /by yyyy-MM-ddTHH:mm + - Saves a task with a given deadline + - yyyy is the year, MM is the month, dd is the day, HH is the hour, + and mm is the minute + + event (description) /at yyyy-MM-ddTHH:mm /to yyyy-MM-ddTHH:mm + - Saves an event happening at a certain time period + - yyyy is the year, MM is the month, dd is the day, HH is the hour, + and mm is the minute + + delete (integer) + - Deletes a task with the corresponding number on the list + + done (integer) + - Marks the task with the corresponding number as done + + find (keyword) + - Finds tasks that contains the String given + + find /d yyyy-MM-dd + - Finds tasks with the same date as the date given + - yyyy is the year, MM is the month and dd is the day + ___________________________________________________________ +``` + +### `bye` - bye -Describe the action and its outcome. +* Terminates the program and saves the tasks to the file "duke.txt". Example of usage: -`keyword (optional arguments)` +`bye` Expected outcome: -Description of the outcome. +* The exit message is printed. ``` -expected output + ___________________________________________________________ + Bye, hope to see you again soon! :) + ___________________________________________________________ ``` diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java index a5ce69877..7a187fee9 100644 --- a/src/main/java/commands/HelpCommand.java +++ b/src/main/java/commands/HelpCommand.java @@ -12,18 +12,23 @@ public class HelpCommand extends Command{ "- Shows you the list of tasks you have\n\n" +INDENTATION + "todo (description)\n" + INDENTATION + "- Saves a general todo task\n\n" + INDENTATION + - "deadline (description) /by yyyy-MM-ddTHH:mm:ss\n" + INDENTATION + - "- Saves a task with a given deadline\n\n" + INDENTATION + - "event (description) /at yyyy-MM-ddTHH:mm:ss /to yyyy-MM-ddTHH:mm:ss\n" + INDENTATION + - "- Saves an event happening at a certain time period\n\n" + INDENTATION + - "delete (number)\n" + INDENTATION + + "deadline (description) /by yyyy-MM-ddTHH:mm\n" + INDENTATION + + "- Saves a task with a given deadline\n" + INDENTATION + + "- yyyy is the year, MM is the month, dd is the day, HH is the hour,\n" +INDENTATION + + " and mm is the minute \n\n" + INDENTATION + + "event (description) /at yyyy-MM-ddTHH:mm /to yyyy-MM-ddTHH:mm\n" + INDENTATION + + "- Saves an event happening at a certain time period\n" + INDENTATION + + "- yyyy is the year, MM is the month, dd is the day, HH is the hour,\n" +INDENTATION+ + " and mm is the minute \n\n" + INDENTATION + + "delete (integer)\n" + INDENTATION + "- Deletes a task with the corresponding number on the list\n\n" + INDENTATION + - "done (number)\n" + INDENTATION + + "done (integer)\n" + INDENTATION + "- Marks the task with the corresponding number as done\n\n" + INDENTATION + - "find (description)\n" + INDENTATION + + "find (keyword)\n" + INDENTATION + "- Finds tasks that contains the String given\n\n" + INDENTATION + "find /d yyyy-MM-dd\n" + INDENTATION + - "- Finds tasks with the same date as the date given"; + "- Finds tasks with the same date as the date given\n" + INDENTATION + + "- yyyy is the year, MM is the month and dd is the day"; /** * Sends the help message to the Ui From 266610b933ed54d4cd445f7288bcf8f48aece61b Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 18:02:05 +0800 Subject: [PATCH 30/33] Slight change to doc/ReadMe --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 4ca248320..af1226cfe 100644 --- a/docs/README.md +++ b/docs/README.md @@ -11,11 +11,11 @@ Allows you to add different kinds of tasks such as ToDos, Deadlines and Events ### List -Shows you the list of tasks you have. +Shows you the list of tasks you have and their status of completion. ### Find -Allows you to find a task with a specific description or date. +Allows you to find tasks with a specific description or date. ### Done From c1dd736a496d1f319f98f9286652bbb98d9f67e5 Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 20:22:13 +0800 Subject: [PATCH 31/33] Update code to adhere to coding standards. Update error message. --- src/main/java/commands/AddCommand.java | 12 ++++++------ src/main/java/commands/FindCommand.java | 4 +++- src/main/java/constants/Message.java | 2 +- src/main/java/parser/Parser.java | 8 ++++---- src/main/java/ui/Ui.java | 3 +-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java index 4ad67f399..0d6914d6d 100644 --- a/src/main/java/commands/AddCommand.java +++ b/src/main/java/commands/AddCommand.java @@ -23,9 +23,9 @@ public class AddCommand extends Command{ public static final String EVENT_FORMAT_ERROR = "Please type the event in the format: \n" + INDENTATION + "'event (description) /at yyyy-MM-ddThh:mm /to yyyy-MM-ddThh:mm' :)"; private static final String DATE_TIME_FORMAT_ERROR_FOR_DEADLINE = "Please write the date and time in the" + - " format : 'yyyy-mm-ddThh:mm:ss' :)"; + " format : 'yyyy-mm-ddThh:mm' :)"; private static final String DATE_TIME_FORMAT_ERROR_FOR_EVENT = "Please write the date and time in the format : \n" - + INDENTATION + "'yyyy-mm-ddThh:mm:ss /to yyy-mm-ddThh:mm' :)"; + + INDENTATION + "'yyyy-mm-ddThh:mm /to yyy-mm-ddThh:mm' :)"; public static final String ADDED_TO_LIST = "I've added this to your list :D"; public static final String TODO_COMMAND = "todo"; public static final String EVENT_COMMAND = "event"; @@ -74,11 +74,11 @@ private CommandResult getCommandResultForDeadline() { } try { tasks.addTask(new Deadline(parsedOutputs[0], LocalDateTime.parse(parsedOutputs[1]))); + return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), + PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); } catch (DateTimeParseException error) { return new CommandResult(DATE_TIME_FORMAT_ERROR_FOR_DEADLINE,PrintOptions.DEFAULT); } - return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), - PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); } /** @@ -94,11 +94,11 @@ private CommandResult getCommandResultForEvent() { try { tasks.addTask(new Event(parsedOutputs[0], LocalDateTime.parse(parsedOutputs[1]), LocalDateTime.parse(parsedOutputs[2]))); + return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), + PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); } catch (DateTimeParseException error) { return new CommandResult(DATE_TIME_FORMAT_ERROR_FOR_EVENT,PrintOptions.DEFAULT); } - return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), - PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); } diff --git a/src/main/java/commands/FindCommand.java b/src/main/java/commands/FindCommand.java index a27f5038f..4aab2f56d 100644 --- a/src/main/java/commands/FindCommand.java +++ b/src/main/java/commands/FindCommand.java @@ -103,6 +103,8 @@ private Task taskWithSameDate(Task temp) { } private Boolean sameDate (LocalDateTime date) { - return (date.getYear() == dateTime.getYear() && date.getDayOfYear() == dateTime.getDayOfYear()); + Boolean isSameYear = date.getYear() == dateTime.getYear(); + Boolean isSameDayOfYear = date.getDayOfYear() == dateTime.getDayOfYear(); + return (isSameYear && isSameDayOfYear); } } diff --git a/src/main/java/constants/Message.java b/src/main/java/constants/Message.java index 09c733ba8..175951850 100644 --- a/src/main/java/constants/Message.java +++ b/src/main/java/constants/Message.java @@ -30,7 +30,7 @@ public class Message { public static final String PROMPT_NUMBER = "Please give me an integer :)"; /** - * A message that asks the user to give a number between 1 and the total number of tasks. + * Asks the user to give a number between 1 and the total number of tasks. * * @param number Total number of tasks * @return Message diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index d0ee42d68..311b5b995 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -112,7 +112,7 @@ private Command executeTaskCase(String input, TaskType type, String todoFormatEr } /** - * Further parses the input to create a different AddCommand Object for different task + * Parses the input further to create a different AddCommand Object for different task * types. Returns an error message if something unexpected happens. * * @param input Input from the user @@ -156,7 +156,7 @@ private String[] getParsedOutputForEvent(String description) throws ArrayIndexOu } /** - * Further parses the input to create a different FindCommand object for finding task with + * Parses the input further to create a different FindCommand object for finding task with * a specific description / date. * * @param input Parsed input from the user. @@ -175,7 +175,7 @@ private FindCommand prepareFindCommand(String input) throws IndexOutOfBoundsExce } /** - * Further parses the input to create a DoneCommand with the index of the task to be + * Parses the input further to create a DoneCommand with the index of the task to be * marked as done. * * @param input Parsed input from the user. @@ -195,7 +195,7 @@ private DoneCommand prepareDoneCommand(String input) throws DukeException, } /** - * Further parses the input to create a DoneCommand with the index of the task to be + * Parses the input further to create a DoneCommand with the index of the task to be * marked as done. * * @param input Parsed input from the user. diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index 005144cb2..9c69ee370 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -55,8 +55,7 @@ public void printStartingMessage() { } /** - * Used to print messages to the user not issued by a CommandResult - * in the given format. + * Prints messages to the user not issued by a CommandResult in the given format. * * @param message The message that needs to be printed */ From 2157f69cfac7dc3e35fda3dae60362867d66d88d Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Wed, 29 Sep 2021 21:18:18 +0800 Subject: [PATCH 32/33] Include error handling for general errors in reading file --- src/main/java/constants/Message.java | 7 +++++-- src/main/java/duke/Duke.java | 13 ++++++------- src/main/java/storage/Storage.java | 4 +++- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/constants/Message.java b/src/main/java/constants/Message.java index 175951850..1866f339c 100644 --- a/src/main/java/constants/Message.java +++ b/src/main/java/constants/Message.java @@ -18,13 +18,16 @@ public class Message { + "|____/ \\__,_|_|\\_\\___|\n"; public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry," + " I don't know what you mean, do type 'help' if you're unsure :)"; - public static final String IO_EXCEPTION_MESSAGE = "Oops, something unexpected happened while writing to file"; + public static final String FILE_WRITING_ERROR_MESSAGE = "Oops, something unexpected happened while " + + "writing to file"; + public static final String FILE_READING_ERROR_MESSAGE = "Oops, something unexpected happened while " + + "reading the file"; public static final String PROMPT_TASK_NUMBER = "Please tell me which task you want to select :)"; public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1 and "; public static final String DEFAULT_ERROR_MESSAGE = "Oops, something went wrong!"; + public static final String FILE_ERROR_MESSAGE = "Oops, there might be something wrong with the file."; public static final String FILE_NOT_CREATED = "Looks like you don't have a file for your tasks, " + "let me create one now....."; - public static final String INCORRECT_FORMAT = "Oops, file format is incorrect. Please correct it!"; public static final String DONE = "Done!"; public static final String GETTING_TASK = "Getting your tasks....."; public static final String PROMPT_NUMBER = "Please give me an integer :)"; diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 72ecc20ea..4d27b6e2c 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -36,15 +36,14 @@ public Duke() { try { storage.loadTextFile(tasks); ui.printMessage(Message.DONE); - } catch (IndexOutOfBoundsException error) { - ui.printMessage(Message.INCORRECT_FORMAT); - System.exit(1); - } catch (IOException error) { - ui.printMessage(Message.DEFAULT_ERROR_MESSAGE); - System.exit(1); } catch (DukeException error) { ui.printMessage(Message.FILE_NOT_CREATED); ui.printMessage(Message.DONE); + } catch (IOException error) { + ui.printMessage(Message.FILE_READING_ERROR_MESSAGE); + } catch (Exception error) { + ui.printMessage(Message.FILE_ERROR_MESSAGE); + System.exit(1); } } @@ -72,7 +71,7 @@ private void exitProcess() { try { storage.saveTasksToFile(tasks); } catch (IOException error) { - ui.printMessage(Message.IO_EXCEPTION_MESSAGE); + ui.printMessage(Message.FILE_WRITING_ERROR_MESSAGE); } System.exit(0); } diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index 17818b978..ba1ce2f86 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -12,6 +12,7 @@ import java.io.FileWriter; import java.io.IOException; import java.time.LocalDateTime; +import java.time.format.DateTimeParseException; import java.util.Scanner; /** @@ -67,7 +68,8 @@ public void saveTasksToFile(TaskList tasks) throws IOException{ * @throws IndexOutOfBoundsException Throws an exception if the format of tasks * saved on the files are different. */ - private void addTaskFromFile(Scanner s, TaskList tasks) throws IndexOutOfBoundsException { + private void addTaskFromFile(Scanner s, TaskList tasks) throws IndexOutOfBoundsException, + DateTimeParseException { String[] parsedOutput = s.nextLine().split(SEPARATOR_FOR_FILE); switch(parsedOutput[0]){ case ToDo.IDENTIFIER: From 4ebd498480c04a40f49ce140893d079d5aa8e28d Mon Sep 17 00:00:00 2001 From: vincentlauhl Date: Tue, 5 Oct 2021 17:35:01 +0800 Subject: [PATCH 33/33] Correction on reused code format --- src/main/java/duke/Duke.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 4d27b6e2c..6f93d609c 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -53,7 +53,7 @@ public Duke() { * command and showing the command output to the user. * Stops when an ExitCommand is created. */ - //@@se-edu vincentlauhl-reused + //@@author vincentlauhl-reused //Reused from https://github.com/se-edu/addressbook-level2/blob/master/src/seedu/addressbook/Main.java //with minor modifications public void run(){ @@ -66,6 +66,7 @@ public void run(){ } exitProcess(); } + //@@author private void exitProcess() { try {