How to define a class GasPump?

Discussion in 'C++' started by Xiaoshen Li, Dec 12, 2005.

  1. Xiaoshen Li

    Xiaoshen Li Guest

    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.
     
    Xiaoshen Li, Dec 12, 2005
    #1
    1. Advertising

  2. Xiaoshen Li wrote:
    > 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
     
    Victor Bazarov, Dec 12, 2005
    #2
    1. Advertising

  3. Xiaoshen Li

    Marcus Kwok Guest

    Xiaoshen Li <> wrote:
    > 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.

    --
    Marcus Kwok
     
    Marcus Kwok, Dec 12, 2005
    #3
  4. Xiaoshen Li

    Xiaoshen Li Guest

    Thank you very much for all the replies. I particularly like accu.org
    web page.
     
    Xiaoshen Li, Dec 13, 2005
    #4
  5. Xiaoshen Li

    Neil Cerutti Guest

    On 2005-12-12, Xiaoshen Li <> wrote:
    > 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.

    --
    Neil Cerutti
     
    Neil Cerutti, Dec 13, 2005
    #5
  6. Xiaoshen Li

    ChenweiWu Guest

    what does this class apply to?
    "Xiaoshen Li" <> ??????:dnmr0r$f5lv$...
    > Thank you very much for all the replies. I particularly like accu.org web
    > page.
    >
     
    ChenweiWu, Dec 16, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,955
    Thomas Weidenfeller
    Oct 12, 2005
  2. theotyflos
    Replies:
    3
    Views:
    508
    Thomas Matthews
    Feb 19, 2004
  3. robin liu
    Replies:
    3
    Views:
    867
    Robin Liu
    Apr 21, 2006
  4. Replies:
    11
    Views:
    733
    Ron Natalie
    Nov 9, 2006
  5. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    516
    Brian Takita
    Jan 23, 2006
Loading...

Share This Page