From 542c68a68f343229b1429927d0961b1ab938d7bf Mon Sep 17 00:00:00 2001 From: Danil Kargin Date: Sun, 27 Nov 2022 16:17:52 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirFighter/AirFighterNotFoundException.cs | 20 +++++++ .../AirFighter/FormMapWithSetAirFighters.cs | 59 +++++++++++++------ AirFighter/AirFighter/MapsCollection.cs | 7 +-- .../AirFighter/SetAirFightersGeneric.cs | 10 +++- .../AirFighter/StorageOverflowException.cs | 21 +++++++ 5 files changed, 93 insertions(+), 24 deletions(-) create mode 100644 AirFighter/AirFighter/AirFighterNotFoundException.cs create mode 100644 AirFighter/AirFighter/StorageOverflowException.cs diff --git a/AirFighter/AirFighter/AirFighterNotFoundException.cs b/AirFighter/AirFighter/AirFighterNotFoundException.cs new file mode 100644 index 0000000..a79b9b4 --- /dev/null +++ b/AirFighter/AirFighter/AirFighterNotFoundException.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + internal class AirFighterNotFoundException : ApplicationException + { + public AirFighterNotFoundException(int i) : base($"Не найден объект по позиции { i}") { } + public AirFighterNotFoundException() : base() { } + public AirFighterNotFoundException(string message) : base(message) { } + public AirFighterNotFoundException(string message, Exception exception) : + base(message, exception) + { } + protected AirFighterNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/AirFighter/AirFighter/FormMapWithSetAirFighters.cs b/AirFighter/AirFighter/FormMapWithSetAirFighters.cs index 83514e4..20b5593 100644 --- a/AirFighter/AirFighter/FormMapWithSetAirFighters.cs +++ b/AirFighter/AirFighter/FormMapWithSetAirFighters.cs @@ -68,14 +68,25 @@ namespace AirFighter if (_airFighter != null) { DrawningObjectAirFighter airFighter = new(_airFighter); - if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airFighter) == 0) + try { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airFighter) == 0) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } } - else + catch (StorageOverflowException ex) { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show($"Ошибка добавления: {ex.Message}"); + } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); } } } @@ -113,14 +124,24 @@ namespace AirFighter 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("Не удалось удалить объект"); + } + }catch(AirFighterNotFoundException ex) + { + MessageBox.Show($"Ошибка удаления: {ex.Message}"); } - else + catch(Exception ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); } } /// @@ -237,14 +258,15 @@ namespace AirFighter { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + try { + _mapsCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBoxButtons.OK, MessageBoxIcon.Information); } - else + catch(Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -258,15 +280,16 @@ namespace AirFighter { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) + try { + _mapsCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadMaps(); } - else + catch(Exception ex) { - MessageBox.Show("Загрузка не удалась", "Результат", + MessageBox.Show($"Загрузка не удалась: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } diff --git a/AirFighter/AirFighter/MapsCollection.cs b/AirFighter/AirFighter/MapsCollection.cs index 9f35ac0..7a27c1e 100644 --- a/AirFighter/AirFighter/MapsCollection.cs +++ b/AirFighter/AirFighter/MapsCollection.cs @@ -114,11 +114,11 @@ namespace AirFighter /// /// /// < returns > - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new FileNotFoundException("Файл не найден"); } string bufferTextFromFile = ""; using (StreamReader sr = new(filename)) @@ -126,7 +126,7 @@ namespace AirFighter string checkMap = sr.ReadLine(); if (!checkMap.Contains("MapsCollection")) { - return false; + throw new FileFormatException("Формат данных в файле не правильный"); } bufferTextFromFile = sr.ReadLine(); _mapStorages.Clear(); @@ -148,7 +148,6 @@ namespace AirFighter bufferTextFromFile = sr.ReadLine(); } } - return true; } } } diff --git a/AirFighter/AirFighter/SetAirFightersGeneric.cs b/AirFighter/AirFighter/SetAirFightersGeneric.cs index 2800138..fbc5f64 100644 --- a/AirFighter/AirFighter/SetAirFightersGeneric.cs +++ b/AirFighter/AirFighter/SetAirFightersGeneric.cs @@ -52,6 +52,10 @@ namespace AirFighter /// public int Insert(T airFighter, int position) { + if (Count >= _maxCount) + { + throw new StorageOverflowException(); + } if (position < 0 && position > _maxCount) { return -1; @@ -69,14 +73,16 @@ namespace AirFighter /// public T Remove(int position) { - if (position >= 0 && position < _maxCount && _places[position] != null) + if (position >= 0 && position < Count) { T temp = _places[position]; _places.RemoveAt(position); return temp; } else - return null; + { + throw new AirFighterNotFoundException(position); + } } /// /// Получение объекта из набора по позиции diff --git a/AirFighter/AirFighter/StorageOverflowException.cs b/AirFighter/AirFighter/StorageOverflowException.cs new file mode 100644 index 0000000..b3e2378 --- /dev/null +++ b/AirFighter/AirFighter/StorageOverflowException.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + [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) { } + } +}