Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Java program using two classes

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java program using two classes

    hi,
    im new to java and to this site so it's good to meet u all
    ok now with my problem, the problem is that i need to set u a add coin system which seems simple but my problem is i have to do it using 2 classes a money class which has the code and then a gui with the buton and a text box on but i dont know what to put on the gui side i think ive got the money class done
    any help with this problem would be great cos ive had this problem now for a couple of days

    this is my money class

    public class Credit {
     
    	private float credit;
     
    	public Credit(){
    		credit = 0;
    		}
     
    	public float addCredit(){
     
    		credit = + 10;
     
    		return credit;
     
    	}
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: using 2 classes

    Hello AZBOY2000 and welcome to the Java Programming Forums

    Its good to meet you too.

    Here is an example of how to create a Swing GUI with a botton. When the button is clicked, an action is performed:

    http://www.javaprogrammingforums.com...ava-swing.html

    You should be able to easily modify this to suite your needs.

    If you get stuck, please post back and I will assist you.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using 2 classes

    hi yeah thanks for the help but i still dont really understand it do u an example more like im trying to do if it helps here is the GUI i have at the moment
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
     
    import javax.swing.*;
     
     
    public class VendingMachineGUI extends JFrame  implements ActionListener{
     
    	private JTextArea displayArea;
    	private JTextField CoinTextField;
    	private JButton CoinButton, saveButton, displayAllItemsButton, findItemButton, updateButton, PurchaseButton;
    	private ArrayList <Dispenser> listRecords;
    	private int counter;
    	private Credit c;
     
    	private Dispenser vendingitems[];
     
    	public static void main(String[] args)
    	{
    		new VendingMachineGUI();
    	}
     
    	public VendingMachineGUI(){
     
    		setLayout(new FlowLayout());
     
    		CoinTextField = new JTextField(20);
     
    		CoinButton = new JButton("Add Coin");
    		CoinButton.setForeground(Color.blue);
     
    		saveButton = new JButton("Save Item");
    		saveButton.setForeground(Color.red);
    		displayAllItemsButton = new JButton("Display All Items!");
    		displayAllItemsButton.setForeground(Color.blue);
    		findItemButton = new JButton("Find Item ?");
    		updateButton = new JButton("Update List");
    		updateButton.setForeground(Color.blue);
     
    		displayArea = new JTextArea();
    		displayArea.setPreferredSize(new Dimension(400, 200));
    		displayArea.setBackground(Color.lightGray);
     
    		PurchaseButton = new JButton("Purchase Items!");
    		PurchaseButton.setForeground(Color.blue);
     
    		c = new Credit();
     
    		add(CoinButton); add(CoinTextField); add(saveButton); add(displayAllItemsButton); add(displayArea);
    		add(findItemButton); add(updateButton); add(PurchaseButton);
    		saveButton.addActionListener(this); displayAllItemsButton.addActionListener(this);
    		findItemButton.addActionListener(this);
    		updateButton.addActionListener(this);
     
    		displayAllItemsButton.setEnabled(false);
    		findItemButton.setEnabled(false);
    		updateButton.setEnabled(false);
     
    		setTitle("*** Vending Machine ****");
    		setSize(450, 500);
    		setVisible(true);
     
    		CoinTextField.setText("0.0");
    		vendingitems = new Dispenser[8];
     
    		counter =0;
     
    	}
     
    	public void actionPerformed(ActionEvent event){
    		if (event.getSource()==CoinButton){
     
    			float totalCredit = c.addCredit();}
    			CoinTextField.setText(Float.toString(totalCredit ));
    }
    }

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: using 2 classes

    Is this your entire code? There are a few errors in the VendingMachineGUI class which are stopping me from compiling it.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using 2 classes

    sos i forgot to post my dispenser class, i havnt added this to the gui yet so i think u can take the gui as all wrong idealy i just need the add coin thing working and then i can build the dispenser in
    import javax.swing.JOptionPane;
     
     
    public class Dispencer {
     
    	private String itemID;
    	private String itemName; 
        private int itemPrice;  
        private int itemQuantity;
        boolean items;
     
        public Dispencer(String id, String in, int ip, int iq)
        {
        	itemID = id;
        	itemName = in;
            itemPrice  = ip;
            itemQuantity = iq;
        }
     
        public String getItemID()
        {
        	return(itemID);
        }
     
        public String getItemName()
        {
        	return(itemName);
        }
     
        public int getItemPrice()
        {
        	return(itemPrice);
        }
     
        public int getItemQuantity()
        {
        	return(itemQuantity);
        }
     
        public void replaceItemQuantity(int iq)
        {
            itemQuantity = iq;
    	}
     
        public void replaceItemPrice(int ip)
        {
            itemPrice  = ip;
        }
     
        public String getItemValues()
        {
        	if (!itemID.equals(""))
        		return "Item ID: " + itemID + "Item Name: " + itemName + "  Price: " + itemPrice + " Quantity:" + itemQuantity + "\n";
        	else
        		return("");
        }
     
        public boolean itemsLeft(){
     
     
    		if ((itemQuantity <1))
    			items = false;
    		else
    			items = true;
     
    		return (items);	
    	}
     
        public void dispenceItem()
        {
    		if (itemQuantity >0)
    			itemQuantity = itemQuantity - 1;
    		else
    		{
    			JOptionPane.showMessageDialog(null, "No items left");
    		} 
    	}
     
     
    }

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: using 2 classes

    OK I have a version of this saved now that will compile.

    I can see your GUI... What are you expecting to happen exactly? Please tell me how you would like the program to work.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Junior Member
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using 2 classes

    well ideally id like it to vend items so select items store them in the panal with an over all amount at the side and then click a purchase button which displays items have vended and your chage is if there is any but i think i know how to solve that, im just stuck at the easy part which is were i always get stuck at, so idealy could u help me with the add coins stuff

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: using 2 classes

    Try updating the VendingMachineGUI class with this:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.*;
     
    public class VendingMachineGUI extends JFrame{
     
        private JTextArea displayArea;
        private JTextField CoinTextField;
        private JButton CoinButton, saveButton, displayAllItemsButton,
                findItemButton, updateButton, PurchaseButton;
        private ArrayList<Dispenser> listRecords;
        private int counter;
        private Credit c;
     
        public float totalCredit;
     
        private Dispenser vendingitems[];
     
        public static void main(String[] args) {
            new VendingMachineGUI();
        }
     
        public VendingMachineGUI() {
     
            setLayout(new FlowLayout());
     
            CoinTextField = new JTextField(20);
     
            CoinButton = new JButton("Add Coin");
            CoinButton.setForeground(Color.blue);
     
            saveButton = new JButton("Save Item");
            saveButton.setForeground(Color.red);
            displayAllItemsButton = new JButton("Display All Items!");
            displayAllItemsButton.setForeground(Color.blue);
            findItemButton = new JButton("Find Item ?");
            updateButton = new JButton("Update List");
            updateButton.setForeground(Color.blue);
     
            displayArea = new JTextArea();
            displayArea.setPreferredSize(new Dimension(400, 200));
            displayArea.setBackground(Color.lightGray);
     
            PurchaseButton = new JButton("Purchase Items!");
            PurchaseButton.setForeground(Color.blue);
     
            c = new Credit();
     
            add(CoinButton);
            add(CoinTextField);
            add(saveButton);
            add(displayAllItemsButton);
            add(displayArea);
            add(findItemButton);
            add(updateButton);
            add(PurchaseButton);
    //        saveButton.addActionListener(this);
    //        displayAllItemsButton.addActionListener(this);
    //        findItemButton.addActionListener(this);
    //        updateButton.addActionListener(this);
     
            displayAllItemsButton.setEnabled(false);
            findItemButton.setEnabled(false);
            updateButton.setEnabled(false);
     
            setTitle("*** Vending Machine ****");
            setSize(450, 500);
            setVisible(true);
     
            CoinTextField.setText("0.0");
            vendingitems = new Dispenser[8];
     
            counter = 0;
     
     
            CoinButton.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
                    totalCredit = c.addCredit();
                    //Execute when button is pressed
                    //System.out.println("Coin button pressed");
                    CoinTextField.setText(Float.toString(totalCredit));
                }
            });  
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Replies: 6
    Last Post: May 15th, 2009, 06:06 PM
  2. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 03:57 PM
  3. Why output is displaying 0 for calculation in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2009, 02:18 PM
  4. Problem in merging two java classes
    By madkris in forum Object Oriented Programming
    Replies: 11
    Last Post: March 16th, 2009, 10:02 AM
  5. Java program with abstract class along with two subclasses
    By crazydeo in forum Collections and Generics
    Replies: 2
    Last Post: June 10th, 2008, 12:45 PM