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

53 lines
1.3 KiB
Java
Raw Permalink Normal View History

2022-11-25 12:49:46 +03:00
import java.util.ArrayList;
2022-12-02 10:17:40 +03:00
import java.util.ListIterator;
2022-11-25 12:49:46 +03:00
2022-11-15 17:20:34 +03:00
public class SetAircraftsGeneric<T>
{
2022-11-25 12:49:46 +03:00
private ArrayList<T> _places;
private int _maxCount;
2022-11-15 17:20:34 +03:00
public int getCount() {
2022-11-25 12:49:46 +03:00
return _places.size();
2022-11-15 17:20:34 +03:00
}
public SetAircraftsGeneric(int count)
{
2022-11-25 12:49:46 +03:00
_places = new ArrayList<>();
_maxCount = count;
2022-11-15 17:20:34 +03:00
}
2022-12-15 13:54:34 +03:00
public int Insert(T aircraft) throws StorageOverflowException
2022-11-15 17:20:34 +03:00
{
2022-12-15 13:54:34 +03:00
if (_places.size() == _maxCount) throw new StorageOverflowException(_maxCount);
2022-11-25 12:49:46 +03:00
_places.add(0, aircraft);
return 0;
2022-11-15 17:20:34 +03:00
}
2022-12-15 13:54:34 +03:00
public int Insert(T aircraft, int position) throws StorageOverflowException
2022-11-15 17:20:34 +03:00
{
2022-12-15 13:54:34 +03:00
if (_places.size() == _maxCount) throw new StorageOverflowException(_maxCount);
2022-11-25 12:49:46 +03:00
_places.add(position, aircraft);
2022-11-15 17:20:34 +03:00
return position;
}
2022-12-15 13:54:34 +03:00
public T Remove(int position) throws AircraftNotFoundException
2022-11-15 17:20:34 +03:00
{
2022-12-15 13:54:34 +03:00
if(position > _places.size() || position < 0) throw new AircraftNotFoundException(position);
2022-11-25 12:49:46 +03:00
T res = _places.get(position);
_places.remove(res);
2022-12-15 13:54:34 +03:00
if(res == null) throw new AircraftNotFoundException(position);
2022-11-15 17:20:34 +03:00
return res;
}
2022-12-02 10:17:40 +03:00
public ListIterator<T> GetElems() {
return _places.listIterator();
}
public void Clear() {
_places.clear();
}
2022-11-15 17:20:34 +03:00
public T Get(int position)
{
2022-11-25 12:49:46 +03:00
return _places.get(position);
2022-11-15 17:20:34 +03:00
}
}