import java.util.*; //A class to represent an (infinite capacity) Piggy Bank //Operation supported are limited to // drop() - add another US_Coin to this PiggyBank // empty() - remove all coins from this PiggyBank // and return their total $-value public class PiggyBank { //Instance Variable //A count of each different US_Coin in this PiggyBank private int[] coinCount; //Constructor //Creates a new empty PiggyBank public PiggyBank() { //One element for each different US_Coin this.coinCount = new int[US_Coin.values().length]; //Initially, there are no coins in this PiggyBank for (int c = 0; c < this.coinCount.length; c++) this.coinCount[c] = 0; } //Mutator //Add any single US_Coin to this PiggyBank public void drop(US_Coin coin) { this.coinCount[ coin.ordinal() ]++; } //Mutator //Remove all coins from this PiggyBank //Return the total $-value of the coins removed public double empty() { double total = 0; for (US_Coin coin : US_Coin.values()) { total += coin.getValue() * this.coinCount[coin.ordinal()]; this.coinCount[coin.ordinal()] = 0; } return total / 100.0; } public String toString() { String image = ""; for (US_Coin coin : US_Coin.values()) image += this.coinCount[coin.ordinal()] + "x" + coin + " "; return image.trim(); } }