*This program needs to simulate a vending machine. In java using this code a ven
ID: 3841377 • Letter: #
Question
*This program needs to simulate a vending machine.
In java using this code a vending machine program that uses an Interface.
public class Inventory {
private Map inventory = new HashMap();
public int getQuantity(T product) {
Integer value = inventory.get(product);
return value == null ? 0 : value;
}
public void add(T product) {
int count = inventory.get(product);
inventory.put(product, count + 1);
}
public void deduct(T product) {
if (hasItem(product)) {
int count = inventory.get(product);
inventory.put(product, count - 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);
}
}
public enum Product {
COKE("Coke", 25), PEPSI("Pepsi", 35), SODA("Soda", 45);
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;
}
}
Explanation / Answer
Solution and Coding
My implementation of Java Vending Machine has following classes and interfaces :
VendingMachine
It defines the public API of vending machine, usually all high-level functionality should go in this class
VendingMachineImpl
Sample implementation of Vending Machine
VendingMachineFactory
A Factory class to create different kinds of Vending Machine
Item
Java Enum to represent Item served by Vending Machine
Inventory
Java class to represent an Inventory, used for creating case and item inventory inside Vending Machine
Coin
Another Java Enum to represent Coins supported by Vending Machine
Bucket
A parameterized class to hold two objects. It's kind of Pair class.
NotFullPaidException
An Exception thrown by Vending Machine when a user tries to collect an item, without paying the full amount.
NotSufficientChangeException
Vending Machine throws this exception to indicate that it doesn't have sufficient change to complete this request.
SoldOutExcepiton
Vending Machine throws this exception if the user request for a product which is sold out.
Code:
VendingMachine.java
The public API of vending machine, usually all high-level functionality should go in this class
VendingMachineImpl.java
A sample implementation of VendingMachine interface represents a real world Vending Machine , which you see in your office, bus stand, railway station and public places.
VendingMachineFactory.java
A Factory class to create different kinds of Vending Machine
Item.java
Java Enum to represent Item served by Vending Machine
Coin.java
Another Java Enum to represent Coins supported by Vending Machine
Inventory.java
A Java class to represent an Inventory, used for creating case and item inventory inside Vending Machine.
Bucket.java
A parameterized utility class to hold two objects.
NotFullPaidException.java
An Exception, thrown by Vending Machine when a user tries to collect an item, without paying the full amount.
NotSufficientChangeException.java
Vending Machine throws this exception to indicate that it doesn't have sufficient change to complete this request.
SoldOutException.java
The Vending Machine throws this exception if the user request for a product which is sold out
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.