-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHousekeeper.java
More file actions
431 lines (407 loc) · 18.4 KB
/
Housekeeper.java
File metadata and controls
431 lines (407 loc) · 18.4 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* Interesting way of tracking your tasks!
*/
package wichackathon2020;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
*
* @author notebook
*/
public class Housekeeper {
static Calendar calendar;
static House house;
static Shield shield;
static String day;
static int semYear;
static int year;
static String month;
static int date;
static String time;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Make a scanner for user input
Scanner s = new Scanner(System.in);
System.out.println("HOUSEKEEPER!");
System.out.print("\nEnter S to start a new semester. Enter L to load an existing semester. ");
String start = s.nextLine();
startSchedule(s, start);
}
private static void startSchedule(Scanner s, String start) {
if (start.equalsIgnoreCase("s")) {
System.out.print("Year? ");
semYear = Integer.parseInt(s.nextLine());
System.out.print("\nEnter F for fall semester. Enter S for spring semester. ");
String semester = s.nextLine();
createCalendar(s, semester, semYear);
house = new House(false); // Not damaged
shield = new Shield(calendar);
interpreter(s);
} else if (start.equalsIgnoreCase("l")) {
System.out.print("Enter the file name: ");
String filename = s.nextLine();
readXML(filename);
house = new House(calculateDamage()); // Need to calculate
shield = new Shield(calendar);
interpreter(s);
} else {
System.out.println("Not a valid input.");
System.out.print("\nEnter S to start a new semester. Enter L to load an existing semester. ");
String another = s.nextLine();
startSchedule(s, another);
}
}
private static void createCalendar(Scanner s, String semester, int year) {
if (semester.equalsIgnoreCase("f") || semester.equalsIgnoreCase("s")) {
calendar = new Calendar(semester, year);
} else {
System.out.println("Not a valid semester.");
System.out.print("\nEnter F for fall semester. Enter S for spring semester. ");
String another = s.nextLine();
createCalendar(s, another, year);
}
}
private static void updateDT() {
LocalDateTime dt = LocalDateTime.now();
DateTimeFormatter formatdt = DateTimeFormatter.ofPattern("E dd MMM yyyy HH:mm:ss");
String formatteddt = dt.format(formatdt);
day = formatteddt.substring(0, 3);
date = Integer.parseInt(formatteddt.substring(4, 6));
month = formatteddt.substring(7, 10);
year = Integer.parseInt(formatteddt.substring(11, 15));
time = formatteddt.substring(16);
System.out.println("Today is " + day + ", " + month + " " + date + ", " + year + " Current time: " + time);
}
private static int monthIndex(String month) {
if (month.equalsIgnoreCase("AUG") || month.equalsIgnoreCase("JAN")) {
return 1;
} else if (month.equalsIgnoreCase("SEP") || month.equalsIgnoreCase("FEB")) {
return 2;
} else if (month.equalsIgnoreCase("OCT") || month.equalsIgnoreCase("MAR")) {
return 3;
} else if (month.equalsIgnoreCase("NOV") || month.equalsIgnoreCase("APR")) {
return 4;
} else if (month.equalsIgnoreCase("DEC") || month.equalsIgnoreCase("MAY")) {
return 5;
} else {
return 0;
}
}
private static boolean validMonth(String month) {
if (calendar.getSemester().equalsIgnoreCase("f")) {
return (month.equalsIgnoreCase("AUG") || month.equalsIgnoreCase("SEP") || month.equalsIgnoreCase("OCT") || month.equalsIgnoreCase("NOV") || month.equalsIgnoreCase("DEC"));
} else {
return (month.equalsIgnoreCase("JAN") || month.equalsIgnoreCase("FEB") || month.equalsIgnoreCase("MAR") || month.equalsIgnoreCase("APR") || month.equalsIgnoreCase("MAY"));
}
}
private static void interpreter(Scanner s) {
updateDT();
while (true) {
System.out.print("\nWhat do you want to do? Enter H for list of commands. ");
String cmd = s.nextLine();
if (cmd.equalsIgnoreCase("H")) {
System.out.println("A - Add a task\n"
+ "C - Check conditions of house and shield\n"
+ "D - Delete a task\n"
+ "F - Finish a task\n"
+ "H - Display all available commands\n"
+ "S - Show tasks in the semester\n"
+ "T - Display current date and time\n"
+ "/ - End the program");
} else if (cmd.equalsIgnoreCase("A")) {
addTask(s);
} else if (cmd.equalsIgnoreCase("C")) {
conditionReport();
} else if (cmd.equalsIgnoreCase("D")) {
deleteTask(s);
} else if (cmd.equalsIgnoreCase("F")) {
finishTask(s);
} else if (cmd.equalsIgnoreCase("S")) {
showTasks(s);
} else if (cmd.equalsIgnoreCase("T")) {
updateDT();
} else if (cmd.equalsIgnoreCase("/")) {
System.out.println("Do you want to save the current semester?");
System.out.print("Y - Yes\n"
+ "N - NO\n"
+ "Any other key - Stay\n");
String save = s.nextLine();
if (save.equalsIgnoreCase("Y")) {
saveAsXML();
System.out.println("Bye!");
break;
} else if (save.equalsIgnoreCase("N")) {
System.out.println("Bye!");
break;
}
} else {
System.out.println("Not a valid command.");
}
}
}
private static boolean calculateDamage() {
return false; // TBD
}
private static void addTask(Scanner s) {
System.out.println("\nEnter the due date of the task: ");
System.out.print("Month? (Enter the first three letter) ");
String m = s.nextLine();
if (validMonth(m)) {
int month = monthIndex(m);
System.out.print("Date? ");
int date = Integer.parseInt(s.nextLine());
if (date > 0 && date < 32) {
System.out.print("Name of the task? ");
String task = s.nextLine();
calendar.addTask(month, date, task, false);
} else {
System.out.println("Not a valid date. Try again.");
}
} else {
System.out.println("Not a valid month. Try again.");
}
}
private static void conditionReport() {
System.out.println("Shield completion: " + shield.getCompletion(calendar) + "%");
house.stateStatus();
}
private static void deleteTask(Scanner s) {
System.out.println("\nEnter the due date of the task: ");
System.out.print("Month? (Enter the first three letter) ");
String m = s.nextLine();
if (validMonth(m)) {
int month = monthIndex(m);
System.out.print("Date? ");
int date = Integer.parseInt(s.nextLine());
if (date > 0 && date < 32) {
calendar.showTask(month, date);
if (calendar.getDayTotal(month, date) != 0) {
System.out.println("Which task are you deleting? ");
System.out.print("Enter the index of the task or enter 0 to cancel the command: ");
int index = Integer.parseInt(s.nextLine());
calendar.deleteTask(month, date, index - 1);
}
} else {
System.out.println("Not a valid date. Try again.");
}
} else {
System.out.println("Not a valid month. Try again.");
}
}
private static void finishTask(Scanner s) {
System.out.println("\nEnter the due date of the task: ");
System.out.print("Month? (Enter the first three letter) ");
String m = s.nextLine();
if (validMonth(m)) {
int month = monthIndex(m);
System.out.print("Date? ");
int date = Integer.parseInt(s.nextLine());
if (date > 0 && date < 32) {
calendar.showTask(month, date);
if (calendar.getDayTotal(month, date) != 0) {
System.out.println("Which task are you finishing? ");
System.out.print("Enter the index of the task or enter 0 to cancel the command: ");
int index = Integer.parseInt(s.nextLine());
calendar.finishTask(month, date, index - 1);
}
} else {
System.out.println("Not a valid date. Try again.");
}
} else {
System.out.println("Not a valid month. Try again.");
}
}
private static void showTasks(Scanner s) {
System.out.println("Enter:\n"
+ "A - all tasks in the semester\n"
+ "D - all tasks that are done\n"
+ "N - all tasks that are not done\n"
+ "U - Urgent tasks (Past due)\n"
+ "M - all tasks in a month\n"
+ "T - today's tasks\n"
+ "Any other key - cancel the action\n");
String cmd = s.nextLine();
if (cmd.equalsIgnoreCase("A")) {
calendar.showAllTask();
} else if (cmd.equalsIgnoreCase("D")) {
System.out.println("TBD...");
} else if (cmd.equalsIgnoreCase("N")) {
System.out.println("TBD...");
} else if (cmd.equalsIgnoreCase("U")) {
System.out.println("TBD...");
} else if (cmd.equalsIgnoreCase("M")) {
System.out.println("TBD...");
} else if (cmd.equalsIgnoreCase("T")) {
System.out.println("TBD...");
} else {
}
}
private static void saveAsXML() {
try {
// create new `Document`
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document dom = builder.newDocument();
// first create root element
Element root = dom.createElement("schedule");
dom.appendChild(root);
// now create child elements (name, email, phone)
Element cal = dom.createElement("calendar");
Attr semester = dom.createAttribute("semester");
semester.setValue(calendar.getSemester());
cal.setAttributeNode(semester);
Attr year = dom.createAttribute("year");
year.setValue(Integer.toString(semYear));
cal.setAttributeNode(year);
Element month1 = dom.createElement("month1");
Attr m1name = dom.createAttribute("name");
m1name.setValue(calendar.getMonth(1).getName());
month1.setAttributeNode(m1name);
for (Map.Entry day : calendar.getMonth(1).getHashmap().entrySet()) {
Element entry = dom.createElement("entry");
Attr date = dom.createAttribute("date");
date.setValue(Integer.toString((int) day.getKey()));
entry.setAttributeNode(date);
for (Task t : (List<Task>) day.getValue()) {
Element task = dom.createElement("task");
Attr name = dom.createAttribute("name");
name.setValue(t.getName());
task.setAttributeNode(name);
Attr done = dom.createAttribute("done");
done.setValue(Boolean.toString(t.isDone()));
task.setAttributeNode(done);
entry.appendChild(task);
}
month1.appendChild(entry);
}
Element month2 = dom.createElement("month2");
Attr m2name = dom.createAttribute("name");
m2name.setValue(calendar.getMonth(2).getName());
month2.setAttributeNode(m2name);
for (Map.Entry day : calendar.getMonth(2).getHashmap().entrySet()) {
Element entry = dom.createElement("entry");
Attr date = dom.createAttribute("date");
date.setValue(Integer.toString((int) day.getKey()));
entry.setAttributeNode(date);
for (Task t : (List<Task>) day.getValue()) {
Element task = dom.createElement("task");
Attr name = dom.createAttribute("name");
name.setValue(t.getName());
task.setAttributeNode(name);
Attr done = dom.createAttribute("done");
done.setValue(Boolean.toString(t.isDone()));
task.setAttributeNode(done);
entry.appendChild(task);
}
month2.appendChild(entry);
}
Element month3 = dom.createElement("month3");
Attr m3name = dom.createAttribute("name");
m3name.setValue(calendar.getMonth(3).getName());
month3.setAttributeNode(m3name);
for (Map.Entry day : calendar.getMonth(3).getHashmap().entrySet()) {
Element entry = dom.createElement("entry");
Attr date = dom.createAttribute("date");
date.setValue(Integer.toString((int) day.getKey()));
entry.setAttributeNode(date);
for (Task t : (List<Task>) day.getValue()) {
Element task = dom.createElement("task");
Attr name = dom.createAttribute("name");
name.setValue(t.getName());
task.setAttributeNode(name);
Attr done = dom.createAttribute("done");
done.setValue(Boolean.toString(t.isDone()));
task.setAttributeNode(done);
entry.appendChild(task);
}
month3.appendChild(entry);
}
Element month4 = dom.createElement("month4");
Attr m4name = dom.createAttribute("name");
m4name.setValue(calendar.getMonth(4).getName());
month4.setAttributeNode(m4name);
for (Map.Entry day : calendar.getMonth(4).getHashmap().entrySet()) {
Element entry = dom.createElement("entry");
Attr date = dom.createAttribute("date");
date.setValue(Integer.toString((int) day.getKey()));
entry.setAttributeNode(date);
for (Task t : (List<Task>) day.getValue()) {
Element task = dom.createElement("task");
Attr name = dom.createAttribute("name");
name.setValue(t.getName());
task.setAttributeNode(name);
Attr done = dom.createAttribute("done");
done.setValue(Boolean.toString(t.isDone()));
task.setAttributeNode(done);
entry.appendChild(task);
}
month4.appendChild(entry);
}
Element month5 = dom.createElement("month5");
Attr m5name = dom.createAttribute("name");
m5name.setValue(calendar.getMonth(5).getName());
month5.setAttributeNode(m5name);
for (Map.Entry day : calendar.getMonth(5).getHashmap().entrySet()) {
Element entry = dom.createElement("entry");
Attr date = dom.createAttribute("date");
date.setValue(Integer.toString((int) day.getKey()));
entry.setAttributeNode(date);
for (Task t : (List<Task>) day.getValue()) {
Element task = dom.createElement("task");
Attr name = dom.createAttribute("name");
name.setValue(t.getName());
task.setAttributeNode(name);
Attr done = dom.createAttribute("done");
done.setValue(Boolean.toString(t.isDone()));
task.setAttributeNode(done);
entry.appendChild(task);
}
month5.appendChild(entry);
}
cal.appendChild(month1);
cal.appendChild(month2);
cal.appendChild(month3);
cal.appendChild(month4);
cal.appendChild(month5);
// add child nodes to root node
root.appendChild(cal);
// write DOM to XML file
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.transform(new DOMSource(dom), new StreamResult(new File(calendar.getSemester() + semYear + ".xml")));
System.out.println("File is saved as " + calendar.getSemester() + semYear + ".xml");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void readXML(String filename) {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = saxParserFactory.newSAXParser();
MyHandler handler = new MyHandler();
File input = new File(filename);
saxParser.parse(input, handler);
calendar = handler.getCalendar();
} catch (Exception e) {
e.printStackTrace();
}
}
}