2023-11-24 21:37:08 +04:00
|
|
|
package Trolleybus;
|
|
|
|
|
2023-12-01 20:42:28 +04:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2023-11-24 21:37:08 +04:00
|
|
|
public class SetGeneric <T extends Object>{
|
2023-12-01 20:42:28 +04:00
|
|
|
private final List<T> _places;
|
2023-11-24 21:37:08 +04:00
|
|
|
public int Count;
|
2023-12-01 20:42:28 +04:00
|
|
|
private final int _maxCount;
|
|
|
|
|
2023-11-24 21:37:08 +04:00
|
|
|
public SetGeneric(int count){
|
2023-12-01 20:42:28 +04:00
|
|
|
_places = new ArrayList<>();
|
|
|
|
_maxCount = count;
|
|
|
|
Count = 0;
|
2023-11-24 21:37:08 +04:00
|
|
|
}
|
|
|
|
//Вставка в начало
|
|
|
|
public int Insert(T bus){
|
|
|
|
return Insert(bus, 0);
|
|
|
|
}
|
|
|
|
//Вставка на какую-то позицию
|
|
|
|
public int Insert(T bus, int position)
|
|
|
|
{
|
2023-12-01 20:42:28 +04:00
|
|
|
if (position >= _maxCount || position < 0)
|
2023-11-24 21:37:08 +04:00
|
|
|
{
|
2023-12-01 20:42:28 +04:00
|
|
|
//позиция неверная, значит вставить нельзя
|
2023-11-24 21:37:08 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2023-12-01 20:42:28 +04:00
|
|
|
if (Count + 1 <= _maxCount) {
|
|
|
|
_places.add(position, bus);
|
|
|
|
Count++;
|
|
|
|
return position;
|
2023-11-24 21:37:08 +04:00
|
|
|
}
|
2023-12-01 20:42:28 +04:00
|
|
|
// Места нет
|
|
|
|
return -1;
|
2023-11-24 21:37:08 +04:00
|
|
|
}
|
|
|
|
public boolean Remove(int position)
|
|
|
|
{
|
|
|
|
if (position >= Count || position < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2023-12-01 20:42:28 +04:00
|
|
|
_places.remove(position);
|
|
|
|
Count--;
|
2023-11-24 21:37:08 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public T Get(int position)
|
2023-12-01 20:42:28 +04:00
|
|
|
{
|
|
|
|
if (position >= Count || position < 0)
|
2023-11-24 21:37:08 +04:00
|
|
|
{
|
2023-12-01 20:42:28 +04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (T)_places.get(position);
|
|
|
|
}
|
|
|
|
public ArrayList<T> GetBuses(int maxBus){
|
|
|
|
ArrayList<T> toReturn = new ArrayList<>();
|
|
|
|
for (int i = 0; i < _maxCount; i++) {
|
|
|
|
toReturn.add(_places.get(i));
|
|
|
|
if (i == maxBus) {
|
|
|
|
return toReturn;
|
2023-11-24 21:37:08 +04:00
|
|
|
}
|
|
|
|
}
|
2023-12-01 20:42:28 +04:00
|
|
|
return toReturn;
|
|
|
|
}
|
2023-11-24 21:37:08 +04:00
|
|
|
}
|