Add Business Rules
By the end of this lesson, you'll be able to:
- Enforce a maximum roster size before allowing enrollment
- Prevent the same student from being enrolled twice
- Explain why business rules belong inside the class that owns the data
Prerequisites: "Model a Course Roster"
Enforcing a maximum size
public class CourseRoster {
private String courseName;
private Integer maxSize;
private List<Student> enrolledStudents = new List<Student>();
public CourseRoster(String courseName, Integer maxSize) {
this.courseName = courseName;
this.maxSize = maxSize;
}
public Boolean enroll(Student student) {
if (enrolledStudents.size() >= maxSize) {
System.debug('Roster full — cannot enroll ' + student.getName());
return false;
}
enrolledStudents.add(student);
return true;
}
}
public class CourseRoster {— the class.private String courseName;— unchanged.private Integer maxSize;— a new field: the enrollment cap.private List<Student> enrolledStudents = new List<Student>();— unchanged.- (blank line)
public CourseRoster(String courseName, Integer maxSize) {— the constructor now takes the cap too.this.courseName = courseName;— sets the name.this.maxSize = maxSize;— sets the cap.}— closes the constructor.- (blank line)
public Boolean enroll(Student student) {— now returns aBooleanreporting whether it actually succeeded.if (enrolledStudents.size() >= maxSize) {— this is Module 6's if-statement pattern doing real business work.System.debug('Roster full — cannot enroll ' + student.getName());— a clear reason logged.return false;— reports failure, so calling code can react instead of blindly assuming every enrollment worked.}— closes theif.enrolledStudents.add(student);— only reached if there was room.return true;— reports success.}— closes the method.}— closes the class.
Preventing duplicate enrollment
public Boolean enroll(Student student) {
for (Student existing : enrolledStudents) {
if (existing.getStudentId() == student.getStudentId()) {
System.debug(student.getName() + ' is already enrolled.');
return false;
}
}
if (enrolledStudents.size() >= maxSize) {
System.debug('Roster full — cannot enroll ' + student.getName());
return false;
}
enrolledStudents.add(student);
return true;
}
public Boolean enroll(Student student) {— the method, now with a duplicate check added first.for (Student existing : enrolledStudents) {— checks every already-enrolled student.if (existing.getStudentId() == student.getStudentId()) {— matches by id.System.debug(student.getName() + ' is already enrolled.');— a clear, specific message.return false;— rejects the duplicate immediately.}— closes theif.}— closes the loop.- (blank line)
if (enrolledStudents.size() >= maxSize) {— the capacity check, run after the duplicate check.System.debug('Roster full — cannot enroll ' + student.getName());— a different, generic message.return false;— rejects for capacity.}— closes theif.- (blank line)
enrolledStudents.add(student);— only reached if neither check rejected the student.return true;— reports success.}— closes the method.
Checking for an existing enrollment before checking capacity means a genuinely duplicate enrollment gets a clear, specific message rather than a generic "roster full" — the order these checks run in actually matters for how helpful the feedback is.
Why these rules live inside CourseRoster
It would be possible to write these checks as separate, standalone code wherever enroll gets called instead — but then every caller would need to remember to repeat them, and any caller that forgot would silently create an invalid roster. Putting the rules inside CourseRoster.enroll() itself (the same encapsulation principle from Module 11) means the rule is enforced everywhere, automatically, with no way to bypass it.
Exercise
Add a getRemainingSeats() method to CourseRoster returning maxSize minus the current enrollment count.
Show hint
return maxSize - enrolledStudents.size();
Add Business Rules Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Real rosters have real limits — this lesson adds business rules (a maximum size, no duplicate enrollments) directly inside CourseRoster, where the data they protect actually lives.