From 3f9f13fd890dae88d8571cb08f4fed8be094f3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Fri, 2 Dec 2022 18:32:54 +0300 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 --- .../AirPlaneWithRadar/FormMapWithSetPlains.cs | 48 ++++++++++++------- .../AirPlaneWithRadar/MapsCollection.cs | 12 ++--- .../PlainNotFoundException.cs | 19 ++++++++ .../StorageOverFullException.cs | 20 ++++++++ 4 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 AirPlaneWithRadar/AirPlaneWithRadar/PlainNotFoundException.cs create mode 100644 AirPlaneWithRadar/AirPlaneWithRadar/StorageOverFullException.cs diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/FormMapWithSetPlains.cs b/AirPlaneWithRadar/AirPlaneWithRadar/FormMapWithSetPlains.cs index a2c6cc7..5a14af4 100644 --- a/AirPlaneWithRadar/AirPlaneWithRadar/FormMapWithSetPlains.cs +++ b/AirPlaneWithRadar/AirPlaneWithRadar/FormMapWithSetPlains.cs @@ -120,15 +120,24 @@ namespace AirPlaneWithRadar } if (maskedTextBoxPosition.Text == null) return; - int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - (pos - 1)) != null) + try { - MessageBox.Show("Объект удален"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - } - else + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - (pos - 1)) != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + }catch(PlainNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show("Wrong id"); + }catch(Exception ex) + { + MessageBox.Show("Error of delete"); } } private void ButtonShowStorage_Click(object sender, EventArgs e) @@ -175,16 +184,18 @@ namespace AirPlaneWithRadar } private void SaveToolStrip_Click(object sender, EventArgs e) { + if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + try { - MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _mapsCollection.SaveData(saveFileDialog.FileName); } - else - { + catch(Exception ex) { MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } } @@ -192,17 +203,20 @@ namespace AirPlaneWithRadar { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName) && _mapsCollection.Count != 0) + try { + _mapsCollection.LoadData(openFileDialog.FileName); + } + catch (Exception ex) + { + MessageBox.Show("Не удалось загрузить", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadMaps(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - } - else - { - MessageBox.Show("Не удалось загрузить", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + } } diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/MapsCollection.cs b/AirPlaneWithRadar/AirPlaneWithRadar/MapsCollection.cs index 7c476e0..1211360 100644 --- a/AirPlaneWithRadar/AirPlaneWithRadar/MapsCollection.cs +++ b/AirPlaneWithRadar/AirPlaneWithRadar/MapsCollection.cs @@ -54,7 +54,7 @@ namespace AirPlaneWithRadar return _mapStorages[ind]; } } - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -69,22 +69,22 @@ namespace AirPlaneWithRadar } } - return true; + } - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new Exception("File not found"); } using (StreamReader sr = new StreamReader(filename)) { if (!sr.ReadLine().Equals("MapsCollection")) { - return false; + throw new Exception("Wrong file"); } _mapStorages.Clear(); string line = sr.ReadLine(); @@ -109,7 +109,7 @@ namespace AirPlaneWithRadar line = sr.ReadLine(); } } - return true; + } } diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/PlainNotFoundException.cs b/AirPlaneWithRadar/AirPlaneWithRadar/PlainNotFoundException.cs new file mode 100644 index 0000000..c2b8cf2 --- /dev/null +++ b/AirPlaneWithRadar/AirPlaneWithRadar/PlainNotFoundException.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 PlainNotFoundException : ApplicationException + { + public PlainNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public PlainNotFoundException() : base() { } + public PlainNotFoundException(string message) : base(message) { } + public PlainNotFoundException(string message, Exception exception) : base(message, exception) { } + protected PlainNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/StorageOverFullException.cs b/AirPlaneWithRadar/AirPlaneWithRadar/StorageOverFullException.cs new file mode 100644 index 0000000..627eb72 --- /dev/null +++ b/AirPlaneWithRadar/AirPlaneWithRadar/StorageOverFullException.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 AirPlaneWithRadar +{ + [Serializable] + internal class StorageOverFullException : ApplicationException + { + public StorageOverFullException(int count) : base($"В наборе превышено допустимое количество: {count}") { } + public StorageOverFullException() : base() { } + public StorageOverFullException(string message) : base(message) { } + public StorageOverFullException(string message, Exception exception) : base(message, exception) { } + protected StorageOverFullException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + + } +}