import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class VendingMachineApplet extends JApplet {
public void init() {
JPanel panel = new VendingMachinePanel();
this.add(panel);
setSize(470,350);
} // end init METHOD
} // end VendingMachineApplet CLASS
class VendingMachinePanel extends JPanel implements ActionListener {
VendingMachine v = new VendingMachine(1.30, 1.10, 0.90);
private JLabel depositLabel,
spaceLabel,
slot0Label,
slot1Label,
slot2Label,
slot3Label,
slot4Label,
slot5Label,
slot6Label,
slot7Label,
msgLabel;
private JTextField messageField;
private JButton depositQuarterButton,
depositDollarButton,
depositNickelButton,
depositDimeButton,
refundButton,
slot0Button,
slot1Button,
slot2Button,
slot3Button,
slot4Button,
slot5Button,
slot6Button,
slot7Button;
NumberFormat cf = NumberFormat.getCurrencyInstance();
public VendingMachinePanel() {
// set the layout mode for the panel
setLayout(new GridBagLayout());
// load the vending machine
v.load(0, new Beverage("Pepsi", v.CAPACITY));
v.load(1, new Beverage("Diet Pepsi", v.CAPACITY));
v.load(2, new Beverage("Mountain Dew", v.CAPACITY));
v.load(3, new Beverage("Dr. Pepper", v.CAPACITY));
v.load(4, new Beverage("Root Beer", v.CAPACITY));
v.load(5, new Beverage("Water", v.CAPACITY));
v.load(6, new Beverage("Lemonade", v.CAPACITY));
v.load(7, new Beverage("Seven-Up", v.CAPACITY));
//deposit nickel button
depositNickelButton = new JButton("Nickel");
setWidth(depositNickelButton, 100);
depositNickelButton.addActionListener(this);
this.add(depositNickelButton, getConstraints(0,0,1,1, GridBagConstraints.NORTH));
//deposit dime button
depositDimeButton = new JButton("Dime");
setWidth(depositDimeButton, 100);
depositDimeButton.addActionListener(this);
this.add(depositDimeButton, getConstraints(0,1,1,1, GridBagConstraints.WEST));
//deposit quarter button
depositQuarterButton = new JButton("Quarter");
setWidth(depositQuarterButton, 100);
depositQuarterButton.addActionListener(this);
this.add(depositQuarterButton, getConstraints(0,2,1,1, GridBagConstraints.WEST));
//deposit dollar button
depositDollarButton = new JButton("Dollar");
setWidth(depositDollarButton, 100);
depositDollarButton.addActionListener(this);
this.add(depositDollarButton, getConstraints(0,3,1,1, GridBagConstraints.WEST));
//deposited label
depositLabel = new JLabel(" Deposited");
this.add(depositLabel, getConstraints(0,4,1,1, GridBagConstraints.WEST));
// message field
messageField = new JTextField(6);
messageField.setEditable(false);
this.add(messageField, getConstraints(0,5,1,1, GridBagConstraints.CENTER));
//refund button
refundButton = new JButton("Refund");
setWidth(refundButton, 100);
refundButton.addActionListener(this);
this.add(refundButton, getConstraints(0,6,1,1, GridBagConstraints.WEST));
//slot 0 button
slot0Button = new JButton(v.getName(0));
setWidth(slot0Button, 120);
slot0Button.addActionListener(this);
this.add(slot0Button, getConstraints(2,0,1,1, GridBagConstraints.CENTER));
slot0Label = new JLabel("$1.30");
this.add(slot0Label, getConstraints(4,0,1,1, GridBagConstraints.EAST));
//slot 1 button
slot1Button = new JButton(v.getName(1));
setWidth(slot1Button, 120);
slot1Button.addActionListener(this);
this.add(slot1Button, getConstraints(2,1,1,1, GridBagConstraints.CENTER));
//slot 1 label
slot0Label = new JLabel("$1.30");
this.add(slot0Label, getConstraints(4,1,1,1, GridBagConstraints.EAST));
//slot 2 button
slot2Button = new JButton(v.getName(2));
setWidth(slot2Button, 120);
slot2Button.addActionListener(this);
this.add(slot2Button, getConstraints(2,2,1,1, GridBagConstraints.CENTER));
//slot 2 label
slot0Label = new JLabel("$1.30");
this.add(slot0Label, getConstraints(4,2,1,1, GridBagConstraints.EAST));
//slot 3 button
slot3Button = new JButton(v.getName(3));
setWidth(slot3Button, 120);
slot3Button.addActionListener(this);
this.add(slot3Button, getConstraints(2,3,1,1, GridBagConstraints.CENTER));
//slot 3 label
slot0Label = new JLabel("$1.30");
this.add(slot0Label, getConstraints(4,3,1,1, GridBagConstraints.EAST));
//slot 4 button
slot4Button = new JButton(v.getName(4));
setWidth(slot4Button, 120);
slot4Button.addActionListener(this);
this.add(slot4Button, getConstraints(2,4,1,1, GridBagConstraints.CENTER));
//slot 4 label
slot0Label = new JLabel("$1.30");
this.add(slot0Label, getConstraints(4,4,1,1, GridBagConstraints.EAST));
//slot 5 button
slot5Button = new JButton(v.getName(5));
setWidth(slot5Button, 120);
slot5Button.addActionListener(this);
this.add(slot5Button, getConstraints(2,6,1,1, GridBagConstraints.CENTER));
//slot 5 label
slot0Label = new JLabel("$1.30");
this.add(slot0Label, getConstraints(4,5,1,1, GridBagConstraints.EAST));
//slot 6 button
slot6Button = new JButton(v.getName(6));
setWidth(slot6Button, 120);
slot6Button.addActionListener(this);
this.add(slot6Button, getConstraints(2,7,1,1, GridBagConstraints.CENTER));
//slot 6 label
slot0Label = new JLabel("$0.90");
this.add(slot0Label, getConstraints(4,6,1,1, GridBagConstraints.EAST));
//slot 7 button
slot7Button = new JButton(v.getName(7));
setWidth(slot7Button, 120);
slot7Button.addActionListener(this);
this.add(slot7Button, getConstraints(2,5,1,1, GridBagConstraints.CENTER));
//slot 7 label
slot0Label = new JLabel("$1.10");
this.add(slot0Label, getConstraints(4,7,1,1, GridBagConstraints.EAST));
}
// a method for setting grid bag constraints
private GridBagConstraints getConstraints(int gridx, int gridy,
int gridwidth, int gridheight, int anchor) {
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.ipadx = 0;
c.ipady = 0;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.anchor = anchor;
return c;
} // end getConstraints METHOD
// a utility method to set the button sizes
private void setWidth(JComponent c, int width) {
c.setPreferredSize(new Dimension(width, 25));
} // end setWidth METHOD
public void setButtonEnabled(boolean b) {
slot0Button.setEnabled(b);
slot1Button.setEnabled(b);
slot2Button.setEnabled(b);
slot3Button.setEnabled(b);
slot4Button.setEnabled(b);
slot5Button.setEnabled(b);
slot6Button.setEnabled(b);
slot7Button.setEnabled(b);
} // end setButtonEnabled METHOD
public void setSlot6Enabled(boolean b) {
slot6Button.setEnabled(b);
}
public void setSlot5Enabled(boolean b) {
slot5Button.setEnabled(b);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == depositNickelButton) {
v.credit += 0.05;
if (v.credit >= v.price)
setButtonEnabled(true);
messageField.setText(cf.format(v.credit));
if (v.credit >= v.price2)
setSlot6Enabled(true);
messageField.setText(cf.format(v.credit));
if (v.credit >= v.price3)
setSlot5Enabled(true);
messageField.setText(cf.format(v.credit));
}
if (source == depositDimeButton) {
v.credit += 0.10;
if (v.credit >= v.price)
{
setButtonEnabled(true);
messageField.setText(cf.format(v.credit));
}
else if (v.credit >= v.price2)
{ setSlot6Enabled(true);
messageField.setText(cf.format(v.credit));
}
else if (v.credit >= v.price3)
{
setSlot5Enabled(true);
messageField.setText(cf.format(v.credit));
}
}
if (source == depositQuarterButton) {
v.credit += 0.25;
if (v.credit >= v.price)
setButtonEnabled(true);
messageField.setText(cf.format(v.credit));
if (v.credit >= v.price2)
setSlot6Enabled(true);
messageField.setText(cf.format(v.credit));
if (v.credit >= v.price3)
setSlot5Enabled(true);
messageField.setText(cf.format(v.credit));
} // end OUTER IF
if (source == depositDollarButton) {
v.credit += 1.00;
if (v.credit >= v.price)
setButtonEnabled(true);
messageField.setText(cf.format(v.credit));
if (v.credit >= v.price2)
setSlot6Enabled(true);
messageField.setText(cf.format(v.credit));
if (v.credit >= v.price3)
setSlot5Enabled(true);
messageField.setText(cf.format(v.credit));
} // end OUTER ELSE IF
else if (source == slot0Button)
order(0);
else if (source == slot1Button)
order(1);
else if (source == slot2Button)
order(2);
else if (source == slot3Button)
order(3);
else if (source == slot4Button)
order(4);
else if (source == slot5Button)
order(5);
else if (source == slot6Button)
order(6);
else if (source == slot7Button)
order(7);
else if (source == refundButton) {
double refund = v.refund();
messageField.setText(cf.format(0.0));
JOptionPane.showMessageDialog(this, "Please take your change of " + cf.format(refund));
setButtonEnabled(false);
} // end ELSE IF
} // end actionPerformed METHOD
public void order(int slot) {
if (v.orderBeverage(slot)) {
JOptionPane.showMessageDialog(this, "Enjoy your " + v.getName(slot));
messageField.setText(cf.format(v.credit));
if (v.credit < v.price)
setButtonEnabled(false);
} // end OUTER IF
else
JOptionPane.showMessageDialog(this, v.getName(slot) + " is out of stock");
//messageField.setText("Out of Stock");
} // end order METHOD
} // end VendingMachinePanel CLASS