import java.awt.*; import javax.swing.*; import java.awt.event.*; public class FoodMachine extends JFrame implements ActionListener { Icon ginger_icon = new ImageIcon("ginger.gif"); Icon toffee_icon = new ImageIcon("toffee.gif"); Icon jumbo_icon = new ImageIcon("jumbo.gif"); Icon cashew_icon = new ImageIcon("cashew.gif"); Icon omega_icon = new ImageIcon("omega.gif"); Icon candy_icon = new ImageIcon("candy.gif"); Icon onep_icon = new ImageIcon("onep.gif"); Icon twop_icon = new ImageIcon("twop.gif"); Icon fivep_icon = new ImageIcon("fivep.gif"); Icon tenp_icon = new ImageIcon("tenp.gif"); Icon twentyp_icon = new ImageIcon("twentyp.gif"); Icon fiftyp_icon = new ImageIcon("fiftyp.gif"); Icon pound_icon = new ImageIcon("pound.gif"); Icon empty_icon = new ImageIcon("empty.gif"); JPanel moneyPanel = new JPanel(); JButton ginger = new JButton(ginger_icon); JButton toffee = new JButton(toffee_icon); JButton jumbo = new JButton(jumbo_icon); JButton cashew = new JButton(cashew_icon); JButton omega = new JButton(omega_icon); JButton candy = new JButton(candy_icon); JButton onep = new JButton(onep_icon); JButton twop = new JButton(twop_icon); JButton fivep = new JButton(fivep_icon); JButton tenp = new JButton(tenp_icon); JButton twentyp = new JButton(twentyp_icon); JButton fiftyp = new JButton(fiftyp_icon); JButton pound = new JButton(pound_icon); JPanel aPanel = new JPanel(); JButton amount = new JButton("0.00"); JButton refund = new JButton("Return"); JButton reset = new JButton("Reset"); int ginger_cnt = 10; int toffee_cnt = 10; int jumbo_cnt = 10; int cashew_cnt = 10; int omega_cnt = 10; int candy_cnt = 10; int cash = 0; public FoodMachine() { setTitle("Food Machine"); getContentPane().setLayout(new GridLayout(2, 2, 2, 2)); moneyPanel.setLayout(new GridLayout(3, 2, 2, 2)); moneyPanel.add(onep); moneyPanel.add(twop); moneyPanel.add(fivep); moneyPanel.add(tenp); moneyPanel.add(twentyp); moneyPanel.add(fiftyp); moneyPanel.add(pound); onep.setActionCommand("1"); onep.addActionListener(this); twop.setActionCommand("2"); twop.addActionListener(this); fivep.setActionCommand("5"); fivep.addActionListener(this); tenp.setActionCommand("10"); tenp.addActionListener(this); twentyp.setActionCommand("20"); twentyp.addActionListener(this); fiftyp.setActionCommand("50"); fiftyp.addActionListener(this); pound.setActionCommand("100"); pound.addActionListener(this); amount.setForeground(Color.red); getContentPane().add(moneyPanel); aPanel.setLayout(new BorderLayout()); aPanel.add(amount, BorderLayout.WEST); reset.setForeground(Color.green); reset.setActionCommand("reset"); reset.addActionListener(this); aPanel.add(reset, BorderLayout.CENTER); refund.setForeground(Color.blue); refund.setActionCommand("refund"); refund.addActionListener(this); aPanel.add(refund, BorderLayout.EAST); getContentPane().add(aPanel); ginger.setActionCommand("ginger"); ginger.addActionListener(this); toffee.setActionCommand("toffee"); toffee.addActionListener(this); jumbo.setActionCommand("jumbo"); jumbo.addActionListener(this); cashew.setActionCommand("cashew"); cashew.addActionListener(this); omega.setActionCommand("omega"); omega.addActionListener(this); candy.setActionCommand("candy"); candy.addActionListener(this); getContentPane().add(ginger); getContentPane().add(toffee); getContentPane().add(jumbo); getContentPane().add(cashew); getContentPane().add(omega); getContentPane().add(candy); pack (); this.addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent e) { FoodMachine.this.windowClosed(); } } ); } public void actionPerformed(java.awt.event.ActionEvent e) { GingerFrame gin; ChangeFrame chg; String cmd= e.getActionCommand(); if(cmd.equals("1")) { cash += 1; } else if(cmd.equals("2")) { cash += 2; } else if(cmd.equals("5")) { cash += 5; } else if(cmd.equals("10")) { cash += 10; } else if(cmd.equals("20")) { cash += 20; } else if(cmd.equals("50")) { cash += 50; } else if(cmd.equals("100")) { cash += 100; } else if(cmd.equals("ginger")) { System.out.println("ginger pressed"); gin = new GingerFrame(1); cash -= 50; if(ginger_cnt == 0) { ginger.setIcon(empty_icon); } } else if(cmd.equals("toffee")) { System.out.println("toffee pressed"); gin = new GingerFrame(2); cash -= 70; if(toffee_cnt == 0) { toffee.setIcon(empty_icon); } } else if(cmd.equals("jumbo")) { System.out.println("jumbo pressed"); gin = new GingerFrame(3); cash -= 100; if(jumbo_cnt == 0) { jumbo.setIcon(empty_icon); } } else if(cmd.equals("cashew")) { System.out.println("cashew pressed"); gin = new GingerFrame(4); cash -= 120; if(cashew_cnt == 0) { cashew.setIcon(empty_icon); } } else if(cmd.equals("omega")) { System.out.println("omega pressed"); gin = new GingerFrame(5); cash -= 150; if(omega_cnt == 0) { omega.setIcon(empty_icon); } } else if (cmd.equals("candy")) { System.out.println("candy pressed"); gin = new GingerFrame(6); cash -= 100; if(candy_cnt== 0) { candy.setIcon(empty_icon); } } else if(cmd.equals("refund")) { System.out.println("refund pressed"); if(cash > 0) { chg = new ChangeFrame(cash); cash = 0; } } else if (cmd.equals("reset")) { System.out.println("reset pressed"); if(cash < 0) { cash= 0; } } int pds, pns; pds = cash / 100; pns = cash % 100; String spns = new Integer(pns).toString(); if(spns.length() == 1) spns = "0" + spns; amount.setText(new Integer(pds).toString() + "." + spns); } protected void windowClosed() { System.exit(0); } public static void main(String[] args) { FoodMachine frame = new FoodMachine(); frame.show(); } }