f Customers who wish to rent vehicles have two main options available to them: r
ID: 3552556 • Letter: F
Question
fCustomers who wish to rent vehicles have two main options available to them: rent from Hertz, or rent from Avis. Each of these rental companies have four different kinds of vehicle: sedan, truck, minivan, or sportscar. The customer will receive a discount if he/she is renting from the company that he/she already has subscription with.
Customer subscribed with Hertz will receive 10% discount on the total rental cost.
Customer subscribed with Avis will receive 15% discount on the total rental cost.
Write a program (by modifying hw5.cpp) that will calculate the full cost of rental and the cost after applying discount (if any). The program will first ask the customer which rental company they
Explanation / Answer
please rate - thanks
// Purpose: This program simulates a calculator for how much a customer needs to pay
// for car rentals depending on his/her subscriptions
//---------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
// Rental companies
const char HERTZ = 'H';
const char AVIS = 'A';
// Vehicle types
const int SEDAN = 1;
const int TRUCK = 2;
const int MINIVAN = 3;
const int SPORTSCAR = 4;
// Rental types
const char DAILY = 'D';
const char WEEKLY = 'W';
const char MONTHLY = 'M';
// Daily vehicle costs
const float SEDAN_COST = 8.50;
const float TRUCK_COST = 10.00;
const float MINIVAN_COST = 16.50;
const float SPORTSCAR_COST = 17.50;
// Discounts
const float HERTZ_DISCOUNT = 0.10; // 10 percent
const float AVIS_DISCOUNT = 0.15; // 15 percent
//---------------------------------------------------------------------------
// Name: GetCompany
// Parameters: Question, string, input: The question to ask the user
// Returns: char, uppercase, the company the user chooses
// Purpose: Ask the user to enter a letter indicating a company
//---------------------------------------------------------------------------
char GetCompany(string question)
{char company;
cout<<question;
cin>>company;
company=toupper(company);
while(company!=HERTZ&&company!=AVIS)
{cout<<"Invalid entry-must be "<<HERTZ<<" or "<<AVIS<<" ";
cout<<question;
cin>>company;
company=toupper(company);
}
return company;
}
//---------------------------------------------------------------------------
// Name: GetVehicleType
// Parameters: None
// Returns: int The type of vehicle selected
// Purpose: Asks the use what type of vehicle they want
// NOTE: Called by GetRentalInfo
//---------------------------------------------------------------------------
int GetVehicleType()
{int type;
cout<<"Enter type of vehicle wanted: ";
cout<<"1.-sedan 2.-truck 3.-minivan 4.-sportscar ";
cin>>type;
while(type<1||type>4)
{cout<<"invalid entry ";
cout<<"Enter type of vehicle wanted: ";
cout<<"1.-sedan 2.-truck 3.-minivan 4.-sportscar ";
cin>>type;
}
return type;
}
//---------------------------------------------------------------------------
// Name: GetRentalType
// Parameters: None
// Returns: char The type of rental (DAILY, WEEKLY, MONTHLY) selected
// Converts character entered to uppercase letter
// Purpose: Asks the user what type of rental they want
// NOTE: Called by GetRentalInfo
//---------------------------------------------------------------------------
char GetRentalType()
{char type;
cout<<"Enter rental type D-daily W-weekly M-monthly ";
cin>>type;
type=toupper(type);
while(type!=DAILY&&type!=WEEKLY&&type!=MONTHLY)
{cout<<"Invalid entry ";
cout<<"Enter rental type D-daily W-weekly M-monthly ";
cin>>type;
type=toupper(type);
}
return type;
}
//---------------------------------------------------------------------------
// Name: GetRentalInfo
// Parameters: Pass by reference: VehicleType, int, RentalType, char.
// Input: Type of vehicle and type of rental
// Returns: N/A
// Purpose: Get the type of vehicle and the type of rental the user chose
// NOTE: This function MUST CALL GetRentalType and GetVehicleType
//---------------------------------------------------------------------------
void GetRentalInfo(int& VehicleType, char& RentalType)
{RentalType=GetRentalType();
VehicleType=GetVehicleType();
}
//---------------------------------------------------------------------------
// Name: GetLength
// Parameters: TimePeriod, string, input: The question to ask the user
// Returns: int, Length, the length of rental the user chooses
// Purpose: Ask the user to enter a number representing the length of rental
//---------------------------------------------------------------------------
int GetLength(string TimePeriod)
{int Length;
cout<<TimePeriod;
cin>>Length;
return Length;
}
//---------------------------------------------------------------------------
// Name: GetCost
// Parameters: VehicleType, int, RentalType, char, input: To determine the cost of rental
// Returns: float, Cost, the cost of rental
// Purpose: Calculate the cost of rental based on the type of vehicle and the type of rental
//---------------------------------------------------------------------------
float GetCost(int VehicleType, char RentalType)
{float Cost;
int TimePeriod;
switch(VehicleType)
{case 1: Cost=SEDAN_COST; break;
case 2: Cost=TRUCK_COST; break;
case 3: Cost=MINIVAN_COST; break;
case 4: Cost=SPORTSCAR_COST; break;
}
switch(RentalType)
{case 'M':
TimePeriod=GetLength("Enter months of rental ");
Cost=TimePeriod*(Cost*25);
break;
case 'D':
TimePeriod=GetLength("Enter number of days of rental ");
Cost=TimePeriod*Cost;
break;
case 'W':
TimePeriod=GetLength("Enter number of weeks of rental ");
Cost=TimePeriod*(Cost*6);
break;
}
return Cost;
}
//---------------------------------------------------------------------------
// Name: EligibleForDiscount
// Parameters: CompanyUsed, char, CompanySubscribed, char,
// input: To compare if CompanyUsed equals to CompanySubscribed
// Returns: bool, the comparison is "true" or "false"
// Purpose: To determine if the user has subscription with the company he/she
// wishes to rent from.
//---------------------------------------------------------------------------
bool EligibleForDiscount(char CompanyUsed, char CompanySubscribed)
{if(CompanyUsed==CompanySubscribed)
return true;
else
return false;
}
//---------------------------------------------------------------------------
// Name: ApplyDiscount
// Parameters: Pass by reference, Cost, float. Pass by value, CompanySubscribed, char,
// input: Apply discount to cost based on company subscribed
// Returns: N/A
// Purpose: Apply discount to the rental cost based on the user's subscribed rental company
//---------------------------------------------------------------------------
void ApplyDiscount(float& Cost, char CompanySubscribed)
{float discount;
if(CompanySubscribed==HERTZ)
discount=HERTZ_DISCOUNT;
else
discount=AVIS_DISCOUNT;
Cost=Cost-(Cost*discount);
}
//---------------------------------------------------------------------------
// Main program
//---------------------------------------------------------------------------
int main ()
{
// Declarations
char CompanySubscribed; // The company you have a subscription at
char CompanyUsed; // The company you wish to rent from
int VehicleType; // The type of vehicle you want to rent
char RentalType; // Whether you are renting by the day, week, month
float Cost; // The cost of your rental
// print name and UAID
cout << "################### ";
cout << "### Name ### ";
cout << "### UAID ### ";
cout << "################### ";
// Print the program information
cout << "------------------------------------------------------------------------ ";
cout << "There are two main rental comapnies for renting vehicles, "
<< "Hertz and Avis. ";
cout << " Each of these rental companies rents four types of vehicles: "
<< "Sedan, Truck, Minivan, or Sportscar. ";
cout << "Based on your type of vehicle, and length of rental, we will figure out "
<< "how much you owe the rental company. ";
cout << "------------------------------------------------------------------------ ";
// Get information from the user
CompanySubscribed = GetCompany(" With which company do you have a subscription?");
CompanyUsed = GetCompany(" From which company do you wish to rent?");
GetRentalInfo(VehicleType, RentalType);
// Calculate the cost
Cost = GetCost(VehicleType, RentalType);
cout << " The full cost or your rental is: $"
<< fixed << showpoint << setprecision(2) << Cost << ". ";
// Apply any discounts
if (EligibleForDiscount(CompanyUsed, CompanySubscribed))
{
ApplyDiscount(Cost, CompanySubscribed);
cout << " After your discount, your rental is: $"
<< fixed << showpoint << setprecision(2) << Cost << ". ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.