A Gas Pump Simulation The model should have member function

. A Gas Pump Simulation

The model should have member functions that

a) display the amount dispensed

b) display the amount charged for the amount dispensed

c) display the cost per gallon

d) reset amount dispensed and amount charged to 0 prior to dispensing

e) dispense fuel. Once started the gas pump continues to dispense fuel, continually keeping track of both amount of money charged and amount of fuel dispensed until stopped.

f) a control to stop dispensing.

The implementation was carried out as follows:

1) Implement a class definition that models of the behavior of the gas pump.

2) Write implementations of the member functions.

3) Decide whether there are other data the pump must keep track of that the user should not have access to.

Parts a) b) c) d) are straightforward to implement. Part e), the pumping operation is a little tricky. Once I realized that the valve on the nozzle must be held to continue to dispense gas, I decided to model that behavior by repeatedly pressing <CR> is an easy step. Tweaking the appearance and behavior detail is all that was left. The tweaking was not trivial.

Solution

#include <iostream>

#include <iostream>

using namespace std;

class GasPump

{

public:

void initialize(); // set charges, amount dispensed, and

                     //gasInMainTank to 0.

void reset();      // set charges and amount dispensed to 0;

void displayCostPerGallon();

//If there is only one grade, of gasoline, this should be a static

//member of GasPump, since it would then apply to all instances of

//GasPump. If there are several grades, then this and costPerGallon

//should be ordinary members.

void displayGasNCharges();

// Dispense member continually updates display of new amount and

// new charges

void dispense();

void stop(); // If called, stops dispensing operation.

               // My implementation never used this.

private:

double gasDispensed;

double charge;

public:

//Perhaps these functions should be static members of this class.

//See the earlier comment about this.

void setPricePerGallon(double newPrice);

void buyFromJobber(double quantity);

void displayAmountInMainTank();

private:

//These variables should be static members, since

//they are associated with all class instances.

double gasInMainTank;

double costPerGallon;

};

void GasPump::displayAmountInMainTank()

{

cout << gasInMainTank;

}

void GasPump::setPricePerGallon(double newPrice)

{

costPerGallon = newPrice;

}

void GasPump::buyFromJobber(double quantityBought)

{

   gasInMainTank += quantityBought;

}

void GasPump::initialize()

{

gasDispensed = 0;

charge = 0;

gasInMainTank = 0;

}

void GasPump::reset()

{

gasDispensed = 0;

charge = 0;

}

void GasPump::displayCostPerGallon()

{

cout << costPerGallon;

}

void GasPump::displayGasNCharges()

{

cout << \"gallons: \" << gasDispensed

       << \"   $\"      << charge << endl;

}

void GasPump::dispense()

{

   char quit;

   system(\"cls\");     // Windows clear screen

   //system(\"clear\"); // Unix/Linux

   cout << \"\ \ DISPENSING FUEL\ \";

   displayGasNCharges();

   cout << \"\ Press <CR> to dispense in 0.1 gallon increments. \"

        << \"\ Press q or Q <CR>to terminate dispensing.\ \";

   while(gasInMainTank > 0)

   {     

     quit = cin.get();

     if (quit == \'q\' || quit == \'Q\')

          break;

     charge += 0.1 * costPerGallon;

     gasDispensed += 0.1;

     gasInMainTank -= 0.1;

     system(\"cls\");     // Windows clear screen

     //system(\"clear\"); // Unix/Linux

     cout << \"\ \ DISPENSING FUEL\ \";

     displayGasNCharges();

     cout << \"\ Press <CR> to dispense in 0.1 gallon increments. \"

           << \"\ Press q or Q <CR>to terminate dispensing.\ \";

   }

if(0 == gasInMainTank)

     cout << \"Dispensing ceased because main tank is empty.\ \";

}

int main()

{

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(2);

GasPump pump;

char ans;

cout << \"Gas Pump Modeling Program\ \";

pump.initialize();

pump.setPricePerGallon(1.50);

pump.buyFromJobber(25);

cout << \"Amount of gasoline in main tank is: \";

pump.displayAmountInMainTank();

cout << endl;

cout << \"Gasoline price is $\";

pump.displayCostPerGallon();

cout << \" per gallon.\ \";

pump.displayGasNCharges();

cout << \"Press Y/y <CR> to run the dispensing model,\"

       << \" anything else quits. \ \";

cin >> ans;

if( !(\'Y\' == ans || \'y\' == ans) )

     return 0;

system(\"cls\");     // Windows

//system(\"clear\"); // Unix/Linux

cout << \"\ Welcome Customer #1\ \";

cout << \"Press Y/y <CR> to start dispensing Fuel,%u201D

       << %u201C other quits \ \";

cin >> ans;

if(\'Y\'==ans || \'y\' == ans)

{

     ans = cin.get(); // eat <cr>

     pump.dispense();

}

cout << \"Thank you. Your charges are:\ \" ;

pump.displayGasNCharges();

cout << endl;

cout << \"\ \ Amount of gasoline remaining in main tank is: \";

pump.displayAmountInMainTank();

cout << \" gallons\ \";

cout << \"\ \ Welcome Customer #2\ \";

pump.displayGasNCharges();

cout << \"Press Y/y <CR> to start dispensing Fuel,\"

       << \" anything else quits dispensing. \ \";

cin >> ans;

if(\'Y\'==ans || \'y\' == ans)

{

     pump.reset(); // zero old charges

     ans = cin.get(); // eat <cr>

     pump.dispense();

}

cout << \"Thank you. Your charges are:\ \" ;

pump.displayGasNCharges();

cout << endl;

cout << \"\ \ Amount of gasoline remaining in main tank is: \";

pump.displayAmountInMainTank();

cout << \" gallons\ \";

return 0;

}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site