Compare commits

...

2 Commits

View File

@ -1,11 +1,12 @@
package laba1Loco; package laba1Loco;
import java.util.ArrayList;
import java.util.Random; import java.util.Random;
public class GenericDopClass<T extends EntityTrain, U extends IWheelDrawing> { public class GenericDopClass<T extends EntityTrain, U extends IWheelDrawing> {
private Object[] Trains; private ArrayList<T> Trains;
private Object[] Wheels; private ArrayList<U> Wheels;
private int maxCountTrains; private int maxCountTrains;
private int countTrains; private int countTrains;
private int maxCountWheels; private int maxCountWheels;
@ -23,8 +24,8 @@ public class GenericDopClass<T extends EntityTrain, U extends IWheelDrawing> {
public GenericDopClass(int _maxCountTrains, int _maxCountWheels, int pictureWidth, int pictureHeight){ public GenericDopClass(int _maxCountTrains, int _maxCountWheels, int pictureWidth, int pictureHeight){
maxCountTrains = _maxCountTrains; maxCountTrains = _maxCountTrains;
maxCountWheels = _maxCountWheels; maxCountWheels = _maxCountWheels;
Trains = new Object[maxCountTrains]; Trains = new ArrayList<T>(maxCountTrains);
Wheels = new Object[maxCountWheels]; Wheels = new ArrayList<U>(maxCountWheels);
countTrains = 0; countTrains = 0;
countWheels = 0; countWheels = 0;
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
@ -37,7 +38,7 @@ public class GenericDopClass<T extends EntityTrain, U extends IWheelDrawing> {
return false; return false;
if (countTrains > maxCountTrains) if (countTrains > maxCountTrains)
return false; return false;
Trains[countTrains++] = train; Trains.add(countTrains++, train);
return true; return true;
} }
@ -46,7 +47,7 @@ public class GenericDopClass<T extends EntityTrain, U extends IWheelDrawing> {
return false; return false;
if (countWheels > maxCountWheels) if (countWheels > maxCountWheels)
return false; return false;
Wheels[countWheels++] = wheel; Wheels.add(countWheels++, wheel);
return true; return true;
} }
@ -56,11 +57,11 @@ public class GenericDopClass<T extends EntityTrain, U extends IWheelDrawing> {
int i = random.nextInt(countTrains); int i = random.nextInt(countTrains);
int j = random.nextInt(countWheels); int j = random.nextInt(countWheels);
DrawingTrain drawingTrain; DrawingTrain drawingTrain;
if (Trains[i] instanceof EntityLoco){ if (Trains.get(i) instanceof EntityLoco){
drawingTrain = new DrawingLoco((EntityLoco)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight); drawingTrain = new DrawingLoco((EntityLoco)Trains.get(i), Wheels.get(j), _pictureWidth, _pictureHeight);
} }
else{ else{
drawingTrain = new DrawingTrain((EntityTrain)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight); drawingTrain = new DrawingTrain(Trains.get(i), Wheels.get(j), _pictureWidth, _pictureHeight);
} }
return drawingTrain; return drawingTrain;
} }