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