@Override public Collection<Coin> getChangeFor(int pence, Product p) throws NotEnoughChangeException, NotEnoughMoneyException { changeInProgress.clear(); int difference = pence - p.getProductPrice(); loadConfig(); if (enoughMoneyProvided(pence, p)) { if (enoughChange(difference)) { for (Coin coin : Coin.values()) { if (isCoinTypeNeeded(coin, difference - getCurrentChange())) { supplyThisCoinType(coin, difference - getCurrentChange(), true); } } if (getCurrentChange() != difference) throw new NotEnoughChangeException("The machine has run out of change."); updateConfig(); } else { throw new NotEnoughChangeException("The machine has run out of change."); } } else { throw new NotEnoughMoneyException("You have not provided enough money."); } return changeInProgress; }
@Override public Collection<Coin> getOptimalChangeFor(int pence, Product p) throws NotEnoughMoneyException { changeInProgress.clear(); int difference = pence - p.getProductPrice(); if (enoughMoneyProvided(pence, p)) { for (Coin coin : Coin.values()) { if (isCoinTypeNeeded(coin, difference - getCurrentChange())) { supplyThisCoinType(coin, difference - getCurrentChange(), false); } } } else { throw new NotEnoughMoneyException( "You have not provided enough money.\n" + "Remeber that 10 pounds need to be entered as 1000 (one thousand pennies), for example."); } return changeInProgress; }