-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathDuke.java
More file actions
63 lines (58 loc) · 1.85 KB
/
Duke.java
File metadata and controls
63 lines (58 loc) · 1.85 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
package duke;
import duke.command.Command;
import duke.exception.DukeException;
import duke.parser.Parser;
import duke.storage.Storage;
import duke.tasklist.TaskList;
import duke.ui.Ui;
import java.io.FileNotFoundException;
import java.util.Locale;
import java.util.Scanner;
public class Duke {
private static final String fileDir = "./data";
private static final String fileName = "./data/save.txt";
private final Storage storage;
private TaskList task;
private final Ui ui;
/**
* Initialise all classes and load save file if it exists otherwise create a new save file
*
* @param fileName The name of the save file
* @param fileDir The directory of the save file
*/
public Duke(String fileName, String fileDir) {
storage = new Storage(fileName, fileDir);
ui = new Ui();
try {
task = new TaskList(storage.items);
storage.load(task);
} catch (FileNotFoundException e) {
ui.showSaveFileError();
storage.create();
task = new TaskList(storage.items);
}
}
private void run() {
Scanner in = new Scanner(System.in);
boolean isExit = false;
while (!isExit) {
try {
String fullCommand = ui.readCommand(in).toLowerCase(Locale.ROOT);
Command c = Parser.parse(fullCommand);
boolean hasError = Parser.verifyCommand(task, c, ui);
if (hasError) {
throw new DukeException();
}
c.execute(task, ui, storage);
isExit = c.isExit();
} catch (DukeException e) {
ui.showError();
}
}
storage.save();
ui.printEndMessage();
}
public static void main(String[] args) {
new Duke(fileName, fileDir).run();
}
}