diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs new file mode 100644 index 0000000..abee0ee --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + + +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 contex) : base(info, contex) { } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs index 21ad8a5..2fb5ea9 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs @@ -147,14 +147,26 @@ namespace AirplaneWithRadar return; } 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}"); } } /// @@ -223,13 +235,14 @@ namespace AirplaneWithRadar { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + try { + _mapsCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } - else + catch (Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -242,14 +255,15 @@ namespace AirplaneWithRadar { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) + try { + _mapsCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadMaps(); } - else + catch (Exception ex) { - MessageBox.Show("Не удалось загрузить", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не удалось загрузить: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs index 91c02a4..61fe105 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs @@ -103,7 +103,7 @@ namespace AirplaneWithRadar /// /// Путь и имя файла /// - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -117,25 +117,24 @@ namespace AirplaneWithRadar sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); } } - return true; } /// /// Загрузка нформации по автомобилям на парковках из файла /// /// /// - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new Exception("Файл не найден"); } using (StreamReader sr = new(filename)) { string strFromFile = ""; if ((strFromFile = sr.ReadLine()) == null || !strFromFile.Contains("MapsCollection")) { - return false; + throw new Exception("Формат данных в файле не правильный"); } _mapStorages.Clear(); while ((strFromFile = sr.ReadLine()) != null) @@ -158,7 +157,6 @@ namespace AirplaneWithRadar _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } } - return true; } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs index 56ab757..43b5ce2 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs @@ -61,6 +61,7 @@ namespace AirplaneWithRadar // TODO вставка по позиции if (position < 0 || position >= _maxCount) { + throw new StorageOverflowException(_maxCount); return -1; } _places.Insert(position, airplane); @@ -74,13 +75,22 @@ namespace AirplaneWithRadar public T Remove(int position) { // TODO проверка позиции - if (position < 0 || position >= Count) + if (position < Count && position >= 0) { + if (_places.ElementAt(position) != null) + { + T result = _places.ElementAt(position); + + _places.RemoveAt(position); + + return result; + } + return null; } - T airplane = _places[position]; - _places.RemoveAt(position); - return airplane; + throw new AirplaneNotFoundException(position); + return null; + } /// /// Получение объекта из набора по позиции diff --git a/AirplaneWithRadar/AirplaneWithRadar/StorageOverflowException.cs b/AirplaneWithRadar/AirplaneWithRadar/StorageOverflowException.cs new file mode 100644 index 0000000..377ea17 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/StorageOverflowException.cs @@ -0,0 +1,15 @@ +using System.Runtime.Serialization; + +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 contex) : base(info, contex) { } + } +} +