223k views
4 votes
Write a program that simulates the functionality of a vending machine having the following characteristics:• The vending machine offers 5 products• The vending machine accepts coins, 1 dollar bills, and 5 dollar bills• The change is always given in coins, with maximum possible number of coins in each value: 25, 10, 5 or 1 cent.• The selections available for user are numbers from 1 to 5.• The user enters the money – simulate the action through a loop that ends when the user enters 0. Each coin, or paper bill will be read individually.• The user makes the selection, and the machine allows a maximum 4 other selections if the amount entered doesn’t cover the price of the item.• Once an item is delivered, the machine gives the change in coins.• There is no increment for the money during one selection.• The user can stop the selection at any time by entering 0 for the product selection.• If the user chooses to cancel the selection, the machine returns the initial amount in coins.• Display the outcome of the operation for each alternative you consider possible for the vending machine.• Make sure that the machine returns the correct change at all times.

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

import java.util.*;

public class VendingMachineAssignement {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

double item1 = 1.25;

double item2 = .75;

double item3 = .90;

double item4 = .75;

double item5 = 1.50;

double item6 = .75;

System.out.print("Enter an item number: ");

int item = keyboard.nextInt();

System.out.print("Enter the amount paid: ");

double paid = keyboard.nextDouble();

if (item == 2 || item == 4 || item == 6)

{

if (paid >= item2)

{

System.out.println("Thank you for buying item " + item + ", your change is $" + (paid-item2) + ". Please come again!");

}

if (paid < item2)

{

System.out.println("Please insert another " + "$" + (item2-paid));

}

}

else if (item == 1)

{

if (paid >= item1)

{

System.out.println("Thank you for buying item " + item + ", your change is $" + (paid-item1) + ". Please come again!");

}

if (paid < item1)

{

System.out.println("Please insert another " + "$" + (item1-paid));

}

}

else if (item == 3)

{

if (paid >= item3)

{

System.out.println("Thank you for buying item " + item + ", your change is $" + (paid-item3) + ". Please come again!");

}

if (paid < item3)

{

System.out.println("Please insert another " + "$" + (item3-paid));

}

}

else if (item == 5)

{

if (paid >= item5)

{

System.out.println("Thank you for buying item " + item + ", your change is $" + (paid-item5) + ". Please come again!");

}

if (paid < item5)

{

System.out.println("Please insert another " + "$" + (item5-paid));

}

}

}

}

User Nodm
by
6.0k points