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

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

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#

enum EnrollmentStatusEnumL8

ClosedOpen
TypeNameModifiersLine
StringcourseNameprivateL14
StringcourseCodeprivateL14
StringscheduleprivateL14
StringdescriptionprivateL14
EnrollmentStatusEnumenrollmentStatusprivateL15
InstructorinstructorprivateL16
intcapacityprivateL17
List<Assessment>gradesprivateL18
intcreditHoursprivateL19
List<Student>enrolledStudentsprivateL22

Constructors#

  • Course(String courseName, String courseCode, String schedule, String description, EnrollmentStatusEnum enrollmentStatus, Instructor instructor, int capacity, List<Assessment> listOfGrades, int creditHours)ParameterisedL25

    Full constructor: sets every field through its setter and always starts with an empty enrolledStudents list.

  • Course()DefaultL43

    Empty course, for file loading and temporary objects.

1 method

Capacity#

Used before every enrolment to prevent over-enrolment.

  • boolean isFull()L179

    True when the number of enrolled students has reached capacity.

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

Course.java183 lines · from the project report
1package schoolcourseenrolmentsystem;
2
3import java.util.*;
4
5public class Course {
6 // Similar to final but with 2+ options.
7 // Enum to represent course enrollment status
8 public enum EnrollmentStatusEnum {
9 Closed,
10 Open
11 }
13 // Attributes
14 private String courseName, courseCode, schedule, description;
15 private EnrollmentStatusEnum enrollmentStatus;
16 private Instructor instructor;
17 private int capacity;
18 private List<Assessment> grades;
19 private int creditHours;
21 // Fixes java.lang.StackOverflowError
22 private List<Student> enrolledStudents; // List of students currently enrolled
24 // Constructor
25 public Course(String courseName, String courseCode, String schedule, String description,
26 EnrollmentStatusEnum enrollmentStatus, Instructor instructor, int capacity, List<Assessment> listOfGrades,
27 int creditHours) {
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<>();
40 }
42 // Default (empty) constructor
43 public Course() {
44 }
46 // ======================= Setters and Getters =======================
47 public void setEnrolledStudents(List<Student> enrolledStudentList) {
48 this.enrolledStudents = enrolledStudentList;
49 }
51 public EnrollmentStatusEnum getEnrollmentStatus() {
52 return enrollmentStatus;
53 }
55 public void setEnrollmentStatus(EnrollmentStatusEnum enrollmentStatus) {
56 this.enrollmentStatus = enrollmentStatus;
57 }
59 public String getCourseName() {
60 return courseName;
61 }
63 public void setCourseName(String courseName) {
64 if (courseName == null || courseName.isEmpty()) {
65 System.out.println("Course name cannot be null or empty.");
66 return;
67 } else {
68 this.courseName = courseName;
70 }
71 }
73 public int getCreditHours() {
74 return creditHours;
75 }
77 public void setCreditHours(int creditHours) {
78 if (creditHours > 0) {
79 this.creditHours = creditHours;
80 } else {
81 System.out.println("Credit hours can not be <= 0");
82 return;
83 }
84 }
86 public List<Student> getEnrolledStudents() {
87 return enrolledStudents;
88 }
90 public String getCourseCode() {
91 return courseCode;
92 }
94 public void setCourseCode(String courseCode) {
95 if (courseCode == null || courseCode.isEmpty()) {
96 System.out.println("Course code cannot be null or empty.");
97 return;
98 } else {
99 this.courseCode = courseCode;
100 }
101 }
103 public String getSchedule() {
104 return schedule;
105 }
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;
111 } else {
112 this.schedule = schedule;
113 }
114 }
116 public String getDescription() {
117 return description;
118 }
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;
124 } else {
125 this.description = description;
126 }
127 }
129 public Instructor getInstructor() {
130 return instructor;
131 }
133 public void setInstructor(Instructor instructor) {
134 if (instructor == null) {
135 this.instructor = null;
136 return;
137 }
138 this.instructor = instructor;
139 }
141 public int getCapacity() {
142 return capacity;
144 }
146 public void setCapacity(int capacity) {
147 if (capacity > 0) {
148 this.capacity = capacity;
149 } else {
150 System.out.println("Error: Capacity must be > 0.");
151 return;
152 }
153 }
155 public List<Assessment> getGrades() {
156 return grades;
157 }
159 public List<Assessment> setAssessment(List<Assessment> listOfGrades) {
160 if (listOfGrades == null) {
161 this.grades = null;
162 return null;
163 } else {
164 this.grades = listOfGrades;
165 return listOfGrades;
166 }
167 }
169 public void setGrades(List<Assessment> listOfGrades) {
170 if (listOfGrades == null) {
171 this.grades = null;
172 } else {
173 this.grades = listOfGrades;
174 }
175 }
177 // ======================= Methods =======================
178 // Check if the course has reached its capacity
179 public boolean isFull() {
181 return enrolledStudents != null && this.getEnrolledStudents().size() >= capacity;
182 }