2022-10-24 10:58:46 +04:00
|
|
|
import java.lang.reflect.Array;
|
2022-10-22 01:15:02 +04:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
public class EntityWithExtraCreator <T extends EntityLocomotive, U extends IDrawningExtra> {
|
2022-10-25 16:54:14 +04:00
|
|
|
private final Object[] entityArr;
|
|
|
|
private final Object[] extraArr;
|
2022-10-22 01:15:02 +04:00
|
|
|
|
|
|
|
int entitiesCount = 0;
|
|
|
|
int extraCount = 0;
|
|
|
|
|
|
|
|
public EntityWithExtraCreator(int countEntities, int countExtra) {
|
2022-10-25 16:54:14 +04:00
|
|
|
entityArr = new Object[countEntities];
|
|
|
|
extraArr = new Object[countExtra];
|
2022-10-22 01:15:02 +04:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:54:14 +04:00
|
|
|
public void Insert(T entityLocomotive) {
|
2022-10-22 01:15:02 +04:00
|
|
|
if(entitiesCount < entityArr.length) {
|
|
|
|
entityArr[entitiesCount] = entityLocomotive;
|
|
|
|
entitiesCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:54:14 +04:00
|
|
|
public void Insert (U extra) {
|
2022-10-22 01:15:02 +04:00
|
|
|
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);
|
|
|
|
|
2022-10-25 16:54:14 +04:00
|
|
|
EntityLocomotive locomotive = (T)entityArr[getEntityRandomIndex];
|
|
|
|
IDrawningExtra extra = (U)extraArr[getExtraRandomIndex];
|
2022-10-22 01:15:02 +04:00
|
|
|
|
|
|
|
if (locomotive instanceof EntityWarmlyLocomotive) {
|
|
|
|
return new DrawningWarmlyLocomotive(locomotive, extra);
|
|
|
|
}
|
|
|
|
return new DrawningLocomotive(locomotive, extra);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|