PIbd-21_KozyrevSS_GasolineT.../ComboGenericCollection.java

97 lines
2.9 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class ComboGenericCollection <T extends BaseTanker, U extends IWheelDraw> {
private final T[] _tankers;
private final U[] _wheels;
public int MaxSize() {return _tankers.length;}
private int CountTankers = 0;
private int CountWheels = 0;
private final Random random;
private final int Width;
private final int Height;
public ComboGenericCollection(int count, int width, int height)
{
_tankers = (T[]) new BaseTanker[count];
_wheels = (U[]) new IWheelDraw[count];
random = new Random();
Width = width;
Height = height;
}
public T GenerateTanker()
{
int mode = random.nextInt(0, 100) % 2;
if (mode == 0)
{
return (T) new BaseTanker(random.nextInt(100, 200), random.nextInt(2000, 4000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
}
else
{
return (T) new GasolineTanker(random.nextInt(100, 200), random.nextInt(2000, 4000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
IntToBool(random.nextInt(0, 100)),IntToBool(random.nextInt(0, 100)), IntToBool(random.nextInt(0, 100)));
}
}
public U GenerateWheel()
{
int mode = random.nextInt(0, 100) % 3;
U Wheels = (U) new DrawWheelSquare();
switch (mode)
{
case 0 -> {Wheels = (U) new DrawWheelCircle();}
case 1 -> {Wheels = (U) new DrawWheelClassic();}
case 2 -> {Wheels = (U) new DrawWheelSquare();}
}
int count = random.nextInt(0, 100);
Wheels.setWheelCount(count);
return Wheels;
}
public boolean Add(T tanker)
{
for (int i = 0; i < MaxSize(); i++)
{
if (_tankers[i] == null)
{
_tankers[i] = tanker;
CountTankers++;
return true;
}
}
return false;
}
public boolean Add(U wheel)
{
for (int i = 0; i < MaxSize(); i++)
{
if (_wheels[i] == null)
{
_wheels[i] = wheel;
CountWheels++;
return true;
}
}
return false;
}
private boolean IntToBool(int n) {return n % 2 == 0;}
public DrawTanker CreateDraw()
{
T tanker = _tankers[random.nextInt(0, CountTankers)];
if (tanker instanceof GasolineTanker) {
return new DrawGasolineTanker((GasolineTanker) tanker, Width, Height, _wheels[random.nextInt(0, CountWheels)]);
}
return new DrawTanker(tanker, Width, Height, _wheels[random.nextInt(0, CountWheels)]);
}
}