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 4 of 4

Thread: Using values set in one class in another

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Using values set in one class in another

    Okay I have 3 classes. One of them called player has an array list with two methods that can add and delete things from the array list. Another is called shop. The player array list has values set and removed within this class using the player methods. After that class is executed it goes back to the main class. I decided to print out the player array list but it gets reset to nothing after you buy something in a shop. Is there any way to keep the values from the shop class and bring them over to the other classes? If you need it heres my code: *EDIT* Just to make this clear I know i'm making a new player object. I need to know how to set it to the same values as the shops player object.

    mainMenu.java
    public class mainMenu {
     
    	public void start() {
     
    		shop shop = new shop();
    		mine mine = new mine();
    		userInput input = new userInput();
    		player player = new player();
     
    		System.out.println("Welcome to the game!");
    		System.out.println("");
    		System.out.println("M - Mine");
    		System.out.println("S - Shop");
    		System.out.println("");
     
    		input.getUserInput("Enter your choice: ");
     
    		if (input.input.equals("M")) {
     
    			System.out.println(player.inventory);
    			mine.addOres();
    			mine.oreVaine();
    			mine.mine();
     
    		}
     
    		if (input.input.equals("S")) {
     
    			shop.shop();
     
    		}
     
    	}
     
    }

    shop.java
    public class shop {
     
    	int goldCoins = 100;
    	player player = new player();
     
     
    	public void shop() {
     
    		userInput input = new userInput();
    		mainMenu mainMenu = new mainMenu();
     
    		input.getUserInput("Would you like to buy or sell something?");
     
    		if (input.input.equals("sell")) {
     
    			System.out.println("");
    			System.out.println("These are the Items you have: " + player.inventory);
    			System.out.println("");
     
    			input.getUserInput("Which Item would you like to sell?");
     
    				if (input.input.equals("Stone")) {
     
    					System.out.println("");
    					System.out.println("That item sells for 10 gold coins.");
     
    				}
     
    				if (input.input.equals("Coal")) {
     
    					System.out.println("");
    					System.out.println("That item sells for 50 gold coins.");
    				}
     
    				if (input.input.equals("Iron")) {
     
    					System.out.println("");
    					System.out.println("That item sells for 75 gold coins.");
     
    				}
     
    				if (input.input.equals("Gold")) {
     
    					System.out.println("");
    					System.out.println("That item sells for 300 gold coins.");
     
    				}
     
    				if (input.input.equals("Diamond")) {
     
    					System.out.println("");
    					System.out.println("That item sells for 500 gold coins.");
     
    				}
     
    		}
     
    		if (input.input.equals("buy")) {
     
    			System.out.println("");
    			System.out.println("Welcome to the shop!");
    			System.out.println("");
    			System.out.println("Items:");
    			System.out.println("------------------------");
    			System.out.println("| P. Pick Axe          |");
    			System.out.println("| BP. Bronze Pick Axe  |");
    			System.out.println("| SP. Steel Pick Axe   |");
    			System.out.println("| DP. Diamond Pick Axe |");
    			System.out.println("------------------------");
     
    			input.getUserInput("Enter the item (abreviation) you want to buy: ");
     
    			if (input.input.equals("P")) {
     
    				input.getUserInput("That item cost 50 gold pieces. Are you sure you want to buy it?");
     
    				if (input.input.equals("yes")) {
     
    					if (goldCoins >= 50) {
     
    						goldCoins -= 50;
    						player.addInventory("P");
    						System.out.println(goldCoins);
    						System.out.println(player.inventory);
    						mainMenu.start();
     
    					} else {
     
    						System.out.println("");
    						System.out.println("You don't have enough coins to do that!");
    						mainMenu.start();
     
    					}
     
    				}
     
    				if (input.input.equals("no")) {
     
    					mainMenu.start();
     
    				}
     
    			}
     
    			if (input.input.equals("BP")) {
     
    				input.getUserInput("That item cost 150 gold pieces. Are you sure you want to buy it?");
     
    				if (input.input.equals("yes")) {
     
    					if (goldCoins >= 150) {
     
    						goldCoins -= 150;
    						player.addInventory("BP");
    						System.out.println(goldCoins);
    						System.out.println(player.inventory);
    						mainMenu.start();
     
    					} else {
     
    						System.out.println("");
    						System.out.println("You don't have enough coins to do that!");
    						mainMenu.start();
     
     
    					}
     
    				}
     
    				if (input.input.equals("no")) {
     
    						mainMenu.start();
     
    					}
     
    				}
     
    				if (input.input.equals("SP")) {
     
    					input.getUserInput("That item cost 300 gold pieces. Are you sure you want to buy it?");
     
    					if (input.input.equals("yes")) {
     
    						if (goldCoins >=300) {
     
    							goldCoins -= 300;
    							player.addInventory("SP");
    							System.out.println(goldCoins);
    							System.out.println(player.inventory);
    							mainMenu.start();
     
    						} else {
     
    							System.out.println("");
    							System.out.println("You don't have enough coins to do that!");
    							mainMenu.start();
     
    						}
     
    				}
     
    				if (input.input.equals("no")) {
     
    					mainMenu.start();
     
    				}
     
    			}
     
    			if (input.input.equals("DP")) {
     
    				input.getUserInput("That item cost 1000 gold pieces. Are you sure you want to buy it?");
     
    				if (input.input.equals("yes")) {
     
    					if (goldCoins >= 1000) {
     
    						goldCoins -= 1000;
    						player.addInventory("DP");	
    						System.out.println(goldCoins);
    						System.out.println(player.inventory);
    						mainMenu.start();
     
    					} else {
     
    						System.out.println("");
    						System.out.println("You don't have enough coins to do that!");
    						mainMenu.start();
     
    					}
     
    				}
     
    				if (input.input.equals("no")) {
     
    					mainMenu.start();
     
    				}
     
    			}
     
    		}
     
    	}
     
    }

    player.java
    import java.util.*;
     
    public class player {
     
    	ArrayList<String> inventory = new ArrayList<String>();
     
    	public void addInventory(String item) {
     
    		inventory.add(item);
     
    	}
     
    	public void removeInventory(String item) {
     
    		inventory.remove(item);
     
    	}
     
    }
    Last edited by sp11k3t3ht3rd; January 23rd, 2011 at 11:47 AM.


  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: Using values set in one class in another

    There are also a couple ways to do this, like setting a getInventory() method in your player class, which returns the inventory. And actually, you don't even need to do this at all. Since you instantiated the inventory in player as public, you can access the inventory in any other class by calling player.inventory

    Another way to do it is by making inventory public and static.

    public static ArrayList<String> inventory = new ArrayList<String>();

    Now, whenever you want to access the inventory from a different class, just call "player.inventory".

    However, some things to clear up:

    It's a convention to make all of your Java classes uppercase, not lowercase. This allows things like "player player = new player()" be less confusing; "Player player = new Player()" helps you identify which one is the class and which one is the object.

    This is important, because the method I mentioned earlier that sets the inventory to static uses the class, and not the object to access the inventory. By making inventory static, it means that there is now only one instance of it. No matter how many classes you had of Player, they would all use the same inventory. If your class was "Player" rather than "player", your code to access the inventory would look like this: "Player.inventory"
    Last edited by dpek; January 23rd, 2011 at 09:02 PM. Reason: cleared up something else I forgot/spelling error

  3. The Following User Says Thank You to dpek For This Useful Post:

    sp11k3t3ht3rd (January 23rd, 2011)

  4. #3
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Using values set in one class in another

    Thanks a ton! I would have never figured that out!

  5. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: Using values set in one class in another

    No problem, glad you got it solved. I would suggest looking more into class-specific things in java like public, private, protected, static, void, get/set methods, what the args in public static void main(String[] args) is, extends, implements, Interfaces, etc. There's a lot out there that will be very helpful for your project.

    ((Also, don't forget to mark the topic as solved))

Similar Threads

  1. Replies: 1
    Last Post: December 22nd, 2011, 08:55 AM
  2. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 08:51 PM
  3. Able to set, but not get values
    By Simple in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 23rd, 2010, 04:01 PM
  4. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM