This feels like such a basic question but this is all new for me:
I have a Person and Room class, both of which have a list of Item objects.
public class Person{
private ArrayList<Item> items;
public Person() {
items = new ArrayList<>();
}
public void addItem(){
...
}
public void removeItem(){
...
}
}
public class Room {
private ArrayList<Item> items;
public Room () {
items = new ArrayList<>();
}
public void addItem(){
...
}
public void removeItem(){
...
}
}
The item methods e.g. addItem() are duplicated in both the Room class and the Person class which wasn't very nice. I thought about making a separate Inventory class which has a list of items and item methods and then every room and person would have an inventory.
But then I wouldn't be able to call the Item methods from a Person or Room if I use a private Inventory field.
What's the best way to stop duplication here? Thanks in advance!