Esempio n. 1
0
  @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;
  }
Esempio n. 2
0
  @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;
  }
Esempio n. 3
0
 /**
  * Checks if the user has provided enough money to purchase a product.
  *
  * @param pence Money provided by the user.
  * @param p The Product selected by the user.
  * @return True if there is enough money. False otherwise.
  */
 private boolean enoughMoneyProvided(int pence, Product p) {
   return pence - p.getProductPrice() >= 0;
 }