*This program needs to simulate a vending machine. *Needs a smartcardreader clas
ID: 3841354 • Letter: #
Question
*This program needs to simulate a vending machine.
*Needs a smartcardreader class to pay, a vendingMachine interface and a VendingMachine Implememtation class.
*Customer walks to vending machine, pays, checks payment, then recieves product
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
Here the required classes and modified classes as well provided comments to make you understand
Enum Product:
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;
}
}
Inventory.java
import java.util.*;
public class Inventory {
private Map inventory = new HashMap();
public int getQuantity(Product product) {
Integer value = (Integer)inventory.get(product);
return value == null ? 0 : value;
}
public void add(Product product) {
int count = (int)inventory.get(product);
inventory.put(product, count + 1);
}
public void deduct(Product product) {
if (hasItem(product)) {
int count =(int) inventory.get(product);
inventory.put(product, count - 1);
}
}
public boolean hasItem(Product product) {
return getQuantity(product) > 0;
}
public void clear() {
inventory.clear();
}
public void put(Product product, int quantity) {
inventory.put(product, quantity);
}
}
VendingMachine.java
// VendingMachine interface
public interface VendingMachine {
// Adds item to the machine for a customer
public void addProduct(Product product);
// removes the product from the item
public void removeProduct(Product product);
// Processing payment
public void processPayment(SmartCardReader card);
}
VendingMachineImp.java
import java.util.Scanner;
// Implementation class of vending machine
public class VendingMachineImp implements VendingMachine {
// Custoer name
String customerName;
// And inventory of the customer
Inventory inventory;
//Cunstroctor to create object
public VendingMachineImp(String customerName,Inventory inventory)
{
this.inventory=inventory;
this.customerName=customerName;
}
// To add product to the item
public void addProduct(Product product){
inventory.add(product);
}
// To remove the product from the list
public void removeProduct(Product product){
inventory.deduct(product);
}
// Processing payment using OTP verification
public void processPayment(SmartCardReader card){
String otp=card.generateOTP();
System.out.println("Enter the OTP "+otp);
String uotp=new Scanner(System.in).next();
if(card.verifyCard(uotp))
{
System.out.println("success");
}
}
// Getters and Setters for customer name and inventory
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public Inventory getInventory() {
return inventory;
}
public void setInventory(Inventory inventory) {
this.inventory = inventory;
}
}
SmartCardReader.java
import java.util.Random;
// SmartCardReader class
public class SmartCardReader {
// OTP for card process
// Just for understanding the process
//Since we are not going to performe actual processing
String OTP;
// Method to verify the OTP
public boolean verifyCard(String OTP)
{
if(this.OTP.equals(OTP))
{
return true;
}
else
{
return false;
}
}
// Generates the otp
public String generateOTP()
{
OTP="";
// Using Random class to generate otp
Random rm=new Random();
for(int i=1;i<=6;i++)
{
OTP=OTP+rm.nextInt(10);
}
return OTP;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.