PIbd-12_Karamushko_M.K._Air.../SetAircraftsGeneric.java

42 lines
921 B
Java
Raw Permalink Normal View History

2022-11-25 13:49:46 +04:00
import java.util.ArrayList;
2022-11-15 18:20:34 +04:00
public class SetAircraftsGeneric<T>
{
2022-11-25 13:49:46 +04:00
private ArrayList<T> _places;
private int _maxCount;
2022-11-15 18:20:34 +04:00
public int getCount() {
2022-11-25 13:49:46 +04:00
return _places.size();
2022-11-15 18:20:34 +04:00
}
public SetAircraftsGeneric(int count)
{
2022-11-25 13:49:46 +04:00
_places = new ArrayList<>();
_maxCount = count;
2022-11-15 18:20:34 +04:00
}
2022-11-25 13:49:46 +04:00
public int Insert(T aircraft)
2022-11-15 18:20:34 +04:00
{
2022-11-25 13:49:46 +04:00
if (_places.size() == _maxCount) return -1;
_places.add(0, aircraft);
return 0;
2022-11-15 18:20:34 +04:00
}
2022-11-25 13:49:46 +04:00
public int Insert(T aircraft, int position)
2022-11-15 18:20:34 +04:00
{
2022-11-25 13:49:46 +04:00
if (_places.size() == _maxCount) return -1;
_places.add(position, aircraft);
2022-11-15 18:20:34 +04:00
return position;
}
public T Remove(int position)
{
2022-11-25 13:49:46 +04:00
if(position > _maxCount || position < 0) return null;
T res = _places.get(position);
_places.remove(res);
2022-11-15 18:20:34 +04:00
return res;
}
public T Get(int position)
{
2022-11-25 13:49:46 +04:00
return _places.get(position);
2022-11-15 18:20:34 +04:00
}
}