C 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
Overview
Source 209 lines
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# Type Name Modifiers Line f int creditLimitprivate L7 f List <Course >enrolledCoursesprivate L8
Constructors# Empty student, used as the throwaway instance for Helpers.login.
c Student (String name, String id, String password, String email, String phoneNumber, String address, int creditLimit, List <Course > enrolledCourses)Parameterised L12 Sets inherited fields through super(), the credit limit, and the course list. A null list becomes a new ArrayList to avoid a NullPointerException.
4 methods
Enrolment# m 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 m 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) m void viewAvailableCourses (List <Course > listOfCourses)L179 List every course that is open and not full.
m void viewEnrolledCourses ()L130 Print each enrolled course with code, credit hours and schedule.
3 methods
Credit hours# m int totalCreditLimit ()L61 Sum the credit hours of every enrolled course.
m int enrolledInHours ()L119 Total credit hours across enrolled courses, null-safe.
m 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) 2 methods
Profile# m 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 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 Source C Student.java 209 lines · from the project report Copy
1 package schoolcourseenrolmentsystem;5 public class Student extends User <Student > {7 private int creditLimit; 8 private List <Course > enrolledCourses; 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; 20 this .enrolledCourses = new ArrayList <>(); 23 //======================= Setters and Getters ======================= 24 public List <Course > getEnrolledCoursesList () { 25 return enrolledCourses; 28 public int getCreditLimit () { 32 public void setCreditLimit (int creditLimit) { 33 if (creditLimit < 0 ) { 34 System .out.println ("Must be greater than 0" ); 37 this .creditLimit = creditLimit; 40 //======================= Methods ======================= 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." ); 51 System .out.println ("No student record was found with the ID and password provided." ); 56 public String logout (Student student) { 57 return ("Student " + student.getName () + " logged out." ); 60 // Count the hours of the courses already enrolled 61 public int totalCreditLimit () { 63 // This loop is to count how many courses and their toal hours 64 for (Course course : enrolledCourses) { 65 total += course.getCreditHours (); 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." ); 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" ); 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 ()); 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." ); 119 public int enrolledInHours () { 121 if (enrolledCourses != null ) { 122 for (Course course : enrolledCourses) { 124 total += course.getCreditHours (); 130 public void viewEnrolledCourses () { 131 if (enrolledCourses.isEmpty ()) { 132 System .out.println (getName () + " is not enrolled in any courses." ); 134 System .out.println (getName () + " is enrolled in: " ); 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 ()); 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 ); 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." ); 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 ()); 167 int courseChoice = Helpers .getSafeIntInput ("\nOption: " ); 168 if (courseChoice < 1 || courseChoice > courses.size ()) { 169 System .out.println ("Invalid course selection." ); 172 String courseCode = courses.get (courseChoice - 1 ).getCourseCode (); 173 temp.viewTotalAverageGrade (listOfGrades, this .getId (), courseCode); 175 System .out.println ("Invalid choice." ); 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" ); 190 if (!avaliableCourses) { 191 System .out.println ("No courses are avaliable." ); 195 public void updateStudentPersonalInfo (String newPassword, String newEmail, String newPhoneNumber, 197 setPassword (newPassword); 200 setPhoneNumber (newPhoneNumber); 201 setAddress (newAddress); 202 System .out.println ("\nStudent " + getName () + "'s information updated." ); 206 public String toString () { 207 return String .format ("-ID: %s Name: %s" , this .getId (), this .getName ());