-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
172 lines (151 loc) · 4.16 KB
/
Model.java
File metadata and controls
172 lines (151 loc) · 4.16 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
/*
* CS151 - HW4
* @author Thy Nguyen
*/
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.Scanner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Model {
public static String FILE_NAME = "src/events.txt";
public static MONTHS[] monthArr = MONTHS.values();
public static DAYS[] dayArr = DAYS.values();
public GregorianCalendar gc = new GregorianCalendar();
private ArrayList<Event> events;
private ArrayList<GregorianCalendar> datesOfEvent;
private ArrayList<ChangeListener> listeners;
public Model(){
events = new ArrayList<>();
listeners = new ArrayList<>();
datesOfEvent = new ArrayList<>();
}
//accessor
public ArrayList<Event> getData(){
return events;
}
public ArrayList<GregorianCalendar> getDatesOfEvent(){
return datesOfEvent;
}
public void attach(ChangeListener l) {
listeners.add(l);
}
//mutator
public void addEvent(Event e) {
//System.out.println(e.toString());
events.add(e);
sortEventList(events);
writeToFile();
}
public void update() {
for(ChangeListener l: listeners) {
l.stateChanged(new ChangeEvent(this));
}
}
private static void sortEventList(ArrayList<Event> list) {
Comparator<Event> comp = new Comparator<Event>() {
public int compare(Event e1, Event e2) {
return e1.compareTo(e2);
}
};
Collections.sort(list, comp);
}
public ArrayList<Event> searchEvent(int day, int mon, int year) {
sortEventList(events);
ArrayList<Event> foundArr = new ArrayList<>();
for (int i = 0; i < events.size(); i++) {
if (events.get(i).getDate().get(Calendar.MONTH) == mon && events.get(i).getDate().get(Calendar.DAY_OF_MONTH) == day
&& events.get(i).getDate().get(Calendar.YEAR) == year)
foundArr.add(events.get(i));
}
return foundArr;
}
/**
* To load the events.txt and add all the events into array list
*/
public void loadFile() {
ArrayList<String> arr = getStringFromFile();
if (arr.size() == 0)
System.out.println("Loadfile This is the first run");
else {
for (int i = 0; i < arr.size(); i++) {
String line = arr.get(i);
String[] column = line.split("\t");
String month = String.valueOf(getIndex(column[1]));
String day = column[2];
String year = column[3];
Event e = new Event(column[0], day, month, year);
e.setStartTime(column[4]);
if (column.length == 6) {
e.setEndTime(column[5]);
}
events.add(e);
} // end for
sortEventList(events);
System.out.println("Loadfile debug Successfully loading file");
}
}
/**
* To save all the events newly-created into the array list of events
*/
public void writeToFile() {
FileOutputStream outFile = null;
try {
outFile = new FileOutputStream(FILE_NAME);
} catch (FileNotFoundException fnf) {
System.err.println("File cannot be opened!");
return;
}
PrintWriter pW = new PrintWriter(outFile, false);
sortEventList(events);
for (int i = 0; i < events.size(); i++) {
Event cur = events.get(i);
String endtime = "";
if (cur.getEndTime() != null)
endtime = cur.getEndTime();
pW.println(cur.getTitle() + "\t" + monthArr[cur.getDate().get(Calendar.MONTH)].toString() + "\t"
+ cur.getDate().get(Calendar.DAY_OF_MONTH) + "\t" + cur.getDate().get(Calendar.YEAR) + "\t"
+ cur.getStartTime() + "\t" + endtime);
}
pW.close();
}
//this will be used for the listener of the day button
private static ArrayList<String> getStringFromFile() {
ArrayList<String> strings = new ArrayList<>();
java.io.File file = new java.io.File(FILE_NAME);
Scanner fileSc = null;
try {
fileSc = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("File not found");
strings = null;
}
String line = "";
while (fileSc.hasNextLine()) {
line = fileSc.nextLine();
strings.add(line);
}
fileSc.close();
return strings;
}
private int getIndex(String s) {
int result = 0;
for (int i = 0; i < monthArr.length; i++) {
if (s.equals(monthArr[i].toString())) {
result = i;
break;
}
}
return result;
}
public void previousDay() {
gc.add(Calendar.DAY_OF_MONTH, -1);
this.update();
}
}//end class