Saturday, November 26, 2016

Programming Challenge 4.24 - Long Distance Calls

/* Long Distance Calls - A long-distance carrier charges the following rates for the
   telephone calls:
  
   Starting Time of Call                Rate per Minute
   00:00 - 06:59                            0.05
   07:00 - 19:00                            0.45
   19:01 - 23:59                            0.20

   This program asks for the starting time and the number of minutes of the call, and
   displays the charges. The program asks for the time to be entered as a floating-
   point number in the form of HH.MM.

   * 07:00 hours will be entered as 07.00 and 16:28 hours will be entered as 16.28

   Input Validation: The program does not accept times that are greater than 23:59.
   No number whose last two digits are greater than 59 should be accepted.

   To get the fractional part of a number, if number is a floating-point variable,
   the following expression is used:

   * number - static_cast<int>(number) */

#include "Utility.h"

int main()
{
    /* Constants for time of day of calls, and duration per tariff */
    const double CALL_START_MORNING = 00.00,
                         CALL_DURATION_MORNING = 06.59,
                         CALL_START_DAY = 07.00,
                         CALL_DURATION_DAY = 19.00,
                         CALL_START_EVENING = 19.01,
                         CALL_DURATION_EVENING = 23.59;

    /* Constants for rate per minute */
    const double RPM_MORNING = 0.05,
                         RPM_DAY = 0.45,
                         RPM_EVENING = 0.20;


    /* Hold start time and duration of call */
    float callStart = 0,
            callDuration = 0;

    /* Hold duration, and total charges */
    double duration = 0,
                totalCharges = 0;

    /* Formating the output */
    cout << fixed << showpoint << setprecision(2);

    /* Introducing the program to the user and explaining the choices */
    cout << "\t\t Long Distance Call Calculator\n\n"
         << "You may enter the duration of calls which you want to inquire the charges you\n"
         << "are interested about. The format to enter starting time and call duration\n"
         << "is as follows: HH:NN is entered as 07.00 or 16:59.\n\n"
         << "The tarifs and call durations that apply to them are as follows:\n"
         << "Morning calls: 00.00 to 06.59\n"
         << "Day calls: 07.00 to 19.00\n"
         << "Evening calls: 19.01 to 23.45\n\n";

    /* Ask the user for the starting time of call and validate the input */
   
    cout << "Enter the starting time of your call (HH.MM - 01.00): ";
    cin >> callStart;

    if (callStart - static_cast<int>(callStart) >= .60 || callStart >= 23.54)
    {
        cout << "You entered an invalid starting time. Only values between\n"
            << ".00 and .59 for the last two digits of time are allowed.\n";
    }
    else
    {
        cout << "Enter the duration of your call (HH.MM - 05.59): ";
        cin >> callDuration;
        {
            if (callDuration - static_cast<int>(callDuration) >= .60)
            {
                cout << "You entered an invalid call duration time. Only values between\n"
                    << ".00 and .59 for the last two digits of time are allowed.\n";
            }
            else if (callStart >= CALL_START_MORNING && callDuration < CALL_START_DAY)
            {
                /* Calculation */
                duration = (callDuration - callStart);
                totalCharges = duration * (RPM_MORNING) * 100;

                /* Display the call information and charges for the call */
                cout << "\nYour call started at: " << callStart
                    << " and had a duration of: " << duration
                    << " hrs.\n"
                    << "Your call was a morning call, for which our company charges: $"
                    << RPM_MORNING << " per minute.\n"
                    << "The total cost of your call is: $" << totalCharges << endl;
            }

            else if (callStart >= CALL_START_DAY && callDuration < CALL_START_EVENING)
            {
                duration = (callDuration - callStart);
                totalCharges = duration * (RPM_DAY) * 100;

                cout << "\nYour call started at: " << callStart
                    << " and had a duration of: " << duration
                    << " hrs.\n"
                    << "Your call was a daytime call, for which our company charges: $"
                    << RPM_DAY << " per minute.\n"
                    << "The total cost of your call is: $" << totalCharges << endl;
            }
            else if (callStart >= CALL_START_EVENING && callDuration <= (CALL_DURATION_EVENING + 1))
            {
                duration = (callDuration - callStart);
                totalCharges = duration * (RPM_EVENING) * 100;

                cout << "\nYour call started at: " << callStart
                    << " and had a duration of: " << duration
                    << " hrs.\n"
                    << "Your call was an evening call, for which our company charges: $"
                    << RPM_EVENING << " per minute.\n"
                    << "The total cost of your call is: $" << totalCharges << endl;
            }
            else
            {
                cout << "Your call duration is out of bounds.\n";
            }
        }
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment