Skip to content

Commit

Permalink
Level 5
Browse files Browse the repository at this point in the history
Completed Level 5: Handle Errors
  • Loading branch information
gachia committed Aug 22, 2019
1 parent ac597c4 commit ea8ba7c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 49 deletions.
117 changes: 68 additions & 49 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import java.util.ArrayList;

public class Duke {

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
Expand All @@ -16,57 +17,75 @@ public static void main(String[] args) {
ArrayList<Task> list = new ArrayList();
String taskName;
Task task;
while(sc.hasNext()){
String userCmd = sc.next();
if(userCmd.equals("bye")){
System.out.println(lineSpace + "Bye. Hope to see you again soon!\n" + lineSpace);
break;
}
switch(userCmd) {
case "list":
System.out.println(lineSpace + "Here are the tasks in your list:");
for(int i=0; i < list.size(); i++){
System.out.println(i+1 + "." + list.get(i));
}
System.out.print(lineSpace);
break;
case "todo":
taskName = sc.nextLine();
taskName = taskName.trim();
task = new Task(taskName);
list.add(task);
System.out.println(lineSpace + "Got it. I've added this task:\n" + task
+ "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace);
break;
case "deadline":
taskName = sc.nextLine();
String[] userWords = taskName.trim().split("/by");
task = new Deadline(userWords[0].trim(), userWords[1].trim());
list.add(task);
System.out.println(lineSpace + "Got it. I've added this task:\n" + task
+ "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace);
break;
case "event":
taskName = sc.nextLine();
userWords = taskName.trim().split("/at");
task = new Event(userWords[0].trim(), userWords[1].trim());
list.add(task);
System.out.println(lineSpace + "Got it. I've added this task:\n" + task
+ "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace);
while (sc.hasNext()) {
try {
String userCmd = sc.next();
String[] userWords;
if (userCmd.equals("bye")) {
System.out.println(lineSpace + "Bye. Hope to see you again soon!\n" + lineSpace);
break;
case "done":
int taskNo = sc.nextInt();
list.get(taskNo-1).markAsDone();
System.out.println(lineSpace + "Nice! I've marked this task as done:\n"
+ list.get(taskNo-1) + "\n" + lineSpace);
break;
default:
list.add(new Task(userCmd));
System.out.println(lineSpace + "added: " + userCmd + "\n" + lineSpace);
//Level-1 code
//System.out.println(lineSpace + "\n" + userInput + "\n" + lineSpace);
}
switch (userCmd) {
case "list":
System.out.println(lineSpace + "Here are the tasks in your list:");
for (int i = 0; i < list.size(); i++) {
System.out.println(i + 1 + "." + list.get(i));
}
System.out.print(lineSpace);
break;
case "todo":
taskName = sc.nextLine().trim();
if (taskName.isEmpty()) {
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
}
task = new Task(taskName);
list.add(task);
System.out.println(lineSpace + "Got it. I've added this task:\n" + task
+ "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace);
break;
case "deadline":
taskName = sc.nextLine().trim();
if (taskName.isEmpty()) {
throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.");
}
userWords = taskName.split("/by");
if (userWords.length == 1) {
throw new DukeException("☹ OOPS!!! The date/time of a deadline cannot be empty or is wrongly typed.");
}
task = new Deadline(userWords[0].trim(), userWords[1].trim());
list.add(task);
System.out.println(lineSpace + "Got it. I've added this task:\n" + task
+ "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace);

break;
case "event":
taskName = sc.nextLine().trim();
if (taskName.isEmpty()) {
throw new DukeException("☹ OOPS!!! The description of an event cannot be empty.");
}
userWords = taskName.split("/at");
if (userWords.length == 1) {
throw new DukeException("☹ OOPS!!! The date/time of an event cannot be empty or is wrongly typed.");
}
task = new Event(userWords[0].trim(), userWords[1].trim());
list.add(task);
System.out.println(lineSpace + "Got it. I've added this task:\n" + task
+ "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace);
break;
case "done":
int taskNo = sc.nextInt();
list.get(taskNo - 1).markAsDone();
System.out.println(lineSpace + "Nice! I've marked this task as done:\n"
+ list.get(taskNo - 1) + "\n" + lineSpace);
break;
default:
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

}

}

6 changes: 6 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class DukeException extends Exception {

public DukeException(String error){
super(error);
}
}

0 comments on commit ea8ba7c

Please sign in to comment.