C class · schoolcourseenrolmentsystem
Course.java
Course stores everything about one course: its details, enrolment status, capacity, credit hours, assigned instructor and the list of enrolled students. Its setters guard against invalid values.
public class Course
Nested enum EnrollmentStatusEnum { Closed, Open }
References Instructor, Student list, Assessment list
Guards Name and code non-empty, capacity and hours > 0
Saved to courses.txt
Overview
Source 183 lines
Responsibilities# Hold course details and the enrolled-student roster Track Open or Closed enrolment status Reject empty names and codes and non-positive numbers Answer whether the course is full Fields & enums# e enum EnrollmentStatusEnumL8
ClosedOpen
Type Name Modifiers Line f String courseNameprivate L14 f String courseCodeprivate L14 f String scheduleprivate L14 f String descriptionprivate L14 f EnrollmentStatusEnum enrollmentStatusprivate L15 f Instructor instructorprivate L16 f int capacityprivate L17 f List <Assessment >gradesprivate L18 f int creditHoursprivate L19 f List <Student >enrolledStudentsprivate L22
Constructors# c Course (String courseName, String courseCode, String schedule, String description, EnrollmentStatusEnum enrollmentStatus, Instructor instructor, int capacity, List <Assessment > listOfGrades, int creditHours)Parameterised L25 Full constructor: sets every field through its setter and always starts with an empty enrolledStudents list.
Empty course, for file loading and temporary objects.
1 method
Capacity# Used before every enrolment to prevent over-enrolment.
Getters & setters# Getters and setters for every field. setCourseName and setCourseCode reject empty values, setCapacity and setCreditHours reject values ≤ 0, and schedule and description may be null so an instructor can fill them in later. Invalid values print an error and leave the field unchanged.
21 accessors void setEnrolledStudents (List <Student > enrolledStudentList)L47 EnrollmentStatusEnum getEnrollmentStatus ()L51 void setEnrollmentStatus (EnrollmentStatusEnum enrollmentStatus)L55 String getCourseName ()L59 void setCourseName (String courseName)L63 int getCreditHours ()L73 void setCreditHours (int creditHours)L77 List <Student > getEnrolledStudents ()L86 String getCourseCode ()L90 void setCourseCode (String courseCode)L94 String getSchedule ()L103 void setSchedule (String schedule)L107 String getDescription ()L116 void setDescription (String description)L120 Instructor getInstructor ()L129 void setInstructor (Instructor instructor)L133 int getCapacity ()L141 void setCapacity (int capacity)L146 List <Assessment > getGrades ()L155 List <Assessment > setAssessment (List <Assessment > listOfGrades)L159 void setGrades (List <Assessment > listOfGrades)L169 Source C Course.java 183 lines · from the project report Copy
1 package schoolcourseenrolmentsystem;6 // Similar to final but with 2+ options. 7 // Enum to represent course enrollment status 8 public enum EnrollmentStatusEnum { 14 private String courseName, courseCode, schedule, description; 15 private EnrollmentStatusEnum enrollmentStatus; 16 private Instructor instructor; 18 private List <Assessment > grades; 19 private int creditHours; 21 // Fixes java.lang.StackOverflowError 22 private List <Student > enrolledStudents; // List of students currently enrolled 25 public Course (String courseName, String courseCode, String schedule, String description, 26 EnrollmentStatusEnum enrollmentStatus, Instructor instructor, int capacity, List <Assessment > listOfGrades, 28 setCourseName (courseName); 29 setCourseCode (courseCode); 30 setSchedule (schedule); 32 setDescription (description); 33 setEnrollmentStatus (enrollmentStatus); 34 setInstructor (instructor); 35 setCapacity (capacity); 36 setAssessment (listOfGrades); 37 setCreditHours (creditHours); 38 // Important: initialize enrolledStudents list to avoid null pointer exceptions 39 this .enrolledStudents = new ArrayList <>(); 42 // Default (empty) constructor 46 // ======================= Setters and Getters ======================= 47 public void setEnrolledStudents (List <Student > enrolledStudentList) { 48 this .enrolledStudents = enrolledStudentList; 51 public EnrollmentStatusEnum getEnrollmentStatus () { 52 return enrollmentStatus; 55 public void setEnrollmentStatus (EnrollmentStatusEnum enrollmentStatus) { 56 this .enrollmentStatus = enrollmentStatus; 59 public String getCourseName () { 63 public void setCourseName (String courseName) { 64 if (courseName == null || courseName.isEmpty ()) { 65 System .out.println ("Course name cannot be null or empty." ); 68 this .courseName = courseName; 73 public int getCreditHours () { 77 public void setCreditHours (int creditHours) { 78 if (creditHours > 0 ) { 79 this .creditHours = creditHours; 81 System .out.println ("Credit hours can not be <= 0" ); 86 public List <Student > getEnrolledStudents () { 87 return enrolledStudents; 90 public String getCourseCode () { 94 public void setCourseCode (String courseCode) { 95 if (courseCode == null || courseCode.isEmpty ()) { 96 System .out.println ("Course code cannot be null or empty." ); 99 this .courseCode = courseCode; 103 public String getSchedule () { 107 public void setSchedule (String schedule) { 108 // Schedule is allowed to be null because instructors can update it later. 109 if (schedule == null || schedule.isEmpty ()) { 110 this .schedule = null ; 112 this .schedule = schedule; 116 public String getDescription () { 120 public void setDescription (String description) { 121 // Description is allowed to be null because instructors can update it later. 122 if (description == null || description.isEmpty ()) { 123 this .description = null ; 125 this .description = description; 129 public Instructor getInstructor () { 133 public void setInstructor (Instructor instructor) { 134 if (instructor == null ) { 135 this .instructor = null ; 138 this .instructor = instructor; 141 public int getCapacity () { 146 public void setCapacity (int capacity) { 148 this .capacity = capacity; 150 System .out.println ("Error: Capacity must be > 0." ); 155 public List <Assessment > getGrades () { 159 public List <Assessment > setAssessment (List <Assessment > listOfGrades) { 160 if (listOfGrades == null ) { 164 this .grades = listOfGrades; 169 public void setGrades (List <Assessment > listOfGrades) { 170 if (listOfGrades == null ) { 173 this .grades = listOfGrades; 177 // ======================= Methods ======================= 178 // Check if the course has reached its capacity 179 public boolean isFull () { 181 return enrolledStudents != null && this .getEnrolledStudents ().size () >= capacity;