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
Code :
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
Code :
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
Code :
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);
}
}
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.
Code java:
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"
Re: Using values set in one class in another
Thanks a ton! I would have never figured that out!
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))