diff --git a/docs/README.md b/docs/README.md index 8077118eb..98bab9dc8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,194 @@ # User Guide +The Chatbot name is very intuitive - Chatbot. ## Features +1. list +2. mark +3. unmark +4. todo +5. deadline +6. event +7. delete +8. find + +### Feature-list +List all tasks +### Feature-mark +Mark a task as done +### Feature-unmark +Unmark a task as done +### Feature-todo +Add a todo +### Feature-deadline +Add a deadline +### Feature-event +Add an event +### Feature-delete +Delete a task +### Feature-find +Find a task -### Feature-ABC -Description of the feature. -### Feature-XYZ -Description of the feature. ## Usage -### `Keyword` - Describe action +### `list` - list all tasks +List all the tasks recorded + +Command: `list` + +Argument: Nil + +Example: `list` + +Example of outcome: + + Tasks are listed +``` + ____________________________________________________________ + Here are the tasks in your list: + 1.[T][X] read book + 2.[D][ ] return book (by: June 6th) + 3.[E][ ] project meeting (from: Aug 6th 2pm to: 4pm) + 4.[T][X] join sports club + 5.[T][ ] borrow book + ____________________________________________________________ +``` + +### `mark` - mark a task +Command: `mark` + +Argument: ID of the task + +Example: `mark 1` + +Example of outcome: + + Task is marked as done + +``` +____________________________________________________________ + Nice! I've marked this task as done: + [X] borrow new book +____________________________________________________________ +``` + +### `unmark` - unmark a task +Mark a task as done -Describe the action and its outcome. +Command: `unmark` -Example of usage: +Argument: ID of the task -`keyword (optional arguments)` +Example: `unmark 1` -Expected outcome: +Example of outcome: -Description of the outcome. + Task is unmarked ``` -expected output +____________________________________________________________ + OK, I've marked this task as not done yet: + [ ] borrow new book +____________________________________________________________ +``` + +### `todo` - add a todo + +Command: `todo` + +Argument: description of the task + +Example: `todo borrow book` + +Example of outcome: + + Todo is added + +``` + ____________________________________________________________ + Got it. I've added this task: + [T][ ] borrow book + Now you have 5 tasks in the list. + ____________________________________________________________ +``` + +### `deadline` - add a deadline + +Command: `deadline` + +Argument: description of the deadline + +Example: `deadline return book /by Sunday` + +Example of outcome: + + Deadline is added + +``` + ____________________________________________________________ + Got it. I've added this task: + [D][ ] return book (by: Sunday) + Now you have 6 tasks in the list. + ____________________________________________________________ +``` + +### `event` - add an event + +Command: `event` + +Argument: description of the event + +Example: `event project meeting /from Mon 2pm /to 4pm` + +Example of outcome: + + Event is added + +``` + ____________________________________________________________ + Got it. I've added this task: + [E][ ] project meeting (from: Mon 2pm to: 4pm) + Now you have 7 tasks in the list. + ____________________________________________________________ +``` +### `delete` - delete a task + +Command: `delete` + +Argument: ID of the task to delete + +Example: `delete 1` + +Example of outcome: + + Task is deleted + +``` + ____________________________________________________________ + Noted. I've removed this task: + [E][ ] project meeting (from: Aug 6th 2pm to: 4pm) + Now you have 4 tasks in the list. + ____________________________________________________________ +``` + +### `find` - find a task +Command: `find` + +Argument: `String` What to search for + +Example: `find book` + +Example of outcome: + + book is found + ``` +____________________________________________________________ + Here are the matching tasks in your list: + [T][ ] borrow book + [T][ ] borrow new book +____________________________________________________________ +``` \ No newline at end of file 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..281e7d70a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: chatbot.Chatbot + diff --git a/src/main/java/chatbot/Chatbot.java b/src/main/java/chatbot/Chatbot.java new file mode 100644 index 000000000..504a1f510 --- /dev/null +++ b/src/main/java/chatbot/Chatbot.java @@ -0,0 +1,65 @@ +package chatbot; + +import chatbot.command.Command; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Chatbot { + private Storage storage; + private Parser parser; + private Ui ui; + private TaskList taskList; + + public Chatbot() { + this.ui = new Ui(); + this.storage = new Storage("./tasklist.txt"); + this.parser = new Parser(); + this.taskList = new TaskList(); + } + + + /** + * Runs the main routine of the app + * + * @author Jeremy + * @since 2023-10-06 + */ + public void run() throws IOException { + this.ui.showGreetingMessage(); + + ArrayList lines = storage.parseFile(this.taskList.getTaskList()); + for(String line : lines) { + try { + Command c = this.parser.parseCommand(line); + c.execute(this.taskList.getTaskList(), false); + } catch(ChatbotUnknownCommandException | ChatbotEmptyDescException e) { + this.ui.showError(e.getMessage(), true); + continue; + } catch (Exception e) { + this.ui.showError(e.getMessage(), false); + } + } + + Scanner in = new Scanner(System.in); + String input = ""; + while(!input.equals("bye")) { + try{ + input = in.nextLine(); + Command c = this.parser.parseCommand(input); + c.execute(this.taskList.getTaskList(), true); + } catch(ChatbotUnknownCommandException | ChatbotEmptyDescException e) { + this.ui.showError(e.getMessage(), true); + continue; + } catch (Exception e) { + this.ui.showError(e.getMessage(), false); + } + } + this.ui.showByeMessage(); + } + + public static void main(String[] args) throws Exception { + new Chatbot().run(); + } +} diff --git a/src/main/java/chatbot/ChatbotEmptyDescException.java b/src/main/java/chatbot/ChatbotEmptyDescException.java new file mode 100644 index 000000000..f6d977ae5 --- /dev/null +++ b/src/main/java/chatbot/ChatbotEmptyDescException.java @@ -0,0 +1,7 @@ +package chatbot; + +public class ChatbotEmptyDescException extends Exception { + public ChatbotEmptyDescException(String exceptionMsg) { + super(exceptionMsg); + } +} diff --git a/src/main/java/chatbot/ChatbotUnknownCommandException.java b/src/main/java/chatbot/ChatbotUnknownCommandException.java new file mode 100644 index 000000000..e91860510 --- /dev/null +++ b/src/main/java/chatbot/ChatbotUnknownCommandException.java @@ -0,0 +1,7 @@ +package chatbot; + +public class ChatbotUnknownCommandException extends Exception { + public ChatbotUnknownCommandException(String exceptionMsg) { + super(exceptionMsg); + } +} diff --git a/src/main/java/chatbot/Deadline.java b/src/main/java/chatbot/Deadline.java new file mode 100644 index 000000000..37c63ca4e --- /dev/null +++ b/src/main/java/chatbot/Deadline.java @@ -0,0 +1,15 @@ +package chatbot; + +public class Deadline extends Task { + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + by + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/chatbot/Event.java b/src/main/java/chatbot/Event.java new file mode 100644 index 000000000..ff96ae806 --- /dev/null +++ b/src/main/java/chatbot/Event.java @@ -0,0 +1,16 @@ +package chatbot; + +public class Event extends Task { + protected String fromDate; + protected String toDate; + public Event(String description, String fromDate, String toDate) { + super(description); + this.fromDate = fromDate; + this.toDate = toDate; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (from: " + this.fromDate + " to: " + this.toDate + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/chatbot/Parser.java b/src/main/java/chatbot/Parser.java new file mode 100644 index 000000000..c329605c0 --- /dev/null +++ b/src/main/java/chatbot/Parser.java @@ -0,0 +1,35 @@ +package chatbot; + +import chatbot.command.*; + +public class Parser { + /** + * Parse the command and returns a Command object + * + * @param input The input to be parsed + * @author Jeremy + * @since 2023-10-06 + */ + public Command parseCommand(String input) throws ChatbotUnknownCommandException { + if (input.equals("list")) { + return new ListCommand("list", input); + } else if (input.startsWith("mark ")) { + return new MarkCommand("mark", input); + } else if (input.startsWith("unmark ")) { + return new UnmarkCommand("unmark", input); + } else if (input.startsWith("todo")) { + return new TodoCommand("todo", input); + } else if (input.startsWith("deadline")) { + return new DeadlineCommand("deadline", input); + } else if (input.startsWith("event")) { + return new EventCommand("event", input); + } else if (input.startsWith("delete")) { + return new DeleteCommand("delete", input); + } else if (input.startsWith("find")) { + return new FindCommand("find", input); + } else { + // unknown command + throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + } + } +} diff --git a/src/main/java/chatbot/Storage.java b/src/main/java/chatbot/Storage.java new file mode 100644 index 000000000..543adf029 --- /dev/null +++ b/src/main/java/chatbot/Storage.java @@ -0,0 +1,67 @@ +package chatbot; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; + + +public class Storage { + private String filepath; + public Storage(String filepath) { + this.filepath = filepath; + } + /** + * Parse the storage file and load the data + * + * @param tasks The current task list + * @author Jeremy + * @since 2023-10-06 + */ + public ArrayList parseFile(ArrayList tasks) throws IOException { + Path path = Paths.get(this.filepath); + ArrayList lines = new ArrayList<>(); + if(!Files.exists(path)) { + return lines; + } + try (BufferedReader br = new BufferedReader(new FileReader(path.toAbsolutePath().toString()))) { + String line; + while ((line = br.readLine()) != null) { + // process the line. + //System.out.println(line); + if(line.trim().isEmpty()) { + continue; + } + lines.add(line); + //parseCommand(tasks, line, false); + } + } + return lines; + } + /** + * Save input to storage file + * + * @param input The input to be saved + * @author Jeremy + * @since 2023-10-06 + */ + public void saveToFile(String input) { + String str = input + System.lineSeparator(); + try { + Path path = Paths.get(this.filepath); + //System.out.println("fullpath: " + path.toAbsolutePath().toString()); + if(Files.exists(path)) { + Files.write(path, str.getBytes(), StandardOpenOption.APPEND); + } else { + Files.write(path, str.getBytes(), StandardOpenOption.CREATE); + } + + } catch (IOException e) { + //exception handling left as an exercise for the reader + } + } +} diff --git a/src/main/java/chatbot/Task.java b/src/main/java/chatbot/Task.java new file mode 100644 index 000000000..9e9adc8e5 --- /dev/null +++ b/src/main/java/chatbot/Task.java @@ -0,0 +1,53 @@ +package chatbot; + +public class Task { + protected String description; + protected boolean isDone; + public Task(String description) { + this.description = description; + this.isDone = false; + } + + /** + * Get the description + * + * @author Jeremy + * @since 2023-10-06 + */ + public String getDescription() { + return this.description; + } + /** + * Get the status icon + * + * @author Jeremy + * @since 2023-10-06 + */ + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + /** + * Mark task as done + * + * @author Jeremy + * @since 2023-10-06 + */ + public void markAsDone() { + this.isDone = true; + } + /** + * Mark task as undone + * + * @author Jeremy + * @since 2023-10-06 + */ + public void markAsUndone() { + this.isDone = false; + } + + @Override + public String toString() { + return "[" + this.getStatusIcon() + "] " + description; + } + +} \ No newline at end of file diff --git a/src/main/java/chatbot/TaskList.java b/src/main/java/chatbot/TaskList.java new file mode 100644 index 000000000..3f3d2fb91 --- /dev/null +++ b/src/main/java/chatbot/TaskList.java @@ -0,0 +1,25 @@ +package chatbot; + +import java.util.ArrayList; + +public class TaskList { + private ArrayList tasks = new ArrayList(100); + /** + * Get the list of tasks + * + * @author Jeremy + * @since 2023-10-06 + */ + public ArrayList getTaskList() { + return this.tasks; + } + /** + * Add a task to the list of tasks + * + * @author Jeremy + * @since 2023-10-06 + */ + public void addTask(Task task) { + this.tasks.add(task); + } +} diff --git a/src/main/java/chatbot/Todo.java b/src/main/java/chatbot/Todo.java new file mode 100644 index 000000000..8d052ade3 --- /dev/null +++ b/src/main/java/chatbot/Todo.java @@ -0,0 +1,14 @@ +package chatbot; + +import chatbot.Task; + +public class Todo extends Task { + public Todo(String description) { + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/chatbot/Ui.java b/src/main/java/chatbot/Ui.java new file mode 100644 index 000000000..11e4c2d23 --- /dev/null +++ b/src/main/java/chatbot/Ui.java @@ -0,0 +1,152 @@ +package chatbot; + +import java.util.ArrayList; + +public class Ui { + /** + * Show greeting message when app first launches. + * + * @author Jeremy + * @since 2023-10-06 + */ + public void showGreetingMessage() { + String greetingMsg = "____________________________________________________________\n" + + " Hello! I'm Chatbot\n" + + " What can I do for you?\n" + + "____________________________________________________________\n"; + System.out.println(greetingMsg); + } + /** + * Show bye message when app is closing + * + * @author Jeremy + * @since 2023-10-06 + */ + public void showByeMessage() { + String byeMsg = "____________________________________________________________\n" + + " Bye. Hope to see you again soon!\n" + + "____________________________________________________________\n"; + System.out.println(byeMsg); + } + /** + * Show an error message, with additional formatting possible + * + * @param message the message to be displayed + * @param addLines whether to wrap the message between to horizontal dashed-lines + * @author Jeremy + * @since 2023-10-06 + */ + public void showError(String message, boolean addLines) { + if( addLines ) { + System.out.println("____________________________________________________________"); + } + System.out.println("Unknown exception. Error message: " + message); + if( addLines ) { + System.out.println("____________________________________________________________"); + } + } + /** + * Print the list of tasks + * + * @param tasks the list of tasks to be printed + * @author Jeremy + * @since 2023-10-06 + */ + public void printList(ArrayList tasks) { + System.out.println("____________________________________________________________"); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + //System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); + System.out.println(" " + (i + 1) + "." + tasks.get(i)); + } + System.out.println("____________________________________________________________"); + } + /** + * Print the result of the `mark` command + * + * @param tasks the current list of tasks + * @param markTaskNo the task number that was marked by the user + * @author Jeremy + * @since 2023-10-06 + */ + public void printMarkResult(ArrayList tasks, int markTaskNo) { + System.out.println("____________________________________________________________"); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" [" + tasks.get(markTaskNo - 1).getStatusIcon() + "] " + tasks.get(markTaskNo - 1).getDescription()); + System.out.println("____________________________________________________________"); + } + /** + * Print the result of the `unmark` command + * + * @param tasks the current list of tasks + * @param unmarkedTaskNo the task number that was unmarked by the user + * @author Jeremy + * @since 2023-10-06 + */ + public void printUnmarkResult(ArrayList tasks, int unmarkedTaskNo) { + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println(" [" + tasks.get(unmarkedTaskNo - 1).getStatusIcon() + "] " + tasks.get(unmarkedTaskNo - 1).getDescription()); + System.out.println("____________________________________________________________"); + } + /** + * Print the result of the `todo` command + * + * @param tasks the current list of tasks + * @param task the task that was added + * @author Jeremy + * @since 2023-10-06 + */ + public void printTodoResult(ArrayList tasks, Task task) { + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + /** + * Print the result of the `deadline` command + * + * @param tasks the current list of tasks + * @param task the task that was added + * @author Jeremy + * @since 2023-10-06 + */ + public void printDeadlineResult(ArrayList tasks, Task task) { + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + /** + * Print the result of the `event` command + * + * @param tasks the current list of tasks + * @param task the task that was added + * @author Jeremy + * @since 2023-10-06 + */ + public void printEventResult(ArrayList tasks, Task task) { + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } + /** + * Print the result of the `delete` command + * + * @param tasks the current list of tasks + * @param task the task that was added + * @author Jeremy + * @since 2023-10-06 + */ + public void printDeleteResult(ArrayList tasks, Task task) { + System.out.println("____________________________________________________________"); + System.out.println(" Noted. I've removed this task: "); + System.out.println(" " + task); + System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list."); + System.out.println("____________________________________________________________"); + } +} diff --git a/src/main/java/chatbot/command/AbstractCommand.java b/src/main/java/chatbot/command/AbstractCommand.java new file mode 100644 index 000000000..c38f5f3a3 --- /dev/null +++ b/src/main/java/chatbot/command/AbstractCommand.java @@ -0,0 +1,19 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.ChatbotUnknownCommandException; +import chatbot.Task; + +import java.util.ArrayList; + +public abstract class AbstractCommand { + /** + * Abstract method to execute the command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public abstract void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException; +} diff --git a/src/main/java/chatbot/command/Command.java b/src/main/java/chatbot/command/Command.java new file mode 100644 index 000000000..38ae76157 --- /dev/null +++ b/src/main/java/chatbot/command/Command.java @@ -0,0 +1,30 @@ +package chatbot.command; + +import chatbot.*; + +import java.util.ArrayList; + +public class Command extends AbstractCommand { + protected Storage storage; + protected String commandType; + protected String input; + protected Ui ui; + public Command(String commandType, String input) { + this.storage = new Storage("./tasklist.txt"); + this.commandType = commandType; + this.input = input; + this.ui = new Ui(); + } + /** + * Execute the command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + return; + } + +} diff --git a/src/main/java/chatbot/command/DeadlineCommand.java b/src/main/java/chatbot/command/DeadlineCommand.java new file mode 100644 index 000000000..9969af911 --- /dev/null +++ b/src/main/java/chatbot/command/DeadlineCommand.java @@ -0,0 +1,43 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Deadline; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class DeadlineCommand extends Command { + public DeadlineCommand(String commandType, String input) { + super(commandType, input); + } + /** + * Execute the `deadline` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String msg = input.replace("deadline", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); + } + String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate + if (byDate.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /by argument of a deadline cannot be empty."); + } + String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description + if (desc.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty."); + } + + Task task = new Deadline(desc, byDate); + tasks.add(task); + if (isUserInput) { + storage.saveToFile(input); + ui.printDeadlineResult(tasks, task); + } + } +} diff --git a/src/main/java/chatbot/command/DeleteCommand.java b/src/main/java/chatbot/command/DeleteCommand.java new file mode 100644 index 000000000..5d965b9e5 --- /dev/null +++ b/src/main/java/chatbot/command/DeleteCommand.java @@ -0,0 +1,37 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class DeleteCommand extends Command { + public DeleteCommand(String commandType, String input) { + super(commandType, input); + } + /** + * Execute the `delete` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String msg = input.replace("delete", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of delete cannot be empty."); + } + int indexToRemove = Integer.parseInt(msg); + if( indexToRemove > 0 ) { + indexToRemove -= 1; + } + Task task = tasks.get(indexToRemove); + tasks.remove(indexToRemove); + if (isUserInput) { + storage.saveToFile(input); + ui.printDeleteResult(tasks, task); + } + } +} diff --git a/src/main/java/chatbot/command/EventCommand.java b/src/main/java/chatbot/command/EventCommand.java new file mode 100644 index 000000000..18a14b170 --- /dev/null +++ b/src/main/java/chatbot/command/EventCommand.java @@ -0,0 +1,49 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Event; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class EventCommand extends Command { + public EventCommand(String commandType, String input) { + super(commandType, input); + } + /** + * Execute the `event` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String msg = input.replace("event", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty."); + } + + String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim(); + if (dateRange.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The date range of a event cannot be empty."); + } + String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim(); + if (fromDate.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /from argument cannot be empty."); + } + String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim(); + if (toDate.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The /to argument cannot be empty."); + } + String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description + + Task task = new Event(desc, fromDate, toDate); + tasks.add(task); + if (isUserInput) { + storage.saveToFile(input); + ui.printEventResult(tasks, task); + } + } +} diff --git a/src/main/java/chatbot/command/FindCommand.java b/src/main/java/chatbot/command/FindCommand.java new file mode 100644 index 000000000..ba0fad89d --- /dev/null +++ b/src/main/java/chatbot/command/FindCommand.java @@ -0,0 +1,36 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class FindCommand extends Command { + public FindCommand(String commandType, String input) { + super(commandType, input); + } + /** + * Execute the `find` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String searchFor = input.replace("find", "").trim(); + if (searchFor.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The find command needs a string to search for."); + } + System.out.println("____________________________________________________________"); + System.out.println(" Here are the matching tasks in your list:"); + for(Task task : tasks) { + String desc = task.getDescription(); + if(desc.contains(searchFor)) { + System.out.println(" " + task); + } + } + System.out.println("____________________________________________________________"); + } +} diff --git a/src/main/java/chatbot/command/ListCommand.java b/src/main/java/chatbot/command/ListCommand.java new file mode 100644 index 000000000..b77e8a9f9 --- /dev/null +++ b/src/main/java/chatbot/command/ListCommand.java @@ -0,0 +1,22 @@ +package chatbot.command; + +import chatbot.Task; + +import java.util.ArrayList; + +public class ListCommand extends Command { + public ListCommand(String commandType, String input) { + super(commandType, input); + } + /** + * Execute the `list` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public void execute(ArrayList tasks, boolean isUserInput) { + this.ui.printList(tasks); + } +} diff --git a/src/main/java/chatbot/command/MarkCommand.java b/src/main/java/chatbot/command/MarkCommand.java new file mode 100644 index 000000000..ce9829ec7 --- /dev/null +++ b/src/main/java/chatbot/command/MarkCommand.java @@ -0,0 +1,30 @@ +package chatbot.command; + +import chatbot.Task; + +import java.util.ArrayList; + +public class MarkCommand extends Command { + public MarkCommand(String commandType, String input) { + super(commandType, input); + } + /** + * Execute the `mark` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public void execute(ArrayList tasks, boolean isUserInput) { + String number = input.replace("mark ", "").trim(); + int markTaskNo = Integer.parseInt(number); + if (markTaskNo > 0) { + tasks.get(markTaskNo - 1).markAsDone(); + } + if (isUserInput) { + storage.saveToFile(input); + ui.printMarkResult(tasks, markTaskNo); + } + } +} diff --git a/src/main/java/chatbot/command/TodoCommand.java b/src/main/java/chatbot/command/TodoCommand.java new file mode 100644 index 000000000..ca7ea92dc --- /dev/null +++ b/src/main/java/chatbot/command/TodoCommand.java @@ -0,0 +1,35 @@ +package chatbot.command; + +import chatbot.ChatbotEmptyDescException; +import chatbot.Task; +import chatbot.Todo; + +import java.util.ArrayList; + +public class TodoCommand extends Command { + public TodoCommand(String commandType, String input) { + super(commandType, input); + } + /** + * Execute the `todo` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public void execute(ArrayList tasks, boolean isUserInput) throws ChatbotEmptyDescException { + String msg = input.replace("todo", "").trim(); + if (msg.isEmpty()) { + throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty."); + } + + Task task = new Todo(msg); + tasks.add(task); + + if (isUserInput) { + storage.saveToFile(input); + ui.printTodoResult(tasks, task); + } + } +} diff --git a/src/main/java/chatbot/command/UnmarkCommand.java b/src/main/java/chatbot/command/UnmarkCommand.java new file mode 100644 index 000000000..4f4f39831 --- /dev/null +++ b/src/main/java/chatbot/command/UnmarkCommand.java @@ -0,0 +1,30 @@ +package chatbot.command; + +import chatbot.Task; + +import java.util.ArrayList; + +public class UnmarkCommand extends Command { + public UnmarkCommand(String commandType, String input) { + super(commandType, input); + } + /** + * Execute the `unmark` command specified by the user + * + * @param tasks the current list of tasks + * @param isUserInput is the command a user input + * @author Jeremy + * @since 2023-10-06 + */ + public void execute(ArrayList tasks, boolean isUserInput) { + String number = input.replace("unmark ", "").trim(); + int unmarkedTaskNo = Integer.parseInt(number); + if (unmarkedTaskNo > 0) { + tasks.get(unmarkedTaskNo - 1).markAsUndone(); + } + if (isUserInput) { + storage.saveToFile(input); + ui.printUnmarkResult(tasks, unmarkedTaskNo); + } + } +} diff --git a/tasklist.txt b/tasklist.txt new file mode 100644 index 000000000..d790807e5 --- /dev/null +++ b/tasklist.txt @@ -0,0 +1,23 @@ +event jeremy attend CS2113 lecture Friday 22 Sep 2023 /from 4 /to 6pm +deadline jeremy submit UG draft /by today 2359 +delete 2 +todo borrow book +todo borrow new book +todo 111 +todo aasdf +mark 1 +unmark 1 +mark 3 +unmark 3 +mark 5 +delete 1 +todo borrow book +event /from 23oct /to 23oct go to hall +deadline return book /by Sunday +event project meeting /from Mon 2pm /to 4pm +mark 8 +unmark 8 +delete 8 +todo borrow book +deadline aaaa /by Sat +deadline aaa2 /by