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

Develop a class called VendingMachine that simulates an imaginary vending machin

ID: 3639016 • Letter: D

Question

Develop a class called VendingMachine that simulates an imaginary vending machine. A user can buy a bottle of water ($1.50), a bottle of coffee ($2.00), a chip ($1.00), and a chocolate bar ($2.50) using the machine. The user can buy severalitem(s) if they are available in the machine and should pay for the items with either a debit card (PIN: 7777) or a credit card (ZIP code: 93955).

Additionally, an administrator of the machine can reset and refill the machine.


-----------------------------------------------------------------------------

Demo Program: The following code presents a demo program that uses the VendingMachine class.

public class VendingMachineDemo
{
public static void main(String[] args)
{
VendingMachine machine1 = new VendingMachine();
VendingMachine machine2 = new VendingMachine(200, “Library”);
System.out.println("===== Welcome to the Vending Machine =====");
System.out.println(machine1);
System.out.println(machine2);
System.out.println(machine1.equals(machine2));
machine1.setNumber(100);
machine1.setLocation(“abc104”);
machine1.reset(5, 7, 3, 10); // In this method, we assume that a machine
// administrator can reset the contents of
// a machine.
machine1.displayMenu()
machine1.buyItems();
if(machine1.buyItems(1, 4) == false) {
System.exit(1);
}
machine1.returned(1, 2);
machine1.returned(2, 2);
machine1.buyItems(3, 5);
if(machine1.payment()) {
machine1.displayReceipt();
}
else {
System.out.println("Invalid payment.");
System.exit(1);
}
machine1.addItems(5,5,5,5); // A system admin cam add items to the machine
machine1.status();
System.out.println("===== Thank you! =====");
}
}

Explanation / Answer

In this program, you will develop a class called VendingMachine that simulates an imaginary vending machine. A user can buy a bottle of water ($1.50), a bottle of coffee ($2.00), a chip ($1.00), and a chocolate bar ($2.50) using the machine. The user can buy severalitem(s) if they are available in the machine and should pay for the items with either a debit card (PIN: 7777) or a credit card (ZIP code: 93955).

Additionally, an administrator of the machine can reset and refill the machine.

Demo Program: The following code presents a demo program that uses the VendingMachine class. If the program has an error, fix it.

public class VendingMachineDemo
{
public static void main(String[] args)
{
VendingMachine machine1 = new VendingMachine();
VendingMachine machine2 = new VendingMachine(200, “Library”);
System.out.println("===== Welcome to CSUMB Vending Machine =====");
System.out.println(machine1);
System.out.println(machine2);
System.out.println(machine1.equals(machine2));
machine1.setNumber(100);
machine1.setLocation(“abc104”);
machine1.reset(5, 7, 3, 10); // In this method, we assume that a machine
// administrator can reset the contents of
// a machine.
machine1.displayMenu()
machine1.buyItems();
if(machine1.buyItems(1, 4) == false) {
System.exit(1);
}
machine1.returned(1, 2);
machine1.returned(2, 2);
machine1.buyItems(3, 5);
if(machine1.payment()) {
machine1.displayReceipt();
}
else {
System.out.println("Invalid payment.");
System.exit(1);
}
machine1.addItems(5,5,5,5); // A system admin cam add items to the machine
machine1.status();
System.out.println("===== Thank you! =====");
}
}

------------------------------------------------------------------------------------------------
Sample Run of the Demo Program:
The following presents a sample result of the demo program.
===== Welcome to the Vending Machine =====
Serial Number: 0, Location: UNKNOWN, Water: 0, Coffee: 0, Sun Chip: 0 Chocolate Bar: 0
Serial Number: 200, Location: Library, Water: 0, Coffee: 0, Sun Chip: 0 Chocolate Bar: 0
false
Machine Reset:
Water: 5
Coffee: 7
Sun Chip: 3
Chocolate Bar: 10
===== Vending Machine Menu =====
1. Water............$1.50
2. Regular Coffee...$2.00
3. Sun Chip.........$1.00
4. Chocolate Bar....$2.50
Select Item: 2
How many do you want to buy? 2
You selected Regular Coffee (2)
You selected Water (4)
You selected Sun Chip (5) – Sorry. We don’t have enough Sun Chip.
Payment Option – Debit (1)/Credit (2): 1
Enter PIN: 7777
This is your receipt:
Water: $1.50 X 2 = $3.00
Tax (10.0%): $0.30
Total: $3.30
Machine Status
Serial Number: 100, Location: abc104
Sold: Water: 2 / Coffee: 0 / Sun Chip: 0 / Chocolate Bar: 0
Remaining: Water: 8 / Coffee: 12 / Sun Chip: 8 / Chocolate Bar: 15
Total Earning: $3.30
====================================================================
Also check these site, I hope that you will find here very useful information.

http://en.literateprograms.org/Vending_Machine_(java)

http://www.coursehero.com/keyword/vending-machine/

**********************************************Thank You**********************************************************