Create a CashRegister
class that represents a cash
register, tracking its balance and computing change for transactions.
Fill in the methods shown below:
package csc201.hw3;
/** Represent a cash register, tracking balance and determining change
* required for transactions.
*/
public class CashRegister {
// Fill in constructor, instance members, and supporting methods or
// other class definitions.
/** Record purchase of given cost, paid with given amount of cash. Return
* representation of change given (including number of each denomination
* dispensed).
*
* If the register cannot make change for the given transaction, signal an
* error (with a method of your choice).
*/
public ??? pay(??? cost, ??? cash) {
}
/** Return current total register balance, as a single value.
*/
public ??? balance() {
}
/** Add given currency to register. If you want, you can alter the method
* signature to better support your data representation, or define separate
* methods for each denomination.
*/
public void addCash(??? cash) {
}
public static void main(String[] args) {
}
}
You'll need to determine how to represent the contents of the register
and the data type to return that represents the change received.
Once you've done that, fill in the ???
in the template.
Note that the pay
method should return the number of
each coin or bill needed. For example, if a cost of $3.45 is paid with a
$10 bill, you should report change as
- 1 $5 bill
- 1 $1 bill
- 2 quarters
- 1 nickel
Make sure to outline your assumptions about the currency denominations
supported. If you're having trouble, simplify the problem, perhaps by
allowing only whole-dollar prices.