Skip to content

Commit

Permalink
Finish Level 5. Handle Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
QianChangru committed Aug 27, 2023
1 parent cab0141 commit 28266b3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 48 deletions.
103 changes: 55 additions & 48 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("____________________________________________________________");
Expand Down
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 @@
public class DukeException extends Exception{
public DukeException(String message) {
super(message);
}
}

0 comments on commit 28266b3

Please sign in to comment.