package laba1Loco; import java.util.ArrayList; import java.util.Random; public class GenericDopClass { private ArrayList Trains; private ArrayList Wheels; private int maxCountTrains; private int countTrains; private int maxCountWheels; private int countWheels; private Random random; /// /// Ширина окна /// private int _pictureWidth; /// /// Высота окна /// private int _pictureHeight; public GenericDopClass(int _maxCountTrains, int _maxCountWheels, int pictureWidth, int pictureHeight){ maxCountTrains = _maxCountTrains; maxCountWheels = _maxCountWheels; Trains = new ArrayList(maxCountTrains); Wheels = new ArrayList(maxCountWheels); countTrains = 0; countWheels = 0; _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; random = new Random(); } public boolean addTrain(T train){ if (train == null) return false; if (countTrains > maxCountTrains) return false; Trains.add(countTrains++, train); return true; } public boolean addWheel(U wheel){ if (wheel == null) return false; if (countWheels > maxCountWheels) return false; Wheels.add(countWheels++, wheel); return true; } public DrawingTrain getRDrawingObject(){ if (countTrains == 0 || countWheels == 0) return null; int i = random.nextInt(countTrains); int j = random.nextInt(countWheels); DrawingTrain drawingTrain; if (Trains.get(i) instanceof EntityLoco){ drawingTrain = new DrawingLoco((EntityLoco)Trains.get(i), Wheels.get(j), _pictureWidth, _pictureHeight); } else{ drawingTrain = new DrawingTrain(Trains.get(i), Wheels.get(j), _pictureWidth, _pictureHeight); } return drawingTrain; } }