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 EntityLocomotive[] entityArr;
private IDrawningExtra[] extraArr;
int entitiesCount = 0;
int extraCount = 0;
public EntityWithExtraCreator(int countEntities, int countExtra) {
entityArr = new EntityLocomotive[countEntities];
extraArr = 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);
}
}