diff --git a/Sailboat/Sailboat/BoatNotFoundException.cs b/Sailboat/Sailboat/BoatNotFoundException.cs new file mode 100644 index 0000000..f5c2284 --- /dev/null +++ b/Sailboat/Sailboat/BoatNotFoundException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + +namespace Sailboat.Exceptions +{ + internal class BoatNotFoundException : ApplicationException + { + public BoatNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public BoatNotFoundException() : base() { } + public BoatNotFoundException(string message) : base(message) { } + public BoatNotFoundException(string message, Exception exception) : base(message, exception) { } + protected BoatNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} \ No newline at end of file diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index 0eb3a8a..4f3476e 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -97,7 +97,6 @@ namespace Sailboat.Generics /// Сохранение информации по лодкам в хранилище в файл /// /// Путь и имя файла - /// true - сохранение прошло успешно, false - ошибка при сохранении данных public bool SaveData(string filename) { if (File.Exists(filename)) @@ -116,11 +115,11 @@ namespace Sailboat.Generics } if (data.Length == 0) { - return false; + throw new Exception("Невалиданя операция, нет данных для сохранения"); } using (StreamWriter writer = new StreamWriter(filename)) { - writer.Write($"BoatStorage{Environment.NewLine}{data}"); + writer.Write($"PlaneStorage{Environment.NewLine}{data}"); } return true; } @@ -133,7 +132,7 @@ namespace Sailboat.Generics { if (!File.Exists(filename)) { - return false; + throw new Exception("Файл не найден"); } using (StreamReader bs = File.OpenText(filename)) @@ -141,11 +140,11 @@ namespace Sailboat.Generics string str = bs.ReadLine(); if (str == null || str.Length == 0) { - return false; + throw new Exception("Нет данных для загрузки"); } if (!str.StartsWith("BoatStorage")) { - return false; + throw new Exception("Неверный формат данных"); } _boatStorages.Clear(); @@ -172,7 +171,7 @@ namespace Sailboat.Generics { if (!(collection + boat)) { - return false; + throw new Exception("Ошибка добавления в коллекцию"); } } } diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index 1aa637c..ed93da7 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using System.Windows.Forms; using Sailboat.DrawingObjects; +using Sailboat.Exceptions; using Sailboat.Generics; using Sailboat.MovementStrategy; @@ -106,16 +107,23 @@ namespace Sailboat return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (obj - pos != null) + try { - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = obj.ShowBoats(); + if (obj - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = obj.ShowBoats(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } } - else + catch (BoatNotFoundException ex) { MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show(ex.Message); } - } private void buttonRefreshCollection_Click(object sender, EventArgs e) @@ -158,7 +166,7 @@ namespace Sailboat { return; } - if (MessageBox.Show($"Удалить объект { listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); @@ -168,18 +176,18 @@ namespace Sailboat private void SaveToolStripMenuItem_Click(object sender, EventArgs e) { - if (saveFileDialog.ShowDialog() == DialogResult.OK) + try { - if (_storage.SaveData(saveFileDialog.FileName)) - { - MessageBox.Show("Сохранение прошло успешно", - "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - } - else - { - MessageBox.Show("Не сохранилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); - } + _storage.SaveData(saveFileDialog.FileName); + MessageBox.Show("Сохранение прошло успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show("Не сохранилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не сохранилось: {ex.Message}", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -187,15 +195,27 @@ namespace Sailboat { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.LoadData(openFileDialog.FileName)) + try { + _storage.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($"Файл {openFileDialog.FileName} успешно загружен"); + foreach (var collection in _storage.Keys) + { + listBoxStorages.Items.Add(collection); + } + ReloadObjects(); + { + MessageBox.Show("Загрузка прошла успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } } - else + catch (Exception ex) { - MessageBox.Show("Не загрузилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + Log.Warning("Не удалось загрузить"); + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } ReloadObjects(); diff --git a/Sailboat/Sailboat/StorageOverflowException.cs b/Sailboat/Sailboat/StorageOverflowException.cs new file mode 100644 index 0000000..5da9a41 --- /dev/null +++ b/Sailboat/Sailboat/StorageOverflowException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + +namespace Sailboat.Exceptions +{ + [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) { } + } +} \ No newline at end of file