Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a JAVA program that simulates a vending machine. Users select a product an

ID: 2247117 • 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:

1. Create a class called CashRegister that has methods to totals up sales and computer change due.

2. Create a class called MonetaryUnit that has methods to returns the name of the monetary unit and the monetary value of the unit.

3. Test your designing classes by running the main methods below.

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

CashRegisterTester.java

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");
}
}

CashRegister.java

import java.text.DecimalFormat;

public class CashRegister {

private double amount;

private static double purchase;

public void recordPurchase(double d) {

amount = d;

}

public void enterPayment(int numOfItems, MonetaryUnit m) {

purchase = purchase + numOfItems * m.getAmount();

}

public double giveChange() {

DecimalFormat df = new DecimalFormat("0.00");

return Double.parseDouble(df.format(purchase - amount));

}

}

MonetaryUnit.java

public class MonetaryUnit {

private double amount;

private String moneyType;

public MonetaryUnit(double d, String s) {

this.amount = d;

this.moneyType = s;

}

public double getAmount() {

return amount;

}

public String getMoneyType() {

return moneyType;

}

}

Output:

Change: 0.03
Expected: 0.03

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote