From 086e1efc24330a5772609cb031aa61abaa61cef3 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Fri, 18 Nov 2022 11:08:22 +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 --- Liner/Liner/FormMapWithSetShips.cs | 62 +++++++++++++++++-------- Liner/Liner/MapsCollection.cs | 11 ++--- Liner/Liner/SetShipsGeneric.cs | 21 ++++++--- Liner/Liner/ShipNotFoundException.cs | 19 ++++++++ Liner/Liner/StorageOverflowException.cs | 19 ++++++++ 5 files changed, 101 insertions(+), 31 deletions(-) create mode 100644 Liner/Liner/ShipNotFoundException.cs create mode 100644 Liner/Liner/StorageOverflowException.cs diff --git a/Liner/Liner/FormMapWithSetShips.cs b/Liner/Liner/FormMapWithSetShips.cs index e02a7e0..cb883f8 100644 --- a/Liner/Liner/FormMapWithSetShips.cs +++ b/Liner/Liner/FormMapWithSetShips.cs @@ -57,20 +57,30 @@ namespace Liner } private void AddShip(DrawingShip ship) { - if (ListBoxMaps.SelectedIndex == -1) + try { - return; + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectShip(ship) != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } } - if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectShip(ship) != -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); } - } private void ButtonRemoveShip_Click(object sender, EventArgs e) { @@ -87,14 +97,25 @@ namespace Liner 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 (ShipNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Ошибка удаления: {ex.Message}"); + } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); } } private void ButtonShowStorage_Click(object sender, EventArgs e) @@ -174,13 +195,15 @@ namespace Liner { 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); } } } @@ -189,14 +212,15 @@ namespace Liner { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) + try { + _mapsCollection.LoadData(openFileDialog.FileName); ReloadMaps(); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } - else + catch(Exception ex) { - MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не загрузилось:{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/Liner/Liner/MapsCollection.cs b/Liner/Liner/MapsCollection.cs index 80106d5..3eb2610 100644 --- a/Liner/Liner/MapsCollection.cs +++ b/Liner/Liner/MapsCollection.cs @@ -44,7 +44,7 @@ namespace Liner return null; } } - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -58,20 +58,19 @@ namespace Liner 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 FileNotFoundException("Файл не найден"); } using (StreamReader sr = new(filename)) { string str = ""; if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection")) { - return false; + throw new FileFormatException("Формат данных в файле не правильный"); } _mapStorages.Clear(); while ((str = sr.ReadLine()) != null) @@ -95,7 +94,7 @@ namespace Liner _mapStorages[tempElem[0]].LoadData(tempElem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } } - return true; + } } diff --git a/Liner/Liner/SetShipsGeneric.cs b/Liner/Liner/SetShipsGeneric.cs index 6e6c3b5..6d682da 100644 --- a/Liner/Liner/SetShipsGeneric.cs +++ b/Liner/Liner/SetShipsGeneric.cs @@ -23,19 +23,28 @@ namespace Liner } public int Insert(T ship, int position) { - if (position < 0 || position > Count || _maxCount == Count) return -1; + if (Count == _maxCount) + { + throw new StorageOverflowException(_maxCount); + } + if (position < 0 || position > _maxCount) return -1; _places.Insert(position, ship); return position; } public T Remove(int position) { - if (position >= Count || position < 0) + if (position < Count || position > 0) { - return null; + if (_places.ElementAt(position) != null) + { + T ship = _places[position]; + _places.RemoveAt(position); + return ship; + } + throw new ShipNotFoundException(position); + } - T ship = _places[position]; - _places.RemoveAt(position); - return ship; + return null; } public T this[int position] { diff --git a/Liner/Liner/ShipNotFoundException.cs b/Liner/Liner/ShipNotFoundException.cs new file mode 100644 index 0000000..63acc0c --- /dev/null +++ b/Liner/Liner/ShipNotFoundException.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 Liner +{ + [Serializable] + internal class ShipNotFoundException: ApplicationException + { + public ShipNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public ShipNotFoundException() : base() { } + public ShipNotFoundException(string message) : base(message) { } + public ShipNotFoundException(string message, Exception exception) : base(message, exception) { } + protected ShipNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} diff --git a/Liner/Liner/StorageOverflowException.cs b/Liner/Liner/StorageOverflowException.cs new file mode 100644 index 0000000..2f3885f --- /dev/null +++ b/Liner/Liner/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 Liner +{ + [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){} + } +}