Skip to content

Instantly share code, notes, and snippets.

@williwambu
Created December 5, 2015 11:41
Show Gist options
  • Save williwambu/9ff9fea88fe3a7c3e71d to your computer and use it in GitHub Desktop.
Save williwambu/9ff9fea88fe3a7c3e71d to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
/*
a function to calculate the total cost of an accomodation
@param accomodationType is accomodation type
@param numberOfDays is days of stay
*/
float calculateTotalCost(string accomodationType,int numberOfDays){
int supCost = 2000; //Cost of SUP per day
int dowCost = 1200; //Cost of DOW per day
float totalCost; //total cost of accomodation
if(accomodationType == "SUP"){
totalCost = numberOfDays * supCost;
}else if(accomodationType == "DOW"){
totalCost = numberOfDays * dowCost;
}
return totalCost;
}
/*
a function to calculate discount
*/
float calulateDiscount(int numberOfDays,string accomodationType){
float totalCost = calculateTotalCost(accomodationType,numberOfDays);
//declare discount variable
float discount = 0;
//SUP type
if(accomodationType =="SUP"){
if(numberOfDays < 3){
discount = 0.1 * totalCost;
}
else if(numberOfDays >= 3){
discount = 0.2 * totalCost;
}
else if(numberOfDays >= 5){
discount = 0.25 * totalCost;
}
}
else if(accomodationType == "DOW"){
if(numberOfDays >= 3){
discount = 0.2 * totalCost;
}
else if(numberOfDays >=5){
discount = 0.25 * totalCost;
}
}
return discount;
}
int main()
{
cout << "Welcome to Accomodation Charges Calculator."<<endl;
cout << "================================================="<<endl;
cout<<"Enter the number of customers you want to calculate charges for:"<<endl;
int numberOfCustomers; //number of customers to compute charges for
string name; //name of customer
string accomodationType; //type of accomodation
int numberOfDays; //days of stay/accomodation
float totalCost; // total cost for a customer
float netCost; //net cost for a customer
float discount; //discount for a customer
float allCustomersTotal; // total cost of all customers
float allCustomersDiscount; //total discount for all customers
//get the customers to compute for
cin >> numberOfCustomers;
//Loop through all customers,get their details,calculate and output their charges
for(int i = 0; i < numberOfCustomers; i++){
cout << "Enter customer name:";
cin >> name;
if(name =="XXX"){
cout << "Program exit.";
exit(1); // exit the program
}
cout << "Enter accommodation type, SUP or DOW:";
cin >> accomodationType;
cout<< "Enter number of days of accomodation: ";
cin >> numberOfDays;
totalCost = calculateTotalCost(accomodationType,numberOfDays);
discount = calulateDiscount(numberOfDays,accomodationType);
netCost = totalCost - discount;
cout << "-------------------------------------------------" << endl;
cout << "Customer Name : " << name<< "\n";
cout << "Accomodation Charge : " << totalCost << "\n";
cout << "Discount : " << discount << endl << "\n";
cout << "Net Cost : " << netCost << "\n";
cout << "-------------------------------------------------" << endl;
allCustomersTotal += totalCost; //add the new total cost
allCustomersDiscount += discount; //add the new discount
}
cout << "Summary for all Customers" << endl;
cout << "-------------------------------"<<endl;
cout << "Total Accomodation charge : " << totalCost<<endl;
cout << "Total Discount : " << discount << endl;
cout << "Total Net Charge: " << allCustomersTotal - allCustomersDiscount << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment