import java.util.Random; public class EntityWithRollers { static Random rnd = new Random(); private final Object[] entities; public int entitiesCount = 0; private final Object[] rollers; public int rollersCount = 0; public EntityWithRollers(int count) { entities = new Object[count]; rollers = new Object[count]; } public boolean add(EntityArtillery entity) { if (entitiesCount >= entities.length) { return false; } entities[entitiesCount++] = entity; return true; } public boolean add(IDrawingRollers roller) { if (rollersCount >= rollers.length) { return false; } rollers[rollersCount++] = roller; return true; } public IDrawingObject constructArtillery() { if (entitiesCount == 0 || rollersCount == 0) { return null; } EntityArtillery entity = (EntityArtillery) entities[rnd.nextInt(0, entitiesCount)]; IDrawingRollers roller = (IDrawingRollers) rollers[rnd.nextInt(0, rollersCount)]; if (entity instanceof EntityAdvancedArtillery advancedEntity) { return new DrawingObjectArtillery(new DrawingAdvancedArtillery(advancedEntity, roller)); } return new DrawingObjectArtillery(new DrawingArtillery(entity, roller)); } }