CPTR 124 Fundamentals of Programming


In this lab you implement the methods of a class according to a behavioral specification.


  1. Teams

    You may work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.

  2. Create a new project

    Create a new project named VendingMachine.

  3. Add the header file

    Add to your project the header file vend.h. The contents of vend.h is

    #ifndef _VEND_H_
    #define _VEND_H_
    
    class VendingMachine {
        //  The target value needed to serve the product (price of
        //  product)
        int product_price;
    
        //  The quantity of the product contained by this vending
        //  machine.
        int product_quantity;
    
        //  The total amount of money inserted during a particular
        //  transaction
        int transaction_total;
    
        //  The total amount of money held by the vending machine.
        int machine_total;
    
    public:
        //----------------------------------------------------------
        //  The constructor accepts the vending machine product's
        //  price and its quanity.  The machine is set to its initial
        //  state.  Note that the product price cannot change
        //  for the life of this VendingMachine object, but the
        //  quantity of the product will decline as customers make 
        //  purchases.
        VendingMachine(int price, int amount);
        
        //----------------------------------------------------------
        //  Input accepts coin values of the following
        //  amounts: 5, 10, 25.
        //  The function returns true if the amount the
        //  client provides is 5, 10, or 25; otherwise,
        //  the function returns false.  An unacceptable
        //  value does not change the state of the machine.
        //  Acceptable values accumulate funds toward a purchase
        //  of the product.
        //  This function does not change the quantity of the product.
        bool input(int amount);
    
        //----------------------------------------------------------
        //  A client calls the vend method in an attempt to purchase
        //  the product.
        //  The function can return a positive, negative, or zero
        //  result:
        //    - returns 0, if the accumulated amount of money for a
        //      transaction is equal to the price of the product; this 
        //      means the product is served, and no change is due.
        //
        //    - returns a value greater than 0 when the amount entered
        //      during a transaction is greater than the price of the
        //      product.  In this case the return value represents the
        //      amount of change due to the customer.  In this case 
        //      the vending machine serves the product and returns 
        //      the change amount.
        //
        //    - returns a value less than 0  when the amount of money
        //      entered during a transaction has yet to reach the price
        //      of the product; this means the customer must insert more 
        //      coins before the vending machine object can serve the 
        //      product can be served.  In this case the return value
        //      represents what the customer still "owes" to get the 
        //      product.
        //
        //  If the vending machine serves the product, the product's
        //  quantity decreases by one and the machine resets to its
        //  ready to begin a new transaction state.
        //
        //  The vending machine can serve only one product per
        //  transaction; for example, if the customer enters coins
        //  amounting to three times the price of the product, the
        //  machine would vend one product and return two times the
        //  product's price in change.
        int vend();
    
        //----------------------------------------------------------
        //  Resets the machine to its ready to begin a new transaction
        //  state.  The value returned is the accumulated amount during 
        //  the current transaction; it represents the money that should 
        //  be sent to the coin return.
        //  This function does not change the quantity of the product.
        int reset();
    
        //----------------------------------------------------------
        //  Returns the price of the product
        //  This function changes neither the accumulated customer
        //  funds nor the quantity of the product.
        int price() const;
    
        //----------------------------------------------------------
        //  Returns the quantity of the product currently held by this
        //  vending machine.
        //  This function changes neither the accumulated customer
        //  funds nor the quantity of the product.
        int quantity() const;
    
        //----------------------------------------------------------
        //  Returns the current amount of money held by the vending
        //  machine.
        //  This function changes neither the accumulated customer
        //  funds nor the quantity of the product.
        int total() const;
    };
    
    #endif
    

  4. Method implementation

    Add to your project a C++ source file named vend.cpp. Within vend.cpp implement the methods of the VendingMachine class according to their specifications within the comments of the vend.h header file.

  5. Client code

    Write a client program that asks the user for the cost and quantity of the product for a vending machine. The resulting vending machine object persists for the life of the program's execution, and the program should run until all the product in the vending machine is purchased.

    The program should continuously ask the user for input (coin amounts) until the product can be released. The program should report the change (if any) to return after each transaction. Once the product is released, the machine begins fresh asking for input (coins). The program's execution continues until the vending machine's product is exhausted.

    With identical inputs your program should behave exactly as shown in the following sample session:

    Enter price of product in cents:75
    Enter quantity of product:4
    =========================================================
      Negative entry resets the machine
    =========================================================
    [Prod Quant:   4  Total:   0]  Please enter  75 cents: 25
    [Prod Quant:   4  Total:  25]  Please enter  50 cents: 25
    [Prod Quant:   4  Total:  50]  Please enter  25 cents: 10
    [Prod Quant:   4  Total:  60]  Please enter  15 cents: 10
    [Prod Quant:   4  Total:  70]  Please enter   5 cents: 25
    Eject product, change = 20
    ---------------------------------------------------------
    [Prod Quant:   3  Total:  75]  Please enter  75 cents: 10
    [Prod Quant:   3  Total:  85]  Please enter  65 cents: 10
    [Prod Quant:   3  Total:  95]  Please enter  55 cents: 5
    [Prod Quant:   3  Total: 100]  Please enter  50 cents: 25
    [Prod Quant:   3  Total: 125]  Please enter  25 cents: 25
    Eject product, change = 0
    ---------------------------------------------------------
    [Prod Quant:   2  Total: 150]  Please enter  75 cents: 30
    Bad entry, collect rejected coin
    [Prod Quant:   2  Total: 150]  Please enter  75 cents: 25
    [Prod Quant:   2  Total: 175]  Please enter  50 cents: 25
    [Prod Quant:   2  Total: 200]  Please enter  25 cents: 5
    [Prod Quant:   2  Total: 205]  Please enter  20 cents: -1
    Transaction cancelled, change: 55
    ---------------------------------------------------------
    [Prod Quant:   2  Total: 150]  Please enter  75 cents: 10
    [Prod Quant:   2  Total: 160]  Please enter  65 cents: 10
    [Prod Quant:   2  Total: 170]  Please enter  55 cents: 5
    [Prod Quant:   2  Total: 175]  Please enter  50 cents: 25
    [Prod Quant:   2  Total: 200]  Please enter  25 cents: 25
    Eject product, change = 0
    ---------------------------------------------------------
    [Prod Quant:   1  Total: 225]  Please enter  75 cents: -1
    Transaction cancelled, change: 0
    ---------------------------------------------------------
    [Prod Quant:   1  Total: 225]  Please enter  75 cents: 25
    [Prod Quant:   1  Total: 250]  Please enter  50 cents: 10
    [Prod Quant:   1  Total: 260]  Please enter  40 cents: 5
    [Prod Quant:   1  Total: 265]  Please enter  35 cents: 5
    [Prod Quant:   1  Total: 270]  Please enter  30 cents: 10
    [Prod Quant:   1  Total: 280]  Please enter  20 cents: 25
    Eject product, change = 5
    ---------------------------------------------------------
    *** Machine empty ***
    

    In the above sample run a number that follows a colon (:) at the end of a line represents user input. The program prints all the other text shown in the sample run. For example, in the line

    [Prod Quant:   4  Total:  25]  Please enter  50 cents: 25
    

    The program prints [Prod Quant: 4 Total: 25] Please enter 50 cents: and the user enters 25.

    Note that the output represents some sort of diagnostic mode that a customer normally would not see. The values in the square brackets at the beginning of each input prompt indicate the quantity of the product remaining in the machine and the total amount the machine has collected. You write code that implements this diagnostic mode so that we can determine if your VendingMachine class is working correctly.

    Restrict all input (cin) and output (cout) statements to your client code; your VendingMachine methods should perform no I/O themselves.

    Important. You need only a relatively simple main function to implement the client code. You should use a VendingMachine object and exercise its methods. Do not use your own variables to keep track of the amount of product, change, total money entered, etc. Your implementation of the VendingMachine class should handle all of these details. If you find it necessary for you client code to do any arithmetic (besides unary minus to display the change properly), you are not doing it correctly; a VendingMachine object itself should take care of all the required arithmetic and accounting.

  6. Check out

    Your finished program will be evaluated for correctness and compliance. When approved, you should submit your C++ source file to eclass. Be sure your name and your partner's name are included in comments at the top of each source file.