For C++ Write a program that uses a class for a gas pump. The functionality of t
ID: 3673015 • Letter: F
Question
For C++
Write a program that uses a class for a gas pump. The functionality of the program is as follows:
The pump should request a method of payment(Cash/Credit). If paying cash, the amount of cash should be entered up front, if paying by credit card the amount of the saile will be determined when the user finished pumping gas.
The user should be able to select a grade(type) of gas. Once the user selects a payment tpe and a grade, the user should be able to start pumping gas. Be creative here. The user should do somthing (key press maybe) to continue puping gas.
As the user is pumping the gas, the pup should display the amount of gas dispensed in gallons, and the total amount charged.
Allow for another customer to use the pump(reset displays).
Be creative when coiding and feel free to add additional feature, however they should be for things that a gas pump might do. Keep the cod in main() to a minimum. I it is somthing that a gas pump would do, then you should have functions/method for such within your class.
So far this is what i've came up with. Please review my program and offer any suggestions to correct it or make it better. Thanks in advance.
#include <iostream>
using namespace std;
class GasPump
{
private:
int payment; //1 for cash, 2 for credit
double cashEntered;
char gasType;
double gallonsDispensed;
double totalCharged;
public:
void enterPaymentInfo();
int getPaymentOption();
char getGasType();
double getCashEntered();
void dispenseCreditGallons(char);
void dispenseCashGallons(double, char);
static const double RPRICE = 1.40; //declare static constant doubles for gas type prices in public
static const double MPRICE = 1.80;
static const double PPRICE = 2.10;
};
void GasPump::enterPaymentInfo() //this function takes payment info
{
cout << "Payment Option, Enter '1' for cash or '2' for credit: ";
cin >> payment;
if(payment == 1)
{
cout << "Enter the amount of cash for gas: ";
cin >> cashEntered;
}
cout << " Regular Gas Mid-Grade Gas Premium Gas ";
cout << "$"<< RPRICE << "/gal $" << MPRICE << "/gal $" << PPRICE << "/gal";
cout << " Enter 'r' for regular, 'm' for midgrade, or 'p' for premium: ";
cin >> gasType;
}
int GasPump::getPaymentOption()
{
return payment;
}
double GasPump::getCashEntered()
{
return cashEntered;
}
char GasPump::getGasType()
{
return gasType;
}
void GasPump::dispenseCashGallons(double c, char g)
{
double cashEntered = c;
double price;
int pump;
double gallonsDispensed = 0;
cout << "Cash Enter 1 to begin pumping: ";
cin >> pump;
//For each of the 3 gas types, gallons prepaid is calculated by cashEntered and gallon type price
while(true)
{
system("cls");
gallonsDispensed += .1;
if(g == 'r')
{
price = gallonsDispensed*RPRICE;
cout <<"r";
}
if(g == 'm')
{
price = gallonsDispensed*MPRICE;
}
if(g == 'p')
{
price = gallonsDispensed*PPRICE;
}
cout << "Gallons: " << gallonsDispensed;
cout << " Price: " << price;
cout << " Enter 1 to continue pumping an additional gallon of gas "
<< "or 2 to stop pumping:";
cin >> pump;
if(pump != 1 || price >= cashEntered-.21)
break;
}
while(true)
{
system("cls");
gallonsDispensed += .01;
if(g == 'r')
{
price = gallonsDispensed*RPRICE;
cout <<"r";
}
if(g == 'm')
{
price = gallonsDispensed*MPRICE;
}
if(g == 'p')
{
price = gallonsDispensed*PPRICE;
}
cout << "Gallons: " << gallonsDispensed;
cout << " Price: $" << price;
cout << " Enter 1 to continue pumping an additional gallon of gas "
<< "or 2 to stop pumping:";
cin >> pump;
if(pump != 1 || price >= cashEntered-.009)
{
cout << "Gallons:" << gallonsDispensed;
cout << " Price: $" << price << " ";
break;
}
}
}
void GasPump::dispenseCreditGallons(char g)
{
int pump;
double credit;
double gallonsDispensed = 0;
cout << "Enter 1 to begin pumping: ";
cin >> pump;
while(true)
{
system("cls");
gallonsDispensed += .1;
if(g == 'r')
{
credit = gallonsDispensed*RPRICE;
}
if(g == 'm')
{
credit = gallonsDispensed*MPRICE;
}
if(g == 'p')
{
credit = gallonsDispensed*PPRICE;
}
cout << "Gallons: " << gallonsDispensed << " ";
cout << "Credit: $" << credit;
cout << " Enter 1 to continue pumping gas "
<< "or 2 to stop pumping:";
cin >> pump;
if(pump != 1)
break;
}
cout <<"You pumped " << gallonsDispensed << " gallons and total of $"
<< credit << " was charged to your credit card. ";
}
int main()
{
GasPump customer1, customer2;
int payment;
double cash, cashGallons, credit;
char gasType;
//Next 3 lines displays all money to 2 decimal places
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
customer1.enterPaymentInfo();
payment = customer1.getPaymentOption();
gasType = customer1.getGasType();
if (payment == 1) //for cash payment
{
cash = customer1.getCashEntered();
customer1.dispenseCashGallons(cash, gasType);
}
if (payment == 2) //for credit payment
{
customer1.dispenseCreditGallons(gasType);
}
customer2.enterPaymentInfo();
payment = customer2.getPaymentOption();
gasType = customer2.getGasType();
if (payment == 1) //for cash payment
{
cash = customer2.getCashEntered();
customer2.dispenseCashGallons(cash, gasType);
}
if (payment == 2) //for credit payment
{
customer2.dispenseCreditGallons(gasType);
}
return 0;
}
Explanation / Answer
Yes. Your code is working good enough. I suggest using an getc(stdin), or getch() instead of cin. This will read the character from the keyboard (even the enter key), without the need for a 1 to be pressed, and then an Enter key to be pressed everytime. And change the prompt to "Press enter key to continue" whereever required.
Other than that, every thing seems ok with this code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.