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); } }