CECS 277
LAB ASSIGNMENT 2
Assigned date: Wednesday 9/5
Due date: Monday 9/10
20 points


Objectives

Prelab

Write a program that simulates a vending machine. Users select  a product and and provide payment. IR the payment is sufficient to cover the purchase price or the product, the product is dispensed and change is given. Otherwise, the payment is returned to the user.

Requirements:

  1. Create a class called CashRegister that has methods to totals up sales and compute change due.
  2. Create a class called MonetaryUnit that has methods to returns the name or the monetary unit and the monetary value or the unit.
  3. Test your designing classes by running the main methods below.

    /**
    This program tests the CashRegister class.
    */
    public class CashRegisterTester
    {
    public static void main(String[] args)
    {
    final double NICKEL_VALUE = 0.05;
    final double DIME_VALUE = 0.1;
    final double QUARTER_VALUE = 0.25;
    final double DOLLAR_VALUE = 1.0;
    CashRegister myRegister = new CashRegister();
    myRegister.recordPurchase(1.82);
    myRegister.enterPayment(1, new MonetaryUnit(DOLLAR_VALUE, "dollar bill"));
    myRegister.enterPayment(3, new MonetaryUnit(QUARTER_VALUE, "quarter"));
    myRegister.enterPayment(2, new MonetaryUnit(NICKEL_VALUE, "nickel"));

    double myChange = myRegister.giveChange();
    System.out.println("Change: " + myChange);
    System.out.println("Expected: 0.03");
    }
    }

Output

Change: 0.030000000000000027
Expected: 0.03

Grading requirements