diff --git a/README.md b/README.md index 8715d4d91..ef54e7635 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,126 @@ -# Duke project template +# Duke project: the task management chatbot +## project user guide -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. +* This is a CLI(command line interface) task management application named Baymax. +* ## 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: +2. 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).
+ 2. Select the project directory, and click `OK`. + 3. If there are any further prompts, accept the defaults. +3. 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 - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| - ``` +4. After that, locate the `src/main/java/Baymax.java` file, right-click it, and choose `Run Baymax.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, I'm Baymax. +Your personal task managing companion. +What can I do for you? +``` + +5. The application now should run in the console. +6. Now you can interact with the program in the console. + + +*Using Command Line* + +1. 'Build' the project using IntelliJ. +2. Open the `Terminal`/`Command Prompt`. +3. `cd` into the project's `out\production\ip` directory +4. Type `java Baymax`, then press enter to execute. +5. Now you can interact with the program through the CLI.. + + +## Features +### three types of tasks +### 1. Feature-event: +- event is a subclass of task, with description and date. +### 2. Feature-deadline: +- deadline is a subclass of task, with description and due date. +### 3. Feature-todo: +- todo is a subclass of task, with description of the task. + +## Usage +1. Adding a todo task + * Add a todo task to the list of tasks. + * Format: `todo ` + * Words inside `< >` are parameters. There is no need to type the `<` and `>`. + * Example of usage:`todo Read textbook` + * Expected oucome: +``` +Got it. I've added this task: +[T][ ] Read textbook +Now you have 1 tasks tasks in the list. +``` + +2. Adding a deadline task + * Add a deadline task to the list of tasks. + * Format: `deadline /by ` + * Example: + * `deadline Submit lab report /by 03 04 2022` + * Expected oucome: +``` +Got it. I've added this task: +[D][ ] Submit lab report /by 03 04 2022` +Now you have 2 tasks tasks in the list. +``` +3. Adding a event task + * Add an event task to the list of tasks. + * Format: `event /at ` + * Example: + * `event Attend meeting /at 03 04 2022` + * Expected oucome: +``` +Got it. I've added this task: +[E][ ] Submit lab report /by 03 04 2022` +Now you have 3 tasks tasks in the list. +``` + +4. Listing all tasks: `list` + * Display the entire list of tasks. + * Format: `list` + * Expected oucome: + +``` +Here are the tasks in your list: +1.[T][ ] Read textbook +2.[D][ ] Submit lab report /by 03 04 2022 +3.[E][ ] Submit lab report /by 03 04 2022` +``` +5. Finding all tasks containing a keyword in their description: `find` + * Find all the tasks whose description contains the specified keyword. + * Format: `find ` + * Example: + * `find meeting` + + Returns all tasks containing `meeting` in description. + +6. Deleting a task: `delete` + * Delete a task by specifying the index of it. + * Format: `delete ` + * Example: + * `delete 2` + + Deletes the second task in the task list. + +7. Marking a task: `mark` + * Mark a task as done by specifying the index of it. + * Format: `mark ` + * Example: + * `mark 3` + + Marks the third task in the task list as done. + +8. Marking a task: `unmark` + * Unmark a task as not done yet by specifying the index of it. + * Format: `unmark ` + * Example: + * `mark 1` + + Unmark the first task in the task list. + +9. Exiting the program: `bye` + * Exit the program. + * Format: `bye` + * Expected output:`Bye, Hope to see you again soon!` diff --git a/data/Baymax.txt b/data/Baymax.txt new file mode 100644 index 000000000..c29faa58b --- /dev/null +++ b/data/Baymax.txt @@ -0,0 +1,4 @@ +D / 0 / xinyi submit cs2113 ip / 03 02 2022 +E / 1 / xinyi learns lecture / 03 29 2022 +T / 0 / xinyi practice coding / +T / 0 / Mark release a jar file / diff --git a/docs/README.md b/docs/README.md index 8077118eb..9b51f0565 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,126 @@ -# User Guide +# Duke project: the task management chatbot +## project user guide -## Features +* This is a CLI(command line interface) task management application named Baymax. +* -### Feature-ABC +## Setting up in Intellij -Description of the feature. +Prerequisites: JDK 11, update Intellij to the most recent version. -### Feature-XYZ +1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) +2. Open the project into Intellij as follows: + 1. Click `Open`. + 2. Select the project directory, and click `OK`. + 3. If there are any further prompts, accept the defaults. +3. 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. +4. After that, locate the `src/main/java/Baymax.java` file, right-click it, and choose `Run Baymax.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: -Description of the feature. +``` +Hello, I'm Baymax. +Your personal task managing companion. +What can I do for you? +``` + +5. The application now should run in the console. +6. Now you can interact with the program in the console. -## Usage -### `Keyword` - Describe action +*Using Command Line* -Describe the action and its outcome. +1. 'Build' the project using IntelliJ. +2. Open the `Terminal`/`Command Prompt`. +3. `cd` into the project's `out\production\ip` directory +4. Type `java Baymax`, then press enter to execute. +5. Now you can interact with the program through the CLI.. -Example of usage: -`keyword (optional arguments)` +## Features +### three types of tasks +### 1. Feature-event: +- event is a subclass of task, with description and date. +### 2. Feature-deadline: +- deadline is a subclass of task, with description and due date. +### 3. Feature-todo: +- todo is a subclass of task, with description of the task. + +## Usage +1. Adding a todo task + * Add a todo task to the list of tasks. + * Format: `todo ` + * Words inside `< >` are parameters. There is no need to type the `<` and `>`. + * Example of usage:`todo Read textbook` + * Expected oucome: +``` +Got it. I've added this task: +[T][ ] Read textbook +Now you have 1 tasks tasks in the list. +``` -Expected outcome: +2. Adding a deadline task + * Add a deadline task to the list of tasks. + * Format: `deadline /by ` + * Example: + * `deadline Submit lab report /by 03 04 2022` + * Expected oucome: +``` +Got it. I've added this task: +[D][ ] Submit lab report (by: 03 04 2022) +Now you have 2 tasks tasks in the list. +``` +3. Adding a event task + * Add an event task to the list of tasks. + * Format: `event /at ` + * Example: + * `event Attend meeting /at 03 04 2022` + * Expected oucome: +``` +Got it. I've added this task: +[E][ ] Submit lab report (by: 03 04 2022) +Now you have 3 tasks tasks in the list. +``` -Description of the outcome. +4. Listing all tasks: `list` + * Display the entire list of tasks. + * Format: `list` + * Expected oucome: ``` -expected output +Here are the tasks in your list: +1 [T][ ] Read textbook +2 [D][ ] Submit lab report (by: 03 04 2022) +3 [E][ ] Submit lab report (at: 03 04 2022) ``` +5. Finding all tasks containing a keyword in their description: `find` + * Find all the tasks whose description contains the specified keyword. + * Format: `find ` + * Example: + * `find meeting` + + Returns all tasks containing `meeting` in description. + +6. Deleting a task: `delete` + * Delete a task by specifying the index of it. + * Format: `delete ` + * Example: + * `delete 2` + + Deletes the second task in the task list. + +7. Marking a task: `mark` + * Mark a task as done by specifying the index of it. + * Format: `mark ` + * Example: + * `mark 3` + + Marks the third task in the task list as done. + +8. Marking a task: `unmark` + * Unmark a task as not done yet by specifying the index of it. + * Format: `unmark ` + * Example: + * `mark 1` + + Unmark the first task in the task list. + +9. Exiting the program: `bye` + * Exit the program. + * Format: `bye` + * Expected output:`Bye, Hope to see you again soon!` diff --git a/src/main/java/Baymax.java b/src/main/java/Baymax.java new file mode 100644 index 000000000..1b99eada0 --- /dev/null +++ b/src/main/java/Baymax.java @@ -0,0 +1,58 @@ +import java.lang.String; + +import java.io.IOException; + +import baymax.data.TaskManager; +import baymax.ui.Ui; +import baymax.parse.Parser; +import baymax.storage.Storage; + +/** + * Enter point for Baymax task management bot + * Initialize the task manager + */ +public class Baymax { + + /** + * initialize UI, taskmanager, and storage. + * @param filePath the txt file location where + * the application read from and store in + */ + private final Ui ui; + private TaskManager tManager; + + public Baymax(String filePath) { + ui = new Ui(); + Storage storage = new Storage(filePath); + try { + tManager = new TaskManager(storage.load()); + } catch (IOException e) { + ui.showLoadingError(e.getMessage()); + tManager = new TaskManager(); + } + } + + /** + * Read user input + * keep iteration until exit signal is sent + * @throws IOException + */ + public void run() throws IOException { + ui.displayWelcomeMessage(); + boolean isBye; + isBye = false; + while (!isBye) { + String fullCommand = ui.getUserCommand(); + Parser parse = new Parser(ui); + parse.parse(fullCommand, tManager); + isBye = parse.getExit(); + } + ui.showByeMessage(); + } + + public static void main(String[] args) throws IOException { + new Baymax("data/Baymax.txt").run(); + } +} + + diff --git a/src/main/java/BaymaxException.java b/src/main/java/BaymaxException.java new file mode 100644 index 000000000..4f831e756 --- /dev/null +++ b/src/main/java/BaymaxException.java @@ -0,0 +1,5 @@ +public class BaymaxException extends RuntimeException{ + public BaymaxException(String errorMessage){ + super(errorMessage); + } +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334c..000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..1da7a8fc7 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Baymax + diff --git a/src/main/java/baymax/data/Deadline.java b/src/main/java/baymax/data/Deadline.java new file mode 100644 index 000000000..56a16169e --- /dev/null +++ b/src/main/java/baymax/data/Deadline.java @@ -0,0 +1,49 @@ +package baymax.data; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +/** + * Inherit from Task class, + * with additioanl attribute: ddl storing the date of deadline + */ +public class Deadline extends Task { + + private LocalDate ddl; + + /** + * class constructor + * @param description description of the deadline task + * @param ddl deadline of the task in format: MM dd yyyy + */ + public Deadline(String description, String ddl) { + super(description); + try{ + this.ddl = LocalDate.parse(ddl); + } catch(Exception e){ + this.ddl = LocalDate.parse(ddl,DateTimeFormatter.ofPattern("MM dd yyyy")); + } + } + + @Override + public String getStatusIcon() { + return "[D]" + super.getStatusIcon() ; + } + + @Override + public String getDescription() { + return super.getDescription()+ + "( by: "+ ddl.format(DateTimeFormatter.ofPattern("MM dd yyyy")) + ")"; + } + + @Override + public String saveInfo(){ + String sep = " / "; + return "D" + super.saveInfo() + sep + this.ddl.format(DateTimeFormatter.ofPattern("MM dd yyyy")) ; + } + + @Override + public String toString(){ + return super.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/baymax/data/Event.java b/src/main/java/baymax/data/Event.java new file mode 100644 index 000000000..d9f38f149 --- /dev/null +++ b/src/main/java/baymax/data/Event.java @@ -0,0 +1,49 @@ +package baymax.data; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +/** + * Inherit from Task class, + * with additioanl attribute: time storing the date of event + */ +public class Event extends Task { + + private LocalDate time; + + /** + * class constructor + * @param description description of the deadline task + * @param time time of the task in format: MM dd yyyy + */ + public Event(String description, String time) { + super(description); + try{ + this.time = LocalDate.parse(time); + } catch(Exception e){ + this.time= LocalDate.parse(time,DateTimeFormatter.ofPattern("MM dd yyyy")); + } + } + + @Override + public String getStatusIcon() { + return "[E]" + super.getStatusIcon(); + } + + @Override + public String getDescription() { + return super.getDescription()+ + "( at: "+ time.format(DateTimeFormatter.ofPattern("MM dd yyyy")) + ")"; + } + + @Override + public String saveInfo(){ + String sep =" / "; + return "E" + super.saveInfo() + sep + this.time.format(DateTimeFormatter.ofPattern("MM dd yyyy")) ; + } + + @Override + public String toString(){ + return super.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/baymax/data/Task.java b/src/main/java/baymax/data/Task.java new file mode 100644 index 000000000..66f3b3f65 --- /dev/null +++ b/src/main/java/baymax/data/Task.java @@ -0,0 +1,42 @@ +package baymax.data; + +/** + * abstract class Task + * the parent of Todo, Deadline, and Event + */ +public class Task { + + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "[X]" : "[ ]"); // mark done task with X + } + + public String getDescription() { + return description; + } + public void markTaskDone() { + isDone = true; + } + + public void unmarkTaskDone() { + isDone = false; + } + + public String saveInfo(){ + String sep = " / "; + String printT = sep + (isDone ? "1" : "0") + sep + this.description; + return printT; + } + + public String toString(){ + return getStatusIcon() + " " + getDescription(); + + } +} diff --git a/src/main/java/baymax/data/TaskManager.java b/src/main/java/baymax/data/TaskManager.java new file mode 100644 index 000000000..6d5001a51 --- /dev/null +++ b/src/main/java/baymax/data/TaskManager.java @@ -0,0 +1,94 @@ +package baymax.data; + +import java.util.ArrayList; + +import baymax.storage.Storage; +import baymax.ui.Ui; + +/** + * taskmanager class is used for manage the arrarylist of tasks + */ +public class TaskManager { + + String horiLine = "____________________________________________________________\n"; + private final ArrayList tasks; + private Ui ui; + + /** + * class constructor, + * no parameter passed,create a new arrarylist + */ + public TaskManager() { + tasks = new ArrayList<>(); + } + + /** + * class constructor + * initialise with user input of tasks + * @param tasks + */ + public TaskManager(ArrayList tasks) { + this.tasks = tasks; + } + + public ArrayList getTasks() { + return tasks; + } + + /** + * add new task into arrylist of tasks + * @param newT the new task + */ + public void addTask(Task newT) { + tasks.add(newT); + System.out.println(horiLine); + System.out.println(" Got it. I've added this task: \n"+ newT.toString()+"\n"+ + "Now you have " + + tasks.size() + " tasks in the list."); + System.out.println(horiLine); + } + + /** + * mark the certain task as done + * @param taskIndex + */ + public void markTask (int taskIndex) { + System.out.println(horiLine); + Task temp = tasks.get(taskIndex); + temp.markTaskDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(temp.getStatusIcon() + " " + + temp.getDescription()); + System.out.println(horiLine); + } + + /** + * unmark the certain task as not done yet + * @param taskIndex + */ + public void unmarkTask (int taskIndex) { + System.out.println(horiLine); + Task temp = tasks.get(taskIndex); + temp.unmarkTaskDone(); + System.out.println("OK, I've marked this task as not done yet:"); + System.out.println(temp.getStatusIcon() + " " + + temp.getDescription()); + System.out.println(horiLine); + } + + /** + * delete the task + * @param taskIndex index of the task to be deleted + */ + public void deleteTask (int taskIndex) { + System.out.println(horiLine); + Task deleted = tasks.get(taskIndex); + System.out.println("Noted. I've removed this task:"); + String text = String.format("%s %s", deleted.getStatusIcon(), deleted.getDescription()); + System.out.println(text); + tasks.remove(taskIndex); + System.out.println("Now you have " + tasks.size() + " tasks in the list."); + System.out.println(horiLine); + } + +} + diff --git a/src/main/java/baymax/data/Todo.java b/src/main/java/baymax/data/Todo.java new file mode 100644 index 000000000..6a2f0d667 --- /dev/null +++ b/src/main/java/baymax/data/Todo.java @@ -0,0 +1,28 @@ +package baymax.data; + +import baymax.data.Task; + +/** + * Inherit from Task class + */ +public class Todo extends Task { + public Todo(String description) { + super(description); + } + + @Override + public String getStatusIcon() { + return "[T]" + super.getStatusIcon(); + } + + @Override + public String saveInfo(){ + String sep =" / "; + return "T" + super.saveInfo() + sep; + } + + @Override + public String toString(){ + return super.toString(); + } +} diff --git a/src/main/java/baymax/exception/BaymaxException.java b/src/main/java/baymax/exception/BaymaxException.java new file mode 100644 index 000000000..317ff8be7 --- /dev/null +++ b/src/main/java/baymax/exception/BaymaxException.java @@ -0,0 +1,7 @@ +package baymax.exception; + +public class BaymaxException extends RuntimeException{ + public BaymaxException(String errorMessage){ + super(errorMessage); + } +} diff --git a/src/main/java/baymax/parse/Parser.java b/src/main/java/baymax/parse/Parser.java new file mode 100644 index 000000000..be89caf9e --- /dev/null +++ b/src/main/java/baymax/parse/Parser.java @@ -0,0 +1,147 @@ +package baymax.parse; + +import baymax.data.Deadline; +import baymax.data.Event; +import baymax.data.TaskManager; +import baymax.data.Todo; +import baymax.storage.Storage; +import baymax.ui.Ui; +import baymax.exception.BaymaxException; +import baymax.data.Task; + +import java.io.IOException; +import java.time.format.DateTimeParseException; +import java.util.ArrayList; +import static java.util.stream.Collectors.toList; + +/** + * Parse user input and execute + */ +public class Parser { + + private static boolean isBye; + private static final String horiLine = "____________________________________________________________"; + private Ui ui; + + public Parser(Ui ui) { + this.ui = ui; + } + public void setExit(boolean bye) { + isBye = bye; + } + + public boolean getExit() { + return isBye; + } + + /** + * Parse user input + * interpret user input with initial command word and execute + * @param fullCommand the whole content of user input + * @param tManager task manager object storing all the tasks + * @throws IOException + */ + public void parse(String fullCommand, TaskManager tManager) throws IOException { + String[] word_split; + word_split = fullCommand.split(" ", 2); + String command = word_split[0]; + + if (command.equals("bye")) { + Storage.save(tManager.getTasks()); + setExit(true); + return; + } + + String taskDescrip; + switch (command) { + case "todo": + tManager.addTask(new Todo(word_split[1])); + break; + case "deadline": + try { + String[] taskWord = word_split[1].split("/by ", 2); + taskDescrip = taskWord[0]; + String ddl = taskWord[1]; + tManager.addTask(new Deadline(taskDescrip, ddl)); + } catch (ArrayIndexOutOfBoundsException e) { + ui.DescripEmptyExceptionMessage(); + } catch (BaymaxException b){ + System.out.println( b.getMessage() +" ☹ OOPS!!! Let's do it again."); + } catch (DateTimeParseException d){ + ui.DateExceptionMessage(); + } + break; + case "event": + try { + String[] taskWord = word_split[1].split("/at ", 2); + taskDescrip = taskWord[0]; + String eventTime = taskWord[1]; + tManager.addTask(new Event(taskDescrip, eventTime)); + } catch (ArrayIndexOutOfBoundsException e) { + ui.DescripEmptyExceptionMessage(); + } catch (BaymaxException b){ + System.out.println( b.getMessage() +" ☹ OOPS!!! Let's do it again."); + }catch (DateTimeParseException d){ + ui.DateExceptionMessage(); + } + break; + case "list": + ui.printTaskList(tManager.getTasks()); + break; + case "mark": + try { + tManager.markTask(Integer.parseInt(word_split[1]) - 1); + } catch (ArrayIndexOutOfBoundsException e) { + ui.ArrayIndexOutOfBoundsExceptionMessage(); + } catch (NumberFormatException e) { + ui.NumberFormatExceptionMessage(); + } catch (BaymaxException b){ + System.out.println( b.getMessage() +" ☹ OOPS!!! Let's do it again."); + } catch (IndexOutOfBoundsException e) { + ui.IndexOutOfBoundsExceptionMessage(); + } + break; + case "unmark": + try { + tManager.unmarkTask(Integer.parseInt(word_split[1]) - 1); + } catch (ArrayIndexOutOfBoundsException e) { + ui.ArrayIndexOutOfBoundsExceptionMessage(); + } catch (NumberFormatException e) { + ui.NumberFormatExceptionMessage(); + }catch (BaymaxException b){ + System.out.println( b.getMessage() +" ☹ OOPS!!! Let's do it again."); + }catch (IndexOutOfBoundsException e) { + ui.IndexOutOfBoundsExceptionMessage(); + } + break; + case "delete": + try { + tManager.deleteTask(Integer.parseInt(word_split[1]) - 1); + } catch (ArrayIndexOutOfBoundsException e) { + ui.ArrayIndexOutOfBoundsExceptionMessage(); + } catch (BaymaxException b){ + System.out.println( b.getMessage() +" ☹ OOPS!!! Let's do it again."); + }catch (IndexOutOfBoundsException e) { + ui.IndexOutOfBoundsExceptionMessage(); + } + break; + case "find": + try { + String search = word_split[1]; + ArrayList filteredTask = (ArrayList) tManager.getTasks().stream(). + filter(task -> task.getDescription().toLowerCase().contains(search.toLowerCase())). + collect(toList()); + ui.printTaskList(filteredTask); + } catch (IndexOutOfBoundsException e) { + ui.IndexOutOfBoundsExceptionMessage(); + } + break; + case "help": + ui.helpMessage(); + break; + default: + ui.displayErrorMessage(); + break; + } + } +} diff --git a/src/main/java/baymax/storage/Storage.java b/src/main/java/baymax/storage/Storage.java new file mode 100644 index 000000000..b1f598fcd --- /dev/null +++ b/src/main/java/baymax/storage/Storage.java @@ -0,0 +1,95 @@ +package baymax.storage; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +import baymax.data.Deadline; +import baymax.data.Event; +import baymax.data.Task; +import baymax.data.Todo; + + +/** + * class for file management + * loading data into baymax, saving data after execution + */ +public class Storage { + private String filePath; + + public Storage(String filePath) { + this.filePath = filePath; + } + + public ArrayList load() throws IOException { + + System.out.println("Record accessing......"); + File dir = new File("data"); + if(!dir.exists()){ + dir.mkdir(); + System.out.println("create a new directory \"data\"...... "); + } + File file = new File("data/Baymax.txt"); + if(!file.exists()) { + file.createNewFile(); + System.out.println("create a new file \"Baymax.txt\"...... "); + } + Scanner s = new Scanner(file); + ArrayList tasks = new ArrayList<>(); + decode(s, tasks); + return tasks; + } + + public static void decode(Scanner s, ArrayList tasks) { + int taskIndex = -1; + while(s.hasNext()){ + taskIndex++; + Task temp; + String[] word_split = s.nextLine().split(" / "); + //System.out.println(word_split[0]+" sdf " + word_split[1] + " df "+ word_split[2]+ " df "+ word_split[3]); + switch(word_split[0]){ + case "T": + tasks.add(new Todo(word_split[2])); + if (word_split[1].equals("1")) { + temp = tasks.get(taskIndex); + temp.markTaskDone(); + } + break; + case "D": + tasks.add(new Deadline(word_split[2],word_split[3])); + if (word_split[1].equals("1")) { + temp = tasks.get(taskIndex); + temp.markTaskDone(); + } + break; + case "E": + tasks.add(new Event(word_split[2],word_split[3])); + if (word_split[1].equals("1")) { + temp = tasks.get(taskIndex); + temp.markTaskDone(); + } + break; + default: + System.out.println("Error accur, please check."); + break; + } + } + + } + + public static void save(ArrayList tasks) throws IOException { + encode(tasks); + } + + public static void encode(ArrayList tasks) throws IOException { + FileWriter fw = new FileWriter("data/Baymax.txt"); + for(int i = 0; i < tasks.size(); i++){ + fw.write(tasks.get(i).saveInfo() + "\n"); + } + fw.close(); + } +} + + diff --git a/src/main/java/baymax/ui/Ui.java b/src/main/java/baymax/ui/Ui.java new file mode 100644 index 000000000..2dd9d593f --- /dev/null +++ b/src/main/java/baymax/ui/Ui.java @@ -0,0 +1,112 @@ +package baymax.ui; + +import baymax.data.Task; +import java.util.ArrayList; +import java.util.Scanner; + +/** + * class for mange UI (output of baymax) + * give response and guide to the user + */ +public class Ui { + String input; + private final Scanner in; + public static final String horiLine = "_____________________________________________"; + boolean isBye; + + public Ui() { + in = new Scanner(System.in); + isBye = false; + } + + public String getUserCommand() { + input = in.nextLine(); + return input; + } + + public void showLoadingError(String message) { + System.out.println(" You have error in loading data. Error message:"); + System.out.println("" + message); + } + + public void displayWelcomeMessage() { + System.out.println(horiLine); + String greeting = " Hello, I'm Baymax.\n"+ + " Your personal task managing companion. \n" + + " What can I do for you? "; + System.out.println(greeting); + this.helpMessage(); + } + + public void helpMessage(){ + System.out.println(horiLine); + String help = " Add three types of tasks: event, deadline, task \n"+ + " \tin the form of \n" + + " \tto add deadline time: append \"'/by MM dd yyyy\" \n" + + " \tto add event time: append \"'/at MM dd yyyy\" \n" + + " Use list method to see all the tasks \n" + + " Use mark and unmark methods with task index to set or unset the tasks as done \n" + + " Use find method with description to search specific tasks \n" + + " Use delete method with task index to delete certain task \n" + + " Let's start!!! "; + System.out.println(help); + System.out.println(horiLine); + } + + public void showByeMessage() { + System.out.println(horiLine); + System.out.println("Bye, Hope to see you again soon!"); + System.out.println(horiLine); + } + + public void displayErrorMessage() { + System.out.println(horiLine); + System.out.println("Error occur: cannot find this command.\n"+ + "Please re-enter your command."); + System.out.println(horiLine); + } + + public void printTaskList(ArrayList tasks) { + System.out.println(horiLine); + if (tasks.size() == 0) { + System.out.println("There's no tasks found! Let's add more tasks"); + System.out.println(horiLine); + return; + } + System.out.println("Here are the tasks in your list:"); + for (Task task : tasks) { + System.out.println(String.format("%d ", tasks.indexOf((task)) + 1) + task); + } + System.out.println(horiLine); + } + + public void DateExceptionMessage() { + System.out.println(horiLine); + System.out.println(" ☹ OOPS!!! Please re-enter the date in format: MM dd yyyy"); + System.out.println(horiLine); + } + + public void DescripEmptyExceptionMessage() { + System.out.println(horiLine); + System.out.println(" ☹ OOPS!!! The description or timestamp of the task cannot be empty."); + System.out.println(horiLine); + } + + public void ArrayIndexOutOfBoundsExceptionMessage() { + System.out.println(horiLine); + System.out.println("☹ OOPS!!!Please input an integer for task index."); + System.out.println(horiLine); + } + + public void NumberFormatExceptionMessage() { + System.out.println(horiLine); + System.out.println("☹ OOPS!!!Please put in an integer value"); + System.out.println(horiLine); + } + + public void IndexOutOfBoundsExceptionMessage() { + System.out.println("☹ OOPS!!!Entered index is out of bounds. Please redo it."); + System.out.println(horiLine); + } + +}