Lesson 3

Managing Student Enrollments Using C# Dictionary and List

Managing Student Enrollments Using C# Dictionary and List

In this lesson, we'll learn how to manage student enrollments using C#'s Dictionary and List 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.

Introducing Methods 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 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 (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 returns true if the student is enrolled and false otherwise.
  • ListStudents(string course): This method returns a List<string> of all students enrolled in a given course. If no students are enrolled, it returns an empty List<string>.

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:

C#
1using System; 2using System.Collections.Generic; 3 4public class EnrollmentSystem 5{ 6 private Dictionary<string, List<string>> enrollments; 7 8 public EnrollmentSystem() 9 { 10 enrollments = new Dictionary<string, List<string>>(); 11 } 12} 13 14class Program 15{ 16 static void Main() 17 { 18 EnrollmentSystem es = new EnrollmentSystem(); 19 Console.WriteLine("EnrollmentSystem initialized."); 20 } 21}

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

Step 2: Implement Enroll Method

Next, we implement the Enroll method:

C#
1using System; 2using System.Collections.Generic; 3 4public class EnrollmentSystem 5{ 6 private Dictionary<string, List<string>> enrollments; 7 8 public EnrollmentSystem() 9 { 10 enrollments = new Dictionary<string, List<string>>(); 11 } 12 13 public void Enroll(string student, string course) 14 { 15 if (!enrollments.ContainsKey(course)) 16 { 17 enrollments[course] = new List<string>(); 18 } 19 if (!enrollments[course].Contains(student)) 20 { 21 enrollments[course].Add(student); 22 } 23 } 24} 25 26class Program 27{ 28 static void Main() 29 { 30 EnrollmentSystem es = new EnrollmentSystem(); 31 es.Enroll("Alice", "Math101"); 32 Console.WriteLine(string.Join(", ", es.enrollments["Math101"])); // Output: Alice 33 } 34}

In the Enroll method:

  1. We check if the course exists in the Dictionary. If it doesn't, we initialize a new List for that course.
  2. We then add the student to the list if they are not already enrolled.
Step 3: Implement Unenroll Method

Let's move on to the Unenroll method:

C#
1using System; 2using System.Collections.Generic; 3 4public class EnrollmentSystem 5{ 6 private Dictionary<string, List<string>> enrollments; 7 8 public EnrollmentSystem() 9 { 10 enrollments = new Dictionary<string, List<string>>(); 11 } 12 13 public void Enroll(string student, string course) 14 { 15 if (!enrollments.ContainsKey(course)) 16 { 17 enrollments[course] = new List<string>(); 18 } 19 if (!enrollments[course].Contains(student)) 20 { 21 enrollments[course].Add(student); 22 } 23 } 24 25 public bool Unenroll(string student, string course) 26 { 27 if (enrollments.ContainsKey(course) && enrollments[course].Contains(student)) 28 { 29 enrollments[course].Remove(student); 30 if (enrollments[course].Count == 0) 31 { 32 enrollments.Remove(course); 33 } 34 return true; 35 } 36 return false; 37 } 38} 39 40class Program 41{ 42 static void Main() 43 { 44 EnrollmentSystem es = new EnrollmentSystem(); 45 es.Enroll("Alice", "Math101"); 46 es.Enroll("Bob", "Math101"); 47 Console.WriteLine(string.Join(", ", es.enrollments["Math101"])); // Output: Alice, Bob 48 es.Unenroll("Alice", "Math101"); 49 Console.WriteLine(string.Join(", ", es.enrollments["Math101"])); // Output: Bob 50 es.Unenroll("Bob", "Math101"); 51 Console.WriteLine(es.enrollments.ContainsKey("Math101")); // Output: False 52 } 53}

This method first checks whether the course exists in the enrollments and whether the student is enrolled in that course. If they are, it removes the student from the course. If the course list becomes empty after removal, it deletes the course from the Dictionary. 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:

C#
1using System; 2using System.Collections.Generic; 3 4public class EnrollmentSystem 5{ 6 private Dictionary<string, List<string>> enrollments; 7 8 public EnrollmentSystem() 9 { 10 enrollments = new Dictionary<string, List<string>>(); 11 } 12 13 public void Enroll(string student, string course) 14 { 15 if (!enrollments.ContainsKey(course)) 16 { 17 enrollments[course] = new List<string>(); 18 } 19 if (!enrollments[course].Contains(student)) 20 { 21 enrollments[course].Add(student); 22 } 23 } 24 25 public bool IsEnrolled(string student, string course) 26 { 27 return enrollments.ContainsKey(course) && enrollments[course].Contains(student); 28 } 29} 30 31class Program 32{ 33 static void Main() 34 { 35 EnrollmentSystem es = new EnrollmentSystem(); 36 es.Enroll("Alice", "Math101"); 37 Console.WriteLine(es.IsEnrolled("Alice", "Math101")); // Output: True 38 Console.WriteLine(es.IsEnrolled("Bob", "Math101")); // Output: False 39 } 40}

This method checks whether the specified course exists in the enrollments and whether the student is enrolled in that course. If both conditions are met, it returns true; otherwise, it returns false.

Step 5: Implement ListStudents Method

Finally, let's implement the ListStudents method:

C#
1using System; 2using System.Collections.Generic; 3 4public class EnrollmentSystem 5{ 6 private Dictionary<string, List<string>> enrollments; 7 8 public EnrollmentSystem() 9 { 10 enrollments = new Dictionary<string, List<string>>(); 11 } 12 13 public void Enroll(string student, string course) 14 { 15 if (!enrollments.ContainsKey(course)) 16 { 17 enrollments[course] = new List<string>(); 18 } 19 if (!enrollments[course].Contains(student)) 20 { 21 enrollments[course].Add(student); 22 } 23 } 24 25 public bool IsEnrolled(string student, string course) 26 { 27 return enrollments.ContainsKey(course) && enrollments[course].Contains(student); 28 } 29 30 public List<string> ListStudents(string course) 31 { 32 if (enrollments.ContainsKey(course)) 33 { 34 return new List<string>(enrollments[course]); 35 } 36 return new List<string>(); 37 } 38} 39 40class Program 41{ 42 static void Main() 43 { 44 EnrollmentSystem es = new EnrollmentSystem(); 45 es.Enroll("Alice", "Math101"); 46 es.Enroll("Bob", "Math101"); 47 Console.WriteLine(string.Join(", ", es.ListStudents("Math101"))); // Output: Alice, Bob 48 Console.WriteLine(string.Join(", ", es.ListStudents("Physics101"))); // Output: 49 } 50}

The ListStudents method returns a List<string> of students enrolled in the given course. If the course is not in the Dictionary, it returns an empty List<string>.

Lesson Summary

In today's lesson, we learned how to manage student enrollments using C# Dictionary and List objects. 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 dictionaries and lists in C#.

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.