How to define a class GasPump?

X

Xiaoshen Li

Dear All,

I am reading a textbook "Absolute C++" by Walter Savitch. After a
chapter talking about classes, a programming project puzzled me:

Write the difinition for a class named GasPump to be used to model a
pump at an automobile service station. .... Below are listed things a
gas pump might be expected to do.
a. A display of the amount dispensed.
b. A display of the amount charged for the amount dispensed.
....
f. Actual behavior of the gas pump is, once started, it dispenses as
long as you hold the
nozzle lever. Peculiarities of console I/O make it difficult to continue
to dispense while
waiting for a signal to stop. One solution is to model this behavior by
having the user
repeatedly press the return (enter) key, dispensing a quantum of fuel
and recomputing the
amount charged, say 0.1 gallons at each press.

I don't know how to let my object of class GasPump do f listed above. In
my class GasPump, there is a method start() and a method stop(). But:

GasPump myGasPump;

myGasPump.start(); //start pumping gas

//now, how can I stop pumping?

I am thinking using a boolean bHold. start() will set bHold = true and
stop() will set bHold = false. Inside start() method, once bHold is
true, the amount dispensed will keep increasing
void GasPump::start()
{
bHold = true;
double dAmount = 0.0;
while (bHold) //while handle is being pressed
{
dAmount += 0.1
} //now the handle has been released
//now the dAmount is the amount dispensed,
}
void GasPump::stop()
{
bHold = false;
}

But this seems getting into inter-processe communication(too complicated
to me) and I don't believe this is author's intention for this
programming project. I don't know how to deal with pressing a return
key, 0.1 gallons gas is added, either.

Could anybod kindly help me out? Thank you very much.
 
V

Victor Bazarov

Xiaoshen said:
I am reading a textbook "Absolute C++" by Walter Savitch.

A side note: this book has not been reviewed by ACCU's reviewers, but
if you look at accu.org , then name "Walter Savitch" does appear once and
his book is _NOT_ recommended. Of course this does not necessarily mean
all his books are of low quality.
> After a
chapter talking about classes, a programming project puzzled me:

Write the difinition for a class named GasPump to be used to model a
pump at an automobile service station. .... Below are listed things a
gas pump might be expected to do.
a. A display of the amount dispensed.
b. A display of the amount charged for the amount dispensed.
...
f. Actual behavior of the gas pump is, once started, it dispenses as
long as you hold the
nozzle lever. Peculiarities of console I/O make it difficult to continue
to dispense while
waiting for a signal to stop. One solution is to model this behavior by
having the user
repeatedly press the return (enter) key, dispensing a quantum of fuel
and recomputing the
amount charged, say 0.1 gallons at each press.

I don't know how to let my object of class GasPump do f listed above. In
my class GasPump, there is a method start() and a method stop(). But:

GasPump myGasPump;

myGasPump.start(); //start pumping gas

//now, how can I stop pumping?

I am thinking using a boolean bHold. start() will set bHold = true and
stop() will set bHold = false. Inside start() method, once bHold is
true, the amount dispensed will keep increasing
void GasPump::start()
{
bHold = true;
double dAmount = 0.0;
while (bHold) //while handle is being pressed
{
dAmount += 0.1
} //now the handle has been released
//now the dAmount is the amount dispensed,
}
void GasPump::stop()
{
bHold = false;
}

But this seems getting into inter-processe communication(too complicated
to me) and I don't believe this is author's intention for this
programming project. I don't know how to deal with pressing a return
key, 0.1 gallons gas is added, either.

Could anybod kindly help me out? Thank you very much.

I think the author suggests to define the behaviour of the GasPump to pump
a small fixed amount of fuel at a request:

myGasPump.dispense_a_little_bit();

and then use this in a loop controlled by the input from the user, but
outside of the pump itself (IOW, the pump is not supposed to have any
user interface with console I/O).

You might (later) experiment with timing (using, say, 'time' function)
between pressing 'Enter' (or accepting user's input). Example (the #
character designates the cursor, --- designates thinking):

Pump Menu:
B<Enter> to begin pumping
X<Enter> to exit .......... #
---------------- <user enters 'B' and presses the 'Enter' key -------
----- The program takes the time reading and goes into new state -----
....pumping...
Pump Menu:
E<Enter> to stop pumping
P<Enter> to pause pumping ... #
----------------- <user enters 'E' and presses the 'Enter' key -------
------ The program takes the second time reading and calculates ------
---- the amount of fuel dispenced based on the time in seconds by ----
--- multiplying it by some amount of fuel-per-second (flow when the --
-- pump is opened) ---------------------------------------------------
.... done pumping: dispensed XXX.XXX litres ($XXXXXX.XX)
Pump Menu
$<Enter> to pay for gas
X<Enter> to try to escape without paying ... #

V
 
M

Marcus Kwok

Xiaoshen Li said:
Dear All,

I am reading a textbook "Absolute C++" by Walter Savitch. After a
chapter talking about classes, a programming project puzzled me:

Write the difinition for a class named GasPump to be used to model a
pump at an automobile service station. .... Below are listed things a
gas pump might be expected to do.
...
f. Actual behavior of the gas pump is, once started, it dispenses as
long as you hold the
nozzle lever. Peculiarities of console I/O make it difficult to continue
to dispense while
waiting for a signal to stop. One solution is to model this behavior by
having the user
repeatedly press the return (enter) key, dispensing a quantum of fuel
and recomputing the
amount charged, say 0.1 gallons at each press.
[snip]
I don't know how to deal with pressing a return
key, 0.1 gallons gas is added, either.

Could anybod kindly help me out? Thank you very much.

Have you learned how to get any input from the user yet? I would
suggest doing the same thing, but just ignoring anything they type and
instead incrementing the amount of gas dispensed.
 
X

Xiaoshen Li

Thank you very much for all the replies. I particularly like accu.org
web page.
 
N

Neil Cerutti

Dear All,

I am reading a textbook "Absolute C++" by Walter Savitch. After
a chapter talking about classes, a programming project puzzled
me:

Write the difinition for a class named GasPump to be used to model a
pump at an automobile service station. .... Below are listed things a
gas pump might be expected to do.
a. A display of the amount dispensed.
b. A display of the amount charged for the amount dispensed.
...
f. Actual behavior of the gas pump is, once started, it dispenses as
long as you hold the
nozzle lever. Peculiarities of console I/O make it difficult to continue
to dispense while
waiting for a signal to stop. One solution is to model this behavior by
having the user
repeatedly press the return (enter) key, dispensing a quantum of fuel
and recomputing the
amount charged, say 0.1 gallons at each press.

I don't know how to let my object of class GasPump do f listed
above. In my class GasPump, there is a method start() and a
method stop().

I would implement a different interface. Timing strategies will
be tough without implementation specific libraries.

Your tank will accept commands to pump X number of units of
petrol, or X units of currency worth of gas.
PUMP 24 GALLONS
Ding... ding... ding... ding... ding... ding... DONE.
You pumped 24 gallons and owe $60.72.
PUMP 5 DOLLARS
Ding... DONE.
You pumped 1.97 gallons and owe $5.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top