I am making a game and would like to implement trading.
What is the best way to do this?
Should I have an inventory class with integers for each number of items
Or should each item be its own object
Or what other way can it be done
import java.util.List;
import java.util.ArrayList;
public class InventoryList extends ArrayList
{
// method to return a list of all objects of a specific type (or all item in the list if 'null' is given for the class type)
public<T> List<T> getAll(Class clss)
{
List<T> objs = new ArrayList<T>();
for (int i=0; i<size(); i++) if (clss == null || clss == get(i).getClass()) objs.add((T)get(i));
return objs;
}
// method to return one object of a specific type from the list ('null' returned if none are found in the list)
// (the first item in the list is returned if 'null' is given for the class type)
public <T>T get(Class clss)
{
if (getAll(clss).isEmpty()) return null;
return (T)getAll(clss).get(0);
}
// method to return the number of objects of a specific type (or the size of the inventory if 'null' is given for the class type)
public int numberOfType(Class clss)
{
return getAll(clss).size();
}
}public InventoryList inv = new InventoryList();