diff --git a/AirBomber/AirBomber/SetAirplanesGeneric.cs b/AirBomber/AirBomber/SetAirplanesGeneric.cs index ab32adf..0848595 100644 --- a/AirBomber/AirBomber/SetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/SetAirplanesGeneric.cs @@ -36,8 +36,12 @@ namespace AirBomber /// public bool Insert(T airplane) { - // TODO вставка в начало набора - return true; + return Insert(airplane, 0); + } + + private bool isCorrectPosition(int position) + { + return 0 < position && position <= Count; } /// /// Добавление объекта в набор на конкретную позицию @@ -47,11 +51,21 @@ namespace AirBomber /// public bool Insert(T airplane, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции + int positionNullElement = position; + while (Get(positionNullElement) != null) + { + positionNullElement++; + } + // Если изначальная позиция была некорректной или пустых элементов справа не оказалось возвращаем false + if (!isCorrectPosition(positionNullElement)) + { + return false; + } + while (positionNullElement != position) // Смещение вправо + { + _places[positionNullElement] = _places[positionNullElement - 1]; + positionNullElement--; + } _places[position] = airplane; return true; } @@ -62,8 +76,9 @@ namespace AirBomber /// public bool Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присовив элементу массива значение null + if (!isCorrectPosition(position)) + return false; + _places[position] = null; return true; } /// @@ -73,8 +88,7 @@ namespace AirBomber /// public T Get(int position) { - // TODO проверка позиции - return _places[position]; + return isCorrectPosition(position) ? _places[position] : null; } } }