PIbd-23_Mochalov_D.V._Locom.../EntityWithExtraCreator.java

48 lines
1.4 KiB
Java
Raw Normal View History

import java.util.Random;
public class EntityWithExtraCreator <T extends EntityLocomotive, U extends IDrawningExtra> {
private T[] entityArr;
private U[] extraArr;
int entitiesCount = 0;
int extraCount = 0;
public EntityWithExtraCreator(int countEntities, int countExtra) {
entityArr = (T[]) new Object[countEntities];
extraArr = (U[]) new Object[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);
T locomotive = entityArr[getEntityRandomIndex];
U extra = extraArr[getExtraRandomIndex];
if (locomotive instanceof EntityWarmlyLocomotive) {
return new DrawningWarmlyLocomotive(locomotive, extra);
}
return new DrawningLocomotive(locomotive, extra);
}
}