In: Computer Science
Build a program that calculates the parking fare for the customer who parks their cars in a public parking lot given the following: i. A character showing the type of vehicle: C for car, B for bus, T for truck ii. An integer between 0 and 24 showing the hour the vehicle entered the lot iii. An integer between 0 and 60 showing the minute the vehicle entered the lot iv. An integer between 0 and 24 showing the hour the vehicle left the lot v. An integer between 0 and 60 showing the minute the vehicle left the lot For encouraging people to park for a short period of time, the management uses two different rates for each type of vehicle as shown below: Vehicle First Rate Second Rate CAR RM 1.00/hr first 3 hr RM 2.00 after first 3 hr TRUCK RM 1.50/hr first 3 hr RM 2.50 after first 3 hr BUS RM 2.00/hr first 3 hr RM 3.00 after first 3 hr No vehicle is allowed to stay in the parking lot later than midnight. If the customer is detected stay parking lot overnight, the customer is required to pay the penalty fee RM 150.00 per day plus the parking fare.
Code:
#include <iostream>
using namespace std;
int main()
{
char type;
int
enterHour,enterMinute,leftHour,leftMinute,enterTime,leftTime,finalTime,penalty=0,finalHours;
float fare;
cout<<"Enter vehicle type: ";
cin>>type;
cout<<"Enter an integer between 0 and 24 showing the hour the
vehicle entered the lot: ";
cin>>enterHour;
cout<<"Enter an integer between 0 and 60 showing the minute
the vehicle entered the lot: ";
cin>>enterMinute;
cout<<"Enter an integer between 0 and 24 showing the hour the
vehicle left the lot: ";
cin>>leftHour;
cout<<"Enter an integer between 0 and 60 showing the minute
the vehicle left the lot: ";
cin>>leftMinute;
enterTime=enterHour*60+enterMinute;
leftTime=leftHour*60+leftMinute;
if(leftTime<enterTime)
{
finalTime=24*60-enterTime+leftTime;
penalty=150;
}
else
{
finalTime=leftTime-enterTime;
}
if(finalTime%60==0)
{
finalHours=finalTime/60;
}
else
{
finalHours=finalTime/60+1;
}
switch(type)
{
case 'C': if(finalHours<=3)
{
fare=finalHours*1;
}
else
{
fare=3*1+(finalHours-3)*2;
}
break;
case 'T': if(finalHours<=3)
{
fare=finalHours*1;
}
else
{
fare=3*1.5+(finalHours-3)*3;
}
break;
case 'B': if(finalHours<=3)
{
fare=finalHours*1;
}
else
{
fare=3*2+(finalHours-3)*3;
}
break;
default: cout<<"Wrong vehicle type.";
}
cout<<"\nTotal Fare: "<<(fare+penalty);
return 0;
}
Code Screenshot:
Output:
1
2