-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataWriter.java
343 lines (277 loc) · 11.4 KB
/
DataWriter.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
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
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
/**
* This is the data writer that writes data to json and text files and extends dataconstant.
* @authors: J TEA: Tessa Neal, Eve Blom, Anna Phan, and Jacqueline Askey
*/
public class DataWriter extends DataConstants{
/**
* This method saves the list of users.
*/
public static void saveUsers() {
UserList user = UserList.getInstance();
ArrayList<User> users = user.getUsers();
JSONArray jsonUsers = new JSONArray();
for(int i=0; i< users.size(); i++) {
jsonUsers.add(getUserJSON(users.get(i)));
}
try (FileWriter file = new FileWriter(USER_FILE_NAME)) {
file.write(jsonUsers.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method converts a user into a JSONOBJECT
* @param user
* @return JSONOBJECT
*/
public static JSONObject getUserJSON(User user) {
JSONObject userDetails = new JSONObject();
userDetails.put(USER_ID, user.getId().toString());
userDetails.put(USER_FIRST_NAME, user.getFirstName());
userDetails.put(USER_LAST_NAME, user.getLastName());
userDetails.put(USER_EMAIL, user.getEmail());
userDetails.put(USER_BIRTHDAY, formattingDate(user.getBirthday()));
userDetails.put(USER_USERNAME, user.getUsername());
userDetails.put(USER_TYPE, user.getType().toString());
userDetails.put(USER_PASSWORD, user.getPassword());
return userDetails;
}
/**
* This takes a Date and turns it into a String
* @param input (Data)
* @return String
*/
public static String formattingDate(Date input){
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String date = formatter.format(input);;
return date;
}
/**
* This saves the list of courses
*/
public static void saveCourses() {
CourseList course = CourseList.getInstance();
ArrayList<Course> courses = course.getAllCourses();
JSONArray jsonCourses = new JSONArray();
for(int i =0; i<courses.size(); i++){
jsonCourses.add(getCourseJSON(courses.get(i)));
}
try (FileWriter file = new FileWriter(COURSE_FILE_NAME)) {
file.write(jsonCourses.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method converts a course into a JSONOBJECT
* @param user
* @return JSONOBJECT
*/
public static JSONObject getCourseJSON(Course course) {
JSONObject courseDetails = new JSONObject();
courseDetails.put(COURSE_ID, course.getId().toString());
courseDetails.put(COURSE_NAME, course.getTitle());
courseDetails.put(COURSE_DIFFICULTY, course.getDifficulty().toString());
courseDetails.put(COURSE_AUTHOR, course.getAuthor().toString());
courseDetails.put(COURSE_LANGUAGE, course.getLanguage().toString());
courseDetails.put(COURSE_DESCRIPTION, course.getDesciption());
courseDetails.put(COURSE_SYLLABUS,course.getSyllabus());
ArrayList<Module> modules = course.getModule();
JSONArray moduleArray = new JSONArray();
for(int i = 0; i <modules.size();i++){
moduleArray.add(getModuleJSON(modules.get(i)));
}
courseDetails.put(COURSE_MODULES, moduleArray);
courseDetails.put(COURSE_RATING,course.getRating());
ArrayList<Review> reviews = course.getReview();
JSONArray reviewArray = new JSONArray();
for(Review review: reviews){
JSONObject reviewObject = new JSONObject();
reviewObject.put(COURSE_REVIEWS_USER, review.getUser().toString());
reviewObject.put(COURSE_REVIEWS_RATING, review.getRating());
reviewObject.put(COURSE_REVIEWS_COMMENT, review.getComment());
reviewArray.add(reviewObject);
}
courseDetails.put(COURSE_REVIEWS, reviewArray);
ArrayList<Student> students = course.getStudent();
JSONArray studentArray = new JSONArray();
for(Student student: students){
JSONObject studentObject = new JSONObject();
studentObject.put(COURSE_STUDENTS_ID, student.getId().toString());
ArrayList<Integer> grades = student.getGrades();
JSONArray gradeArray = new JSONArray();
for(Integer grade: grades){
JSONObject gradeObject = new JSONObject();
gradeArray.add(grade);
}
studentObject.put(COURSE_STUDENTS_GRADES, gradeArray);
studentArray.add(studentObject);
}
courseDetails.put(COURSE_STUDENTS, studentArray);
ArrayList<Comment> comments = course.getComment();
JSONArray commentArray = new JSONArray();
for(Comment comment: comments){
JSONObject commentDetails = new JSONObject();
commentDetails.put(COURSE_COMMENTS_USER, comment.getUser().toString());
commentDetails.put(COURSE_COMMENTS_COMMENT, comment.getComment());
ArrayList<Reply> replies = comment.getReply();
JSONArray repliesArray = new JSONArray();
for(Reply reply: replies){
JSONObject replyObject = new JSONObject();
replyObject.put(COURSE_COMMENTS_REPLIES_USER, reply.getUser().toString());
replyObject.put(COURSE_COMMENTS_REPLIES_COMMENT, reply.getComment());
repliesArray.add(replyObject);
}
commentDetails.put(COURSE_COMMENTS_REPLIES, repliesArray);
commentArray.add(commentDetails);
}
courseDetails.put(COURSE_COMMENTS, commentArray);
return courseDetails;
}
/**
* This turns a module into a JSONObject
* @param module
* @return JSONObject
*/
public static JSONObject getModuleJSON (Module module){
JSONObject moduleObject = new JSONObject();
moduleObject.put(COURSE_MODULES_NAME, module.getTitle());
ArrayList<Lesson> lessons = module.getLesson();
JSONArray lessonArray = new JSONArray();
for (int i =0; i<lessons.size(); i++){
lessonArray.add(getLessonJSON(lessons.get(i)));
}
moduleObject.put(COURSE_MODULES_LESSON, lessonArray);
ArrayList<Comment> comments = module.getComment();
JSONArray commentArray = new JSONArray();
for(int i =0; i<comments.size(); i++){
commentArray.add(getModuleCommentJSON(comments.get(i)));
}
moduleObject.put(COURSE_MODULES_COMMENTS, commentArray);
return moduleObject;
}
/**
* This turns lesson into a JSONObject
* @param lesson
* @return JSONObject
*/
public static JSONObject getLessonJSON (Lesson lesson){
JSONObject lessonObject = new JSONObject();
lessonObject.put(COURSE_MODULES_LESSON_TITLE, lesson.getTitle());
lessonObject.put(COURSE_MODULES_LESSON_CONTENT, lesson.getContent());
//lessonObject.put(COURSE_MODULES_LESSON_QUIZ, lesson.getQuiz());
ArrayList<Question> questions = lesson.getQuiz().getQuestion();
JSONArray questionArray = new JSONArray();
for(Question question: questions){
JSONObject questionObject = new JSONObject();
questionObject.put(COURSE_MODULES_LESSON_QUIZQUESTIONS_QUESTION, question.getQuestion());
ArrayList<String> answers = question.getAnswers();
JSONArray answerArray = new JSONArray();
for(String answer: answers){
answerArray.add(answer);
}
questionObject.put(COURSE_MODULES_LESSON_QUIZQUESTIONS_ANSWERS, answerArray);
questionObject.put(COURSE_MODULES_LESSON_QUIZQUESTIONS_CORRECTANS, question.getCorrectAnswer());
questionArray.add(questionObject);
}
lessonObject.put(COURSE_MODULES_LESSON_QUIZ, questionArray);
return lessonObject;
}
public static JSONObject getModuleCommentJSON(Comment comment){
JSONObject commentObject = new JSONObject();
commentObject.put(COURSE_MODULES_COMMENTS_USER, comment.getUser().toString());
commentObject.put(COURSE_MODULES_COMMENTS_COMMENT, comment.getComment());
ArrayList<Reply> replies = comment.getReply();
JSONArray repliesArray = new JSONArray();
for(Reply reply: replies){
JSONObject replyObject = new JSONObject();
replyObject.put(COURSE_MODULES_COMMENTS_REPLIES_USER, reply.getUser().toString());
replyObject.put(COURSE_MODULES_COMMENTS_REPLIES_COMMENT, reply.getComment());
repliesArray.add(replyObject);
}
commentObject.put(COURSE_MODULES_COMMENTS_REPLIES, repliesArray);
return commentObject;
}
/**
* This saves the list of FAQs
*/
public static void saveFAQs() {
FAQList faq = FAQList.getInstance();
ArrayList<FAQ> FAQs = faq.getFAQ();
JSONArray jsonFAQs = new JSONArray();
for(int i =0; i<FAQs.size(); i++){
jsonFAQs.add(getFAQJSON(FAQs.get(i)));
}
try (FileWriter file = new FileWriter(FAQ_FILE_NAME)) {
file.write(jsonFAQs.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This turns a FAQ into a JSONObject
* @param faq
* @return JSONObject
*/
public static JSONObject getFAQJSON(FAQ faq) {
JSONObject FAQDetails = new JSONObject();
FAQDetails.put(FAQ_QUESTION, faq.getQuestion());
FAQDetails.put(FAQ_ANSWERS, faq.getAnswers());
return FAQDetails;
}
/**
* This prints a certification into a text file
* @param certification
* @return Whether or not it worked (True if did or false if not)
*/
public static boolean CreateCertificationFile(Certification certification){
boolean worked = false;
FileWriter Certification;
try {
Certification = new FileWriter("Certification.txt");
Certification.write(certification.toString());
Certification.close();
worked = true;
} catch (IOException e) {
e.printStackTrace();
}
return worked;
}
/**
* This prints a module of a course into a text file
* @param module
* @return Whether or not it worked (True if did or false if not)
*/
public static boolean CreateCourseFile(Module module){
boolean worked = false;
FileWriter Course;
try {
Course = new FileWriter("Module.txt");
Course.write(module.fileInfo());
Course.close();
worked = true;
} catch (IOException e) {
e.printStackTrace();
}
return worked;
}
/*
* This is the main method for this class. This saves all data that may have changed. Also a way to test that it is working correctly.
*/
public static void main(String[] args){
saveUsers();
saveCourses();
}
}