forked from nus-cs2103-AY2122S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duke.java
75 lines (66 loc) · 1.82 KB
/
Duke.java
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
package seedu.duke;
import seedu.commands.Command;
import seedu.storage.Storage;
import seedu.storage.TaskList;
/**
* The main class of Duke
*/
public class Duke {
private Ui ui;
private TaskList tasks;
private Parser parser;
private Storage storage;
/**
* Constructor for Duke class
*
* @param filePath The file path of the save file.
*/
public Duke(String filePath) {
ui = new Ui();
parser = new Parser();
storage = new Storage(filePath);
try {
tasks = new TaskList(storage.load());
} catch (DukeException e) {
ui.showError(e.getMessage());
}
System.out.println("Welcome!\n");
}
/**
* Runner for the program.
* Saves task list into save file for every command run.
* Stops if command == "bye".
*/
public void run() {
while (!Command.isExit()) {
try {
String fullCommand = ui.readCommand();
Command c = parser.parse(fullCommand);
String resp = c.execute(tasks);
storage.saveAll(tasks.getTasks());
ui.printCompleted(resp);
} catch (DukeException e) {
ui.showError(e.getMessage());
}
}
}
/**
* Used in the GUI to get responses from Duke
*
* @param input The command to be entered
* @return The response of Duke
*/
public String getResponse(String input) {
try {
Command c = parser.parse(input);
String resp = c.execute(tasks);
storage.saveAll(tasks.getTasks());
return resp;
} catch (DukeException e) {
return e.getMessage();
}
}
public static void main(String[] args) {
new Duke("data/tasks.txt").run();
}
}