76 lines
1.7 KiB
Java
76 lines
1.7 KiB
Java
package ProjectElectricLocomotive;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class SetGeneric<T extends DrawingLocomotive>{
|
|
private ArrayList<T> _places;
|
|
|
|
public int Count()
|
|
{
|
|
return _places.size();
|
|
}
|
|
|
|
public int maxCount;
|
|
|
|
public SetGeneric(int count)
|
|
{
|
|
maxCount = count;
|
|
_places = new ArrayList<>(count);
|
|
}
|
|
|
|
public int Insert(T loco)
|
|
{
|
|
return Insert(loco, 0);
|
|
}
|
|
|
|
public int Insert(T loco, int position)
|
|
{
|
|
if(position < 0)
|
|
return -1;
|
|
if(_places.size() >= maxCount)
|
|
throw new LocoStorageOverflowException(maxCount);
|
|
else
|
|
{
|
|
_places.add(position, loco);
|
|
return position;
|
|
}
|
|
}
|
|
|
|
public T Remove(int position)
|
|
{
|
|
if (position >= Count() || position < 0)
|
|
return null;
|
|
else
|
|
{
|
|
T plane = _places.get(position);
|
|
if(plane == null)
|
|
throw new LocoNotFoundException(position);
|
|
_places.set(position, null);
|
|
return plane;
|
|
}
|
|
}
|
|
|
|
public T Get(int position)
|
|
{
|
|
if (position < 0 || position >= Count()) return null;
|
|
return _places.get(position);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public void clear() {
|
|
_places.clear();
|
|
}
|
|
} |