Calculate Parking Charges based on different price range - C++ Data Type

C++ examples for Data Type:double

Description

Calculate Parking Charges based on different price range

Demo Code

#include <math.h>
#include <iomanip>
#include <iostream>

const double BASE_HOURS = 3.0f;
const double MINIMUM_FEE = 2.00f;
const double PART_CHARGE = 0.50f;
const double MAXIMUM_FEE = 10.0f;

double calculateCharges(double);

int main(int argc, const char *argv[]) {
    double hours1, hours2, hours3 = 0.0f;
    double charges1, charges2, charges3 = 0.0f;

    std::cout << "Program to calculate parking charges for 3 cars\n"
              << std::endl;/*from  www  . ja va 2  s .com*/

    std::cout << "Enter hours parked of 3 cars: ";
    std::cin >> hours1 >> hours2 >> hours3;

    std::cout << "Car" << std::setw(15) << "Hours" << std::setw(15) << "Charge"
              << std::endl;

    charges1 = calculateCharges(hours1);
    charges2 = calculateCharges(hours2);
    charges3 = calculateCharges(hours3);

    std::cout << std::fixed << std::setprecision(2);
    std::cout << "1" << std::setw(17) << hours1 << std::setw(15) << charges1
              << std::endl;
    std::cout << "2" << std::setw(17) << hours2 << std::setw(15) << charges2
              << std::endl;
    std::cout << "3" << std::setw(17) << hours3 << std::setw(15) << charges3
              << std::endl;
    std::cout << "TOTAL" << std::setw(13) << hours1 + hours2 + hours3
              << std::setw(15) << charges1 + charges2 + charges3 << std::endl;

    return 0;
}
double calculateCharges(double hours) {
    if (hours >= 24) {
        return MAXIMUM_FEE;
    }
    if (hours <= BASE_HOURS) {
        return MINIMUM_FEE;
    }
    // only charge for each hour/part hour after BASE_HOURS
    double charge = MINIMUM_FEE + ((ceil(hours - BASE_HOURS)) * PART_CHARGE);

    return (charge <= MAXIMUM_FEE) ? charge : MAXIMUM_FEE;
}

Result


Related Tutorials