From a63c9f71c58715075586b4a90e7350636e0826e0 Mon Sep 17 00:00:00 2001 From: monihog Date: Wed, 18 Jan 2023 01:04:30 +0800 Subject: [PATCH 01/23] Level-0 --- src/main/java/Duke.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..4c24caf3d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,9 @@ public class Duke { + public static void exitMessage() { + System.out.println("Goodbye!"); + } public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + System.out.println("Hi it's Duke\nWhat can I do for you?"); + exitMessage(); } } From ef8d231475c0e1288496d48269d5bc485ce35ea6 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 27 Jan 2023 12:26:25 +0800 Subject: [PATCH 02/23] Added echo Changed Duke's Name --- src/main/java/Duke.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4c24caf3d..3aefa1b4b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,9 +1,22 @@ +import java.util.Scanner; public class Duke { public static void exitMessage() { - System.out.println("Goodbye!"); + System.out.println("Go away Anna"); + System.out.println("O-kay bye......"); } public static void main(String[] args) { - System.out.println("Hi it's Duke\nWhat can I do for you?"); - exitMessage(); + System.out.println("Hi it's Anna!\nWhat can I do for you?"); + Scanner in = new Scanner(System.in); + String input; + boolean exit = false; + while (!exit) { + input = in.nextLine(); + if (input.equals("bye")) { + exitMessage(); + exit = true; + } else { + System.out.println(input); + } + } } } From bdd7c9d91f120f423920230abb575461d02ef8a9 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 27 Jan 2023 13:42:32 +0800 Subject: [PATCH 03/23] Added list functionality --- src/main/java/Duke.java | 7 +++++-- src/main/java/ToDoList.java | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ToDoList.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3aefa1b4b..6f833651d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,11 +1,12 @@ import java.util.Scanner; +import java.util.Vector; public class Duke { public static void exitMessage() { System.out.println("Go away Anna"); System.out.println("O-kay bye......"); } public static void main(String[] args) { - System.out.println("Hi it's Anna!\nWhat can I do for you?"); + System.out.println("Hi it's Anna!\nWhat do you need to do?"); Scanner in = new Scanner(System.in); String input; boolean exit = false; @@ -14,8 +15,10 @@ public static void main(String[] args) { if (input.equals("bye")) { exitMessage(); exit = true; + } else if (input.equals("list")){ + ToDoList.viewList(); } else { - System.out.println(input); + ToDoList.addItem(input); } } } diff --git a/src/main/java/ToDoList.java b/src/main/java/ToDoList.java new file mode 100644 index 000000000..096e62876 --- /dev/null +++ b/src/main/java/ToDoList.java @@ -0,0 +1,17 @@ +import java.util.Vector; + +public class ToDoList { + private static Vector list = new Vector(10,1); + + public static void addItem (String in) { + list.addElement(in); + System.out.println("added: " + in); + } + + public static void viewList () { + for (int i = 0; i < list.size(); ++i) { + System.out.println(i+1 + ". " + list.elementAt(i)); + } + } + +} From 9d947097ad661781e7ec82c9fe0c657226140e89 Mon Sep 17 00:00:00 2001 From: monihog Date: Wed, 1 Feb 2023 01:47:59 +0800 Subject: [PATCH 04/23] Added ability to mark and unmark as done Improved handling of commands by splitting user input Changed to use Tasks classes --- src/main/java/Duke.java | 66 ++++++++++++++++++++++++++++++++----- src/main/java/Task.java | 36 ++++++++++++++++++++ src/main/java/ToDoList.java | 39 ++++++++++++++++++---- 3 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 6f833651d..f720fb3e0 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,25 +1,75 @@ import java.util.Scanner; -import java.util.Vector; + public class Duke { public static void exitMessage() { System.out.println("Go away Anna"); System.out.println("O-kay bye......"); } + public static void main(String[] args) { System.out.println("Hi it's Anna!\nWhat do you need to do?"); Scanner in = new Scanner(System.in); - String input; + boolean exit = false; while (!exit) { - input = in.nextLine(); - if (input.equals("bye")) { + String input = (in.nextLine()).trim(); + String inputCMD, inputItem; + if (input.contains(" ")) { + inputCMD = input.split(" ", 2)[0]; + inputItem = input.split(" ", 2)[1]; + } else { + inputCMD = input; + inputItem = null; + } + + switch (inputCMD) { + case "bye": exitMessage(); exit = true; - } else if (input.equals("list")){ - ToDoList.viewList(); - } else { - ToDoList.addItem(input); + break; + case "list": + if (ToDoList.getNumItems() == 0) { + System.out.println("We are free! Let's go play!"); + } else { + System.out.println("Here's what we've gotta do:"); + ToDoList.viewList(); + } + break; + case "mark": + if (inputItem == null) { + System.out.println("What should I mark?"); + ToDoList.viewList(); + inputItem = in.nextLine().trim(); + } + + ToDoList.markDone(Integer.parseInt(inputItem)-1); + System.out.println("Okay I've marked item " + inputItem + " as done:"); + ToDoList.printItem(Integer.parseInt(inputItem)-1); + break; + + case "unmark": + if (inputItem == null) { + System.out.println("What should I unmark?"); + //TODO: add in handling if input is "unmark 2" again + ToDoList.viewList(); + inputItem = in.nextLine().trim(); + } + + ToDoList.markNotDone(Integer.parseInt(inputItem)-1); + System.out.println("Oh no! Are we not done with " + inputItem + " after all?"); + ToDoList.printItem(Integer.parseInt(inputItem)-1); + break; + case "add": + ToDoList.addItem(inputItem); + break; + default: + System.out.println("I didn't get that!"); + break; } } } } + + + + diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..38bb66d83 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,36 @@ + +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { //ok to leave as public? + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + + public void setDescription(String description) { + this.description = description; + } + + public void markDone() { + isDone = true; + } + + public void markNotDone() { + isDone = false; + } + + public String getDescription() { + return description; + } + + public String getTask() { + String Task = "["; + return Task.concat(getStatusIcon() + "] " + getDescription()); + } + +} diff --git a/src/main/java/ToDoList.java b/src/main/java/ToDoList.java index 096e62876..9727c9605 100644 --- a/src/main/java/ToDoList.java +++ b/src/main/java/ToDoList.java @@ -1,17 +1,42 @@ -import java.util.Vector; +import java.util.ArrayList; public class ToDoList { - private static Vector list = new Vector(10,1); - + private static final ArrayList TaskList = new ArrayList<>(10); + private static int NumTasks = 0; public static void addItem (String in) { - list.addElement(in); + Task task = new Task(in); + + TaskList.add(task); + NumTasks += 1; System.out.println("added: " + in); } - + public static int getNumItems() { + return NumTasks; + } public static void viewList () { - for (int i = 0; i < list.size(); ++i) { - System.out.println(i+1 + ". " + list.elementAt(i)); + for (int i = 0; i < TaskList.size(); ++i) { + System.out.print(i+1 + ". "); + + System.out.println(TaskList.get(i).getTask()); + } + } + public static void markDone (int index) { + if (TaskList.get(index).getStatusIcon().equals(" ")) { + TaskList.get(index).markDone(); + } + } + public static void markNotDone (int index) { + if (TaskList.get(index).getStatusIcon().equals("X")) { + TaskList.get(index).markNotDone(); } } + public static Task getItem (int index) { + return TaskList.get(index); + } + public static void printItem (int index) { + System.out.print(index+1 + ". "); + System.out.println(TaskList.get(index).getTask()); + } + } From a3a43470403d361b9f286370a4d979ea05dd1301 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 10 Feb 2023 02:14:28 +0800 Subject: [PATCH 05/23] Added deadline, event, todo types for list objects --- src/main/java/Deadline.java | 22 +++ src/main/java/Duke.java | 132 +++++++++++++----- src/main/java/Event.java | 27 ++++ src/main/java/Task.java | 9 +- .../java/{ToDoList.java => TaskList.java} | 10 +- src/main/java/Todo.java | 10 ++ 6 files changed, 164 insertions(+), 46 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java rename src/main/java/{ToDoList.java => TaskList.java} (83%) 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..3a44e5b66 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,22 @@ +public class Deadline extends Task { + protected String dueDate; + + public Deadline(String description, String dueDate) { + super(description); + this.dueDate = dueDate; + } + @Override + public String getTypeIcon() { + return "D"; + } + + public String getDueDate() { + return dueDate; + } + + @Override + public String getTask() { + return taskTypeIcon() + isDoneIcon() + getDescription() + + " (by: " + getDueDate() + ")"; + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f720fb3e0..310d4ac4e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,65 +6,123 @@ public static void exitMessage() { System.out.println("O-kay bye......"); } + public static String getItemDescription(String[] input) { + Scanner in = new Scanner(System.in); + if (input.length != 1) { + return input[1]; + } else { + System.out.println("What are you referring to?"); + return in.nextLine().trim(); + } + } + + public static String getDueDate(String[] input) { + Scanner in = new Scanner(System.in); + if (input[1].contains("/by")) { + return input[1].split("/by",2)[1]; + } else { + System.out.println("When is this due by?"); { + return in.nextLine().trim(); + } + } + } + public static String[] getStartEndDates(String[] input) { + Scanner in = new Scanner(System.in); + String[] StartEndDates = new String[2]; + + if (input[1].contains("/from") && input[1].contains("/to")) { + StartEndDates[0] = input[1].substring(input[1].indexOf("/from"),input[1].indexOf("/to")).trim(); + StartEndDates[1] = input[1].substring(input[1].indexOf("/to")).trim(); + } else if (input[1].contains("/from")) { + StartEndDates[0] = input[1].substring(input[1].indexOf("/from")).trim(); + System.out.println("When does this event end?"); + StartEndDates[1] = in.nextLine().trim(); + } else if (input[1].contains("/to")) { + System.out.println("When does this event start?"); + StartEndDates[0] = in.nextLine().trim(); + StartEndDates[1] = input[1].substring(input[1].indexOf("/to")).trim(); + } else { + System.out.println("When does this event start?"); + StartEndDates[0] = in.nextLine().trim(); + System.out.println("When does this event end?"); + StartEndDates[1] = in.nextLine().trim(); + } + return StartEndDates; + } public static void main(String[] args) { System.out.println("Hi it's Anna!\nWhat do you need to do?"); Scanner in = new Scanner(System.in); - boolean exit = false; - while (!exit) { - String input = (in.nextLine()).trim(); - String inputCMD, inputItem; - if (input.contains(" ")) { - inputCMD = input.split(" ", 2)[0]; - inputItem = input.split(" ", 2)[1]; - } else { - inputCMD = input; - inputItem = null; - } + while (true) { + String userInput = in.nextLine().trim(); + //String[] input = new String[2]; + String[] input = userInput.split(" ",2); - switch (inputCMD) { + String inputCommand = input[0]; + switch (inputCommand) { case "bye": exitMessage(); - exit = true; - break; + return; case "list": - if (ToDoList.getNumItems() == 0) { + if (TaskList.getNumItems() == 0) { System.out.println("We are free! Let's go play!"); } else { System.out.println("Here's what we've gotta do:"); - ToDoList.viewList(); + TaskList.viewList(); } break; - case "mark": - if (inputItem == null) { - System.out.println("What should I mark?"); - ToDoList.viewList(); - inputItem = in.nextLine().trim(); - } + case "mark": { + String itemNum = getItemDescription(input); - ToDoList.markDone(Integer.parseInt(inputItem)-1); - System.out.println("Okay I've marked item " + inputItem + " as done:"); - ToDoList.printItem(Integer.parseInt(inputItem)-1); + TaskList.markDone(Integer.parseInt(itemNum) - 1); + System.out.println("Okay I've marked item " + itemNum + " as done:"); + TaskList.printItem(Integer.parseInt(itemNum) - 1); break; + } - case "unmark": - if (inputItem == null) { - System.out.println("What should I unmark?"); - //TODO: add in handling if input is "unmark 2" again - ToDoList.viewList(); - inputItem = in.nextLine().trim(); - } + case "unmark": { + String itemNum = getItemDescription(input); + + TaskList.markNotDone(Integer.parseInt(itemNum) - 1); + System.out.println("Oh no! Are we not done with " + itemNum + " after all?"); + TaskList.printItem(Integer.parseInt(itemNum) - 1); + break; + } + + case "add": { + String itemDescription = getItemDescription(input); + Task newTask = new Task(itemDescription); + TaskList.addItem(newTask); + break; + } + case "todo": { + String itemDescription = getItemDescription(input); + Todo newTask = new Todo(itemDescription); + TaskList.addItem(newTask); + break; + } - ToDoList.markNotDone(Integer.parseInt(inputItem)-1); - System.out.println("Oh no! Are we not done with " + inputItem + " after all?"); - ToDoList.printItem(Integer.parseInt(inputItem)-1); + case "deadline": { + String itemDescription = getItemDescription(input); + String dueDate = getDueDate(input); + Deadline newTask = new Deadline(itemDescription,dueDate); + TaskList.addItem(newTask); break; - case "add": - ToDoList.addItem(inputItem); + } + + case "event": + String itemDescription = getItemDescription(input); + String[] StartEndDates = getStartEndDates(input); + String startDate = StartEndDates[0]; + String endDate = StartEndDates[1]; + Event newTask = new Event(itemDescription,startDate,endDate); + TaskList.addItem(newTask); break; + default: System.out.println("I didn't get that!"); break; + } } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..386091217 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,27 @@ +public class Event extends Task{ + String startDate, endDate; + public Event(String description, String startDate, String endDate) { + super(description); + this.startDate = startDate; + this.endDate = endDate; + } + @Override + public String getTypeIcon() { + return "E"; + } + + public String getStartDate() { + return startDate; + } + + public String getEndDate() { + return endDate; + } + + @Override + public String getTask() { + return taskTypeIcon() + isDoneIcon() + getDescription() + System.lineSeparator() + + "Start: " + getStartDate() + System.lineSeparator() + + "End: " + getEndDate(); + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 38bb66d83..90f7d9c7c 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -11,7 +11,10 @@ public Task(String description) { //ok to leave as public? public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } - + public String getTypeIcon() { + return "NULL"; + } + public String taskTypeIcon() { return "[" + getTypeIcon() + "]";} public void setDescription(String description) { this.description = description; } @@ -24,13 +27,13 @@ public void markNotDone() { isDone = false; } + public String isDoneIcon() { return "[" + getStatusIcon() + "]";} public String getDescription() { return description; } public String getTask() { - String Task = "["; - return Task.concat(getStatusIcon() + "] " + getDescription()); + return taskTypeIcon() + isDoneIcon() + " " + getDescription(); } } diff --git a/src/main/java/ToDoList.java b/src/main/java/TaskList.java similarity index 83% rename from src/main/java/ToDoList.java rename to src/main/java/TaskList.java index 9727c9605..d36b5d58e 100644 --- a/src/main/java/ToDoList.java +++ b/src/main/java/TaskList.java @@ -1,14 +1,12 @@ import java.util.ArrayList; -public class ToDoList { +public class TaskList { private static final ArrayList TaskList = new ArrayList<>(10); private static int NumTasks = 0; - public static void addItem (String in) { - Task task = new Task(in); - - TaskList.add(task); + public static void addItem (Task newTask) { + TaskList.add(newTask); NumTasks += 1; - System.out.println("added: " + in); + System.out.println("Okay! I've added: [" + newTask.getTypeIcon() +"] " + newTask.getDescription()); } public static int getNumItems() { return NumTasks; diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java new file mode 100644 index 000000000..935127dd2 --- /dev/null +++ b/src/main/java/Todo.java @@ -0,0 +1,10 @@ +public class Todo extends Task { + public Todo(String description) { + super(description); + } + + @Override + public String getTypeIcon() { + return "T"; + } +} From c711aeb4190636a067efef8b3dff3aafe5e591dc Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 10 Feb 2023 02:19:30 +0800 Subject: [PATCH 06/23] fixed coding standard issues --- src/main/java/Duke.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 310d4ac4e..d9c88ebc6 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -21,7 +21,8 @@ public static String getDueDate(String[] input) { if (input[1].contains("/by")) { return input[1].split("/by",2)[1]; } else { - System.out.println("When is this due by?"); { + System.out.println("When is this due by?"); + { return in.nextLine().trim(); } } @@ -55,10 +56,9 @@ public static void main(String[] args) { while (true) { String userInput = in.nextLine().trim(); - //String[] input = new String[2]; String[] input = userInput.split(" ",2); - String inputCommand = input[0]; + switch (inputCommand) { case "bye": exitMessage(); @@ -71,6 +71,7 @@ public static void main(String[] args) { TaskList.viewList(); } break; + case "mark": { String itemNum = getItemDescription(input); @@ -95,6 +96,7 @@ public static void main(String[] args) { TaskList.addItem(newTask); break; } + case "todo": { String itemDescription = getItemDescription(input); Todo newTask = new Todo(itemDescription); From 28406be17bef1a371a3498e6cb5c176dc466f5e5 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 10 Feb 2023 17:18:32 +0800 Subject: [PATCH 07/23] added bad input handling added immediate prompt for missing information reworked the terrible code in getStartEnd --- src/main/java/Duke.java | 84 ++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index d9c88ebc6..f8b75e8ca 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,52 +1,59 @@ +import java.lang.reflect.Array; +import java.util.ArrayList; import java.util.Scanner; public class Duke { + static final int COMMAND_INDEX = 0; + static final int MAX_COMMAND_LENGTH = 1; + static final int DESCRIPTION_INDEX = 1; + static final int STARTDATE_INDEX = 0; + static final int ENDDATE_INDEX = 1; + public static void exitMessage() { System.out.println("Go away Anna"); System.out.println("O-kay bye......"); } - public static String getItemDescription(String[] input) { + public static String getItemDescription(String userInput) { Scanner in = new Scanner(System.in); - if (input.length != 1) { - return input[1]; - } else { + String description; + try { + description = userInput.split(" ", 2)[DESCRIPTION_INDEX]; + } catch (ArrayIndexOutOfBoundsException e) { System.out.println("What are you referring to?"); - return in.nextLine().trim(); + description = in.nextLine().trim(); } + return description; } - public static String getDueDate(String[] input) { + public static String getDueDate(String userInput) { Scanner in = new Scanner(System.in); - if (input[1].contains("/by")) { - return input[1].split("/by",2)[1]; + String dueDate; + if (userInput.contains("/by")) { + dueDate = userInput.substring(userInput.indexOf("/by")); } else { System.out.println("When is this due by?"); - { - return in.nextLine().trim(); - } - } + dueDate = in.nextLine().trim(); + } + return dueDate; } - public static String[] getStartEndDates(String[] input) { + + public static String[] getStartEndDates(String userInput) { Scanner in = new Scanner(System.in); String[] StartEndDates = new String[2]; - if (input[1].contains("/from") && input[1].contains("/to")) { - StartEndDates[0] = input[1].substring(input[1].indexOf("/from"),input[1].indexOf("/to")).trim(); - StartEndDates[1] = input[1].substring(input[1].indexOf("/to")).trim(); - } else if (input[1].contains("/from")) { - StartEndDates[0] = input[1].substring(input[1].indexOf("/from")).trim(); - System.out.println("When does this event end?"); - StartEndDates[1] = in.nextLine().trim(); - } else if (input[1].contains("/to")) { - System.out.println("When does this event start?"); - StartEndDates[0] = in.nextLine().trim(); - StartEndDates[1] = input[1].substring(input[1].indexOf("/to")).trim(); + if (userInput.contains("/from")) { + StartEndDates[STARTDATE_INDEX] = userInput.substring(userInput.indexOf("/from"),userInput.indexOf("/to")).trim(); } else { System.out.println("When does this event start?"); - StartEndDates[0] = in.nextLine().trim(); + StartEndDates[STARTDATE_INDEX] = in.nextLine().trim(); + } + + if (userInput.contains("/to")) { + StartEndDates[ENDDATE_INDEX] = userInput.substring(userInput.indexOf("/to")).trim(); + } else { System.out.println("When does this event end?"); - StartEndDates[1] = in.nextLine().trim(); + StartEndDates[ENDDATE_INDEX] = in.nextLine().trim(); } return StartEndDates; } @@ -56,8 +63,9 @@ public static void main(String[] args) { while (true) { String userInput = in.nextLine().trim(); - String[] input = userInput.split(" ",2); - String inputCommand = input[0]; + ArrayList input = new ArrayList<>(); + input.add(COMMAND_INDEX, userInput.split(" ", 2)[COMMAND_INDEX]); + String inputCommand = input.get(COMMAND_INDEX); switch (inputCommand) { case "bye": @@ -73,7 +81,7 @@ public static void main(String[] args) { break; case "mark": { - String itemNum = getItemDescription(input); + String itemNum = getItemDescription(userInput); TaskList.markDone(Integer.parseInt(itemNum) - 1); System.out.println("Okay I've marked item " + itemNum + " as done:"); @@ -82,7 +90,7 @@ public static void main(String[] args) { } case "unmark": { - String itemNum = getItemDescription(input); + String itemNum = getItemDescription(userInput); TaskList.markNotDone(Integer.parseInt(itemNum) - 1); System.out.println("Oh no! Are we not done with " + itemNum + " after all?"); @@ -91,32 +99,32 @@ public static void main(String[] args) { } case "add": { - String itemDescription = getItemDescription(input); + String itemDescription = getItemDescription(userInput); Task newTask = new Task(itemDescription); TaskList.addItem(newTask); break; } case "todo": { - String itemDescription = getItemDescription(input); + String itemDescription = getItemDescription(userInput); Todo newTask = new Todo(itemDescription); TaskList.addItem(newTask); break; } case "deadline": { - String itemDescription = getItemDescription(input); - String dueDate = getDueDate(input); + String itemDescription = getItemDescription(userInput); + String dueDate = getDueDate(userInput); Deadline newTask = new Deadline(itemDescription,dueDate); TaskList.addItem(newTask); break; } case "event": - String itemDescription = getItemDescription(input); - String[] StartEndDates = getStartEndDates(input); - String startDate = StartEndDates[0]; - String endDate = StartEndDates[1]; + String itemDescription = getItemDescription(userInput); + String[] StartEndDates = getStartEndDates(userInput); + String startDate = StartEndDates[STARTDATE_INDEX]; + String endDate = StartEndDates[ENDDATE_INDEX]; Event newTask = new Event(itemDescription,startDate,endDate); TaskList.addItem(newTask); break; From 6cf1e94a580a81fbd246a6adb18c9ef3f910043b Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 24 Feb 2023 22:09:07 +0800 Subject: [PATCH 08/23] added delete functionality to list --- src/main/java/Duke.java | 27 +++++++++++++++++++++++++-- src/main/java/TaskList.java | 6 +++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f8b75e8ca..5d083b553 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -8,6 +8,10 @@ public class Duke { static final int DESCRIPTION_INDEX = 1; static final int STARTDATE_INDEX = 0; static final int ENDDATE_INDEX = 1; + static final String DEADLINE_MARKER = "/by"; + + static final String TASK_NO_EXIST = "Task does not exist!"; + static final String DELETE_TASK_MESSAGE = "Okay! I've deleted task: "; public static void exitMessage() { System.out.println("Go away Anna"); @@ -77,6 +81,7 @@ public static void main(String[] args) { } else { System.out.println("Here's what we've gotta do:"); TaskList.viewList(); + System.out.println("We currently have " + TaskList.getNumItems() + " tasks"); } break; @@ -105,6 +110,18 @@ public static void main(String[] args) { break; } + case "delete": { + String itemNum = getItemDescription(userInput); + int itemIndex = Integer.parseInt(itemNum) - 1; + if (itemIndex <= TaskList.getNumItems()-1) { + TaskList.deleteTask(itemIndex); + System.out.println(DELETE_TASK_MESSAGE + itemNum); + } else { + System.out.println(TASK_NO_EXIST); + } + break; + } + case "todo": { String itemDescription = getItemDescription(userInput); Todo newTask = new Todo(itemDescription); @@ -113,8 +130,14 @@ public static void main(String[] args) { } case "deadline": { - String itemDescription = getItemDescription(userInput); - String dueDate = getDueDate(userInput); + String itemDescription, dueDate; + if (userInput.contains(DEADLINE_MARKER)) { + itemDescription = userInput.split(DEADLINE_MARKER, 2)[0]; + dueDate = userInput.split(DEADLINE_MARKER, 2)[1]; + } else { + itemDescription = getItemDescription(userInput); + dueDate = getDueDate(userInput); + } Deadline newTask = new Deadline(itemDescription,dueDate); TaskList.addItem(newTask); break; diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index d36b5d58e..a20681260 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -14,7 +14,6 @@ public static int getNumItems() { public static void viewList () { for (int i = 0; i < TaskList.size(); ++i) { System.out.print(i+1 + ". "); - System.out.println(TaskList.get(i).getTask()); } } @@ -36,5 +35,10 @@ public static void printItem (int index) { System.out.println(TaskList.get(index).getTask()); } + public static void deleteTask(int index) { + TaskList.remove(index); + NumTasks -= 1; + } + } From e3a2df52830e8a1b2fa6068fc92c351a9d3216f1 Mon Sep 17 00:00:00 2001 From: monihog Date: Mon, 27 Feb 2023 01:11:01 +0800 Subject: [PATCH 09/23] added tasklist export functionality fixed minor presentation error in event --- src/main/java/Duke.java | 25 +++++++++++++++++++++++++ src/main/java/Event.java | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f8b75e8ca..1a5a940ec 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,6 +1,9 @@ +import java.io.FileWriter; +import java.io.IOException; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Scanner; +import java.io.File; public class Duke { static final int COMMAND_INDEX = 0; @@ -8,12 +11,29 @@ public class Duke { static final int DESCRIPTION_INDEX = 1; static final int STARTDATE_INDEX = 0; static final int ENDDATE_INDEX = 1; + public static final String TASKLIST_EXPORT_PATH = "C:\\Users\\lhyao\\Desktop\\tasks.txt"; public static void exitMessage() { System.out.println("Go away Anna"); System.out.println("O-kay bye......"); } + public static void writeToTaskList() { + File exportTaskList = new File (TASKLIST_EXPORT_PATH); + try { + FileWriter writeTaskList = new FileWriter(TASKLIST_EXPORT_PATH); + int numTasks = TaskList.getNumItems(); + for (int i = 0; i < numTasks; ++i) { + writeTaskList.write(TaskList.getItem(i).getTask()); + writeTaskList.write(System.lineSeparator()); + } + writeTaskList.close(); + System.out.println("Successfully exported TaskList!"); + System.out.println("Written to: " + TASKLIST_EXPORT_PATH); + } catch (IOException e) { + System.out.println("Error occurred while writing to tasks.txt"); + } + } public static String getItemDescription(String userInput) { Scanner in = new Scanner(System.in); String description; @@ -105,6 +125,11 @@ public static void main(String[] args) { break; } + case "save": { + writeToTaskList(); + break; + } + case "todo": { String itemDescription = getItemDescription(userInput); Todo newTask = new Todo(itemDescription); diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 386091217..6a840409d 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -20,7 +20,7 @@ public String getEndDate() { @Override public String getTask() { - return taskTypeIcon() + isDoneIcon() + getDescription() + System.lineSeparator() + return taskTypeIcon() + isDoneIcon() + " " + getDescription() + System.lineSeparator() + "Start: " + getStartDate() + System.lineSeparator() + "End: " + getEndDate(); } From e0bf091bf007230bf36fba1496df5df51d462a40 Mon Sep 17 00:00:00 2001 From: monihog Date: Wed, 1 Mar 2023 01:07:22 +0800 Subject: [PATCH 10/23] reverted desktop export path to relative /out path introduced some constants introduced file path printout in event of export failure --- src/main/java/Duke.java | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 1a5a940ec..f4fcc8082 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,6 +1,5 @@ import java.io.FileWriter; import java.io.IOException; -import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Scanner; import java.io.File; @@ -11,7 +10,12 @@ public class Duke { static final int DESCRIPTION_INDEX = 1; static final int STARTDATE_INDEX = 0; static final int ENDDATE_INDEX = 1; - public static final String TASKLIST_EXPORT_PATH = "C:\\Users\\lhyao\\Desktop\\tasks.txt"; + public static final String TASKLIST_EXPORT_PATH = "TaskList.txt"; + public static final String SUCCESS_EXPORT = "Successfully exported TaskList!"; + public static final String EXPORT_ERROR_PREFIX = "Error occurred while writing to "; + public static final String STARTDATE_USERINPUT_PREFIX = "/from"; + public static final String ENDDATE_USERINPUT_PREFIX = "/to"; + public static final String DEADLINE_USERINPUT_PREFIX = "/by"; public static void exitMessage() { System.out.println("Go away Anna"); @@ -28,10 +32,10 @@ public static void writeToTaskList() { writeTaskList.write(System.lineSeparator()); } writeTaskList.close(); - System.out.println("Successfully exported TaskList!"); + System.out.println(SUCCESS_EXPORT); System.out.println("Written to: " + TASKLIST_EXPORT_PATH); } catch (IOException e) { - System.out.println("Error occurred while writing to tasks.txt"); + System.out.println(EXPORT_ERROR_PREFIX + exportTaskList.getAbsolutePath()); } } public static String getItemDescription(String userInput) { @@ -49,8 +53,8 @@ public static String getItemDescription(String userInput) { public static String getDueDate(String userInput) { Scanner in = new Scanner(System.in); String dueDate; - if (userInput.contains("/by")) { - dueDate = userInput.substring(userInput.indexOf("/by")); + if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { + dueDate = userInput.substring(userInput.indexOf(DEADLINE_USERINPUT_PREFIX)); } else { System.out.println("When is this due by?"); dueDate = in.nextLine().trim(); @@ -62,15 +66,15 @@ public static String[] getStartEndDates(String userInput) { Scanner in = new Scanner(System.in); String[] StartEndDates = new String[2]; - if (userInput.contains("/from")) { - StartEndDates[STARTDATE_INDEX] = userInput.substring(userInput.indexOf("/from"),userInput.indexOf("/to")).trim(); + if (userInput.contains(STARTDATE_USERINPUT_PREFIX)) { + StartEndDates[STARTDATE_INDEX] = userInput.substring(userInput.indexOf(STARTDATE_USERINPUT_PREFIX),userInput.indexOf(ENDDATE_USERINPUT_PREFIX)).trim(); } else { System.out.println("When does this event start?"); StartEndDates[STARTDATE_INDEX] = in.nextLine().trim(); } - if (userInput.contains("/to")) { - StartEndDates[ENDDATE_INDEX] = userInput.substring(userInput.indexOf("/to")).trim(); + if (userInput.contains(ENDDATE_USERINPUT_PREFIX)) { + StartEndDates[ENDDATE_INDEX] = userInput.substring(userInput.indexOf(ENDDATE_USERINPUT_PREFIX)).trim(); } else { System.out.println("When does this event end?"); StartEndDates[ENDDATE_INDEX] = in.nextLine().trim(); @@ -88,6 +92,8 @@ public static void main(String[] args) { String inputCommand = input.get(COMMAND_INDEX); switch (inputCommand) { + + case "exit": case "bye": exitMessage(); return; From 44155f7c6a5a539a414886457957baf61892b80a Mon Sep 17 00:00:00 2001 From: monihog Date: Wed, 1 Mar 2023 01:37:39 +0800 Subject: [PATCH 11/23] packaged files removed redundanct "add" method in Duke.java --- src/main/java/{ => com/ip/duke}/Duke.java | 12 ++++++------ src/main/java/{ => com/ip/duke/tasks}/Deadline.java | 2 ++ src/main/java/{ => com/ip/duke/tasks}/Event.java | 2 ++ src/main/java/{ => com/ip/duke/tasks}/Task.java | 1 + src/main/java/{ => com/ip/duke/tasks}/TaskList.java | 3 +++ src/main/java/{ => com/ip/duke/tasks}/Todo.java | 2 ++ 6 files changed, 16 insertions(+), 6 deletions(-) rename src/main/java/{ => com/ip/duke}/Duke.java (96%) rename src/main/java/{ => com/ip/duke/tasks}/Deadline.java (94%) rename src/main/java/{ => com/ip/duke/tasks}/Event.java (96%) rename src/main/java/{ => com/ip/duke/tasks}/Task.java (97%) rename src/main/java/{ => com/ip/duke/tasks}/TaskList.java (97%) rename src/main/java/{ => com/ip/duke/tasks}/Todo.java (86%) diff --git a/src/main/java/Duke.java b/src/main/java/com/ip/duke/Duke.java similarity index 96% rename from src/main/java/Duke.java rename to src/main/java/com/ip/duke/Duke.java index 9ea029166..f65c3b56a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/com/ip/duke/Duke.java @@ -1,3 +1,9 @@ +package com.ip.duke; + +import com.ip.duke.tasks.Deadline; +import com.ip.duke.tasks.Event; +import com.ip.duke.tasks.TaskList; +import com.ip.duke.tasks.Todo; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; @@ -128,12 +134,6 @@ public static void main(String[] args) { break; } - case "add": { - String itemDescription = getItemDescription(userInput); - Task newTask = new Task(itemDescription); - TaskList.addItem(newTask); - break; - } case "delete": { String itemNum = getItemDescription(userInput); diff --git a/src/main/java/Deadline.java b/src/main/java/com/ip/duke/tasks/Deadline.java similarity index 94% rename from src/main/java/Deadline.java rename to src/main/java/com/ip/duke/tasks/Deadline.java index 3a44e5b66..d67063e5a 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/com/ip/duke/tasks/Deadline.java @@ -1,3 +1,5 @@ +package com.ip.duke.tasks; + public class Deadline extends Task { protected String dueDate; diff --git a/src/main/java/Event.java b/src/main/java/com/ip/duke/tasks/Event.java similarity index 96% rename from src/main/java/Event.java rename to src/main/java/com/ip/duke/tasks/Event.java index 6a840409d..d4d06ecb1 100644 --- a/src/main/java/Event.java +++ b/src/main/java/com/ip/duke/tasks/Event.java @@ -1,3 +1,5 @@ +package com.ip.duke.tasks; + public class Event extends Task{ String startDate, endDate; public Event(String description, String startDate, String endDate) { diff --git a/src/main/java/Task.java b/src/main/java/com/ip/duke/tasks/Task.java similarity index 97% rename from src/main/java/Task.java rename to src/main/java/com/ip/duke/tasks/Task.java index 90f7d9c7c..d06a0be7d 100644 --- a/src/main/java/Task.java +++ b/src/main/java/com/ip/duke/tasks/Task.java @@ -1,3 +1,4 @@ +package com.ip.duke.tasks; public class Task { protected String description; diff --git a/src/main/java/TaskList.java b/src/main/java/com/ip/duke/tasks/TaskList.java similarity index 97% rename from src/main/java/TaskList.java rename to src/main/java/com/ip/duke/tasks/TaskList.java index a20681260..6f2fefc04 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/com/ip/duke/tasks/TaskList.java @@ -1,3 +1,6 @@ +package com.ip.duke.tasks; + + import java.util.ArrayList; public class TaskList { diff --git a/src/main/java/Todo.java b/src/main/java/com/ip/duke/tasks/Todo.java similarity index 86% rename from src/main/java/Todo.java rename to src/main/java/com/ip/duke/tasks/Todo.java index 935127dd2..318609936 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/com/ip/duke/tasks/Todo.java @@ -1,3 +1,5 @@ +package com.ip.duke.tasks; + public class Todo extends Task { public Todo(String description) { super(description); From 95409e72cdb03bca7d5d59b80e95026a8288c81b Mon Sep 17 00:00:00 2001 From: monihog Date: Wed, 1 Mar 2023 03:37:54 +0800 Subject: [PATCH 12/23] moved some methods to Ui class --- src/main/java/Duke.java | 85 +++++++++-------------------------------- src/main/java/Ui.java | 64 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 68 deletions(-) create mode 100644 src/main/java/Ui.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 9ea029166..d605bfea8 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -7,24 +7,15 @@ public class Duke { static final int COMMAND_INDEX = 0; static final int MAX_COMMAND_LENGTH = 1; - static final int DESCRIPTION_INDEX = 1; static final int STARTDATE_INDEX = 0; static final int ENDDATE_INDEX = 1; - static final String DEADLINE_MARKER = "/by"; + public static final String DEADLINE_USERINPUT_PREFIX = "/by"; static final String TASK_NO_EXIST = "Task does not exist!"; static final String DELETE_TASK_MESSAGE = "Okay! I've deleted task: "; public static final String TASKLIST_EXPORT_PATH = "TaskList.txt"; public static final String SUCCESS_EXPORT = "Successfully exported TaskList!"; public static final String EXPORT_ERROR_PREFIX = "Error occurred while writing to "; - public static final String STARTDATE_USERINPUT_PREFIX = "/from"; - public static final String ENDDATE_USERINPUT_PREFIX = "/to"; - public static final String DEADLINE_USERINPUT_PREFIX = "/by"; - - public static void exitMessage() { - System.out.println("Go away Anna"); - System.out.println("O-kay bye......"); - } - + public static void writeToTaskList() { File exportTaskList = new File (TASKLIST_EXPORT_PATH); try { @@ -41,51 +32,9 @@ public static void writeToTaskList() { System.out.println(EXPORT_ERROR_PREFIX + exportTaskList.getAbsolutePath()); } } - public static String getItemDescription(String userInput) { - Scanner in = new Scanner(System.in); - String description; - try { - description = userInput.split(" ", 2)[DESCRIPTION_INDEX]; - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("What are you referring to?"); - description = in.nextLine().trim(); - } - return description; - } - - public static String getDueDate(String userInput) { - Scanner in = new Scanner(System.in); - String dueDate; - if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { - dueDate = userInput.substring(userInput.indexOf(DEADLINE_USERINPUT_PREFIX)); - } else { - System.out.println("When is this due by?"); - dueDate = in.nextLine().trim(); - } - return dueDate; - } - - public static String[] getStartEndDates(String userInput) { - Scanner in = new Scanner(System.in); - String[] StartEndDates = new String[2]; - - if (userInput.contains(STARTDATE_USERINPUT_PREFIX)) { - StartEndDates[STARTDATE_INDEX] = userInput.substring(userInput.indexOf(STARTDATE_USERINPUT_PREFIX),userInput.indexOf(ENDDATE_USERINPUT_PREFIX)).trim(); - } else { - System.out.println("When does this event start?"); - StartEndDates[STARTDATE_INDEX] = in.nextLine().trim(); - } - - if (userInput.contains(ENDDATE_USERINPUT_PREFIX)) { - StartEndDates[ENDDATE_INDEX] = userInput.substring(userInput.indexOf(ENDDATE_USERINPUT_PREFIX)).trim(); - } else { - System.out.println("When does this event end?"); - StartEndDates[ENDDATE_INDEX] = in.nextLine().trim(); - } - return StartEndDates; - } + public static void main(String[] args) { - System.out.println("Hi it's Anna!\nWhat do you need to do?"); + Ui.welcomeMessage(); Scanner in = new Scanner(System.in); while (true) { @@ -98,7 +47,7 @@ public static void main(String[] args) { case "exit": case "bye": - exitMessage(); + Ui.exitMessage(); return; case "list": if (TaskList.getNumItems() == 0) { @@ -111,7 +60,7 @@ public static void main(String[] args) { break; case "mark": { - String itemNum = getItemDescription(userInput); + String itemNum = Ui.getItemDescription(userInput); TaskList.markDone(Integer.parseInt(itemNum) - 1); System.out.println("Okay I've marked item " + itemNum + " as done:"); @@ -120,7 +69,7 @@ public static void main(String[] args) { } case "unmark": { - String itemNum = getItemDescription(userInput); + String itemNum = Ui.getItemDescription(userInput); TaskList.markNotDone(Integer.parseInt(itemNum) - 1); System.out.println("Oh no! Are we not done with " + itemNum + " after all?"); @@ -129,14 +78,14 @@ public static void main(String[] args) { } case "add": { - String itemDescription = getItemDescription(userInput); + String itemDescription = Ui.getItemDescription(userInput); Task newTask = new Task(itemDescription); TaskList.addItem(newTask); break; } case "delete": { - String itemNum = getItemDescription(userInput); + String itemNum = Ui.getItemDescription(userInput); int itemIndex = Integer.parseInt(itemNum) - 1; if (itemIndex <= TaskList.getNumItems() - 1) { TaskList.deleteTask(itemIndex); @@ -153,7 +102,7 @@ public static void main(String[] args) { } case "todo": { - String itemDescription = getItemDescription(userInput); + String itemDescription = Ui.getItemDescription(userInput); Todo newTask = new Todo(itemDescription); TaskList.addItem(newTask); break; @@ -161,12 +110,12 @@ public static void main(String[] args) { case "deadline": { String itemDescription, dueDate; - if (userInput.contains(DEADLINE_MARKER)) { - itemDescription = userInput.split(DEADLINE_MARKER, 2)[0]; - dueDate = userInput.split(DEADLINE_MARKER, 2)[1]; + if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { + itemDescription = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[0]; + dueDate = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[1]; } else { - itemDescription = getItemDescription(userInput); - dueDate = getDueDate(userInput); + itemDescription = Ui.getItemDescription(userInput); + dueDate = Ui.getDueDate(userInput); } Deadline newTask = new Deadline(itemDescription,dueDate); TaskList.addItem(newTask); @@ -174,8 +123,8 @@ public static void main(String[] args) { } case "event": - String itemDescription = getItemDescription(userInput); - String[] StartEndDates = getStartEndDates(userInput); + String itemDescription = Ui.getItemDescription(userInput); + String[] StartEndDates = Ui.getStartEndDates(userInput); String startDate = StartEndDates[STARTDATE_INDEX]; String endDate = StartEndDates[ENDDATE_INDEX]; Event newTask = new Event(itemDescription,startDate,endDate); diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 000000000..297948eda --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,64 @@ +import java.util.Scanner; + +public class Ui { + + public static final String EXIT_MESSAGE = "Go away Anna\nO-kay bye......"; + static final int DESCRIPTION_INDEX = 1; + public static final String STARTDATE_USERINPUT_PREFIX = "/from"; + public static final String ENDDATE_USERINPUT_PREFIX = "/to"; + public static final String DEADLINE_USERINPUT_PREFIX = "/by"; + + static final int STARTDATE_INDEX = 0; + static final int ENDDATE_INDEX = 1; + private static final String WELCOME_MESSAGE = "Hi it's Anna!\nWhat do you need to do?"; + + public static void welcomeMessage() { + System.out.println(WELCOME_MESSAGE); + } + + public static void exitMessage() { + System.out.println(EXIT_MESSAGE); + } + public static String getItemDescription(String userInput) { + Scanner in = new Scanner(System.in); + String description; + try { + description = userInput.split(" ", 2)[DESCRIPTION_INDEX]; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("What are you referring to?"); + description = in.nextLine().trim(); + } + return description; + } + + public static String getDueDate(String userInput) { + Scanner in = new Scanner(System.in); + String dueDate; + if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { + dueDate = userInput.substring(userInput.indexOf(DEADLINE_USERINPUT_PREFIX)); + } else { + System.out.println("When is this due by?"); + dueDate = in.nextLine().trim(); + } + return dueDate; + } + public static String[] getStartEndDates(String userInput) { + Scanner in = new Scanner(System.in); + String[] StartEndDates = new String[2]; + + if (userInput.contains(STARTDATE_USERINPUT_PREFIX)) { + StartEndDates[STARTDATE_INDEX] = userInput.substring(userInput.indexOf(STARTDATE_USERINPUT_PREFIX),userInput.indexOf(ENDDATE_USERINPUT_PREFIX)).trim(); + } else { + System.out.println("When does this event start?"); + StartEndDates[STARTDATE_INDEX] = in.nextLine().trim(); + } + + if (userInput.contains(ENDDATE_USERINPUT_PREFIX)) { + StartEndDates[ENDDATE_INDEX] = userInput.substring(userInput.indexOf(ENDDATE_USERINPUT_PREFIX)).trim(); + } else { + System.out.println("When does this event end?"); + StartEndDates[ENDDATE_INDEX] = in.nextLine().trim(); + } + return StartEndDates; + } +} From fd05d8e18573b940ff224073e4d11f28ac7f8155 Mon Sep 17 00:00:00 2001 From: monihog Date: Wed, 1 Mar 2023 17:13:39 +0800 Subject: [PATCH 13/23] rewrote and combined mark/unmark methos --- src/main/java/Duke.java | 86 +++++++---------------------------------- 1 file changed, 15 insertions(+), 71 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index d605bfea8..25fbcffbf 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,8 +1,4 @@ -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; import java.util.Scanner; -import java.io.File; public class Duke { static final int COMMAND_INDEX = 0; @@ -12,82 +8,36 @@ public class Duke { public static final String DEADLINE_USERINPUT_PREFIX = "/by"; static final String TASK_NO_EXIST = "Task does not exist!"; static final String DELETE_TASK_MESSAGE = "Okay! I've deleted task: "; - public static final String TASKLIST_EXPORT_PATH = "TaskList.txt"; - public static final String SUCCESS_EXPORT = "Successfully exported TaskList!"; - public static final String EXPORT_ERROR_PREFIX = "Error occurred while writing to "; - - public static void writeToTaskList() { - File exportTaskList = new File (TASKLIST_EXPORT_PATH); - try { - FileWriter writeTaskList = new FileWriter(TASKLIST_EXPORT_PATH); - int numTasks = TaskList.getNumItems(); - for (int i = 0; i < numTasks; ++i) { - writeTaskList.write(TaskList.getItem(i).getTask()); - writeTaskList.write(System.lineSeparator()); - } - writeTaskList.close(); - System.out.println(SUCCESS_EXPORT); - System.out.println("Written to: " + TASKLIST_EXPORT_PATH); - } catch (IOException e) { - System.out.println(EXPORT_ERROR_PREFIX + exportTaskList.getAbsolutePath()); - } - } - + public static final int STATUSTYPE_DONE = 1; + public static final int STATUSTYPE_NOTDONE = 0; + + public static void main(String[] args) { Ui.welcomeMessage(); Scanner in = new Scanner(System.in); - while (true) { String userInput = in.nextLine().trim(); - ArrayList input = new ArrayList<>(); - input.add(COMMAND_INDEX, userInput.split(" ", 2)[COMMAND_INDEX]); - String inputCommand = input.get(COMMAND_INDEX); + String inputCommand = userInput.split(" ", 2)[COMMAND_INDEX]; switch (inputCommand) { - case "exit": case "bye": Ui.exitMessage(); return; case "list": - if (TaskList.getNumItems() == 0) { - System.out.println("We are free! Let's go play!"); - } else { - System.out.println("Here's what we've gotta do:"); - TaskList.viewList(); - System.out.println("We currently have " + TaskList.getNumItems() + " tasks"); - } + Commands.showList(); break; - - case "mark": { - String itemNum = Ui.getItemDescription(userInput); - - TaskList.markDone(Integer.parseInt(itemNum) - 1); - System.out.println("Okay I've marked item " + itemNum + " as done:"); - TaskList.printItem(Integer.parseInt(itemNum) - 1); + case "mark": + Commands.markTask(userInput, STATUSTYPE_DONE); break; - } - case "unmark": { - String itemNum = Ui.getItemDescription(userInput); - - TaskList.markNotDone(Integer.parseInt(itemNum) - 1); - System.out.println("Oh no! Are we not done with " + itemNum + " after all?"); - TaskList.printItem(Integer.parseInt(itemNum) - 1); - break; - } - - case "add": { - String itemDescription = Ui.getItemDescription(userInput); - Task newTask = new Task(itemDescription); - TaskList.addItem(newTask); + Commands.markTask(userInput, STATUSTYPE_NOTDONE); break; } - case "delete": { String itemNum = Ui.getItemDescription(userInput); - int itemIndex = Integer.parseInt(itemNum) - 1; - if (itemIndex <= TaskList.getNumItems() - 1) { + int itemIndex = Integer.parseInt(itemNum) - STATUSTYPE_DONE; + if (itemIndex <= TaskList.getNumItems() - STATUSTYPE_DONE) { TaskList.deleteTask(itemIndex); System.out.println(DELETE_TASK_MESSAGE + itemNum); } else { @@ -95,24 +45,21 @@ public static void main(String[] args) { } break; } - case "save": { - writeToTaskList(); + Storage.writeToTaskList(); break; } - case "todo": { String itemDescription = Ui.getItemDescription(userInput); Todo newTask = new Todo(itemDescription); TaskList.addItem(newTask); break; } - case "deadline": { String itemDescription, dueDate; if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { - itemDescription = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[0]; - dueDate = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[1]; + itemDescription = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[STATUSTYPE_NOTDONE]; + dueDate = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[STATUSTYPE_DONE]; } else { itemDescription = Ui.getItemDescription(userInput); dueDate = Ui.getDueDate(userInput); @@ -121,7 +68,6 @@ public static void main(String[] args) { TaskList.addItem(newTask); break; } - case "event": String itemDescription = Ui.getItemDescription(userInput); String[] StartEndDates = Ui.getStartEndDates(userInput); @@ -130,11 +76,9 @@ public static void main(String[] args) { Event newTask = new Event(itemDescription,startDate,endDate); TaskList.addItem(newTask); break; - default: - System.out.println("I didn't get that!"); + Commands.invalidCommand(); break; - } } } From bc51fbb9c7c378771dafaf635942e17a83c6bc06 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 3 Mar 2023 02:30:44 +0800 Subject: [PATCH 14/23] Moved switch cases into OOP Reformatted event and deadline displays --- src/main/java/Commands.java | 76 +++++++++++++++++++++++++++---------- src/main/java/Deadline.java | 2 +- src/main/java/Duke.java | 38 +++---------------- src/main/java/Event.java | 4 +- src/main/java/Ui.java | 28 +++++++++++++- 5 files changed, 91 insertions(+), 57 deletions(-) diff --git a/src/main/java/Commands.java b/src/main/java/Commands.java index 314e1a9ba..1d22b6274 100644 --- a/src/main/java/Commands.java +++ b/src/main/java/Commands.java @@ -1,6 +1,13 @@ import java.util.Scanner; public class Commands { + static final String TASK_NO_EXIST = "Task does not exist!"; + static final String DELETE_TASK_MESSAGE = "Okay! I've deleted task: "; + public static final String DEADLINE_USERINPUT_PREFIX = "/by"; + public static final int STATUSTYPE_DONE = 1; + public static final int STATUSTYPE_NOTDONE = 0; + static final int STARTDATE_INDEX = 0; + static final int ENDDATE_INDEX = 1; public static void invalidCommand() { System.out.println("I didn't get that!"); } @@ -15,29 +22,58 @@ public static void showList() { } public static void markTask(String userInput, int statusType) { - String itemDescription = Ui.getItemDescription(userInput); - int itemNum = 0; - try { - itemNum = Integer.parseInt(itemDescription); - } catch (NumberFormatException e) { - Scanner in = new Scanner(System.in); - System.out.println("Sorry! What number is that on the list?"); - itemNum = in.nextInt(); - } finally { - if (itemNum > 0 && itemNum <= TaskList.getNumItems()) { - if (statusType == 1) { - TaskList.markDone(itemNum - 1); - System.out.println("Okay I've marked item " + itemNum + " as done:"); - TaskList.printItem(itemNum - 1); - } else { - TaskList.markNotDone(itemNum - 1); - System.out.println("Oh we aren't done with item " + itemNum + "?"); - TaskList.printItem(itemNum - 1); - } + int itemNum = Ui.getItemNumber(userInput); + if (itemNum > 0 && itemNum <= TaskList.getNumItems()) { + if (statusType == 1) { + TaskList.markDone(itemNum - 1); + System.out.println("Okay I've marked item " + itemNum + " as done:"); + TaskList.printItem(itemNum - 1); } else { - System.out.println("Item does not exist!"); + TaskList.markNotDone(itemNum - 1); + System.out.println("Oh we aren't done with item " + itemNum + "?"); + TaskList.printItem(itemNum - 1); } + } else { + System.out.println("Item does not exist!"); + } + + } + + public static void deleteTask(String userInput) { + int itemIndex = Ui.getItemIndex(userInput); + if (itemIndex <= TaskList.getNumItems() - 1) { + TaskList.deleteTask(itemIndex); + System.out.println(DELETE_TASK_MESSAGE + (itemIndex+1)); + } else { + System.out.println(TASK_NO_EXIST); + } + } + + public static void addTodoTask(String userInput) { + String itemDescription = Ui.getItemDescription(userInput); + Todo newTask = new Todo(itemDescription); + TaskList.addItem(newTask); + } + + public static void addDeadlineTask(String userInput) { + String itemDescription, dueDate; + if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { + itemDescription = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[STATUSTYPE_NOTDONE]; + dueDate = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[STATUSTYPE_DONE]; + } else { + itemDescription = Ui.getItemDescription(userInput); + dueDate = Ui.getDueDate(userInput); } + Deadline newTask = new Deadline(itemDescription,dueDate); + TaskList.addItem(newTask); } + public static void addEventTask(String userInput) { + String itemDescription = Ui.getItemDescription(userInput); + String[] StartEndDates = Ui.getStartEndDates(userInput); + String startDate = StartEndDates[STARTDATE_INDEX]; + String endDate = StartEndDates[ENDDATE_INDEX]; + Event newTask = new Event(itemDescription,startDate,endDate); + TaskList.addItem(newTask); + } } diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 3a44e5b66..4422a1008 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -17,6 +17,6 @@ public String getDueDate() { @Override public String getTask() { return taskTypeIcon() + isDoneIcon() + getDescription() - + " (by: " + getDueDate() + ")"; + + System.lineSeparator() + " Deadline: " + getDueDate(); } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 25fbcffbf..491a7be2d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,15 +3,10 @@ public class Duke { static final int COMMAND_INDEX = 0; static final int MAX_COMMAND_LENGTH = 1; - static final int STARTDATE_INDEX = 0; - static final int ENDDATE_INDEX = 1; - public static final String DEADLINE_USERINPUT_PREFIX = "/by"; - static final String TASK_NO_EXIST = "Task does not exist!"; - static final String DELETE_TASK_MESSAGE = "Okay! I've deleted task: "; + public static final int STATUSTYPE_DONE = 1; public static final int STATUSTYPE_NOTDONE = 0; - public static void main(String[] args) { Ui.welcomeMessage(); Scanner in = new Scanner(System.in); @@ -35,14 +30,7 @@ public static void main(String[] args) { break; } case "delete": { - String itemNum = Ui.getItemDescription(userInput); - int itemIndex = Integer.parseInt(itemNum) - STATUSTYPE_DONE; - if (itemIndex <= TaskList.getNumItems() - STATUSTYPE_DONE) { - TaskList.deleteTask(itemIndex); - System.out.println(DELETE_TASK_MESSAGE + itemNum); - } else { - System.out.println(TASK_NO_EXIST); - } + Commands.deleteTask(userInput); break; } case "save": { @@ -50,31 +38,15 @@ public static void main(String[] args) { break; } case "todo": { - String itemDescription = Ui.getItemDescription(userInput); - Todo newTask = new Todo(itemDescription); - TaskList.addItem(newTask); + Commands.addTodoTask(userInput); break; } case "deadline": { - String itemDescription, dueDate; - if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { - itemDescription = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[STATUSTYPE_NOTDONE]; - dueDate = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[STATUSTYPE_DONE]; - } else { - itemDescription = Ui.getItemDescription(userInput); - dueDate = Ui.getDueDate(userInput); - } - Deadline newTask = new Deadline(itemDescription,dueDate); - TaskList.addItem(newTask); + Commands.addDeadlineTask(userInput); break; } case "event": - String itemDescription = Ui.getItemDescription(userInput); - String[] StartEndDates = Ui.getStartEndDates(userInput); - String startDate = StartEndDates[STARTDATE_INDEX]; - String endDate = StartEndDates[ENDDATE_INDEX]; - Event newTask = new Event(itemDescription,startDate,endDate); - TaskList.addItem(newTask); + Commands.addEventTask(userInput); break; default: Commands.invalidCommand(); diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 6a840409d..f36363e0e 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -21,7 +21,7 @@ public String getEndDate() { @Override public String getTask() { return taskTypeIcon() + isDoneIcon() + " " + getDescription() + System.lineSeparator() - + "Start: " + getStartDate() + System.lineSeparator() - + "End: " + getEndDate(); + + " Start: " + getStartDate() + System.lineSeparator() + + " End: " + getEndDate(); } } diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 297948eda..ef7996880 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,3 +1,4 @@ +import java.util.InputMismatchException; import java.util.Scanner; public class Ui { @@ -23,7 +24,7 @@ public static String getItemDescription(String userInput) { Scanner in = new Scanner(System.in); String description; try { - description = userInput.split(" ", 2)[DESCRIPTION_INDEX]; + description = userInput.split(" ", 3)[DESCRIPTION_INDEX]; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("What are you referring to?"); description = in.nextLine().trim(); @@ -31,6 +32,31 @@ public static String getItemDescription(String userInput) { return description; } + public static int getItemNumber(String userInput) { + Scanner in = new Scanner(System.in); + int itemNumber; + try { + itemNumber = Integer.parseInt(userInput.split(" ", 2)[1]); + } catch (ArrayIndexOutOfBoundsException e) { + TaskList.viewList(); + System.out.println("What is the number of the item in the list?"); + try { + itemNumber = in.nextInt(); + } catch (InputMismatchException i){ + System.out.println("Please enter a number"); + itemNumber = getItemNumber(userInput); + } + } catch (NumberFormatException n) { + //Search for the item and spit out the index? + itemNumber = getItemNumber(userInput); + } + return itemNumber; + } + + public static int getItemIndex(String userInput) { + return getItemNumber(userInput) - 1; + } + public static String getDueDate(String userInput) { Scanner in = new Scanner(System.in); String dueDate; From 81192bc195612b5b5fed4d071dcd5cbe63645712 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 3 Mar 2023 13:46:37 +0800 Subject: [PATCH 15/23] added search functionality --- src/main/java/Commands.java | 33 ++++++++++++++++++++++++++++----- src/main/java/Duke.java | 7 ++++--- src/main/java/Storage.java | 3 ++- src/main/java/TaskList.java | 3 +++ src/main/java/Ui.java | 16 +++++++++++++++- 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/main/java/Commands.java b/src/main/java/Commands.java index 1d22b6274..896db5f88 100644 --- a/src/main/java/Commands.java +++ b/src/main/java/Commands.java @@ -1,4 +1,4 @@ -import java.util.Scanner; +import java.util.ArrayList; public class Commands { static final String TASK_NO_EXIST = "Task does not exist!"; @@ -8,6 +8,10 @@ public class Commands { public static final int STATUSTYPE_NOTDONE = 0; static final int STARTDATE_INDEX = 0; static final int ENDDATE_INDEX = 1; + public static final String SHOWLIST_HEADER = "Here's what we've gotta do:"; + public static final String SHOWLIST_FOOTER = "We currently have " + TaskList.getNumItems() + " tasks"; + public static final String SEARCH_NO_RESULT = "We don't have that in the list!"; + public static void invalidCommand() { System.out.println("I didn't get that!"); } @@ -15,9 +19,8 @@ public static void showList() { if (TaskList.getNumItems() == 0) { System.out.println("We are free! Let's go play!"); } else { - System.out.println("Here's what we've gotta do:"); - TaskList.viewList(); - System.out.println("We currently have " + TaskList.getNumItems() + " tasks"); + //TODO: CHANGE HEADER INTO FORMATTING FUCKERY + Ui.printList(TaskList.getList(),SHOWLIST_HEADER,SHOWLIST_FOOTER); } } @@ -36,7 +39,6 @@ public static void markTask(String userInput, int statusType) { } else { System.out.println("Item does not exist!"); } - } public static void deleteTask(String userInput) { @@ -76,4 +78,25 @@ public static void addEventTask(String userInput) { Event newTask = new Event(itemDescription,startDate,endDate); TaskList.addItem(newTask); } + public static boolean containsSearchTerm(String searchTerm, String taskDescription) { + return taskDescription.contains(searchTerm); + } + public static void searchTask(String userInput) { + String searchTerm = Ui.getItemDescription(userInput); + ArrayList resultsList = new ArrayList<>(); + int searchResults = 0; + for (int i = 1; i <= TaskList.getNumItems(); ++i) { + String taskDescription = TaskList.getItem(i-1).getDescription(); + if (containsSearchTerm(searchTerm, taskDescription)) { + resultsList.add(TaskList.getItem(i-1)); + searchResults += 1; + } + } + if (searchResults == 0) { + System.out.println(SEARCH_NO_RESULT); + } else { + //TODO: CHANGE ME INTO FORMATTING FUCKERY + Ui.printList(resultsList, "We have " + searchResults + " results!", ""); + } + } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 491a7be2d..6e8101305 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,8 +2,6 @@ public class Duke { static final int COMMAND_INDEX = 0; - static final int MAX_COMMAND_LENGTH = 1; - public static final int STATUSTYPE_DONE = 1; public static final int STATUSTYPE_NOTDONE = 0; @@ -13,7 +11,7 @@ public static void main(String[] args) { while (true) { String userInput = in.nextLine().trim(); String inputCommand = userInput.split(" ", 2)[COMMAND_INDEX]; - + inputCommand = inputCommand.toLowerCase(); switch (inputCommand) { case "exit": case "bye": @@ -48,6 +46,9 @@ public static void main(String[] args) { case "event": Commands.addEventTask(userInput); break; + case "search": + Commands.searchTask(userInput); + break; default: Commands.invalidCommand(); break; diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index de54cb642..e69d7e924 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -4,6 +4,7 @@ public class Storage { public static final String TASKLIST_EXPORT_PATH = "TaskList.txt"; + public static final String WRITTEN_TO_PATH = "Written to: " + TASKLIST_EXPORT_PATH; public static final String SUCCESS_EXPORT = "Successfully exported TaskList!"; public static final String EXPORT_ERROR_PREFIX = "Error occurred while writing to "; public static void writeToTaskList() { @@ -17,7 +18,7 @@ public static void writeToTaskList() { } writeTaskList.close(); System.out.println(SUCCESS_EXPORT); - System.out.println("Written to: " + TASKLIST_EXPORT_PATH); + System.out.println(WRITTEN_TO_PATH); } catch (IOException e) { System.out.println(EXPORT_ERROR_PREFIX + exportTaskList.getAbsolutePath()); } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index a20681260..f79a85b39 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -3,6 +3,9 @@ public class TaskList { private static final ArrayList TaskList = new ArrayList<>(10); private static int NumTasks = 0; + public static ArrayList getList() { + return TaskList; + } public static void addItem (Task newTask) { TaskList.add(newTask); NumTasks += 1; diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index ef7996880..fcec32e63 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,3 +1,4 @@ +import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; @@ -24,7 +25,7 @@ public static String getItemDescription(String userInput) { Scanner in = new Scanner(System.in); String description; try { - description = userInput.split(" ", 3)[DESCRIPTION_INDEX]; + description = userInput.split(" ", 2)[DESCRIPTION_INDEX]; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("What are you referring to?"); description = in.nextLine().trim(); @@ -87,4 +88,17 @@ public static String[] getStartEndDates(String userInput) { } return StartEndDates; } + + public static void printList(ArrayList itemList, String header, String footer) { + if (!header.isBlank()) { + System.out.println(header); + } + for (int i = 0 ; i < itemList.size(); ++i) { + System.out.print((i+1)+ ". "); + System.out.println(itemList.get(i).getTask()); + } + if (!footer.isBlank()) { + System.out.println(footer); + } + } } From 4984f8c98f2553966d78a8d13f55360da90ae5a8 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 3 Mar 2023 15:57:21 +0800 Subject: [PATCH 16/23] added parser class --- src/main/java/Commands.java | 13 ++++-- src/main/java/Duke.java | 51 ++++----------------- src/main/java/Storage.java | 14 ++++-- src/main/java/TaskList.java | 2 + src/main/java/Ui.java | 11 ++--- src/main/java/{ => tasktypes}/Deadline.java | 2 + src/main/java/{ => tasktypes}/Event.java | 2 + src/main/java/{ => tasktypes}/Task.java | 2 + src/main/java/{ => tasktypes}/Todo.java | 2 + 9 files changed, 42 insertions(+), 57 deletions(-) rename src/main/java/{ => tasktypes}/Deadline.java (96%) rename src/main/java/{ => tasktypes}/Event.java (97%) rename src/main/java/{ => tasktypes}/Task.java (97%) rename src/main/java/{ => tasktypes}/Todo.java (90%) diff --git a/src/main/java/Commands.java b/src/main/java/Commands.java index 896db5f88..3f6752b9f 100644 --- a/src/main/java/Commands.java +++ b/src/main/java/Commands.java @@ -1,3 +1,6 @@ +import tasktypes.Todo; +import tasktypes.*; + import java.util.ArrayList; public class Commands { @@ -9,7 +12,7 @@ public class Commands { static final int STARTDATE_INDEX = 0; static final int ENDDATE_INDEX = 1; public static final String SHOWLIST_HEADER = "Here's what we've gotta do:"; - public static final String SHOWLIST_FOOTER = "We currently have " + TaskList.getNumItems() + " tasks"; + public static final String SEARCH_NO_RESULT = "We don't have that in the list!"; public static void invalidCommand() { @@ -19,8 +22,8 @@ public static void showList() { if (TaskList.getNumItems() == 0) { System.out.println("We are free! Let's go play!"); } else { - //TODO: CHANGE HEADER INTO FORMATTING FUCKERY - Ui.printList(TaskList.getList(),SHOWLIST_HEADER,SHOWLIST_FOOTER); + String showList_footer = "We currently have " + TaskList.getNumItems() + " tasks"; + Ui.printList(TaskList.getList(),SHOWLIST_HEADER,showList_footer); } } @@ -95,8 +98,8 @@ public static void searchTask(String userInput) { if (searchResults == 0) { System.out.println(SEARCH_NO_RESULT); } else { - //TODO: CHANGE ME INTO FORMATTING FUCKERY - Ui.printList(resultsList, "We have " + searchResults + " results!", ""); + String resultsList_header = "We have " + searchResults + " results!"; + Ui.printList(resultsList, resultsList_header, ""); } } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 6e8101305..1ffe2d632 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,56 +1,25 @@ import java.util.Scanner; public class Duke { - static final int COMMAND_INDEX = 0; - public static final int STATUSTYPE_DONE = 1; - public static final int STATUSTYPE_NOTDONE = 0; + public static final String EXIT_CMD = "exit"; + public static final String BYE_CMD = "bye"; public static void main(String[] args) { Ui.welcomeMessage(); Scanner in = new Scanner(System.in); - while (true) { + + boolean isExit = false; + while (!isExit) { String userInput = in.nextLine().trim(); - String inputCommand = userInput.split(" ", 2)[COMMAND_INDEX]; - inputCommand = inputCommand.toLowerCase(); + String inputCommand = Parser.ParseInputCommand(userInput); switch (inputCommand) { - case "exit": - case "bye": + case EXIT_CMD: + case BYE_CMD: + isExit = true; Ui.exitMessage(); - return; - case "list": - Commands.showList(); - break; - case "mark": - Commands.markTask(userInput, STATUSTYPE_DONE); - break; - case "unmark": { - Commands.markTask(userInput, STATUSTYPE_NOTDONE); - break; - } - case "delete": { - Commands.deleteTask(userInput); - break; - } - case "save": { - Storage.writeToTaskList(); - break; - } - case "todo": { - Commands.addTodoTask(userInput); - break; - } - case "deadline": { - Commands.addDeadlineTask(userInput); - break; - } - case "event": - Commands.addEventTask(userInput); - break; - case "search": - Commands.searchTask(userInput); break; default: - Commands.invalidCommand(); + Parser.ParseCommand(inputCommand,userInput); break; } } diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index e69d7e924..034562477 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -3,22 +3,26 @@ import java.io.IOException; public class Storage { - public static final String TASKLIST_EXPORT_PATH = "TaskList.txt"; - public static final String WRITTEN_TO_PATH = "Written to: " + TASKLIST_EXPORT_PATH; + public static final String TASKLIST_EXPORT_PATH = "data\\TaskList.txt"; + public static final String WRITTEN_TO_PATH_PREFIX = "Written to: "; public static final String SUCCESS_EXPORT = "Successfully exported TaskList!"; public static final String EXPORT_ERROR_PREFIX = "Error occurred while writing to "; + public static File createFile() { + return new File (TASKLIST_EXPORT_PATH); + } public static void writeToTaskList() { - File exportTaskList = new File (TASKLIST_EXPORT_PATH); + File exportTaskList = createFile(); try { FileWriter writeTaskList = new FileWriter(TASKLIST_EXPORT_PATH); + int numTasks = TaskList.getNumItems(); for (int i = 0; i < numTasks; ++i) { writeTaskList.write(TaskList.getItem(i).getTask()); - writeTaskList.write(System.lineSeparator()); + writeTaskList.write(System.lineSeparator()); } writeTaskList.close(); System.out.println(SUCCESS_EXPORT); - System.out.println(WRITTEN_TO_PATH); + System.out.println(WRITTEN_TO_PATH_PREFIX + exportTaskList.getCanonicalPath()); } catch (IOException e) { System.out.println(EXPORT_ERROR_PREFIX + exportTaskList.getAbsolutePath()); } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index f79a85b39..d14a9752d 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,3 +1,5 @@ +import tasktypes.Task; + import java.util.ArrayList; public class TaskList { diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index fcec32e63..b01f3832e 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,3 +1,5 @@ +import tasktypes.Task; + import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; @@ -9,18 +11,16 @@ public class Ui { public static final String STARTDATE_USERINPUT_PREFIX = "/from"; public static final String ENDDATE_USERINPUT_PREFIX = "/to"; public static final String DEADLINE_USERINPUT_PREFIX = "/by"; - static final int STARTDATE_INDEX = 0; static final int ENDDATE_INDEX = 1; private static final String WELCOME_MESSAGE = "Hi it's Anna!\nWhat do you need to do?"; - + public static void exitMessage() { + System.out.println(EXIT_MESSAGE); + } public static void welcomeMessage() { System.out.println(WELCOME_MESSAGE); } - public static void exitMessage() { - System.out.println(EXIT_MESSAGE); - } public static String getItemDescription(String userInput) { Scanner in = new Scanner(System.in); String description; @@ -48,7 +48,6 @@ public static int getItemNumber(String userInput) { itemNumber = getItemNumber(userInput); } } catch (NumberFormatException n) { - //Search for the item and spit out the index? itemNumber = getItemNumber(userInput); } return itemNumber; diff --git a/src/main/java/Deadline.java b/src/main/java/tasktypes/Deadline.java similarity index 96% rename from src/main/java/Deadline.java rename to src/main/java/tasktypes/Deadline.java index 4422a1008..869c11856 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/tasktypes/Deadline.java @@ -1,3 +1,5 @@ +package tasktypes; + public class Deadline extends Task { protected String dueDate; diff --git a/src/main/java/Event.java b/src/main/java/tasktypes/Event.java similarity index 97% rename from src/main/java/Event.java rename to src/main/java/tasktypes/Event.java index f36363e0e..96df40da8 100644 --- a/src/main/java/Event.java +++ b/src/main/java/tasktypes/Event.java @@ -1,3 +1,5 @@ +package tasktypes; + public class Event extends Task{ String startDate, endDate; public Event(String description, String startDate, String endDate) { diff --git a/src/main/java/Task.java b/src/main/java/tasktypes/Task.java similarity index 97% rename from src/main/java/Task.java rename to src/main/java/tasktypes/Task.java index 238b7f671..3f88475a3 100644 --- a/src/main/java/Task.java +++ b/src/main/java/tasktypes/Task.java @@ -1,3 +1,5 @@ +package tasktypes; + public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/Todo.java b/src/main/java/tasktypes/Todo.java similarity index 90% rename from src/main/java/Todo.java rename to src/main/java/tasktypes/Todo.java index 935127dd2..eb4289dae 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/tasktypes/Todo.java @@ -1,3 +1,5 @@ +package tasktypes; + public class Todo extends Task { public Todo(String description) { super(description); From 39fb97b3abade5edcdf62d95f65edec3c31ff41a Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 3 Mar 2023 18:10:14 +0800 Subject: [PATCH 17/23] added load from text file functionality --- src/main/java/Duke.java | 3 +- src/main/java/Storage.java | 61 +++++++++++++++++++++++++-- src/main/java/Ui.java | 55 +++++++++++++++++++----- src/main/java/tasktypes/Deadline.java | 5 ++- src/main/java/tasktypes/Event.java | 3 +- src/main/java/tasktypes/Task.java | 2 +- src/main/java/tasktypes/Todo.java | 5 ++- 7 files changed, 113 insertions(+), 21 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 1ffe2d632..96c25f632 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -3,9 +3,8 @@ public class Duke { public static final String EXIT_CMD = "exit"; public static final String BYE_CMD = "bye"; - public static void main(String[] args) { - Ui.welcomeMessage(); + Ui.welcome(); Scanner in = new Scanner(System.in); boolean isExit = false; diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index 034562477..e785aee53 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -1,24 +1,79 @@ +import tasktypes.Deadline; +import tasktypes.Event; +import tasktypes.Todo; + import java.io.File; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; +import java.util.Scanner; public class Storage { public static final String TASKLIST_EXPORT_PATH = "data\\TaskList.txt"; public static final String WRITTEN_TO_PATH_PREFIX = "Written to: "; public static final String SUCCESS_EXPORT = "Successfully exported TaskList!"; public static final String EXPORT_ERROR_PREFIX = "Error occurred while writing to "; + public static final String TASKTYPE_READ_ERROR = "Error reading TaskType"; + public static final int DESCRIPTION_INDEX = 9; + public static final int DEADLINE_INDEX = 9; + public static final int STARTDATE_INDEX = 6; + public static final int ENDDATE_INDEX = 4; + public static File createFile() { - return new File (TASKLIST_EXPORT_PATH); + return new File(TASKLIST_EXPORT_PATH); } + + public static void readFromExistingData() { + File existingData = new File(TASKLIST_EXPORT_PATH); + try { + Scanner s = new Scanner(existingData); + + String itemDescription; + while (s.hasNext()) { + String newLine = s.nextLine(); + String TaskType = String.valueOf(newLine.charAt(3)); + + switch (TaskType) { + case (Todo.TYPE_ICON): + itemDescription = newLine.substring(DESCRIPTION_INDEX); + System.out.println("Description is: " + itemDescription); + Commands.addTodoTask("T " + itemDescription); + break; + case (Deadline.TYPE_ICON): + itemDescription = newLine.substring(DESCRIPTION_INDEX); + String deadline = s.nextLine().trim(); + deadline = deadline.substring(DEADLINE_INDEX); + itemDescription = Parser.ParseDeadlineInput(itemDescription, deadline); + Commands.addDeadlineTask(itemDescription); + break; + case (Event.TYPE_ICON): + itemDescription = newLine.substring(DESCRIPTION_INDEX); + String startDate = s.nextLine().trim(); + startDate = startDate.substring(STARTDATE_INDEX); + String endDate = s.nextLine().trim(); + endDate = endDate.substring(ENDDATE_INDEX); + itemDescription = Parser.ParseEventInput("E " + itemDescription,startDate,endDate); + Commands.addEventTask(itemDescription); + break; + default: + System.out.println(TASKTYPE_READ_ERROR); + return; + } + } + } catch (FileNotFoundException f) { + System.out.println("Existing data not found!"); + } + } + public static void writeToTaskList() { File exportTaskList = createFile(); try { FileWriter writeTaskList = new FileWriter(TASKLIST_EXPORT_PATH); - int numTasks = TaskList.getNumItems(); for (int i = 0; i < numTasks; ++i) { + writeTaskList.write(i + 1 + "."); writeTaskList.write(TaskList.getItem(i).getTask()); - writeTaskList.write(System.lineSeparator()); + writeTaskList.write(System.lineSeparator()); } writeTaskList.close(); System.out.println(SUCCESS_EXPORT); diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index b01f3832e..4189dec76 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -5,7 +5,6 @@ import java.util.Scanner; public class Ui { - public static final String EXIT_MESSAGE = "Go away Anna\nO-kay bye......"; static final int DESCRIPTION_INDEX = 1; public static final String STARTDATE_USERINPUT_PREFIX = "/from"; @@ -17,8 +16,9 @@ public class Ui { public static void exitMessage() { System.out.println(EXIT_MESSAGE); } - public static void welcomeMessage() { + public static void welcome() { System.out.println(WELCOME_MESSAGE); + System.out.println("You may load existing data using the load command"); } public static String getItemDescription(String userInput) { @@ -39,6 +39,7 @@ public static int getItemNumber(String userInput) { try { itemNumber = Integer.parseInt(userInput.split(" ", 2)[1]); } catch (ArrayIndexOutOfBoundsException e) { + //TODO: change to updated UI code TaskList.viewList(); System.out.println("What is the number of the item in the list?"); try { @@ -68,23 +69,55 @@ public static String getDueDate(String userInput) { } return dueDate; } - public static String[] getStartEndDates(String userInput) { - Scanner in = new Scanner(System.in); - String[] StartEndDates = new String[2]; + public static int getStartMarkerIndex(String userInput) { if (userInput.contains(STARTDATE_USERINPUT_PREFIX)) { - StartEndDates[STARTDATE_INDEX] = userInput.substring(userInput.indexOf(STARTDATE_USERINPUT_PREFIX),userInput.indexOf(ENDDATE_USERINPUT_PREFIX)).trim(); + return userInput.indexOf(STARTDATE_USERINPUT_PREFIX); } else { - System.out.println("When does this event start?"); - StartEndDates[STARTDATE_INDEX] = in.nextLine().trim(); + return -99; } - + } + public static int getEndMarkerIndex(String userInput) { if (userInput.contains(ENDDATE_USERINPUT_PREFIX)) { - StartEndDates[ENDDATE_INDEX] = userInput.substring(userInput.indexOf(ENDDATE_USERINPUT_PREFIX)).trim(); + return userInput.indexOf(ENDDATE_USERINPUT_PREFIX); } else { + return -99; + } + } + public static String getStartDate(String userInput) { + Scanner in = new Scanner (System.in); + int startMarkerIndex = getStartMarkerIndex(userInput); + int endMarkerIndex = getEndMarkerIndex(userInput); + if (startMarkerIndex == -99) { + System.out.println("When does this event start?"); + return in.nextLine().trim(); + } else { + if (endMarkerIndex == -99) { + return userInput.substring(startMarkerIndex + 1); + } else { + return userInput.substring(startMarkerIndex + 1,endMarkerIndex); + } + } + } + public static String getEndDate(String userInput) { + Scanner in = new Scanner (System.in); + int startMarkerIndex = getStartMarkerIndex(userInput); + int endMarkerIndex = getEndMarkerIndex(userInput); + if (endMarkerIndex == -99) { System.out.println("When does this event end?"); - StartEndDates[ENDDATE_INDEX] = in.nextLine().trim(); + return in.nextLine().trim(); + } else { + if (startMarkerIndex > endMarkerIndex) { + return userInput.substring(endMarkerIndex + 1,startMarkerIndex); + } else { + return userInput.substring(endMarkerIndex + 1); + } } + } + public static String[] getStartEndDates(String userInput) { + String[] StartEndDates = new String[2]; + StartEndDates[STARTDATE_INDEX] = getStartDate(userInput); + StartEndDates[ENDDATE_INDEX] = getEndDate(userInput); return StartEndDates; } diff --git a/src/main/java/tasktypes/Deadline.java b/src/main/java/tasktypes/Deadline.java index 869c11856..0333d6897 100644 --- a/src/main/java/tasktypes/Deadline.java +++ b/src/main/java/tasktypes/Deadline.java @@ -1,6 +1,7 @@ package tasktypes; public class Deadline extends Task { + public static final String TYPE_ICON = "D"; protected String dueDate; public Deadline(String description, String dueDate) { @@ -9,7 +10,7 @@ public Deadline(String description, String dueDate) { } @Override public String getTypeIcon() { - return "D"; + return TYPE_ICON; } public String getDueDate() { @@ -18,7 +19,7 @@ public String getDueDate() { @Override public String getTask() { - return taskTypeIcon() + isDoneIcon() + getDescription() + return taskTypeIcon() + isDoneIcon() + " " + getDescription() + System.lineSeparator() + " Deadline: " + getDueDate(); } } diff --git a/src/main/java/tasktypes/Event.java b/src/main/java/tasktypes/Event.java index 96df40da8..fa32c3004 100644 --- a/src/main/java/tasktypes/Event.java +++ b/src/main/java/tasktypes/Event.java @@ -1,6 +1,7 @@ package tasktypes; public class Event extends Task{ + public static final String TYPE_ICON = "E"; String startDate, endDate; public Event(String description, String startDate, String endDate) { super(description); @@ -9,7 +10,7 @@ public Event(String description, String startDate, String endDate) { } @Override public String getTypeIcon() { - return "E"; + return TYPE_ICON; } public String getStartDate() { diff --git a/src/main/java/tasktypes/Task.java b/src/main/java/tasktypes/Task.java index 3f88475a3..ca9449328 100644 --- a/src/main/java/tasktypes/Task.java +++ b/src/main/java/tasktypes/Task.java @@ -3,7 +3,7 @@ public class Task { protected String description; protected boolean isDone; - + protected int listIndex; public Task(String description) { //ok to leave as public? this.description = description; this.isDone = false; diff --git a/src/main/java/tasktypes/Todo.java b/src/main/java/tasktypes/Todo.java index eb4289dae..0869172a1 100644 --- a/src/main/java/tasktypes/Todo.java +++ b/src/main/java/tasktypes/Todo.java @@ -1,12 +1,15 @@ package tasktypes; public class Todo extends Task { + + public static final String TYPE_ICON = "T"; + public Todo(String description) { super(description); } @Override public String getTypeIcon() { - return "T"; + return TYPE_ICON; } } From 30d3513598a344d3b3e4239a9b96d7e183c85ad8 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 3 Mar 2023 20:23:30 +0800 Subject: [PATCH 18/23] Added javadoc documentation --- src/main/java/Commands.java | 103 ++++++++++++++++++------- src/main/java/Storage.java | 36 ++++++--- src/main/java/TaskList.java | 48 ++++++++++-- src/main/java/Ui.java | 105 ++++++++++++++++++++++---- src/main/java/tasktypes/Deadline.java | 3 +- src/main/java/tasktypes/Event.java | 1 + src/main/java/tasktypes/Task.java | 5 ++ 7 files changed, 240 insertions(+), 61 deletions(-) diff --git a/src/main/java/Commands.java b/src/main/java/Commands.java index 3f6752b9f..71266f32c 100644 --- a/src/main/java/Commands.java +++ b/src/main/java/Commands.java @@ -4,67 +4,98 @@ import java.util.ArrayList; public class Commands { + + public static final int DEADLINE_PARAM_INDEX = 1; static final String TASK_NO_EXIST = "Task does not exist!"; static final String DELETE_TASK_MESSAGE = "Okay! I've deleted task: "; public static final String DEADLINE_USERINPUT_PREFIX = "/by"; - public static final int STATUSTYPE_DONE = 1; - public static final int STATUSTYPE_NOTDONE = 0; + public static final boolean STATUSTYPE_DONE = true; static final int STARTDATE_INDEX = 0; static final int ENDDATE_INDEX = 1; public static final String SHOWLIST_HEADER = "Here's what we've gotta do:"; - public static final String SEARCH_NO_RESULT = "We don't have that in the list!"; - + public static final String INVALID_COMMAND_MESSAGE = "I didn't get that!"; + public static final String EMPTY_TASKLIST_MESSAGE = "We are free! Let's go play!"; + public static final int USERINPUT_DUEDATE_INDEX = 1; + public static final int USERINPUT_DESCRIPTION_INDEX = 0; + /** + * Prints out a message stating that the input command is not a valid command + */ public static void invalidCommand() { - System.out.println("I didn't get that!"); + System.out.println(INVALID_COMMAND_MESSAGE); } + + /** + * Shows the current tasklist contents + * Prints out a unique message if the tasklist is empty + */ public static void showList() { - if (TaskList.getNumItems() == 0) { - System.out.println("We are free! Let's go play!"); + if (TaskList.getNumTasks() == USERINPUT_DESCRIPTION_INDEX) { + System.out.println(EMPTY_TASKLIST_MESSAGE); } else { - String showList_footer = "We currently have " + TaskList.getNumItems() + " tasks"; + String showList_footer = "We currently have " + TaskList.getNumTasks() + " tasks"; Ui.printList(TaskList.getList(),SHOWLIST_HEADER,showList_footer); } } - public static void markTask(String userInput, int statusType) { + /** + * Marks a given task in the tasklist as done or not done depending on parameter statusType + * + * @param userInput the input from the user + * @param statusType input true for done, false for notDone + */ + public static void markTask(String userInput, boolean statusType) { int itemNum = Ui.getItemNumber(userInput); - if (itemNum > 0 && itemNum <= TaskList.getNumItems()) { - if (statusType == 1) { - TaskList.markDone(itemNum - 1); + if (itemNum > USERINPUT_DESCRIPTION_INDEX && itemNum <= TaskList.getNumTasks()) { + if (statusType == STATUSTYPE_DONE) { + TaskList.markDone(itemNum - USERINPUT_DUEDATE_INDEX); System.out.println("Okay I've marked item " + itemNum + " as done:"); - TaskList.printItem(itemNum - 1); + TaskList.printItem(itemNum - USERINPUT_DUEDATE_INDEX); } else { - TaskList.markNotDone(itemNum - 1); + TaskList.markNotDone(itemNum - USERINPUT_DUEDATE_INDEX); System.out.println("Oh we aren't done with item " + itemNum + "?"); - TaskList.printItem(itemNum - 1); + TaskList.printItem(itemNum - USERINPUT_DUEDATE_INDEX); } } else { - System.out.println("Item does not exist!"); + System.out.println(TASK_NO_EXIST); } } + /** + * Removes a given task from the tasklist based on the input message + * @param userInput the input from the user + */ public static void deleteTask(String userInput) { int itemIndex = Ui.getItemIndex(userInput); - if (itemIndex <= TaskList.getNumItems() - 1) { + if (itemIndex <= TaskList.getNumTasks() - USERINPUT_DUEDATE_INDEX) { TaskList.deleteTask(itemIndex); - System.out.println(DELETE_TASK_MESSAGE + (itemIndex+1)); + System.out.println(DELETE_TASK_MESSAGE + (itemIndex+ USERINPUT_DUEDATE_INDEX)); } else { System.out.println(TASK_NO_EXIST); } } + /** + * takes in user's description and adds a Todo task to the tasklist + * @param userInput description of the todo + */ public static void addTodoTask(String userInput) { String itemDescription = Ui.getItemDescription(userInput); Todo newTask = new Todo(itemDescription); TaskList.addItem(newTask); } + /** + * takes in user's description and adds a deadline task to the tasklist + * if the user has not supplied a deadline, method will request for the deadline + * @param userInput description of the deadline task + */ public static void addDeadlineTask(String userInput) { String itemDescription, dueDate; if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { - itemDescription = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[STATUSTYPE_NOTDONE]; - dueDate = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[STATUSTYPE_DONE]; + itemDescription = userInput.split(" ", 2)[DEADLINE_PARAM_INDEX]; + itemDescription = itemDescription.split(DEADLINE_USERINPUT_PREFIX,2)[USERINPUT_DESCRIPTION_INDEX]; + dueDate = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[USERINPUT_DUEDATE_INDEX]; } else { itemDescription = Ui.getItemDescription(userInput); dueDate = Ui.getDueDate(userInput); @@ -72,7 +103,11 @@ public static void addDeadlineTask(String userInput) { Deadline newTask = new Deadline(itemDescription,dueDate); TaskList.addItem(newTask); } - + /** + * takes in user's input and adds an event task to the tasklist + * if the user has not supplied start and/or end dates, method will request for the dates + * @param userInput description of the event task + */ public static void addEventTask(String userInput) { String itemDescription = Ui.getItemDescription(userInput); String[] StartEndDates = Ui.getStartEndDates(userInput); @@ -81,21 +116,35 @@ public static void addEventTask(String userInput) { Event newTask = new Event(itemDescription,startDate,endDate); TaskList.addItem(newTask); } + + /** + * Checks if searchTerm can be found inside taskDescription. + * @param searchTerm String to be found in the taskDescription + * @param taskDescription String where searchTerm is to be found in + * @return true if taskDescription contains searchTerm + */ public static boolean containsSearchTerm(String searchTerm, String taskDescription) { return taskDescription.contains(searchTerm); } + + /** + * Searches the current tasklist for a user-supplied String + * Will print out the list of items that match the user input if exists. + * Else will print out a no items found message + * @param userInput the String to be searched for + */ public static void searchTask(String userInput) { String searchTerm = Ui.getItemDescription(userInput); ArrayList resultsList = new ArrayList<>(); - int searchResults = 0; - for (int i = 1; i <= TaskList.getNumItems(); ++i) { - String taskDescription = TaskList.getItem(i-1).getDescription(); + int searchResults = USERINPUT_DESCRIPTION_INDEX; + for (int i = USERINPUT_DUEDATE_INDEX; i <= TaskList.getNumTasks(); ++i) { + String taskDescription = TaskList.getItem(i- USERINPUT_DUEDATE_INDEX).getDescription(); if (containsSearchTerm(searchTerm, taskDescription)) { - resultsList.add(TaskList.getItem(i-1)); - searchResults += 1; + resultsList.add(TaskList.getItem(i- USERINPUT_DUEDATE_INDEX)); + searchResults += USERINPUT_DUEDATE_INDEX; } } - if (searchResults == 0) { + if (searchResults == USERINPUT_DESCRIPTION_INDEX) { System.out.println(SEARCH_NO_RESULT); } else { String resultsList_header = "We have " + searchResults + " results!"; diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index e785aee53..1e2c6daa5 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -9,7 +9,7 @@ import java.util.Scanner; public class Storage { - public static final String TASKLIST_EXPORT_PATH = "data\\TaskList.txt"; + public static final String TASKLIST_EXPORT_PATH = "TaskList.txt"; public static final String WRITTEN_TO_PATH_PREFIX = "Written to: "; public static final String SUCCESS_EXPORT = "Successfully exported TaskList!"; public static final String EXPORT_ERROR_PREFIX = "Error occurred while writing to "; @@ -18,16 +18,24 @@ public class Storage { public static final int DEADLINE_INDEX = 9; public static final int STARTDATE_INDEX = 6; public static final int ENDDATE_INDEX = 4; + public static final String EXISTING_DATA_NOT_FOUND = "Existing data not found!"; + /** + * creates a file in a specified export path + * @return a File object pertaining to the created file + */ public static File createFile() { return new File(TASKLIST_EXPORT_PATH); } + /** + * finds an existing tasklist.txt file and reads from it to populate the current instance's tasklist + * returns an error message if the existing file does not exist + */ public static void readFromExistingData() { File existingData = new File(TASKLIST_EXPORT_PATH); try { Scanner s = new Scanner(existingData); - String itemDescription; while (s.hasNext()) { String newLine = s.nextLine(); @@ -36,15 +44,15 @@ public static void readFromExistingData() { switch (TaskType) { case (Todo.TYPE_ICON): itemDescription = newLine.substring(DESCRIPTION_INDEX); - System.out.println("Description is: " + itemDescription); - Commands.addTodoTask("T " + itemDescription); + Todo task = new Todo(itemDescription); + TaskList.addItem(task); break; case (Deadline.TYPE_ICON): itemDescription = newLine.substring(DESCRIPTION_INDEX); - String deadline = s.nextLine().trim(); - deadline = deadline.substring(DEADLINE_INDEX); - itemDescription = Parser.ParseDeadlineInput(itemDescription, deadline); - Commands.addDeadlineTask(itemDescription); + String dueDate = s.nextLine().trim(); + dueDate = dueDate.substring(DEADLINE_INDEX); + Deadline deadline = new Deadline(itemDescription, dueDate); + TaskList.addItem(deadline); break; case (Event.TYPE_ICON): itemDescription = newLine.substring(DESCRIPTION_INDEX); @@ -52,8 +60,8 @@ public static void readFromExistingData() { startDate = startDate.substring(STARTDATE_INDEX); String endDate = s.nextLine().trim(); endDate = endDate.substring(ENDDATE_INDEX); - itemDescription = Parser.ParseEventInput("E " + itemDescription,startDate,endDate); - Commands.addEventTask(itemDescription); + Event event = new Event(itemDescription,startDate, endDate); + TaskList.addItem(event); break; default: System.out.println(TASKTYPE_READ_ERROR); @@ -61,15 +69,19 @@ public static void readFromExistingData() { } } } catch (FileNotFoundException f) { - System.out.println("Existing data not found!"); + System.out.println(EXISTING_DATA_NOT_FOUND); } } + /** + * exports the current tasklist into a text file at the specified export path + * + */ public static void writeToTaskList() { File exportTaskList = createFile(); try { FileWriter writeTaskList = new FileWriter(TASKLIST_EXPORT_PATH); - int numTasks = TaskList.getNumItems(); + int numTasks = TaskList.getNumTasks(); for (int i = 0; i < numTasks; ++i) { writeTaskList.write(i + 1 + "."); writeTaskList.write(TaskList.getItem(i).getTask()); diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index d14a9752d..35b11e6e3 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -5,41 +5,75 @@ public class TaskList { private static final ArrayList TaskList = new ArrayList<>(10); private static int NumTasks = 0; + + /** + * Returns the current tasklist + * @return TaskList currently stored in the method + */ public static ArrayList getList() { return TaskList; } + + /** + * Adds a Task to the tasklist, increments the number of tasks in the list then echos the task added + * @param newTask Task to be added to the list + */ public static void addItem (Task newTask) { TaskList.add(newTask); NumTasks += 1; System.out.println("Okay! I've added: [" + newTask.getTypeIcon() +"] " + newTask.getDescription()); } - public static int getNumItems() { + + /** + * gets the number of tasks in the list + * @return the number of tasks in the list currently + */ + public static int getNumTasks() { return NumTasks; } - public static void viewList () { - for (int i = 0; i < TaskList.size(); ++i) { - System.out.print(i+1 + ". "); - System.out.println(TaskList.get(i).getTask()); - } - } + + /** + * marks a task's status as done + * @param index index of the task in the tasklist + */ public static void markDone (int index) { if (TaskList.get(index).getStatusIcon().equals(" ")) { TaskList.get(index).markDone(); } } + + /** + * marks a task's status as not done + * @param index index of the task in the tasklist + */ public static void markNotDone (int index) { if (TaskList.get(index).getStatusIcon().equals("X")) { TaskList.get(index).markNotDone(); } } + + /** + * get a task item from the tasklist + * @param index index of the task in the tasklist + * @return returns a Task object from the tasklist according to the index + */ public static Task getItem (int index) { return TaskList.get(index); } + + /** + * prints out a task in a pre-set format + * @param index index of the task in the tasklist + */ public static void printItem (int index) { System.out.print(index+1 + ". "); System.out.println(TaskList.get(index).getTask()); } + /** + * removes a task from the tasklist and decrements the counter for number of tasks + * @param index index of the task in the tasklist + */ public static void deleteTask(int index) { TaskList.remove(index); NumTasks -= 1; diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 4189dec76..533207d10 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -13,39 +13,69 @@ public class Ui { static final int STARTDATE_INDEX = 0; static final int ENDDATE_INDEX = 1; private static final String WELCOME_MESSAGE = "Hi it's Anna!\nWhat do you need to do?"; + public static final String WELCOME_LOAD_PROMPT = "You may load existing data using the load command"; + public static final String ITEM_NUMBER_PROMPT = "What is the number of the item in the list?"; + public static final String REJECTED_NON_NUMBER_INPUT = "Please enter a number"; + public static final String DUEDATE_PROMPT = "When is this due by?"; + public static final String EVENT_START_PROMPT = "When does this event start?"; + public static final String EVENT_END_PROMPT = "When does this event end?"; + public static final String ITEM_DESCRIPTION_PROMPT = "What are you referring to?"; + public static final int NOT_FOUND_IN_STRING = -99; + + /** + * prints the exit message when shutdown is desired + */ public static void exitMessage() { System.out.println(EXIT_MESSAGE); } + + /** + * prints a welcome message on startup. + * Also prompts user if they would like to load an existing file + */ public static void welcome() { System.out.println(WELCOME_MESSAGE); - System.out.println("You may load existing data using the load command"); + System.out.println(WELCOME_LOAD_PROMPT); } + /** + * gets the description from the user for a new task object + * will prompt the user if additional information is required + * @param userInput input string from the user + * @return returns the description of the task + */ public static String getItemDescription(String userInput) { Scanner in = new Scanner(System.in); String description; try { description = userInput.split(" ", 2)[DESCRIPTION_INDEX]; } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("What are you referring to?"); + System.out.println(ITEM_DESCRIPTION_PROMPT); description = in.nextLine().trim(); } + if (description.contains("/")) { + description = description.split("/",2)[0]; + } return description; } + /** + * gets the item number for the user supplied command to act on + * @param userInput command input from the user + * @return returns number of the item in the tasklist (1-based) + */ public static int getItemNumber(String userInput) { Scanner in = new Scanner(System.in); int itemNumber; try { itemNumber = Integer.parseInt(userInput.split(" ", 2)[1]); } catch (ArrayIndexOutOfBoundsException e) { - //TODO: change to updated UI code - TaskList.viewList(); - System.out.println("What is the number of the item in the list?"); + Ui.printList(TaskList.getList(),"",""); + System.out.println(ITEM_NUMBER_PROMPT); try { itemNumber = in.nextInt(); } catch (InputMismatchException i){ - System.out.println("Please enter a number"); + System.out.println(REJECTED_NON_NUMBER_INPUT); itemNumber = getItemNumber(userInput); } } catch (NumberFormatException n) { @@ -54,57 +84,92 @@ public static int getItemNumber(String userInput) { return itemNumber; } + /** + * gets the item index for the user supplied command to act on + * @param userInput command input from the user + * @return returns index of the item in the tasklist (0-based) + */ public static int getItemIndex(String userInput) { return getItemNumber(userInput) - 1; } + /** + * gets a duedate for the creation of a new deadline object + * will prompt the user if additional information is required + * @param userInput command input from the user + * @return duedate of the deadline object + */ public static String getDueDate(String userInput) { Scanner in = new Scanner(System.in); String dueDate; if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { dueDate = userInput.substring(userInput.indexOf(DEADLINE_USERINPUT_PREFIX)); } else { - System.out.println("When is this due by?"); + System.out.println(DUEDATE_PROMPT); dueDate = in.nextLine().trim(); } return dueDate; } + /** + * gets the index of the startdate marker in the user supplied input + * @param userInput user supplied input + * @return index of the startdate marker, else -99 if not found + */ public static int getStartMarkerIndex(String userInput) { if (userInput.contains(STARTDATE_USERINPUT_PREFIX)) { return userInput.indexOf(STARTDATE_USERINPUT_PREFIX); } else { - return -99; + return NOT_FOUND_IN_STRING; } } + + /** + * gets the index of the enddate marker in the user supplied input + * @param userInput user supplied input + * @return index of the enddate marker, else -99 if not found + */ public static int getEndMarkerIndex(String userInput) { if (userInput.contains(ENDDATE_USERINPUT_PREFIX)) { return userInput.indexOf(ENDDATE_USERINPUT_PREFIX); } else { - return -99; + return NOT_FOUND_IN_STRING; } } + /** + * gets the startdate from the user supplied input + * will prompt the user to enter the startdate if not found + * @param userInput user supplied command input + * @return startdate of the event + */ public static String getStartDate(String userInput) { Scanner in = new Scanner (System.in); int startMarkerIndex = getStartMarkerIndex(userInput); int endMarkerIndex = getEndMarkerIndex(userInput); - if (startMarkerIndex == -99) { - System.out.println("When does this event start?"); + if (startMarkerIndex == NOT_FOUND_IN_STRING) { + System.out.println(EVENT_START_PROMPT); return in.nextLine().trim(); } else { - if (endMarkerIndex == -99) { + if (endMarkerIndex == NOT_FOUND_IN_STRING) { return userInput.substring(startMarkerIndex + 1); } else { return userInput.substring(startMarkerIndex + 1,endMarkerIndex); } } } + + /** + * gets the enddate from the user supplied input + * will prompt the user to enter the enddate if not found + * @param userInput user supplied command input + * @return enddate of the event + */ public static String getEndDate(String userInput) { Scanner in = new Scanner (System.in); int startMarkerIndex = getStartMarkerIndex(userInput); int endMarkerIndex = getEndMarkerIndex(userInput); - if (endMarkerIndex == -99) { - System.out.println("When does this event end?"); + if (endMarkerIndex == NOT_FOUND_IN_STRING) { + System.out.println(EVENT_END_PROMPT); return in.nextLine().trim(); } else { if (startMarkerIndex > endMarkerIndex) { @@ -114,6 +179,12 @@ public static String getEndDate(String userInput) { } } } + /** + * gets the start and end dates for the creation of a new event object + * will prompt the user if additional information is required + * @param userInput command input from the user + * @return a string array containing the start and end dates respectively + */ public static String[] getStartEndDates(String userInput) { String[] StartEndDates = new String[2]; StartEndDates[STARTDATE_INDEX] = getStartDate(userInput); @@ -121,6 +192,12 @@ public static String[] getStartEndDates(String userInput) { return StartEndDates; } + /** + * prints a given arrayList of Tasks with a header and footer if required + * @param itemList arrayList of Task objects + * @param header line to print before the arrayList. input empty string if not required + * @param footer line to print after the arrayList. input empty string if not required + */ public static void printList(ArrayList itemList, String header, String footer) { if (!header.isBlank()) { System.out.println(header); diff --git a/src/main/java/tasktypes/Deadline.java b/src/main/java/tasktypes/Deadline.java index 0333d6897..aa2c798d0 100644 --- a/src/main/java/tasktypes/Deadline.java +++ b/src/main/java/tasktypes/Deadline.java @@ -8,6 +8,7 @@ public Deadline(String description, String dueDate) { super(description); this.dueDate = dueDate; } + @Override public String getTypeIcon() { return TYPE_ICON; @@ -20,6 +21,6 @@ public String getDueDate() { @Override public String getTask() { return taskTypeIcon() + isDoneIcon() + " " + getDescription() - + System.lineSeparator() + " Deadline: " + getDueDate(); + + System.lineSeparator() + " Deadline:" + getDueDate(); } } diff --git a/src/main/java/tasktypes/Event.java b/src/main/java/tasktypes/Event.java index fa32c3004..cee15ef3c 100644 --- a/src/main/java/tasktypes/Event.java +++ b/src/main/java/tasktypes/Event.java @@ -8,6 +8,7 @@ public Event(String description, String startDate, String endDate) { this.startDate = startDate; this.endDate = endDate; } + @Override public String getTypeIcon() { return TYPE_ICON; diff --git a/src/main/java/tasktypes/Task.java b/src/main/java/tasktypes/Task.java index ca9449328..cb1feeea5 100644 --- a/src/main/java/tasktypes/Task.java +++ b/src/main/java/tasktypes/Task.java @@ -4,6 +4,7 @@ public class Task { protected String description; protected boolean isDone; protected int listIndex; + public Task(String description) { //ok to leave as public? this.description = description; this.isDone = false; @@ -12,10 +13,13 @@ public Task(String description) { //ok to leave as public? public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + public String getTypeIcon() { return "NULL"; } + public String taskTypeIcon() { return "[" + getTypeIcon() + "]";} + public void setDescription(String description) { this.description = description; } @@ -29,6 +33,7 @@ public void markNotDone() { } public String isDoneIcon() { return "[" + getStatusIcon() + "]";} + public String getDescription() { return description; } From 86c9546d6c288c67d5f3ee36372e0673d0c94b68 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 3 Mar 2023 20:26:17 +0800 Subject: [PATCH 19/23] added javadoc documentation --- src/main/java/Commands.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Commands.java b/src/main/java/Commands.java index 71266f32c..c0e8867ba 100644 --- a/src/main/java/Commands.java +++ b/src/main/java/Commands.java @@ -63,7 +63,7 @@ public static void markTask(String userInput, boolean statusType) { /** * Removes a given task from the tasklist based on the input message - * @param userInput the input from the user + * @param userInput user's input */ public static void deleteTask(String userInput) { int itemIndex = Ui.getItemIndex(userInput); From 7ba7c8ec36ff2cfd3d45634d1da9fe186557a7a3 Mon Sep 17 00:00:00 2001 From: monihog Date: Fri, 3 Mar 2023 20:49:31 +0800 Subject: [PATCH 20/23] Updated basic user guide --- README.md | 71 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 8715d4d91..4cc867d19 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,47 @@ -# Duke project template - -This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. - -## Setting up in Intellij - -Prerequisites: JDK 11, update Intellij to the most recent version. - -1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) -1. Open the project into Intellij as follows: - 1. Click `Open`. - 1. Select the project directory, and click `OK`. - 1. If there are any further prompts, accept the defaults. -1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
- In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: - ``` - Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| - ``` +# Anna + +Anna is a chatbot that helps you to organise your tasks. It can add, delete and mark done tasks you've listed. It can also help you keep track of deadlines and calender events. + +Anna runs on a simple CLI interface that is lightweight and cross-platform friendly + +## Setting up + +Prerequisites: JDK 11 +1. Download the ip.jar under 'Releases' +2. Extract the file and run + +## Commands +To interact with Anna, please use the following commands + +1. `bye` to exit the program +2. `list` to view tasks in the tasklist currently +3. `mark ` sets a given task as done :D +4. `unmark ` sets a given task as not done D: +5. `todo ` adds a simple task into the tasklist +6. `deadline /by ` adds a task with a deadline into the tasklist +7. `event /from /to ` adds a task with start and end times into the tasklist +8. `delete ` removes a given task from the list + +Anna also supports import/export of your tasklist in the form of a .txt file. This can be helpful if you want to refer to the tasklist in a future date! + +1. `save` exports the current tasklist into a tasklist.txt file +2. `load` imports a previous tasklist into the program's tasklist + +## Usage Example + +#### Input Example 1 +`deadline ip /by tonight` + +#### Expected Output 1 +`Okay! I've added: [D] ip` + +#### Input Example 2 +`list` + +#### Expected Output 2 +``` +Here's what we've gotta do: +1. [D][ ] ip + Deadline: tonight +We currently have 1 tasks +``` \ No newline at end of file From ca442dc7e26091d85150b3ddecb595248530410a Mon Sep 17 00:00:00 2001 From: monihog Date: Thu, 16 Mar 2023 13:04:51 +0800 Subject: [PATCH 21/23] Added UserGuide documentation in docs directory for website --- README.md | 130 +++++++++++++++++++++++++++++++++++++----- docs/README.md | 150 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 251 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 4cc867d19..17aa12446 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,134 @@ # Anna -Anna is a chatbot that helps you to organise your tasks. It can add, delete and mark done tasks you've listed. It can also help you keep track of deadlines and calender events. +Anna is a chatbot that helps you to organise your tasks. It can add, +delete and mark done tasks you've listed. It can also help you keep track of deadlines +and calender events. -Anna runs on a simple CLI interface that is lightweight and cross-platform friendly +Anna runs on a simple CLI interface that is lightweight and cross-platform friendly. +Anna is also designed to be friendly to first time users by being capable of handling missing inputs ## Setting up Prerequisites: JDK 11 1. Download the ip.jar under 'Releases' -2. Extract the file and run +2. Extract the file into a folder you wish to keep it (e.g. `C:\Users\\Desktop\AnnaBot`) +3. Open a terminal (e.g Command Prompt for Windows) and navigate to the previous directory
+4. Run the jar file using command `java -jar ip.jar`
+#### Example (Windows Command Prompt): +``` +C:\Users\user> cd Desktop\AnnaBot +C:\Users\user\Desktop\AnnaBot> java -jar ip.jar +Hi it's Anna! +What do you need to do? +You may load existing data using the load command +``` ## Commands To interact with Anna, please use the following commands -1. `bye` to exit the program -2. `list` to view tasks in the tasklist currently -3. `mark ` sets a given task as done :D -4. `unmark ` sets a given task as not done D: -5. `todo ` adds a simple task into the tasklist -6. `deadline /by ` adds a task with a deadline into the tasklist -7. `event /from /to ` adds a task with start and end times into the tasklist -8. `delete ` removes a given task from the list +### exit the program: `bye` +This command exits the running instance. +``` +bye +Go away Anna +O-kay bye...... +``` +### view the current tasklist: `list` +`list` will show the current tasklist in order of addition (oldest task first) +``` +todo task1 +Okay! I've added: [T] task1 +event task2 /from today /to tomorrow +Okay! I've added: [E] task2 +list +Here's what we've gotta do: +1. [T][ ] task1 +2. [E][ ] task2 + Start: from today + End: to tomorrow +We currently have 2 tasks +``` -Anna also supports import/export of your tasklist in the form of a .txt file. This can be helpful if you want to refer to the tasklist in a future date! +### Mark task as done: `mark ` +Sets a given task as completed. Yay! +``` +mark 1 +Okay I've marked item 1 as done: +1. [T][X] task1 +``` + +### Mark task as not done: `unmark ` +Sets a given task as not done +``` +unmark 1 +Oh we aren't done with item 1? +1. [T][ ] task1 +``` -1. `save` exports the current tasklist into a tasklist.txt file -2. `load` imports a previous tasklist into the program's tasklist +### Create a simple todo task: `todo ` +Creates a standard todo task and adds it into the tasklist +``` +todo task1 +Okay! I've added: [T] task1 +``` +Anna will prompt you if you forget to input the name of the task +``` +todo +What are you referring to? +task1 +Okay! I've added: [T] task1 +``` +### Create a task with a deadline: `deadline /by ` +Creates a task with a deadline and adds it into the tasklist +``` +deadline task2 /by 5 jan +Okay! I've added: [D] task2 +``` +Anna will prompt you for the deadline if it is missing +``` +deadline task2 +When is this due by? +5 jan +Okay! I've added: [D] task2 +``` +### Create a task with start and end dates:
`event /from /to ` +Creates a task with start and end times and adds it into the tasklist +``` +event task3 /from today /to tomorrow +Okay! I've added: [E] task3 +``` +Similarly to `deadline`, Anna will prompt you if you are missing date details +``` +event task3 /to tomorrow +When does this event start? +today +Okay! I've added: [E] task3 +``` +### Remove a task: `delete ` +The given task will be deleted from the tasklist +``` +delete 1 +Okay! I've deleted task: 1 +``` +### Loading and Saving of tasklist +Anna also supports import/export of your tasklist in the form of a .txt file. This can be helpful if you want to refer to the tasklist in a future date! +`save` exports the current tasklist into a tasklist.txt file +``` +save +Successfully exported TaskList! +Written to: C:\Users\lhyao\Desktop\TaskList.txt +``` +`load` imports a previous tasklist into the program's tasklist
+Anna looks for a `TaskList.txt` file in the same directory to import from +``` +load +Okay! I've added: [D] task2 +Okay! I've added: [D] task2 +Okay! I've added: [E] task3 +Okay! I've added: [E] task3 +Okay! I've added: [T] task1 +``` ## Usage Example #### Input Example 1 diff --git a/docs/README.md b/docs/README.md index 8077118eb..2bf0ce7b7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,149 @@ -# User Guide +# Anna -## Features +Anna is a chatbot that helps you to organise your tasks. It can add, +delete and mark done tasks you've listed. It can also help you keep track of deadlines +and calender events. -### Feature-ABC +Anna runs on a simple CLI interface that is lightweight and cross-platform friendly. +Anna is also designed to be friendly to first time users by being capable of handling missing inputs -Description of the feature. +## Setting up -### Feature-XYZ +Prerequisites: JDK 11 +1. Download the ip.jar under 'Releases' +2. Extract the file into a folder you wish to keep it (e.g. `C:\Users\\Desktop\AnnaBot`) +3. Open a terminal (e.g Command Prompt for Windows) and navigate to the previous directory
+4. Run the jar file using command `java -jar ip.jar`
+#### Example (Windows Command Prompt): +``` +C:\Users\user> cd Desktop\AnnaBot +C:\Users\user\Desktop\AnnaBot> java -jar ip.jar +Hi it's Anna! +What do you need to do? +You may load existing data using the load command +``` -Description of the feature. +## Commands +To interact with Anna, please use the following commands -## Usage +### exit the program: `bye` +This command exits the running instance. +``` +bye +Go away Anna +O-kay bye...... +``` +### view the current tasklist: `list` +`list` will show the current tasklist in order of addition (oldest task first) +``` +todo task1 +Okay! I've added: [T] task1 +event task2 /from today /to tomorrow +Okay! I've added: [E] task2 +list +Here's what we've gotta do: +1. [T][ ] task1 +2. [E][ ] task2 + Start: from today + End: to tomorrow +We currently have 2 tasks +``` -### `Keyword` - Describe action +### Mark task as done: `mark ` +Sets a given task as completed. Yay! +``` +mark 1 +Okay I've marked item 1 as done: +1. [T][X] task1 +``` -Describe the action and its outcome. +### Mark task as not done: `unmark ` +Sets a given task as not done +``` +unmark 1 +Oh we aren't done with item 1? +1. [T][ ] task1 +``` + +### Create a simple todo task: `todo ` +Creates a standard todo task and adds it into the tasklist +``` +todo task1 +Okay! I've added: [T] task1 +``` +Anna will prompt you if you forget to input the name of the task +``` +todo +What are you referring to? +task1 +Okay! I've added: [T] task1 +``` +### Create a task with a deadline: `deadline /by ` +Creates a task with a deadline and adds it into the tasklist +``` +deadline task2 /by 5 jan +Okay! I've added: [D] task2 +``` +Anna will prompt you for the deadline if it is missing +``` +deadline task2 +When is this due by? +5 jan +Okay! I've added: [D] task2 +``` +### Create a task with start and end dates:
`event /from /to ` +Creates a task with start and end times and adds it into the tasklist +``` +event task3 /from today /to tomorrow +Okay! I've added: [E] task3 +``` +Similarly to `deadline`, Anna will prompt you if you are missing date details +``` +event task3 /to tomorrow +When does this event start? +today +Okay! I've added: [E] task3 +``` +### Remove a task: `delete ` +The given task will be deleted from the tasklist +``` +delete 1 +Okay! I've deleted task: 1 +``` +### Loading and Saving of tasklist +Anna also supports import/export of your tasklist in the form of a .txt file. This can be helpful if you want to refer to the tasklist in a future date! -Example of usage: +`save` exports the current tasklist into a tasklist.txt file +``` +save +Successfully exported TaskList! +Written to: C:\Users\lhyao\Desktop\TaskList.txt +``` +`load` imports a previous tasklist into the program's tasklist
+Anna looks for a `TaskList.txt` file in the same directory to import from +``` +load +Okay! I've added: [D] task2 +Okay! I've added: [D] task2 +Okay! I've added: [E] task3 +Okay! I've added: [E] task3 +Okay! I've added: [T] task1 +``` +## Usage Example -`keyword (optional arguments)` +#### Input Example 1 +`deadline ip /by tonight` -Expected outcome: +#### Expected Output 1 +`Okay! I've added: [D] ip` -Description of the outcome. +#### Input Example 2 +`list` +#### Expected Output 2 ``` -expected output -``` +Here's what we've gotta do: +1. [D][ ] ip + Deadline: tonight +We currently have 1 tasks +``` \ No newline at end of file From 3fff458ba34c1cb396b269c9471a3d6e9a0ebda9 Mon Sep 17 00:00:00 2001 From: monihog Date: Thu, 16 Mar 2023 20:25:07 +0800 Subject: [PATCH 22/23] fixed bug with exception handling fixed formatting for Deadline objects --- src/main/java/Commands.java | 48 +++++++++++++----- src/main/java/Duke.java | 2 +- src/main/java/Ui.java | 70 ++++++++++++++++----------- src/main/java/tasktypes/Deadline.java | 2 +- 4 files changed, 81 insertions(+), 41 deletions(-) diff --git a/src/main/java/Commands.java b/src/main/java/Commands.java index c0e8867ba..f0ce889f1 100644 --- a/src/main/java/Commands.java +++ b/src/main/java/Commands.java @@ -2,6 +2,7 @@ import tasktypes.*; import java.util.ArrayList; +import java.util.Scanner; public class Commands { @@ -18,6 +19,8 @@ public class Commands { public static final String EMPTY_TASKLIST_MESSAGE = "We are free! Let's go play!"; public static final int USERINPUT_DUEDATE_INDEX = 1; public static final int USERINPUT_DESCRIPTION_INDEX = 0; + public static final int INVALID_INDEX = -99; + /** * Prints out a message stating that the input command is not a valid command */ @@ -34,19 +37,26 @@ public static void showList() { System.out.println(EMPTY_TASKLIST_MESSAGE); } else { String showList_footer = "We currently have " + TaskList.getNumTasks() + " tasks"; - Ui.printList(TaskList.getList(),SHOWLIST_HEADER,showList_footer); + Ui.printList(TaskList.getList(), SHOWLIST_HEADER, showList_footer); } } /** * Marks a given task in the tasklist as done or not done depending on parameter statusType * - * @param userInput the input from the user + * @param userInput the input from the user * @param statusType input true for done, false for notDone */ public static void markTask(String userInput, boolean statusType) { - int itemNum = Ui.getItemNumber(userInput); - if (itemNum > USERINPUT_DESCRIPTION_INDEX && itemNum <= TaskList.getNumTasks()) { + int itemNum = INVALID_INDEX; + try { + itemNum = Ui.getItemNumber(userInput); + } catch (DukeException d) { + System.out.println(d.getMessage()); + } + if (itemNum == INVALID_INDEX) { + return; + } else if (itemNum > USERINPUT_DESCRIPTION_INDEX && itemNum <= TaskList.getNumTasks()) { if (statusType == STATUSTYPE_DONE) { TaskList.markDone(itemNum - USERINPUT_DUEDATE_INDEX); System.out.println("Okay I've marked item " + itemNum + " as done:"); @@ -63,13 +73,20 @@ public static void markTask(String userInput, boolean statusType) { /** * Removes a given task from the tasklist based on the input message + * * @param userInput user's input */ public static void deleteTask(String userInput) { - int itemIndex = Ui.getItemIndex(userInput); + int itemIndex; + try { + itemIndex = Ui.getItemIndex(userInput); + } catch (DukeException d) { + System.out.println(d.getMessage()); + return; + } if (itemIndex <= TaskList.getNumTasks() - USERINPUT_DUEDATE_INDEX) { TaskList.deleteTask(itemIndex); - System.out.println(DELETE_TASK_MESSAGE + (itemIndex+ USERINPUT_DUEDATE_INDEX)); + System.out.println(DELETE_TASK_MESSAGE + (itemIndex + USERINPUT_DUEDATE_INDEX)); } else { System.out.println(TASK_NO_EXIST); } @@ -77,6 +94,7 @@ public static void deleteTask(String userInput) { /** * takes in user's description and adds a Todo task to the tasklist + * * @param userInput description of the todo */ public static void addTodoTask(String userInput) { @@ -88,24 +106,27 @@ public static void addTodoTask(String userInput) { /** * takes in user's description and adds a deadline task to the tasklist * if the user has not supplied a deadline, method will request for the deadline + * * @param userInput description of the deadline task */ public static void addDeadlineTask(String userInput) { String itemDescription, dueDate; if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { itemDescription = userInput.split(" ", 2)[DEADLINE_PARAM_INDEX]; - itemDescription = itemDescription.split(DEADLINE_USERINPUT_PREFIX,2)[USERINPUT_DESCRIPTION_INDEX]; + itemDescription = itemDescription.split(DEADLINE_USERINPUT_PREFIX, 2)[USERINPUT_DESCRIPTION_INDEX]; dueDate = userInput.split(DEADLINE_USERINPUT_PREFIX, 2)[USERINPUT_DUEDATE_INDEX]; } else { itemDescription = Ui.getItemDescription(userInput); dueDate = Ui.getDueDate(userInput); } - Deadline newTask = new Deadline(itemDescription,dueDate); + Deadline newTask = new Deadline(itemDescription, dueDate); TaskList.addItem(newTask); } + /** * takes in user's input and adds an event task to the tasklist * if the user has not supplied start and/or end dates, method will request for the dates + * * @param userInput description of the event task */ public static void addEventTask(String userInput) { @@ -113,13 +134,14 @@ public static void addEventTask(String userInput) { String[] StartEndDates = Ui.getStartEndDates(userInput); String startDate = StartEndDates[STARTDATE_INDEX]; String endDate = StartEndDates[ENDDATE_INDEX]; - Event newTask = new Event(itemDescription,startDate,endDate); + Event newTask = new Event(itemDescription, startDate, endDate); TaskList.addItem(newTask); } /** * Checks if searchTerm can be found inside taskDescription. - * @param searchTerm String to be found in the taskDescription + * + * @param searchTerm String to be found in the taskDescription * @param taskDescription String where searchTerm is to be found in * @return true if taskDescription contains searchTerm */ @@ -131,6 +153,7 @@ public static boolean containsSearchTerm(String searchTerm, String taskDescripti * Searches the current tasklist for a user-supplied String * Will print out the list of items that match the user input if exists. * Else will print out a no items found message + * * @param userInput the String to be searched for */ public static void searchTask(String userInput) { @@ -138,9 +161,9 @@ public static void searchTask(String userInput) { ArrayList resultsList = new ArrayList<>(); int searchResults = USERINPUT_DESCRIPTION_INDEX; for (int i = USERINPUT_DUEDATE_INDEX; i <= TaskList.getNumTasks(); ++i) { - String taskDescription = TaskList.getItem(i- USERINPUT_DUEDATE_INDEX).getDescription(); + String taskDescription = TaskList.getItem(i - USERINPUT_DUEDATE_INDEX).getDescription(); if (containsSearchTerm(searchTerm, taskDescription)) { - resultsList.add(TaskList.getItem(i- USERINPUT_DUEDATE_INDEX)); + resultsList.add(TaskList.getItem(i - USERINPUT_DUEDATE_INDEX)); searchResults += USERINPUT_DUEDATE_INDEX; } } @@ -151,4 +174,5 @@ public static void searchTask(String userInput) { Ui.printList(resultsList, resultsList_header, ""); } } + } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 96c25f632..564fbcb88 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -15,7 +15,7 @@ public static void main(String[] args) { case EXIT_CMD: case BYE_CMD: isExit = true; - Ui.exitMessage(); + Ui.endSavePrompt(); break; default: Parser.ParseCommand(inputCommand,userInput); diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 533207d10..96d8ef535 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,5 +1,4 @@ import tasktypes.Task; - import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; @@ -20,14 +19,10 @@ public class Ui { public static final String EVENT_START_PROMPT = "When does this event start?"; public static final String EVENT_END_PROMPT = "When does this event end?"; public static final String ITEM_DESCRIPTION_PROMPT = "What are you referring to?"; - public static final int NOT_FOUND_IN_STRING = -99; - - /** - * prints the exit message when shutdown is desired - */ - public static void exitMessage() { - System.out.println(EXIT_MESSAGE); - } + public static final int EVENT_STARTMARKER_LENGTH = 5; + public static final int EVENT_ENDMARKER_LENGTH = 3; + public static final int INVALID_INDEX = -99; + public static final String END_SAVE_PROMPT = "Wait, do you want to save the current list?\n Type yes if so"; /** * prints a welcome message on startup. @@ -55,6 +50,7 @@ public static String getItemDescription(String userInput) { } if (description.contains("/")) { description = description.split("/",2)[0]; + description = description.trim(); } return description; } @@ -63,10 +59,11 @@ public static String getItemDescription(String userInput) { * gets the item number for the user supplied command to act on * @param userInput command input from the user * @return returns number of the item in the tasklist (1-based) + * @throws DukeException if user's input is not valid or out of bounds */ - public static int getItemNumber(String userInput) { + public static int getItemNumber(String userInput) throws DukeException { Scanner in = new Scanner(System.in); - int itemNumber; + int itemNumber = INVALID_INDEX; try { itemNumber = Integer.parseInt(userInput.split(" ", 2)[1]); } catch (ArrayIndexOutOfBoundsException e) { @@ -74,12 +71,11 @@ public static int getItemNumber(String userInput) { System.out.println(ITEM_NUMBER_PROMPT); try { itemNumber = in.nextInt(); - } catch (InputMismatchException i){ - System.out.println(REJECTED_NON_NUMBER_INPUT); - itemNumber = getItemNumber(userInput); + } catch (InputMismatchException i) { + throw new DukeException(REJECTED_NON_NUMBER_INPUT); } } catch (NumberFormatException n) { - itemNumber = getItemNumber(userInput); + throw new DukeException(REJECTED_NON_NUMBER_INPUT); } return itemNumber; } @@ -88,9 +84,14 @@ public static int getItemNumber(String userInput) { * gets the item index for the user supplied command to act on * @param userInput command input from the user * @return returns index of the item in the tasklist (0-based) + * @throws DukeException if user's input is not valid or out of bounds */ - public static int getItemIndex(String userInput) { - return getItemNumber(userInput) - 1; + public static int getItemIndex(String userInput) throws DukeException{ + int itemIndex = INVALID_INDEX; + while (itemIndex <= 0) { + itemIndex = getItemNumber(userInput); + } + return itemIndex - 1; } /** @@ -103,7 +104,7 @@ public static String getDueDate(String userInput) { Scanner in = new Scanner(System.in); String dueDate; if (userInput.contains(DEADLINE_USERINPUT_PREFIX)) { - dueDate = userInput.substring(userInput.indexOf(DEADLINE_USERINPUT_PREFIX)); + dueDate = userInput.substring(userInput.indexOf(DEADLINE_USERINPUT_PREFIX)).trim(); } else { System.out.println(DUEDATE_PROMPT); dueDate = in.nextLine().trim(); @@ -120,7 +121,7 @@ public static int getStartMarkerIndex(String userInput) { if (userInput.contains(STARTDATE_USERINPUT_PREFIX)) { return userInput.indexOf(STARTDATE_USERINPUT_PREFIX); } else { - return NOT_FOUND_IN_STRING; + return INVALID_INDEX; } } @@ -133,7 +134,7 @@ public static int getEndMarkerIndex(String userInput) { if (userInput.contains(ENDDATE_USERINPUT_PREFIX)) { return userInput.indexOf(ENDDATE_USERINPUT_PREFIX); } else { - return NOT_FOUND_IN_STRING; + return INVALID_INDEX; } } /** @@ -146,14 +147,14 @@ public static String getStartDate(String userInput) { Scanner in = new Scanner (System.in); int startMarkerIndex = getStartMarkerIndex(userInput); int endMarkerIndex = getEndMarkerIndex(userInput); - if (startMarkerIndex == NOT_FOUND_IN_STRING) { + if (startMarkerIndex == INVALID_INDEX) { System.out.println(EVENT_START_PROMPT); return in.nextLine().trim(); } else { - if (endMarkerIndex == NOT_FOUND_IN_STRING) { - return userInput.substring(startMarkerIndex + 1); + if (endMarkerIndex == INVALID_INDEX) { + return userInput.substring(startMarkerIndex + EVENT_STARTMARKER_LENGTH).trim(); } else { - return userInput.substring(startMarkerIndex + 1,endMarkerIndex); + return userInput.substring(startMarkerIndex + EVENT_STARTMARKER_LENGTH,endMarkerIndex).trim(); } } } @@ -168,14 +169,14 @@ public static String getEndDate(String userInput) { Scanner in = new Scanner (System.in); int startMarkerIndex = getStartMarkerIndex(userInput); int endMarkerIndex = getEndMarkerIndex(userInput); - if (endMarkerIndex == NOT_FOUND_IN_STRING) { + if (endMarkerIndex == INVALID_INDEX) { System.out.println(EVENT_END_PROMPT); return in.nextLine().trim(); } else { if (startMarkerIndex > endMarkerIndex) { - return userInput.substring(endMarkerIndex + 1,startMarkerIndex); + return userInput.substring(endMarkerIndex + EVENT_ENDMARKER_LENGTH,startMarkerIndex).trim(); } else { - return userInput.substring(endMarkerIndex + 1); + return userInput.substring(endMarkerIndex + EVENT_ENDMARKER_LENGTH).trim(); } } } @@ -210,4 +211,19 @@ public static void printList(ArrayList itemList, String header, String foo System.out.println(footer); } } + /** + * Checks with the user if the data should be saved before shutdown of the program + * prints the exit message afterwards + */ + public static void endSavePrompt() { + Scanner in = new Scanner(System.in); + if (!TaskList.getList().isEmpty()) { + System.out.println(END_SAVE_PROMPT); + String saveDecision = in.nextLine().trim().toLowerCase(); + if (saveDecision.equals("yes")) { + Storage.writeToTaskList(); + } + } + System.out.println(EXIT_MESSAGE); + } } diff --git a/src/main/java/tasktypes/Deadline.java b/src/main/java/tasktypes/Deadline.java index aa2c798d0..7e1ac5f76 100644 --- a/src/main/java/tasktypes/Deadline.java +++ b/src/main/java/tasktypes/Deadline.java @@ -21,6 +21,6 @@ public String getDueDate() { @Override public String getTask() { return taskTypeIcon() + isDoneIcon() + " " + getDescription() - + System.lineSeparator() + " Deadline:" + getDueDate(); + + System.lineSeparator() + " Deadline: " + getDueDate(); } } From 488a811fce469058bdeee2df46b344e27a0ace64 Mon Sep 17 00:00:00 2001 From: monihog Date: Thu, 16 Mar 2023 20:51:37 +0800 Subject: [PATCH 23/23] re-commit with missing DukeException file --- src/main/java/DukeException.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..b1ea276ef --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,5 @@ +public class DukeException extends Exception { + DukeException(String errorMessage) { + System.out.println(errorMessage); + } +}