C class · schoolcourseenrolmentsystem
Administrator.java
Administrator extends User<Administrator> and holds the management operations: adding and removing accounts, creating and closing courses, and assigning instructors.
public class Administrator extends User <Administrator >
Extends User<Administrator>
Role Always ADMIN, set in the constructor
Menu 14 options, see the Admin session
Saved to administrators.txt
Overview
Source 192 lines
Responsibilities# Add, remove and update students and instructors Create courses and close them Assign instructors to courses List every student or instructor Constructors# c Administrator ()Default L10 Empty administrator, used as the throwaway instance for Helpers.login.
c Administrator (String name, String id, String password, String email, String phoneNumber, String address)Parameterised L13 Sets inherited fields and the role explicitly to ADMIN.
6 methods
Accounts# m void addStudent (Student specificStudent, List <Student > listOfStudents)L82 Append a student and print a confirmation.
m boolean removeStudent (List <Student > listOfStudents, String targetId)L45 Remove a student by ID with an Iterator, so the list can change safely mid-loop.
m void updateStudent (String targetId, List <Student > listOfStudents, String newName, String newId, String newPassword, String newEmail, String newPhoneNumber, User .Role newRole, String newAddress)L89 Overwrite a student's name, password, email, phone, role and address by ID.
m void addInstructor (Instructor specificInstructor, List <Instructor > listOfInstructors)L76 Append an instructor and print a confirmation.
m boolean removeInstructor (List <Instructor > listOfInstructors, String targetId)L60 Remove an instructor by ID with an Iterator.
m void updateInstructor (String instructorTargetId, List <Instructor > listOfInstructors, String newName, String newPassword, String newEmail, String newPhoneNumber, User .Role newRole, String newAddress)L111 Same as updateStudent, for instructors.
2 methods
Listings# m void viewStudentList (List <Student > listOfStudents)L172 Print every student with toString(), or a message when empty.
m void viewInstructorList (List <Instructor > instructors)L182 Print every instructor with toString(), or a message when empty.
Source C Administrator.java 192 lines · from the project report Copy
1 package schoolcourseenrolmentsystem;4 import schoolcourseenrolmentsystem.Course .EnrollmentStatusEnum ;6 public class Administrator extends User <Administrator > {8 // We must define the default constructor in case we define any other custom 10 public Administrator () { 13 public Administrator (String name, String id, String password, String email, String phoneNumber, String address) { 14 super (name, id, password, email, phoneNumber, User .Role .ADMIN , address); 19 // Login/logout & role 21 public Administrator login (List <Administrator > listOfAdministrators, String id, String password) { 22 Administrator returnAdmin = null ; 23 for (Administrator administrator : listOfAdministrators) { 24 if (administrator.getId ().equals (id) && administrator.getPassword ().equals (password)) { 25 System .out.println ("Administrator " + administrator.getName () + " logged in." ); 26 returnAdmin = administrator; 29 System .out.println ("No admin record was found with the ID and password provided." ); 35 public String logout (Administrator administrators) { 36 return ("Administrator " + administrators.getName () + " logged out." ); 39 // Add and remove users 41 /// Loop used to iterate over all the students/instructors. 42 /// An Iterator keeps track of the current position safely, and when you call 43 /// remove(), it tells the collection to safely remove the current element 44 /// without invalidating the loop. 45 public boolean removeStudent (List <Student > listOfStudents, String targetId) { 46 Iterator <Student > iterator = listOfStudents.iterator (); 47 while (iterator.hasNext ()) { 48 Student specificStudent = iterator.next (); 49 if (specificStudent.getId ().equals (targetId)) { 51 System .out.println (specificStudent.getName () + " with the ID: " + specificStudent.getId () + " has been 52 successfully removed"); 56 System .out.println (targetId + " is not found." ); 60 public boolean removeInstructor (List <Instructor > listOfInstructors, String targetId) { 61 // Loop used to iterate over all the instructors 62 Iterator <Instructor > iterator = listOfInstructors.iterator (); 63 while (iterator.hasNext ()) { 64 Instructor specificInstructor = iterator.next (); 65 if (specificInstructor.getId ().equals (targetId)) { 67 System .out.println (specificInstructor.getName () + " with the ID: " + specificInstructor.getId () + " removed 72 System .out.println ("Instructor with the ID: " + targetId + " not found." ); 76 public void addInstructor (Instructor specificInstructor, List <Instructor > listOfInstructors) { 78 listOfInstructors.add (specificInstructor); 79 System .out.printf ("Instructor added: %s with the ID: %s" , specificInstructor.getName (), specificInstructor.getId ()); 82 public void addStudent (Student specificStudent, List <Student > listOfStudents) { 83 listOfStudents.add (specificStudent); 84 System .out.printf ("Student added: %s with the ID: %s" , specificStudent.getName (), specificStudent.getId ()); 89 public void updateStudent (String targetId, List <Student > listOfStudents, String newName, String newId, String 90 newPassword, String newEmail, String newPhoneNumber, User .Role newRole, String newAddress) { 92 Student updateStudentInfo = null ; 93 for (Student student : listOfStudents) { 94 if (student.getId ().equals (targetId)) { 95 updateStudentInfo = student; 99 if (updateStudentInfo != null ) { 100 updateStudentInfo.setName (newName); 101 updateStudentInfo.setPassword (newPassword); 102 updateStudentInfo.setEmail (newEmail); 103 updateStudentInfo.setPhoneNumber (newPhoneNumber); 104 updateStudentInfo.setRole (newRole); 105 updateStudentInfo.setAddress (newAddress); 107 System .out.println ("Student with the ID " + targetId + " not found." ); 111 public void updateInstructor (String instructorTargetId, List <Instructor > listOfInstructors, String newName, String 112 newPassword, String newEmail, String newPhoneNumber, User .Role newRole, String newAddress) { 114 Instructor updateInstructorInfo = null ; 116 for (Instructor instructor : listOfInstructors) { 117 if (instructor.getId ().equals (instructorTargetId)) { 118 updateInstructorInfo = instructor; 122 if (updateInstructorInfo != null ) { 123 updateInstructorInfo.setName (newName); 124 updateInstructorInfo.setPassword (newPassword); 125 updateInstructorInfo.setEmail (newEmail); 126 updateInstructorInfo.setPhoneNumber (newPhoneNumber); 127 updateInstructorInfo.setRole (newRole); 128 updateInstructorInfo.setAddress (newAddress); 130 System .out.println ("Instructor with the ID " + instructorTargetId + " not found." ); 134 public void addCourse (List <Course > listOfCourses, Course specificCourse) { 135 if (listOfCourses.add (specificCourse)) { 136 System .out.printf ("Course added: (%s) (%s) succesfully." , specificCourse.getCourseName (), 137 specificCourse.getCourseCode ()); 139 System .out.println ("Course already exists: " + specificCourse.getCourseName ()); 143 public void assignInstructor (Course specificCourse, Instructor specificInstructor) { 144 if (specificCourse.getInstructor () == null ) { 145 specificCourse.setInstructor (specificInstructor); 146 System .out.printf ("Instructor %s is assigned to %s" , specificInstructor.getName (), 147 specificCourse.getCourseName ()); 148 } else if (specificCourse.getInstructor ().equals (specificInstructor)) { 149 System .out.printf ("Instructor %s is already assigned to %s" , specificInstructor.getName (), 150 specificCourse.getCourseName ()); 152 System .out.println ("Course already has a different instructor assigned." ); 157 public void closeCourse (Course specificCourse, List <Course > listOfCourses) { 158 if (listOfCourses.contains (specificCourse)) { 159 if (specificCourse.getEnrolledStudents ().size () >= specificCourse.getCapacity ()) { 160 System .out.println (specificCourse.getCourseName () + " is at full capacity." ); 161 specificCourse.setEnrollmentStatus (EnrollmentStatusEnum .Closed ); 162 } else if (specificCourse.getEnrollmentStatus () == Course .EnrollmentStatusEnum .Closed ) { 163 System .out.println (specificCourse.getCourseName () + " enrollment status is closed." ); 165 } else if (!listOfCourses.contains (specificCourse)) { 166 System .out.println (specificCourse.getCourseName () + " not found." ); 168 System .out.println (specificCourse.getCourseName () + " still has available capacity." ); 172 public void viewStudentList (List <Student > listOfStudents) { 173 if (listOfStudents == null || listOfStudents.isEmpty ()) { 174 System .out.println ("\nNo students are on record." ); 176 for (Student student : listOfStudents) { 177 System .out.println (student.toString ()); 182 public void viewInstructorList (List <Instructor > instructors) { 183 if (instructors == null || instructors.isEmpty ()) { 184 System .out.println ("\nNo instructors are on record." ); 186 for (Instructor instructor : instructors) { 187 System .out.println (instructor.toString ());