-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathDuke.java
More file actions
73 lines (63 loc) · 1.77 KB
/
Duke.java
File metadata and controls
73 lines (63 loc) · 1.77 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
package duke;
import duke.DukeExceptions.EmptyCommand;
import duke.DukeExceptions.InvalidCommandException;
import duke.task.Task;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
public class Duke {
private final Storage storage;
private TaskList tasksLs;
private final Ui ui;
private final Scanner in = new Scanner(System.in);
/**
* A constructor to initiate Duke.
*
* @param filepath Filepath when initiating.
*/
public Duke(String filepath) {
this.tasksLs = new TaskList(new ArrayList<Task>());
this.storage = new Storage(filepath);
this.ui = new Ui();
}
/**
* Runs Duke object until user exits with </bye>.
*/
private void run() {
ui.greetMessage();
String input = "";
try {
tasksLs = storage.loadFile(tasksLs.getList());
} catch (FileNotFoundException e) {
storage.createFile();
}
boolean closeDuke = false;
do {
try {
input = getInput();
String parsed = new Parser(tasksLs, ui).parse(input);
System.out.println(parsed);
storage.autoSave(tasksLs.getList());
closeDuke = input.trim().equals("bye");
} catch (InvalidCommandException | EmptyCommand e) {
System.out.println(e);
}
} while (!closeDuke);
}
/**
* Prompts user for input.
*
* @return String input by user.
*/
private String getInput() {
return in.nextLine();
}
/**
* application.
*
* @param args to start Duke Application.
*/
public static void main(String[] args) {
new Duke("duke.txt").run();
}
}