Simple Inventory System

Pretty new to Java and want to make a simple command line game that I can expand on later. My goal is to have two items combine into a new item, and I’m thinking that I need to make a class that is a recipe list and another class that is an overall item list. The part where I’m getting stuck is giving these items to a player when they get one of the recipes correctly. I’m assuming I’d need a third class to represent the inventory itself but I’m a little confused as to how I would add and remove existing items from an extend to that inventory. I’d love to create save states as well but I can come back to that later, for now I would appreciate any recommendations on making an inventory like this.

Anyone got a recommendation for how to do this or a path for me to go down to study this sort of thing?

You’re more or less defined the classes you need right there in your natural language description of the problem: you need an Inventory, Items, and Recipes. An Inventory is a collection of Items - I’d suggest a map of items to quantities. A Recipe is a similar looking thing - a map of Items and quantities, ie. what’s in the recipe; and it needs an output, that is, what it makes, which will be presumably an Item.

Given a particular Inventory, you could query it for all the available Recipes that could be made from the things in the inventory.

You might do that by inverting the query somewhat, and asking of each possible Recipe, “can I make this thing using this Inventory?”, and the Recipe would then check to see that the inventory contains enough of each of its ingredients, and return true or false.

Then you’ll want a master list of all the possible Recipes somewhere, and a master list of all the possible Items. Possibly as static collections set up in the Recipe and Item classes.

1 Like