-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathDuke.java
More file actions
141 lines (124 loc) · 4.88 KB
/
Duke.java
File metadata and controls
141 lines (124 loc) · 4.88 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
package duke;
import duke.exceptions.DukeException;
import duke.exceptions.DukeIDKException;
import duke.exceptions.DukeInvalidDesException;
import duke.exceptions.DukeMissingDesException;
import duke.handler.Parser;
import duke.handler.Queries;
import duke.tasks.*;
import duke.tasks.Event;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Duke {
private boolean exit = false;
private Storage storage;
private TaskList tasks;
private Ui ui;
private Duke(String storagePath) {
storage = new Storage(storagePath);
tasks = storage.load();
ui = new Ui();
}
public static void main(String[] args) throws IOException {
new Duke("data/tasks.txt").run();
}
public void run() {
ui.displayIntro();
while (exit == false) {
processInput(ui.readCommand());
}
ui.close();
}
private void processInput(String userInput) {
String keyword_UC = userInput.toUpperCase().split(" ", -1)[0];
Queries query = Queries.ADD;
String response = "";
if (Queries.containsValue(keyword_UC)) {
query = Queries.valueOf(keyword_UC);
}
try {
switch (query) {
case BYE:
response = "Bye. Hope to see you again soon!";
ui.respond(response);
exit = true;
break;
case ADD:
Task toAdd;
try {
if (keyword_UC.equals("TODO")) {
toAdd = new Todo(userInput.split(" ", 2)[1]);
} else if (keyword_UC.equals("DEADLINE")) {
String[] info = userInput.split(" ", 2);
if (info[1].contains("/by")) {
info = info[1].split("/by");
} else {
throw new DukeInvalidDesException(keyword_UC);
}
toAdd = new Deadline(info[0], info[1]);
} else if (keyword_UC.equals("EVENT")) {
String[] info = userInput.split(" ", 2);
if (info[1].contains("/at")) {
info = info[1].split(("/at"));
} else {
throw new DukeInvalidDesException(keyword_UC);
}
toAdd = new Event(info[0], info[1]);
} else {
throw new DukeIDKException();
}
tasks.addTask(toAdd);
response = "Got it. I've added this task:\n"
+ " " + toAdd + "\n"
+ "Now you have " + tasks.getNumOfTasks() + " tasks in the list.\n";
ui.respond(response);
storage.addTask(toAdd);
} catch (ArrayIndexOutOfBoundsException e) {
throw new DukeMissingDesException(keyword_UC);
}
break;
case DONE:
try {
int taskNum = Integer.parseInt(userInput.split(" ")[1]);
tasks.markDone(taskNum);
Task updatedTask = tasks.getTask(taskNum);
response = "Nice! I've marked this task as done: \n"
+ " " + updatedTask+ "\n";
ui.respond(response);
storage.markDoneInStorage(updatedTask, taskNum);
} catch (ArrayIndexOutOfBoundsException e) {
throw new DukeMissingDesException("DONE");
} catch (IndexOutOfBoundsException | NumberFormatException e) {
throw new DukeInvalidDesException("DONE");
}
break;
case DELETE:
try {
int taskNum = Integer.parseInt(userInput.split(" ")[1]);
Task task = tasks.getTask(taskNum);
tasks.removeTask(taskNum);
response = "Noted. I've removed this task: \n"
+ " " + task + "\n";
ui.respond(response);
storage.delete(taskNum);
} catch (ArrayIndexOutOfBoundsException e) {
throw new DukeMissingDesException("DELETE");
} catch (IndexOutOfBoundsException | NumberFormatException e) {
throw new DukeInvalidDesException("DELETE");
}
break;
case LIST:
response += tasks.toString();
ui.respond(response);
break;
}
} catch (DukeException e) {
String output = e.getMessage();
ui.respond(output);
}
}
}