PIbd-21_Kouvshinoff_T._A._W.../laba1Loco/GenericDopClass.java

69 lines
2.0 KiB
Java

package laba1Loco;
import java.util.ArrayList;
import java.util.Random;
public class GenericDopClass<T extends EntityTrain, U extends IWheelDrawing> {
private ArrayList<T> Trains;
private ArrayList<U> Wheels;
private int maxCountTrains;
private int countTrains;
private int maxCountWheels;
private int countWheels;
private Random random;
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
public GenericDopClass(int _maxCountTrains, int _maxCountWheels, int pictureWidth, int pictureHeight){
maxCountTrains = _maxCountTrains;
maxCountWheels = _maxCountWheels;
Trains = new ArrayList<T>(maxCountTrains);
Wheels = new ArrayList<U>(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;
}
}