PIbd-12_Karamushko_M.K._Air.../AircraftMixer.java

51 lines
1.4 KiB
Java
Raw Permalink Normal View History

2022-11-15 18:20:34 +04:00
import java.util.Random;
public class AircraftMixer<T extends EntityAircraft, U extends IDrawingEngines> {
private T[] aircrafts;
private U[] engines;
public AircraftMixer(int countAircrafts, int countEngines) {
aircrafts = (T[])(new EntityAircraft[countAircrafts]);
engines = (U[])(new IDrawingEngines[countEngines]);
}
public int add(T a) {
int index = 0;
while(index < aircrafts.length && aircrafts[index] != null) index++;
if(index == aircrafts.length) return -1;
aircrafts[index] = a;
return index;
}
public int add(U e) {
int index = 0;
while(index < engines.length && engines[index] != null) index++;
if(index == engines.length) return -1;
engines[index] = e;
return index;
}
public DrawingAircraft constructAircraft(int width, int height) {
Random rnd = new Random();
DrawingAircraft air;
T selectedAircraft = aircrafts[rnd.nextInt(0, aircrafts.length)];
U selectedEngines = engines[rnd.nextInt(0, engines.length)];
DrawingAircraft result;
if(selectedAircraft instanceof EntityModernAircraft) {
result = new DrawingModernAircraft(selectedAircraft, selectedEngines);
}
else result = new DrawingAircraft(selectedAircraft, selectedEngines);
result.SetPosition(10, 10, width, height);
return result;
}
}