Vending Machine Classes & Methods

pass objects as arguments

Page 1 of 1

3 Replies - 13222 Views - Last Post: 06 January 2008 - 08:44 AM Rate Topic: -----

#1 Guillen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 11-December 07

Vending Machine Classes & Methods

Post icon  Posted 28 December 2007 - 08:55 AM

Hi everyone, I have been instructed to build a vending machine, and I have seen some examples of vending machines and they have all a good amount of stuff in common but what I am looking for more specifically is how to create a method (in a different class) with a button or a JTextField as an argument. It would help me simplify my code so I dont have about 200 lines of code on the main page. The full code for it is attached below and there is the orignal vending machine and the class I intend to use for my calculations(*).

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Vending Machine Classes & Methods

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2071
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Vending Machine Classes & Methods

Posted 29 December 2007 - 06:58 AM

Is this what you mean:

public void method1( JButton x ) {
}

public void method2( JTextField x ) {
}

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5944
  • View blog
  • Posts: 12,874
  • Joined: 16-October 07

Re: Vending Machine Classes & Methods

Posted 29 December 2007 - 11:31 AM

I'm not sure if this answers your question. In the interests of cleaning up code, I refactored a little.

Basically, this code does the same as Workout2, but in less than half the calls. One thing was to include a method to create a JButton, add it to the form's actionListener, and set the command. I then saw the huge amount of repetition in the actionListener and cleaned it but extending JButton to include so custom properties.

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 amount) {
		return new MoneyButton(this, text, amount, true, iconFileName);
	}
	
	private MoneyButton getProductButton(String text, String iconFileName, double 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));
			}
		}
	}
}


Hope this helps.

This post has been edited by baavgai: 29 December 2007 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guillen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 11-December 07

Re: Vending Machine Classes & Methods

Posted 06 January 2008 - 08:44 AM

THANKS, ITS EXACTLY WHAT I NEEDED.

This post has been edited by Guillen: 06 January 2008 - 08:45 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1