-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathDuke.java
More file actions
59 lines (51 loc) · 1.61 KB
/
Duke.java
File metadata and controls
59 lines (51 loc) · 1.61 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
package duke;
import duke.commands.Command;
import duke.commands.CommandOutput;
import duke.tasks.*;
import java.util.Scanner;
public class Duke {
private Storage storage;
private TaskList tasks;
private Ui ui;
private Parser parser;
public Duke(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
parser = new Parser();
TaskListResponse response = storage.initialiseTasks();
tasks = response.taskList;
Ui.printMessage(response.response);
}
//attempts to execute the user command
public CommandOutput executeCommand(Command command) {
try {
return command.execute();
} catch (Exception e){
return new CommandOutput(e.getMessage(), false, null);
}
}
//runs the program
public void run() {
ui.printGreeting();
String line;
Scanner in = new Scanner(System.in);
ui.printPrompt();
line = in.nextLine();
while(!line.equals(parser.BYE)) {
Command command = parser.parseCommand(tasks, line);
CommandOutput commandOutput = executeCommand(command);
if (commandOutput.isUpdated()) {
tasks = commandOutput.getTaskList();
String errorMessage = storage.write(tasks);
ui.printMessage(errorMessage);
}
ui.printMessage(commandOutput.getResponse());
ui.printPrompt();
line = in.nextLine();
}
ui.printExit();
}
public static void main(String[] args) {
new Duke("data/duke.txt").run();
}
}