Skip to content

Commit

Permalink
Add exceptions to task commands
Browse files Browse the repository at this point in the history
Exceptions are missing index, invalid index, invalid command and
incomplete command caused by missing task or task time.
  • Loading branch information
ZhuLeYao committed Feb 7, 2023
1 parent 0035506 commit b5e1e00
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 97 deletions.
261 changes: 168 additions & 93 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,101 +27,100 @@ public void chatDuke() {
boolean saidBye = false;
while (!saidBye) {
String command = sc.nextLine();
if (command.equals("list")) {
this.printCommandList(allTasks);
} else if (command.startsWith("mark")) {
String[] str = command.split(" ");
int taskIndex = Integer.parseInt(str[1]) - 1;
Task oldTask = allTasks.get(taskIndex);
if (oldTask.getTaskType().equals("[T]")) {
Todo todo = new Todo(oldTask.getTaskNumber(),
true, oldTask.getTask(),
allTasks.size());
allTasks.set(taskIndex, todo);
todo.markAsDone();
} else if (oldTask.getTaskType().equals("[D]")) {
Deadline deadline = new Deadline(oldTask.getTaskNumber(),
true, oldTask.getTask(),
oldTask.getDeadline(), allTasks.size());
allTasks.set(taskIndex, deadline);
deadline.markAsDone();
} else if (oldTask.getTaskType().equals("[E]")) {
Event event = new Event(oldTask.getTaskNumber(),
true, oldTask.getTask(),
oldTask.getEventStartTime(),
oldTask.getEventEndTime(), allTasks.size());
allTasks.set(taskIndex, event);
event.markAsDone();
} else {
Task task = new Task(oldTask.getTaskNumber(),
true, oldTask.getTask(),
allTasks.size());
allTasks.set(taskIndex, task);
task.markAsDone();
}
} else if (command.startsWith("unmark")) {
String[] str = command.split(" ");
int taskIndex = Integer.parseInt(str[1]) - 1;
Task oldTask = allTasks.get(taskIndex);
if (oldTask.getTaskType().equals("[T]")) {
Todo todo = new Todo(oldTask.getTaskNumber(),
false, oldTask.getTask(),
allTasks.size());
allTasks.set(taskIndex, todo);
todo.markAsDone();
} else if (oldTask.getTaskType().equals("[D]")) {
Deadline deadline = new Deadline(oldTask.getTaskNumber(),
false, oldTask.getTask(),
oldTask.getDeadline(), allTasks.size());
allTasks.set(taskIndex, deadline);
deadline.markAsDone();
} else if (oldTask.getTaskType().equals("[E]")) {
Event event = new Event(oldTask.getTaskNumber(),
false, oldTask.getTask(),
oldTask.getEventStartTime(),
oldTask.getEventEndTime(), allTasks.size());
allTasks.set(taskIndex, event);
event.markAsDone();

try {
if (command.equals("list")) {
this.printCommandList(allTasks);
} else if (command.startsWith("mark")) {
missingIndexException(command);
invalidIndexException(command, allTasks.size());
String[] str = command.split(" ");
int taskIndex = Integer.parseInt(str[1]) - 1;
Task oldTask = allTasks.get(taskIndex);
if (oldTask.getTaskType().equals("[T]")) {
Todo todo = new Todo(oldTask.getTaskNumber(),
true, oldTask.getTask(),
allTasks.size());
allTasks.set(taskIndex, todo);
todo.markAsDone();
} else if (oldTask.getTaskType().equals("[D]")) {
Deadline deadline = new Deadline(oldTask.getTaskNumber(),
true, oldTask.getTask(),
oldTask.getDeadline(), allTasks.size());
allTasks.set(taskIndex, deadline);
deadline.markAsDone();
} else if (oldTask.getTaskType().equals("[E]")) {
Event event = new Event(oldTask.getTaskNumber(),
true, oldTask.getTask(),
oldTask.getEventStartTime(),
oldTask.getEventEndTime(), allTasks.size());
allTasks.set(taskIndex, event);
event.markAsDone();
}
} else if (command.startsWith("unmark")) {
missingIndexException(command);
invalidIndexException(command, allTasks.size());
String[] str = command.split(" ");
int taskIndex = Integer.parseInt(str[1]) - 1;
Task oldTask = allTasks.get(taskIndex);
if (oldTask.getTaskType().equals("[T]")) {
Todo todo = new Todo(oldTask.getTaskNumber(),
false, oldTask.getTask(),
allTasks.size());
allTasks.set(taskIndex, todo);
todo.unmarkAsUndone();
} else if (oldTask.getTaskType().equals("[D]")) {
Deadline deadline = new Deadline(oldTask.getTaskNumber(),
false, oldTask.getTask(),
oldTask.getDeadline(), allTasks.size());
allTasks.set(taskIndex, deadline);
deadline.unmarkAsUndone();
} else if (oldTask.getTaskType().equals("[E]")) {
Event event = new Event(oldTask.getTaskNumber(),
false, oldTask.getTask(),
oldTask.getEventStartTime(),
oldTask.getEventEndTime(), allTasks.size());
allTasks.set(taskIndex, event);
event.unmarkAsUndone();
}
} else if (command.startsWith("todo")) {
emptyCommandException(command);
String[] str = command.split("todo");
String taskName = str[1];
Todo todo = new Todo(allTasks.size(), false,
taskName, allTasks.size() + 1);
allTasks.add(todo);
todo.printToDoTask();
} else if (command.startsWith("deadline")) {
emptyCommandException(command);
missingTimingException(command);
String[] str = command.split("/by");
String taskName = str[0].split("deadline")[1];
String taskDeadline = str[1];
Deadline deadline = new Deadline(allTasks.size(), false,
taskName, taskDeadline, allTasks.size() + 1);
allTasks.add(deadline);
deadline.printDeadlineTask();
} else if (command.startsWith("event")) {
emptyCommandException(command);
missingTimingException(command);
String[] str = command.split("/from");
String taskName = str[0].split("event")[1];
String[] eventStartEndTime = str[1].split("/to");
String eventStartTime = eventStartEndTime[0];
String eventEndTime = eventStartEndTime[1];
Event event = new Event(allTasks.size(), false,
taskName, eventStartTime, eventEndTime, allTasks.size() + 1);
allTasks.add(event);
event.printEventTask();
} else if (command.equals("bye")){
saidBye = true;
this.printByeMessage();
} else {
Task task = new Task(oldTask.getTaskNumber(),
false, oldTask.getTask(),
allTasks.size());
allTasks.set(taskIndex, task);
task.markAsDone();
invalidCommandException(command);
}
} else if (command.startsWith("todo")) {
String[] str = command.split("todo");
String taskName = str[1];
Todo todo = new Todo(allTasks.size(), false,
taskName, allTasks.size() + 1);
allTasks.add(todo);
todo.printToDoTask();
} else if (command.startsWith("deadline")) {
String[] str = command.split("/by");
String taskName = str[0].split("deadline")[1];
String taskDeadline = str[1];
Deadline deadline = new Deadline(allTasks.size(), false,
taskName, taskDeadline, allTasks.size() + 1);
allTasks.add(deadline);
deadline.printDeadlineTask();
} else if (command.startsWith("event")) {
String[] str = command.split("/from");
String taskName = str[0].split("event")[1];
String[] eventStartEndTime = str[1].split("/to");
String eventStartTime = eventStartEndTime[0];
String eventEndTime = eventStartEndTime[1];
Event event = new Event(allTasks.size(), false,
taskName, eventStartTime, eventEndTime, allTasks.size() + 1);
allTasks.add(event);
event.printEventTask();
} else if (!command.equals("bye")) {
this.echoCommand(command);
Task task = new Task(allTasks.size(), false,
command, allTasks.size());
allTasks.add(task);
} else {
saidBye = true;
this.printByeMessage();
} catch (DukeException d) {
System.out.println(d.getMessage());
}
}
}
Expand Down Expand Up @@ -166,4 +165,80 @@ public void printByeMessage() {
"\n\t____________________________________________________________");
}

public void emptyCommandException(String command) throws DukeException {
switch (command) {
case "todo":
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The description of a todo cannot be empty." +
"\n\t____________________________________________________________");
case "deadline":
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The description of a deadline cannot be empty." +
"\n\t____________________________________________________________");
case "event":
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The description of an event cannot be empty." +
"\n\t____________________________________________________________");
}
}

public void missingTimingException(String command) throws DukeException {
if (command.startsWith("deadline") && !command.contains("/by")) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The timing of a deadline cannot be empty." +
"\n\t____________________________________________________________");
} else if (command.startsWith("event") && !command.contains("/from")) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The start time of an event cannot be empty." +
"\n\t____________________________________________________________");
} else if (command.startsWith("event") && !command.contains("/to")) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The end time of an event cannot be empty." +
"\n\t____________________________________________________________");
}
}

public void missingIndexException(String command) throws DukeException {
if (command.equals("mark")) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to mark a task as done cannot be empty." +
"\n\t____________________________________________________________");
} else if (command.equals("unmark")) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to unmark a task as not done cannot be empty." +
"\n\t____________________________________________________________");
}
}

public void invalidIndexException(String command, int taskSize) throws DukeException {
if (command.startsWith("mark") || command.startsWith("unmark")) {
String index = command.split(" ")[1];
int index1 = Integer.parseInt(index);
if (index1 <= 0) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to mark a task as done cannot be zero or less." +
"\n\t____________________________________________________________");
} else if (index.equals("")) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to mark a task as done cannot be empty." +
"\n\t____________________________________________________________");
} else if (index1 > taskSize) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to mark a task as done cannot be more than" +
" number of tasks." +
"\n\t____________________________________________________________");
}
}
}

public void invalidCommandException(String command) throws DukeException {
if (!command.startsWith("event") || !(command.startsWith("deadline")) ||
!command.startsWith("todo") || command.startsWith("mark") ||
!command.startsWith("unmark")) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! I'm sorry, but I don't know what that means :-(" +
"\n\t____________________________________________________________");
}
}

}
5 changes: 5 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DukeException extends Exception {
DukeException(String message) {
super(message);
}
}
22 changes: 22 additions & 0 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,25 @@ Hello from
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

____________________________________________________________
Got it. I've added this task:
[T][ ] borrow book
Now you have 1 tasks in the list.
____________________________________________________________

____________________________________________________________
Here are the tasks in your list:
1.[T][ ] borrow book
____________________________________________________________

____________________________________________________________
Got it. I've added this task:
[D][ ] return book (by: Sunday)
Now you have 2 tasks in the list.
____________________________________________________________

____________________________________________________________
Got it. I've added this task:
[E][ ] project meeting (from: Mon 2pm to: 4pm)
Now you have 3 tasks in the list.
____________________________________________________________
4 changes: 4 additions & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
todo borrow book
list
deadline return book /by Sunday
event project meeting /from Mon 2pm /to 4pm
4 changes: 2 additions & 2 deletions text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ REM delete output from previous run
if exist ACTUAL.TXT del ACTUAL.TXT

REM compile the code into the bin folder
javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java
javac -cp \Users\leyaozhu\Documents\NUS\CS2103\ip\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java
IF ERRORLEVEL 1 (
echo ********** BUILD FAILURE **********
exit /b 1
)
REM no error here, errorlevel == 0

REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ..\bin Duke < input.txt > ACTUAL.TXT
java -classpath \Users\leyaozhu\Documents\NUS\CS2103\ip\src\main\bin Duke < input.txt > ACTUAL.TXT

REM compare the output to the expected output
FC ACTUAL.TXT EXPECTED.TXT
4 changes: 2 additions & 2 deletions text-ui-test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ then
fi

# compile the code into the bin folder, terminates if error occurred
if ! javac -cp ../src/main/java -Xlint:none -d ../bin ../src/main/java/*.java
if ! javac -cp /Users/leyaozhu/Documents/NUS/CS2103/ip/src/main/java -Xlint:none -d /Users/leyaozhu/Documents/NUS/CS2103/ip/bin /Users/leyaozhu/Documents/NUS/CS2103/ip/src/main/java/*.java
then
echo "********** BUILD FAILURE **********"
exit 1
fi

# run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ../bin Duke < input.txt > ACTUAL.TXT
java -classpath /Users/leyaozhu/Documents/NUS/CS2103/ip/src/bin Duke < input.txt > ACTUAL.TXT

# convert to UNIX format
cp EXPECTED.TXT EXPECTED-UNIX.TXT
Expand Down

0 comments on commit b5e1e00

Please sign in to comment.