Create a Swing program called VendingApp.java that has a GUI similar to the foll
ID: 3641856 • Letter: C
Question
Create a Swing program called VendingApp.java that has a GUI similar to the following one:
Specifications
• This is a Swing application that simulates a vending machine that dispenses soft drinks.
• The Quarter button deposits $0.25.
• The Dollar button deposits $1.00.
• The label after the Dollar button indicates the current credit.
• The Refund button refunds the customer’s credit.
• The slot buttons display the name of the product currently loaded in one of the vending machine’s six slots. The example above shows that the vending machine is loaded with Pepsi, Diet Pepsi, Mountain Dew, Dr. Pepper, Root Beer, and Water.
• When the program starts, the six slot buttons are disabled. The program enables these buttons when the user has a credit of at least $1.00 (the cost of one item).
• If the user clicks one of the slot buttons, the program displays a dialog box that tells the user to enjoy the beverage. For example, “Enjoy your Diet Pepsi.”
• If the user clicks Refund, the program displays a dialog box that asks the user to take his or her change. For example, “Please take your change of $1.25.”
• You could add other classes you wish to use for this application. But the main should be VendingApp.java.
• Each slot in the vending machine can hold up to 2 bottles. The application should keep track of how many bottles are available in each slot and display a dialog box with the message “Out of Stock” if the user selects a beverage that’s out of stock. Or if you are able to do, you can make the beverage’s button disabled if the user selects a beverage that’s out of stock.
Explanation / Answer
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class VendingApp extends JFrame {
// Constructor
public VendingApp() {
initComponents();
}
// Constructor
// Init method
private void initComponents() {
this.setSize(WIDTH, HEIGHT);
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Vending Machine");
buildAddPanel();
buildRefundPanel();
buildSlotsPanel();
this.add(slotsPanel);
this.add(addPanel);
this.add(refundPanel);
}
// Init method
// Panel builders
private void buildAddPanel() {
addPanel = new JPanel();
addQuarterBtn = new JButton("Quarter");
addDollarBtn = new JButton("Dollar");
currentCents = 0;
currentCreditLbl = new JLabel();
updateCurrentCredit();
addQuarterBtn.setActionCommand("25");
addDollarBtn.setActionCommand("100");
addQuarterBtn.addActionListener(new ButtonAddListener());
addDollarBtn.addActionListener(new ButtonAddListener());
addPanel.add(addQuarterBtn);
addPanel.add(addDollarBtn);
addPanel.add(currentCreditLbl);
}
private void buildRefundPanel() {
refundPanel = new JPanel();
refundBtn = new JButton("Refund");
refundBtn.addActionListener(new ButtonRefundListener());
refundPanel.add(refundBtn);
}
private void buildSlotsPanel() {
slotsPanel = new JPanel();
slotNames = new String[MAX_SLOTS];
slotButtons = new JButton[MAX_SLOTS];
currentSlotAmounts = new int[MAX_SLOTS];
slotPrices = new int[MAX_SLOTS];
slotNames[0] = "Pepsi";
slotNames[1] = "Diet Pepsi";
slotNames[2] = "Mountain Dew";
slotNames[3] = "Dr. Pepper";
slotNames[4] = "Root Beer";
slotNames[5] = "Water";
for (int i = 0; i < MAX_SLOTS; i++) {
slotButtons[i] = new JButton(slotNames[i]);
slotButtons[i].setEnabled(false);
slotButtons[i].setActionCommand("" + i);
slotButtons[i].addActionListener(new ButtonGetDrinkListener());
currentSlotAmounts[i] = MAX_SLOT_AMT;
slotPrices[i] = DEFAULT_PRICE;
slotsPanel.add(slotButtons[i]);
}
}
// Panel builders
// ActionListener implementations for addButtons, refundButton and slotButtons
private class ButtonAddListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
addToCurrentCents(Integer.parseInt(e.getActionCommand()));
updateCurrentCredit();
updateSlotAvails();
}
}
private class ButtonRefundListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int cents = getCurrentCents();
String change = getCurrentCredit();
if (cents != 0) {
addToCurrentCents(cents * -1);
updateCurrentCredit();
updateSlotAvails();
JOptionPane.showMessageDialog(
rootPane, "Please take your change of " + change,
"Refund", MessageType.INFO.ordinal());
}
}
}
private class ButtonGetDrinkListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int slotIndex = Integer.parseInt(e.getActionCommand());
addToCurrentCents(slotPrices[slotIndex] * -1);
decrementSlotAmount(slotIndex);
updateCurrentCredit();
updateSlotAvails();
JOptionPane.showMessageDialog(
rootPane, "Enjoy your " + slotNames[slotIndex] + "!",
"Beverage", MessageType.INFO.ordinal());
}
}
// ActionListener implementations for addButtons, refundButton and slotButtons
// Private methods to help ActionListener implementations
private int getCurrentCents() {
return this.currentCents;
}
private String getCurrentCredit() {
int dollars = currentCents / 100;
int cents = currentCents % 100;
return "$" + dollars + "." + (cents > 9 ? ("" + cents) : ("0" + cents));
}
private void addToCurrentCents(int amount) {
this.currentCents += amount;
}
private void decrementSlotAmount(int slotIndex) {
if (currentSlotAmounts[slotIndex] > 0) {
currentSlotAmounts[slotIndex]--;
}
}
private void updateCurrentCredit() {
currentCreditLbl.setText(getCurrentCredit());
}
private void updateSlotAvails() {
for (int i = 0; i < MAX_SLOTS; i++) {
slotButtons[i].setEnabled(currentCents >= slotPrices[i] && currentSlotAmounts[i] > 0);
}
}
// Private methods to help ActionListener implementations
// Variables declaration
private final int WIDTH = 600;
private final int HEIGHT = 200;
private final int MAX_SLOTS = 6;
private final int MAX_SLOT_AMT = 2;
private final int DEFAULT_PRICE = 100;
private JPanel addPanel;
private JPanel refundPanel;
private JPanel slotsPanel;
private JButton addQuarterBtn;
private JButton addDollarBtn;
private int currentCents;
private JLabel currentCreditLbl;
private JButton refundBtn;
private String[] slotNames;
private JButton[] slotButtons;
private int[] currentSlotAmounts;
private int[] slotPrices;
// Variables declaration
// Main
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new VendingApp().setVisible(true);
}
});
}
}