CPTR 124 Fundamentals of Programming


In this lab you implement the methods of a class according to a behavioral specification. You also write client code to test the functionality of your custom class.


  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 and add the header file vend.h. The vend.h file contains the declaration of class named VendingMachine. Read the comments in the code carefully to understand the purpose of each method in the VendingMachine class.

    The VendingMachine class defines the structure and behavior of a very simple vending machine object. A vending machine instance can serve only one product. It accepts only nickels, dimes, and quarters, and provides change if the customer enters more than the amount of the product before attempting to obtain the product.

    You will implement the methods declared in vend.h and write client code to test a vending machine object.

  3. 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 described within the comments of the vend.h header file.

  4. Client code

    Write a client program that asks the user for the cost and quantity of the product for a vending machine. It then creates a VendingMachine with those given parameters. 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 (only integer amounts of 5, 10, or 25 are acceptable) until enough funds accumulate to match the price of the product. Once the product's cost has been covered, the vending machine object releases the product. Your client code must exercise the public methods of the VendingMachine object to report how much money remains to be entered and to indicate when the product should be released. The vending machine object provides change for any overage the client pays, and your client code needs to report that change amount via its interaction with the vending machine object. Once the machine releases the product, the machine begins fresh asking for input. 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 (std::cin) and output (std::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.

  5. Check out

    Your finished program will be evaluated for correctness and compliance. When approved, you should submit your C++ source files to eclass.