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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started