In this lesson, we'll learn how to manage student enrollments using C#'s Dictionary and HashSet collections. These data structures allow efficient data organization and access, which are essential for building an enrollment system. We'll implement methods to handle enrolling and unenrolling students, checking enrollment status, and listing students in a course. This practical approach will enhance your understanding of these advanced structures. Now, let's dive into the methods we need to implement.
Here are the methods we need to implement in our enrollment system:
Enroll(string student, string course): This method adds a student to a course. If the student is already enrolled, it does nothing.Unenroll(string student, string course): This method removes a student from a course. It returnstrueif the student was enrolled and has now been removed. Otherwise, it returnsfalse. If, after unenrolling the student, the course becomes empty (no one is enrolled there), remove the course as well.IsEnrolled(string student, string course): This method checks if a student is enrolled in a course. It returnstrueif the student is enrolled andfalseotherwise.ListStudents(string course): This method returns aList<string>of all students enrolled in a given course. If no students are enrolled, it returns an emptyList<string>.
Let's look at how to implement each of these methods step by step.
We'll start by defining our class and then add each method one by one.
First, we define our EnrollmentSystem class:
This code initializes an EnrollmentSystem class with a Dictionary named enrollments that maps courses to sets of students.
