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

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 {

private HashMap inventory = new HashMap();

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

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!