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) { } + + } +}