57 lines
2.0 KiB
Java
57 lines
2.0 KiB
Java
package Trolleybus;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.Random;
|
||
|
||
public class GenericDifficult <T extends EntityBus, U extends IDrawingDoors> {
|
||
private ArrayList<T> Buses;
|
||
private ArrayList<U> Doors;
|
||
private int CntOfBuses;
|
||
private int MaxCntOfBuses;
|
||
private int CntOfDoors;
|
||
private int MaxCntOfDoors;
|
||
private int WidthOfPanel;
|
||
private int HeightOfPanel;
|
||
|
||
public GenericDifficult(int maxCountOfBuses, int maxCountOfDoors, int width, int height) {
|
||
MaxCntOfBuses = maxCountOfBuses;
|
||
MaxCntOfDoors = maxCountOfDoors;
|
||
Buses = new ArrayList<T>(MaxCntOfBuses);
|
||
Doors = new ArrayList<U>(MaxCntOfDoors);
|
||
CntOfBuses = 0;
|
||
CntOfDoors = 0;
|
||
WidthOfPanel = width;
|
||
HeightOfPanel = height;
|
||
}
|
||
|
||
//Полиморфные методы добавления в массив
|
||
public boolean Add(T bus) {
|
||
if (bus == null || CntOfBuses >= MaxCntOfBuses) {
|
||
return false;
|
||
}
|
||
Buses.add(CntOfBuses++, bus);
|
||
return true;
|
||
}
|
||
|
||
public boolean Add(U door) {
|
||
if (door == null || CntOfDoors >= MaxCntOfDoors) {
|
||
return false;
|
||
}
|
||
Doors.add(CntOfDoors++, door);
|
||
return true;
|
||
}
|
||
|
||
public DrawingBus CreateObject() {
|
||
if (CntOfBuses == 0 || CntOfDoors == 0) {
|
||
return null;
|
||
}
|
||
// В массивах с дверьми и самими автобусами берём по 1 случайному элементу (необязательно с одинаковым индексом),
|
||
// чтобы получить рандомное сочетание дверей и самого корпуса
|
||
Random rand = new Random();
|
||
int indexOfEntityBus = rand.nextInt(0, CntOfBuses);
|
||
int indexOfDoor = rand.nextInt(0, CntOfDoors);
|
||
T bus = Buses.get(indexOfEntityBus);
|
||
U doors = Doors.get(indexOfDoor);
|
||
return new DrawingBus(bus, doors, WidthOfPanel, HeightOfPanel);
|
||
}
|
||
} |