Merge branch 'LabWork03' into LabWork04

This commit is contained in:
Safgerd 2022-11-27 15:13:33 +04:00
commit 8a33beaab2

View File

@ -1,41 +1,44 @@
import java.util.ArrayList;
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;
private ArrayList<T> entities;
private ArrayList<U> rollers;
public int maxEntitiesCount = 0;
public int maxRollersCount = 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];
entities = new ArrayList<T>();
rollers = new ArrayList<U>();
maxEntitiesCount = count;
maxRollersCount = count;
}
public boolean add(T entity) {
if (entitiesCount >= entities.length) {
if (entities.size() >= maxEntitiesCount){
return false;
}
entities[entitiesCount++] = entity;
entities.add(entity);
return true;
}
public boolean add(U roller) {
if (rollersCount >= rollers.length) {
if (rollers.size() >= maxRollersCount){
return false;
}
rollers[rollersCount++] = roller;
rollers.add(roller);
return true;
}
public IDrawningObject constructTracktor() {
if (entitiesCount == 0 || rollersCount == 0) {
if (entities.size() == 0 || rollers.size() == 0) {
return null;
}
EntityTracktor entity = (EntityTracktor) entities[rnd.nextInt(0, entitiesCount)];
IDrawningRollers roller = (IDrawningRollers) rollers[rnd.nextInt(0, rollersCount)];
EntityTracktor entity = (EntityTracktor) entities.get(rnd.nextInt(0, entities.size()));
IDrawningRollers roller = (IDrawningRollers) rollers.get(rnd.nextInt(0, rollers.size()));
if (entity instanceof EntityTrackedVehicle advancedEntity) {
return new DrawningObjectExcavator(new DrawningTrackedVehicle(advancedEntity, roller));