Skip to content

Commit

Permalink
Add DukeException and JUnit Tests for commands package
Browse files Browse the repository at this point in the history
  • Loading branch information
g-erm committed Aug 25, 2020
1 parent 2b1906d commit ef8876c
Show file tree
Hide file tree
Showing 16 changed files with 668 additions and 68 deletions.
27 changes: 14 additions & 13 deletions src/main/java/dd/Duke.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dd;

import dd.commands.Command;
import dd.exception.DukeException;
import dd.parser.Parser;
import dd.storage.DataStorage;
import dd.tasks.TaskList;
Expand All @@ -9,40 +10,40 @@
import java.io.IOException;

public class Duke {
private final DataStorage ds;
private final Ui ui;

private DataStorage ds;
private Ui ui;
private TaskList tasks;

public Duke() {
this.ds = new DataStorage();
this.ui = new Ui();

try {
tasks = new TaskList(ds.loadData());
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
ui.showLoadingError();
tasks = new TaskList();
}
}

public void run() {
ui.greeting();

boolean isExit = false;

while (!isExit) {
String input = ui.readInput();
Command c = Parser.parse(input);

if (c != null) {
try {
String input = ui.readInput();
Command c = Parser.parse(input);
c.execute(tasks, ui, ds);
isExit = c.isExit();
}
else {
// not valid command
System.out.println("Sorry what?");
catch (DukeException e){
ui.showError(e.getMessage());
}
finally {
ui.printLine();
}
ui.printLine();
}
}

Expand Down
28 changes: 12 additions & 16 deletions src/main/java/dd/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dd.commands;

import dd.datetimehandler.DateTimeHandler;
import dd.exception.DukeException;
import dd.storage.DataStorage;
import dd.tasks.Deadline;
import dd.tasks.Event;
Expand All @@ -20,12 +21,12 @@ public AddCommand(String command, String item) {

public void addTodo() {
tasks.addTask(new Todo(item));
System.out.println("Ok, To-do added:\n " + tasks.getLastTask());

ui.startAddTodo(tasks.getLastTask());
ui.printTasksSize(tasks.getTaskSize());
}

public void addDeadline() {
public void addDeadline() throws DukeException {
String[] temp = item.split(" /by "); // create array of [task desc, task date]

if (temp.length == 2) {
Expand All @@ -35,25 +36,22 @@ public void addDeadline() {
// valid
String formattedDate = dth.categorizeInput(temp[1]);
tasks.addTask(new Deadline(temp[0], formattedDate));
System.out.println("Ok, Deadline added:\n " + tasks.getLastTask());

ui.startAddDeadline(tasks.getLastTask());
ui.printTasksSize(tasks.getTaskSize());
}
else {
// not valid date
System.out.println("I don't understand :( Please input date as DD-MM-YYYY or DD-MM-YYYY HHmm\n"
+ "Example: 31-12-2020 or 31-12-2020 2359");
throw new DukeException().invalidDate();
}
}
else {
// no date input
System.out.println("Due date not detected, try again!\n"
+ "Please input deadline as 'deadline (title) /by (date)'\n"
+ "Example: deadline return book /by 31-12-2020");
throw new DukeException().invalidDeadline();
}
}

public void addEvent() {
public void addEvent() throws DukeException {
String[] temp = item.split(" /at "); // create array of [task desc, task date]

if (temp.length == 2) {
Expand All @@ -63,29 +61,27 @@ public void addEvent() {
// valid
String formattedDate = dth.categorizeInput(temp[1]);
tasks.addTask(new Event(temp[0], formattedDate));
System.out.println("Ok, Event added:\n " + tasks.getLastTask());

ui.startAddEvent(tasks.getLastTask());
ui.printTasksSize(tasks.getTaskSize());
}
else {
// not valid date
System.out.println("I don't understand :( Please input date as DD-MM-YYYY or DD-MM-YYYY HHmm\n"
+ "Example: 31-12-2020 or 31-12-2020 2359");
throw new DukeException().invalidDate();
}
}
else {
// no date input
System.out.println("Event date not detected, try again!\n"
+ "Please input event as 'event (title) /at (date)'\n"
+ "Example: event group meeting /at 31-12-2020");
throw new DukeException().invalidEvent();
}
}

@Override
public void execute(TaskList taskList, Ui u, DataStorage ds) {
public void execute(TaskList taskList, Ui u, DataStorage ds) throws DukeException {
tasks = taskList;
ui = u;
this.dth = new DateTimeHandler();

if (command.equals("todo")) {
addTodo();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dd/commands/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dd.commands;

import dd.exception.DukeException;
import dd.storage.DataStorage;
import dd.tasks.TaskList;
import dd.ui.Ui;
Expand All @@ -13,6 +14,6 @@ public Command(String command, String item) {
this.item = item;
}

public abstract void execute(TaskList taskList, Ui u, DataStorage ds);
public abstract void execute(TaskList taskList, Ui u, DataStorage ds) throws DukeException;
public abstract boolean isExit();
}
9 changes: 5 additions & 4 deletions src/main/java/dd/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dd.commands;

import dd.exception.DukeException;
import dd.storage.DataStorage;
import dd.tasks.TaskList;
import dd.ui.Ui;
Expand All @@ -11,7 +12,7 @@ public DeleteCommand(String command, String item) {
}

@Override
public void execute(TaskList tasks, Ui ui, DataStorage ds) {
public void execute(TaskList tasks, Ui ui, DataStorage ds) throws DukeException {
int delNum = 0;

try {
Expand All @@ -21,13 +22,13 @@ public void execute(TaskList tasks, Ui ui, DataStorage ds) {
}

if (delNum > 0 && delNum <= tasks.getTaskSize()) {
System.out.println("Alright! I've deleted the task:\n " + tasks.getTask(delNum-1));
ui.printDeletedTask(tasks.getTask(delNum-1));
tasks.deleteTask(delNum-1);

System.out.println("You now have " + tasks.getTaskSize() + " task(s) in your list!");
ui.printTasksSize(tasks.getTaskSize());
}
else {
System.out.println("hmm.. I don't think thats a valid task, try again?");
throw new DukeException().invalidTaskNumber();
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/dd/commands/DoneCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dd.commands;

import dd.exception.DukeException;
import dd.storage.DataStorage;
import dd.tasks.TaskList;
import dd.ui.Ui;
Expand All @@ -11,7 +12,7 @@ public DoneCommand(String command, String item) {
}

@Override
public void execute(TaskList tasks, Ui ui, DataStorage ds) {
public void execute(TaskList tasks, Ui ui, DataStorage ds) throws DukeException {
int taskNum = 0;

try {
Expand All @@ -21,11 +22,11 @@ public void execute(TaskList tasks, Ui ui, DataStorage ds) {
}

if (taskNum > 0 && taskNum <= tasks.getTaskSize()) {
tasks.getTaskList().get(taskNum - 1).markAsDone();
System.out.println("Wow!! Good job!!\n " + tasks.getTaskList().get(taskNum - 1));
tasks.getTask(taskNum - 1).markAsDone();
ui.printDoneTask(tasks.getTask(taskNum - 1));
}
else {
System.out.println("hmm.. I don't think thats a valid task, try again?");
throw new DukeException().invalidTaskNumber();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dd/commands/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dd.commands;

import dd.exception.DukeException;
import dd.storage.DataStorage;
import dd.tasks.TaskList;
import dd.ui.Ui;
Expand All @@ -11,9 +12,8 @@ public ExitCommand(String command, String item) {
}

@Override
public void execute(TaskList tasks, Ui u, DataStorage ds) {
System.out.println("You're leaving? Bye :( Come back soon!"
+ "\n_________________________________________");
public void execute(TaskList tasks, Ui u, DataStorage ds) throws DukeException {
u.exit();
ds.writeData(tasks.getTaskList());
}

Expand Down
16 changes: 9 additions & 7 deletions src/main/java/dd/commands/ListCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dd.commands;

import dd.datetimehandler.DateTimeHandler;
import dd.exception.DukeException;
import dd.storage.DataStorage;
import dd.tasks.Task;
import dd.tasks.TaskList;
Expand All @@ -20,39 +21,40 @@ public ListCommand(String command, String item) {

public void list(ArrayList<Task> tasks) {
int curr = 0;

while (curr < tasks.size()) {
System.out.println((curr + 1) + ". " + tasks.get(curr));
ui.printTask(curr + 1, tasks.get(curr));
curr += 1;
}
}

public void checkDate() {
public void checkDate() throws DukeException {
boolean validInput = dth.checkInput(item);

if (validInput && item.length() == 10) {
// valid
ArrayList<Task> tasksOnDate = dth.filterDate(item, tasks.getTaskList());

if (tasksOnDate.isEmpty()) {
System.out.println("No tasks found on " + item + "!");
throw new DukeException().emptyCheckDate(item);
}
else {
System.out.println("Here is your list of task(s) on " + item + ":");
ui.startCheckDate(item);
list(tasksOnDate);
}
}
else {
// not valid date
System.out.println("I don't understand :( Please input date as DD-MM-YYYY\n"
+ "Example: 31-12-2020");
throw new DukeException().invalidCheckDate();
}
}

@Override
public void execute(TaskList taskList, Ui u, DataStorage ds) {
public void execute(TaskList taskList, Ui u, DataStorage ds) throws DukeException {
tasks = taskList;
ui = u;
this.dth = new DateTimeHandler();

if (command.equals("list")) {
list(tasks.getTaskList());
}
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/dd/exception/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dd.exception;

public class DukeException extends Exception {

private String message;

public DukeException(String errorMessage) {
message = errorMessage;
}

public DukeException() {

}

public String getMessage() {
return this.message;
}

public DukeException emptyCheckDate(String date) {
String msg = "No tasks found on " + date + "!";

return new DukeException(msg);
}

public DukeException invalidCheckDate() {
String msg = "I don't understand :( Please input date as DD-MM-YYYY\n"
+ "Example: 31-12-2020";

return new DukeException(msg);
}

public DukeException invalidDate() {
String msg = "I don't understand :( Please input date as DD-MM-YYYY or DD-MM-YYYY HHmm\n"
+ "Example: 31-12-2020 or 31-12-2020 2359";

return new DukeException(msg);
}

public DukeException invalidDeadline() {
String msg = "Due date not detected, try again!\n"
+ "Please input deadline as 'deadline (title) /by (date)'\n"
+ "Example: deadline return book /by 31-12-2020";

return new DukeException(msg);
}

public DukeException invalidEvent() {
String msg = "Event date not detected, try again!\n"
+ "Please input event as 'event (title) /at (date)'\n"
+ "Example: event group meeting /at 31-12-2020";

return new DukeException(msg);
}

public DukeException invalidTaskNumber() {
String msg = "hmm.. I don't think thats a valid task, try again?";

return new DukeException(msg);
}

public DukeException noData() {
String msg = "No data written to file.";

return new DukeException(msg);
}
}

0 comments on commit ef8876c

Please sign in to comment.