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

In this program, you will develop a class called VendingMachine that simulates a

ID: 3638832 • Letter: I

Question

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
===== Thank you! =====

Explanation / Answer

001 importjava.awt.*; 002 importjavax.swing.*; 003 importjava.awt.event.*; 004 importjava.text.DecimalFormat; 005 006 publicclassVendingMachine extendsJFrameimplementsActionListener { 007     privateclassMoneyButton extendsJButton { 008         privatedoubleamount; 009         privatebooleanisCoin; 010         publicMoneyButton(ActionListener listener, String text, doubleamount, booleanisCoin) { 011             super(text); 012             this.amount = amount; 013             this.isCoin = isCoin; 014             this.addActionListener(listener); 015         } 016         publicMoneyButton(ActionListener listener, String text, doubleamount, booleanisCoin, String iconFileName) { 017             super(text, newImageIcon(iconFileName)); 018             this.amount = amount; 019             this.isCoin = isCoin; 020             this.addActionListener(listener); 021         } 022         publicdoublegetAmount() { returnthis.amount; } 023         publicbooleanisCoin() { returnthis.isCoin; } 024     } 025 026     JTextField txtMoney=newJTextField(10); 027     JTextField txtChange=newJTextField(10); 028     JTextField nothing=newJTextField(10); 029 030     doublecounter; 031     String counterConv; 032     String StrPrice; 033 034     DecimalFormat change=newDecimalFormat("#0.00"); 035 036     privateJButton getButton(String text, String command) { 037         JButton button = newJButton(text); 038         button.setActionCommand(command); 039         button.addActionListener(this); 040         returnbutton; 041     } 042 043     privateMoneyButton getCoinButton(String text, String iconFileName, doubleamount) { 044         returnnewMoneyButton(this, text, amount, true, iconFileName); 045     } 046      047     privateMoneyButton getProductButton(String text, String iconFileName, doubleamount) { 048         returnnewMoneyButton(this, text, amount, false, iconFileName); 049     } 050 051     privateMoneyButton getProductButton(String text, doubleamount) { 052         returnnewMoneyButton(this, text, amount, false); 053     } 054      055     publicVendingMachine() { 056         setLayout(newBorderLayout()); 057         setSize(600, 400); 058         setTitle("Title"); 059         setResizable(true); 060         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 061 062         JPanel left=newJPanel(newGridLayout(0,2)); 063         left.add(getCoinButton("1p","onep.gif", 0.01)); 064         left.add(getCoinButton("2p","twop.gif", 0.02)); 065         left.add(getCoinButton("5p","fivep.gif", 0.05)); 066         left.add(getCoinButton("10p","tenp.gif", 0.1)); 067         left.add(getCoinButton("20p","twentyp.gif", 0.2)); 068         left.add(getCoinButton("50p","fiftyp.gif", 0.5)); 069         left.add(getCoinButton("£1","pound.gif", 1.0)); 070         left.add(getCoinButton("£2", "2pounds copy.gif", 2.0)); 071         left.add(getButton("Done", "done")); 072         add("West",left); 073 074         JPanel top=newJPanel(newFlowLayout()); 075         top.add(newLabel("COINS IN")); 076         top.add(txtMoney); 077         txtMoney.setEditable(false); // disable the ability to change the result 078         top.add(newLabel("CHANGE OUT")); 079         top.add(txtChange); 080         txtChange.setEditable(false);// disable the ability to change the result. 081         top.add(getButton("Reset", "reset")); 082         add("North",top); 083 084         JPanel right=newJPanel(newGridLayout(0,2)); 085         right.add(getProductButton("Mars","mars copy.gif", 0.5)); 086         right.add(getProductButton("Snickers",0.75)); 087         right.add(getProductButton("Twix",1.0)); 088         right.add(getProductButton("M&M",0.50)); 089         right.add(getProductButton("Bounty",1.50)); 090         right.add(getProductButton("Skittles",1.20)); 091         add("East",right); 092 093 094         // label added with nothing written on 095         JPanel middle=newJPanel(); 096         nothing.setEditable(false); 097         middle.add(nothing); 098         add("Center",middle); 099 100         setVisible(true); 101     } 102 103 104     publicvoidactionPerformed(ActionEvent e) { 105         Object source = e.getSource(); 106         if(source instanceofMoneyButton) { 107             MoneyButton button = (MoneyButton)source; 108             if(button.isCoin()) { 109                 counter+=button.getAmount(); 110                 counterConv=String.valueOf(change.format(counter)); 111                 txtMoney.setText(counterConv); 112             } else{ 113                 StrPrice=String.valueOf(button.getAmount()); 114                 nothing.setText(StrPrice); 115             } 116             return; 117         } elseif(source instanceofJButton) { 118             JButton button = (JButton)source; 119             String cmd = button.getActionCommand(); 120             if(cmd=="reset") { 121                 txtMoney.setText(""); 122                 txtChange.setText(""); 123                 counter=0; 124             } elseif(cmd=="done") { 125                 doubletotal=0; 126                 doublein=Double.parseDouble(txtMoney.getText()); 127                 doubleout=Double.parseDouble(nothing.getText()); 128                 if(in>=out) { 129                     total=in-out; 130                 } elseif(in
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