diff --git a/WindeTasks.txt b/WindeTasks.txt new file mode 100644 index 0000000000..e8a090a47c --- /dev/null +++ b/WindeTasks.txt @@ -0,0 +1 @@ +T | O | something diff --git a/src/main/java/commands/AddDeadline.java b/src/main/java/commands/AddDeadline.java index 2d5f962676..861577ebc6 100644 --- a/src/main/java/commands/AddDeadline.java +++ b/src/main/java/commands/AddDeadline.java @@ -2,8 +2,10 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import exceptions.EmptyDescriptionException; +import exceptions.InvalidDateFormatException; import tasks.Deadline; import windebot.History; import windebot.Reminder; @@ -28,22 +30,27 @@ public class AddDeadline extends Command { */ public boolean execute(String input, Reminder reminder, Ui ui, History history) - throws EmptyDescriptionException { + throws EmptyDescriptionException, InvalidDateFormatException { String[] command = input.split(" ", 2); String[] order = command[1].split(" /by "); - if (order.length == 2) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"); - LocalDateTime deadline = LocalDateTime.parse(order[1], formatter); - Deadline deadlineTask = new Deadline(order[0], deadline); - reminder.addDeadline(deadlineTask); - ui.print("Got it. I've added this task:"); - ui.print(" " + deadlineTask.toString()); - ui.print("Now you have " + reminder.size() + " tasks in the list."); - history.save(reminder.getSchedule()); - } else { - throw new EmptyDescriptionException("WHEN DEADLINE END!"); + String taskDescription = order[0].trim(); + try { + if (!(taskDescription.equals("")) && (order.length == 3)) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"); + LocalDateTime deadline = LocalDateTime.parse(order[1], formatter); + Deadline deadlineTask = new Deadline(taskDescription, deadline); + reminder.addDeadline(deadlineTask); + ui.print("Got it. I've added this task:"); + ui.print(" " + deadlineTask.toString()); + ui.print("Now you have " + reminder.size() + " tasks in the list."); + history.save(reminder.getSchedule()); + } else { + throw new EmptyDescriptionException("ADD THE CORRECT PARAMETERS!"); + } + return true; + } catch (DateTimeParseException e) { + throw new InvalidDateFormatException("FOLLOW THE CORRECT DATE FORMAT: DD/MM/YYYY HH:MM"); } - return true; } /** diff --git a/src/main/java/commands/AddEvent.java b/src/main/java/commands/AddEvent.java index d7917cbe9f..8d48004edc 100644 --- a/src/main/java/commands/AddEvent.java +++ b/src/main/java/commands/AddEvent.java @@ -2,8 +2,10 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import exceptions.EmptyDescriptionException; +import exceptions.InvalidDateFormatException; import tasks.Event; import windebot.History; import windebot.Reminder; @@ -29,25 +31,28 @@ public class AddEvent extends Command { */ public boolean execute(String input, Reminder reminder, Ui ui, History history) - throws EmptyDescriptionException { + throws EmptyDescriptionException, InvalidDateFormatException { String[] command = input.split(" ", 2); - String[] order = command[1].split(" /from "); - if (order.length == 2) { - String[] fillerName = order[1].split(" /to "); - DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"); - DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm"); - LocalDateTime start = LocalDateTime.parse(fillerName[0], dateFormatter); - LocalDateTime end = LocalDateTime.parse(fillerName[1], dateFormatter); - Event eventTask = new Event(order[0], start, end); - reminder.addEvent(eventTask); - ui.print("Got it. I've added this task:"); - ui.print(" " + eventTask.toString()); - ui.print("Now you have " + reminder.size() + " tasks in the list."); - history.save(reminder.getSchedule()); - } else { - throw new EmptyDescriptionException("WHEN EVENT DATE!"); + String[] order = command[1].split(" /from | /to "); + String taskDescription = order[0].trim(); + try { + if (!(taskDescription.equals("")) && (order.length == 3)) { + DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"); + LocalDateTime start = LocalDateTime.parse(order[1], dateFormatter); + LocalDateTime end = LocalDateTime.parse(order[2], dateFormatter); + Event eventTask = new Event(taskDescription, start, end); + reminder.addEvent(eventTask); + ui.print("Got it. I've added this task:"); + ui.print(" " + eventTask.toString()); + ui.print("Now you have " + reminder.size() + " tasks in the list."); + history.save(reminder.getSchedule()); + } else { + throw new EmptyDescriptionException("ADD THE CORRECT PARAMETERS!"); + } + return true; + } catch (DateTimeParseException e) { + throw new InvalidDateFormatException("FOLLOW THE CORRECT DATE FORMAT: DD/MM/YYYY HH:MM"); } - return true; } /** diff --git a/src/main/java/commands/AddMark.java b/src/main/java/commands/AddMark.java index 441f1f2d24..441fdcddc4 100644 --- a/src/main/java/commands/AddMark.java +++ b/src/main/java/commands/AddMark.java @@ -1,6 +1,8 @@ package commands; import exceptions.EmptyDescriptionException; +import exceptions.IndexOutBoundsException; +import exceptions.NotIntegerException; import exceptions.TooManyParametersException; import windebot.History; import windebot.Reminder; @@ -24,23 +26,33 @@ public class AddMark extends Command { * @return true if the command was executed successfully. * @throws EmptyDescriptionException If no index is provided in the input. * @throws TooManyParametersException If too many parameters are provided in the input. + * @throws NotIntegerException If no integer is provided in the input. + * @throws IndexOutBoundsException If integer provided is out of bounds. */ public boolean execute(String input, Reminder reminder, Ui ui, History history) - throws EmptyDescriptionException, TooManyParametersException { + throws EmptyDescriptionException, TooManyParametersException, + NotIntegerException, IndexOutBoundsException { String[] command = input.split(" "); assert(command.length == 2); - if (command.length == 2) { - ui.print("Nice! I've marked this task as done:"); - reminder.mark(Integer.parseInt(command[1]) - 1); - ui.print(" " + reminder.getTask(Integer.parseInt(command[1]) - 1).toString()); - history.save(reminder.getSchedule()); - } else if (command.length < 2) { - throw new EmptyDescriptionException("I NEED TO KNOW WHAT I'M MARKING!"); - } else { - throw new TooManyParametersException("ONE AT A TIME!"); + try { + int index = Integer.parseInt(command[1].trim()); + if (command.length == 2) { + ui.print("Nice! I've marked this task as done:"); + reminder.mark(index - 1); + ui.print(" " + reminder.getTask(Integer.parseInt(command[1]) - 1).toString()); + history.save(reminder.getSchedule()); + } else if (command.length < 2) { + throw new EmptyDescriptionException("I NEED TO KNOW WHAT I'M MARKING!"); + } else { + throw new TooManyParametersException("ONE AT A TIME!"); + } + return true; + } catch (NumberFormatException e) { + throw new NotIntegerException("THAT IS NOT AN INTEGER!"); + } catch (StringIndexOutOfBoundsException e) { + throw new IndexOutBoundsException("THIS IS NOT A VALID TASK NUMBER!"); } - return true; } /** diff --git a/src/main/java/commands/AddTodo.java b/src/main/java/commands/AddTodo.java index 220fb6e3fd..c013052b3d 100644 --- a/src/main/java/commands/AddTodo.java +++ b/src/main/java/commands/AddTodo.java @@ -28,8 +28,9 @@ public class AddTodo extends Command { public boolean execute(String input, Reminder reminder, Ui ui, History history) throws EmptyDescriptionException { String[] command = input.split(" ", 2); - if (command.length == 2) { - Todos todoTask = new Todos(command[1]); + String taskDescription = command[1].trim(); + if (!(taskDescription.equals(""))) { + Todos todoTask = new Todos(taskDescription); reminder.addTodo(todoTask); ui.print("Got it. I've added this task:"); ui.print(" " + todoTask.toString()); diff --git a/src/main/java/commands/AddUnmark.java b/src/main/java/commands/AddUnmark.java index 5258fb5377..9d6ff39852 100644 --- a/src/main/java/commands/AddUnmark.java +++ b/src/main/java/commands/AddUnmark.java @@ -1,6 +1,8 @@ package commands; import exceptions.EmptyDescriptionException; +import exceptions.IndexOutBoundsException; +import exceptions.NotIntegerException; import exceptions.TooManyParametersException; import windebot.History; import windebot.Reminder; @@ -17,30 +19,40 @@ public class AddUnmark extends Command { /** * Executes the AddUnmark command, marking a task as not completed. * - * @param input The user input string containing the index of the task to be unmarked. + * @param input The user input string containing the index of the task to be marked. * @param reminder The Reminder object that manages the task list. * @param ui The Ui object used to interact with the user. * @param history The History object used to save the data * @return true if the command was executed successfully. * @throws EmptyDescriptionException If no index is provided in the input. * @throws TooManyParametersException If too many parameters are provided in the input. + * @throws NotIntegerException If no integer is provided in the input. + * @throws IndexOutBoundsException If integer provided is out of bounds. */ public boolean execute(String input, Reminder reminder, Ui ui, History history) - throws EmptyDescriptionException, TooManyParametersException { + throws EmptyDescriptionException, TooManyParametersException, + NotIntegerException, IndexOutBoundsException { String[] command = input.split(" "); assert(command.length == 2); - if (command.length == 2) { - ui.print("OK, I've marked this task as not done yet:"); - reminder.unmark(Integer.parseInt(command[1]) - 1); - ui.print(" " + reminder.getTask(Integer.parseInt(command[1]) - 1).toString()); - history.save(reminder.getSchedule()); - } else if (command.length < 2) { - throw new EmptyDescriptionException("I NEED TO KNOW WHAT I'M MARKING!"); - } else { - throw new TooManyParametersException("ONE AT A TIME!"); + try { + int index = Integer.parseInt(command[1].trim()); + if (command.length == 2) { + ui.print("Ok! I've marked this task as not done:"); + reminder.unmark(index - 1); + ui.print(" " + reminder.getTask(Integer.parseInt(command[1]) - 1).toString()); + history.save(reminder.getSchedule()); + } else if (command.length < 2) { + throw new EmptyDescriptionException("I NEED TO KNOW WHAT I'M MARKING!"); + } else { + throw new TooManyParametersException("ONE AT A TIME!"); + } + return true; + } catch (NumberFormatException e) { + throw new NotIntegerException("THAT IS NOT AN INTEGER!"); + } catch (StringIndexOutOfBoundsException e) { + throw new IndexOutBoundsException("THIS IS NOT A VALID TASK NUMBER!"); } - return true; } /** diff --git a/src/main/java/commands/ByeCommand.java b/src/main/java/commands/ByeCommand.java index 8ed7ed75ef..e1f8ca0218 100644 --- a/src/main/java/commands/ByeCommand.java +++ b/src/main/java/commands/ByeCommand.java @@ -22,6 +22,7 @@ public class ByeCommand extends Command { */ public boolean execute(String input, Reminder reminder, Ui ui, History history) { + ui.print("Bye! Hope to see you again soon!"); return false; } diff --git a/src/main/java/commands/Command.java b/src/main/java/commands/Command.java index 615a3218b5..5d0e3094e9 100644 --- a/src/main/java/commands/Command.java +++ b/src/main/java/commands/Command.java @@ -1,6 +1,9 @@ package commands; import exceptions.EmptyDescriptionException; +import exceptions.IndexOutBoundsException; +import exceptions.InvalidDateFormatException; +import exceptions.NotIntegerException; import exceptions.TooManyParametersException; import windebot.History; import windebot.Reminder; @@ -27,7 +30,8 @@ public abstract class Command { */ public abstract boolean execute(String input, Reminder reminder, Ui ui, History history) - throws EmptyDescriptionException, TooManyParametersException; + throws EmptyDescriptionException, TooManyParametersException, + NotIntegerException, IndexOutBoundsException, InvalidDateFormatException; /** * Exits the chatbot diff --git a/src/main/java/exceptions/IndexOutBoundsException.java b/src/main/java/exceptions/IndexOutBoundsException.java new file mode 100644 index 0000000000..ad8bd0e8fc --- /dev/null +++ b/src/main/java/exceptions/IndexOutBoundsException.java @@ -0,0 +1,17 @@ +package exceptions; + +/** + * The IndexOutBoundsException is thrown when the Index is out of bounds. + * This exception is typically used in commands that require Integer input. + */ +public class IndexOutBoundsException extends Exception { + + /** + * Constructs a new IndexOutBoundsException with the specified detail message. + * + * @param message The detail message explaining the reason for the exception. + */ + public IndexOutBoundsException(String message) { + super(message); + } +} diff --git a/src/main/java/exceptions/InvalidDateFormatException.java b/src/main/java/exceptions/InvalidDateFormatException.java new file mode 100644 index 0000000000..c34597ae0c --- /dev/null +++ b/src/main/java/exceptions/InvalidDateFormatException.java @@ -0,0 +1,17 @@ +package exceptions; + +/** + * The InvalidDateFormatException is thrown when a String cannot be formatted to DateTime. + * This exception is typically used in commands that require Integer input. + */ +public class InvalidDateFormatException extends Exception { + + /** + * Constructs a new InvalidDateFormatException with the specified detail message. + * + * @param message The detail message explaining the reason for the exception. + */ + public InvalidDateFormatException(String message) { + super(message); + } +} diff --git a/src/main/java/exceptions/NotIntegerException.java b/src/main/java/exceptions/NotIntegerException.java new file mode 100644 index 0000000000..f8d5aca4bc --- /dev/null +++ b/src/main/java/exceptions/NotIntegerException.java @@ -0,0 +1,17 @@ +package exceptions; + +/** + * The NotIntegerException is thrown when a String cannot be formatted to Integer. + * This exception is typically used in commands that require Integer input. + */ +public class NotIntegerException extends Exception { + + /** + * Constructs a new NotIntegerException with the specified detail message. + * + * @param message The detail message explaining the reason for the exception. + */ + public NotIntegerException(String message) { + super(message); + } +} diff --git a/src/main/java/windebot/Parser.java b/src/main/java/windebot/Parser.java index b5e17d277d..5f6bd21330 100644 --- a/src/main/java/windebot/Parser.java +++ b/src/main/java/windebot/Parser.java @@ -90,29 +90,29 @@ public static Command parse(String input) throws UnsupportedCommandException { */ private static SubCommandType getCommandType(String input) { - if (input.startsWith("todo")) { + if (input.startsWith("todo ")) { return SubCommandType.TODO; - } else if (input.startsWith("deadline")) { + } else if (input.startsWith("deadline ")) { return SubCommandType.DEADLINE; - } else if (input.startsWith("event")) { + } else if (input.startsWith("event ")) { return SubCommandType.EVENT; } else if (input.equals("list")) { return SubCommandType.LIST; - } else if (input.startsWith("delete")) { + } else if (input.startsWith("delete ")) { return SubCommandType.DELETE; } else if (input.equals("bye")) { return SubCommandType.BYE; - } else if (input.startsWith("mark")) { + } else if (input.startsWith("mark ")) { return SubCommandType.MARK; - } else if (input.startsWith("unmark")) { + } else if (input.startsWith("unmark ")) { return SubCommandType.UNMARK; - } else if (input.startsWith("date")) { + } else if (input.startsWith("date ")) { return SubCommandType.DATE; - } else if (input.startsWith("find")) { + } else if (input.startsWith("find ")) { return SubCommandType.FIND; - } else if (input.startsWith("change") || input.startsWith("cutoff") - || input.startsWith("start") || input.startsWith("end") - || input.startsWith("action")) { + } else if (input.startsWith("change ") || input.startsWith("cutoff ") + || input.startsWith("start ") || input.startsWith("end ") + || input.startsWith("action ")) { return SubCommandType.CHANGE; } else { return SubCommandType.UNKNOWN; diff --git a/src/main/java/windebot/Winde.java b/src/main/java/windebot/Winde.java index 532729d5f4..44813f8348 100644 --- a/src/main/java/windebot/Winde.java +++ b/src/main/java/windebot/Winde.java @@ -1,9 +1,9 @@ package windebot; import commands.Command; -import exceptions.EmptyDescriptionException; -import exceptions.TooManyParametersException; -import exceptions.UnsupportedCommandException; +import exceptions.*; + +import java.time.format.DateTimeParseException; /** * The Winde class is the main class that runs the WindeBot chatbot. @@ -60,6 +60,12 @@ public String getResponse(String input) { throw new RuntimeException(e); } catch (TooManyParametersException e) { throw new RuntimeException(e); + } catch (NotIntegerException e) { + throw new RuntimeException(e); + } catch (IndexOutBoundsException e) { + throw new RuntimeException(e); + } catch (InvalidDateFormatException e) { + throw new RuntimeException(e); } }