This assignment will be your first where you define classes. Specifically, you should define a class that acts like a cash register, and another class that represents a cash bundle (e.g. an amount of money in various denominations). Your Cash class should keep track of pennies up to 10 dollar bills, (that is, it should represent the number of pennies, nickles, dimes, quarters, dollar bills, fives and tens in a given cash bundle). A cash register then contains an amout of cash. Your CashRegister class should have two constructors: one where you specify how many of all cash denominations are initially in the cash register, and one where you initialize it to have 5 of everything. There may be several cash registers in a store, so make sure your TestCashRegister program instantiates several of them in your tests.
Each CashRegister object should keep track of the following transactions. There should be a method
public Cash makePurchase(int purchasePrice, Cash cashTendered)
Here, purchasePrice is in pennies (to avoid roundoff error as discussed in Liang on p.
), and cashTendered is the the breakdown of bills and coins that were tendered to pay for it. For example, if a purchase price is $1.87, the argument will be 187. The method should return the change (if any) that is due back to the customer. This is also a Cash object, possibly with a value equal to 0. (Note that this is different from not returning any object at all).
Your program should try to make change using the algorithm we discussed in class at the beginning of the semester, highest coins first, giving the smallest number of coins in the change. However if your register does not have enough of certain coins then you should try to make change using the coins you have. At the end of the makePurchase method you should print a message noting the transaction.
If you cannot make change, you cannot complete the transaction. Your method should print a message saying the transaction cannot be completed. Under no circum- stances should your cash register “lose” money.
Your CashRegister class should also have a method
public int getTotalSales().
Your CashRegister class should keep track of this across all cash registers in the store.
Cash registers should also be able to process returns. If a customer comes in with a receipt, you need to return the amount of the money in the sale to the customer. This method should be
public Cash makeReturn(int purchasePrice).
A return involves making change for the amount of the purchasePrice. You should
also keep track of totalReturns and have a method
public int getTotalReturns().
To make sure the amount of money in the register at the end of the day is correct, we should have
totalCashValue = initialCashValue + totalPurchases - totalReturns. so your cash register will have to remember the initial Cash it started with.
Finally, two more methods your CashRegister class should have are:
public Cash getCashInRegister = amount of cash (in pennies) in the register at the moment
public String toString(CashRegister c) = print contents of register.
Your Cash class should have a method
public int getCashValue()
which returns the value of a Cash bundle. (For example, a cash bundle fo a dollar bille and 3 quarters is 175.)
You should email both your classes CashRegister.java and TestCashRegister.java to the grader (firstname.lastname@example.org). Note that the grader will call your methods with his own TestCashRegister class, so make sure you name your methods as specified above.