import java.util.ArrayList; import java.util.Random; public class EntityWithRollers { static Random rnd = new Random(); private ArrayList entities; private ArrayList rollers; public int maxEntitiesCount = 0; public int maxRollersCount = 0; public EntityWithRollers(int count) { entities = new ArrayList(); rollers = new ArrayList(); maxEntitiesCount = count; maxRollersCount = count; } public boolean add(T entity) { if (entities.size() >= maxEntitiesCount){ return false; } entities.add(entity); return true; } public boolean add(U roller) { if (rollers.size() >= maxRollersCount){ return false; } rollers.add(roller); return true; } public IDrawningObject constructTracktor() { if (entities.size() == 0 || rollers.size() == 0) { return null; } EntityTracktor entity = (EntityTracktor) entities.get(rnd.nextInt(0, entities.size())); IDrawningRollers roller = (IDrawningRollers) rollers.get(rnd.nextInt(0, rollers.size())); if (entity instanceof EntityTrackedVehicle advancedEntity) { return new DrawningObjectExcavator(new DrawningTrackedVehicle(advancedEntity, roller)); } return new DrawningObjectExcavator(new DrawningTracktor(entity, roller)); } }