-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppointmentBookGUI.java
More file actions
786 lines (676 loc) · 24 KB
/
Copy pathAppointmentBookGUI.java
File metadata and controls
786 lines (676 loc) · 24 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
import java.util.Calendar;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.YearMonth;
import java.util.GregorianCalendar;
import java.util.Arrays;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ButtonGroup;
import javax.swing.event.*;
import javax.swing.*;
public class AppointmentBookGUI extends JFrame implements ItemListener, ActionListener {
private JPanel initial, show, add;
private static JTextField titleField;
private static JTextField descriptionField;
private static JTextField minuteField, hourField, dayField, monthField, yearField;
private static JComboBox amOrPm, eventTypeOptions;
private File workingFile;
private String fileName;
private String mainFilePath;
private AppointmentBook appBook;
private ButtonGroup actions;
private JPanel lower;
private JTabbedPane navigation;
private JComboBox[] entryBoxes;
private JTextArea schedule;
private JButton showSchedule;
private File myFile;
private AppointmentBook myAppBook;
private static final int STARTING_MONTHS = 0;
private static final int ENDING_MONTHS = 1;
private static final int STARTING_DAYS = 2;
private static final int ENDING_DAYS = 3;
private static final int STARTING_YEARS = 4;
private static final int ENDING_YEARS = 5;
private String startDay, endDay, startYear, endYear, startMonth, endMonth;
public AppointmentBookGUI(AppointmentBook... ab) {
//navigation.addTab("Remove Appointments", remove);
initializeFrame();
// INITIAL
//
//
// INITIAL
JButton createNewFile = new JButton("Create new file.");
createNewFile.addActionListener(new CreateFileListener());
JButton openOldFile = new JButton("Open previous file.");
//TODO: Add implementation for this method.
openOldFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
ObjectInputStream in = null;
FileInputStream fin = null;
try {
fileName = JOptionPane.showInputDialog("What will the file name be?") + ".ser";
myFile = new File(fileName);
myAppBook = new AppointmentBook();
fin = new FileInputStream(myFile);
in = new ObjectInputStream(fin);
myAppBook = (AppointmentBook) in.readObject();
//TODO: verify successful connection
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(initial, "Error. Please try again.");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(initial, "Error. Please try again.");
} catch (IOException e) {
JOptionPane.showMessageDialog(initial, "Error. Please try again.");
} finally {
try {
if (in != null) in.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(initial, "Error. Please try again.");
}
}
//fileName = JOptionPane.showInputDialog("What is the name of this file? Please enter the absolute path of the file.") + ".txt";
}
});
initial.add(createNewFile);
initial.add(openOldFile);
// SHOW
//
//
// SHOW
String[] startingMonths = {"Select a starting month", "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String[] endingMonths = {"Select an ending month", "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String[] startingYears = new String[87], endingYears = new String[87];
startingYears[0] = "Select a starting year"; startingYears[1] = "";
endingYears[0] = "Select an ending year"; endingYears[1] = "";
for (int i = 2016; i <= 2100; i++) {
startingYears[i - 2014] = i + "";
endingYears[i - 2014] = i + "";
}
String[] startingDays = new String[33], endingDays = new String[33];
startingDays[0]="Select a starting day"; startingDays[1]="";
endingDays[0]="Select an ending day"; endingDays[1] = "";
for (int i = 3; i <= 33; i++) {
startingDays[i-1] = (i-2) + "";
endingDays[i-1] = (i-2) + "";
}
JPanel innerTable = new JPanel(new GridLayout(3, 2));
entryBoxes = new JComboBox[] {
new JComboBox(startingMonths),
new JComboBox(endingMonths),
new JComboBox(startingDays),
new JComboBox(endingDays),
new JComboBox(startingYears),
new JComboBox(endingYears)
};
//innerTable.setBackground(Color.BLACK);
for (JComboBox j : entryBoxes) {
innerTable.add(j);
j.addItemListener(this);
}
show.add(innerTable);
// add everything in the text panel
JPanel textPanel = new JPanel(new BorderLayout());
schedule = new JTextArea("Test");
JScrollPane tempScrollPane = new JScrollPane(schedule);
textPanel.add(tempScrollPane);
show.add(textPanel);
// add the schedule button
JPanel showScheduleButton = new JPanel(new BorderLayout());
showSchedule = new JButton("Show schedule");
showSchedule.addActionListener(new ActionListener() {
//TODO: Add verification mechanism.
@Override
public void actionPerformed(ActionEvent ae) {
FileInputStream fis = null;
ObjectInputStream in = null;
System.out.println("File connection: " + myFile.exists());
try {
fis = new FileInputStream(myFile);
in = new ObjectInputStream(fis);
myAppBook = (AppointmentBook) in.readObject();
System.out.println("here 1");
int sYear = Integer.parseInt((String)entryBoxes[STARTING_YEARS].getSelectedItem());
int eYear = Integer.parseInt((String)entryBoxes[ENDING_YEARS].getSelectedItem());
int sDay = Integer.parseInt((String)entryBoxes[STARTING_DAYS].getSelectedItem());
int eDay = Integer.parseInt((String)entryBoxes[ENDING_DAYS].getSelectedItem());
int sMonth = AppointmentBookGUI.monthToNumber((String)entryBoxes[STARTING_MONTHS].getSelectedItem());
int eMonth = AppointmentBookGUI.monthToNumber((String)entryBoxes[ENDING_MONTHS].getSelectedItem());
System.out.println(sYear);
System.out.println(eYear);
System.out.println(sDay);
System.out.println(eDay);
System.out.println(sMonth);
System.out.println(eMonth);
//String str = myAppBook.toString(sYear, eYear, sDay, eDay, sMonth, eMonth);
//System.out.println(str);
//myAppBook.displayAppointmentsFromTo(sYear, eYear, sDay, eDay, sMonth, eMonth);
schedule.setText(myAppBook.toString(
Integer.parseInt((String)entryBoxes[STARTING_YEARS].getSelectedItem()),
Integer.parseInt((String)entryBoxes[ENDING_YEARS].getSelectedItem()),
Integer.parseInt((String)entryBoxes[STARTING_DAYS].getSelectedItem()),
Integer.parseInt((String)entryBoxes[ENDING_DAYS].getSelectedItem()),
AppointmentBookGUI.monthToNumber((String)entryBoxes[STARTING_MONTHS].getSelectedItem()),
AppointmentBookGUI.monthToNumber((String)entryBoxes[ENDING_MONTHS].getSelectedItem())
));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
showScheduleButton.add(showSchedule);
show.add(showScheduleButton);
// ADD TAB
//
//
// ADD TAB
JPanel entryFields = new JPanel(new GridLayout(10, 2));
entryFields.add(new JLabel("Title: "));
titleField = new JTextField();
entryFields.add(titleField);
entryFields.add(new JLabel("Description: "));
descriptionField = new JTextField();
entryFields.add(descriptionField);
// entryFields.add(new JLabel("test"));
// entryFields.add(new JLabel("test2"));
entryFields.add(new JLabel("Event Type: "));
eventTypeOptions = new JComboBox(new String[] {"Select an event type", "", "Onetime", "Monthly", "Daily"});
entryFields.add(eventTypeOptions);
entryFields.add(new JLabel("Day: "));
dayField = new JTextField();
dayField.setEditable(true);
entryFields.add(dayField);
entryFields.add(new JLabel("Month: "));
monthField = new JTextField();
monthField.setEditable(true);
entryFields.add(monthField);
entryFields.add(new JLabel("Year: "));
yearField = new JTextField(Calendar.getInstance().get(Calendar.YEAR) + "");
yearField.setEditable(true);
entryFields.add(yearField);
entryFields.add(new JLabel("Hour: "));
hourField = new JTextField();
entryFields.add(hourField);
entryFields.add(new JLabel("Minute: "));
minuteField = new JTextField();
entryFields.add(minuteField);
entryFields.add(new JLabel("AM or PM: "));
amOrPm = new JComboBox(new String[] {"Select AM or PM", "", "AM", "PM"});
entryFields.add(amOrPm);
JButton clearButton = new JButton("Clear");
JButton submitButton = new JButton("Submit");
entryFields.add(submitButton);
entryFields.add(clearButton);
clearButton.addActionListener(new ClearButtonListener()); /*{
@Override public void actionPerformed(ActionEvent ae) {
titleField.setText("");
descriptionField.setText("");
yearField.setText(Calendar.getInstance().get(Calendar.YEAR) + "");
monthField.setText("");
dayField.setText("");
hourField.setText("");
minuteField.setText("");
amOrPm.setSelectedIndex(0);
eventTypeOptions.setSelectedIndex(0);
}
});*/
submitButton.addActionListener(new SubmitButtonListener()); /*{
@Override
public void actionPerformed(ActionEvent ae) {
boolean dataVerified = allFieldsVerified();
System.out.println(dataVerified);
if (dataVerified) {
String title = titleField.getText();
String description = descriptionField.getText();
int year = Integer.parseInt(yearField.getText());
int month = AppointmentBookGUI.monthToNumber(monthField.getText());
int day = Integer.parseInt(dayField.getText());
int hour = Integer.parseInt(hourField.getText());
int minute = Integer.parseInt(minuteField.getText());
boolean isAM = (amOrPm.getSelectedItem().equals("AM"))? true : false;
if (eventTypeOptions.getSelectedItem().equals("Onetime")) {
} else if ( eventTypeOptions.getSelectedItem().equals("Monthly")) {
FileOutputStream fos = null;
ObjectOutputStream out = null;
FileInputStream fis = null;
ObjectInputStream in = null;
System.out.println("File connection: " + myFile.exists());
try {
fis = new FileInputStream(myFile);
in = new ObjectInputStream(fis);
myAppBook = (AppointmentBook) in.readObject();
myAppBook.add(new Monthly(title, description, day, hour, minute, isAM));
fos = new FileOutputStream(myFile);
out = new ObjectOutputStream(fos);
out.writeObject(myAppBook);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else if (eventTypeOptions.getSelectedItem().equals("Daily")) {
}
titleField.setText("");
descriptionField.setText("");
yearField.setText(Calendar.getInstance().get(Calendar.YEAR) + "");
monthField.setText("");
dayField.setText("");
hourField.setText("");
minuteField.setText("");
amOrPm.setSelectedIndex(0);
eventTypeOptions.setSelectedIndex(0);
JOptionPane.showMessageDialog(add, "Successful submission!");
}
}
public boolean minuteFieldIsVerified() {
try {
int minutes = Integer.parseInt(minuteField.getText());
return minutes < 60 && minutes >= 0;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(add, "Invalid minute field.");
return false;
}
}
public boolean hourFieldIsVerified() {
try {
int hours = Integer.parseInt(hourField.getText());
return hours <= 12 && hours > 0;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(add, "Invalid hour field.");
return false;
}
}
public boolean dateIsVerified() {
try {
int year = Integer.parseInt(yearField.getText());
int month = AppointmentBookGUI.monthToNumber(monthField.getText());
int day = Integer.parseInt(dayField.getText());
YearMonth ym = YearMonth.of(year, month);
return ym.isValidDay(day);
} catch (NumberFormatException e) {
return false;
}
}
public boolean titleIsVerified() {
return (titleField.getText() != null) && (titleField.getText() != "");
}
public boolean descriptionIsVerified() {
return (descriptionField.getText() != null) && (descriptionField.getText() != "");
}
public boolean amPmIsVerified() {
return amOrPm.getSelectedIndex() != 0 && amOrPm.getSelectedIndex() != 1;
}
public boolean eventTypeOptionsIsVerified() {
return eventTypeOptions.getSelectedIndex() != 0 && eventTypeOptions.getSelectedIndex() != 1;
}
public boolean allFieldsVerified() {
boolean debug = true;
if (debug) {
System.out.println(minuteFieldIsVerified());
System.out.println(hourFieldIsVerified());
System.out.println(dateIsVerified());
System.out.println(titleIsVerified());
System.out.println(descriptionIsVerified());
System.out.println(amPmIsVerified());
System.out.println(eventTypeOptionsIsVerified());
}
return minuteFieldIsVerified()
&& hourFieldIsVerified()
&& dateIsVerified()
&& titleIsVerified()
&& descriptionIsVerified()
&& amPmIsVerified()
&& eventTypeOptionsIsVerified();
}
});*/
add.add(entryFields);
setVisible(true);
}
public void initializeFrame() {
navigation = new JTabbedPane();
this.getContentPane().add(navigation);
this.setTitle("Appointment Book");
this.setSize(600, 400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initial = new JPanel(new GridLayout(2, 1));
show = new JPanel(new GridLayout(3, 1));
add = new JPanel(new GridLayout(1,1));
navigation.addTab("Start Here", initial);
navigation.addTab("Show Appointments", show);
navigation.addTab("Add Appointments", add);
}
public static void main(String[] args) {
new AppointmentBookGUI();
}
public void updateStartEndData(String s, JComboBox j) {
switch ((String) j.getItemAt(0)) {
case "Select a starting month":
System.out.println("Starting month == " + s);
if (s != "Select a starting month" && s!= "" ) startMonth = s;
break;
case "Select an ending month":
if (s != "Select an ending month" && s!= "" ) endMonth = s;
break;
case "Select a starting day":
if (s != "Select an starting day" && s!= "" ) startDay = s;
break;
case "Select an ending day":
if (s != "Select an ending day" && s!= "" ) endDay = s;
break;
case "Select a starting year":
if (s != "Select a starting year" && s!= "" ) startYear = s;
break;
case "Select an ending year":
if (s != "Select an ending year" && s!= "" ) endYear = s;
break;
default:
break;
}
}
/**
* Clears all entries under the "add" tab.
* To be used after submission or when the clear button is pressed.
*/
public static void clearEntries() {
titleField.setText("");
descriptionField.setText("");
yearField.setText(Calendar.getInstance().get(Calendar.YEAR) + "");
monthField.setText("");
dayField.setText("");
hourField.setText("");
minuteField.setText("");
amOrPm.setSelectedIndex(0);
eventTypeOptions.setSelectedIndex(0);
}
/**
* Action listener for the "clear" button.
*/
class ClearButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
titleField.setText("");
descriptionField.setText("");
yearField.setText(Calendar.getInstance().get(Calendar.YEAR) + "");
monthField.setText("");
dayField.setText("");
hourField.setText("");
minuteField.setText("");
amOrPm.setSelectedIndex(0);
eventTypeOptions.setSelectedIndex(0);
}
}
/**
* Checks for a click on the submit button, and then opens the object located in myFile
* to add the new event to. Also verifies the data of the submit boxes.
*/
class SubmitButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
boolean dataVerified = allFieldsVerified();
System.out.println(dataVerified);
if (dataVerified) {
String title = titleField.getText();
String description = descriptionField.getText();
int year = Integer.parseInt(yearField.getText());
int month = AppointmentBookGUI.monthToNumber(monthField.getText());
int day = Integer.parseInt(dayField.getText());
int hour = Integer.parseInt(hourField.getText());
int minute = Integer.parseInt(minuteField.getText());
boolean isAM = (amOrPm.getSelectedItem().equals("AM"))? true : false;
if (eventTypeOptions.getSelectedItem().equals("Onetime")) {
} else if (eventTypeOptions.getSelectedItem().equals("Monthly")) {
FileOutputStream fos = null;
ObjectOutputStream out = null;
FileInputStream fis = null;
ObjectInputStream in = null;
System.out.println("File connection: " + myFile.exists());
try {
fis = new FileInputStream(myFile);
in = new ObjectInputStream(fis);
myAppBook = (AppointmentBook) in.readObject();
myAppBook.add(new Monthly(title, description, day, hour, minute, isAM));
fos = new FileOutputStream(myFile);
out = new ObjectOutputStream(fos);
out.writeObject(myAppBook);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else if (eventTypeOptions.getSelectedItem().equals("Daily")) {
}
titleField.setText("");
descriptionField.setText("");
yearField.setText(Calendar.getInstance().get(Calendar.YEAR) + "");
monthField.setText("");
dayField.setText("");
hourField.setText("");
minuteField.setText("");
amOrPm.setSelectedIndex(0);
eventTypeOptions.setSelectedIndex(0);
JOptionPane.showMessageDialog(add, "Successful submission!");
}
}
public boolean minuteFieldIsVerified() {
try {
int minutes = Integer.parseInt(minuteField.getText());
return minutes < 60 && minutes >= 0;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(add, "Invalid minute field.");
return false;
}
}
public boolean hourFieldIsVerified() {
try {
int hours = Integer.parseInt(hourField.getText());
return hours <= 12 && hours > 0;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(add, "Invalid hour field.");
return false;
}
}
public boolean dateIsVerified() {
boolean debug = true;
try {System.out.println("here");
int year = Integer.parseInt(yearField.getText());
System.out.println("here2");
int month = AppointmentBookGUI.monthToNumber(monthField.getText());
System.out.println("here3");
int day = Integer.parseInt(dayField.getText());
YearMonth ym = YearMonth.of(year, month);
if (debug) System.out.println("Year of " + year + " and month of " + month + " and day of " + day + "is valid: " + ym.isValidDay(day));
return ym.isValidDay(day);
} catch (NumberFormatException e) {
return false;
}
}
public boolean titleIsVerified() {
return (titleField.getText() != null) && (titleField.getText() != "");
}
public boolean descriptionIsVerified() {
try {
return (descriptionField.getText() != null) && (descriptionField.getText() != "");}
catch (NullPointerException e) {
System.out.println("Desc field: " + descriptionField);
} return false;
}
public boolean amPmIsVerified() {
return amOrPm.getSelectedIndex() != 0 && amOrPm.getSelectedIndex() != 1;
}
public boolean eventTypeOptionsIsVerified() {
return eventTypeOptions.getSelectedIndex() != 0 && eventTypeOptions.getSelectedIndex() != 1;
}
public boolean allFieldsVerified() {
boolean debug = true;
if (debug) {
System.out.println(minuteFieldIsVerified());
System.out.println(hourFieldIsVerified());
System.out.println(dateIsVerified());
System.out.println(titleIsVerified());
System.out.println(descriptionIsVerified());
System.out.println(amPmIsVerified());
System.out.println(eventTypeOptionsIsVerified());
}
return minuteFieldIsVerified()
&& hourFieldIsVerified()
&& dateIsVerified()
&& titleIsVerified()
&& descriptionIsVerified()
&& amPmIsVerified()
&& eventTypeOptionsIsVerified();
}
}
class CreateFileListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
ObjectOutputStream out = null;
FileOutputStream fos = null;
try {
fileName = JOptionPane.showInputDialog("What will the file name be?") + ".ser";
myFile = new File(fileName);
boolean fileCreationSuccessful = myFile.createNewFile();
myAppBook = new AppointmentBook();
fos = new FileOutputStream(myFile);
out = new ObjectOutputStream(fos);
out.writeObject(myAppBook);
//TODO: check if the file creation was successful.
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(initial, "Error. Please try again.");
} catch (IOException e) {
JOptionPane.showMessageDialog(initial, "Error. Please try again.");
} finally {
try {
if (out != null) out.close();
} catch (IOException e) {
}
}
}
}
@Override
public void itemStateChanged(ItemEvent e) throws NullPointerException {
String s = (String) e.getItem();
JComboBox currentComboBox = (JComboBox) e.getItemSelectable();
for (JComboBox j : entryBoxes) {
if (e.getStateChange() == ItemEvent.SELECTED
&&((String) j.getItemAt(0)).equals((String) currentComboBox.getItemAt(0))) {
// this means that the same comboboxes are being accessed right now
updateStartEndData(s, j);
}
}
System.out.println(dataIsVerified());
System.out.println(s);
}
public static int monthToNumber(String s) {
int rn = 0;
switch (s.toLowerCase()) {
case "january":
return 1;
case "february":
return 2;
case "march":
return 3;
case "april":
return 4;
case "may":
return 5;
case "june":
return 6;
case "july":
return 7;
case "august":
return 8;
case "september":
return 9;
case "october":
return 10;
case "november":
return 11;
case "december":
return 12;
default:
throw new NumberFormatException();
}
}
public boolean dataIsVerified() {
try {
int startYearInt = Integer.parseInt(startYear);
int startMonthInt = monthToNumber(startMonth);
int startDayInt = Integer.parseInt(startDay);
int endYearInt = Integer.parseInt(endYear);
int endMonthInt = monthToNumber(endMonth);
int endDayInt = Integer.parseInt(endDay);
System.out.println(startYearInt);
System.out.println(startMonthInt);
System.out.println(startDayInt);
System.out.println(endYearInt);
System.out.println(endMonthInt);
System.out.println(endDayInt);
YearMonth startYM = YearMonth.of(startYearInt, startMonthInt);
YearMonth endYM = YearMonth.of(endYearInt, endMonthInt);
if (!(startYM.isValidDay(startDayInt) && endYM.isValidDay(endDayInt))) {System.out.println("here!");return false;}
return (startYM.isBefore(endYM));
} catch (NumberFormatException e) {
return false;
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (dataIsVerified()) {
// show the schedule in the text area
schedule.setText("test");
} else {
// give a warning pop-up box.
JOptionPane.showMessageDialog(this, "Sorry, invalid year! Please fix.");
}
}
}