// Created by George Austin Bradley on 19/11/2019.
// Copyright © 2019 George Austin Bradley. All rights reserved.
Hi. I was wondering if any of you would mind reviewing my program. I am a beginner / aspiring programmer and I am looking for advice on how I could make my program more efficient. For example, have I made good use of subroutines / functions? Am I using the classes correctly? Is there anything I can do to reduce the duration of the program? Any advice from programmers would be greatly appreciated even if it's just a prayer!
Thank you very much,
George bradley
#include
#include
#include
#include
using namespace std;
class cCar{
private:
string _sName;
double _dPrice;
public:
cCar(string s, double d){
_sName = s;
_dPrice = d;
}
string getName(){return _sName;}
double getPrice(){return _dPrice;}
};
vectorCarDatabase(vector&car_list){
car_list.push_back(cCar("Blue Nissan Skyline",1000));
car_list.push_back(cCar("Red Mini",3000));
car_list.push_back(cCar("Black Land Rover",4000));
car_list.push_back(cCar("Beatle",9000));
car_list.push_back(cCar("Ferrari",300000));
return car_list;
}
class Finance{
private:
string _sUserName;
double _dCostOfCar;
string _sChosenCar;
int _iFinancePlan;
double _dDepositedAmount;
double _dMonthlyPayments;
double _dTotalLeftToPay;
public:
Finance(string sName, double dCostOfCar, string sChosenCar, int iFinancePlan, double dDepositedAmount, double dDMonthlyPayments, double dTotalLeftToPay){
_sUserName = sName;
_dCostOfCar = dCostOfCar;
_sChosenCar = sChosenCar;
_iFinancePlan = iFinancePlan;
_dDepositedAmount = dDepositedAmount;
_dMonthlyPayments = dDMonthlyPayments;
_dTotalLeftToPay = dTotalLeftToPay;
}
string getUserName(){return _sUserName;}
double getdCostOfCar(){return _dCostOfCar;}
string getChosenCar(){return _sChosenCar;}
int getFinancePlan(){return _iFinancePlan;}
double getDepositAmount(){return _dDepositedAmount;}
double getMonthlyAmount(){return _dMonthlyPayments;}
double getTotalLeftToPay(){return _dTotalLeftToPay;}
};
//START OF PROTOTYPE
void ViewPurchases(vector&buyers, char &cOption, bool &bExit);
//END OF PROTOTYPE
//1. This displays the car menu items.
void display_menu(vector&car_list)
{
cout << "nMENU";
for (int iCount = 0; iCount != car_list.size(); iCount++)
{
cout << "n" << iCount + 1 << ". " << car_list(iCount).getName();
cout << "ntPrice: £" << car_list(iCount).getPrice();
cout << "n";
}
}
//This procedure proccesses the user's selection and all information regarding price and name of car are then transferred to transaction variables.
void selectedCar(vector&car_list, string &sNameOfChosenCar, double &dCostOfChosenCar)
{
int iSelectionFromMenu = -1;
do{
cout << "nChoose a car that you'd wish to buy from the menu (1 - " << car_list.size() << "): ";
cin >> iSelectionFromMenu;
if(iSelectionFromMenu > 0 && iSelectionFromMenu <= car_list.size())
{
sNameOfChosenCar = car_list(iSelectionFromMenu - 1).getName();
dCostOfChosenCar = car_list(iSelectionFromMenu - 1).getPrice();
}
else
{
cout << "nPlease enter valid number!";
iSelectionFromMenu = -1;
}
}while(iSelectionFromMenu == -1);
}
//This procedure gets from the user their preferred finance plan through their input.
void FinanceLength(int &iFinanceLength)
{
do{
cout << "nHow long do you wish for your finance plan to last? (1 - 4 years): ";
cin >> iFinanceLength;
if (iFinanceLength < 0 || iFinanceLength > 4)
{
cout << "nOops, try again! Please enter between 1 - 4!";
}
}while(iFinanceLength < 0 || iFinanceLength > 4);
}
//This procedure gets the user's deposit.
void DepositMoney(double &dDepositAmount)
{
do{
cout << "nEnter deposit amount (minimum £500 accepted): £";
cin >> dDepositAmount;
if (dDepositAmount < 500)
{
cout << "nTry again! Deposit an amount greater than or equal to £500.";
}
}while(dDepositAmount < 500);
}
//This function calculates the amount of money the user has to pay after deposit, added tax and charge percentage of 10%
double TotalLeftToPay(double iFinanceLength, double dDepositAmount, double dCostOfChosenCar)
{
double dChargePercentage = 0.10;
double dTotalLeftToPay = dCostOfChosenCar + (dCostOfChosenCar * dChargePercentage) - dDepositAmount + 135;
return dTotalLeftToPay;
}
//This calculates monthly payments.
double MonthlyPayments(double dTotalLeftToPay, int iFinanceLength)
{
double dMonthlyPayments = (dTotalLeftToPay / iFinanceLength) / 12;
return dMonthlyPayments;
}
void EndOfProgramOptions(vector&buyers, char &cOption, bool &bExit)
{
char cInputSelection = 0;
do{
cout << "View your purchases (y/n): ";
cin >> cInputSelection;
cInputSelection = toupper(cInputSelection);
if (cInputSelection == 'Y')
{
ViewPurchases(buyers, cOption, bExit);
}
}while(cInputSelection != 'Y' && cInputSelection != 'N');
}
//This asks the user whether they'd like to restart the application.
void RestartOptions(char &cOption, bool &bExit, vector&buyers)
{
do{
cout << "nDo you wish to make another purchase? (y/n): ";
cin >> cOption;
cOption = toupper(cOption);
switch(cOption)
{
case 'Y':
bExit = false;
break;
case 'N':
EndOfProgramOptions(buyers, cOption, bExit);
bExit = true;
break;
default:
cout << "Sorry, that's an invalid input, please try again!";
continue;
}
}while(cOption != 'Y' && cOption != 'N');
}
//This string function returns either year or years (plural)
string YearOrYears(int iFinanceLength)
{
return (iFinanceLength > 1)? "years" : "year";
}
//This displays receipt of the user's transaction.
void Receipt(const string &sUserName, const int &iFinanceLength, const double &dDepositAmount, char cOption, bool &bExit, const string &sNameOfChosenCar, const double &dCostOfChosenCar, vector&buyers)
{
double dTotalLeftToPay = TotalLeftToPay(iFinanceLength, dDepositAmount, dCostOfChosenCar);
double dMonthlyPayments = MonthlyPayments(dTotalLeftToPay, iFinanceLength);
buyers.push_back(Finance(sUserName,dCostOfChosenCar,sNameOfChosenCar,iFinanceLength,dDepositAmount, dMonthlyPayments,dTotalLeftToPay));
cout << "nReceipt for: " << sUserName << ". ";
cout << "nYou have chosen " << sNameOfChosenCar << ".";
cout << "nYour finance plan timescale is " << iFinanceLength << " " << YearOrYears(iFinanceLength) << ".";
cout << "nYou've deposited £" << dDepositAmount << ".";
cout << "nTotal left to pay: £" << dTotalLeftToPay;
cout << "nMonthly Payments: £" << dMonthlyPayments;
cout << "n";
RestartOptions(cOption, bExit, buyers);
}
//This displays receipt of the user's transaction.
void ViewPurchases(vector&buyers, char &cOption, bool &bExit)
{
for (int iCount = 0; iCount != buyers.size(); iCount++)
{
cout << "nPurchase " << iCount + 1 << " by " << buyers(iCount).getUserName() << ". ";
cout << "nYou have chosen " << buyers(iCount).getChosenCar() << ".";
cout << "nYour finance plan timescale is " << buyers(iCount).getFinancePlan() << " " << YearOrYears(buyers(iCount).getFinancePlan()) << ".";
cout << "nYou've deposited £" << buyers(iCount).getDepositAmount() << ".";
cout << "nTotal left to pay: £" << buyers(iCount).getTotalLeftToPay() << ".";
cout << "nMonthly Payments: £" << buyers(iCount).getMonthlyAmount() << ".";
cout << "n";
}
RestartOptions(cOption, bExit,buyers);
}
//This asks the user whether they're happy with the options of they've chosen.
void AcceptDeclineOptions(string &sUserName, int &iFinanceLength, double &dDepositAmount, bool &bExit, string &sNameOfChosenCar, double &dCostOfChosenCar, vector&buyers)
{
char cOption = 0;
do
{
cout << "nConfirm finance plan (y/n): ";
cin >> cOption;
cOption = toupper(cOption);
if (cOption == 'Y')
{
Receipt(sUserName, iFinanceLength, dDepositAmount, cOption, bExit, sNameOfChosenCar, dCostOfChosenCar, buyers);
}
else if (cOption == 'N')
{
RestartOptions(cOption, bExit, buyers);
}
else
{
cout << "nSorry, that's not a valid command.";
}
}while(cOption != 'Y' && cOption != 'N');
}
int main()
{
bool bExit = false;
int iFinanceLength = 0;
double dDepositAmount = 0;
string sNameOfChosenCar = "";
double dCostOfChosenCar = 0;
vectorcar_list;
CarDatabase(car_list);
vectorcar_purchases;
vectorbuyers;
cout << "Welcome!";
string sUserName = "";
cout << "nEnter your name: ";
cin >> sUserName;
do{
display_menu(car_list);
selectedCar(car_list, sNameOfChosenCar, dCostOfChosenCar);
FinanceLength(iFinanceLength);
DepositMoney(dDepositAmount);
AcceptDeclineOptions(sUserName, iFinanceLength,dDepositAmount,bExit, sNameOfChosenCar, dCostOfChosenCar, buyers);
}while(bExit == false);
}