P4.4 The CashRegister class has an unfortunate limitation, it is closely tied to
ID: 3563738 • Letter: P
Question
P4.4 The CashRegister class has an unfortunate limitation, it is closely tied
to the coin system in the United States and Canada. Research the system
used in most of Europe. Your goal is to produce a cash register that works
on Euros and cents. Rather than designing another limited cash register
implementation for the European market, you should design a separate Coin
class and a cash register that can work with coins of all types.
Create a Tester class that will test all of the methods of the Coin class class
and CashRegister class.
Explanation / Answer
public class CashRegister { // the amount in the cash register double amount; // a constructor to build a new CashRegister CashRegister(double amount) { this.amount = amount; } // a method to accept cash which return the change double pay(double payment, double purchase) { System.out.println("For a purchase of $ " + purchase + " I received $" + payment); double change = payment - purchase; amount += purchase; System.out.println("Now I have $" + amount + " in my cash register"); return change; } // the method to test the whole thing public static void main(String[] args) { // build a new CashRegister with $ 1000.00 CashRegister cr = new CashRegister(1000.0); double change = cr.pay(100.00, 55.55); System.out.println("I received $" + change + " as change"); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.