Beginner 30 min read

Model a Course Roster

By the end of this lesson, you'll be able to:

  • Write the CourseRoster class holding a List<Student>
  • Add a method to enroll a student into the roster
  • Add a method to report how many students are currently enrolled

Prerequisites: "Model a Student"

Holding a list of students

public class CourseRoster {
    private String courseName;
    private List<Student> enrolledStudents = new List<Student>();

    public CourseRoster(String courseName) {
        this.courseName = courseName;
    }

    public void enroll(Student student) {
        enrolledStudents.add(student);
    }

    public Integer getEnrollmentCount() {
        return enrolledStudents.size();
    }
}
  1. public class CourseRoster { — the class.
  2. private String courseName; — a protected field.
  3. private List<Student> enrolledStudents = new List<Student>(); — this is composition (Module 11) in action: CourseRoster has a list of Student objects rather than being one itself, starting empty the moment a CourseRoster is created.
  4. (blank line)
  5. public CourseRoster(String courseName) { — the constructor.
  6. this.courseName = courseName; — sets the course name.
  7. } — closes the constructor.
  8. (blank line)
  9. public void enroll(Student student) { — grows the roster one student at a time.
  10. enrolledStudents.add(student); — the roster owns and protects its own list, exactly like Student protects its own fields.
  11. } — closes the method.
  12. (blank line)
  13. public Integer getEnrollmentCount() { — reports the current size.
  14. return enrolledStudents.size(); — returns it.
  15. } — closes the method.
  16. } — closes the class.

Putting Student and CourseRoster together

CourseRoster apexBasics = new CourseRoster('Apex Basics');

Student alice = new Student('S001', 'Alice Nkosi', 'alice@example.com');
Student ben = new Student('S002', 'Ben Dlamini', 'ben@example.com');

apexBasics.enroll(alice);
apexBasics.enroll(ben);

System.debug(apexBasics.getEnrollmentCount()); // 2
  1. CourseRoster apexBasics = new CourseRoster('Apex Basics'); — creates the roster.
  2. (blank line)
  3. Student alice = new Student('S001', 'Alice Nkosi', 'alice@example.com'); — creates a student.
  4. Student ben = new Student('S002', 'Ben Dlamini', 'ben@example.com'); — creates another.
  5. (blank line)
  6. apexBasics.enroll(alice); — enrolls the first student.
  7. apexBasics.enroll(ben); — enrolls the second.
  8. (blank line)
  9. System.debug(apexBasics.getEnrollmentCount()); — logs 2 — the natural fit, since a roster isn't a kind of student, just a holder of them.

Exercise

Add a method listStudentDescriptions() to CourseRoster that returns a List<String> of every enrolled student's describe() output.

Show hint

Loop over enrolledStudents, calling describe() on each, adding to a new List<String>.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Model a Course Roster Quiz

1. What kind of relationship does CourseRoster have with Student?

Log in to submit the quiz and save your score.

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

CourseRoster models the second piece from the design sketch — a class that holds a growing collection of Student objects and reports on them.