|
1 | 1 | import java.io.File; |
2 | | -import java.io.FileNotFoundException; |
3 | | -import java.io.FileWriter; |
4 | 2 | import java.io.IOException; |
5 | 3 | import java.util.ArrayList; |
6 | 4 | import java.util.Scanner; |
|
12 | 10 | - making task an abstract class |
13 | 11 | - upload from hard drive default expression |
14 | 12 | - declared get_date abstract even though todo doesn't use it |
| 13 | +- support different date formats and time also |
15 | 14 | */ |
16 | 15 |
|
17 | 16 | public class Duke { |
18 | | - private static final String SPACE = " "; |
19 | | - private static final String LINE = "\n <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>\n"; |
20 | | - private static final ArrayList<Task> storage = new ArrayList<>(); |
21 | | - private static int count = 0; |
22 | | - |
23 | 17 | public static void main(String[] args) throws IOException { |
24 | | - Scanner sc = new Scanner(System.in); |
25 | | - greet(); |
26 | | - boolean exit_now = false; |
27 | | - check_file_folder_specifications(); |
28 | | - |
29 | | - while (!exit_now) { |
30 | | - String inp = sc.nextLine(); |
31 | | - String[] spl = inp.split(" ", 2); |
32 | | - try { |
33 | | - switch (spl[0]) { |
34 | | - case "todo": |
35 | | - check_spl_length(spl, 2, "todo"); |
36 | | - process_todo(spl); |
37 | | - break; |
38 | | - case "deadline": |
39 | | - check_spl_length(spl, 2, "deadline"); |
40 | | - String[] spl2 = spl[1].split(" /by ", 2); |
41 | | - check_spl_length(spl2, 2, "deadline"); |
42 | | - process_deadline(spl2); |
43 | | - break; |
44 | | - case "event": |
45 | | - check_spl_length(spl, 2, "event"); |
46 | | - String[] spl3 =spl[1].split(" /at ", 2); |
47 | | - check_spl_length(spl3, 2, "event"); |
48 | | - process_event(spl3); |
49 | | - break; |
50 | | - case "done": |
51 | | - check_spl_length(spl, 2, "done"); |
52 | | - is_int(spl[1], "done"); |
53 | | - process_done(spl); |
54 | | - break; |
55 | | - case "list": |
56 | | - check_spl_length(spl, 1, "list"); |
57 | | - process_list(); |
58 | | - break; |
59 | | - case "delete": |
60 | | - check_spl_length(spl, 2, "delete"); |
61 | | - is_int(spl[1], "delete"); |
62 | | - process_delete(spl); |
63 | | - break; |
64 | | - case "bye": |
65 | | - check_spl_length(spl, 1, "bye"); |
66 | | - process_bye(); |
67 | | - exit_now = true; |
68 | | - break; |
69 | | - default: |
70 | | - throw new InvalidKeywordException(); |
71 | | - } |
72 | | - } catch (InvalidTaskFormatException e) { |
73 | | - e.printMessage(); |
74 | | - } catch (InvalidNumberException e) { |
75 | | - e.printMessage(); |
76 | | - } catch (InvalidKeywordException e) { |
77 | | - e.printMessage(); |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - static void greet() { |
83 | | - System.out.println(LINE + SPACE + "Heyyoo!! I am Luna :D\n" + SPACE + "What can I do for you today?" + LINE); |
84 | | - } |
85 | | - |
86 | | - static void is_int(String s, String task) throws InvalidNumberException { |
87 | | - try { |
88 | | - int x = Integer.parseInt(s); |
89 | | - if (x > count) { |
90 | | - throw new InvalidNumberException(LINE + SPACE + "ERROR! D: The number should be smaller than the total number of tasks in the following task: " + task + LINE); |
91 | | - } |
92 | | - } catch (NumberFormatException e){ |
93 | | - throw new InvalidNumberException(LINE + SPACE + "ERROR! D: A number should be added for the following task: " + task + LINE); |
94 | | - } |
95 | | - } |
96 | | - |
97 | | - |
98 | | - static void check_file_folder_specifications() { |
99 | | - try { |
100 | | - File dir = new File("./data"); |
101 | | - dir.mkdir(); |
102 | | - File f = new File("./data/tasks.txt"); |
103 | | - if (!f.createNewFile()) { |
104 | | - upload_from_hard_drive(); |
105 | | - } |
106 | | - |
107 | | - } catch (IOException e) { |
108 | | - System.out.println("error in making folder/file"); |
109 | | - } |
110 | | - } |
111 | | - |
112 | | - static void check_spl_length(String[] spl, int x, String task) throws InvalidTaskFormatException { |
113 | | - if (spl.length != x) |
114 | | - throw new InvalidTaskFormatException(LINE + SPACE + "ERROR! D: The format for the following task is wrong: " + task + LINE); |
115 | | - } |
116 | | - |
117 | | - /* |
118 | | - static void process_task(String[] spl) throws InvalidTaskFormatException { |
119 | | - Task new_task; |
120 | | - switch(spl[0]) { |
121 | | - case "todo": |
122 | | - new_task = new todo(spl[1]); |
123 | | - break; |
124 | | - case "deadline": |
125 | | - String[] spl2 = spl[1].split(" ", 2); |
126 | | - new_task = new deadline(spl2[1], spl2[2]); |
127 | | - break; |
128 | | - case "event": |
129 | | - String[] spl3 = spl[1].split(" ", 2); |
130 | | - new_task = new event(spl3[1], spl3[2]); |
131 | | - break; |
132 | | - default: |
133 | | - throw new InvalidTaskFormatException("Wrong"); |
134 | | - } |
135 | | - storage.add(new_task); |
136 | | - System.out.println(LINE + SPACE + "Done adding the " + spl[0] + "task: " + storage.get(count - 1) + LINE); |
137 | | - } |
138 | | -
|
139 | | - */ |
140 | | - |
141 | | - static void process_todo(String[] spl) throws InvalidTaskFormatException { |
142 | | - storage.add(new todo(spl[1])); |
143 | | - count++; |
144 | | - System.out.println(LINE + SPACE + "Done adding the TODO Task: " + storage.get(count - 1) + LINE); |
| 18 | + Ui.greet(); |
| 19 | + TaskList.check_file_folder_specifications(); |
| 20 | + Parser.parse(); |
145 | 21 | } |
146 | | - |
147 | | - static void process_deadline(String[] spl) { |
148 | | - storage.add(new deadline(spl[0], LocalDate.parse(spl[1]))); |
149 | | - count++; |
150 | | - System.out.println(LINE + SPACE + "Done adding the DEADLINE Task: " + storage.get(count - 1) + LINE); |
151 | | - } |
152 | | - |
153 | | - static void process_event(String[] spl) { |
154 | | - storage.add(new event(spl[0], LocalDate.parse(spl[1]))); |
155 | | - count++; |
156 | | - System.out.println(LINE + SPACE + "Done adding the EVENT Task: " + storage.get(count - 1) + LINE); |
157 | | - } |
158 | | - |
159 | | - static void process_done(String[] spl) { |
160 | | - int number = Integer.parseInt(spl[1]); |
161 | | - Task current = storage.get(number - 1); |
162 | | - current.finished(); |
163 | | - System.out.println(LINE + SPACE + "Good job! Another Task completed! I have marked it as done:\n" + SPACE + current + LINE); |
164 | | - } |
165 | | - |
166 | | - static void process_list() { |
167 | | - System.out.println(LINE); |
168 | | - for (int i = 1; i <= count; i++) { |
169 | | - System.out.println(SPACE + i + ". " + storage.get(i-1)); |
170 | | - } |
171 | | - System.out.println(LINE); |
172 | | - } |
173 | | - |
174 | | - static void process_delete(String[] spl) { |
175 | | - int num = Integer.parseInt(spl[1]); |
176 | | - System.out.println(LINE + " Deleted the following task: " + storage.get(num - 1) + LINE); |
177 | | - storage.remove(num-1); |
178 | | - count--; |
179 | | - } |
180 | | - |
181 | | - static void process_bye() throws IOException { |
182 | | - upload_to_hard_drive(); |
183 | | - System.out.println(LINE + SPACE + "Byee, hope to see you again soon!" + LINE); |
184 | | - } |
185 | | - |
186 | | - static void upload_from_hard_drive() throws FileNotFoundException { |
187 | | - File f = new File("./data/tasks.txt"); |
188 | | - Scanner s = new Scanner(f); |
189 | | - while (s.hasNext()) { |
190 | | - count++; |
191 | | - String[] spl = s.nextLine().split("@@@", 4); |
192 | | - Task to_add; |
193 | | - switch(spl[0]) { |
194 | | - case "T": |
195 | | - to_add = new todo(spl[2]); |
196 | | - break; |
197 | | - case "D": |
198 | | - to_add = new deadline(spl[2], LocalDate.parse(spl[3])); |
199 | | - break; |
200 | | - case "E": |
201 | | - to_add = new event(spl[2], LocalDate.parse(spl[3])); |
202 | | - break; |
203 | | - default: |
204 | | - to_add = new todo("error"); |
205 | | - } |
206 | | - if (spl[1].equals("1")) { |
207 | | - to_add.finished(); |
208 | | - } |
209 | | - storage.add(to_add); |
210 | | - } |
211 | | - } |
212 | | - |
213 | | - static void upload_to_hard_drive() throws IOException { |
214 | | - FileWriter fw = new FileWriter("./data/tasks.txt"); |
215 | | - String between = "@@@"; |
216 | | - for (Task t : storage) { |
217 | | - String zero = t.get_initial(); |
218 | | - String one = t.get_done(); |
219 | | - String two = t.get_description(); |
220 | | - if (zero.equals("T")) { |
221 | | - fw.write(zero + between + one + between + two + "\n"); |
222 | | - } else { |
223 | | - LocalDate three = t.get_date(); |
224 | | - fw.write(zero + between + one + between + two + between + three + "\n"); |
225 | | - } |
226 | | - } |
227 | | - fw.close(); |
228 | | - } |
229 | | - |
230 | 22 | } |
0 commit comments