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!
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 returnstrue
if 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(student, course)
: This method checks if a student is enrolled in a course. It returnstrue
if the student is enrolled andfalse
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.
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:
TypeScript1class 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.
Next, we implement the enroll
method, using type annotations for parameters:
TypeScript1enroll(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.
Let's move on to the unenroll
method, adding return type and parameter annotations:
TypeScript1unenroll(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.
Next, let's implement the isEnrolled
method with type annotations:
TypeScript1isEnrolled(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
.
Finally, let's implement the listStudents
method:
TypeScript1listStudents(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.
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.
TypeScript1class 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: {}
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!