diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs new file mode 100644 index 0000000..b9f1b52 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar +{ + [Serializable] + internal class AirplaneNotFoundException : ApplicationException + { + public AirplaneNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public AirplaneNotFoundException() : base() { } + public AirplaneNotFoundException(string message) : base(message) { } + public AirplaneNotFoundException(string message, Exception exception) : base(message, exception) { } + protected AirplaneNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs index 16181cf..29e4aaa 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs @@ -85,20 +85,30 @@ namespace AirplaneWithRadar } private void AddAirplane(DrawningAirplane airplane) { - if (ListBoxMaps.SelectedIndex == -1) + try { - return; + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectAirplane(airplane) != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } } - if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectAirplane(airplane) != -1) + catch (StorageOverflowException ex) { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + MessageBox.Show($"Ошибка хранилище переполнено: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } - else + catch (ArgumentException ex) { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } - } /// /// Добавление объекта @@ -131,15 +141,25 @@ namespace AirplaneWithRadar } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - - if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? String.Empty] - pos != null) + try { - MessageBox.Show("Объект удалён"); - pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? String.Empty].ShowSet(); + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? String.Empty] - pos != null) + { + MessageBox.Show("Объект удалён"); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? String.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } } - else + catch (AirplaneNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Ошибка удаления: {ex.Message}"); + } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); } } /// @@ -253,7 +273,7 @@ namespace AirplaneWithRadar } catch (Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не сохранилось:{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs index 6ebcebc..b9a7f88 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs @@ -26,20 +26,29 @@ // Добавление объекта в набор на конкретную позицию public int Insert(T airplane, int position) { - if (position >= _maxCount || position < 0) return -1; + if (Count == _maxCount) + { + throw new StorageOverflowException(_maxCount); + } + if (position < 0 || position > _maxCount) return -1; _places.Insert(position, airplane); return position; } // Удаление объекта из набора с конкретной позиции public T Remove(int position) { - // проверка позиции - if (position >= _maxCount || position < 0) return null; - // удаление объекта из массива, присовив элементу массива значение null - T temp = _places[position]; - _places.RemoveAt(position); - return temp; - } + if (position < Count || position > 0) + { + if (_places.ElementAt(position) != null) + { + T ship = _places[position]; + _places.RemoveAt(position); + return ship; + } + throw new AirplaneNotFoundException(position); + } + return null; + } // Получение объекта из набора по позиции public T this[int position] { diff --git a/AirplaneWithRadar/AirplaneWithRadar/StorageOverflowException.cs b/AirplaneWithRadar/AirplaneWithRadar/StorageOverflowException.cs new file mode 100644 index 0000000..ae5e87f --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/StorageOverflowException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar +{ + [Serializable] + internal class StorageOverflowException : ApplicationException + { + public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: {count}") { } + public StorageOverflowException() : base() { } + public StorageOverflowException(string message) : base(message) { } + public StorageOverflowException(string message, Exception exception) : base(message, exception) { } + protected StorageOverflowException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +}