0% found this document useful (0 votes)
648 views

Vending Machine

1) The document defines a VendingMachine class that extends JFrame and implements ActionListener. 2) The class contains methods to create MoneyButton objects for coins and products, add them to panels, and handle button clicks. 3) When a coin button is clicked, the amount is added to a running total displayed in a text field. When a product button is clicked, the price is displayed in a label. 4) The "Done" button calculates change owed and displays it, or shows an error if insufficient funds.

Uploaded by

Arman Azmi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
648 views

Vending Machine

1) The document defines a VendingMachine class that extends JFrame and implements ActionListener. 2) The class contains methods to create MoneyButton objects for coins and products, add them to panels, and handle button clicks. 3) When a coin button is clicked, the amount is added to a running total displayed in a text field. When a product button is clicked, the price is displayed in a label. 4) The "Done" button calculates change owed and displays it, or shows an error if insufficient funds.

Uploaded by

Arman Azmi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.text.

DecimalFormat;

public class VendingMachine extends JFrame implements ActionListener { private class MoneyButton extends JButton { private double amount; private boolean isCoin; public MoneyButton(ActionListener listener, String text, double amount, boolean isCoin) { super(text); this.amount = amount; this.isCoin = isCoin; this.addActionListener(listener); } public MoneyButton(ActionListener listener, String text, double amount, boolean isCoin, String iconFileName) { super(text, new ImageIcon(iconFileName)); this.amount = amount; this.isCoin = isCoin; this.addActionListener(listener); } public double getAmount() { return this.amount; } public boolean isCoin() { return this.isCoin; } } JTextField txtMoney=new JTextField(10); JTextField txtChange=new JTextField(10); JTextField nothing=new JTextField(10); double counter; String counterConv; String StrPrice; DecimalFormat change=new DecimalFormat("#0.00");

private JButton getButton(String text, String command) { JButton button = new JButton(text); button.setActionCommand(command); button.addActionListener(this); return button; } private MoneyButton getCoinButton(String text, String iconFileName, double a mount) { return new MoneyButton(this, text, amount, true, iconFileName); }

private MoneyButton getProductButton(String text, String iconFileName, doubl e amount) { return new MoneyButton(this, text, amount, false, iconFileName); } private MoneyButton getProductButton(String text, double amount) { return new MoneyButton(this, text, amount, false); } public VendingMachine() { setLayout(new BorderLayout()); setSize(600, 400); setTitle("Title"); setResizable(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel left=new JPanel(new GridLayout(0,2)); left.add(getCoinButton("1p", "onep.gif", 0.01)); left.add(getCoinButton("2p", "twop.gif", 0.02)); left.add(getCoinButton("5p", "fivep.gif", 0.05)); left.add(getCoinButton("10p", "tenp.gif", 0.1)); left.add(getCoinButton("20p", "twentyp.gif", 0.2));

left.add(getCoinButton("50p", "fiftyp.gif", 0.5)); left.add(getCoinButton("1", "pound.gif", 1.0)); left.add(getCoinButton("2", "2pounds copy.gif", 2.0)); left.add(getButton("Done", "done")); add("West",left);

JPanel top=new JPanel(new FlowLayout()); top.add(new Label("COINS IN")); top.add(txtMoney); txtMoney.setEditable(false); // disable the ability to change the result top.add(new Label("CHANGE OUT")); top.add(txtChange); txtChange.setEditable(false);// disable the ability to change the result . top.add(getButton("Reset", "reset")); add("North",top);

JPanel right=new JPanel(new GridLayout(0,2)); right.add(getProductButton("Mars", "mars copy.gif", 0.5)); right.add(getProductButton("Snickers", 0.75)); right.add(getProductButton("Twix", 1.0)); right.add(getProductButton("M&M", 0.50)); right.add(getProductButton("Bounty", 1.50)); right.add(getProductButton("Skittles", 1.20)); add("East",right);

// label added with nothing written on JPanel middle=new JPanel(); nothing.setEditable(false);

middle.add(nothing); add("Center",middle);

setVisible(true); }

public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source instanceof MoneyButton) { MoneyButton button = (MoneyButton)source; if (button.isCoin()) { counter+=button.getAmount(); counterConv=String.valueOf(change.format(counter)); txtMoney.setText(counterConv); } else { StrPrice=String.valueOf(button.getAmount()); nothing.setText(StrPrice); } return; } else if (source instanceof JButton) { JButton button = (JButton)source; String cmd = button.getActionCommand(); if (cmd=="reset") { txtMoney.setText(""); txtChange.setText(""); counter=0; } else if (cmd=="done") { double total=0; double in=Double.parseDouble(txtMoney.getText()); double out=Double.parseDouble(nothing.getText());

if (in>=out) { total=in-out; } else if (in<out) { JOptionPane.showMessageDialog(null,"Not Enough"); txtMoney.setText(""); nothing.setText(""); txtChange.setText(""); } txtChange.setText(""+ change.format(total)); } } } }

You might also like