Question: In java, write a driver class to test this code mport java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class VendingMachineImpl implements VendingMachine
In java, write a driver class to test this code
mport java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map;
public class VendingMachineImpl implements VendingMachine { private Inventory cashInventory = new Inventory(); private Inventory- itemInventory = new Inventory
- (); private long totalSales; private Item currentItem; private long currentBalance; public VendingMachineImpl(){ initialize(); } private void initialize(){ //initialize machine with 5 coins of each denomination //and 5 cans of each Item for(Coin c : Coin.values()){ cashInventory.put(c, 5); } for(Item i : Item.values()){ itemInventory.put(i, 5); } } @Override public long selectItemAndGetPrice(Item item) { if(itemInventory.hasItem(item)){ currentItem = item; return currentItem.getPrice(); } throw new SoldOutException("Sold Out, Please buy another item"); }
@Override public void insertCoin(Coin coin) { currentBalance = currentBalance + coin.getDenomination(); cashInventory.add(coin); }
@Override public Bucket- > collectItemAndChange() { Item item = collectItem(); totalSales = totalSales + currentItem.getPrice(); List change = collectChange(); return new Bucket
- >(item, change); } private Item collectItem() throws NotSufficientChangeException, NotFullPaidException{ if(isFullPaid()){ if(hasSufficientChange()){ itemInventory.deduct(currentItem); return currentItem; } throw new NotSufficientChangeException("Not Sufficient change in Inventory"); } long remainingBalance = currentItem.getPrice() - currentBalance; throw new NotFullPaidException("Price not full paid, remaining : ", remainingBalance); } private List collectChange() { long changeAmount = currentBalance - currentItem.getPrice(); List change = getChange(changeAmount); updateCashInventory(change); currentBalance = 0; currentItem = null; return change; } @Override public List refund(){ List refund = getChange(currentBalance); updateCashInventory(refund); currentBalance = 0; currentItem = null; return refund; } private boolean isFullPaid() { if(currentBalance >= currentItem.getPrice()){ return true; } return false; }
private List getChange(long amount) throws NotSufficientChangeException{ List changes = Collections.EMPTY_LIST; if(amount > 0){ changes = new ArrayList(); long balance = amount; while(balance > 0){ if(balance >= Coin.QUARTER.getDenomination() && cashInventory.hasItem(Coin.QUARTER)){ changes.add(Coin.QUARTER); balance = balance - Coin.QUARTER.getDenomination(); continue; }else if(balance >= Coin.DIME.getDenomination() && cashInventory.hasItem(Coin.DIME)) { changes.add(Coin.DIME); balance = balance - Coin.DIME.getDenomination(); continue; }else if(balance >= Coin.NICKLE.getDenomination() && cashInventory.hasItem(Coin.NICKLE)) { changes.add(Coin.NICKLE); balance = balance - Coin.NICKLE.getDenomination(); continue; }else if(balance >= Coin.PENNY.getDenomination() && cashInventory.hasItem(Coin.PENNY)) { changes.add(Coin.PENNY); balance = balance - Coin.PENNY.getDenomination(); continue; }else{ throw new NotSufficientChangeException("NotSufficientChange, Please try another product"); } } } return changes; } @Override public void reset(){ cashInventory.clear(); itemInventory.clear(); totalSales = 0; currentItem = null; currentBalance = 0; } public void printStats(){ System.out.println("Total Sales : " + totalSales); System.out.println("Current Item Inventory : " + itemInventory); System.out.println("Current Cash Inventory : " + cashInventory); } private boolean hasSufficientChange(){ return hasSufficientChangeForAmount(currentBalance - currentItem.getPrice()); } private boolean hasSufficientChangeForAmount(long amount){ boolean hasChange = true; try{ getChange(amount); }catch(NotSufficientChangeException nsce){ return hasChange = false; } return hasChange; }
private void updateCashInventory(List change) { for(Coin c : change){ cashInventory.deduct(c); } } public long getTotalSales(){ return totalSales; } }
class VendingMachineFactory { public static VendingMachine createVendingMachine() { return new VendingMachineImpl(); } }
enum Item { COKE("Coke", 25), PEPSI("Pepsi", 35), SODA("Soda", 45); private String name; private int price;
private Item(String name, int price) { this.name = name; this.price = price; }
public String getName() { return name; }
public long getPrice() { return price; } }
enum Coin { PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
class Inventory { private Map inventory = new HashMap();
public int getQuantity(T item) { Integer value = inventory.get(item); return value == null ? 0 : value; }
public void add(T item) { int count = inventory.get(item); inventory.put(item, count + 1); }
public void deduct(T item) { if (hasItem(item)) { int count = inventory.get(item); inventory.put(item, count - 1); } }
public boolean hasItem(T item) { return getQuantity(item) > 0; }
public void clear() { inventory.clear(); }
public void put(T item, int quantity) { inventory.put(item, quantity); } }
package com.chegg.vending.test;
public class NotFullPaidException extends RuntimeException { private String message; private long remaining;
public NotFullPaidException(String message, long remaining) { this.message = message; this.remaining = remaining; }
public long getRemaining() { return remaining; }
@Override public String getMessage() { return message + remaining; } }
class NotSufficientChangeException extends RuntimeException { private String message;
public NotSufficientChangeException(String string) { this.message = string; }
@Override public String getMessage() { return message; } }
class SoldOutException extends RuntimeException { private String message;
public SoldOutException(String string) { this.message = string; }
@Override public String getMessage() { return message; } }
private int denomination;
private Coin(int denomination) { this.denomination = denomination; }
public int getDenomination() { return denomination; } }
public interface VendingMachine { public long selectItemAndGetPrice(Item item); public void insertCoin(Coin coin); public List refund(); public Bucket- > collectItemAndChange(); public void reset(); }