In this lesson, we'll learn how to manage student enrollments using Java's HashMap
and ArrayList
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 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 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(String student, String course)
: This method checks if a student is enrolled in a course. It returnstrue
if the student is enrolled andfalse
otherwise.listStudents(String course)
: This method returns anArrayList<String>
of all students enrolled in a given course. If no students are enrolled, it returns an emptyArrayList<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:
Java1import java.util.ArrayList; 2import java.util.HashMap; 3 4class EnrollmentSystem { 5 private HashMap<String, ArrayList<String>> enrollments; 6 7 public EnrollmentSystem() { 8 enrollments = new HashMap<>(); 9 } 10} 11 12public class Solution { 13 public static void main(String[] args) { 14 EnrollmentSystem es = new EnrollmentSystem(); 15 System.out.println("EnrollmentSystem initialized."); 16 } 17}
This code initializes an EnrollmentSystem
class with a HashMap
named enrollments
that maps courses to lists of students.
Next, we implement the enroll
method:
Java1import java.util.ArrayList; 2import java.util.HashMap; 3 4class EnrollmentSystem { 5 private HashMap<String, ArrayList<String>> enrollments; 6 7 public EnrollmentSystem() { 8 enrollments = new HashMap<>(); 9 } 10 11 public void enroll(String student, String course) { 12 enrollments.putIfAbsent(course, new ArrayList<>()); 13 ArrayList<String> students = enrollments.get(course); 14 if (!students.contains(student)) { 15 students.add(student); 16 } 17 } 18 19 public ArrayList<String> getStudents(String course) { 20 return enrollments.getOrDefault(course, new ArrayList<>()); 21 } 22} 23 24public class Solution { 25 public static void main(String[] args) { 26 EnrollmentSystem es = new EnrollmentSystem(); 27 es.enroll("Alice", "Math101"); 28 System.out.println(String.join(", ", es.getStudents("Math101"))); // Output: Alice 29 } 30}
In the enroll
method:
- We check if the
course
exists in theHashMap
. If it doesn't, we initialize a newArrayList
for that course. - We then add the
student
to the list if they are not already enrolled.
Let's move on to the unenroll
method:
Java1import java.util.ArrayList; 2import java.util.HashMap; 3 4class EnrollmentSystem { 5 private HashMap<String, ArrayList<String>> enrollments; 6 7 public EnrollmentSystem() { 8 enrollments = new HashMap<>(); 9 } 10 11 public void enroll(String student, String course) { 12 enrollments.putIfAbsent(course, new ArrayList<>()); 13 ArrayList<String> students = enrollments.get(course); 14 if (!students.contains(student)) { 15 students.add(student); 16 } 17 } 18 19 public boolean unenroll(String student, String course) { 20 if (enrollments.containsKey(course)) { 21 ArrayList<String> students = enrollments.get(course); 22 if (students.remove(student)) { 23 if (students.isEmpty()) { 24 enrollments.remove(course); 25 } 26 return true; 27 } 28 } 29 return false; 30 } 31 32 public ArrayList<String> getStudents(String course) { 33 return enrollments.getOrDefault(course, new ArrayList<>()); 34 } 35 36 public boolean containsCourse(String course) { 37 return enrollments.containsKey(course); 38 } 39} 40 41public class Solution { 42 public static void main(String[] args) { 43 EnrollmentSystem es = new EnrollmentSystem(); 44 es.enroll("Alice", "Math101"); 45 es.enroll("Bob", "Math101"); 46 System.out.println(String.join(", ", es.getStudents("Math101"))); // Output: Alice, Bob 47 es.unenroll("Alice", "Math101"); 48 System.out.println(String.join(", ", es.getStudents("Math101"))); // Output: Bob 49 es.unenroll("Bob", "Math101"); 50 System.out.println(es.containsCourse("Math101")); // Output: false 51 } 52}
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 HashMap
. The method returns true
if the student was successfully unenrolled and false
otherwise.
Next, let's implement the isEnrolled
method:
Java1import java.util.ArrayList; 2import java.util.HashMap; 3 4class EnrollmentSystem { 5 private HashMap<String, ArrayList<String>> enrollments; 6 7 public EnrollmentSystem() { 8 enrollments = new HashMap<>(); 9 } 10 11 public void enroll(String student, String course) { 12 enrollments.putIfAbsent(course, new ArrayList<>()); 13 ArrayList<String> students = enrollments.get(course); 14 if (!students.contains(student)) { 15 students.add(student); 16 } 17 } 18 19 public boolean isEnrolled(String student, String course) { 20 if (enrollments.containsKey(course)) { 21 ArrayList<String> students = enrollments.get(course); 22 return students.contains(student); 23 } 24 return false; 25 } 26} 27 28public class Solution { 29 public static void main(String[] args) { 30 EnrollmentSystem es = new EnrollmentSystem(); 31 es.enroll("Alice", "Math101"); 32 System.out.println(es.isEnrolled("Alice", "Math101")); // Output: true 33 System.out.println(es.isEnrolled("Bob", "Math101")); // Output: false 34 } 35}
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:
Java1import java.util.ArrayList; 2import java.util.HashMap; 3 4class EnrollmentSystem { 5 private HashMap<String, ArrayList<String>> enrollments; 6 7 public EnrollmentSystem() { 8 enrollments = new HashMap<>(); 9 } 10 11 public void enroll(String student, String course) { 12 enrollments.putIfAbsent(course, new ArrayList<>()); 13 ArrayList<String> students = enrollments.get(course); 14 if (!students.contains(student)) { 15 students.add(student); 16 } 17 } 18 19 public ArrayList<String> listStudents(String course) { 20 return enrollments.getOrDefault(course, new ArrayList<>()); 21 } 22} 23 24public class Solution { 25 public static void main(String[] args) { 26 EnrollmentSystem es = new EnrollmentSystem(); 27 es.enroll("Alice", "Math101"); 28 es.enroll("Bob", "Math101"); 29 System.out.println(String.join(", ", es.listStudents("Math101"))); // Output: Alice, Bob 30 System.out.println(String.join(", ", es.listStudents("Physics101"))); // Output: 31 } 32}
The listStudents
method returns an ArrayList<String>
of students enrolled in the given course. If the course is not in the HashMap
, it returns an empty ArrayList<String>
.
In today's lesson, we learned how to manage student enrollments using Java HashMap
and ArrayList
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 hash maps and array lists in Java.
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!