I am working on a videogame with a friend. To account for different types of items, we originally had a class for each item extending an Item class. There wasn't much data in these classes, so I was looking for an alternative so that our workspace wasn't so cluttered. I started learning about HashMaps, and I thought they were an awesome way to add items. We could also set it up so that instead of accessing the items in the HashMap with an int, which would basically just make it an ArrayList, we could access them with Strings. So I started adding this functionality, creating anonymous Items in the Item class,
private static Item coal = new Item() {
weight = .2;
setImageID(0, 16);
}
and adding them to the HashMap.
itemMap.put("Coal", coal);
After doing a few of these, I realized that there was only one item of each type in the list, and if we ever wanted to have multiples of those items that could be modified without modifying the original, we would need to make copies. I started doing research on how to do that. We could use a copy constructor, but there are too many variables in an Item for that to be done efficiently. We certainly could do that, but I was wondering if there was a simple solution. Could we make all of the items final? I'm just spitballing, because I am totally new to this area of programming. We could be doing this whole thing wrong, too. I just need a way to use a HashMap to create something of an "Item database" that I can use to access an indefinite amount of an item in the list. Any suggestions?