Challenge 80

Description

The purpose of this challenge is to implement FIFO and Priority Queues together. This challenge simulates a course enrollment system.

 Requirements

  1. For this program, do not use a global std namespace. Instead, scope the necessary references using std:: where appropriate. For example, std::string, std::cout and so on.
  2. 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
    };
    
  3. 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
  4. Add the struct below
    struct Student 
    {
        std::string name;
        std::string id;
        double gpa;
    };
    
  5. 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;
    }
    
  6. Notice the static properties EnrollMax and WaitingMax in the class. Immediately after the class definition. Define and set both of these values to 3.
  7. 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.
  8. Complete the enroll() function according to these requirements:
    1. Make sure to review the prototype to understand the function’s signature (the parameter list and return types)
    2. Make sure to use the enrolledCount and waitingCount variables.
    3. The function will return ENROLL_INVALID_GPA if the gpa value is invalid.
    4. 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
    5. 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.
    6. Make sure to update the enrolledCount and waitingCount variables.
    7. Otherwise, if not added to either of Enrolled or Waiting array, return ENROLL_FULL
  9. Complete the drop() function according to these requirements:
    1. Make sure to review the prototype to understand the function’s signature (the parameter list and return types)
    2. Make sure to use the enrolledCount and waitingCount variables.
    3. 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.
    4. If successful, return DropResult::FromMain. Note that this function uses modern enum class values.
    5. 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.
    6. If not found in either array, set lastIndex to -1 and return DropResult::NotFound
  10. Complete the find() function according to these requirements:
    1. Make sure to review the prototype to understand the function’s signature (the parameter list and return types)
    2. 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.
    3. This function will return the appropriate FindResult enum value. You should be able to determine which ones to use.
  11. Add the following 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";
        }
    }
    
  12. Add the following main(). You should be able to copy as-is. Be sure to read the code in main to understand it.
    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