My first program using Classes, it should allow you to set the price of three different pumps, times how long you pump, then calculates what you owe, and updates the amount of gas left in the pump. Working on an associates degree and my teacher doesn't give any critique so anything you all could point out would be helpful and appreciated. Here it is...
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime> //for the timer
#include <cctype> //toupper and to lower functions
using namespace std;
const float FLOWRATE = .18;
const float MAX_GAS = 100.0;
class GasPump {
public:
float RegularInPump(float rip);
float SuperInPump(float sip);
float DiesalInPump(float dip);
void SetPrice();
void DisplayOutput(int gasChoice, float amountDispensed, float ®ularInPump, float &superInPump, float &diesalInPump);
private:
float regularInPump;
float superInPump;
float diesalInPump;
float regGasPrice;
float supGasPrice;
float dieselGasPrice;
};
float TimeSpentPumping(float &timePumping, float &amountDispensed, int &gasChoice) {
while (true) {
cout << "Do you want unleaded, super unleaded or diseal gas?(Enter 1, 2, or 3) ";
cin >> gasChoice;
if ((cin) && (gasChoice >= 1 && gasChoice <= 3)) {
cin.ignore();
break;
}
}
//Start pump timer
cout << "Press Enter to start pumping.";
cin.ignore();
float start = clock();
//stop pump timer
cout << "Press Enter when done pumping.";
cin.ignore();
timePumping = (clock() - start) / 1000;
cout << endl;
cout << "You pumped for " << timePumping << " seconds." << endl;
cout << endl;
amountDispensed = timePumping * FLOWRATE;
return amountDispensed;
}
int RestartProgram() {
char response;
bool endProgram = false;
cout << std::endl;
while (true) {
cout << "Do you wish to run this program again? Enter Y or N: ";
cin >> response;
response = tolower(response);
if ((cin) && response == 'y' || response == 'n') {
cin.ignore();
break;
}
}
if (response == 'n') {
return true;
}
return 0;
}
int main()
{
GasPump SetPump;
float timePumping = 0.0;
float amountDispensed = 0.0;
float regularInPump = MAX_GAS;
float superInPump = MAX_GAS;
float diesalInPump = MAX_GAS;
int gasChoice = 0; //1=reg, 2=super, 3=diseal
bool endProgram = 0;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Set intial pump prices
SetPump.SetPrice();
while (!endProgram) {
amountDispensed = TimeSpentPumping(timePumping, amountDispensed, gasChoice);
SetPump.DisplayOutput(gasChoice, amountDispensed, regularInPump, superInPump, diesalInPump);
endProgram = RestartProgram();
}
return 0;
}
void GasPump::SetPrice() {
cout << "Enter the price for unleaded gas: ";
cin >> regGasPrice;
cout << "Enter the price for super unleaded gas: ";
cin >> supGasPrice;
cout << "Enter the price for diesal gas: ";
cin >> dieselGasPrice;
cin.ignore();
}
void GasPump::DisplayOutput(int gasChoice, float amountDispensed, float ®ularInPump, float &superInPump, float &diesalInPump) {
float amtOwed = 0.0;
float pumpPrice = 0.0;
float pumpLevel = 0.0;
string gasString;
//Calculate price of chosen gas
switch (gasChoice) {
case 1:
pumpPrice = regGasPrice;
amtOwed = amountDispensed * regGasPrice;
pumpLevel = RegularInPump(regularInPump -= amountDispensed);
gasString = "Regular Gas";
break;
case 2:
pumpPrice = supGasPrice;
amtOwed = amountDispensed * supGasPrice;
pumpLevel = SuperInPump(superInPump -= amountDispensed);
gasString = "Super Unleaded Gas";
break;
default:
pumpPrice = dieselGasPrice;
amtOwed = amountDispensed * dieselGasPrice;
pumpLevel = DiesalInPump(diesalInPump -= amountDispensed);
gasString = "Diesal Gas";
break;
}
//Display results
int indent = 20;
cout << setw(indent) << left << "Amount dispensed: " << amountDispensed << " gallons of " << gasString << endl;
cout << setw(indent) << left << "At a price of: " << "$" << pumpPrice << endl;
cout << setw(indent) << left << "You owe " << "$" << amtOwed << endl;
cout << endl;
cout << "There is " << pumpLevel << " gallons of " << gasString << " in the pump." << endl;
cout << endl;
}
float GasPump::RegularInPump(float rip) {
regularInPump = rip;
return regularInPump;
};
float GasPump::SuperInPump(float sip) {
superInPump = sip;
return superInPump;
};
float GasPump::DiesalInPump(float dip) {
diesalInPump = dip;
return diesalInPump;
};