From 33b1fe2e2bfe11106e27ad52639d58ff231aab7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Tue, 12 Dec 2023 12:38:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=B0=D1=8F=207?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatNotFoundException.cs | 17 +++++++ Sailboat/Sailboat/BoatsGenericStorage.cs | 13 +++--- .../Sailboat/FormBoatCollection.Designer.cs | 6 +-- Sailboat/Sailboat/FormBoatCollection.cs | 46 +++++++++++++------ Sailboat/Sailboat/StorageOverflowException.cs | 16 +++++++ 5 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 Sailboat/Sailboat/BoatNotFoundException.cs create mode 100644 Sailboat/Sailboat/StorageOverflowException.cs diff --git a/Sailboat/Sailboat/BoatNotFoundException.cs b/Sailboat/Sailboat/BoatNotFoundException.cs new file mode 100644 index 0000000..79aa34c --- /dev/null +++ b/Sailboat/Sailboat/BoatNotFoundException.cs @@ -0,0 +1,17 @@ +using System.Runtime.Serialization; + +namespace Sailboat.Exceptions +{ + [Serializable] + 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 a1f376b..7108ca1 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 fs = File.OpenText(filename)) @@ -141,11 +140,11 @@ namespace Sailboat.Generics string str = fs.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.Designer.cs b/Sailboat/Sailboat/FormBoatCollection.Designer.cs index 0de397b..7968f58 100644 --- a/Sailboat/Sailboat/FormBoatCollection.Designer.cs +++ b/Sailboat/Sailboat/FormBoatCollection.Designer.cs @@ -182,24 +182,24 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(177, 26); + SaveToolStripMenuItem.Size = new Size(224, 26); SaveToolStripMenuItem.Text = "Сохранение"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(177, 26); + LoadToolStripMenuItem.Size = new Size(224, 26); LoadToolStripMenuItem.Text = "Загрузка"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // // openFileDialog // + openFileDialog.FileName = "openFileDialog"; openFileDialog.Filter = "txt file | *.txt"; // // saveFileDialog // - saveFileDialog.FileName = "BoatSave"; saveFileDialog.Filter = "txt file | *.txt"; // // FormBoatCollection diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index ad3b6a0..b5f3d00 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; @@ -91,14 +92,21 @@ 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); } } @@ -153,15 +161,16 @@ namespace Sailboat { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.SaveData(saveFileDialog.FileName)) + try { + _storage.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); } } } @@ -170,18 +179,25 @@ 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(); } - else + catch (Exception ex) { - MessageBox.Show("Не загрузилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + Log.Warning("Не удалось загрузить"); + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } - ReloadObjects(); } } -} +} \ No newline at end of file diff --git a/Sailboat/Sailboat/StorageOverflowException.cs b/Sailboat/Sailboat/StorageOverflowException.cs new file mode 100644 index 0000000..00e7a03 --- /dev/null +++ b/Sailboat/Sailboat/StorageOverflowException.cs @@ -0,0 +1,16 @@ +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