Write a Coin
class that identifies a value
for a type of coin and the name
of the coin. Both of these values should be specified as parameters in the constructor. Include appropriate getters for these values.
Then write a CashRegister
class that includes three methods:
recordPurchase()
which takes amount
as a parameter and adds it to the running total of the cost of items being purchased.enterPayment()
which takes parameters the number
of coins submitted for payment and a coin
object (of the Coin
class) that has a value and a name. This method adds the payment to a running total of how much money has been paid for the purchase.giveChange()
which returns the amount of money that should be returned to a client after. (A total purchase of 15.25 and a payment of 20.00 would result in 4.75 of change being given.)Use the following tester to check your classes.
/**
* The CashRegisterTester class is used to test students' CashRegister
* and Coin classes.
*
* @author Richard White
* @version 2015-12-08)
*/
public class CashRegisterTester
{
static final double EPSILON = 1E-12;
public static void main(String[] args)
{
int testsPassed = 0;
System.out.println("Cash register instantiating...");
CashRegister r = new CashRegister();
testsPassed++;
System.out.println(testsPassed + "/7 tests passed");
System.out.println("Coin 'quarter' instantiating...");
Coin quarter = new Coin(0.25, "quarter");
testsPassed++;
System.out.println(testsPassed + "/7 tests passed");
System.out.println("Coin 'bitcoin' instantiating...");
Coin bitcoin = new Coin(364.47, "bitcoin");
testsPassed++;
System.out.println(testsPassed + "/7 tests passed");
System.out.println("Purchase being recorded...");
r.recordPurchase(5.00);
testsPassed++;
System.out.println(testsPassed + "/7 tests passed");
r.recordPurchase(400.00);
System.out.println("Entering payment...");
r.enterPayment(2, quarter);
testsPassed++;
System.out.println(testsPassed + "/7 tests passed");
r.enterPayment(2, bitcoin);
System.out.println("Getting change...");
double payBack = r.giveChange();
testsPassed++;
System.out.println(testsPassed + "/7 tests passed");
System.out.println("Change is: " + payBack + "; Expected: 324.44");
if (Math.abs(payBack - 324.44) < EPSILON)
testsPassed++;
System.out.println(testsPassed + "/7 tests passed");
}
}