Java Program for Course Enrollment Management
Fifth Assignment: Collections
Question: Design a Java program to manage course enrollment details using an ArrayList. Implement the following functionalities using built-in methods (such as add(), set(), size(), remove(), clear(), contains(), isEmpty()) and user input:
a. Include course_id, course name, students enrolled, student_name, and student_id.
Use built-in methods to add this entry to the ArrayList. Display a success message after adding the entry.
CODE:
import java.util.ArrayList;
import java.util.Scanner;
class Course {
private String courseId;
private String courseName;
private ArrayList<String> enrolledStudents;
public Course(String courseId, String courseName) {
this.courseId = courseId;
this.courseName = courseName;
this.enrolledStudents = new ArrayList<>();
}
public String getCourseId() {
return courseId;
}
public String getCourseName() {
return courseName;
}
public ArrayList<String> getEnrolledStudents() {
return enrolledStudents;
}
public void enrollStudent(String studentName, String studentId) {
enrolledStudents.add(studentName + " (" + studentId + ")");
}
public boolean isEnrolled(String studentName, String studentId) {
String student = studentName + " (" + studentId + ")";
return enrolledStudents.contains(student);
}
public boolean isEmpty() {
return enrolledStudents.isEmpty();
}
public void clearEnrollments() {
enrolledStudents.clear();
}
}
public class courseEnroll {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create ArrayList to store courses
ArrayList<Course> courses = new ArrayList<>();
// Main menu
while (true) {
System.out.println("\nMenu:");
System.out.println("1. Add Course");
System.out.println("2. Enroll Student");
System.out.println("3. Display Courses and Enrollments");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addCourse(scanner, courses);
break;
case 2:
enrollStudent(scanner, courses);
break;
case 3:
displayCoursesAndEnrollments(courses);
break;
case 4:
System.out.println("Exiting program.");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void addCourse(Scanner scanner, ArrayList<Course> courses) {
System.out.print("Enter Course ID: ");
String courseId = scanner.nextLine();
System.out.print("Enter Course Name: ");
String courseName = scanner.nextLine();
Course course = new Course(courseId, courseName);
courses.add(course);
System.out.println("Course added successfully.");
}
private static void enrollStudent(Scanner scanner, ArrayList<Course> courses) {
System.out.print("Enter Course ID: ");
String courseId = scanner.nextLine();
Course course = findCourse(courseId, courses);
if (course != null) {
System.out.print("Enter Student Name: ");
String studentName = scanner.nextLine();
System.out.print("Enter Student ID: ");
String studentId = scanner.nextLine();
course.enrollStudent(studentName, studentId);
System.out.println("Student enrolled successfully.");
} else {
System.out.println("Course not found.");
}
}
private static void displayCoursesAndEnrollments(ArrayList<Course> courses) {
if (courses.isEmpty()) {
System.out.println("No courses available.");
return;
}
for (Course course : courses) {
System.out.println("Course ID: " + course.getCourseId());
System.out.println("Course Name: " + course.getCourseName());
ArrayList<String> enrolledStudents = course.getEnrolledStudents();
if (!enrolledStudents.isEmpty()) {
System.out.println("Enrolled Students:");
for (String student : enrolledStudents) {
System.out.println("- " + student);
}
} else {
System.out.println("No students enrolled yet.");
}
}
}
private static Course findCourse(String courseId, ArrayList<Course> courses) {
for (Course course : courses) {
if (course.getCourseId().equals(courseId)) {
return course;
}
}
return null;
}
}