Write a JAVA program that simulates a vending machine. Users select a product an
ID: 2247098 • Letter: W
Question
Write a JAVA program that simulates a vending machine. Users select a product and provide payment. If the payment is sufficient to cover the purchase price of the product, the product is dispensed and change is given. Otherwise, the payment is returned to the user.
Requirements:
Create a class called CashRegister that has methods to totals up sales and computer change due.
Create a class called MonetaryUnit that has methods to returns the name of the monetary unit and the monetary value of the unit.
Test your designing classes by running the main methods below.
/**
This program tests the CashRegister class.
*/
public class CashRegisterTester
{
public static void main(String[] args)
{
final double NICKEL_VALUE = 0.05;
final double DIME_VALUE = 0.1;
final double QUARTER_VALUE = 0.25;
final double DOLLAR_VALUE = 1.0;
CashRegister myRegister = new CashRegister();
myRegister.recordPurchase(1.82);
myRegister.enterPayment(1, new MonetaryUnit(DOLLAR_VALUE, "dollar bill"));
myRegister.enterPayment(3, new MonetaryUnit(QUARTER_VALUE, "quarter"));
myRegister.enterPayment(2, new MonetaryUnit(NICKEL_VALUE, "nickel"));
double myChange = myRegister.giveChange();
System.out.println("Change: " + myChange);
System.out.println("Expected: 0.03");
}
}
Explanation / Answer
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));
}
}
}
}
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));
}
}
...
if (paid >= (prices[item - 1]))
{
System.out.println("Thank you for buying item " + item + ", your change is " + (paid - prices[item - 1]) + ". Please come again!");
}
keyboard.close();
import java.text.NumberFormat;
import java.util.Scanner;
public class Test
{
public static void main(String... args)
{
Scanner keyboard = new Scanner(System.in);
double[] prices = new double[] { 1.25, 0.75, 0.9, 0.75, 1.5, 0.75 };
int item = 0;
System.out.print("Enter an item number: ");
item = Integer.parseInt(keyboard.nextLine());
if (!(item <= prices.length))
{
System.out.println("Sorry, we don't have that item in stock.");
keyboard.close();
return;
}
System.out.print("Enter the amount paid: ");
double paid = Double.parseDouble(keyboard.nextLine());
while (paid < (prices[item - 1]))
{
System.out.print("You need " + NumberFormat.getCurrencyInstance().format(Math.abs((paid - prices[item - 1]))) + " more for item " + item);
paid += Double.parseDouble(keyboard.nextLine());
}
System.out.println("Thank you for buying item " + item + ", your change is " + NumberFormat.getCurrencyInstance().format((paid - prices[item - 1])) + ". Please come again!");
keyboard.close();
}
}
double [] prices = {1.25, 0.75, 0.90, 0.75, 1.50, 0.75};
System.out.print("Enter an item number: ");
int item = keyboard.nextInt();
System.out.print("Enter the amount paid: ");
double paid = keyboard.nextDouble();
if (paid >= prices[item-1])
System.out.println("Thank you for buying item " + item + ", your change is $" + (paid-prices[item-1]) + ". Please come again!");
else
System.out.println("Please insert another " + "$" + (prices[item-1]-paid));
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double[] itemPrices = {1.25, .75, .90, .75, 1.50, .75};
int maxItem = itemPrices.length;
do {
System.out.print("Enter an item number: ");
try {
item = keyboard.nextInt();
if ( item >= 1 && item <= maxItem ) {
itemFound = true;
} else {
String message = String.format("Unfortunately, item #%d does not exist.", item);
System.out.println(message);
}
} catch (InputMismatchException e) {
System.out.println("Bad input. Try again.");
keyboard.next();
}
} while (!itemFound);
Double paid = null;
do {
System.out.print("Enter the amount paid: ");
try {
paid = keyboard.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Bad input. Try again.");
keyboard.next();
}
} while (paid == null);
double itemValue = itemPrices[item-1];
double change = paid - itemValue;
double moneyNeeded = 0 - change;
if ( moneyNeeded > 0 ) {
String message = String.format("Please insert another $%.2f.%n", moneyNeeded);
System.out.println(message);
System.exit(0);
}
String message = String.format("Thank you for buying item #%d, your change is $%.2f. Please come again!", item, change);
System.out.println(message);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.