Saturday, December 10, 2016

Programming Challenge 5.9 - Hotel Occupancy

/* Hotel Occupancy - This program calculates the occupancy rate for a hotel.

The program starts by asking the user how many floors the hotel has. A loop
then iterates once for each floor. In each iteration, the loop asks the user
for the number of rooms on the floor and how many of them are occupied.

After all iterations, the program displays:
* How many rooms the hotel has
* How many of them are occupied
* How many are unoccupied
* The percentage of rooms that are occupied

The percentage is calculated by dividing the number of rooms occupied by the
number of rooms.

Note: It is traditional that most hotels do not have a thirteenth floor. The
loop of this program will skip the entire thirteenth iteration. */

#include "Utility.h"

int main()
{
    /* Variables: Number of Floors (counter variable), Number of Rooms,
    Total number of floors, Total number of rooms */
    int numFloors = 1,
        numRooms = 0,
        floorsTotal = 0,
        roomsTotal = 0;

    /* Variables: Number of Rooms Occupied, Number of Rooms unoccupied,
    Total number of rooms occupied, Total number of rooms unoccupied,
    Percentage of Occupied Rooms */
    double roomsOccupied = 0,
           roomsUnoccupied = 0,
           roomsOccupiedTotal = 0,
           roomsUnoccupiedTotal = 0,
           pctRoomsOccupiedTotal = 0;

    /* Display: Program title, Information */
    cout << "\t\tHotel Occupancy Plan\n\n"
        << "You can access a report about the current occupancy rate\n"
        << "if you provide the following information.\n"
        << "* Number of Floors\n"
        << "* Number of Rooms on each floor.\n"
        << "\nAn automated report will be created once the program has\n"
        << "finished calculating the following information:\n"
        << "* How many rooms the hotel has\n"
        << "* How many rooms are occupied\n"
        << "* How many rooms are unoccupied\n"
        << "* The total percentage of rooms occupied right now.\n\n";

    /* Input: Ask the user for the number of floors */
    cout << "Please enter the number of floors: ";
    cin >> floorsTotal;

    /* While loop::Check: While the user enters 13 for the total number of
    floors, the input must be repeated */
    while (floorsTotal == 13)
    {
        /* Display: Information */
        cout << "Your entry was invalid. No hotel that we know of, has 13"
            << " floors.\n";

        /* Ask the user again */
        cout << "Please enter the number of floors: ";
        cin >> floorsTotal;
    }

    /* For loop: As long as number of floors is lower than the total number of
       floors the user entered, this loop will iterate */
    for (numFloors; numFloors < floorsTotal; numFloors++)
    {
        /* Nested While loop: While the number of floors is lower than number of
        total floors, the loop will iterate once for each floor */
        while (numFloors <= floorsTotal)
        {
            /* If Statement::Check: If the number of floors equals 13, the floor
               number will be omitted */
            if (numFloors == 13)
            {
                numFloors += 1;
                continue;
            }

            /* Input: Ask the user for the number of rooms on each floor */
            cout << "\nPlease enter the number of rooms on floor " << numFloors << ": ";
            cin >> numRooms;

            /* Nested While loop::Input Validation: While number of rooms is lower
            than 1, this while loop will iterate */
            while (numRooms <= 0)
            {
                /* Display: Information about the input error */
                cout << "\nThere seems to be a mistake in your entry. The number\n"
                    << "of rooms can not be lower than 1.\n";

                /* Ask the user again */
                cout << "\nPlease enter the number of rooms on floor " << numFloors << ": ";
                cin >> numRooms;
            }

            cout << "How many rooms on floor " << numFloors << " are occupied? ";
            cin >> roomsOccupied;

            /* Nested While loop::Input Validation: While number of rooms occupied
            is greater than the number of rooms on the floor, this while loop
            will iterate */
            while (roomsOccupied > numRooms)
            {
                /* Display: Information about the input error */
                cout << "\nThere seems to be a mistake in your entry. The number\n"
                    << "of rooms occupied can not be greater than the number of\n"
                    << "rooms available on any given floor.\n";

                /* Ask the user again */
                cout << "\nHow many rooms on floor " << numFloors << " are occupied? ";
                cin >> roomsOccupied;
            }

            /* Calculations */
            roomsUnoccupied = numRooms - roomsOccupied;
            roomsTotal += numRooms;
            roomsOccupiedTotal += roomsOccupied;
            roomsUnoccupiedTotal += roomsUnoccupied;
            pctRoomsOccupiedTotal = (roomsOccupiedTotal / roomsTotal) * 100;

            /* Display: Information about each floor during input, number of floors
               increments only as long as the condition in the outer loop is true */
            cout << "\nOn floor " << (numFloors++) << " are " << numRooms
                << " rooms." << roomsOccupied << " is/are currently occupied"
                << " and " << roomsUnoccupied << " is/are currently unoccupied\n";
        }
  
        /* Display: The formatted information */
        cout << "\nThis hotel has a capacity of " << roomsTotal << " rooms\n";
        cout << "\nOccupied Rooms" << "\t\t\t" << "Unoccupied Rooms\n";
        cout << "-----------------------------------------------------------\n";
        cout << roomsOccupiedTotal << setw(4) << right << "\t\t\t\t"
            << roomsUnoccupiedTotal << setw(4) << right;
        cout << "\n-----------------------------------------------------------\n";
        cout << fixed << showpoint << setprecision(2);
        cout << "The current occupancy rate is: " << pctRoomsOccupiedTotal << "%\n\n";
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment