-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTheCourseFunction.java
More file actions
556 lines (484 loc) · 23.1 KB
/
Copy pathTheCourseFunction.java
File metadata and controls
556 lines (484 loc) · 23.1 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
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.InputMismatchException;
/**
* TheCourseFunction
* <p>
* Runs a course feature in a learning management system.
*
* @author Anushka Nilangekar, Zuhair, and Anish
* @version December 11, 2021
*/
public class TheCourseFunction {
/**
* sync threads
*/
private static Object sync = new Object();
public static void main() throws InvalidCourseException, InvalidQuizException, FileNotFoundException {
}
//menu that only the teacher sees.
public static boolean courseFunctionMenu(String username, int[] quitProgram) throws Exception {
String answer;
String answer2;
String error;
String info;
String question;
ArrayList<String> allStudentsArrayList = new ArrayList<>();
CourseArchive courseArchive = new CourseArchive();
do {
int x = 0;
String availableCourses = "";
for (Course course : CourseArchive.allCourses) {
availableCourses += (++x) + ". " + course.getName() + "\n";
}
if (x == 0)
availableCourses = "None.";
String teacherCourseMenu = "\nSelect the action you want:\n1. Create a course\n2. " +
"Use quiz options for the course\n3. " +
"Add students to a course\n4. Modify course attributes-Course teacher/Course enrollment/" +
"Course name\n5. Delete a course\n6. Exit";
String[] options = {"1", "2", "3", "4", "5", "6"};
answer = inputChecker(availableCourses, teacherCourseMenu, quitProgram);
if (answer == null) {
JOptionPane.showMessageDialog(null, "Thank you for using the Teacher Portal!",
"Teacher Portal", JOptionPane.INFORMATION_MESSAGE);
quitProgram[0] = 1;
return false;
}
if (answer.equals("1")) {
question = "What's the course's title?";
answer = enterAnswerForCourseFunctionDialog(question, quitProgram);
if (quitProgram[0] == 1) {
return false;
}
ArrayList<Course> courses = courseArchive.getCourses();
boolean courseExists = false;
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getName().equals(answer)) {
courseExists = true;
break;
}
}
if (courseExists) {
JOptionPane.showMessageDialog(null, "This course already exists!",
"Courses", JOptionPane.ERROR_MESSAGE);
break;
}
Teacher assignedTeacher = null;
for (int i = 0; i < Teacher.teachers.size(); i++) {
if (Teacher.teachers.get(i).getUsername().equals(username)) {
assignedTeacher = Teacher.teachers.get(i);
break;
}
}
int enrollmentCapacity = 0;
do {
question = "What's the course's enrollment capacity?";
String enrollmentCapacityString = enterAnswerForCourseFunctionDialog(question, quitProgram);
if (quitProgram[0] == 1) {
return false;
}
try {
enrollmentCapacity = Integer.parseInt(enrollmentCapacityString);
if (enrollmentCapacity < 1) {
throw new NumberFormatException();
}
break;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid Input",
"Courses", JOptionPane.ERROR_MESSAGE);
}
} while (true);
JOptionPane.showMessageDialog(null, "Course created!",
"Courses", JOptionPane.INFORMATION_MESSAGE);
Course course = new Course(answer, assignedTeacher, enrollmentCapacity);
CourseArchive courseArchive1 = new CourseArchive();
synchronized (sync) {
courseArchive1.addCourses(course);
}
Teacher.writeCourses(CourseArchive.allCourses);
} else if (answer.equals("2")) {
boolean courseExists = false;
question = "What's the course's title?";
answer = enterAnswerForCourseFunctionDialog(question, quitProgram);
if (quitProgram[0] == 1) {
return false;
}
ArrayList<Course> courses = courseArchive.getCourses();
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getName().equals(answer)) {
courseExists = true;
Course course = courses.get(i);
if (!(courses.get(i).getCourseTeacher().getUsername().equals(username)))
JOptionPane.showMessageDialog(null, "This course belongs to " +
"another teacher.", "Courses", JOptionPane.ERROR_MESSAGE);
else
course.callTheQuizFunction(answer);
break;
}
}
if (!courseExists) {
error = "This course does not exist!";
errorMessage(error);
break;
}
} else if (answer.equals("3")) {
question = "What's the title of the course to which you want to add the student?";
answer = enterAnswerForCourseFunctionDialog(question, quitProgram);
if (quitProgram[0] == 1) {
return false;
}
Course course = null;
ArrayList<Course> courses = courseArchive.getCourses();
boolean courseExists = false;
Teacher tempTeacher = null;
for (int i = 0; i < Teacher.teachers.size(); i++) {
Teacher teacherToBeCompared = Teacher.teachers.get(i);
if (teacherToBeCompared.getUsername().equals(username)) {
tempTeacher = Teacher.teachers.get(i);
break;
}
}
boolean notAssignedTeacher = true;
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getName().equals(answer)) {
courseExists = true;
if (courses.get(i).getCourseTeacher().getUsername().equals(username)) {
notAssignedTeacher = false;
course = courses.get(i);
break;
}
}
}
if (!courseExists) {
error = "This course does not exist!";
errorMessage(error);
break;
}
if (notAssignedTeacher) {
error = "You're not the assigned teacher for this course!";
errorMessage(error);
break;
}
var allStudents = Student.getStudents();
if (allStudents.size() == 0) {
error = "No students available to be added to course! Please make sure students are " +
"available and then try again";
errorMessage(error);
break;
}
var stuList = new ArrayList<String>();
for (int i = 0; i < allStudents.size(); i++) {
stuList.add((i + 1) + ". " + allStudents.get(i).getName() + "\n");
}
String studentNumber;
boolean check = false;
do {
check = false;
studentNumber = selectStudent(stuList, quitProgram);
if (studentNumber == null) {
JOptionPane.showMessageDialog(null, "Thank you for " +
"using the Teacher Portal!",
"Teacher Portal", JOptionPane.INFORMATION_MESSAGE);
quitProgram[0] = 1;
}
if (quitProgram[0] == 1) {
return false;
}
try {
int num = 0;
if (studentNumber != null) {
num = Integer.parseInt(studentNumber);
}
if (!(num >= 1 && num <= allStudents.size()))
check = true;
} catch (NumberFormatException e) {
check = true;
}
} while (check);
Student student = allStudents.get(Integer.valueOf(studentNumber) - 1);
boolean checkDuplicateStudents = true;
for (int i = 0; i < CourseArchive.allCourses.size(); i++) {
Course tempCourse = CourseArchive.allCourses.get(i);
if (tempCourse.getName().equals(answer)) {
var students = tempCourse.getStudentsInThisCourse();
for (int j = 0; j < students.size(); j++) {
if (students.get(j).equals(student)) {
error = "This student is already added to the course!";
errorMessage(error);
checkDuplicateStudents = false;
break;
}
}
}
}
if (checkDuplicateStudents) {
course.addAStudentToTheCourse(student);
Teacher.writeCourses(CourseArchive.allCourses);
info = "Student added!";
informationMessage(info);
}
} else if (answer.equals("4")) {
boolean courseExists = false;
question = "What's the title of the course whose details you want to modify?";
answer = enterAnswerForCourseFunctionDialog(question, quitProgram);
if (quitProgram[0] == 1) {
return false;
}
Course course = null;
ArrayList<Course> courses = courseArchive.getCourses();
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getName().equals(answer)) {
courseExists = true;
course = courses.get(i);
break;
}
}
if (!courseExists) {
error = "This course does not exist!";
errorMessage(error);
break;
}
do {
String courseModificationMenu = "Select the action you want:\n1. Modify course name\n2. Change " +
"course teacher\n3. Modify course enrollment capacity\n4. Exit";
String teacherAssignedCourse = answer;
answer = courseModification(courseModificationMenu, quitProgram);
if (answer == null) {
JOptionPane.showMessageDialog(null, "Thank you for using the " +
"Teacher Portal!",
"Teacher Portal", JOptionPane.INFORMATION_MESSAGE);
quitProgram[0] = 1;
return false;
}
if (answer.equals("1")) {
boolean checkAssignedTeacher = assignedTeacherChecker(courses, course.getName(), username);
if (checkAssignedTeacher) {
question = "What's the new course title?";
answer2 = enterAnswerForCourseFunctionDialog(question, quitProgram);
if (quitProgram[0] == 1) {
return false;
}
course.setName(answer2);
info = "Course name modified!";
informationMessage(info);
Teacher.writeCourses(CourseArchive.allCourses);
}
} else if (answer.equals("2")) {
question = "What's the new course teacher's full name?";
String teacherName = enterAnswerForCourseFunctionDialog(question, quitProgram);
if (quitProgram[0] == 1) {
return false;
}
var allTeachers = Teacher.getTeachers();
Teacher teacher = null;
boolean found = false;
for (int i = 0; i < allTeachers.size(); i++) {
if (allTeachers.get(i).getName().equals(teacherName)) {
found = true;
teacher = allTeachers.get(i);
break;
}
}
if (!found) {
error = "The teacher entered does not exist in the database!";
errorMessage(error);
break;
}
course.setCourseTeacher(teacher);
info = "Course teacher changed!";
informationMessage(info);
Teacher.writeCourses(CourseArchive.allCourses);
} else if (answer.equals("3")) {
boolean checkAssignedTeacher = assignedTeacherChecker(courses, course.getName(), username);
if (checkAssignedTeacher) {
int loop = 0;
do {
loop = 0;
question = "What's the new course enrollment capacity?";
answer2 = enterAnswerForCourseFunctionDialog(question, quitProgram);
if (quitProgram[0] == 1) {
return false;
}
try {
course.setEnrollmentCapacity(Integer.parseInt(answer2));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid Input",
"Courses", JOptionPane.ERROR_MESSAGE);
loop = 1;
}
} while (loop == 1);
info = "Course enrollment capacity modified!";
informationMessage(info);
Teacher.writeCourses(CourseArchive.allCourses);
}
} else if (answer.equals("4")) {
break;
}
} while (true);
} else if (answer.equals("5")) {
boolean courseExists = false;
question = "What's the course's title?";
answer = enterAnswerForCourseFunctionDialog(question, quitProgram);
if (answer == null) {
quitProgram[0] = 1;
return false;
}
ArrayList<Course> courses = courseArchive.getCourses();
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getName().equals(answer)) {
courseExists = true;
boolean checkAssignedTeacher = assignedTeacherChecker(courses, answer, username);
if (!checkAssignedTeacher) {
break;
}
var allCourseQuizzes = new QuizArchive().getQuizzes();
for (int j = 0; j < allCourseQuizzes.size(); j++) {
if (allCourseQuizzes.get(j).getCourse().equals(courses.get(i).getName())) {
allCourseQuizzes.remove(j);
j--;
}
}
synchronized (sync) {
courseArchive.deleteACourse(answer);
}
PrintInformation.writeQuizQuestions(new QuizArchive());
try {
PrintWriter pw = new PrintWriter(new FileWriter("StudentQuizzes.txt"));
pw.print("");
} catch (IOException e) {
error = "Couldn't modify the quiz.";
errorMessage(error);
}
var allQuizzes = new QuizArchive().getQuizzes();
StudentAnish.writeScores(new QuizArchive());
info = "Course deleted!";
informationMessage(info);
Teacher.writeCourses(CourseArchive.allCourses);
break;
}
}
if (!courseExists) {
error = "The course to be deleted does not exist!";
errorMessage(error);
break;
}
} else if (answer.equals("6")) {
return false;
}
} while (true);
return true;
}
//method for a person that is logged in as a teacher to create their own course to store quizzes
// in which students access
public static void creatingACourse(String answer, Teacher teacher, int enrollmentCapacity)
throws InvalidCourseException, InvalidQuizException, FileNotFoundException {
CourseArchive courseArchive = new CourseArchive();
Course course = new Course(answer, teacher, enrollmentCapacity);
synchronized (sync) {
courseArchive.addCourses(course);
}
}
//assigns teacher
public static boolean assignedTeacherChecker(ArrayList<Course> courses, String answer, String username) {
Teacher tempTeacher = null;
String error;
boolean notAssignedTeacher = true;
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getName().equals(answer)) {
if (courses.get(i).getCourseTeacher().getUsername().equals(username)) {
notAssignedTeacher = false;
break;
}
}
}
if (notAssignedTeacher) {
error = "You're not the assigned teacher for this course!";
errorMessage(error);
return false;
}
return true;
}
//checks for input
public static String inputChecker(String courses, String prompt, int[] quitProgram) {
do {
String option;
String[] optionList = {"1", "2", "3", "4", "5", "6"};
option = (String) JOptionPane.showInputDialog(null, "Available Courses:\n"
+ courses + prompt, "Teacher Portal", JOptionPane.QUESTION_MESSAGE, null,
optionList,
optionList[0]);
if (option == null) {
quitProgram[0] = 1;
return null;
} else {
return option;
}
} while (true);
}
//prompts for courses modification
public static String courseModification(String prompt, int[] quitProgram) {
do {
String option;
String[] optionList = {"1", "2", "3", "4"};
option = (String) JOptionPane.showInputDialog(null, prompt,
"Courses", JOptionPane.QUESTION_MESSAGE, null, optionList, optionList[0]);
if (option == null) {
quitProgram[0] = 1;
return null;
} else {
return option;
}
} while (true);
}
//select a specific student from the arraylist
public static String selectStudent(ArrayList<String> students, int[] quitProgram) {
do {
String option;
String optionNumber;
String[] optionList = new String[students.size()];
for (int i = 0; i < students.size(); i++) {
optionList[i] = students.get(i);
}
option = (String) JOptionPane.showInputDialog(null, "Select the student:",
"Courses", JOptionPane.QUESTION_MESSAGE, null, optionList, optionList[0]);
if (option == null) {
quitProgram[0] = 1;
return null;
} else {
optionNumber = option.substring(0, 1);
return optionNumber;
}
} while (true);
}
//prompts error message
public static void errorMessage(String error) {
JOptionPane.showMessageDialog(null, error, "Courses", JOptionPane.ERROR_MESSAGE);
}
//prompts info message
public static void informationMessage(String info) {
JOptionPane.showMessageDialog(null, info, "Courses", JOptionPane.INFORMATION_MESSAGE);
}
//prompts course answers
public static String enterAnswerForCourseFunctionDialog(String question, int[] quitProgram) {
String answer;
int loop = 0;
do {
loop = 0;
answer = JOptionPane.showInputDialog(null, question,
"Courses", JOptionPane.QUESTION_MESSAGE);
//if user wants to exit the program
if (answer == null) {
JOptionPane.showMessageDialog(null, "Thank you for using the Teacher Portal!",
"Teacher Portal", JOptionPane.INFORMATION_MESSAGE);
quitProgram[0] = 1;
} else if (answer.isBlank()) {
JOptionPane.showMessageDialog(null, "Answer cannot be empty!",
"Courses", JOptionPane.ERROR_MESSAGE);
loop = 1;
}
} while (loop == 1);
return answer;
}
}