Coin Sorting Machine 1 Write a java class called CoinStackj
Coin Sorting Machine ---------------------------
1. Write a java class called CoinStack.java, which implements CoinStackInterface
2. Write your application that read coins (three data files are provided for testing purpose): Construct 4 stacks (for penny, nickel, dime, and quarter stack), read coins from the data file then push to their corresponding stacks. For example, if the value is 10, then you need to push that coin to dime stack.
3. Use the static method print in the PrintCoinStacks class to print sorted coins. The print method takes 4 CoinStackInterface as its arguments. The signature of this method is as follows: print(CoinStackInterface, CoinStackInterface , CoinStackInterface ,CoinStackInterface );
Solution
Coin sorting:
import java.math.BigDecimal;
 import java.math.MathContext;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Scanner;
public class CoinSort {
public static void main(String[] args) {
       
 Scanner scan = new Scanner(System.in);
 System.out.println(\"Enter Amount to pa \");
       
 Value price = scan.nextBigDecimal
       
 System.out.println(\"Paid amount \");
       
 Value paidamount = scan.nextBigDecimal();
       
 Map<Denom, Integer> changeDue = getDenomination(paidamount, price);
for(Denom denomination : changeDue.keySet()) {
 System.out.println(+ denomination + \" value\"+ changeDue.get(denomination));
 }
 scan.close();
 }
public static Map<Denom, Integer> getDenomination(Value paidamount, Value price) {
 Value changeamount = amountPayed.subtract(price);
 System.out.println(\"change\"+ changeamount);
       
 Map<Denom, Integer> returnchange = new LinkedHashMap<Denom, Integer>();
       
 for(Denom denomination : Denom.values()) {
Value[] values = changeamount.divideAndRemainder(denomination.value, MathContext.DECIMAL32);
           
 if(!values[0].equals(Value.valueOf(0.0)) && !values[1].equals(Value.valueOf(0.0))) {
 returnchange.put(denomination, values[0].intValue());
 changeamount = values [1];
 }
}
 return returnchange;
 }
enum Denom {
 HUNDRED(Value.valueOf(100)),
 ONE(Value.valueOf(1)),
 QUARTER(Value.valueOf(0.25)),
 DIME(Value.valueOf(0.10)),
 NICKEL(Value.valueOf(0.5)),
 PENNY(Value.valueOf(0.1));
private Value value;
Denom(Value value) {
 this.value = value;
 }
 }
 }


