-1

I'm making a DnD program to essentially just keep track of everything. I have three classes relevant to this problem, my main (which is just a hub to execute everything else), an Item class which holds all the properties of various individual items, and an InventoryList class which basically just defines an unholy load of items(this is a separate class because of the sheer number of items in 4e DnD). I had decided on making an Array of these Items, and calling that the users inventory; first off, is there a better way to do that -- i.e. using a HashMap or ArrayList? Second, I'm getting a classic "error, cannot find symbol" compiler error in the following code (shortened to relevance):

Main Method:

public class DnD {
    public static void main (String[] args) throws FileNotFoundException, IOException {
        . . .
        Item[] myInventory = {Candle}; //error: symbol cannot be found
    } //main()
} //DnD

Item Method:

public class Item {
    . . .
    public static Item Candle = new Item("Candle", 1, "Provides dim light for 2 squares, and lasts for 1 hour.");
    public Item(String _name, double _price, String _desc) {
        name = _name;
        price = priceConverter(_price);
        description = _desc;
    } //Item()
} //Item

InventoryList Method:

public class InventoryList {
    . . .
    public InventoryList() {
        // I have no idea where to proceed with this.
        // Is there a way for me to use this to initialize ...
        // ... an Array of Items in this method, to use in the main?
        // The Item object stores name, price, and a description; ...
        // ... is there a way to create an Array or similar to ...
        // ... display all that information at once and hold it together?
    }
}

I seem to be unable to summon Items in the main from the Item class. My Inventory (Item[]) is not working because of this.

Ankit Patidar
  • 2,731
  • 1
  • 14
  • 22
Seymour Guado
  • 246
  • 2
  • 13

2 Answers2

1

For the "cannot find symbol" error, you can fix it like this:

Item[] myInventory = {Item.Candle};

Candle is defined in the class Item, so you have to type the class name out as well.

For how to implement InventoryList, I recommend using an ArrayList<Item> to store the player's items:

private ArrayList<Item> items = new ArrayList<>();

public ArrayList<Item> getItems() {
    return items;
}

ArrayLists can dynamically change size, so if you want to add more items to the player's inventory, you can do that very conveniently.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

You must qualify the static property

Item[] myInventory = {Item.Candle};

Second question is rather braod, do more research and ask a more focussed question.

tkruse
  • 10,222
  • 7
  • 53
  • 80