-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathDuke.java
More file actions
156 lines (137 loc) · 4.83 KB
/
Duke.java
File metadata and controls
156 lines (137 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package duke;
import java.util.Scanner;
public class Duke {
public static Task[] tasks = new Task[100];
public static int taskCount = 0;
public static void printGreeting() {
System.out.println("Hello! I'm Duke\n" + "What can I do for you?");
}
public static void printConfused() {
System.out.println("Could you say that again?");
}
public static void printExit() {
System.out.println("Bye. Hope to see you again soon!");
}
public static void printGotIt() {
System.out.println("Got it. I've added this task:");
}
public static void printTaskCount() {
System.out.println("Now you have " + taskCount + " tasks in the list.");
}
public static void printTodoException() {
System.out.println("The description of todo cannot be empty.");
}
public static void printDeadlineException() {
System.out.println("The description of deadline cannot be empty.");
}
public static void printEventException() {
System.out.println("The description of event cannot be empty.");
}
public static void printDoneException() {
System.out.println("The description of done cannot be empty.");
}
public static void printTaskTypeResponse() {
//printing different responses depending if its duke.Todo/duke.Deadline/duke.Event
printGotIt();
tasks[taskCount - 1].printTask();
printTaskCount();
}
public static void printList() {
System.out.println("Here are the tasks in your list:");
for (int i = 0; i < taskCount; i += 1) {
System.out.print((i + 1) + ".");
tasks[i].printTask();
}
}
public static void taskDone(String line) throws DoneException {
if (line.equals("") || line.equals("done")) {
throw new DoneException();
} else {
int index = Integer.parseInt(line) - 1;
tasks[index].setDone();
System.out.println("Nice! I've marked this task as done:");
tasks[index].printTask();
}
}
public static void addTodo(String line) throws TodoException {
if (line.equals("") || line.equals("todo")) {
throw new TodoException();
} else {
tasks[taskCount] = new Todo(line);
taskCount = taskCount + 1;
printTaskTypeResponse();
}
}
public static void addDeadline(String line) throws DeadlineException {
if (!line.matches("(.*)/by(.*)")) {
throw new DeadlineException();
} else {
//extracting the description and date
String description = line.replaceAll("/.+", "");
String by = line.replaceAll(".+/by", "");
tasks[taskCount] = new Deadline(description, by);
taskCount = taskCount + 1;
printTaskTypeResponse();
}
}
public static void addEvent(String line) throws EventException {
if (!line.matches("(.*)/at(.*)")) {
throw new EventException();
} else {
//extracting the description and date
String description = line.replaceAll("/.+", "");
String by = line.replaceAll(".+/at", "");
tasks[taskCount] = new Event(description, by);
taskCount = taskCount + 1;
printTaskTypeResponse();
}
}
public static void readInput(String line) {
String[] splitLine = line.split(" ", 2);
String command = splitLine[0];
line = line.replaceAll("^" + command + " ", "");
if (command.equals("list")) {
printList();
} else if (command.equals("done")) {
try {
//find which task user is done with
taskDone(line);
} catch (DoneException e) {
printDoneException();
}
} else if (command.equals("todo")) {
try {
addTodo(line);
} catch (TodoException e) {
printTodoException();
}
} else if (command.equals("deadline")) {
try {
addDeadline(line);
} catch (DeadlineException e) {
printDeadlineException();
}
} else if (command.equals("event")) {
try {
addEvent(line);
} catch (EventException e) {
printEventException();
}
} else {
//error with input
printConfused();
}
}
public static void main(String[] args) {
printGreeting();
String line;
Scanner in = new Scanner(System.in);
line = in.nextLine();
//continue to run program unless types "bye" to exit program
while(!line.equals("bye")) {
readInput(line);
line = in.nextLine();
}
printExit();
}
}