forked from serenapascual/cs151-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventModel.java
More file actions
232 lines (207 loc) · 4.42 KB
/
EventModel.java
File metadata and controls
232 lines (207 loc) · 4.42 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
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
/**
*
* @author Himanshu Mehta, Serena Pascual and Cherie Sew
*
*/
public class EventModel {
private ArrayList<Event> events = new ArrayList<>();
private Calendar cal;
private MainView view;
private ViewTypes viewType;
private Calendar agendaStart;
private Calendar agendaEnd;
public enum ViewTypes {
DAY, WEEK, MONTH, AGENDA
}
/**
* MVC model of calendar program
*/
public EventModel() {
cal = new GregorianCalendar();
viewType = ViewTypes.DAY;
}
/**
* Set the different view types
* @param t the view type for the events panel
*/
public void setViewType(ViewTypes t) {
viewType = t;
}
/**
* Get the view type
* @return the view type for the events panel
*/
public ViewTypes getViewType() {
return viewType;
}
/**
* Get start date for agenda
* @return start date for agenda
*/
public Calendar getAgendaStart() {
return agendaStart;
}
/**
* Get end date for agenda
* @return end date for agenda
*/
public Calendar getAgendaEnd() {
return agendaEnd;
}
/**
* Set date for Agenda
* @param start - start date of agenda
* @param end - end date of agenda
*/
public void setAgenda(Calendar start, Calendar end) {
agendaStart = start;
agendaEnd = end;
}
/**
* Set day in calendar
* @param day
*/
public void setDay(int day) {
cal.set(Calendar.DAY_OF_MONTH, day);
view.repaint();
}
/**
* Set current date in calendar
* @param day - current day
* @param month - current month
* @param year - current year
*/
public void setToday(int day, int month, int year) {
cal.set(year, month, day);
view.repaint();
}
/**
* Get the previous day in the calendar
*/
public void previousDay() {
cal.add(Calendar.DAY_OF_MONTH, -1);
view.repaint();
}
/**
* Get the next day in the calendar
*/
public void nextDay() {
cal.add(Calendar.DAY_OF_MONTH, 1);
view.repaint();
}
/**
* Get the next month in the calendar
*/
public void nextMonth() {
cal.add(Calendar.MONTH, 1);
view.repaint();
}
/**
* Get previous month in the calendar
*/
public void previousMonth() {
cal.add(Calendar.MONTH, -1);
view.repaint();
}
public Calendar getCal() {
return cal;
}
/**
* Add events into list
* @param e the event to be added
*/
public void addEvent(Event e) {
events.add(e);
Collections.sort(events);
view.repaint();
}
/**
* Set layout of events panel
* @param view - the layout of the events panel
*/
public void setView(MainView view) {
this.view = view;
}
/**
* Get the layout of events panel
* @return main view
*/
public MainView getView() {
return this.view;
}
/**
* List of events
* @return all events in the arraylist
*/
public ArrayList<Event> getEvents() {
return events;
}
/**
* Quits the program
*/
public void quit() {
Loader load = new Loader();
load.saveEvent(events);
}
/**
* Populate the events panel with events
* Recurring event loaded from file
* @param values
*/
public void populateEvents(String values) {
String[] val = values.split(";");
String description = val[0];
int year = Integer.parseInt(val[1]);
int startMonth = Integer.parseInt(val[2]);
int endMonth = Integer.parseInt(val[3]);
String dayOfWeek = val[4];
String startTime = val[5];
String endTime = val[6];
for(int i = startMonth; i <= endMonth; i++) {
cal.set(year, i-1, 1);
int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for(int j = 1; j <= daysInMonth; j++) {
if(dayOfWeek.contains(Weekday(year, i, j))) {
Event e = new Event(description, year, i, j, startTime, endTime+"");
events.add(e);
}
}
}
}
public String getWeekDay(int year, int month, int day) {
return Weekday(year, month, day);
}
/**
* Get day of the week
* @param year
* @param month
* @param day
* @return
*/
private String Weekday(int year, int month, int day) {
cal.set(year, month - 1, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
switch (dayOfWeek) {
case 1:
return "S";
case 2:
return "M";
case 3:
return "T";
case 4:
return "W";
case 5:
return "H";
case 6:
return "F";
case 7:
return "A";
default:
return null;
}
}
}