import java.lang.reflect.Array; import java.util.Random; public class EntityWithExtraCreator { private final Object[] entityArr; private final Object[] extraArr; int entitiesCount = 0; int extraCount = 0; public EntityWithExtraCreator(int countEntities, int countExtra) { entityArr = new Object[countEntities]; extraArr = new Object[countExtra]; } public void Insert(T entityLocomotive) { if(entitiesCount < entityArr.length) { entityArr[entitiesCount] = entityLocomotive; entitiesCount++; } } public void Insert (U extra) { if(extraCount < extraArr.length) { extraArr[extraCount] = extra; extraCount++; } } public DrawningLocomotive getEntityWithExtra() { Random random = new Random(); int getEntityRandomIndex = random.nextInt(entityArr.length); int getExtraRandomIndex = random.nextInt(extraArr.length); EntityLocomotive locomotive = (T)entityArr[getEntityRandomIndex]; IDrawningExtra extra = (U)extraArr[getExtraRandomIndex]; if (locomotive instanceof EntityWarmlyLocomotive) { return new DrawningWarmlyLocomotive(locomotive, extra); } return new DrawningLocomotive(locomotive, extra); } }