Advertisement
Guest User

CoinManager

a guest
Oct 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.80 KB | None | 0 0
  1. package greigmmyles.coins;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.util.ArrayList;
  6.  
  7. public class Driver
  8. {
  9.  
  10.     public static void main(String[] args)
  11.     {
  12.         //Variables
  13.         ArrayList<Coin> specificCoins = new ArrayList<Coin>();
  14.         ArrayList<Coin> coins = new ArrayList<Coin>();
  15.         BigDecimal totalValue = new BigDecimal(3.80); //The value of £3.80 was selected as it should use one of every coin, to empty all coins, use the value £38.00
  16.  
  17.         /*
  18.          * Creating specific coins that will be used to compare later in the
  19.          * program
  20.          */
  21.         specificCoins.add(new Coin(2.0));
  22.         specificCoins.add(new Coin(1.0));
  23.         specificCoins.add(new Coin(0.50));
  24.         specificCoins.add(new Coin(0.20));
  25.         specificCoins.add(new Coin(0.10));
  26.  
  27.         /*
  28.          * A for loop to get the stock of coins from start
  29.          */
  30.         for (int i = 0; i < 10; i++) {
  31.             for (int j = 0; j < specificCoins.size(); j++) {
  32.                 coins.add(specificCoins.get(j));
  33.             }
  34.         }
  35.  
  36.         System.out.println(coins.size());
  37.  
  38.         /**
  39.          * A try catch that has a nested do while loop, which loops through
  40.          * until the totalValue is < 0 or if the NoCoins exception is thrown.
  41.          */
  42.         try {
  43.             do {
  44.                 totalValue = dispenseChange(totalValue, coins, specificCoins);
  45.             }
  46.             while (totalValue.doubleValue() > 0);
  47.         }
  48.         catch (IllegalArgumentException e) {
  49.             System.out.println(e.getMessage());
  50.         }
  51.         System.out.println("Remaining Balance : £" + totalValue);
  52.         //        System.out.println(coins.size()); FOR DEBUG PURPOSES
  53.     }
  54.  
  55.     /**
  56.      * The algorithm that checks to see if there is balance left and then
  57.      * dispenses the coins. This uses an if statement check. Checking if there
  58.      * is an amount left and if there is a specific coin in the ArrayList that
  59.      * can be dispensed If there is less than an amount left, it will move on.
  60.      * As only certain coins can be inserted (via buttons) there should never be
  61.      * a remainder of < 10p. If there is also no coins of that value (ie £2)
  62.      * then it will also move on to the next statement.
  63.      *
  64.      * It is possible to run out of coins, though come the main program I will
  65.      * be fixing that so that it will be checking if it has enough change every
  66.      * time a coin is entered. If there is a remainder the NoCoins exception
  67.      * will be thrown. If it is zero then the program will exit.
  68.      *
  69.      * @param totalValue
  70.      * @param coins
  71.      * @param specificCoins
  72.      * @return the totalValue which will have either the remainder or be zero.
  73.      */
  74.     private static BigDecimal dispenseChange(BigDecimal totalValue, ArrayList<Coin> coins,
  75.             ArrayList<Coin> specificCoins)
  76.     {
  77.         if (totalValue.doubleValue() >= 2.00 && coins.contains(specificCoins.get(0))) {
  78.             BigDecimal twoPounds = new BigDecimal(2.00);
  79.             if (dispenseTwoPound(coins)) {
  80.                 totalValue = totalValue.subtract(twoPounds);
  81.             }
  82.  
  83.         }
  84.         else if (totalValue.doubleValue() >= 1.00 && coins.contains(specificCoins.get(1))) {
  85.             BigDecimal onePound = new BigDecimal(1.00);
  86.  
  87.             if (dispenseOnePound(coins)) {
  88.                 totalValue = totalValue.subtract(onePound);
  89.             }
  90.         }
  91.         else if (totalValue.doubleValue() >= 0.50 && coins.contains(specificCoins.get(2))) {
  92.             BigDecimal fiftyPence = new BigDecimal(0.50);
  93.  
  94.             if (dispenseFiftyPence(coins)) {
  95.                 totalValue = totalValue.subtract(fiftyPence);
  96.             }
  97.         }
  98.         else if (totalValue.doubleValue() >= 0.20 && coins.contains(specificCoins.get(3))) {
  99.             BigDecimal twentyPence = new BigDecimal(0.20);
  100.  
  101.             if (dispenseTwentyPence(coins)) {
  102.                 totalValue = totalValue.subtract(twentyPence);
  103.             }
  104.         }
  105.         else if (totalValue.doubleValue() >= 0.10 && coins.contains(specificCoins.get(4))) {
  106.             BigDecimal tenPence = new BigDecimal(0.10);
  107.  
  108.             if (dispenseTenPence(coins)) {
  109.                 totalValue = totalValue.subtract(tenPence);
  110.             }
  111.         }
  112.  
  113.         else {
  114.             NoCoinsException();
  115.             return totalValue.setScale(2, RoundingMode.HALF_EVEN);
  116.         }
  117.  
  118.         return totalValue.setScale(2, RoundingMode.HALF_EVEN);
  119.     }
  120.  
  121.     /**
  122.      *
  123.      * @param coins
  124.      * @return if the coin has been dispensed
  125.      */
  126.     private static boolean dispenseTenPence(ArrayList<Coin> coins)
  127.     {
  128.         for (Coin coin : coins) {
  129.             if (coin.getCoinValue() == 0.10) {
  130.                 coins.remove(coin);
  131.                 System.out.println("Dispensed 10p coin");
  132.                 return true;
  133.             }
  134.         }
  135.         return false;
  136.     }
  137.  
  138.     /**
  139.      *
  140.      * @param coins
  141.      * @return if the coin has been dispensed
  142.      */
  143.     private static boolean dispenseTwentyPence(ArrayList<Coin> coins)
  144.     {
  145.         for (Coin coin : coins) {
  146.             if (coin.getCoinValue() == 0.20) {
  147.                 coins.remove(coin);
  148.                 System.out.println("Dispensed 20p coin");
  149.                 return true;
  150.             }
  151.         }
  152.         return false;
  153.     }
  154.  
  155.     /**
  156.      *
  157.      * @param coins
  158.      * @return if the coin has been dispensed
  159.      */
  160.     private static boolean dispenseFiftyPence(ArrayList<Coin> coins)
  161.     {
  162.         for (Coin coin : coins) {
  163.             if (coin.getCoinValue() == 0.50) {
  164.                 coins.remove(coin);
  165.                 System.out.println("Dispensed 50p coin");
  166.                 return true;
  167.             }
  168.         }
  169.         return false;
  170.     }
  171.  
  172.     private static boolean dispenseOnePound(ArrayList<Coin> coins)
  173.     {
  174.         for (Coin coin : coins) {
  175.             if (coin.getCoinValue() == 1) {
  176.                 coins.remove(coin);
  177.                 System.out.println("Dispensed £1 coin");
  178.                 return true;
  179.             }
  180.         }
  181.         return false;
  182.     }
  183.  
  184.     /**
  185.      *
  186.      * @param coins
  187.      * @return if the coin has been dispensed
  188.      */
  189.     private static boolean dispenseTwoPound(ArrayList<Coin> coins)
  190.     {
  191.         for (int i = 0; i < coins.size(); i++) {
  192.             if (coins.get(i).getCoinValue() == 2) {
  193.                 coins.remove(i);
  194.                 System.out.println("Dispensed £2 coin");
  195.                 return true;
  196.             }
  197.         }
  198.         return false;
  199.     }
  200.  
  201.     /**
  202.      * A custom excpetion that is thrown if there are no coins left to dispense
  203.      */
  204.     private static void NoCoinsException()
  205.     {
  206.         throw new IllegalArgumentException("There are no coins that can refund the change");
  207.  
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement