A gym wants to manage membership registrations and class bookings. Write a Java program to handle the registration process and update class availability accordingly.
a) Write a class called GymClass to represent the details of a gym class with the following private attributes:
classIdclassNameclassTimespotsAvailable
b) Implement constructor overloading in GymClass:
- One constructor should accept all four parameters:
classId,className,classTime, andspotsAvailable. - Another constructor should accept only
classIdandclassName, and assign default values:classTime = "To be announced"andspotsAvailable = 0.
c) Implement method overloading for a method called bookClass():
-
One version should accept the number of spots a customer wants to book and display appropriate messages:
-
If
spotsAvailableis less than the requested number, display:"Not enough spots available. Please select fewer spots or another class." -
Otherwise:
- Deduct the requested number of spots from
spotsAvailable. - Display:
"Booking successful!"
- Deduct the requested number of spots from
-
-
Another version should take no parameters and book 1 spot by default.
d) Implement a method called displayClassDetails() to display the complete details of the class, including how many spots are still available.
e) Write another Java class called GymApp to perform the following tasks:
I. Input details of two gym classes from the user — one using the full constructor and one using the overloaded constructor.
II. Ask how many spots a customer wants to book for each class, and process the booking using both versions of the bookClass() method.
III. After all booking operations, display the details of both classes using the displayClassDetails() method.
🌟 Expected Output
Enter details for first class:
Class ID: C101
Class Name: Yoga
Class Time: 10:00 AM
Spots Available: 5
Enter details for second class:
Class ID: C102
Class Name: Zumba
How many spots to book in Class 1? 3
Booking successful!
Booking 1 spot in Class 2 by default:
Not enough spots available. Please select fewer spots or another class.
Final Class Details:
Class ID: C101
Class Name: Yoga
Class Time: 10:00 AM
Spots Available: 2
------------------------------
Class ID: C102
Class Name: Zumba
Class Time: To be announced
Spots Available: 0
------------------------------