fix object

This commit is contained in:
Кашин Максим 2022-11-15 18:15:47 +04:00
parent 66f961be08
commit 2035ffd79d

View File

@ -1,24 +1,25 @@
import java.util.Random; import java.util.Random;
import java.util.ArrayList;
public class CreaterGeneric <T extends EntityGasolineTanker, U extends IDrawningObjectWheels>{ public class CreaterGeneric <T extends EntityGasolineTanker, U extends IDrawningObjectWheels>{
Object[] GasolineTanker; ArrayList<T> GasolineTanker;
Object[] Wheels; ArrayList<U> Wheels;
int GasolineTankerCount=0; int GasolineTankerCount=0;
int WheelsCount=0; int WheelsCount=0;
public CreaterGeneric(int gasolineTankerCount,int wheelsCount){ public CreaterGeneric(int gasolineTankerCount,int wheelsCount){
GasolineTanker=new Object[gasolineTankerCount]; GasolineTanker=new ArrayList<>(gasolineTankerCount);
Wheels=new Object[wheelsCount]; Wheels=new ArrayList<>(wheelsCount);
} }
public int AddGasolineTanker(T gasolineTanker){ public int AddGasolineTanker(T gasolineTanker){
if(GasolineTankerCount<GasolineTanker.length){ if(GasolineTankerCount<=GasolineTanker.size()){
GasolineTanker[GasolineTankerCount]=gasolineTanker; GasolineTanker.add(gasolineTanker);
GasolineTankerCount++; GasolineTankerCount++;
return GasolineTankerCount-1; return GasolineTankerCount-1;
} }
return -1; return -1;
} }
public int AddWheels(U wheels){ public int AddWheels(U wheels){
if(WheelsCount<Wheels.length){ if(WheelsCount<=Wheels.size()){
Wheels[WheelsCount]=wheels; Wheels.add(wheels);
WheelsCount++; WheelsCount++;
return WheelsCount-1; return WheelsCount-1;
} }
@ -27,8 +28,8 @@ public class CreaterGeneric <T extends EntityGasolineTanker, U extends IDrawning
public DrawingGasolineTanker NewGasolineTankerCreating() public DrawingGasolineTanker NewGasolineTankerCreating()
{ {
Random rand=new Random(); Random rand=new Random();
T gasolineTanker = (T)GasolineTanker[rand.nextInt(GasolineTankerCount)]; T gasolineTanker = (GasolineTanker.get(rand.nextInt(GasolineTankerCount)));
U wheel = (U)Wheels[rand.nextInt(WheelsCount)]; U wheel = (Wheels.get(rand.nextInt(WheelsCount)));
if(gasolineTanker instanceof EntityImprovedGasolineTanker){ if(gasolineTanker instanceof EntityImprovedGasolineTanker){
return new DrawingImprovedGasolineTanker(gasolineTanker,wheel); return new DrawingImprovedGasolineTanker(gasolineTanker,wheel);
} }