PIbd-23_Minhasapov_R.H._Exc.../EntityWithRollers.java
2022-11-22 16:01:36 +04:00

46 lines
1.5 KiB
Java

import java.util.Random;
public class EntityWithRollers<T extends EntityTracktor, U extends IDrawningRollers> {
static Random rnd = new Random();
private Object[] entities;
public int entitiesCount = 0;
private Object[] rollers;
public int rollersCount = 0;
public EntityWithRollers(int count) {
//new T[count] не работает "Type parameter 'T' cannot be instantiated directly"
entities = new Object[count];
//new U[count] не работает "Type parameter 'U' cannot be instantiated directly"
rollers = new Object[count];
}
public boolean add(T entity) {
if (entitiesCount >= entities.length) {
return false;
}
entities[entitiesCount++] = entity;
return true;
}
public boolean add(U roller) {
if (rollersCount >= rollers.length) {
return false;
}
rollers[rollersCount++] = roller;
return true;
}
public IDrawningObject constructTracktor() {
if (entitiesCount == 0 || rollersCount == 0) {
return null;
}
EntityTracktor entity = (EntityTracktor) entities[rnd.nextInt(0, entitiesCount)];
IDrawningRollers roller = (IDrawningRollers) rollers[rnd.nextInt(0, rollersCount)];
if (entity instanceof EntityTrackedVehicle advancedEntity) {
return new DrawningObjectExcavator(new DrawningTrackedVehicle(advancedEntity, roller));
}
return new DrawningObjectExcavator(new DrawningTracktor(entity, roller));
}
}