PIbd-22_Chernyshev_G.J._29_.../Trolleybus/GenericDifficult.java
2023-11-24 20:37:08 +03:00

57 lines
2.0 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}