I am constructing a rather large program that holds a lot of hard-coded data from a game (DnD 4e). For the sake of organization, I have broken this up into different classes (separate .java files) to avoid a 10k+ line innavigable program. I am storing an ArrayList
of objects called Item
. Each Item
contains a String name
, String price
, and String description
.
The problem is that I have all of the items stored in one method, Item
, and the ArrayList<Item>
in another method, InvList
. How can I print every Item
in the ArrayList<Item>
? I am more experienced in Arrays, and I realize I could use a for loop and just iterate through the Array, but I'm unaware of the method of doing so for ArrayLists.
Main Method:
public class DnD {
. . .
public static void main(String[] args) {
. . .
System.out.println("The inventory has "+inv.size()+" items.");
inv.add(Item.Candle);
printInv(inv);
} //main()
} //DnD
InvList Method, to keep track of people's inventories and the methods to manage them:
public class InvList {
public static ArrayList<Item> inv = new ArrayList<>();
// I want to create multiples ArrayLists for multiple people, ...
// ... so there will be several here.
} //InvList()
Item Method, housing all of the Item information and management methods:
public class Item {
. . .
public String name;
public String price;
public String description;
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); //separate method,
description = _desc;
} //Item()
. . .
} //Item
Any attempt I make just ends up print hashcodes or giving errors. I've looked through many posts here that talk about overwriting toString()
and what not, but I have not found an example of how to do so from other classes; these examples all seem to demonstrate how to do it if everything is in the exact same class/file. Am I doomed to just making one large mega file for everything? Or can I use three different classes here to print this one ArrayList<Item>
?
Side note: let me know if I'm overkilling the code
words, I'm trying to get a sense of what's acceptable over-formatting here.