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

class· schoolcourseenrolmentsystem

Student.java

Student extends User<Student> with a credit limit and a list of enrolled courses, plus everything the student menu needs: enrolment, dropping, credit hours and grades.

public class Student extends User<Student>
Extends
User<Student>
Adds
creditLimit, enrolledCourses
Menu
8 options, see the Student session
Saved to
students.txt

Responsibilities#

  • Model a student account
  • Enrol in and drop courses
  • Track credit limit and enrolled hours
  • View grades through a temporary Assessment
  • Update personal information

Fields & enums#

TypeNameModifiersLine
intcreditLimitprivateL7
List<Course>enrolledCoursesprivateL8

Constructors#

  • Student()DefaultL10

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

  • Student(String name, String id, String password, String email, String phoneNumber, String address, int creditLimit, List<Course> enrolledCourses)ParameterisedL12

    Sets inherited fields through super(), the credit limit, and the course list. A null list becomes a new ArrayList to avoid a NullPointerException.

2 methods

Authentication#

  • Student login(List<Student> listOfStudents, String id, String password)L43

    Search the student list for a matching ID and password.

    • Prints “Student <name> logged in.” on success
    • Prints “No student record was found with the ID and password provided.” otherwise
  • String logout(Student student)L56

    Return “Student <name> logged out.”

4 methods

Enrolment#

  • void enroll_In_Course(Course course)L70

    Enrol in a course if every rule passes.

    • Not already enrolled
    • Course is not full
    • Enrolment status is Open
    • Current hours + course hours ≤ credit limit
    • On success adds the course to the student and the student to the course
  • void dropCourse(Course course)L93

    Drop a course while its enrolment is still Open.

    • Must be enrolled in it
    • Closed courses print “Drop period is closed.”
    • Removes the course from the student only (see Known issues)
  • void viewAvailableCourses(List<Course> listOfCourses)L179

    List every course that is open and not full.

  • void viewEnrolledCourses()L130

    Print each enrolled course with code, credit hours and schedule.

3 methods

Credit hours#

  • int totalCreditLimit()L61

    Sum the credit hours of every enrolled course.

  • int enrolledInHours()L119

    Total credit hours across enrolled courses, null-safe.

  • void viewCreditLimit(List<Student> listOfStudents)L108

    Print the credit limit and the hours still available.

    • Loops over the student list and breaks after the first one (see Known issues)

1 method

Grades#

  • void viewGrades(List<Assessment> listOfGrades)L144

    Sub-menu: one exam score, all grades in a course, or the course average.

    • Uses a temporary Assessment object to reach the grade methods

2 methods

Profile#

  • void updateStudentPersonalInfo(String newPassword, String newEmail, String newPhoneNumber, String newAddress)L195

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

    • Values are validated beforehand by Helpers.updateStudentProfile
  • String toString()L206

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

Getters & setters#

getEnrolledCoursesList, getCreditLimit and setCreditLimit (which rejects negative values).

3 accessors
  • List<Course> getEnrolledCoursesList()L24
  • int getCreditLimit()L28
  • void setCreditLimit(int creditLimit)L32

Known issues#

Source

Student.java209 lines · from the project report
1package schoolcourseenrolmentsystem;
2
3import java.util.*;
4
5public class Student extends User<Student> {
6 // Attributes
7 private int creditLimit;
8 private List<Course> enrolledCourses;
9
10 public Student() { }
11 // Constructor
12 public Student(String name, String id, String password, String email, String phoneNumber, String address, int
13 creditLimit, List<Course> enrolledCourses) {
14 super(name, id, password, email, phoneNumber, Role.STUDENT, address);
15 setCreditLimit(creditLimit);
16 // If the list is null, create a new one. Fixes NullPointerException.
17 if (enrolledCourses != null) {
18 this.enrolledCourses = enrolledCourses;
19 } else {
20 this.enrolledCourses = new ArrayList<>();
21 }
22 }
23 //======================= Setters and Getters =======================
24 public List<Course> getEnrolledCoursesList() {
25 return enrolledCourses;
26 }
28 public int getCreditLimit() {
29 return creditLimit;
30 }
32 public void setCreditLimit(int creditLimit) {
33 if (creditLimit < 0) {
34 System.out.println("Must be greater than 0");
35 return;
36 }
37 this.creditLimit = creditLimit;
38 }
40 //======================= Methods =======================
42 @Override
43 public Student login(List<Student> listOfStudents, String id, String password) {
44 for (Student student : listOfStudents) {
45 if (student.getId().equals(id) && student.getPassword().equals(password)) {
46 System.out.println("Student " + student.getName() + " logged in.");
48 return student;
49 }
50 }
51 System.out.println("No student record was found with the ID and password provided.");
52 return null;
53 }
55 @Override
56 public String logout(Student student) {
57 return ("Student " + student.getName() + " logged out.");
58 }
60 // Count the hours of the courses already enrolled
61 public int totalCreditLimit() {
62 int total = 0;
63 // This loop is to count how many courses and their toal hours
64 for (Course course : enrolledCourses) {
65 total += course.getCreditHours();
66 }
67 return total;
68 }
70 public void enroll_In_Course(Course course) {
71 // 1. make sure credit limit is not excited and that the capacity is avaliable
72 if (enrolledCourses.contains(course)) {
73 System.out.printf("\nYou are already enrolled in (%s) (%s).", course.getCourseName(),
74 course.getCourseCode());
75 } else if (course.isFull()) {
76 System.out.println("\nCannot enroll in " + course.getCourseName() + " - course is full.");
77 } // Check if the enrollment status is open or not
78 else if (course.getEnrollmentStatus() == Course.EnrollmentStatusEnum.Closed) {
79 System.out.println("\nEnrollment status is closed.");
80 } // Previously enrolled hours + new hours
81 else if (totalCreditLimit() + course.getCreditHours() > creditLimit) {
82 System.out.println("\nCannot enroll in " + course.getCourseName() + " - credit limit exceeded.");
83 } else {
84 /// Adding the current student to the course's list of enrolled students.
86 course.getEnrolledStudents().add(this);
87 /// Adding the course to the student's list of enrolled courses.
88 this.enrolledCourses.add(course);
89 System.out.println("\n" + getName() + " enrolled successfully");
90 }
91 }
93 public void dropCourse(Course course) {
94 if (!enrolledCourses.contains(course)) {
95 System.out.printf("%s is not enrolled in (%s) (%s).", getName(), course.getCourseName(),
96 course.getCourseCode());
97 } else if (enrolledCourses.contains(course)
98 && course.getEnrollmentStatus() == Course.EnrollmentStatusEnum.Closed) {
99 System.out.println("Drop period is closed.");
100 } else if (enrolledCourses.contains(course)
101 && course.getEnrollmentStatus() == Course.EnrollmentStatusEnum.Open) {
102 this.enrolledCourses.remove(course);
103 System.out.printf("%s has successfully dropped (%s) (%s).", getName(), course.getCourseName(),
104 course.getCourseCode());
105 }
106 }
108 public void viewCreditLimit(List<Student> listOfStudents) {
109 for (Student student : listOfStudents) {
110 System.out.println("Your credit limit is: " + student.getCreditLimit());
112 // (condition) ? (value if true) : (value if false);
113 int enrolledHours = (student.getEnrolledCoursesList() != null) ? student.enrolledInHours() : 0;
114 System.out.println("Remaining credit limit is: " + (student.getCreditLimit() - enrolledHours) + " hours.");
115 break;
116 }
117 }
119 public int enrolledInHours() {
120 int total = 0;
121 if (enrolledCourses != null) {
122 for (Course course : enrolledCourses) {
124 total += course.getCreditHours();
125 }
126 }
127 return total;
128 }
130 public void viewEnrolledCourses() {
131 if (enrolledCourses.isEmpty()) {
132 System.out.println(getName() + " is not enrolled in any courses.");
133 } else {
134 System.out.println(getName() + " is enrolled in: ");
135 int index = 1;
136 for (Course course : enrolledCourses) {
137 System.out.printf(index + "- (%s) (%s) (%s). The schedule is: %s.", course.getCourseName(),
138 course.getCourseCode(), course.getCreditHours(), course.getSchedule());
139 index++;
140 }
141 }
142 }
144 public void viewGrades(List<Assessment> listOfGrades) {
145 System.out.println("\nWhat would you like to view?\n1. View a specific exam score.\n2. View all grades in a
146 course.\n3. View total average in a course.");
148 int choice = Helpers.getSafeIntInput("\nOption: ");
149 Assessment temp = new Assessment(null, null, null, 0.0, null);
151 if (choice == 1) {
152 temp.viewSpecificExamScore(listOfGrades, this);
153 } else if (choice == 2) {
154 temp.viewAllGradesForCourse(listOfGrades, this);
155 } else if (choice == 3) {
156 List<Course> courses = this.getEnrolledCoursesList();
158 if (courses.isEmpty()) {
159 System.out.println("\nYou are not enrolled in any courses.");
160 return;
162 }
163 System.out.println("\nSelect a course:");
164 for (int i = 0; i < courses.size(); i++) {
165 System.out.printf((i + 1) + ".%s ( %s )", courses.get(i).getCourseName(), courses.get(i).getCourseCode());
166 }
167 int courseChoice = Helpers.getSafeIntInput("\nOption: ");
168 if (courseChoice < 1 || courseChoice > courses.size()) {
169 System.out.println("Invalid course selection.");
170 return;
171 }
172 String courseCode = courses.get(courseChoice - 1).getCourseCode();
173 temp.viewTotalAverageGrade(listOfGrades, this.getId(), courseCode);
174 } else {
175 System.out.println("Invalid choice.");
176 }
177 }
179 public void viewAvailableCourses(List<Course> listOfCourses) {
180 boolean avaliableCourses = false;
181 System.out.println("\nAvailable courses are: ");
182 for (Course course : listOfCourses) {
183 if (course.isFull() == false && course.getEnrollmentStatus() == Course.EnrollmentStatusEnum.Open) {
184 avaliableCourses = true;
185 System.out.println("- (Course name: " + course.getCourseName() + ") (Course code: " +
186 course.getCourseCode() + ") (Course schedule: " + course.getSchedule() + ") (Course instructor: " +
187 course.getInstructor()+ ") (Course capacity: " + course.getCapacity() + ").\n");
188 }
189 }
190 if (!avaliableCourses) {
191 System.out.println("No courses are avaliable.");
192 }
193 }
195 public void updateStudentPersonalInfo(String newPassword, String newEmail, String newPhoneNumber,
196 String newAddress) {
197 setPassword(newPassword);
198 setEmail(newEmail);
200 setPhoneNumber(newPhoneNumber);
201 setAddress(newAddress);
202 System.out.println("\nStudent " + getName() + "'s information updated.");
203 }
205 @Override
206 public String toString() {
207 return String.format("-ID: %s Name: %s", this.getId(), this.getName());
208 }