Skip to content

Commit

Permalink
More exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
WinstonJin committed Sep 20, 2024
1 parent b3dce62 commit 4c256df
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 207 deletions.
17 changes: 12 additions & 5 deletions src/main/java/commands/AddDeadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import exceptions.EmptyDescriptionException;
import exceptions.InvalidDateFormatException;
import exceptions.TooManyParametersException;
import tasks.Deadline;
import windebot.History;
import windebot.Reminder;
Expand All @@ -30,12 +31,12 @@ public class AddDeadline extends Command {
*/

public boolean execute(String input, Reminder reminder, Ui ui, History history)
throws EmptyDescriptionException, InvalidDateFormatException {
throws EmptyDescriptionException, InvalidDateFormatException, TooManyParametersException {
String[] command = input.split(" ", 2);
String[] order = command[1].split(" /by ");
String taskDescription = order[0].trim();
try {
if (!(taskDescription.equals("")) && (order.length == 3)) {
if (!(taskDescription.equals("")) && (order.length == 2)) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime deadline = LocalDateTime.parse(order[1], formatter);
Deadline deadlineTask = new Deadline(taskDescription, deadline);
Expand All @@ -44,13 +45,19 @@ public boolean execute(String input, Reminder reminder, Ui ui, History history)
ui.print(" " + deadlineTask.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else if (order.length > 2) {
throw new TooManyParametersException();
} else {
throw new EmptyDescriptionException("ADD THE CORRECT PARAMETERS!");
throw new EmptyDescriptionException();
}
return true;
} catch (DateTimeParseException e) {
throw new InvalidDateFormatException("FOLLOW THE CORRECT DATE FORMAT: DD/MM/YYYY HH:MM");
ui.invalidDateFormatMessage();
} catch (EmptyDescriptionException e) {
ui.emptyDescriptionMessage();
} catch (TooManyParametersException e) {
ui.tooManyParametersMessage();
}
return true;
}

/**
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/commands/AddEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import exceptions.EmptyDescriptionException;
import exceptions.InvalidDateFormatException;
import exceptions.TooManyParametersException;
import tasks.Event;
import windebot.History;
import windebot.Reminder;
Expand All @@ -27,11 +28,11 @@ public class AddEvent extends Command {
* @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 the input is incomplete or incorrectly formatted.
* @throws InvalidDateFormatException If the input date is incorrectly formatted.
*/

public boolean execute(String input, Reminder reminder, Ui ui, History history)
throws EmptyDescriptionException, InvalidDateFormatException {
throws EmptyDescriptionException, InvalidDateFormatException, TooManyParametersException {
String[] command = input.split(" ", 2);
String[] order = command[1].split(" /from | /to ");
String taskDescription = order[0].trim();
Expand All @@ -46,13 +47,19 @@ public boolean execute(String input, Reminder reminder, Ui ui, History history)
ui.print(" " + eventTask.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else if (order.length > 3) {
throw new TooManyParametersException();
} else {
throw new EmptyDescriptionException("ADD THE CORRECT PARAMETERS!");
throw new EmptyDescriptionException();
}
return true;
} catch (DateTimeParseException e) {
throw new InvalidDateFormatException("FOLLOW THE CORRECT DATE FORMAT: DD/MM/YYYY HH:MM");
ui.invalidDateFormatMessage();
} catch (EmptyDescriptionException e) {
ui.emptyDescriptionMessage();
} catch (TooManyParametersException e) {
ui.tooManyParametersMessage();
}
return true;
}

/**
Expand Down
35 changes: 19 additions & 16 deletions src/main/java/commands/AddMark.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import exceptions.EmptyDescriptionException;
import exceptions.IndexOutBoundsException;
import exceptions.NotIntegerException;
import exceptions.TooManyParametersException;
import windebot.History;
import windebot.Reminder;
import windebot.Ui;
Expand All @@ -25,34 +24,38 @@ public class AddMark extends Command {
* @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,
NotIntegerException, IndexOutBoundsException {
String[] command = input.split(" ");
assert(command.length == 2);
throws NotIntegerException, IndexOutBoundsException, EmptyDescriptionException {
try {
int index = Integer.parseInt(command[1].trim());
if (command.length == 2) {
ui.print("Nice! I've marked this task as done:");
String[] command = input.split(" ", 2);
if (command.length < 2) {
throw new EmptyDescriptionException();
}
String search = command[1].trim();
if (!(search.equals(""))) {
int index = Integer.parseInt(command[1].trim());
if (index > reminder.size() + 1) {
throw new IndexOutOfBoundsException();
}
reminder.mark(index - 1);
ui.print("Nice! I've marked this task as done:");
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!");
throw new EmptyDescriptionException();
}
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!");
ui.notIntegerMessage();
} catch (IndexOutOfBoundsException e) {
ui.indexOutBoundsMessage();
} catch (EmptyDescriptionException e) {
ui.emptyDescriptionMessage();
}
return true;
}

/**
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/commands/AddTodo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,24 @@ public class AddTodo extends Command {

public boolean execute(String input, Reminder reminder, Ui ui, History history)
throws EmptyDescriptionException {
String[] command = input.split(" ", 2);
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());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else {
throw new EmptyDescriptionException("I NEED TO KNOW WHAT I'M TODO-ING!");
try {
String[] command = input.split(" ", 2);
if (command.length < 2) {
throw new EmptyDescriptionException();
}
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());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else {
throw new EmptyDescriptionException();
}
} catch (EmptyDescriptionException e) {
ui.emptyDescriptionMessage();
}
return true;
}
Expand Down
35 changes: 19 additions & 16 deletions src/main/java/commands/AddUnmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import exceptions.EmptyDescriptionException;
import exceptions.IndexOutBoundsException;
import exceptions.NotIntegerException;
import exceptions.TooManyParametersException;
import windebot.History;
import windebot.Reminder;
import windebot.Ui;
Expand All @@ -25,34 +24,38 @@ public class AddUnmark extends Command {
* @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,
NotIntegerException, IndexOutBoundsException {
String[] command = input.split(" ");
assert(command.length == 2);
throws NotIntegerException, IndexOutBoundsException, EmptyDescriptionException {
try {
int index = Integer.parseInt(command[1].trim());
if (command.length == 2) {
ui.print("Ok! I've marked this task as not done:");
String[] command = input.split(" ", 2);
if (command.length < 2) {
throw new EmptyDescriptionException();
}
String search = command[1].trim();
if (!(search.equals(""))) {
int index = Integer.parseInt(command[1].trim());
if (index > reminder.size() + 1) {
throw new IndexOutOfBoundsException();
}
reminder.unmark(index - 1);
ui.print("Ok! I've marked this task as not done:");
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!");
throw new EmptyDescriptionException();
}
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!");
ui.notIntegerMessage();
} catch (IndexOutOfBoundsException e) {
ui.indexOutBoundsMessage();
} catch (EmptyDescriptionException e) {
ui.emptyDescriptionMessage();
}
return true;
}

/**
Expand Down
106 changes: 56 additions & 50 deletions src/main/java/commands/ChangeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,63 @@ public class ChangeCommand extends Command {

public boolean execute(String input, Reminder reminder, Ui ui, History history)
throws EmptyDescriptionException {
String[] command = input.split(" ", 2);
assert(command.length == 2);
int toChange = reminder.getSelected();
Task task = reminder.getSchedule().get(toChange);
if (command[0].equalsIgnoreCase("change")) {
toChange = Integer.parseInt(command[1]) - 1;
reminder.changeSelected(toChange);
task = reminder.getSchedule().get(toChange);
ui.print("Got it. You want to edit this task:");
ui.print(" " + task.toString());
ui.print("To change the task description, input: action (task description)");
if (task.getClass() == Deadline.class) {
ui.print("To change the deadline, input: cutoff (new deadline)");
} else if (task.getClass() == Event.class) {
ui.print("To change the start date, input: start (new start)");
ui.print("To change the end date, input: end (new end)");
try {
String[] command = input.split(" ", 2);
if (command.length < 2) {
throw new EmptyDescriptionException();
}
} else if (command[0].equalsIgnoreCase("action")) {
task.changeAction(command[1]);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
} else if (command[0].equalsIgnoreCase("cutoff")
&& task.getClass() == Deadline.class) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime deadline = LocalDateTime.parse(command[1], formatter);
task.changeDate(deadline);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else if (command[0].equalsIgnoreCase("start")
&& task.getClass() == Event.class) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime start = LocalDateTime.parse(command[1], formatter);
task.changeStartDate(start);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else if (command[0].equalsIgnoreCase("end")
&& task.getClass() == Event.class) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime end = LocalDateTime.parse(command[1], formatter);
task.changeEndDate(end);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else {
throw new EmptyDescriptionException("PUT IN PARAMETERS");
int toChange = reminder.getSelected();
Task task = reminder.getSchedule().get(toChange);
if (command[0].equalsIgnoreCase("change")) {
toChange = Integer.parseInt(command[1]) - 1;
reminder.changeSelected(toChange);
task = reminder.getSchedule().get(toChange);
ui.print("Got it. You want to edit this task:");
ui.print(" " + task.toString());
ui.print("To change the task description, input: action (task description)");
if (task.getClass() == Deadline.class) {
ui.print("To change the deadline, input: cutoff (new deadline)");
} else if (task.getClass() == Event.class) {
ui.print("To change the start date, input: start (new start)");
ui.print("To change the end date, input: end (new end)");
}
} else if (command[0].equalsIgnoreCase("action")) {
task.changeAction(command[1]);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
} else if (command[0].equalsIgnoreCase("cutoff")
&& task.getClass() == Deadline.class) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime deadline = LocalDateTime.parse(command[1], formatter);
task.changeDate(deadline);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else if (command[0].equalsIgnoreCase("start")
&& task.getClass() == Event.class) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime start = LocalDateTime.parse(command[1], formatter);
task.changeStartDate(start);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else if (command[0].equalsIgnoreCase("end")
&& task.getClass() == Event.class) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime end = LocalDateTime.parse(command[1], formatter);
task.changeEndDate(end);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else {
throw new EmptyDescriptionException();
}
} catch (EmptyDescriptionException e) {
ui.emptyDescriptionMessage();
}
return true;
}
Expand Down
Loading

0 comments on commit 4c256df

Please sign in to comment.