diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 522872cc6d..a4c3039597 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -17,58 +17,65 @@ public static void main(String[] args) { while (!command.startsWith("bye")) { - if (command.startsWith("list")) { - System.out.println("____________________________________________________________"); - System.out.println(" Here are the tasks in your list:"); - int counter = 0; - while (counter != list.size()) { - counter++; - System.out.println(" " + counter + "." + list.get(counter - 1).toString()); + try{ + if (command.startsWith("list")) { + System.out.println("____________________________________________________________"); + System.out.println(" Here are the tasks in your list:"); + int counter = 0; + while (counter != list.size()) { + counter++; + System.out.println(" " + counter + "." + list.get(counter - 1).toString()); + } + System.out.println("____________________________________________________________"); + } else if (command.startsWith("mark")) { + list.get(Integer.valueOf(command.split(" ")[1]) - 1).markAsDone(); + System.out.println("____________________________________________________________"); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" " + list.get(Integer.valueOf(command.split(" ")[1]) - 1).toString()); + System.out.println("____________________________________________________________"); + } else if (command.startsWith("todo")) { + if (command.split(" ", 2).length == 1) { + throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); + } + ToDo newToDo = new ToDo(command.split(" ", 2)[1]); + list.add(newToDo); + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + newToDo.toString()); + System.out.println(" Now you have " + list.size() + " tasks in the list."); + System.out.println("____________________________________________________________"); + } else if (command.startsWith("deadline")) { + String deadline = command.split(" /by ", 2)[1]; + String name = command.split(" /by ", 2)[0].split(" ", 2)[1]; + Deadline newDeadline = new Deadline(deadline, name); + list.add(newDeadline); + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + newDeadline.toString()); + System.out.println(" Now you have " + list.size() + " tasks in the list."); + System.out.println("____________________________________________________________"); + } else if (command.startsWith("event")) { + String startTime = command.split(" /from ", 2)[1] + .split(" /to ", 2)[0]; + String endTime = command.split(" /to ", 2)[1]; + String name = command.split(" /from ", 2)[0].split(" ", 2)[1]; + Event newEvent = new Event(name, startTime, endTime); + list.add(newEvent); + System.out.println("____________________________________________________________"); + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + newEvent.toString()); + System.out.println(" Now you have " + list.size() + " tasks in the list."); + System.out.println("____________________________________________________________"); + } else { + throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } + } catch (DukeException e) { System.out.println("____________________________________________________________"); - } else if (command.startsWith("mark")) { - list.get(Integer.valueOf(command.split(" ")[1]) - 1).markAsDone(); - System.out.println("____________________________________________________________"); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" " + list.get(Integer.valueOf(command.split(" ")[1]) - 1).toString()); - System.out.println("____________________________________________________________"); - } else if (command.startsWith("todo")) { - ToDo newToDo = new ToDo(command.split(" ", 2)[1]); - list.add(newToDo); - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + newToDo.toString()); - System.out.println(" Now you have " + list.size() + " tasks in the list."); - System.out.println("____________________________________________________________"); - } else if (command.startsWith("deadline")) { - String deadline = command.split(" /by ", 2)[1]; - String name = command.split(" /by ", 2)[0].split(" ", 2)[1]; - Deadline newDeadline = new Deadline(deadline, name); - list.add(newDeadline); - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + newDeadline.toString()); - System.out.println(" Now you have " + list.size() + " tasks in the list."); - System.out.println("____________________________________________________________"); - } else if (command.startsWith("event")) { - String startTime = command.split(" /from ", 2)[1] - .split(" /to ", 2)[0]; - String endTime = command.split(" /to ", 2)[1]; - String name = command.split(" /from ", 2)[0].split(" ", 2)[1]; - Event newEvent = new Event(name, startTime, endTime); - list.add(newEvent); - System.out.println("____________________________________________________________"); - System.out.println(" Got it. I've added this task:"); - System.out.println(" " + newEvent.toString()); - System.out.println(" Now you have " + list.size() + " tasks in the list."); - System.out.println("____________________________________________________________"); - } else { - list.add(new Task(command)); - System.out.println("____________________________________________________________"); - System.out.println(" added: " + command); + System.out.println(" " + e.getMessage()); System.out.println("____________________________________________________________"); + } finally { + command = userInput.nextLine(); } - command = userInput.nextLine(); } System.out.println("____________________________________________________________"); diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 0000000000..c858d79e91 --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,5 @@ +public class DukeException extends Exception{ + public DukeException(String message) { + super(message); + } +}