Assignment 2: The Vending Machine

Summary
Collaboration
You must work individually on this assignment. You may only discuss this assignment with the course staff.

Goals for this assignment.

  • Design, write, and test a Java program that simulates the operation of a vending machine that sells cans of soda.
  • Implement Composition, the design technique in OOP to implement a has-a relationship between objects.
  • Practice reading from a file and handling possible exceptions.
  • Research Java packages: the ArrayList Class. ArrayList is a variable-length data structure.

Starter Code

Use the start-up code in the following path on MathLAN to complete this assingment. Please use Eclipse to import files in vendingmachine directory into a Java project.

  /home/jimenezp/csc207/assignments/vendingmachine/

You should have these 6 files:

  1. BillBox.java
  2. BrandInventory.java
  3. CoinBox.java
  4. Display.java
  5. VendingMachine.java
  6. VendingMachineSim.java

You will also see 2 text files:

  1. config.dat
  2. sample.txt

You must use all 6 classes in your solution, although you may modify the design slightly from the suggested one given via the method descriptions and signatures.

Note: there will be errors showing since many of these files have sections where you will need to add code!! You may want to comment out sections while you are developing.

The VendingMachineSim Program

This program contains your main method and is intended to manage the interactions between the user and the VendingMachine object. As much as possible, make use of the principle of encapsulation, using methods in VendingMachine to perform tasks and return information that you would see if you were interacting with a tangible vending machine.

Use the Display class to communicate with the user rather than using System.out.println. This practice will separate the message from the implementation, and may allow us to revise this program later to implement a more attractive graphical interface once we learn how to use a Drawing Panel in Java.

The Vending Machine Components

The interal and external components of the vending machine are shown below:

  • a coin box for depositing quarters, dimes, and nickels during the current transaction
  • a second coin box for making change to be given to users in a successful sale
  • a bill box for depositing one-dollar bills
  • inventory information that keeps track of the beverage brands, the ID number associated with each brand, and the number of sodas that are currently in stock
  • a display that should be used for ALL messages to the user

In a normal operation, the user inserts coins and one-dollar bills until the amount deposited equals the cost of the soda they want. The vending machine either reports that the item is sold out or asks the user to take soda from the beverage receiver. If the amount deposited exceeds the cost of soda, the machine drops coins, which make up difference, into the change receiver. The machine turns on the change light if it lacks (enough) coins to make up the a difference between the cost of a soda and the amount deposited. If the machine lacks (enough) coins to pay the difference, it drops as many coins as it can to the change receiver.

We have simplified the process slightly and will use the Display to provide messages that a sale is successful, the amount of change to take, or that there is not enough change to provide exact change after a transaction. All of these things could be added later if we have time to create a graphical interface.

Complete the Program

Complete the definition of a class for each of these pieces in the start-up code. Each class contains appropriate methods, however, you may add additional methods or change these slightly so long as the class as a whole handles similar tasks.

  1. Complete the code in each of the classes that are components of the Vending machine:
    1. BillBox: accepts one-dollar bills and reports how many it has received in the current transaction
    2. CoinBox: accepts coins, reports number of each coin type it has, reports the total value currently stored in the box, and can transfer all coins from itself to another coin box.
    3. Display: must be used to convey ALL messages to the user. You may not use System.out.println anywhere else in your program except for diagnostic messages (which should be commented out before submission).
    4. BrandInventory: for each brand that will be stocked in the machine, this tracks the ID number of the brand, the price per can, and the number of cans currently in the machine. It will also need “getter” methods to return the ID, the price, and the number on hand, a “toString()” method, the status of the inventory (i.e. is it sold out?). Finally, it will need a “setter” method to reduce inventory when an item is sold.
  2. Complete the code for the Vending Machine object itself, which will need to create and initialize the objects in the component classes:
    1. The constructor will need to read the “config.dat” file in order to create an ArrayList of BrandInventory objects.
    2. You will create two CoinBox objects: one is for the coins deposited in the current transaction, and one is to be used to make change.
    3. You should initialize the change-making CoinBox so that it has 10 quarters, 10 dimes, and 10 nickels and is ready to make change for the first transaction.
    4. You will need to create one BillBox.
    5. You will need to create a Display that the Vending Machine will use to communicate to the user.
  3. Complete the VendingMachineSim class. This program will be responsible for interactions with the user, using the standard terminal input (System.in). The VendingMachineSim class should create its own Display object that it will use to display messages to the user. You may not use System.out.println to convey messages in your submitted code.

The main method uses the vending machine pieces explained above through its public methods. The purpose of the main method of this class is:

  1. to call the VendingMachine constructor, which will use config.dat to create the inventory ArrayList
  2. to greet the user and announce the Vending Machine Simulation is starting
  3. to ask if user wants to purchase a beverage and request the ID number. The ID number is similar to the buttons you push on many modern vending machines to indicate your selection
  4. to act as the physical controls of a vending machine, accepting bills or coins using the VendingMachine methods and its display object
  5. to terminate the program when the user no longer wants to purchase sodas.

Your vending machine can use any technique to make change given that the change box contains appropriate coin types to make that change. You do not need to try to implement an efficient algorithm, especially since those generally assume that you have an infinite number of coins of each type!

Note that after each successful purchase, the deposited coins (not bills) are moved into the second coin box so that you have more coins available with which to make change. You may want to have a private field in the Bill Box to track the number of bills associated with the current transaction. Note also that, since we never ask for the total amount of money in the machine itself, do not need to track the total number of bills … only those associated with the current transaction.

When using Eclipse, your config.dat file should be located in the project directory at the same level as the /bin and /src directories.

Testing


The vending machine directory contains sample.txt that shows a sample run of the program. It shows the interactions of a couple of purchases based on the default config.dat file.

You need to test your program thoroughly; the provided files are a starting point (basic example). You should test your program with both successful and unsuccessful transactions. You must at least do:

  • A successful purchase in which the user receives a soda. The user may receive change, if any, after the purchase.
  • A purchase attempt that is unsuccessful because the brand ran out of stock.

Run your program and submit a text file showing the output of the test. If you are running it in Eclipse, you should copy and paste the output from the console frame.

Documentation


Create a folder in the project directory for your documentation and use the Javadoc utility to generate the documentation of your classes.

Submission and Grading


You MUST include an Academic Honesty Statement in your client program. If you do not, your submission will be assessed a 10% penalty.

Your program must compile and run for the graders. If it does not, your submission will not be graded until the corrected file is received and you will be assessed a 20% penalty. You will need to use a token for resubmitting.

You must submit all of your source code, any configuration files that you created (other than the one provided) and a text file that shows a test run of your program. This must be in a compressed tar.gz file (see Lab 1 for instructions on how to create this file from your directory). Files that are in a .zip format will lose one point.

This assignment is worth up to 25 points based on the following:

Good practices

  • [2 points] Well documented code (use of Javadoc utility). The code meets good programming and naming standards. Points will be deducted if your code is difficult to read.
  • [2 points] Messages containing dollar amounts are formatted with a “$” sign and two decimal places (such as $2.00)

Meet Requirement specification

  • [1 point] Complete BillBox.java
  • [3 points] Complete CoinBox.java
  • [2 points] Complete Display.java
  • [2 points] Complete BrandInventory.java
  • [4 points] Complete VendingMachine.java
  • [2 points] Complete VendingMachineSim.java
  • [1 point] Reads config.dat file
  • [1 point] System.out.println used only in Display.java
  • [1 point] The program uses an ArrayList instead of an array for the inventory
  • [1 point] Test file exists and shows successful and unsuccessful purchase attempts

Apply object-oriented design principles to the creation of Java programs

  • [1 point] Setter methods ensure that parameter values are in acceptable range

Handling errors

  • [2 points] Handles possible exceptions