Skip to content

Commit

Permalink
Add more exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
WinstonJin committed Sep 20, 2024
1 parent e10edbf commit ea954eb
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 70 deletions.
1 change: 1 addition & 0 deletions WindeTasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
T | O | something
33 changes: 20 additions & 13 deletions src/main/java/commands/AddDeadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down
39 changes: 22 additions & 17 deletions src/main/java/commands/AddEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down
34 changes: 23 additions & 11 deletions src/main/java/commands/AddMark.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package commands;

import exceptions.EmptyDescriptionException;
import exceptions.IndexOutBoundsException;
import exceptions.NotIntegerException;
import exceptions.TooManyParametersException;
import windebot.History;
import windebot.Reminder;
Expand All @@ -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;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/commands/AddTodo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
36 changes: 24 additions & 12 deletions src/main/java/commands/AddUnmark.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package commands;

import exceptions.EmptyDescriptionException;
import exceptions.IndexOutBoundsException;
import exceptions.NotIntegerException;
import exceptions.TooManyParametersException;
import windebot.History;
import windebot.Reminder;
Expand All @@ -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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/commands/ByeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/commands/Command.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/exceptions/IndexOutBoundsException.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
17 changes: 17 additions & 0 deletions src/main/java/exceptions/InvalidDateFormatException.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
17 changes: 17 additions & 0 deletions src/main/java/exceptions/NotIntegerException.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
22 changes: 11 additions & 11 deletions src/main/java/windebot/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/windebot/Winde.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit ea954eb

Please sign in to comment.