Question: Write a java program that simulates a vending machine. Products can be purchased by inserting coins with a value at least equal to the cost

Write a java program that simulates a vending machine. Products can be purchased by inserting coins with a value at least equal to the cost of the product. A user select a product from a list of available products, add coins, and either gets the products or gets the coins returned. The coins are returned if insufficient money was supplied or if the product it's sold out. The machine does not give change if too much money was added. Products can be restocked and money remove by an operator. Your solution should include a class VendingMachine that is not coupled with the Scanner or PrintStream classes.

Runtime output

S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit a Description: Cookie Price: 1.50 Quantity: 3

S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit A Description: Candy Price: 1.25 Quantity: 2

S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit S Cookie @ $1.50 Candy @ $1.25

S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit b A) Cookie @ $1.50 B) Candy @ $1.25 a Insufficient money

S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit i A) nickel @ 0.05 B) dime @ 0.1 C) quarter @ 0.25 D) dollar @ 1.0 d

S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit i A) nickel @ 0.05 B) dime @ 0.1 C) quarter @ 0.25 D) dollar @ 1.0 d

S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit b A) Cookie @ $1.500 B) Candy @ $1.25 a

Purchased: Cookie @ $1.50 Your change: $0.50 S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit s

Cookie @ $1.50 Candy @ $1.25 S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit b

A) Cookie @ $1.50 B) Candy @ $1.25 b

Insufficient money S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit i

A) nickel @ 0.05 B) dime @ 0.1 C) quarter @ 0.25 D) dollar @ 1.0 d

S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit i A) nickel @ 0.05 B) dime @ 0.1 C) quarter @ 0.25 D) dollar @ 1.0 c

S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit b A) Cookie @ $1.50 B) Candy @ $1.25 b

Purchased: Candy @ $1.25 S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit r

Removed: $2.75

--Here is the code I have so far(skeleton code.) Please help me finish it BASED ON THIS CODE I have!! Thank you!!--

public class Coins {

String typeofcoin;

double value;

}

public class Coinset {

ArrayList set = new ArrayList();

public void addcoins(Coins coin) { //add coin to Coins(array)

set.add(coin);

}

public class Product {

String description;

double price;

int quantity;

public Product(String description, double price, int quantity) {

this.description = description;

this.price = price;

this.quantity = quantity;

}

public boolean equals(object o) {

//search for the product you want in the vending machine

}

}

public class Vendingmachine { //should know about the coin and product

ArrayList products = new ArrayList<>(); //goods in machine

Coinset currentcoin = new Coinset(); //this will be current coin in machine

public void addCoin(Coins c) { //add to current coins by user

currentcoin.addcoins(c);

}

public void addProduct(Product product) {

products.add(product); //add product to bendingmaching by user

}

public void buyProduct(Product p) { //loop through all products to find a match p

if(.equals(p)) {

//get price of product

//compare with payment

if(price of product < payment) {

//remove product from products

//add customer coinset to vending machine coinset

//remove all coins in customer coinset

}

}

}

void addProduct(Product p, int quantity) {

for(int i =0; i

products.add(p);

}

}

public double removeMoney() {

//return momey from vending machine

}

static Product[] getProductType() {

ArrayListtypes = new ArrayList<>();

for(Product p) {

if(!types.contains(p)) {

types.add(p);

Product[]p = new Product[];

}

}

for(int i=o; i

p[i]=types.set(i);

}

}

}

public class VendingmachineMenu {

void run(Vendingmachine v) {

Scanner input = new Scanner(System.in);

char ch = input.nextLine().charAt(0);

int count = 0;

if(ch == 'A' || ch == 'a')

{

System.out.println("Product: ");

String pro = input.nextLine();

System.out.println("Price: ");

double price = input.nextDouble();

System.out.println("Quantity:");

int quantity = input.nextInt();

input.nextLine();

}

else if(ch == 'S' || ch == 's') {

Vendingmachine.getProductType();

for(Product p; Vendingmachine.getProductType()) {

System.out.print(p);

}

}

else if(ch == 'I' || ch == 'i')

{

System.out.println("A) nickel @ 0.05");

System.out.println("B) dime @ 0.1");

System.out.println("C) quarter @ 0.25");

System.out.println("D) dollar @ 1.0");

ch = input.nextLine().charAt(0);

if(ch == 'A' || ch == 'a')

Vendingmachine.addcoins(getCoins());

else if(ch == 'B' || ch == 'b')

else if(ch == 'C' || ch == 'd')

else if(ch == 'D' || ch == 'd')

}

else if(ch == 'B' || ch == 'b')

{

for(int i = 0; i < count; i++)

//show choice of products

ch = input.nextLine().charAt(0);

else if(ch == 'R' || ch == 'r')

{

//remove coins

System.out.print("Removed: "+/**totalcoins*/);

}

else if(ch != 'Q' && ch != 'q')

{

System.out.println("Invalid choice");

}

}

public class Test {

public static void main(String[] args) {

Vendingmachine v = new Vendingmachine();

VendingmachineMenu m = new VendingmachineMenu();

m.run(v);

}

}

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!