Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions