-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAYJDatabase.java
More file actions
executable file
·1696 lines (1445 loc) · 67.4 KB
/
AYJDatabase.java
File metadata and controls
executable file
·1696 lines (1445 loc) · 67.4 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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
File Name: AYJDatabase.java
Description: The following program is the core of the AYJDatabase program.
It contains all the functions of the program and executes the GUI interface.
*/
//This is the import of all necessary packages.
import java.util.*;
import java.io.*;
import java.lang.*;
//This is the declaration of the class.
public class AYJDatabase{
//These are the declarations of the fields.
private ArrayList<Student> studentList = new ArrayList<Student>();
private ArrayList<Course> courseList = new ArrayList <Course>();
private ArrayList<Teacher> teacherList = new ArrayList<Teacher>();
private ArrayList<Department> departmentList = new ArrayList<Department>();
//This is the method that executes the program.
//It takes in no parameter and returns no values.
public void run(){
//This initializes a boolean to be false to initiate the running of the program.
boolean quit = false;
boolean successInit = false;
boolean validChoice = false;
boolean validInput = false;
Scanner sc = new Scanner (System.in);
int choice = -1;
String addInfoType, printInfoType, fileName;
int sortInfoType = -1;
int searchInfoType = -1;
Department temDepartment;
Course temCourse;
Student temStudent = null;
Teacher temTeacher;
int departmentNum = -1;
String courseCode;
Name name;
//This prints out a welcoming message to the user.
System.out.println ("Welcome to the AYJDatabase application.");
System.out.println();
//This is a method that would initialize the program with appropriate information.
while (!successInit){
successInit = initProgram();
//This prints out error messages to indicate that initialization did not complete.
if (!successInit){
System.out.println("Initialization failed. Please try again.");
System.out.println();
System.out.println ("Help: Please make sure your file is in the correct format and try other methods of input.");
System.out.println();
}
}
System.out.println();
//This shows that the user has yet to exit the execution of the program.
while (!quit){
printOptions();
while (!validChoice){
System.out.println();
System.out.print ("Enter the number that corresponds to the desired choice: ");
//This checks if the user is entering a valid option.
try{
choice = sc.nextInt();
if (choice < 1 || choice > 9){
System.out.println ("The entered choice does not exist. Please try again.");
}
else {
validChoice = true;
}
}
catch (InputMismatchException imx){
sc.nextLine();
System.out.println ("Invalid type entered. Please try again.");
}
}
validChoice = false;
//This initiates the different options depending on the user selection of option.
switch (choice){
case 1:
sc.nextLine();
System.out.print ("Enter what type of information do you want to add (Department/Teacher/Student/Course): ");
addInfoType = sc.nextLine();
addInfoType = addInfoType.toUpperCase();
if (addInfoType.equals("DEPARTMENT")){
addDepartment();
}
else if (addInfoType.equals("TEACHER")){
addTeacher();
}
else if (addInfoType.equals("STUDENT")){
addStudent();
}
else if (addInfoType.equals("COURSE")){
addCourse();
}
else{
System.out.println ("This type of information is invalid.");
}
break;
case 2:
sc.nextLine();
System.out.print ("Enter what type of information do you want to delete (Department/Teacher/Student/Course): ");
addInfoType = sc.nextLine();
addInfoType = addInfoType.toUpperCase();
if (addInfoType.equals("DEPARTMENT")){
deleteDepartment();
}
else if (addInfoType.equals("TEACHER")){
deleteTeacher();
}
else if (addInfoType.equals("STUDENT")){
deleteStudent();
}
else if (addInfoType.equals("COURSE")){
deleteCourse();
}
else{
System.out.println ("This type of information is invalid.");
}
break;
case 3:
sc.nextLine();
System.out.print ("Enter the file name (without the \".txt\" extension) you want to save the database's information to: ");
fileName = sc.nextLine();
saveAllCurrentInfo(fileName);
break;
case 4:
System.out.println ("The following types of searching are avaliable: ");
System.out.println ("1)Search for a specific student by his/her name.\n2)Search for a specific teacher by his/her name.");
System.out.println ("3)Search for a course by its course code.\n4)Search for a specific department by its department number.");
while (!validChoice){
System.out.print ("Enter a choice you want: ");
try{
searchInfoType = sc.nextInt();
if (searchInfoType > 0 && searchInfoType < 5){
validChoice = true;
}
else {
System.out.println ("Choice entered is not within the given range.");
}
}
catch (InputMismatchException imx){//This checks if the user enters the wrong type of data.
sc.nextLine();
System.out.println ("Wrong type of data inputted. Please try again.");
}
System.out.println();
}
//This calls the corresponding method to complete the user's choice of option.
switch (searchInfoType){
case 1:
sc.nextLine();
System.out.print ("Enter the name of the student (first last): ");
name = new Name (sc.nextLine());
System.out.println();
temStudent = searchStudentName(name);
if (temStudent != null){
System.out.println (temStudent);
}
else {
System.out.println ("The student does not exist in the database.");
}
break;
case 2:
sc.nextLine();
System.out.print ("Enter the name of the teacher (first last): ");
name = new Name (sc.nextLine());
System.out.println();
temTeacher = searchTeacherName(name);
if (temTeacher != null){
System.out.println (temTeacher);
}
else {
System.out.println ("The teacher does not exist in the database.");
}
break;
case 3:
sc.nextLine();
System.out.print ("Enter the course code of the course: ");
courseCode = sc.nextLine();
System.out.println();
temCourse = searchCourse(courseCode);
if (temCourse != null){
System.out.println(temCourse);
}
else {
System.out.println ("The course does not exist in the database.");
}
break;
default:
while (!validInput){
try{
System.out.print ("Enter the department number: ");
departmentNum = sc.nextInt();
validInput = true;
}
catch(InputMismatchException imx){
sc.nextLine();
System.out.println ("Wrong type of data inputted. Please try again.");
}
}
System.out.println();
temDepartment = searchDepartment (departmentNum);
if (temDepartment != null){
System.out.println (temDepartment);
}
else {
System.out.println ("The department does not exist in the database.");
}
}
break;
case 5:
System.out.println ("The following types of sorting are avaliable: ");
System.out.println ("1)Sort all students by their student numbers.\n2)Sort all teachers by their teacher numbers.");
System.out.println ("3)Sort all departments by their departments numbers.\n4)Sort all courses by their course codes.");
while (!validChoice){
System.out.print ("Enter a choice you want: ");
try{
sortInfoType = sc.nextInt();
if (sortInfoType > 0 && sortInfoType < 5){
validChoice = true;
}
else {
System.out.println ("Choice entered is not within the given range.");
}
}
catch (InputMismatchException imx){//This checks if the user enters the wrong type of data.
sc.nextLine();
System.out.println ("Wrong type of data inputted. Please try again.");
}
System.out.println();
}
//This calls the corresponding method to complete the user's choice of option.
switch (sortInfoType){
case 1:
sortByStudentNum();
break;
case 2:
sortByTeacherNum();
break;
case 3:
sortByDepartmentNum();
break;
default:
sortByCourseCode();
}
break;
case 6:
sc.nextLine();
while (!validInput){
System.out.print ("Enter the student's name to modify his/her time table (first last): ");
name = new Name (sc.nextLine());
//This checks if the student exists in the database.
temStudent = searchStudentName(name);
if (temStudent != null){
validInput = true;
}
else {
System.out.println ("The student does not exist.");
System.out.println ("Please try again.");
System.out.println();
}
}
modifyTimeTable(temStudent);
break;
case 7:
printAllCombinations();
break;
case 8:
sc.nextLine();
System.out.print ("Enter what type of information do you want to print out (Department/Teacher/Student/Course): ");
printInfoType = sc.nextLine();
printInfoType = printInfoType.toUpperCase();
if (printInfoType.equals ("DEPARTMENT")){
System.out.println("Printing all current departments' information: ");
System.out.println();
reportAllDepartments();
}
else if (printInfoType.equals ("STUDENT")){
System.out.println("Printing all current students' information: ");
System.out.println();
reportAllStudents();
}
else if (printInfoType.equals ("TEACHER")){
System.out.println("Printing all current teachers' information: ");
System.out.println();
reportAllTeachers();
}
else if (printInfoType.equals ("COURSE")){
System.out.println("Printing all current courses' information: ");
System.out.println();
reportAllCourses();
}
else {
System.out.println ("This type of information is invalid.");
System.out.println ("Please try again.");
}
break;
default:
quit = true;
}
System.out.println();
validChoice = false;
validInput = false;
}
//This prints out an ending message to the user.
System.out.println ("Thank you for using the AYJDatabase application!");
}
//This is the method that deletes a existing Department object from the department list.
//It takes in no parameters and returns no values.
public void deleteDepartment(){
Scanner sc = new Scanner (System.in);
Department temDepartment;
int departmentNum = -1;
boolean validInput = false;
while (!validInput){
try{
System.out.print ("Enter the department's department number: ");
departmentNum = sc.nextInt();
validInput = true;
}
catch (InputMismatchException imx){
System.out.println ("Wrong type of information entered.");
System.out.println ("Please try again.");
System.out.println ();
}
}
temDepartment = searchDepartment(departmentNum);
if (temDepartment != null){
departmentList.remove(temDepartment);
System.out.println ("The department is removed from the database.");
}
else {
System.out.println ("This department does not exist in the database.");
System.out.println ("Please try again.");
}
}
//This is the method that deletes a existing Student object from the student list.
//It takes in no parameters and returns no values.
public void deleteStudent(){
Scanner sc = new Scanner (System.in);
Student temStudent;
Name name;
System.out.print ("Enter the student's name (first last): ");
name = new Name (sc.nextLine());
temStudent = searchStudentName(name);
if (temStudent != null){
studentList.remove(temStudent);
System.out.println ("The student is removed from the database.");
}
else {
System.out.println ("This student does not exist in the database.");
System.out.println ("Please try again.");
}
}
//This is the method that deletes a existing Teacher object from the teacher list.
//It takes in no parameters and returns no values.
public void deleteTeacher(){
Scanner sc = new Scanner (System.in);
Teacher temTeacher;
Course temCourse;
int courseLen;
Name name;
Department temDepartment;
boolean delete;
System.out.print ("Enter the teacher's name (first last): ");
name = new Name (sc.nextLine());
temTeacher = searchTeacherName(name);
if (temTeacher != null){
teacherList.remove(temTeacher);
for (int i = 0; i < courseList.size(); i ++){
temCourse = courseList.get(i);
if (temCourse.getTeacher().equalsToName(temTeacher)){
temCourse.setTeacher(null);
}
}
for (int i = 0; i < departmentList.size(); i ++){
temDepartment = departmentList.get(i);
delete = temDepartment.getTeacherList().remove(temTeacher);
if (delete){
temDepartment.setACL(temDepartment.determineACL());
}
}
System.out.println ("The teacher is removed from the database.");
}
else {
System.out.println ("This teacher does not exist in the database.");
System.out.println ("Please try again.");
}
}
//This is the method that deletes a existing Course object from the course list.
//It takes in no parameters and returns no values.
public void deleteCourse(){
Scanner sc = new Scanner (System.in);
Course temCourse;
String courseCode;
ArrayList<Course> temCourseList;
System.out.print ("Enter the course's course code: ");
courseCode = sc.nextLine();
temCourse = searchCourse(courseCode);
if (temCourse != null){
courseList.remove(temCourse);
for (int i = 0; i < studentList.size(); i ++){
temCourseList = studentList.get(i).getSchedule().getCourseChosen();
temCourseList.remove(temCourse);
}
for (int i = 0; i < teacherList.size(); i ++){
temCourseList = teacherList.get(i).getCourseTaught();
temCourseList.remove(temCourse);
}
for (int i = 0; i < departmentList.size(); i ++){
temCourseList = departmentList.get(i).getCourseOffered();
temCourseList.remove(temCourse);
}
System.out.println ("The course is removed from the database.");
}
else {
System.out.println ("This course does not exist in the database.");
System.out.println ("Please try again.");
}
}
//This is the method that adds a new Department object to the existing department list.
//It takes in no parameters and returns no values.
public void addDepartment(){
Scanner sc = new Scanner (System.in);
String departmentType, courseCode;
int departmentNum = -1;
int teacherNum = -1;
int teacherLen = 0;
int courseLen = 0;
int teacherPos;
ArrayList<Teacher>temTeacherList = new ArrayList<Teacher>();
ArrayList<Course>temCourseList = new ArrayList<Course>();
boolean validType = false;
boolean validInput = false;
boolean exist = true;
Course temCourse;
System.out.print ("Enter the type of department you want to add (Music/Mathematics/Computer Studies): ");
departmentType = sc.nextLine();
departmentType = departmentType.toUpperCase();
if (departmentType.equals("MUSIC") || departmentType.equals("MATHEMATICS") || departmentType.equals("COMPUTER STUDIES")){
validType = true;
}
if (validType){
while (!validInput){
try{
System.out.print ("Enter the department's department number: ");
departmentNum = sc.nextInt();
System.out.print ("Enter the number of teachers that will be in this department: ");
teacherLen = sc.nextInt();
for (int i = 0; i < teacherLen; i ++){
System.out.print ("Enter the teacher number for teacher #" + (i + 1) + ": ");
teacherNum = sc.nextInt();
teacherPos = findTeacher(teacherNum);
if (teacherPos != -1){
temTeacherList.add (teacherList.get(teacherPos));
}
else {
System.out.println ("This teacher does not exist.");
exist = false;
System.out.println ("The program continues without adding this teacher to the department.");
}
}
if (exist){
System.out.print ("Enter the number of courses that will be offered in this department: ");
courseLen = sc.nextInt();
for (int i = 0; i < courseLen; i ++){
System.out.print ("Enter the course code of course #" + (i + 1) + ": ");
courseCode = sc.nextLine();
temCourse = searchCourse(courseCode);
if (temCourse != null){
temCourseList.add(temCourse);
}
else {
System.out.println ("This course does not exist.");
System.out.println ("The program continues without adding this course to the department.");
}
}
validInput = true;
}
}
catch(InputMismatchException imx){
System.out.println ("Input type not valid. Please try again from the start.");
sc.nextLine();
}
if (departmentType.equals("MUSIC")){
departmentList.add(new Music (temTeacherList,temCourseList,departmentNum));
}
else if (departmentType.equals("MATHEMATICS")){
departmentList.add(new Mathematics (temTeacherList,temCourseList,departmentNum));
}
else {
departmentList.add(new ComputerStudies (temTeacherList,temCourseList,departmentNum));
}
}
}
else {
System.out.println ("Selected type of department is not supported by the program.");
System.out.println ("Please try again.");
}
}
//This is the method that adds a new Course object to the existing course list.
//It takes in no parameters and returns no values.
public void addCourse(){
Scanner sc = new Scanner (System.in);
String courseCode, teacherType;
int size = -1;
int teacherNum = -1;
int teacherPos, departmentPos;
Teacher temTeacher;
boolean validInput = false;
System.out.print("Enter the course code: ");
courseCode = sc.nextLine();
while (!validInput){
System.out.print("Enter the maximum number of students that can be admitted to the course: ");
//This catches the exception of wrong type of data input.
try{
size = sc.nextInt();
validInput = true;
sc.nextLine();
}
catch (InputMismatchException imx){
System.out.println ("Input type not valid. Please try again.");
sc.nextLine();
}
}
validInput = false;
//This checks if the teacher is an existing teacher or a new object should be made.
System.out.print ("If the teacher responsible for the course is a new teacher, enter \"yes\": ");
teacherType = sc.nextLine();
teacherType = teacherType.toUpperCase();
if (teacherType.equals ("YES")){
addTeacher();
courseList.add (new Course (courseCode, size, teacherList.get(teacherList.size() - 1)));
}
else {
while (!validInput){
System.out.print ("Enter the teacher number of the teacher: ");
try{
teacherNum = sc.nextInt();
validInput = true;
}
catch (InputMismatchException imx){
System.out.println ("Input type not valid. Please try again.");
sc.nextLine();
}
}
teacherPos = findTeacherByNum(teacherNum);
if (teacherPos != -1){
courseList.add (new Course (courseCode, size, teacherList.get(teacherPos)));
System.out.println ("New course successfully added.");
}
else{
System.out.println ("No such teacher exists. No new course is added.");
System.out.println ("Please try again.");
}
}
}
//This is the method that adds a new Teacher object to the existing teacher list.
//It takes in no parameters and returns no values.
public void addTeacher(){
Scanner sc = new Scanner (System.in);
String teacherName, courseType;
int teacherNum = -1;
int coursePos;
int departmentNum = -1;
int experience = 0;
int numCourses = -1;
ArrayList<Course>temCourseList = new ArrayList<Course>();
boolean validInput = false;
String courseCode;
Course temCourse;
Department temDepartment;
System.out.print ("Enter the teacher's name: ");
teacherName = sc.nextLine();
while (!validInput){
//This catches the error of user inputting the wrong type of information.
try{
System.out.print ("Enter the teacher number: ");
teacherNum = sc.nextInt();
System.out.print ("Enter the number of years the teacher has been teaching: ");
experience = sc.nextInt();
System.out.print ("Enter the number of courses the teacher teachers: ");
numCourses = sc.nextInt();
sc.nextLine();
validInput = true;
}
catch(InputMismatchException imx){
System.out.println ("Input type not valid. Please try again.");
sc.nextLine();
}
}
validInput = false;
//This reads in user inputs to fulfill the different courses the new teacher is able to teach.
for (int i = 0; i < numCourses; i ++){
//This checks if the course is a new course or an existing course.
System.out.print ("Enter the course code: ");
courseCode = sc.nextLine();
temCourse = searchCourse(courseCode);
if (temCourse != null){
temCourseList.add(temCourse);
}
}
//This adds a new Teacher object.
teacherList.add(new Teacher (teacherName, teacherNum, experience, temCourseList));
System.out.println ("The new teacher is successfully created.");
while (!validInput){
//This updates the new teacher to the existing department.
try{
System.out.print ("Enter the department number to add the teacher to an existing department: ");
departmentNum = sc.nextInt();
validInput = true;
}
catch (InputMismatchException imx){
System.out.println ("Input type not valid. Please try again.");
sc.nextLine();
}
}
temDepartment = searchDepartment (departmentNum);
if (temDepartment != null){
temDepartment.getTeacherList().add(teacherList.get(teacherList.size() - 1));
System.out.println ("The new teacher is successfully updated in the system.");
}
else{
teacherList.remove (teacherList.size() - 1);
System.out.println ("The department does not exist.");
System.out.println ("The created teacher does not follow school regulation.");
System.out.println ("The teacher is removed from the database.");
}
}
//This is the method that adds a new Student object to the existing student list.
//It takes in no parameters and returns no values.
public void addStudent(){
Scanner sc = new Scanner (System.in);
int numStudent;
int studentNum = -1;
int numCourse = 0;
String studentName, courseCode;
ArrayList<Course> temCourseList = new ArrayList<Course> ();
Course temCourse;
boolean validInput = false;
System.out.print ("Enter the student name: ");
studentName = sc.nextLine();
while (!validInput){
try{
//This catches the wrong type of information the user might enter.
System.out.print ("Enter the student number: ");
studentNum = sc.nextInt();
System.out.print ("Enter the number of courses the student is currently taking: ");
numCourse = sc.nextInt();
validInput = true;
}
catch (InputMismatchException imx){
System.out.println ("Input type not valid. Please try again.");
sc.nextLine();
}
}
for (int i = 0; i < numCourse; i ++){
System.out.print ("Enter the course code for course #" + (i + 1) + ": ");
courseCode = sc.nextLine();
temCourse = searchCourse (courseCode);
if (temCourse != null){
temCourseList.add(temCourse);
}
}
//This adds the new Student object to the end of the student list.
studentList.add(new Student (studentName, studentNum, temCourseList));
System.out.println ("The new student is successfully created.");
}
//This is the private method that will print out all avaliable options for the user.
//It takes in no parameters and returns no values.
//It is a private method and can be only accessed in the AYJDatabase class.
private static void printOptions(){
System.out.println ("Here are your options: ");
System.out.println ("1)Add information to current database.\n2)Delete information from current database.");
System.out.println ("3)Save all current information onto a file.");
System.out.println ("4)Search for specific information within the database.");
System.out.println ("5)Sort information.");
System.out.println ("6)Change a student's current time table.");
System.out.println ("7)Print out all possible combinations of a student's chosen courses.");
System.out.println ("8)Print out general information.");
System.out.println ("9)Exit the application.");
}
//This is the method that would initiate the program with an accepted method (either file input or user input).
//It takes in no parameters and returns a boolean value.
public boolean initProgram(){
//This initalizes all variables used.
int choice = -1;
Scanner sc = new Scanner (System.in);
boolean validChoice = false;
System.out.println ("Please choose a method to initialize the program (enter the number in front of the option): ");
System.out.println ("1)File Input\n2)User Input");
//This catches potential errorneous inputs and continues to prompt the user until the right type of information is entered.
while (!validChoice){
try{
choice = sc.nextInt();
//This checks if the options selected is within range.
if (choice == 1 || choice == 2){
//This sets the boolean to be true if the right type of data is entered.
validChoice = true;
}
else {
System.out.println ("The selected choice is not within range.");
System.out.println ("Please only select the options given and try again.");
}
}
catch (InputMismatchException imx){
sc.nextLine();
System.out.println ("Invalid choice selected.");
System.out.println ("Enter the number only and please try again.");
}
}
System.out.println();
//This calls the corresponding initialization methods based on user input.
if (choice == 1){
return initFileInput();
}
else {
return initUserInput();
}
}
//This is the method that will initialize the program through a file which the user has inputted.
//It takes in no parameters and returns a boolean to indicate the success of initialization.
public boolean initFileInput(){
//This initalizes all variables used.
Scanner sc = new Scanner (System.in);
String fileName;
BufferedReader in;
boolean validFile = false;
int departmentLen, courseLen, teacherLen, totalCourseLen, scheduleLen, studentLen;
int teacherCount = 0;
int courseCount = 0;
String departmentName, courseCode, teacherName, studentName;
int departmentNum, teacherNum, experience, size, studentNum;
int teacherPos, coursePos;
ArrayList<Teacher> temTeacherList = new ArrayList<Teacher>();
ArrayList<Course> temCourseList = new ArrayList<Course>();
ArrayList<Course> courseTaught = new ArrayList<Course>();
//This continues to ask the user for a valid input of file name.
while (!validFile){
System.out.print ("Enter the file name (without the \".txt\" extension): ");
fileName = sc.nextLine();
//This catches the multiple errors that might occur during the initialization.
try{
in = new BufferedReader (new FileReader (fileName + ".txt"));
validFile = true;
//This reads through the file and initializes the length of the fields.
departmentLen = Integer.parseInt(in.readLine());
in.readLine();
//This continues to read in department information.
for (int i = 0; i < departmentLen; i ++){
departmentName = in.readLine();
departmentNum = Integer.parseInt(in.readLine());
teacherLen = Integer.parseInt(in.readLine());
totalCourseLen = Integer.parseInt(in.readLine());
in.readLine();
//This creates an array of Teacher objects.
for (int j = 0; j < teacherLen; j ++){
teacherName = in.readLine();
teacherNum = Integer.parseInt(in.readLine());
experience = Integer.parseInt(in.readLine());
courseLen = Integer.parseInt(in.readLine());
for (int k = 0; k < courseLen; k ++){
courseTaught.add(new Course(in.readLine(), -1, null));
}
temTeacherList.add(new Teacher (teacherName, teacherNum, experience, courseTaught));
teacherList.add(teacherCount,temTeacherList.get(j));
teacherCount ++;
courseTaught = new ArrayList<Course>();
in.readLine();
}
//This creates an array of Course objects.
for (int j = 0; j < totalCourseLen; j ++){
courseCode = in.readLine();
size = Integer.parseInt(in.readLine());
teacherPos = findTeacher(Integer.parseInt(in.readLine()));
if (teacherPos != -1){
temCourseList.add (new Course (courseCode, size, teacherList.get(teacherPos)));
//This updates the teacher list with the corresponding courses.
coursePos = teacherList.get(teacherPos).findCourse(temCourseList.get(j).getCourseCode());
if (coursePos != -1){
teacherList.get(teacherPos).getCourseTaught().set(coursePos, temCourseList.get(j));
}
else{//This appends the new course to the end of the teacher's list of teachable courses.
teacherList.get(teacherPos).getCourseTaught().add(temCourseList.get(j));
}
}
else {
temCourseList.add(new Course (courseCode, size, new Teacher("",-1,-1,null)));
}
courseList.add(courseCount,temCourseList.get(j));
courseCount ++;
in.readLine();
}
//This creates the Department object depending on the type of Department.
//It returns false if the department type is not supported by the program.
departmentName = departmentName.toUpperCase();
if (departmentName.equals("MUSIC")){
departmentList.add (new Music (temTeacherList, temCourseList, departmentNum));
}
else if (departmentName.equals("MATHEMATICS")){
departmentList.add (new Mathematics(temTeacherList, temCourseList, departmentNum));
}
else if (departmentName.equals("COMPUTER STUDIES")){
departmentList.add (new ComputerStudies (temTeacherList, temCourseList, departmentNum));
}
else{
System.out.println ("Department not supported by current version.");
return false;
}
temTeacherList = new ArrayList<Teacher>();
temCourseList = new ArrayList<Course>();
}
//This reads in the number of students.
studentLen = Integer.parseInt(in.readLine());
in.readLine();
for (int i = 0; i < studentLen; i ++){
studentName = in.readLine();
studentNum = Integer.parseInt(in.readLine());
scheduleLen = Integer.parseInt(in.readLine());
for (int j = 0; j < scheduleLen; j ++){
courseCode = in.readLine();
coursePos = findCourse(courseCode);
if (coursePos != -1){
temCourseList.add(courseList.get(coursePos));
}
else{//This catches if there is an illegal course selected by the student.
System.out.println ("Illegal course selected by a student.");
return false;
}
}
studentList.add(new Student (studentName, studentNum, temCourseList));
temCourseList = new ArrayList<Course>();
in.readLine();
}
in.close();
}
catch (IOException iox){//This checks if the file name exists.
System.out.println ("File name does not exist.");
System.out.println();
}
catch (NumberFormatException nfx){//This checks if the format of the file is correct.
System.out.println ("There is an error in the format of the file.");
return false;
}
}
return true;
}
//This is the method that will search for the position of the Teacher responsible for a specific course.
//It takes in an integer and returns a integer.