LabWork03: Фиксы

This commit is contained in:
Safgerd 2022-11-27 15:08:50 +04:00
parent 2f4b0a65e7
commit 076bdb8196

View File

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