Lesson 4
Managing Student Enrollments Using TypeScript Sets
Introduction

Welcome to today's lesson! We'll be exploring a practical application of TypeScript's type safety features 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. With TypeScript's robust type checking and the use of Set collections, you can efficiently manage these tasks, prevent duplicate enrollments, and ensure data integrity.

By the end of this session, you'll be well-versed in using Set objects 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, course): This method adds a student to a course. If the student is already enrolled, it does nothing.
  • unenroll(student, 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(student, course): This method checks if a student is enrolled in a course. It returns true if the student is enrolled and false otherwise.
  • listStudents(course): This method returns an array of all students enrolled in a given course. If no students are enrolled, it returns an empty array.

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 with appropriate type annotations:

TypeScript
1class EnrollmentSystem { 2 enrollments: { [course: string]: Set<string> }; 3 4 constructor() { 5 this.enrollments = {}; 6 } 7}

This code initializes an EnrollmentSystem class with an enrollments property. This property is a TypeScript object, where each course name maps to a Set of student names.

Step 2: Implement 'enroll' Method

Next, we implement the enroll method, using type annotations for parameters:

TypeScript
1enroll(student: string, course: string): void { 2 if (!this.enrollments[course]) { 3 this.enrollments[course] = new Set<string>(); 4 } 5 this.enrollments[course].add(student); 6}

The enroll method adds a student to a specified course. It takes two string parameters: student and course. The method checks if the course already exists in this.enrollments. If not, it initializes a new Set for the course. It then adds the student to the set, leveraging the Set to prevent duplicate enrollments.

Step 3: Implement 'unenroll' Method

Let's move on to the unenroll method, adding return type and parameter annotations:

TypeScript
1unenroll(student: string, course: string): boolean { 2 if (this.enrollments[course] && this.enrollments[course].has(student)) { 3 this.enrollments[course].delete(student); 4 if (this.enrollments[course].size === 0) { // If the set is empty, remove the course 5 delete this.enrollments[course]; 6 } 7 return true; 8 } 9 return false; 10} 11

The unenroll 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 set becomes empty after removal, it deletes the course from the object. 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 with type annotations:

TypeScript
1isEnrolled(student: string, course: string): boolean { 2 return this.enrollments[course] ? this.enrollments[course].has(student) : false; 3}

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:

TypeScript
1listStudents(course: string): string[] { 2 if (this.enrollments[course]) { 3 return Array.from(this.enrollments[course]); 4 } 5 return []; 6}

The listStudents method returns an array of students enrolled in the given course. If the course is not in the enrollments object, it returns an empty array.

When using Array.from(<set>), the elements in the set are returned in the order of their insertion. This means that the array created by Array.from will maintain the order in which students were added to the set.

Final Code and Usage

Here is the complete implementation of the EnrollmentSystem class, which includes all the methods we've discussed. This example demonstrates how to manage student enrollments using TypeScript's Set collections.

TypeScript
1class EnrollmentSystem { 2 enrollments: { [course: string]: Set<string> }; 3 4 constructor() { 5 this.enrollments = {}; 6 } 7 8 enroll(student: string, course: string): void { 9 if (!this.enrollments[course]) { 10 this.enrollments[course] = new Set<string>(); 11 } 12 this.enrollments[course].add(student); 13 } 14 15 unenroll(student: string, course: string): boolean { 16 if (this.enrollments[course] && this.enrollments[course].has(student)) { 17 this.enrollments[course].delete(student); 18 if (this.enrollments[course].size === 0) { 19 delete this.enrollments[course]; 20 } 21 return true; 22 } 23 return false; 24 } 25 26 isEnrolled(student: string, course: string): boolean { 27 return this.enrollments[course] ? this.enrollments[course].has(student) : false; 28 } 29 30 listStudents(course: string): string[] { 31 if (this.enrollments[course]) { 32 return Array.from(this.enrollments[course]); 33 } 34 return []; 35 } 36} 37 38// Example usage: 39const es = new EnrollmentSystem(); 40es.enroll("Alice", "Math101"); 41es.enroll("Bob", "Math101"); 42console.log(es.enrollments); // Output: { Math101: Set(2) { 'Alice', 'Bob' } } 43 44console.log(es.isEnrolled("Alice", "Math101")); // Output: true 45console.log(es.isEnrolled("Bob", "Math101")); // Output: true 46 47es.unenroll("Alice", "Math101"); 48console.log(es.enrollments); // Output: { Math101: Set(1) { 'Bob' } } 49 50console.log(es.listStudents("Math101")); // Output: ['Bob'] 51console.log(es.listStudents("Physics101")); // Output: [] 52 53es.unenroll("Bob", "Math101"); 54console.log(es.enrollments); // Output: {}
Lesson Summary

In today's lesson, we learned how to manage student enrollments using TypeScript's type annotations and Set 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 TypeScript's benefits, particularly in maintaining data integrity and reliability. TypeScript helps manage collections like Set efficiently with strong type safety, minimizing potential errors and improving code maintainability.

I encourage you to practice similar challenges to deepen your understanding of TypeScript's capabilities. 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.