Item item = items[place]; // by default you can have an EmptyItem that does nothing in the inventory when the place is empty
now let's imagine you have something like
interface Item {
void use();
}
class Berry implements Item{
@Override
public void use(){
//eating a a barry
}
}
class EmptyItem implements Item{
@Override
public void use(){
//do nothing
}
}
then you would implement the logic in the method inside the use
item.use();
I would suggest you to create your inventory as bit more complex object instead of an array (the complex object can be composed itself using an array)
Imagine to have an inventory that can be created this way (passing its size as a parameter):
class Inventory{
private Item[] items;
public Inventory(int size){
items = new Item[size];
// references are null by default, just making this explicit for op
for(int i = 0; i<size; i++){
items[i] = null;
}
}
public void addItem(int space, Item item){
// check for input params
// check array boundaries
// check for item in the selected space
if(items[space] == null){
items[space] = item;
}else{
// space not empty!
}
}
// I'd like more the approach of using get item -> item.use() :)
// but this is a bit more inline with op approach
public void useItem(int space){
// check array boundaries
Item item = items[space];
if(item != null){
item.use();
}
}
// keeping the null management inside the class itself
public Item getItem(int space){
// check array boundaries
Item item = items[space];
if(item == null){
item = new EmptyItem();
}
return item;
}
}
Just imagine what you are modeling to be real life objects, imagine how they intefact one another, and try to model their properties and behaviour accordingly :)