PIbd-21_KozyrevSS_GasolineT.../ComboGenericCollection.java

67 lines
1.6 KiB
Java
Raw Permalink Normal View History

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;
}
2023-11-15 14:21:31 +04:00
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;
}
2023-11-15 14:21:31 +04:00
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)]);
}
}