68 lines
2.0 KiB
Java
68 lines
2.0 KiB
Java
package laba1Loco;
|
|
|
|
import java.util.Random;
|
|
|
|
public class GenericDopClass<T extends EntityTrain, U extends IWheelDrawing> {
|
|
|
|
private Object[] Trains;
|
|
private Object[] 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 Object[maxCountTrains];
|
|
Wheels = new Object[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[countTrains++] = train;
|
|
return true;
|
|
}
|
|
|
|
public boolean addWheel(U wheel){
|
|
if (wheel == null)
|
|
return false;
|
|
if (countWheels > maxCountWheels)
|
|
return false;
|
|
Wheels[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[i] instanceof EntityLoco){
|
|
drawingTrain = new DrawingLoco((EntityLoco)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight);
|
|
}
|
|
else{
|
|
drawingTrain = new DrawingTrain((EntityTrain)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight);
|
|
}
|
|
return drawingTrain;
|
|
}
|
|
}
|