CPTR 124 Fundamentals of Programming


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


  1. Create a new project

    Create a new project named VendingMachine.

  2. Create a header file

    Add to your project the header file vendinglogic.h. The contents of vendinglogic.h should be

    
    #ifndef _VENDINGLOGIC_H_
    #define _VENDINGLOGIC_H_
    
    class VendingLogic
    {
        //  The target value needed to serve the product.
        //  The price must be a multiple of five cents since
        //  the machine will accept only nickels, dimes, and
        //  quarters.
        int goal;
    
        //  The total amount of money inserted.  This value is set
        //  to zero at the beginning of each transaction.  Each call
        //  to the input method may increase the value.
        int total;
    
    public:
        //  The constructor accepts the vending machine's
        //  goal amount.  The machine is set to its initial
        //  state.  Note that the goal amount cannot change
        //  for the life of this VendingLogic object.
        VendingLogic(int goal);
        
        //  The input method expects coin values of the 
        //  following amounts: 5, 10, 25.
        //  The function returns true if the amount the
        //  client provides is 5, 10, or 25, AND the 
        //  accumulated amount before this call is less than
        //  the product price (goal).  In addition to returning
        //  true, the function increases the accumulated amount
        //  by the value of the parameter.
        //  If the parameter is neither 5, 10, nor 25, the
        //  function returns false and the accumulated amount
        //  is not modified.  If the parameter is 5, 10, or 15 but
        //  the accumulated amount is greater than or equal to the
        //  product price, the function returns false and does not
        //  modify the accumlated amount.
        //  Clients can check the return value to implement a coin
        //  return feature for coins that are not accepted.
        bool input(int amount);
    
        //  Resets the machine to its initial state.  The value
        //  returned is the accumulated amount, if any; it 
        //  represents the money that can be sent to the 
        //  coin return unit, if a coin return feature is 
        //  implemented.
        int reset();
    
        //  Returns the cost of the product (same as the goal)
        int cost() const;
    
        //  Returns the amount remaining that must be entered to
        //  vend the product.  Returns a negative number if the 
        //  amount entered is greater than the price of the 
        //  product.  Clients can call this method to determine
        //  if the product can be vended (return value less than
        //  or equal to zero) and to compute how much change is due.
        int amount_due() const;
    };
    
    #endif
    
    

    All the files for this lab except for the class implementation file that you must provide are found here.

  3. Method implementation

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

  4. Client code

    Add the file vending_machine.cpp to your project. This file provides a GUI that allows a user to interact with a vending machine. Your code within vendinglogic.cpp controls how the machine functions. The machine should behave exactly as demonstrated in class.

  5. Image Files

    Add to your project the image files for the four candy bars and the fours coins. These image files are found here.

  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.