diff --git a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs index 31b77aa..70af78c 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs @@ -94,13 +94,9 @@ namespace AirplaneWithRadar public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setAirplanes.Count; i++) + foreach (var airplane in _setAirplanes.GetAirplanes()) { - var airplane = _setAirplanes.Get(i); - if (airplane != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, airplane); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, airplane); } return new(_pictureWidth, _pictureHeight); } @@ -125,11 +121,11 @@ namespace AirplaneWithRadar int j = _setAirplanes.Count - 1; for (int i = 0; i < _setAirplanes.Count; i++) { - if (_setAirplanes.Get(i) == null) + if (_setAirplanes[i] == null) { for (; j > i; j--) { - var airplane = _setAirplanes.Get(j); + var airplane = _setAirplanes[j]; if (airplane != null) { _setAirplanes.Insert(airplane, i); @@ -178,14 +174,12 @@ namespace AirplaneWithRadar /// private void DrawAirplanes(Graphics g) { - int numInRow = _pictureWidth / _placeSizeWidth; - int maxLeft = (numInRow - 1) * _placeSizeWidth; - for (int i = 0; i < _setAirplanes.Count; i++) + foreach (var airplane in _setAirplanes.GetAirplanes()) { - var airplane = _setAirplanes.Get(i); - airplane?.SetObject(maxLeft - i % numInRow * _placeSizeWidth + 5, i / numInRow * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - airplane?.DrawingObject(g); + // TODO установка позиции + airplane.DrawingObject(g); } } + } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs index 2a28e06..56ab757 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs @@ -14,20 +14,25 @@ namespace AirplaneWithRadar where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetAirplanesGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -36,7 +41,13 @@ namespace AirplaneWithRadar /// public int Insert(T airplane) { - return Insert(airplane, 0); + // TODO вставка в начало набора + // TODO проверка на _maxCount + if (Count < _maxCount) + { + return Insert(airplane, 0); + } + return -1; } /// /// Добавление объекта в набор на конкретную позицию @@ -46,21 +57,13 @@ namespace AirplaneWithRadar /// public int Insert(T airplane, int position) { - int positionNullElement = position; - while (Get(positionNullElement) != null) - { - positionNullElement++; - } - if (positionNullElement > Count || positionNullElement < 0) + // TODO проверка позиции + // TODO вставка по позиции + if (position < 0 || position >= _maxCount) { return -1; } - while (positionNullElement != position) - { - _places[positionNullElement] = _places[positionNullElement - 1]; - positionNullElement--; - } - _places[position] = airplane; + _places.Insert(position, airplane); return position; } /// @@ -70,32 +73,59 @@ namespace AirplaneWithRadar /// public T Remove(int position) { - if (position > Count || position < 0) + // TODO проверка позиции + if (position < 0 || position >= Count) { return null; } - else - { - var removedObject = _places[position]; - _places[position] = null; - return removedObject; - } + T airplane = _places[position]; + _places.RemoveAt(position); + return airplane; } /// /// Получение объекта из набора по позиции /// /// /// - public T Get(int position) + public T this[int position] { - if (position > Count || position < 0) - { - return null; - } - else + get { + // TODO проверка позиции + if (position < 0 || position >= Count) + { + return null; + } return _places[position]; } + set + { + // TODO проверка позиции + // TODO вставка в список по позиции + if (position < 0 || position >= _maxCount) + { + return; + } + _places.Add(value); + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetAirplanes() + { + foreach (var airplane in _places) + { + if (airplane != null) + { + yield return airplane; + } + else + { + yield break; + } + } } } }