-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.java
More file actions
1662 lines (1429 loc) · 66.6 KB
/
cli.java
File metadata and controls
1662 lines (1429 loc) · 66.6 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
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public class cli { //Based on working with text files
public static void main(String[] args) throws IOException {
System.out.println("Hello! welcome to Sbu!!");
login();
}
public static void login() throws IOException {
System.out.println("Please specify your role");
System.out.println("1: student");
System.out.println("2: teacher");
System.out.println("3: admin");
System.out.println("4: Sign up"+ "(have no account)");
// Before the login you should sign up
Scanner scanner = new Scanner(System.in);
int answer = scanner.nextInt();
switch (answer) {
case 1:
studentLogin();
break;
case 2:
teacherLogin();
break;
case 3:
adminLogin();
break;
case 4:
signUp();
break;
default:
System.out.println("Please enter a valid number");
login();
}
}
public static void adminLogin () throws IOException {
//In this method, we check the validity of the entered username and password for the admin
//For admin, we have only one username and password
//That specification is stored in a text file
System.out.println("Here is admin login");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your username and password:");
System.out.print("username: ");
String username = scanner.nextLine();
System.out.print("password: ");
String password = scanner.nextLine();
try {
File file = new File("D:\\University\\AP\\project\\project files\\Admin info.txt");
Scanner fileScanner = new Scanner(file);
boolean found = false;
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
if (line.contains(", Password: ")) {
String[] parts = line.split(", Password: ");
String usernameFromFile = parts[0].substring(10);
String passwordFromFile = parts[1];
if (username.equals(usernameFromFile) && password.equals(passwordFromFile)) {
found = true;
break;
}
} else {
System.out.println("Invalid line in file: " + line);
}
}
if (found) {
System.out.println("Login successful");
long l = Long.parseLong(username);
Admin admin = Admin.getInstance();
adminAfterLog();
} else {
System.out.println("Invalid username or password");
login();
}
fileScanner.close();
} catch (FileNotFoundException e) {
System.out.println(Arrays.toString(e.getStackTrace()));
}
}
public static void adminAfterLog() throws IOException {
//In this method, we show the tasks that the admin can do
Scanner scanner = new Scanner(System.in);
System.out.println("What are you going to do?");
System.out.println("1: Define the course");
System.out.println("2: delete the course");
System.out.println("3: Student registration");
System.out.println("4: delete the student");
System.out.println("5: Teacher registration");
System.out.println("6: delete the teacher");
System.out.println("7: Do some teacher works!");
System.out.println("8: Do some student works!");
// We always provide a way back for the user!!
System.out.println("9: Back to login page");
int ans = scanner.nextInt();
switch (ans){
case 1:
difineCourse();
break;
case 2:
deleteCourse();
break;
case 3:
studentRegis();
break;
case 4:
deleteStudent();
break;
case 5:
teacherRegis();
break;
case 6:
deleteTeacher();
break;
case 7:
teacherLogin();
break;
case 8:
studentLogin();
break;
case 9:
login();
break;
default:
System.out.println("Please enter a valid number");
adminAfterLog();
break;
}
}
public static void difineCourse() throws IOException {
//In this method, the admin can define a course and perform tasks for that course
/*
Each course that is born as an object creates two files.
One for maintaining the students of that course and the other for maintaining other details
*/
Scanner scanner = new Scanner(System.in);
System.out.println("Please print the course name");
String courseName = scanner.next();
Course course = new Course(courseName);
System.out.println("What do you want to do with this course?");
System.out.println("1: Choose a teacher");
System.out.println("2: Add student");
System.out.println("3: Determine the number of units");
System.out.println("4: Back");
int x = scanner.nextInt();
switch (x){
case 1:
// Both the teacher ID is written in the course file and the course name is written in the teacher file
// These two tasks are done in the methods of teacher and course classes
System.out.println("Please enter the teacher id");
long teacherId = scanner.nextLong();
Teacher teacher = new Teacher(teacherId);
course.setTeacher(teacher);
teacher.addCourse(course);
System.out.println("Done");
adminAfterLog();
break;
case 2:
// Both the student ID is written in the course file and the course name is written in the student file
System.out.println("Please enter the student id");
long studentIdForAdd = scanner.nextLong();
Student student = new Student(studentIdForAdd);
course.addStudent(student);
student.getCourses().add(course);
student.courseWriter.write("Course: " + course.getName() + "\n");
student.courseWriter.flush();
course.studentsWriter.write("Student: " + student.getId() + "\n");
course.studentsWriter.flush();
System.out.println("Done");
adminLogin();
break;
case 3:
System.out.println("Please enter the number of units");
int unitsNum = scanner.nextInt();
course.infoWriter.write("number of the units: " + unitsNum + "\n");
course.infoWriter.flush();
adminAfterLog();
break;
case 4:
adminAfterLog();
break;
default:
System.out.println("Please enter a valid number");
difineCourse();
break;
}
}
public static void deleteCourse() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the course name:");
String courseName = scanner.nextLine();
// Delete the name of the course from the student file
String courseStudentsPath = "D:\\University\\AP\\project\\project files\\courses\\courseStudents\\"
+ courseName + "_students.txt";
List<String> studentIds = new ArrayList<>();
try {
File file = new File(courseStudentsPath);
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String line = reader.nextLine();
String studentId = line.split(": ")[1].split(", grade")[0];
studentIds.add(studentId);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred while reading the file");
e.printStackTrace();
}
for (String studentId : studentIds) {
String studentCoursesPath = "D:\\University\\AP\\project\\project files\\students\\studentCourses\\"
+ studentId + "_courses.txt";
try {
List<String> lines = new ArrayList<>(Files.readAllLines(Paths.get(studentCoursesPath)));
lines.removeIf(line -> line.contains(courseName));
Files.write(Paths.get(studentCoursesPath), lines);
} catch (IOException e) {
System.out.println("An error occurred while updating the student's courses");
e.printStackTrace();
}
}
// Delete the name of the course from the teacher's file of that course
String courseInfoPath = "D:\\University\\AP\\project\\project files\\courses\\courseInformations\\"
+ courseName + "_info.txt";
String teacherId = "";
try {
File file = new File(courseInfoPath);
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String line = reader.nextLine();
if (line.startsWith("Teacher ID: ")) {
teacherId = line.split(": ")[1];
break;
}
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred while reading the file");
e.printStackTrace();
}
String teacherCoursesPath = "D:\\University\\AP\\project\\project files\\teachers\\teacherCourses\\"
+ teacherId + "_courses.txt";
try {
List<String> lines = new ArrayList<>(Files.readAllLines(Paths.get(teacherCoursesPath)));
lines.removeIf(line -> line.contains(courseName));
Files.write(Paths.get(teacherCoursesPath), lines);
} catch (IOException e) {
System.out.println("An error occurred while updating the teacher's courses");
e.printStackTrace();
}
// Delete all assignments related to that course
String assignmentsPath = "D:\\University\\AP\\project\\project files\\assignments\\";
try {
File file = new File(courseInfoPath);
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String line = reader.nextLine();
if (line.startsWith("Assignment: ")) {
String assignmentName = line.split(": ")[1].split(", Deadline")[0];
Files.deleteIfExists(Paths.get(assignmentsPath + assignmentName + ".txt"));
}
}
reader.close();
} catch (IOException e) {
System.out.println("An error occurred while deleting the assignments");
e.printStackTrace();
}
// Delete course files
try {
Files.deleteIfExists(Paths.get(courseStudentsPath));
Files.deleteIfExists(Paths.get(courseInfoPath));
} catch (IOException e) {
System.out.println("An error occurred while deleting the course files");
e.printStackTrace();
}
System.out.println("Done");
adminAfterLog();
}
public static void studentRegis() throws IOException {
//In this method, the administrator defines a new student
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the student id:");
System.out.print("student id: ");
long studentId = scanner.nextLong();
//Username verification
while(!usernameValidator(studentId)) {
System.out.println("This student id is not valid");
System.out.println("Be careful that the student id is an 8-digit number (it is your ID).");
System.out.println("Please enter the student id");
System.out.print("student id: ");
studentId = scanner.nextLong();
}
System.out.println("Please enter the password:");
System.out.print("password: ");
String password = scanner.next();
//password verification
while (!passwordValidator(password)) {
System.out.println("This password in not valid");
System.out.println("Be careful that the length of the password must be at least 8 and " +
"the password must consist of uppercase and lowercase letters as well as numbers");
System.out.println("Please enter your password");
System.out.print("password: ");
password = scanner.next();
}
try { // Username and password in a specified file
Student student = new Student(studentId);
FileWriter writer = new FileWriter("D:\\University\\AP\\project\\project files\\" +
"Student info.txt", true);
writer.write("Username: " + studentId + ", Password: " + password + "\n");
writer.close();
System.out.println("A new student has been registered with id: " + studentId);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
adminAfterLog();
}
public static void deleteStudent() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the student ID:");
String studentId = scanner.nextLine();
String studentCoursesPath = "D:\\University\\AP\\project\\project files\\students\\studentCourses\\"
+ studentId + "_courses.txt";
List<String> courseNames = new ArrayList<>();
try {
File file = new File(studentCoursesPath);
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String line = reader.nextLine();
String courseName = line.split(": ")[1];
courseNames.add(courseName);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred while reading the file");
e.printStackTrace();
}
// Deleting the student's name in his courses file
String courseStudentsPath = "D:\\University\\AP\\project\\project files\\courses\\courseStudents\\";
for (String courseName : courseNames) {
try {
File file = new File(courseStudentsPath + courseName + "_students.txt");
List<String> lines = new ArrayList<>(Files.readAllLines(Paths.get(file.getPath())));
lines.removeIf(line -> line.contains(studentId));
Files.write(Paths.get(file.getPath()), lines);
} catch (IOException e) {
System.out.println("An error occurred while updating the course's students");
e.printStackTrace();
}
}
// Deleting the student's files
try {
Files.deleteIfExists(Paths.get(studentCoursesPath));
Files.deleteIfExists(Paths.get("D:\\University\\AP\\project\\project files\\students\\studentInfos\\"
+ studentId + "_info.txt"));
} catch (IOException e) {
System.out.println("An error occurred while deleting the student's files");
e.printStackTrace();
}
System.out.println("Done");
adminAfterLog();
}
public static void teacherRegis() throws IOException {
//In this method, the administrator defines a new teacher
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the teacher id:");
System.out.print("teacher id: ");
long teacherId = scanner.nextLong();
//Username verification
while(!usernameValidator(teacherId)) {
System.out.println("This techer id is not valid");
System.out.println("Be careful that the teacher id is an 8-digit number (it is your ID).");
System.out.println("Please enter the teacher id");
System.out.print("teacher id: ");
teacherId = scanner.nextLong();
}
System.out.println("Please enter the password:");
System.out.print("password: ");
String password = scanner.next();
//password verification
while (!passwordValidator(password)) {
System.out.println("This password in not valid");
System.out.println("Be careful that the length of the password must be at least 8 and " +
"the password must consist of uppercase and lowercase letters as well as numbers");
System.out.println("Please enter your password");
System.out.print("password: ");
password = scanner.next();
}
try { // Username and password in a specified file
Teacher teacher = new Teacher(teacherId);
FileWriter writer = new FileWriter("D:\\University\\AP\\project\\project files\\" +
"Teacher info.txt", true);
writer.write("Username: " + teacherId + ", Password: " + password + "\n");
writer.close();
System.out.println("A new teacher has been registered with id: " + teacherId);
} catch (IOException e) {
e.printStackTrace();
}
adminAfterLog();
}
public static void deleteTeacher() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the teacher ID:");
String teacherId = scanner.nextLine();
String teacherCoursesPath = "D:\\University\\AP\\project\\project files\\teachers\\teacherCourses\\" +
teacherId + "_courses.txt";
List<String> courseNames = new ArrayList<>();
try {
File file = new File(teacherCoursesPath);
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String line = reader.nextLine();
String courseName = line.split(": ")[1];
courseNames.add(courseName);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred while reading the file");
e.printStackTrace();
}
// Deleting the teacher's name in his courses file
String courseInfosPath = "D:\\University\\AP\\project\\project files\\courses\\courseInformations\\";
for (String courseName : courseNames) {
try {
File file = new File(courseInfosPath + courseName + "_info.txt");
List<String> lines = new ArrayList<>(Files.readAllLines(Paths.get(file.getPath())));
lines.removeIf(line -> line.contains(teacherId));
Files.write(Paths.get(file.getPath()), lines);
} catch (IOException e) {
System.out.println("An error occurred while updating the course's teachers");
e.printStackTrace();
}
}
// Deleting the teacher's files
try {
Files.deleteIfExists(Paths.get(teacherCoursesPath));
Files.deleteIfExists(Paths.get("D:\\University\\AP\\project\\project files\\teachers\\teacherInfo\\"
+ teacherId + "_info.txt"));
} catch (IOException e) {
System.out.println("An error occurred while deleting the teacher's files");
e.printStackTrace();
}
System.out.println("Done");
adminAfterLog();
}
public static void teacherLogin () {
//In this method, professors enter the application by entering valid ID and password.
//Username and password must match what is in the database
System.out.println("Here is teacher login");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your username and password:" + "username is your id");
System.out.print("username: ");
String username = scanner.nextLine();
System.out.print("password: ");
String password = scanner.nextLine();
try {
File file = new File("D:\\University\\AP\\project\\project files\\Teacher info.txt");
Scanner fileScanner = new Scanner(file);
boolean found = false;
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] parts = line.split(", Password: ");
String usernameFromFile = parts[0].substring(10);
String passwordFromFile = parts[1];
if (username.equals(usernameFromFile) && password.equals(passwordFromFile)) {
found = true;
break;
}
}
if (found) {
System.out.println("Login successful");
long l = Long.parseLong(username);
// When each teacher logs in, an object of the teacher's type is created
Teacher teacher = new Teacher(l);
teacherAfterLog(teacher);
} else {
System.out.println("Invalid username or password");
login();
}
fileScanner.close();
} catch (FileNotFoundException e) {
System.out.println(Arrays.toString(e.getStackTrace()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void teacherAfterLog(Teacher teacher) throws IOException {
//This method displays what the master can do
/*
With the birth of each teacher as an object, two text files are created.
One for maintaining the teacher's courses and the other for maintaining other teacher's information
*/
Scanner scanner = new Scanner(System.in);
System.out.println("What are you going to do?");
System.out.println("1: Complete the profile");
System.out.println("2: View profile");
System.out.println("3: View courses");
System.out.println("4: Add a course");
System.out.println("5: Add student to course");
System.out.println("6: Remove student to course");
System.out.println("7: Add assignment to course");
System.out.println("8: Remove assignment to course");
System.out.println("9: Grade student in course");
System.out.println("10: Back to login page");
int ans = scanner.nextInt();
switch (ans){
case 1:
teacherCompleteProf(teacher);
break;
case 2:
teacherViewProf(teacher);
break;
case 3:
teacherViewCourses(teacher);
break;
case 4:
teacherAddCourse(teacher);
break;
case 5:
teacherAddStudent(teacher);
break;
case 6:
teacherRemoveStudent(teacher);
break;
case 7:
teacherAddAssignment(teacher);
break;
case 8:
teacherRemoveAssignment(teacher);
break;
case 9:
teacherGradeStudent(teacher);
break;
case 10:
login();
default:
System.out.println("Please enter a valid number");
teacherAfterLog(teacher);
break;
}
}
public static void teacherCompleteProf(Teacher teacher) throws IOException {
// Here the professor can complete his personal information
// This information is stored in the teacher's file
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your full name");
System.out.print("Full name: ");
String teacherFullName = scanner.nextLine();
FileWriter teacherFile = new FileWriter("D:\\University\\AP\\project\\project files\\teachers\\teacherInfo\\"
+ teacher.getId() + "_info.txt" , true);
teacherFile.write("Fullname: " + teacherFullName + "\n");
teacherFile.close();
System.out.println("done successfully");
teacherAfterLog(teacher);
}
public static void teacherViewProf(Teacher teacher) throws IOException {
// This is done by reading the teacher file
String filePath = "D:\\University\\AP\\project\\project files\\teachers\\teacherInfo\\"
+ teacher.getId() + "_info.txt";
try {
File file = new File(filePath);
Scanner reader = new Scanner(file);
String firstName = reader.nextLine().split(": ")[1];
String lastName = reader.nextLine().split(": ")[1];
System.out.println("First Name: " + firstName);
System.out.println("Last Name: " + lastName);
System.out.println("ID: " + teacher.getId());
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred while reading the file");
e.printStackTrace();
}
System.out.println("done successfully");
teacherAfterLog(teacher);
}
public static void teacherViewCourses(Teacher teacher) throws IOException {
// Here the professor can see the courses he is the professor of
// This is done by reading the teacher file
File teacherFile = new File("D:\\University\\AP\\project\\project files\\teachers\\teacherCourses\\"
+ teacher.getId() + "_courses.txt");
try {
Scanner fileReader = new Scanner(teacherFile);
System.out.println("Courses for " + teacher.getId() + ":");
while (fileReader.hasNextLine()) {
String line = fileReader.nextLine();
if (line.startsWith("Course: ")) {
System.out.println(line.substring(8)); // Print the course name
}
}
fileReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
teacherAfterLog(teacher);
}
public static void teacherAddCourse(Teacher teacher) {
//Here the teacher can take a new course
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the course name:");
String courseName = scanner.nextLine();
File courseInfoFile = new File("D:\\University\\AP\\project\\project files\\courses\\courseInformations\\"
+ courseName + "_info.txt");
File teacherFile = new File("D:\\University\\AP\\project\\project files\\teachers\\teacherCourses\\"
+ teacher.getId() + "_courses.txt");
File courseStuFile = new File("D:\\University\\AP\\project\\project files\\courses\\courseStudents\\"
+ courseName + "_students.txt");
try {
//If this course has not been defined before, we will define it ourselves
if (!courseInfoFile.exists()) {
courseInfoFile.createNewFile();
courseStuFile.createNewFile();
}
// Write the teacher's ID in the course file
FileWriter courseWriter = new FileWriter(courseInfoFile , true);
courseWriter.write("Teacher ID: " + teacher.getId() + "\n");
courseWriter.close();
if (!teacherFile.exists()) {
teacherFile.createNewFile();
}
// Write the name of the course in the teacher's file
FileWriter teacherWriter = new FileWriter(teacherFile, true);
teacherWriter.write("Course: " + courseName + "\n");
teacherWriter.close();
System.out.println("The course has been added successfully!");
teacherAfterLog(teacher);
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public static void teacherAddStudent(Teacher teacher) throws IOException {
//Here the professor can add students to the course he has taken
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the student ID:");
System.out.print("student id: ");
String studentId = scanner.nextLine();
System.out.println("Please enter the course name:");
System.out.print("course name: ");
String courseName = scanner.nextLine();
File studentCourseFile = new File("D:\\University\\AP\\project\\project files\\students\\studentCourses\\"
+ studentId + "_courses.txt");
File studentInfoFile = new File("D:\\University\\AP\\project\\project files\\students\\studentInfos"
+ studentId + "_info.txt");
File courseStuFile = new File("D:\\University\\AP\\project\\project files\\courses\\courseStudents\\"
+ courseName + "_students.txt");
//If the entered student or course does not exist, an appropriate error message will be printed
try {
if (!studentCourseFile.exists()) {
System.out.println("This student does not exist.");
teacherAfterLog(teacher);
return;
}
if (!courseStuFile.exists()) {
System.out.println("This course does not exist.");
teacherAfterLog(teacher);
return;
}
// Write the name of the course in the student file
FileWriter studentWriter = new FileWriter(studentCourseFile, true);
studentWriter.write( "Course: " + courseName + "\n");
studentWriter.close();
// Write the student ID in the course file
FileWriter courseWriter = new FileWriter(courseStuFile, true);
courseWriter.write("Student: " + studentId + "\n");
courseWriter.close();
System.out.println("The student has been added to the course successfully!");
} catch (IOException e) {
e.printStackTrace();
}
try {
File oldFile = new File("D:\\University\\AP\\project\\project files\\students\\studentInfos\\"
+ studentId + "_info.txt");
File courseInfo = new File("D:\\University\\AP\\project\\project files\\courses\\courseInformations\\"
+ courseName + "_info.txt");
//Find the number of course units
String targetLinePrefix = "number of the units: ";
BufferedReader reader = new BufferedReader(new FileReader(courseInfo));
String line;
int x = 0;
while ((line = reader.readLine()) != null) {
if (line.startsWith(targetLinePrefix)) {
String valueString = line.substring(targetLinePrefix.length()).trim();
x = Integer.parseInt(valueString);
break;
}
}
int numberToAdd = x;
BufferedReader studentReader = new BufferedReader(new FileReader(oldFile));
String firstLine = studentReader.readLine();
studentReader.close();
String[] parts = firstLine.split(": ");
int currentNumber = Integer.parseInt(parts[1]);
int newNumber = currentNumber + numberToAdd;
String updatedFirstLine = "Number of units: " + newNumber;
Files.write(Paths.get("D:\\University\\AP\\project\\project files\\students\\studentInfos\\"
+ studentId + "_info.txt"), Collections.singleton(updatedFirstLine));
} catch (IOException e){
e.printStackTrace();
}
teacherAfterLog(teacher);
}
public static void teacherRemoveStudent(Teacher teacher) throws IOException {
//In this method, the professor can remove a student from his course
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the student id:");
System.out.print("student id: ");
String studentId = scanner.next();
System.out.println("Please enter the course name:");
System.out.print("course name: ");
String courseName = scanner.next();
// The course must belong to the teacher
File teacherFile = new File("D:\\University\\AP\\project\\project files\\teachers\\teacherCourses\\"
+ teacher.getId() + "_courses.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(teacherFile))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.equals("Course: " + courseName)) {
Course courseToRemove = null;
for (Course course : teacher.getCourses()) {
if (course.getName().equals(courseName)) {
courseToRemove = course;
break;
}
}
if (courseToRemove != null) {
Student studentToRemove = null;
for (Student student : courseToRemove.getStudents()) {
if (String.valueOf(student.getId()).equals(String.valueOf(studentId))) {
studentToRemove = student;
break;
}
}
if (studentToRemove != null) {
courseToRemove.getStudents().remove(studentToRemove);
studentToRemove.getCourses().remove(courseToRemove);
}
}
// Our method is to write a new file as requested and replace the previous file
// Replace the student file
File oldFile = new File("D:\\University\\AP\\project\\project files\\students\\studentCourses\\"
+ studentId + "_courses.txt");
File newFile = new File("D:\\University\\AP\\project\\project files\\students\\studentCourses\\"
+ "new.txt");
BufferedReader studentReader = new BufferedReader(new FileReader(oldFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(newFile));
String lineToRemove = "Course: " + courseName;
String currentLine;
while((currentLine = studentReader.readLine()) != null) {
if(currentLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
studentReader.close();
oldFile.delete();
newFile.renameTo(oldFile);
// Replace the course file
oldFile = new File("D:\\University\\AP\\project\\project files\\courses\\courseStudents\\"
+ courseName + "_students.txt");
newFile = new File("D:\\University\\AP\\project\\project files\\courses\\courseStudents\\"
+ "temp.txt");
studentReader = new BufferedReader(new FileReader(oldFile));
writer = new BufferedWriter(new FileWriter(newFile));
lineToRemove = "Student: " + studentId;
while((currentLine = studentReader.readLine()) != null) {
if(currentLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
studentReader.close();
oldFile.delete();
newFile.renameTo(oldFile);
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
teacherAfterLog(teacher);
}
public static void teacherAddAssignment(Teacher teacher) throws IOException {
//In this method, the professor can add an assignment to the course
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the assignment name:");
String assignmentName = scanner.next();
System.out.println("Please enter the course name:");
String courseName = scanner.next();
System.out.println("Please enter the deadline:");
String deadline = scanner.next();
// The course must belong to the teacher
File teacherFile = new File("D:\\University\\AP\\project\\project files\\teachers\\teacherCourses\\"
+ teacher.getId() + "_courses.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(teacherFile))) {
String line;
boolean teacherHasCourse = false;
while ((line = reader.readLine()) != null) {
if (line.equals("Course: " + courseName)) {
teacherHasCourse = true;
break;
}
}
if (!teacherHasCourse) {
System.out.println("The teacher does not teach this course.");
teacherAfterLog(teacher);
return;
}
} catch (IOException e) {
e.printStackTrace();
}
// If the teacher has the course, create a new assignment and add it to the course
try {
Course course = new Course(courseName);
Assignment assignment = new Assignment(assignmentName, deadline, course);
course.getAssignments().add(assignment);
// Write the assignment's name and deadline to the course's file
File courseFile =
new File("D:\\University\\AP\\project\\project files\\courses\\courseInformations\\"
+ courseName + "_info.txt");
FileWriter courseWriter = new FileWriter(courseFile , true);
courseWriter.write("Assignment: " + assignmentName + ", Deadline: " + deadline + ", Active" + "\n");
courseWriter.close();
// Write the assignment's details to the assignment's file
FileWriter assignmentWriter = new FileWriter("D:\\University\\AP\\project\\project files\\assignments\\"
+ assignmentName + ".txt", true);
assignmentWriter.write("Name: " + assignmentName + "\nCourse: " + courseName + "\nActive" + "\n" + "Done by:" + "\n");
assignmentWriter.close();
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
}
teacherAfterLog(teacher);
}
public static void teacherRemoveAssignment(Teacher teacher) throws IOException {
// In this method, the teacher can remove an assignment from the course
// For this purpose, we only change "active" to "active: false" in the files
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the course name:");
String courseName = scanner.next();
System.out.println("Please enter the assignment name:");
String assignmentName = scanner.next();
// The course must belong to the teacher
File teacherFile = new File("D:\\University\\AP\\project\\project files\\teachers\\teacherCourses\\"
+ teacher.getId() + "_courses.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(teacherFile))) {
String line;
boolean teacherHasCourse = false;
while ((line = reader.readLine()) != null) {
if (line.equals("Course: " + courseName)) {
teacherHasCourse = true;
break;
}
}
if (!teacherHasCourse) {
System.out.println("The teacher does not teach this course.");
return;
}
} catch (IOException e) {
e.printStackTrace();
}
// Check if the assignment is in the course
File courseFile = new File("D:\\University\\AP\\project\\project files\\courses\\courseInformations\\"
+ courseName + "_info.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(courseFile))) {
String line;
boolean courseHasAssignment = false;