Lesson 3
Managing Course Enrollments with Kotlin Sets
Introduction

Welcome to today's lesson! We'll explore a practical application of Kotlin's set capabilities by managing student enrollments for various courses. Imagine you're running an online course platform and need to handle enrollments, checks, and listings of students in different courses. Sets are perfect for this kind of problem as they don’t allow duplicates, ensuring that a student can't enroll in the same course more than once!

By the end of this session, you'll be well-versed in using sets for such tasks. Let’s dive in!

Introducing Methods to Implement

Here are the methods we need to implement in our enrollment system:

  • enroll(student: String, course: String): This method adds a student to a course. If the student is already enrolled, it does nothing.
  • unenroll(student: String, course: String): Boolean: This method removes a student from a course. It returns true if the student was enrolled and has now been removed. Otherwise, it returns false. If, after unenrolling the student, the course becomes empty, remove the course as well.
  • isEnrolled(student: String, course: String): Boolean: This method checks if a student is enrolled in a course. It returns true if the student is enrolled and false otherwise.
  • listStudents(course: String): List<String>: This method returns a list of all students enrolled in a given course. If no students are enrolled, it returns an empty list.

Let's look at how to implement each of these methods step-by-step.

Step 1: Define the Class

We'll start by defining our class and then add each method one by one.

First, we define our EnrollmentSystem class:

Kotlin
1class EnrollmentSystem { 2 val enrollments: MutableMap<String, MutableSet<String>> = mutableMapOf() 3}

This code initializes an EnrollmentSystem class with a map named enrollments that maps courses to sets of students.

Step 2: Implement `enroll` Method

Next, we implement the enroll method:

Kotlin
1class EnrollmentSystem { 2 /* Other methods and properties omitted for brevity */ 3 4 fun enroll(student: String, course: String) { 5 enrollments.computeIfAbsent(course) { mutableSetOf() }.add(student) 6 } 7} 8 9// Example usage: 10fun main() { 11 val es = EnrollmentSystem() 12 es.enroll("Alice", "Math101") 13 println(es.enrollments) // Output: {Math101=[Alice]} 14}

Here, the enroll method checks if the course exists in the enrollments map. If it doesn't, it initializes a new set for that course using computeIfAbsent. Then, it adds the student to the set of students for that course.

Step 3: Implement `unenroll` Method

Let's move on to the unenroll method:

Kotlin
1class EnrollmentSystem { 2 /* Other methods and properties omitted for brevity */ 3 4 fun unenroll(student: String, course: String): Boolean { 5 val students = enrollments[course] ?: return false 6 val wasEnrolled = students.remove(student) 7 if (students.isEmpty()) { 8 enrollments.remove(course) 9 } 10 return wasEnrolled 11 } 12} 13 14// Example usage: 15fun main() { 16 val es = EnrollmentSystem() 17 es.enroll("Alice", "Math101") 18 es.enroll("Bob", "Math101") 19 println(es.enrollments) // Output: {Math101=[Alice, Bob]} 20 es.unenroll("Alice", "Math101") 21 println(es.enrollments) // Output: {Math101=[Bob]} 22 es.unenroll("Bob", "Math101") 23 println(es.enrollments) // Output: {} 24}

This method first checks whether the course and student exist in the enrollments. If they do, it removes the student from the course. If the course set becomes empty after removal, it deletes the course from the map. The method returns true if the student was successfully unenrolled and false otherwise.

Step 4: Implement `isEnrolled` Method

Next, let's implement the isEnrolled method:

Kotlin
1class EnrollmentSystem { 2 /* Other methods and properties omitted for brevity */ 3 4 fun isEnrolled(student: String, course: String): Boolean { 5 return enrollments[course]?.contains(student) == true 6 } 7} 8 9// Example usage: 10fun main() { 11 val es = EnrollmentSystem() 12 es.enroll("Alice", "Math101") 13 println(es.isEnrolled("Alice", "Math101")) // Output: true 14 println(es.isEnrolled("Bob", "Math101")) // Output: false 15}

This method checks whether both the course and the student exist in the enrollments map, returning true if they do and false otherwise.

Step 5: Implement `listStudents` Method

Finally, let's implement the listStudents method:

Kotlin
1class EnrollmentSystem { 2 /* Other methods and properties omitted for brevity */ 3 4 fun listStudents(course: String): List<String> { 5 return enrollments[course]?.toList() ?: emptyList() 6 } 7} 8 9// Example usage: 10fun main() { 11 val es = EnrollmentSystem() 12 es.enroll("Alice", "Math101") 13 es.enroll("Bob", "Math101") 14 println(es.listStudents("Math101")) // Output: [Alice, Bob] 15 println(es.listStudents("Physics101")) // Output: [] 16}

This method returns a list of students enrolled in the given course. If the course is not in the enrollments map, it returns an empty list.

Lesson Summary

In today's lesson, we learned how to manage student enrollments using Kotlin's set capabilities within a map structure. We implemented methods to enroll and unenroll students, check enrollments, and list students in a course. This task provided a practical way to reinforce your understanding of sets and collection management in Kotlin.

I encourage you to move on to the practice to undertake similar challenges to deepen your understanding. Keep experimenting and honing your skills. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.