-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataReader.java
230 lines (216 loc) · 12.4 KB
/
DataReader.java
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
import java.io.FileReader;
import java.util.ArrayList;
import java.util.UUID;
import java.util.HashMap;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
/**
* @author Andrew Chisolm, Bryan Munoz, Matt Olson, Daniel Ruiz, Jaden Arnold
* The data writer for JSON files that gets information from the arraylist
*/
public class DataReader extends DataConstants {
/**
* Gets all the accounts in the arrayList Account
* Must be run after getAllUsers
* @return All acounts in the system
*/
public static ArrayList<Account> getAllAccounts(){
ArrayList<Account> accounts = new ArrayList<Account>();
try {
FileReader reader = new FileReader(ACCOUNT_FILE_NAME);
JSONArray accountsJSON = (JSONArray)new JSONParser().parse(reader);
for(int i=0; i < accountsJSON.size(); i++)
{
JSONObject accountJSON = (JSONObject)accountsJSON.get(i);
String username = (String)accountJSON.get(ACCOUNT_USERNAME);
String password = (String)accountJSON.get(ACCOUNT_PASSWORD);
Account account = new Account(username, password);
JSONArray userListJSON = (JSONArray)accountJSON.get(ACCOUNT_USERS);
for(Object id : userListJSON) {
if (UserList.getInstance().getUserByUUID(UUID.fromString((String)id)) != null)
account.addUser(UserList.getInstance().getUserByUUID(UUID.fromString((String)id)));
}
accounts.add(account);
}
} catch(Exception e) {
e.printStackTrace();
}
return accounts;
}
/**
* Get an ArrayList of Users from users.json
* @return All the users in the system
*/
public static ArrayList<User> getAllUsers(){
ArrayList<User> users = new ArrayList<User>();
ArrayList<Camper> campers = getAllCampers();
for(Camper camper : campers) {
users.add(camper);
}
ArrayList<Counselor> counselors = getAllCounselors();
for(Counselor counselor : counselors) {
users.add(counselor);
}
ArrayList<Director> directors = getAllDirectors();
for(Director director : directors) {
users.add(director);
}
return users;
}
public static ArrayList<Camper> getAllCampers() {
ArrayList<Camper> campers = new ArrayList<Camper>();
try {
FileReader reader = new FileReader(USER_FILE_NAME);
JSONArray usersJSON = (JSONArray)new JSONParser().parse(reader);
JSONObject camperListJSON = (JSONObject)usersJSON.get(0);
JSONArray campersJSON = (JSONArray)camperListJSON.get(USER_CAMPER_LIST);
for(int i = 0; i < campersJSON.size(); i++) { // looping through each camper
JSONObject camperJSON = (JSONObject)campersJSON.get(i);
UUID id = UUID.fromString((String)camperJSON.get(USER_ID));
String firstName = (String)camperJSON.get(USER_FIRST_NAME);
String lastName = (String)camperJSON.get(USER_LAST_NAME);
String phone = (String)camperJSON.get(USER_PHONE);
String email = (String)camperJSON.get(USER_EMAIL);
String birthdate = (String)camperJSON.get(ATTENDEE_BIRTHDATE);
String address = (String)camperJSON.get(ATTENDEE_ADDRESS);
String guardianName = (String)camperJSON.get(ATTENDEE_GUARDIAN_NAME);
String pediatricCare = (String)camperJSON.get(ATTENDEE_PEDIATRIC_CARE);
String pediatritianName = (String)camperJSON.get(ATTENDEE_PEDIATRITIAN_NAME);
String pediatritianPhone = (String)camperJSON.get(ATTENDEE_PEDIATRITIAN_PHONE);
String emergencyContactName = (String)camperJSON.get(ATTENDEE_EMERGENCY_CONTACT_NAME);
String emergencyContactPhone = (String)camperJSON.get(ATTENDEE_EMERGENCY_CONTACT_PHONE);
String emergencyContactName2 = (String)camperJSON.get(ATTENDEE_EMERGENCY_CONTACT_NAME_2);
String emergencyContactPhone2 = (String)camperJSON.get(ATTENDEE_EMERGENCY_CONTACT_PHONE_2);
ArrayList<String> allergies = new ArrayList<String>();
for(int j=0; j<((JSONArray)camperJSON.get(ATTENDEE_ALLERGIES)).size();j++){
allergies.add((String)(((JSONArray)camperJSON.get(ATTENDEE_ALLERGIES)).get(j)));
}
ArrayList<String> diets = new ArrayList<String>();
for(int j=0; j<((JSONArray)camperJSON.get(ATTENDEE_DIETS)).size();j++){
diets.add((String)(((JSONArray)camperJSON.get(ATTENDEE_DIETS)).get(j)));
}
int strikes = ((Long)camperJSON.get(CAMPER_STRIKES)).intValue();
Camper camper = new Camper(id, firstName, lastName, phone, email, birthdate, address,
guardianName, pediatricCare, pediatritianName, pediatritianPhone,
new EmergencyContact(emergencyContactName,emergencyContactPhone), new EmergencyContact(emergencyContactName2, emergencyContactPhone2), allergies,
diets, strikes);
campers.add(camper);
}
} catch (Exception e) {
e.printStackTrace();
}
return campers;
}
public static ArrayList<Counselor> getAllCounselors() {
ArrayList<Counselor> counselors = new ArrayList<Counselor>();
try {
FileReader reader = new FileReader(USER_FILE_NAME);
JSONArray usersJSON = (JSONArray)new JSONParser().parse(reader);
JSONObject counselorListJSON = (JSONObject)usersJSON.get(1);
JSONArray counselorsJSON = (JSONArray)counselorListJSON.get(USER_COUNSELOR_LIST);
for(int i = 0; i < counselorsJSON.size(); i++) { // looping through each counselor
JSONObject counselorJSON = (JSONObject)counselorsJSON.get(i);
UUID id = UUID.fromString((String)counselorJSON.get(USER_ID));
String firstName = (String)counselorJSON.get(USER_FIRST_NAME);
String lastName = (String)counselorJSON.get(USER_LAST_NAME);
String phone = (String)counselorJSON.get(USER_PHONE);
String email = (String)counselorJSON.get(USER_EMAIL);
String birthdate = (String)counselorJSON.get(ATTENDEE_BIRTHDATE);
String address = (String)counselorJSON.get(ATTENDEE_ADDRESS);
String guardianName = (String)counselorJSON.get(ATTENDEE_GUARDIAN_NAME);
String pediatricCare = (String)counselorJSON.get(ATTENDEE_PEDIATRIC_CARE);
String pediatritianName = (String)counselorJSON.get(ATTENDEE_PEDIATRITIAN_NAME);
String pediatritianPhone = (String)counselorJSON.get(ATTENDEE_PEDIATRITIAN_PHONE);
String emergencyContactName = (String)counselorJSON.get(ATTENDEE_EMERGENCY_CONTACT_NAME);
String emergencyContactPhone = (String)counselorJSON.get(ATTENDEE_EMERGENCY_CONTACT_PHONE);
String emergencyContactName2 = (String)counselorJSON.get(ATTENDEE_EMERGENCY_CONTACT_NAME_2);
String emergencyContactPhone2 = (String)counselorJSON.get(ATTENDEE_EMERGENCY_CONTACT_PHONE_2);
ArrayList<String> allergies = new ArrayList<String>();
for(int j=0; j<((JSONArray)counselorJSON.get(ATTENDEE_ALLERGIES)).size();j++){
allergies.add((String)(((JSONArray)counselorJSON.get(ATTENDEE_ALLERGIES)).get(j)));
}
ArrayList<String> diets = new ArrayList<String>();
for(int j=0; j<((JSONArray)counselorJSON.get(ATTENDEE_DIETS)).size();j++){
diets.add((String)(((JSONArray)counselorJSON.get(ATTENDEE_DIETS)).get(j)));
}
String bio = (String)counselorJSON.get(COUNSELOR_BIO);
Counselor counselor = new Counselor(id, firstName, lastName, phone, email, birthdate, address,
guardianName, pediatricCare, pediatritianName, pediatritianPhone,
new EmergencyContact(emergencyContactPhone,emergencyContactName), new EmergencyContact(emergencyContactPhone2, emergencyContactName2), allergies,
diets, bio);
counselors.add(counselor);
}
} catch (Exception e) {
e.printStackTrace();
}
return counselors;
}
public static ArrayList<Director> getAllDirectors() {
ArrayList<Director> directors = new ArrayList<Director>();
try {
FileReader reader = new FileReader(USER_FILE_NAME);
JSONArray usersJSON = (JSONArray)new JSONParser().parse(reader);
JSONObject directorListJSON = (JSONObject)usersJSON.get(2);
JSONArray directorsJSON = (JSONArray)directorListJSON.get(USER_DIRECTOR_LIST);
for(int i = 0; i < directorsJSON.size(); i++) { // looping through each director
JSONObject directorJSON = (JSONObject)directorsJSON.get(i);
UUID id = UUID.fromString((String)directorJSON.get(USER_ID));
String firstName = (String)directorJSON.get(USER_FIRST_NAME);
String lastName = (String)directorJSON.get(USER_LAST_NAME);
String phone = (String)directorJSON.get(USER_PHONE);
String email = (String)directorJSON.get(USER_EMAIL);
Director director = new Director(id, firstName, lastName, phone, email);
directors.add(director);
}
} catch (Exception e) {
e.printStackTrace();
}
return directors;
}
/**
* Gets all the Sessions in the arrayList Session
* @return All sessions in the system
*/
public static ArrayList<Session> getAllSessions(){
ArrayList<Session> sessions = new ArrayList<Session>();
try {
FileReader reader = new FileReader(SESSION_FILE_NAME);
JSONArray sessionsJSON = (JSONArray)new JSONParser().parse(reader);
for(int i=0; i < sessionsJSON.size(); i++) { // loops through sessions
JSONObject sessionJSON = (JSONObject)sessionsJSON.get(i);
int week = ((Long)sessionJSON.get(SESSION_WEEK)).intValue();
Session session = new Session(week);
String theme = (String)sessionJSON.get(SESSION_THEME);
session.setTheme(theme);
JSONArray cabinsJSON = (JSONArray)sessionJSON.get(SESSION_CABINS);
for(int j = 0; j < cabinsJSON.size(); j++) { // loops through cabins
JSONObject cabinJSON = (JSONObject)cabinsJSON.get(j);
int cabinNum = ((Long)cabinJSON.get(CABIN_NUM)).intValue();
Cabin cabin = new Cabin(cabinNum);
cabin.setCounselor((Counselor)UserList.getInstance().getUserByUUID(UUID.fromString((String)cabinJSON.get(CABIN_COUNSELOR))));
JSONArray campersJSON = (JSONArray)cabinJSON.get(CABIN_CAMPERS);
for(int k = 0; k < campersJSON.size(); k++) { // loops through campers in cabin
cabin.addCamper((Camper)UserList.getInstance().getUserByUUID(UUID.fromString((String)campersJSON.get(k))));
}
JSONArray scheduleJSON = (JSONArray)cabinJSON.get(CABIN_SCHEDULE);
HashMap<String, ArrayList<Activity>> schedule = new HashMap<String, ArrayList<Activity>>();
for(int k = 0; k < scheduleJSON.size(); k++) { // loops through days in cabin schedule
JSONObject dailyScheduleJSON = (JSONObject)scheduleJSON.get(k);
JSONArray activitiesJSON = (JSONArray)dailyScheduleJSON.get(SCHEDULE_ACTIVITIES);
ArrayList<Activity> activities = new ArrayList<Activity>();
for(int l = 0; l < activitiesJSON.size(); l++) { // loops through activities in day
activities.add(Activity.valueOf((String)activitiesJSON.get(l)));
}
schedule.put((String)dailyScheduleJSON.get(SCHEDULE_DAY),activities);
}
session.addCabin(cabin);
}
sessions.add(session);
}
} catch(Exception e) {
e.printStackTrace();
}
return sessions;
}
}