2023-10-21 19:25:05 +04:00
|
|
|
package ProjectElectricLocomotive;
|
|
|
|
|
2023-11-07 00:00:07 +04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2023-10-21 19:25:05 +04:00
|
|
|
public class SetGeneric<T extends DrawingLocomotive>{
|
2023-11-07 00:00:07 +04:00
|
|
|
private ArrayList<T> _places;
|
2023-11-07 20:36:52 +04:00
|
|
|
|
2023-11-07 00:00:07 +04:00
|
|
|
public int Count()
|
|
|
|
{
|
|
|
|
return _places.size();
|
2023-10-21 19:25:05 +04:00
|
|
|
}
|
2023-11-07 00:00:07 +04:00
|
|
|
|
|
|
|
public int maxCount;
|
2023-11-07 20:36:52 +04:00
|
|
|
|
2023-11-07 00:00:07 +04:00
|
|
|
public SetGeneric(int count)
|
|
|
|
{
|
|
|
|
maxCount = count;
|
2023-11-18 23:58:18 +04:00
|
|
|
_places = new ArrayList<>(count);
|
2023-10-21 19:25:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public int Insert(T loco)
|
|
|
|
{
|
|
|
|
return Insert(loco, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Insert(T loco, int position)
|
|
|
|
{
|
2023-11-18 23:58:18 +04:00
|
|
|
if(position < 0 || position > maxCount)
|
2023-11-07 00:00:07 +04:00
|
|
|
return -1;
|
|
|
|
else
|
2023-10-21 19:25:05 +04:00
|
|
|
{
|
2023-11-07 00:00:07 +04:00
|
|
|
_places.add(position, loco);
|
2023-10-21 19:25:05 +04:00
|
|
|
return position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public T Remove(int position)
|
|
|
|
{
|
|
|
|
if (position >= Count() || position < 0)
|
|
|
|
return null;
|
2023-11-07 00:00:07 +04:00
|
|
|
else
|
|
|
|
{
|
2023-11-07 20:36:52 +04:00
|
|
|
T plane = _places.get(position);
|
|
|
|
_places.set(position, null);
|
|
|
|
return plane;
|
2023-11-07 00:00:07 +04:00
|
|
|
}
|
2023-10-21 19:25:05 +04:00
|
|
|
}
|
|
|
|
|
2023-11-07 00:40:53 +04:00
|
|
|
public T Get(int position)
|
|
|
|
{
|
|
|
|
if (position < 0 || position >= Count()) return null;
|
|
|
|
return _places.get(position);
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:36:52 +04:00
|
|
|
public void Set(int position, T loco) {
|
|
|
|
// Проверка позиции
|
|
|
|
// Проверка свободных мест в списке
|
|
|
|
if (position < 0 || position >= maxCount || Count() == maxCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Вставка в список по позиции
|
|
|
|
_places.set(position, loco);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<T> GetEnumerator() {
|
|
|
|
return _places;
|
|
|
|
}
|
|
|
|
|
2023-12-19 12:47:07 +04:00
|
|
|
public void clear() {
|
|
|
|
_places.clear();
|
|
|
|
}
|
2023-11-06 20:57:19 +04:00
|
|
|
}
|