|
1 | 1 | import java.util.Scanner; |
2 | | -import java.util.ArrayList; |
3 | 2 | import java.io.File; |
4 | 3 | import java.util.regex.Pattern; |
5 | 4 | import java.util.regex.Matcher; |
6 | 5 | import java.time.LocalDate; |
7 | 6 | import java.time.format.DateTimeFormatter; |
8 | | -import java.io.BufferedReader; |
9 | | -import java.io.FileReader; |
10 | | -import java.io.IOException; |
11 | | -import java.io.FileWriter; |
12 | | -import java.io.BufferedWriter; |
13 | | -import java.io.IOException; |
14 | 7 |
|
15 | 8 | public class Duke { |
16 | 9 |
|
17 | | - |
18 | | - public static void main(String[] args) { |
19 | | - String name = "Johnnythesnake"; |
20 | | - System.out.println("Hello I'm " + name + "\n" + "What can I do for you? Aside from completing your CS2103 project for you"); |
21 | | - Scanner scanner = new Scanner(System.in); |
22 | | - String filename = "tasks.txt"; |
23 | | - // Create a File object with the filename |
24 | | - File file = new File(filename); |
25 | | - |
26 | | - ArrayList<Tasks> tasksList = new ArrayList<>(); |
27 | | - if (file.exists()) { |
28 | | - tasksList = TaskReader.readTasksFromFile(filename); |
29 | | - System.out.println(tasksList); |
| 10 | + private final Storage storage; |
| 11 | + private TaskList tasks; |
| 12 | + private final Ui ui; |
| 13 | + |
| 14 | + public Duke(String filePath) { |
| 15 | + ui = new Ui(); |
| 16 | + storage = new Storage(filePath); |
| 17 | + try { |
| 18 | + tasks = storage.load(); |
| 19 | + } catch (DukeException e) { |
| 20 | + ui.showLoadingError(); |
| 21 | + tasks = new TaskList(); |
30 | 22 | } |
31 | | - while (true) { |
32 | | - System.out.println("Enter a command: "); |
33 | | - String command = scanner.nextLine(); |
34 | | - if (command.equalsIgnoreCase("bye")) { // bye exits the code |
35 | | - Exit exit = new Exit(); |
36 | | - System.out.println(exit.exitMessage()); |
37 | | - break; |
38 | | - } else if (command.equalsIgnoreCase("list")) { //list shows the task list |
39 | | - System.out.println("Here are the tasks in your list: "); |
40 | | - if (!tasksList.isEmpty()) { |
41 | | - for (int i = 1; i <= tasksList.size(); i++) { |
42 | | - System.out.println(i + "." + tasksList.get(i - 1)); |
43 | | - } |
44 | | - } |
45 | | - } else if (command.startsWith("unmark")) { // unmark the task in question |
46 | | - int taskNumber = Integer.parseInt(command.substring(7)) - 1; |
47 | | - if (taskNumber < tasksList.size()) { |
48 | | - Tasks task = tasksList.get(taskNumber); |
49 | | - task.setMarked(false); |
50 | | - tasksList.set(taskNumber, task); |
51 | | - System.out.println("OK, I've marked this task as not done yet:\n" + " " + tasksList.get(taskNumber)); |
52 | | - } |
53 | | - } else if (command.startsWith("mark")) { // mark the task in question |
54 | | - int taskNumber = Integer.parseInt(command.substring(5)) - 1; |
55 | | - if (taskNumber < tasksList.size()) { |
56 | | - Tasks task = tasksList.get(taskNumber); |
57 | | - task.setMarked(true); |
58 | | - tasksList.set(taskNumber, task); |
59 | | - System.out.println("Nice! I've marked this task as done:\n" + " " + tasksList.get(taskNumber)); |
60 | | - } |
61 | | - |
62 | | - |
63 | | - } else if (command.startsWith("todo")) { |
64 | | - String description = command.substring(4).trim(); // Trim any leading/trailing spaces |
65 | | - |
66 | | - try { |
67 | | - if (description.isEmpty()) { |
68 | | - throw new EmptyTodoException(); |
69 | | - } |
| 23 | + } |
70 | 24 |
|
71 | | - Todo todo = new Todo(description, false); |
72 | | - tasksList.add(todo); |
73 | 25 |
|
74 | | - System.out.println("Got it. I've added this task:"); |
75 | | - System.out.println(" " + todo); |
76 | | - System.out.println("Now you have " + tasksList.size() + " tasks in the list."); |
77 | | - } catch (EmptyTodoException e) { |
78 | | - System.out.println(e.getMessage()); |
79 | | - } |
80 | | - } else if (command.startsWith("deadline")) { |
81 | | - // Split the input |
82 | | - String descriptionDeadline = command.substring(8).trim(); // Remove "deadline" and leading spaces |
| 26 | + public void run() { |
| 27 | + ui.showWelcomeMessage(); |
83 | 28 |
|
84 | | - if (descriptionDeadline.isEmpty()) { |
| 29 | + while (true) { |
| 30 | + String userInput = ui.getUserInput(); |
| 31 | + Command command = Parser.parseCommand(userInput); |
| 32 | + String description = Parser.parseDescription(userInput); |
| 33 | + switch (command) { |
| 34 | + case EXIT: |
| 35 | + storage.save(tasks, "tasks.txt"); |
| 36 | + ui.closeScanner(); |
| 37 | + ui.showExitMessage(); |
| 38 | + return; |
| 39 | + case LIST: |
| 40 | + ui.showTaskList(tasks); |
| 41 | + break; |
| 42 | + case UNMARK: |
85 | 43 | try { |
86 | | - throw new EmptyDeadlineException(); |
87 | | - } catch (EmptyDeadlineException e) { |
88 | | - System.out.println(e.getMessage()); |
| 44 | + int taskNumber = Integer.parseInt(description) - 1; |
| 45 | + if (taskNumber >= 0 && taskNumber < tasks.size()) { |
| 46 | + tasks.unmarkTask(taskNumber); |
| 47 | + } else { |
| 48 | + ui.showError("Task number out of range."); |
| 49 | + } |
| 50 | + } catch (NumberFormatException e) { |
| 51 | + ui.showError("Invalid task number. Please provide a valid integer."); |
89 | 52 | } |
90 | | - } else { |
91 | | - // Find the index of the deadline separator "/" |
92 | | - int separatorIndex = descriptionDeadline.indexOf('/'); |
93 | | - |
94 | | - if (separatorIndex != -1) { // Ensure the separator exists in the input |
95 | | - // Extract the task description and deadline |
96 | | - |
97 | | - String description = descriptionDeadline.substring(0, separatorIndex).trim(); |
98 | | - String deadline = descriptionDeadline.substring(separatorIndex + 4).trim(); |
99 | | - String pattern = "\\d{4}/\\d{2}/\\d{2}"; |
100 | | - Pattern datePattern = Pattern.compile(pattern); |
101 | | - Matcher matcher = datePattern.matcher(deadline); |
102 | | - if (matcher.find()) { |
103 | | - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); |
104 | | - LocalDate localDateDeadline = LocalDate.parse(deadline, formatter); |
105 | | - Deadline deadlineTask = new Deadline(description,false, localDateDeadline); |
106 | | - tasksList.add(deadlineTask); |
107 | | - System.out.println("Got it. I've added this deadline:"); |
108 | | - System.out.println(" " + deadlineTask); |
109 | | - System.out.println("Now you have " + tasksList.size() + " tasks in the list."); |
| 53 | + break; |
| 54 | + case MARK: |
| 55 | + try { |
| 56 | + int taskNumber = Integer.parseInt(description) - 1; |
| 57 | + if (taskNumber >= 0 && taskNumber < tasks.size()) { |
| 58 | + tasks.markTaskAsDone(taskNumber); |
110 | 59 | } else { |
111 | | - System.out.println("Please input your deadline in YYYY/MM/DD format"); |
| 60 | + ui.showError("Task number out of range."); |
112 | 61 | } |
113 | | - } else { |
114 | | - System.out.println("Invalid input format for deadline. Please input in the following format: <deadline> <description> /by <YYYY/MM/DD> "); |
| 62 | + } catch (NumberFormatException e) { |
| 63 | + ui.showError("Invalid task number. Please provide a valid integer."); |
115 | 64 | } |
116 | | - } |
117 | | - } else if (command.startsWith("event")) { |
118 | | - // split the input |
119 | | - String descriptionStartEndTime = command.substring(5).trim(); // Remove "event" and leading spaces |
120 | | - if (descriptionStartEndTime.isEmpty()) { |
| 65 | + break; |
| 66 | + case TODO: |
121 | 67 | try { |
122 | | - throw new EmptyEventException(); |
123 | | - } catch (EmptyEventException e) { |
| 68 | + if (description.isEmpty()) { |
| 69 | + throw new EmptyTodoException(); |
| 70 | + } |
| 71 | + Todo todo = new Todo(description, false); |
| 72 | + tasks.addTask(todo); |
| 73 | + |
| 74 | + } catch (EmptyTodoException e) { |
124 | 75 | System.out.println(e.getMessage()); |
125 | 76 | } |
126 | | - } else { |
127 | | - // Find the indices of the time separators |
128 | | - int fromIndex = descriptionStartEndTime.indexOf("/from"); |
129 | | - int toIndex = descriptionStartEndTime.indexOf("/to"); |
130 | | - |
131 | | - if (fromIndex != -1 && toIndex != -1) { |
132 | | - // Extract the task description, startTime, and endTime |
133 | | - String description = descriptionStartEndTime.substring(0, fromIndex).trim(); |
134 | | - String startTime = descriptionStartEndTime.substring(fromIndex + 5, toIndex).trim(); |
135 | | - String endTime = descriptionStartEndTime.substring(toIndex + 3).trim(); |
136 | | - |
137 | | - // Create a new Event object |
138 | | - Event eventTask = new Event(description, false, startTime, endTime); |
139 | | - tasksList.add(eventTask); |
140 | | - |
141 | | - // Print confirmation message |
142 | | - System.out.println("Got it. I've added this task:"); |
143 | | - System.out.println(" " + eventTask); |
144 | | - System.out.println("Now you have " + tasksList.size() + " tasks in the list."); |
| 77 | + break; |
| 78 | + case DEADLINE: |
| 79 | + |
| 80 | + if (description.isEmpty()) { |
| 81 | + try { |
| 82 | + throw new EmptyDeadlineException(); |
| 83 | + } catch (EmptyDeadlineException e) { |
| 84 | + System.out.println(e.getMessage()); |
| 85 | + } |
145 | 86 | } else { |
146 | | - System.out.println("Invalid input format for event command."); |
| 87 | + // Find the index of the deadline separator "/" |
| 88 | + int separatorIndex = description.indexOf('/'); |
| 89 | + |
| 90 | + if (separatorIndex != -1) { // Ensure the separator exists in the input |
| 91 | + // Extract the task description and deadline |
| 92 | + |
| 93 | + String descriptionString = description.substring(0, separatorIndex).trim(); |
| 94 | + String deadline = description.substring(separatorIndex + 4).trim(); |
| 95 | + String pattern = "\\d{4}/\\d{2}/\\d{2}"; |
| 96 | + Pattern datePattern = Pattern.compile(pattern); |
| 97 | + Matcher matcher = datePattern.matcher(deadline); |
| 98 | + if (matcher.find()) { |
| 99 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); |
| 100 | + LocalDate localDateDeadline = LocalDate.parse(deadline, formatter); |
| 101 | + Deadline deadlineTask = new Deadline(descriptionString, false, localDateDeadline); |
| 102 | + tasks.addTask(deadlineTask); |
| 103 | + |
| 104 | + } else { |
| 105 | + System.out.println("Please input your deadline in YYYY/MM/DD format"); |
| 106 | + } |
| 107 | + } else { |
| 108 | + System.out.println("Invalid input format for deadline. Please input in the following format: <deadline> <description> /by <YYYY/MM/DD> "); |
| 109 | + } |
147 | 110 | } |
148 | | - } |
149 | | - } else if (command.startsWith("delete")) { |
150 | | - int taskNumber = Integer.parseInt(command.substring(7)) - 1; |
151 | | - if (taskNumber < tasksList.size()) { |
152 | | - Tasks task = tasksList.get(taskNumber); |
153 | | - tasksList.remove(taskNumber); |
154 | | - |
155 | | - System.out.println("Noted. I've removed this task: \n" + " " + task); |
156 | | - System.out.println("Now you have " + tasksList.size() + " tasks in the list."); |
157 | | - } |
158 | | - |
159 | | - |
160 | | - } else { |
161 | | - try { |
162 | | - throw new UnknownInputException(); |
163 | | - } catch (UnknownInputException e) { |
164 | | - System.out.println(e.getMessage()); |
165 | | - } |
166 | | - } |
| 111 | + break; |
| 112 | + case EVENT: |
| 113 | + if (description.isEmpty()) { |
| 114 | + try { |
| 115 | + throw new EmptyEventException(); |
| 116 | + } catch (EmptyEventException e) { |
| 117 | + System.out.println(e.getMessage()); |
| 118 | + } |
| 119 | + } else { |
| 120 | + // Find the indices of the time separators |
| 121 | + int fromIndex = description.indexOf("/from"); |
| 122 | + int toIndex = description.indexOf("/to"); |
167 | 123 |
|
| 124 | + if (fromIndex != -1 && toIndex != -1) { |
| 125 | + // Extract the task description, startTime, and endTime |
| 126 | + String descriptionString = description.substring(0, fromIndex).trim(); |
| 127 | + String startTime = description.substring(fromIndex + 5, toIndex).trim(); |
| 128 | + String endTime = description.substring(toIndex + 3).trim(); |
| 129 | + |
| 130 | + // Create a new Event object |
| 131 | + Event eventTask = new Event(descriptionString, false, startTime, endTime); |
| 132 | + tasks.addTask(eventTask); |
| 133 | + |
| 134 | + } else { |
| 135 | + System.out.println("Invalid input format for event command."); |
| 136 | + } |
| 137 | + } |
| 138 | + break; |
| 139 | + case DELETE: |
| 140 | + try { |
| 141 | + int taskNumber = Integer.parseInt(description) - 1; |
| 142 | + if (taskNumber >= 0 && taskNumber < tasks.size()) { |
| 143 | + tasks.deleteTask(taskNumber); |
| 144 | + } else { |
| 145 | + ui.showError("Task number out of range."); |
| 146 | + } |
| 147 | + } catch (NumberFormatException e) { |
| 148 | + ui.showError("Invalid task number. Please provide a valid integer."); |
| 149 | + } |
| 150 | + break; |
| 151 | + case INVALID: |
| 152 | + ui.showError("Invalid command. Please try again."); |
| 153 | + break; |
| 154 | + } |
168 | 155 | } |
169 | | - TaskWriter.writeTasksToFile(tasksList, "tasks.txt"); |
| 156 | + } |
| 157 | + |
| 158 | + public static void main(String[] args) { |
| 159 | + new Duke("tasks.txt").run(); |
170 | 160 | } |
171 | 161 | } |
172 | 162 |
|
0 commit comments