CS102/enrolment
Report
enrolment›src›schoolcourseenrolmentsystem›Administrator.java

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

Responsibilities#

  • Add, remove and update students and instructors
  • Create courses and close them
  • Assign instructors to courses
  • List every student or instructor

Constructors#

  • Administrator()DefaultL10

    Empty administrator, used as the throwaway instance for Helpers.login.

  • Administrator(String name, String id, String password, String email, String phoneNumber, String address)ParameterisedL13

    Sets inherited fields and the role explicitly to ADMIN.

2 methods

Authentication#

  • Administrator login(List<Administrator> listOfAdministrators, String id, String password)L21

    Search the administrator list for a matching ID and password.

    • Prints the “No admin record” message inside the loop (see Known issues)
  • String logout(Administrator administrators)L35

    Return “Administrator <name> logged out.”

6 methods

Accounts#

  • void addStudent(Student specificStudent, List<Student> listOfStudents)L82

    Append a student and print a confirmation.

  • boolean removeStudent(List<Student> listOfStudents, String targetId)L45

    Remove a student by ID with an Iterator, so the list can change safely mid-loop.

  • 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.

  • void addInstructor(Instructor specificInstructor, List<Instructor> listOfInstructors)L76

    Append an instructor and print a confirmation.

  • boolean removeInstructor(List<Instructor> listOfInstructors, String targetId)L60

    Remove an instructor by ID with an Iterator.

  • 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.

3 methods

Courses#

  • void addCourse(List<Course> listOfCourses, Course specificCourse)L134

    Append a course and print a confirmation.

  • void assignInstructor(Course specificCourse, Instructor specificInstructor)L143

    Assign an instructor if the course has none yet.

    • Refuses to overwrite a different instructor
  • void closeCourse(Course specificCourse, List<Course> listOfCourses)L157

    Close a full course.

    • Full: prints “is at full capacity” and closes it
    • Already closed: says so
    • Open and not full: prints nothing (see Known issues)

2 methods

Listings#

  • void viewStudentList(List<Student> listOfStudents)L172

    Print every student with toString(), or a message when empty.

  • void viewInstructorList(List<Instructor> instructors)L182

    Print every instructor with toString(), or a message when empty.

Known issues#

Source

Administrator.java192 lines · from the project report
1package schoolcourseenrolmentsystem;
2
3import java.util.*;
4import schoolcourseenrolmentsystem.Course.EnrollmentStatusEnum;
5
6public class Administrator extends User<Administrator> {
7 // Constructors
8 // We must define the default constructor in case we define any other custom
9 // constructors
10 public Administrator() {
11 }
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);
15 }
17 // Methods
19 // Login/logout & role
20 @Override
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;
27 break;
28 }
29 System.out.println("No admin record was found with the ID and password provided.");
30 }
31 return returnAdmin;
32 }
34 @Override
35 public String logout(Administrator administrators) {
36 return ("Administrator " + administrators.getName() + " logged out.");
37 }
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)) {
50 iterator.remove();
51 System.out.println(specificStudent.getName() + " with the ID: " + specificStudent.getId() + " has been
52 successfully removed");
53 return true;
54 }
55 }
56 System.out.println(targetId + " is not found.");
57 return false;
58 }
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)) {
66 iterator.remove();
67 System.out.println(specificInstructor.getName() + " with the ID: " + specificInstructor.getId() + " removed
68 successfully.");
69 return true;
70 }
71 }
72 System.out.println("Instructor with the ID: " + targetId + " not found.");
73 return false;
74 }
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());
80 }
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());
85 }
87 // Update user info.
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;
96 break;
97 }
98 }
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);
106 } else {
107 System.out.println("Student with the ID " + targetId + " not found.");
108 }
109 }
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;
119 break;
120 }
121 }
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);
129 } else {
130 System.out.println("Instructor with the ID " + instructorTargetId + " not found.");
131 }
132 }
133 // Course related.
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());
138 } else {
139 System.out.println("Course already exists: " + specificCourse.getCourseName());
140 }
141 }
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());
151 } else {
152 System.out.println("Course already has a different instructor assigned.");
154 }
155 }
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.");
164 }
165 } else if (!listOfCourses.contains(specificCourse)) {
166 System.out.println(specificCourse.getCourseName() + " not found.");
167 } else {
168 System.out.println(specificCourse.getCourseName() + " still has available capacity.");
169 }
170 }
172 public void viewStudentList(List<Student> listOfStudents) {
173 if (listOfStudents == null || listOfStudents.isEmpty()) {
174 System.out.println("\nNo students are on record.");
175 } else {
176 for (Student student : listOfStudents) {
177 System.out.println(student.toString());
178 }
179 }
180 }
182 public void viewInstructorList(List<Instructor> instructors) {
183 if (instructors == null || instructors.isEmpty()) {
184 System.out.println("\nNo instructors are on record.");
185 } else {
186 for (Instructor instructor : instructors) {
187 System.out.println(instructor.toString());
188 }
189 }
190 }