0

I am currently working on a small project simulating a game. Each Player object has an inventory which is a HashMap<Item, Integer> where the Integer is the quantity of that Item.

I am currently trying to write a method in the Player class, that allows the player to buy an Item from a Shop, if the Player have enough Coins in their inventory. Each Player starts with 50 Coins in their inventory.

Although, when I am trying to acces the coins in the players inventory (using inventory.get(coins)), I get an "coins cannot be resolved to a variable error".

CONSTRUCTOR

private String name; private HashMap<Item, Integer> inventory; private String location; public Player (String name){ this.name = name; location = "Home"; inventory = new HashMap<>(); Resource coins = new Resource("Coins", 1, 1); Tool pickaxe = new Tool("Pickaxe", 100, 100); Tool axe = new Tool("Axe", 100, 100); Tool shovel = new Tool("Shovel", 100, 100); Crop turnip = new Crop("Turnip", 20, "Spring"); this.inventory.put(coins, 50); this.inventory.put(pickaxe, 1); this.inventory.put(axe, 1); this.inventory.put(shovel, 1); this.inventory.put(turnip, 10); } 

METHOD THAT FAILS

public void buyItemPierre(Shop pierres, Item item){ if (location.equals("Pierres")){ if (pierres.getForSale().containsKey(item)){ if (inventory.get(**coins**) >= pierres.getForSale().get(item)){ // ERROR HERE } } else{ System.out.println("Sorry, that item is not for sale here"); } } else{ System.out.println("You have to visit Pierres before you can buy anything from there!"); } } 

I've tried instantiating the objects in the main method, although I get the same error. There must be something that I don't get regarding how to reference objects as keys in a HashMap... How else can I check if the player has enough coins? Thank you in advance!

1
  • This could be fixed by proper usable of variables, but honestly: don't. Don't use Objects like them as Map keys. They don't bring you any advantage. Use something static like a String "coins" or a own Enum. Much more flexible. Commented Dec 30, 2022 at 16:51

1 Answer 1

1

You have declared coins as a local variable in the constructor.

 Resource coins = new Resource("Coins", 1, 1); 

So as soon as the constructor is complete, that variable goes away. You still have the object as a key in the map, but if you want to use the coins variable to look it up in the map, you have to declare it as a member (outside the constructor).

private Resource coins= new Resource("Coins", 1, 1); 
Sign up to request clarification or add additional context in comments.

5 Comments

Ah okay that does make sense! But how can I then add a new object to the inventory and access it later? Should I then declare that as a member too?
Just like you have a buyItemPierre method, you need a addItem method so you can add items to the map.
Aside: the type of map you are using is also called a "multiset" or a "bag":en.wikipedia.org/wiki/Multiset
I would create a wrapper class around Item to hold the count. public class InventoryItem { Item item; Integer count; ...constructors and setter} And then declare your map to hold these, indexed by a string. private Map<String, InventoryItem> inventory=new HashMap<>(); Then, in your constructor, use inventory.put("Coins", new InventoryItem(new Resource("Coins",1,1), 50);
Thank you so much for all your responses! Really appreciated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.