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

class· schoolcourseenrolmentsystem

Instructor.java

Instructor extends User<Instructor>. It adds no fields; courses point to their instructor, so every method works by filtering the course list for courses assigned to this instructor.

public class Instructor extends User<Instructor>
Extends
User<Instructor>
Adds
No fields, courses reference the instructor
Menu
5 options, see the Instructor session
Saved to
instructors.txt

Responsibilities#

  • See students enrolled in your own courses
  • Record or update a grade for an assessment
  • Update schedule and description of courses you teach
  • Update personal information

Constructors#

  • Instructor()DefaultL7

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

  • Instructor(String name, String id, String password, String email, String phoneNumber, String address, List<Student> enrolledStudents)ParameterisedL10

    Sets inherited fields with Role.INSTRUCTOR. It accepts an enrolledStudents list but never uses it.

2 methods

Authentication#

  • Instructor login(List<Instructor> listOfInstructors, String id, String password)L17

    Search the instructor list for a matching ID and password.

    • Prints “Instructor <name> logged in.” on success
  • String logout(Instructor instructor)L29

    Return “Instructor <name> logged out.”

3 methods

Teaching#

  • void viewEnrolledStudents(Instructor instructor, List<Course> listOfCourses)L33

    Print the roster of every course assigned to this instructor.

    • Handles no assigned courses and empty rosters
  • void gradeStudent(List<Assessment> listOfGrades, List<Course> listOfCourses)L62

    Five-step grading flow: course → student → exam type → score → save.

    • Only lists your own courses
    • Score must be 0 to 100
    • Updates an existing grade or records a new Assessment
  • void updateCourseInfo(String courseCode, List<Course> listOfCourses, String newSchedule, String newDescription)L161

    Change schedule and description of a course you teach.

    • Course must exist
    • You must be its assigned instructor

2 methods

Profile#

  • void updateInstructorPersonalInfo(String newPassword, String newEmail, String newPhoneNumber, String newAddress)L196

    Apply a new password, email, phone number and address.

  • String toString()L207

    Return “-ID: <id> Name: <name>” for listings.

Known issues#

Source

Instructor.java210 lines · from the project report
1package schoolcourseenrolmentsystem;
2
3import java.util.*;
4
5public class Instructor extends User<Instructor> {
6 // Constructor
7 public Instructor() {
8 }
9
10 public Instructor(String name, String id, String password, String email, String phoneNumber,
11 String address, List<Student> enrolledStudents) {
12 super(name, id, password, email, phoneNumber, Role.INSTRUCTOR, address);
13 }
15 // ======================= Methods =======================
16 @Override
17 public Instructor login(List<Instructor> listOfInstructors, String id, String password) {
18 for (Instructor instructor : listOfInstructors) {
19 if (instructor.getId().equals(id) && instructor.getPassword().equals(password)) {
20 System.out.println("Instructor " + instructor.getName() + " logged in.\n");
21 return instructor;
22 }
23 }
24 System.out.println("No instructor record was found with the ID and password provided.");
25 return null;
27 }
28 @Override
29 public String logout(Instructor instructor) {
30 return ("Instructor " + instructor.getName() + " logged out.");
31 }
33 public void viewEnrolledStudents(Instructor instructor, List<Course> listOfCourses) {
34 boolean enrolledCourse = false;
35 // Look through all the courses
36 for (Course course : listOfCourses) {
37 // Make sure that the instructor is actually assigned to the course
38 if (course.getInstructor() != null && course.getInstructor().getId().equals(instructor.getId())) {
39 enrolledCourse = true;
40 // The instructor might teach multiple courses
41 System.out.printf("\n(Course: %s) (Code: %s)", course.getCourseName(), course.getCourseCode());
42 course.getEnrolledStudents();
44 // Make sure there are actually students enrolled in the course.
45 // Update the list.
46 List<Student> courseStudents = course.getEnrolledStudents();
47 if (courseStudents == null || courseStudents.isEmpty()) {
48 System.out.println("No students enrolled in this course.");
49 } else {
50 for (Student student : courseStudents) {
51 System.out.printf("\n-Student: %s, ID: %s, Email: %s, Phone number: %s", student.getName(),
52 student.getId(), student.getEmail(), student.getPhoneNumber());
53 }
54 }
55 }
56 }
57 if (!enrolledCourse) {
58 System.out.println("Instructor " + instructor.getName() + " is not assigned to any courses.");
59 }
60 }
62 public void gradeStudent(List<Assessment> listOfGrades, List<Course> listOfCourses) {
63 Scanner input = new Scanner(System.in);
65 try {
66 // Step 1: Show instructor's courses
67 List<Course> instructorCourses = new ArrayList<>();
68 System.out.println("\nCourses you're assigned to:");
69 for (Course course : listOfCourses) {
70 if (course.getInstructor() != null && course.getInstructor().getId().equals(this.getId())) {
71 instructorCourses.add(course);
72 System.out.println((instructorCourses.size()) + ". " + course.getCourseName() + " (" +
73 course.getCourseCode() + ")");
74 }
75 }
76 if (instructorCourses.isEmpty()) {
77 System.out.println("You're not assigned to any courses.");
78 return;
79 }
80 int courseChoice = Helpers.getSafeIntInput("Select a course to grade: ");
81 if (courseChoice < 1 || courseChoice > instructorCourses.size()) {
82 System.out.println("Invalid selection.");
83 return;
84 }
85 Course selectedCourse = instructorCourses.get(courseChoice - 1);
87 // Step 2: Select student
88 List<Student> enrolled = selectedCourse.getEnrolledStudents();
89 if (enrolled.isEmpty()) {
90 System.out.println("No students enrolled in this course.");
91 return;
92 }
93 System.out.println("\nStudents in this course:");
94 for (int i = 0; i < enrolled.size(); i++) {
95 System.out.printf((i + 1) + ".%s (ID: %s)", enrolled.get(i).getName(), enrolled.get(i).getId());
96 }
98 int studentChoice = Helpers.getSafeIntInput("\nSelect a student to grade: ");
99 if (studentChoice < 1 || studentChoice > enrolled.size()) {
100 System.out.println("Invalid student selection.");
101 return;
103 }
104 Student selectedStudent = enrolled.get(studentChoice - 1);
106 // Step 3: Choose exam type
107 Assessment.ExamType[] examTypes = Assessment.ExamType.values();
108 System.out.println("\nSelect the assessment type to grade:");
109 for (int i = 0; i < examTypes.length; i++) {
110 System.out.println((i + 1) + ". " + examTypes[i]);
111 }
112 int examChoice = Helpers.getSafeIntInput("Choice: ");
113 if (examChoice < 1 || examChoice > examTypes.length) {
114 System.out.println("Invalid selection.");
115 return;
116 }
117 Assessment.ExamType selectedExamType = examTypes[examChoice - 1];
119 // Step 4: Enter score
120 double score;
121 while (true) {
122 System.out.print("Enter score (0-100): ");
123 try {
124 score = input.nextDouble();
125 input.nextLine(); // Buffer
126 if (score < 0 || score > 100) {
127 System.out.println("Score must be between 0 and 100.");
128 } else {
129 break;
130 }
131 } catch (InputMismatchException e) {
132 System.out.println("Invalid input. Please enter a number.");
133 input.nextLine(); // Clear input
134 }
135 }
137 // Step 5: Assign or update grade
138 boolean updated = false;
139 for (Assessment g : listOfGrades) {
141 if (g.getStudentId().equals(selectedStudent.getId())
142 && g.getCourseCode().equals(selectedCourse.getCourseCode())
143 && g.getExamType().equals(selectedExamType)) {
144 g.setScore(score);
145 System.out.println("Grade updated successfully.");
146 updated = true;
147 break;
148 }
149 }
150 if (!updated) {
151 Assessment newAssessment = new Assessment(selectedStudent.getId(),
152 selectedCourse.getCourseCode(), selectedExamType, score, selectedExamType.toString());
153 listOfGrades.add(newAssessment);
154 System.out.println("Grade recorded successfully.");
155 }
156 } catch (Exception e) {
157 System.out.println(e.getMessage());
158 }
159 }
161 public void updateCourseInfo(String courseCode, List<Course> listOfCourses, String newSchedule, String
162 newDescription) {
164 // Now find the actual Course object
165 Course courseToUpdate = null;
166 for (Course course : listOfCourses) {
167 if (course.getCourseCode().equals(courseCode)) {
168 courseToUpdate = course;
169 break;
170 }
171 }
172 if (courseToUpdate == null) {
173 System.out.println("Course with code " + courseCode + " not found.");
174 return;
175 }
176 if (courseToUpdate != null) {
177 Instructor assignedInstructor = courseToUpdate.getInstructor();
179 if (assignedInstructor != null && assignedInstructor.getId().equals(this.getId())) {
180 courseToUpdate.setSchedule(newSchedule);
181 courseToUpdate.setDescription(newDescription);
183 System.out.printf("Instructor %s's course information updated for %s", assignedInstructor.getName(),
184 courseToUpdate.getCourseName());
185 } else if (assignedInstructor != null) {
186 System.out.printf("%s with the ID number: %s is not assigned to %s.", assignedInstructor.getName(),
187 assignedInstructor.getId(), courseToUpdate.getCourseName());
188 }
189 // You don't see it but its basically (assignedInstructor == null);
190 else {
191 System.out.printf("No instructor assigned to %s.", courseToUpdate.getCourseName());
192 }
193 }
194 }
196 public void updateInstructorPersonalInfo(String newPassword, String newEmail, String newPhoneNumber, String
197 newAddress) {
198 // No need to check if they match because instructors are already logged in.
199 setPassword(newPassword);
200 setEmail(newEmail);
201 setPhoneNumber(newPhoneNumber);
202 setAddress(newAddress);
203 System.out.println("Instructor " + getName() + "'s information updated.");
204 }
206 @Override
207 public String toString() {
208 return String.format("-ID: %s Name: %s", this.getId(), this.getName());
209 }