Skip to content

Commit de2ffa6

Browse files
committed
Level-5
1 parent 2db4f6b commit de2ffa6

2 files changed

Lines changed: 61 additions & 62 deletions

File tree

src/main/java/Duke.java

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,56 @@ public static void printExit(){
3535
System.out.println();
3636
}
3737

38+
public static int addTask(Task[] tasks, String command, int numberOfTasks) throws DukeException{
39+
if(command.equalsIgnoreCase("list")){
40+
printLine();
41+
System.out.println("Here are the tasks in your list:");
42+
for(int i=0; i<numberOfTasks; i++){
43+
System.out.println(i+1 + "." + tasks[i]);
44+
}
45+
printLine();
46+
}
47+
48+
else if(command.startsWith("done")){
49+
int index = Integer.parseInt(command.substring(5)) - 1;
50+
if(index < numberOfTasks){
51+
tasks[index].setDone();
52+
printLine();
53+
System.out.println("Nice! I've marked this task as done:");
54+
System.out.println("[\u2713] " + tasks[index].description);
55+
printLine();
56+
}
57+
}
58+
59+
else if(command.startsWith("deadline")){
60+
tasks[numberOfTasks] = new Deadline(command.substring(9, command.indexOf("/")),
61+
command.substring(command.indexOf("/") + 4));
62+
printTask(tasks[numberOfTasks], numberOfTasks+1);
63+
numberOfTasks++;
64+
}
65+
66+
else if(command.startsWith("event")){
67+
68+
tasks[numberOfTasks] = new Event(command.substring(6, command.indexOf("/")),
69+
command.substring(command.indexOf("/") + 4));
70+
printTask(tasks[numberOfTasks], numberOfTasks+1);
71+
numberOfTasks++;
72+
73+
}
74+
75+
else if(command.startsWith("todo")){
76+
tasks[numberOfTasks] = new Todo(command.substring(5));
77+
printTask(tasks[numberOfTasks], numberOfTasks+1);
78+
numberOfTasks++;
79+
}
80+
81+
else{
82+
throw new DukeException();
83+
}
84+
85+
return numberOfTasks;
86+
}
87+
3888
public static void main(String[] args) {
3989
printGreeting();
4090

@@ -46,73 +96,19 @@ public static void main(String[] args) {
4696
int numberOfTasks = 0;
4797

4898
while(!command.equalsIgnoreCase("Bye")){
49-
50-
if(command.equalsIgnoreCase("list")){
99+
try{
100+
numberOfTasks = addTask(tasks, command, numberOfTasks);
101+
} catch(DukeException e){
51102
printLine();
52-
System.out.println("Here are the tasks in your list:");
53-
for(int i=0; i<numberOfTasks; i++){
54-
System.out.println(i+1 + "." + tasks[i]);
55-
}
103+
System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
56104
printLine();
57-
}
58-
59-
else if(command.startsWith("done")){
60-
int index = Integer.parseInt(command.substring(5)) - 1;
61-
if(index < numberOfTasks){
62-
tasks[index].setDone();
63-
printLine();
64-
System.out.println("Nice! I've marked this task as done:");
65-
System.out.println("[\u2713] " + tasks[index].description);
66-
printLine();
67-
}
68-
}
69-
70-
else if(command.startsWith("deadline")){
71-
tasks[numberOfTasks] = new Deadline(command.substring(9, command.indexOf("/")),
72-
command.substring(command.indexOf("/") + 4));
73-
printTask(tasks[numberOfTasks], numberOfTasks+1);
74-
numberOfTasks++;
75-
}
76-
77-
else if(command.startsWith("event")){
78-
String[] checkEvent = command.split(" ");
79-
if(checkEvent.length < 2){
80-
printLine();
81-
System.out.println("☹ OOPS!!! The description of a event cannot be empty.");
82-
printLine();
83-
}
84-
else if(!command.contains("/")){
85-
printLine();
86-
System.out.println("☹ OOPS!!! There need to be a time for event.");
87-
printLine();
88-
}
89-
else{
90-
tasks[numberOfTasks] = new Event(command.substring(6, command.indexOf("/")),
91-
command.substring(command.indexOf("/") + 4));
92-
printTask(tasks[numberOfTasks], numberOfTasks+1);
93-
numberOfTasks++;
94-
}
95-
96-
}
97-
98-
else if(command.startsWith("todo")){
99-
String[] checkTodo = command.split(" ");
100-
if(checkTodo.length < 2){
101-
printLine();
102-
System.out.println("☹ OOPS!!! The description of a todo cannot be empty.");
103-
printLine();
104-
}
105-
else{
106-
tasks[numberOfTasks] = new Todo(command.substring(5));
107-
printTask(tasks[numberOfTasks], numberOfTasks+1);
108-
numberOfTasks++;
109-
}
110-
}
111-
else{
105+
} catch (StringIndexOutOfBoundsException e){
112106
printLine();
113-
System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
107+
System.out.println("☹ OOPS!!! The description of a " + command + " cannot be empty.");
114108
printLine();
115109
}
110+
111+
116112
command = sc.nextLine();
117113
}
118114

src/main/java/DukeException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class DukeException extends Exception {
2+
3+
}

0 commit comments

Comments
 (0)