Question: Course Registration - Revisited import java.util.Scanner; public class ClassSection { private String courseNumber; private int sectionNumber; private int registrationNumber; private String day; private int startTime;

Course Registration - Revisited import java.util.Scanner; public class ClassSection { private String courseNumber; private int sectionNumber; private int registrationNumber; private String day; private int startTime; private String[] enrolledStudents = new String[20]; private int enrolledCount =0; public ClassSection(String courseNumber, int sectionNumber, int registrationNumber, String day){ this.courseNumber = courseNumber; this.sectionNumber = sectionNumber; this.registrationNumber = registrationNumber; this.day = day; this.startTime =9; } public int getSectionNumber(){ return sectionNumber; } public int getRegistrationNumber(){ return registrationNumber; } public String getDay(){ return day; } public int getStartTime(){ return startTime; } public void setStartTime(int startTime){ if (startTime >=0 && startTime <=23) this.startTime = startTime; else System.out.println("Invalid time."); } public boolean addStudent(String studentId){ if (getSeatsAvailable()>0 && !studentIsEnrolled(studentId)){ enrolledStudents[enrolledCount++]= studentId; return true; } return false; } public int getEnrolledNumber(){ return enrolledCount; } public int getSeatsAvailable(){ return enrolledStudents.length - enrolledCount; } public boolean studentIsEnrolled(String studentId){ for (int i =0; i < enrolledCount; i++){ if (enrolledStudents[i].equals(studentId)) return true; } return false; } @Override public String toString(){ return courseNumber +"-"+ sectionNumber +"-"+ registrationNumber +"-"+ day +""+ startTime +":00- Seats: "+ getSeatsAvailable(); }} public class EnrollmentManager { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); ClassSection[] sections ={ new ClassSection("CSCI 111",1,12345,"M"), new ClassSection("CSCI 111",2,23456,"M"), new ClassSection("CSCI 111",3,13456,"T"), new ClassSection("ENG 101",1,20987,"M"), new ClassSection("ENG 101",2,30099,"W")}; String studentId; while (true){ System.out.print("Enter your J-Number or zero to exit: "); studentId = scanner.nextLine(); if (studentId.equals("0")){ System.out.println("Goodbye."); break; } System.out.println("Course - Section - Registration - Meets - Available Seats"); for (ClassSection section : sections){ System.out.println(section); } while (true){ System.out.print("Select a section or enter zero to return: "); String regNumber = scanner.nextLine(); if (regNumber.equals("0")) break; boolean registered = false; for (ClassSection section : sections){ if (Integer.toString(section.getRegistrationNumber()).equals(regNumber)){ if (section.addStudent(studentId)){ System.out.println("Successfully enrolled in section "+ section.getSectionNumber()+"."); registered = true; break; } else { System.out.println("Error: Already enrolled or no seats available."); registered = true; break; }}} if (!registered) System.out.println("Invalid registration number."); }} scanner.close(); }}``` Steps Overview 1. Step 0: Modify the `registrationNumber` in `ClassSection` to an integer and optionally store the number of enrolled students directly in the object. 2. Step 1: Add a new Java class to populate the database with courses and students. 3. Step 2: Modify the program to pull course data from the database instead of using hardcoded values. 4. Step 3: Save the enrolled student list as a string in the database. 5. Step 4: Load the enrolled students' list from the database at startup and use `addStudent` to enroll them. 6. Step 5: Verify student IDs by querying the database before enrollment. 7. Step 6: Celebrate! The program can now persist and load data from the database. Database Interaction - Populate Data: Insert five students and five course sections into the database. - Dynamic Data Fetch: Use database queries to load course data and students dynamically, and update the database with the student enrollments.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!