From 076bdb81965d1d45394165119a64d921a948b3c4 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Sun, 27 Nov 2022 15:08:50 +0400 Subject: [PATCH] =?UTF-8?q?LabWork03:=20=D0=A4=D0=B8=D0=BA=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EntityWithRollers.java | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/EntityWithRollers.java b/EntityWithRollers.java index 09ff219..ccc3c89 100644 --- a/EntityWithRollers.java +++ b/EntityWithRollers.java @@ -1,41 +1,44 @@ +import java.util.ArrayList; import java.util.Random; public class EntityWithRollers { static Random rnd = new Random(); - private Object[] entities; - public int entitiesCount = 0; - private Object[] rollers; - public int rollersCount = 0; + private ArrayList entities; + private ArrayList 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(); + rollers = new ArrayList(); + 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));