Description
The purpose of this challenge is to implement FIFO and Priority Queues together. This challenge simulates a course enrollment system.
How the enrollment system works
- The system will present a menu that allows students to enroll into a class, drop a class, find a class, or view the current list of enrolled students, as well as the list of waiting students.
- When enrolling into a class, students will automatically be added to the Enrolled list – as long as it still has capacity and is not yet full.
- Once the Enrolled list fills up, enrolling into a class will use a Waiting list. But this Waiting list is a Priority Queue. A Priority Queue uses a FIFO order for students with equal GPAs, but a priority order for those with higher GPAs. This means that the waiting list will always have students added in decreasing order of GPAs – the first person in the list will have the highest GPA, the last person on the list will have the lowest GPA of those in the Waiting list. New additions to the Waiting list will be placed accordingly, moving lower GPAs down. This continues until the Waiting list is also full. Once both lists are full, additional enrollments will be rejected.
- Students can drop out of the system. First, the system will ask for the ID of the student to be dropped. Once that student is found, they will be removed from whichever list they are found. If they are found in the Enrolled list, then everyone listed after this person will “roll up.” If there are students in the Waiting list, they will also roll up and into the Enrolled list. During a drop operation, the Enrolled and Waiting list work as one unified FIFO queue.
- The system also allows the user to search for a student by ID. The system will either report that the student was found in the Enrolled list, the Waiting list, or was not found.
Requirements
- For this program, do not include a reference to the global std namespace. Instead, scope the necessary references using std:: where appropriate. For example, std::string, std::cout and so on.
- Create the following enums below. Notice that the enums use two different styles (classic C/POSIX style and modern C++). You normally wouldn’t mix styles in a program, but we’re doing it to learn both.
enum EnrollResult { ENROLL_SUCCESS, ENROLL_WAITLISTED, ENROLL_FULL, ENROLL_INVALID_GPA }; enum class DropResult { NotFound, FromMain, FromWaitlist }; - Add one more enum called FindResult. Use classic C-style for this one. Add enum values : FIND_NOT_FOUND, FIND_IN_MAIN, FIND_IN_WAITLIST
- Add the struct below
struct Student { std::string name; std::string id; double gpa; }; - Add the class below
class Course { private: Student* Enrolled; Student* Waiting; int enrolledCount; int waitingCount; int lastIndex; // last affected index public: static int EnrollMax; static int WaitingMax; Course(); ~Course(); EnrollResult enroll(std::string name, std::string id, double gpa); DropResult drop(const std::string& id); FindResult find(const std::string& id); int getLastIndex() const { return lastIndex; } friend void showEnrolled(const Course& c); friend void showWaiting(const Course& c); }; // initialize statics here Course::Course() { Enrolled = new Student[EnrollMax]; Waiting = new Student[WaitingMax]; enrolledCount = 0; waitingCount = 0; lastIndex = -1; } Course::~Course() { delete[] Enrolled; delete[] Waiting; } - Notice the static properties EnrollMax and WaitingMax in the class immediately after the class definition. Define and set both of these values to 3. Understand that because these two properties are defined as static for the Course class, any and all objects instantiated from this class will all share the same values of EnrollMax and WaitingMax.
- Notice that the class contains prototypes for functions that will be written outside the class body using the name resolution operator (::). Notice that some of these functions return enum types.
- Complete the enroll() function according to these requirements:
- Make sure to review the prototype to understand the function’s signature (the parameter list and return types)
- Make sure to use the enrolledCount, waitingCount and lastIndex variables appropriately.
- The enrolledCount represents how many students are in the Enrolled array
- The waitingCount represents how many students are in the Waiting array
- The lastIndex represents the index/location at which the last enroll(), drop() or find() operation occured for a student.
- The function will return ENROLL_INVALID_GPA if the gpa value is invalid. Valid gpa values should be between 0.0 and 4.0.
- If enrolledCount is less than EnrollMax, add a new student into the next location in the Enrolled array. Add all data points for a student into the array element. If successful, return ENROLL_SUCCESS
- If not successful in step 3, check if waitingCount < WaitingMax. Add the new student info into the Waiting array using Priority Queue rules using the gpa to determine the order — higher GPAs take priority. If successfully aded to the Waiting array, return ENROLL_WAITLISTED. Set lastIndex to the position at which the student was added to the Waiting array.
- Make sure to update the enrolledCount and waitingCount variables.
- Otherwise, if not added to either of Enrolled or Waiting array, return ENROLL_FULL and set lastIndex to -1.
- Complete the drop() function according to these requirements:
- Make sure to review the prototype to understand the function’s signature (the parameter list and return types)
- Make sure to use the enrolledCount, waitingCount and lastIndex variables appropriately.
- The enrolledCount represents how many students are in the Enrolled array
- The waitingCount represents how many students are in the Waiting array
- The lastIndex represents the index/location at which the last enroll(), drop() or find() operation occured for a student.
- First attempt to find the student by id in the Enrolled array. If found in the Enrolled array, make sure to “roll up” elements below if the student is found here. Make sure to “roll up” from the Waiting array as well. Make sure to update the enrolledCount and waitingCount variables.
- If successful, return DropResult::FromMain. Note that this function uses modern enum class values.
- If not found in Enrolled array, check the Waiting array. Make sure to “roll up” elements below if student is found here. If succesful, return DropResult::FromWaitList. Set lastIndex to the position at which the student was removed from the Waiting array.
- If not found in either array, set lastIndex to -1 and return DropResult::NotFound
- Complete the find() function according to these requirements:
- Make sure to review the prototype to understand the function’s signature (the parameter list and return types)
- Make sure to use the enrolledCount, waitingCount and lastIndex variables appropriately.
- The enrolledCount represents how many students are in the Enrolled array
- The waitingCount represents how many students are in the Waiting array
- The lastIndex represents the index/location at which the last enroll(), drop() or find() operation occured for a student.
- Search for the student using id as the search value in both Enrolled and Waiting arrays. This function will set lastIndex to the index location where the student is found, or -1 if not found.
- This function will return the appropriate FindResult enum value. You should be able to determine which ones to use.
- Add the following global (non-class) function for showEnrolled(). Write a similar function for showWaiting().
void showEnrolled(const Course& c) { if (c.enrolledCount == 0) { std::cout << "Enrolled list is empty.\n"; return; } std::cout << "\nEnrolled Students:\n"; for (int i = 0; i < c.enrolledCount; ++i) { std::cout << i + 1 << ". " << c.Enrolled[i].name << " (" << c.Enrolled[i].id << ") GPA: " << std::fixed << std::setprecision(2) << c.Enrolled[i].gpa << "\n"; } } - Add the following main(). You should be able to copy as-is. Be sure to read the code in main to understand it.
// You'll need to include <iomanip> int main() { Course course; int choice; std::string name, id; double gpa; while (true) { std::cout << "\n===== Course Enrollment Menu =====\n"; std::cout << "1. Enroll Student\n"; std::cout << "2. Drop Student\n"; std::cout << "3. Show Enrolled List\n"; std::cout << "4. Show Waiting List\n"; std::cout << "5. Find Student by ID\n"; std::cout << "6. Exit\n"; std::cout << "==================================\n"; std::cout << "Enter choice: "; std::cin >> choice; switch (choice) { case 1: std::cout << "Enter name: "; std::cin >> name; std::cout << "Enter ID: "; std::cin >> id; std::cout << "Enter GPA: "; std::cin >> gpa; { EnrollResult r = course.enroll(name, id, gpa); int pos = course.getLastIndex() + 1; switch (r) { case ENROLL_SUCCESS: std::cout << "Enrolled successfully - " << name << " (" << id << ") at position " << pos << " in Enrolled list.\n"; break; case ENROLL_WAITLISTED: std::cout << "Added to waiting list - " << name << " (" << id << ") at position " << pos << ".\n"; break; case ENROLL_FULL: std::cout << "Enrollment failed both lists full.\n"; break; case ENROLL_INVALID_GPA: std::cout << "Invalid GPA entered.\n"; break; } } break; case 2: std::cout << "Enter ID to drop: "; std::cin >> id; { DropResult d = course.drop(id); switch (d) { case DropResult::FromMain: std::cout << "Dropped from Enrolled list successfully.\n"; break; case DropResult::FromWaitlist: std::cout << "Dropped from Waiting list successfully.\n"; break; case DropResult::NotFound: std::cout << "ID not found in any list.\n"; break; } } break; case 3: showEnrolled(course); break; case 4: showWaiting(course); break; case 5: std::cout << "Enter ID to find: "; std::cin >> id; { FindResult f = course.find(id); int pos = course.getLastIndex() + 1; switch (f) { case FIND_IN_MAIN: std::cout << "Found in Enrolled list at position " << pos << ".\n"; break; case FIND_IN_WAITLIST: std::cout << "Found in Waiting list at position " << pos << ".\n"; break; case FIND_NOT_FOUND: std::cout << "Student not found.\n"; break; } } break; case 6: std::cout << "Exiting...\n"; return 0; default: std::cout << "Invalid choice. Try again.\n"; } } }
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL00080
Print Requirements
