|
1 | 1 | package prerthan.duke; |
2 | 2 |
|
3 | | -import prerthan.duke.IO.Input; |
4 | | -import prerthan.duke.IO.Output; |
5 | | -import prerthan.duke.IO.Storage; |
| 3 | +import javafx.application.Platform; |
| 4 | +import prerthan.duke.exception.DukeEmptyDetailException; |
6 | 5 | import prerthan.duke.task.TaskList; |
| 6 | +import prerthan.duke.command.Command; |
| 7 | +import prerthan.duke.exception.DukeInvalidArgumentException; |
| 8 | +import prerthan.duke.exception.DukeInvalidCommandException; |
| 9 | +import prerthan.duke.exception.DukeInvalidDateException; |
| 10 | +import prerthan.duke.io.Input; |
| 11 | +import prerthan.duke.io.Output; |
| 12 | +import prerthan.duke.io.Storage; |
| 13 | + |
| 14 | +import java.util.Optional; |
7 | 15 |
|
8 | 16 | /** |
9 | | - * |
| 17 | + * |
10 | 18 | */ |
11 | | -public class Duke { |
12 | | - public static TaskList tasks; |
13 | | - public static Input input; |
14 | | - public static Output output; |
15 | | - public static Storage fileRW; |
16 | | - |
17 | | - public boolean run() { |
18 | | - return false; |
19 | | - } |
20 | | - |
21 | | - /** |
22 | | - * Runs the main program loop. |
23 | | - * |
24 | | - * @return {@code false} when the user says 'bye'; otherwise, never returns. |
25 | | - */ |
26 | | - public static boolean programLoop() { |
27 | | - boolean exitLoop = false; |
28 | | - String command; |
29 | | - |
30 | | - input.nextLine(); |
31 | | - return false; |
32 | | - } |
33 | | - |
34 | | - /** |
35 | | - * Cleans up objects and quits the program by calling {@link System#exit(int)}. |
36 | | - */ |
37 | | - public static void exit() { |
38 | | - output.sayGoodBye(); |
39 | | - |
40 | | - input.close(); |
41 | | - output.close(); |
42 | | - |
43 | | - System.exit(0); |
44 | | - } |
45 | | - |
46 | | - public static void main(String[] args) { |
47 | | - // Initialises file and UI I/O |
48 | | - fileRW = new Storage("data", "duke.txt"); |
49 | | - input = new Input(); |
50 | | - output = new Output(); |
51 | | - |
52 | | - // Greets the user. |
53 | | - output.sayHello(); |
54 | | - |
55 | | - // Creates the task list |
56 | | - tasks = new TaskList(); |
| 19 | +public class Duke |
| 20 | +{ |
| 21 | + public static TaskList tasks; |
| 22 | + public static Input input; |
| 23 | + public static Output output; |
| 24 | + public static Storage fileRW; |
| 25 | + |
| 26 | + /** |
| 27 | + * Runs the main program loop. |
| 28 | + * |
| 29 | + * @return {@code false} when the user says 'bye'; otherwise, never returns. |
| 30 | + */ |
| 31 | + public static boolean programLoop() |
| 32 | + { |
| 33 | + boolean isExit = false; |
| 34 | + while (!isExit) |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + Optional<Command> possibleCommand = input.nextLine().getCommand(); |
| 39 | + if (!possibleCommand.isPresent()) |
| 40 | + { |
| 41 | + output.say("Input cannot be empty; please enter a command."); |
| 42 | + continue; |
| 43 | + } |
| 44 | + possibleCommand.get().execute(tasks, fileRW, output); |
| 45 | + isExit = possibleCommand.get().willTerminate(); |
| 46 | + } |
| 47 | + catch (DukeInvalidArgumentException | DukeInvalidCommandException | DukeEmptyDetailException |
| 48 | + | DukeInvalidDateException e) |
| 49 | + { |
| 50 | + output.sayError(e); |
| 51 | + } |
| 52 | + } |
| 53 | + return isExit; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Cleans up objects and quits the program by calling {@link System#exit(int)}. |
| 58 | + */ |
| 59 | + public static void exit() |
| 60 | + { |
| 61 | + output.sayGoodBye(); |
| 62 | + |
| 63 | + input.close(); |
| 64 | + output.close(); |
| 65 | + Platform.exit(); |
| 66 | + System.exit(0); |
| 67 | + } |
| 68 | + |
| 69 | + public static void main(String[] args) |
| 70 | + { |
| 71 | + // Initialises file and UI I/O |
| 72 | + fileRW = new Storage("data", "duke.txt"); |
| 73 | + input = new Input(); |
| 74 | + output = new Output(); |
| 75 | + |
| 76 | + // Greets the user. |
| 77 | + output.sayHello(); |
| 78 | + |
| 79 | + // Creates the task list |
| 80 | + tasks = fileRW.loadFromFile(); |
| 81 | + |
| 82 | + if (programLoop()) |
| 83 | + { |
| 84 | + exit(); |
| 85 | + } |
| 86 | + } |
57 | 87 |
|
58 | | - if (programLoop()) |
59 | | - exit(); |
60 | | - } |
| 88 | + public String getResponse(String input) |
| 89 | + { |
| 90 | + return ""; |
| 91 | + } |
61 | 92 | } |
0 commit comments