diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 6976c28251..ee8f86c51c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -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()); } } } @@ -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____________________________________________________________"); + } + } + } diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 0000000000..c5e18daf45 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,5 @@ +class DukeException extends Exception { + DukeException(String message) { + super(message); + } +} diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e7..dc3a3d376e 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -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. + ____________________________________________________________ \ No newline at end of file diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb2..4e4722e9c6 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,4 @@ +todo borrow book +list +deadline return book /by Sunday +event project meeting /from Mon 2pm /to 4pm \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 0873744649..03fb94f330 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,7 @@ 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 @@ -15,7 +15,7 @@ IF ERRORLEVEL 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 diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index c9ec870033..12004f6ec9 100644 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -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