Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java simulate a vending machine that Accepts bills of 1,5,10 Allow user to se

ID: 3841416 • Letter: I

Question

In Java simulate a vending machine that

Accepts bills of 1,5,10

Allow user to select products Water(1), Soda(2), Monster(3)

Allow user to take refund by canceling the request.

Needs generics and to implment an interface

Return selected product and remaining change if any....

This is what I have so far

public enum Product {

  

   WATER("Coke", 1), SODA("Pepsi", 2), MONSTER("Soda", 3);

   private String name;

   private int price;

   private Product(String name, int price) {

       this.name = name;

       this.price = price;

   }

   public String getName() {

       return name;

   }

   public double getPrice() {

       return price;

   }

}

public class Inventory<T> {

  

   private HashMap<T, Integer> inventory = new HashMap<T, Integer>();

public int getQuantity(T product) {

Integer quanity = inventory.get(product);

return quanity == null ? 0 : quanity;

}

public void add(T product) {

int quanity = inventory.get(product);

inventory.put(product, quanity + 1);

}

public void deduct(T product) {

if (hasItem(product)) {

int quanity = inventory.get(product);

inventory.put(product, quanity - 1);

}

}

public boolean hasItem(T product) {

return getQuantity(product) > 0;

}

public void clear() {

inventory.clear();

}

public void put(T product, int quantity) {

inventory.put(product, quantity);

}

}

Explanation / Answer

This is VendingMachine interface:

package com.vendingMachine1;

import java.util.List;

public interface VendingMachine {

   public long selectItemAndGetPrice(Product item);
   public void insertCoin(Coin coin);
   public List<Coin> refund();
   public Bucket<Product, List<Coin>> collectItemAndChange();
   public void reset();

  
}

This is a VendingMachineImpl class which implements all method of VendingMachine interface:

package com.vendingMachine1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class VendingMachineImpl implements VendingMachine{

   private Inventory<Coin> cashInventory = new Inventory<Coin>();
private Inventory<Product> itemInventory = new Inventory<Product>();
private long totalSales;
private Product 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(Product i : Product.values()){
itemInventory.put(i, 5);
}

}

@Override
public long selectItemAndGetPrice(Product 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<Product, List<Coin>> collectItemAndChange() {
   Product item = collectItem();
totalSales = totalSales + currentItem.getPrice();

List<Coin> change = collectChange();

return new Bucket<Product, List<Coin>>(item, change);
}

private Product 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<Coin> collectChange() {
long changeAmount = currentBalance - currentItem.getPrice();
List<Coin> change = getChange(changeAmount);
updateCashInventory(change);
currentBalance = 0;
currentItem = null;
return change;
}

@Override
public List<Coin> refund(){
List<Coin> refund = getChange(currentBalance);
updateCashInventory(refund);
currentBalance = 0;
currentItem = null;
return refund;
}


private boolean isFullPaid() {
if(currentBalance >= currentItem.getPrice()){
return true;
}
return false;
}

  
private List<Coin> getChange(long amount) throws NotSufficientChangeException{
List<Coin> changes = Collections.EMPTY_LIST;

if(amount > 0){
changes = new ArrayList<Coin>();
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<Coin> change) {
for(Coin c : change){
cashInventory.deduct(c);
}
}

public long getTotalSales(){
return totalSales;
}

}

This is a VendingMachineFactory class:

package com.vendingMachine1;

public class VendingMachineFactory {
  
   public static VendingMachine createVendingMachine() {
       return new VendingMachineImpl();
       }

  
}

This is Product enum:

package com.vendingMachine;

public enum Product {

   WATER("Coke", 1), SODA("Pepsi", 2), MONSTER("Soda", 3);
     
   private String name;
   private int price;
     
   private Product(String name, int price) {
   this.name = name;
   this.price = price;
   }
     
   public String getName() {
   return name;
   }
     
   public long getPrice() {
   return price;
   }
}

This is a Coin enum:

package com.vendingMachine1;

public enum Coin {

   PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
   private int denomination;
   private Coin(int denomination){
       this.denomination = denomination;
       }
   public int getDenomination(){
       return denomination;
       }

  
}

This is Inventory class:

package com.vendingMachine;

import java.util.HashMap;

public class Inventory<T> {
   private HashMap<T, Integer> inventory = new HashMap<T, Integer>();
     
public int getQuantity(T product) {
Integer quanity = inventory.get(product);
return quanity == null ? 0 : quanity;
}

public void add(T product) {
int quanity = inventory.get(product);
inventory.put(product, quanity + 1);
}

public void deduct(T product) {
if (hasItem(product)) {
int quanity = inventory.get(product);
inventory.put(product, quanity - 1);
}
}

public boolean hasItem(T product) {
return getQuantity(product) > 0;
}

public void clear() {
inventory.clear();
}

public void put(T product, int quantity) {
inventory.put(product, quantity);
}
  
}

This is our custom ececption NotFullPaidException class:

package com.vendingMachine1;

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;
       }

  
}

This is our custom ececption NotSufficientChangeException class:

package com.vendingMachine1;

public class NotSufficientChangeException extends RuntimeException{

   private String message;
   public NotSufficientChangeException(String string) {
       this.message = string;
       }
   @Override
   public String getMessage(){
       return message;
       }

  
}

This is our custom ececption SoldOutException class:

package com.vendingMachine1;

public class SoldOutException extends RuntimeException {

   private String message;
   public SoldOutException(String string) {
       this.message = string;
       }
   @Override
   public String getMessage(){
       return message;
       }

  
}

This is Sample class:

package com.vendingMachine1;

public class Sample {

   int total=0;
   public int input(int coins)
   {
   switch(coins)
   {
   case 1:
   total = total +1;
   break;
   case 5:
   total = total + 5;
   break;
   case 10:
   total = total + 10;
   break;
  
   default :
   System.out.println("Wrong Input");
   break;
   }
   return 0;
   }

   public int select(int choice)
   {

   switch(choice)
   {
  
   case 1:
   System.out.println("You have selected COKE(1)");
   total = total - 1;
   System.out.println("Your Remaining change: "+total);
   break;
   case 2:
   System.out.println("You have selected PEPSI(2)");
   total = total - 2;
   System.out.println("Your Remaining change: "+total);
   break;
   case 3:
   System.out.println("You have selected SODA(3)");
   total = total - 3;
   System.out.println("Your Remaining change: "+total);
   break;
   case 4:
   System.out.println("You have cancelled your Item.");
   total = total - 0;
   System.out.println("Your Remaining change: "+total);
   break;
   case 5:
   VendingTest.main(null);
   break;
  
   case 6:System.out.println("Thank you...bye");
       System.exit(0);
       break;

   default:
   System.out.println("Wrong Choice");

   }
   return 0;
   }

  
}

This is Driver class:

package com.vendingMachine1;

import java.util.Scanner;

public class VendingTest {

  
   public static void main(String args[])
   {
   Sample sp = new Sample();
   Scanner sc = new Scanner(System.in);
     
   while(true){
       System.out.println(" Welcome to Vending Machine.");
       System.out.println("Put coins in the denomination of: 1,5,10");
       int coins = sc.nextInt();
       sp.input(coins);
       System.out.println("Select one of the Product: ");
      
       System.out.println("1.COKE(1)");
       System.out.println("2.PEPSI(2)");
       System.out.println("3.SODA(3)");
       System.out.println("4.CANCEL");
       System.out.println("5.RESET");
       System.out.println("6.EXIT");
       int choice = sc.nextInt();
       sp.select(choice);
   }
  

   }
  
  
}

Output:

Welcome to Vending Machine.
Put coins in the denomination of: 1,5,10
10
Select one of the Product:

1.COKE(1)
2.PEPSI(2)
3.SODA(3)
4.CANCEL
5.RESET
6.EXIT
3
You have selected SODA(3)
Your Remaining change: 7

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote