From 6747efcf15178fbad7ce9ad0d9d432ad597387f0 Mon Sep 17 00:00:00 2001 From: Kirill <117719052+KirillFirsof@users.noreply.github.com> Date: Sat, 20 Jan 2024 09:35:50 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=B2=D1=81=D0=B5=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionCreationErrorException.cs | 22 ++++++++++ .../FormTractorCollection.cs | 41 +++++++++++++------ .../RPP_FirstLaba_Tractor/SetGeneric.cs | 19 ++++++--- .../TractorsGenericCollection.cs | 9 ---- .../TractorsGenericStorage.cs | 13 +++++- 5 files changed, 76 insertions(+), 28 deletions(-) create mode 100644 RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/CollectionCreationErrorException.cs diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/CollectionCreationErrorException.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/CollectionCreationErrorException.cs new file mode 100644 index 0000000..7fbed22 --- /dev/null +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/CollectionCreationErrorException.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + +namespace ProjectTractor.Exceptions +{ + [Serializable] + internal class CollectionCreationErrorException : ApplicationException + { + public CollectionCreationErrorException(string name) : base($"Коллекция с именем {name} уже существует") { } + public CollectionCreationErrorException() : base() { } + public CollectionCreationErrorException(string message, Exception exception) : + base(message, exception) + { } + protected CollectionCreationErrorException(SerializationInfo info, + StreamingContext contex) : base(info, contex) { } + } + +} diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs index 4e55f2d..bbb93a5 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs @@ -72,9 +72,17 @@ namespace ProjectTractor MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _storage.AddSet(textBoxStorageName.Text); - ReloadObjects(); - Log.Information($"Добавлен набор: {textBoxStorageName.Text}"); + try + { + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + Log.Information($"Добавлен набор: {textBoxStorageName.Text}"); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + Log.Information($"Не удалось добавить коллекцию: Коллекция с именем {listBoxStorages.SelectedItem.ToString() ?? string.Empty} уже существует"); + } } /// /// Выбор набора @@ -139,9 +147,9 @@ namespace ProjectTractor pictureBoxCollection.Image = obj.ShowTractors(); } - catch + catch (Exception ex) { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show(ex.Message); Log.Information($"Не удалось добавить объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); } }); @@ -155,7 +163,9 @@ namespace ProjectTractor /// private void ButtonRemoveTractor_Click(object sender, EventArgs e) { - if (listBoxStorages.SelectedIndex == -1) + try + { + if (listBoxStorages.SelectedIndex == -1) { return; } @@ -171,16 +181,23 @@ namespace ProjectTractor return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - try - { - bool m = obj - pos; - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = obj.ShowTractors(); + + if (obj - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = obj.ShowTractors(); + Log.Information($"Обьект удален из набора {listBoxStorages.SelectedItem.ToString()}"); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + Log.Warning($"Обьект не удален из набора {listBoxStorages.SelectedItem.ToString()}"); + } } catch (TractorNotFoundException ex) { - Log.Warning($"Не удалось удалить объкт"); MessageBox.Show(ex.Message); + Log.Warning($"Обьект не найден: {ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}"); } } diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs index 639f129..2996fd2 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs @@ -67,7 +67,14 @@ namespace ProjectTractor.Generics { if (!(position >= 0 && position < Count)) throw new TractorNotFoundException(position); - _places.RemoveAt(position); + try + { + _places.RemoveAt(position); + } + catch + { + throw new TractorNotFoundException(position); + } } /// /// Получение объекта из набора по позиции @@ -81,15 +88,15 @@ namespace ProjectTractor.Generics try { if (!(position >= 0 && position <= Count)) - return null; + throw new TractorNotFoundException(position); + return _places[position]; } - catch (TractorNotFoundException ex) + catch { - return null; - Log.Warning($"Не удалось удалить объкт"); - MessageBox.Show(ex.Message); + throw new TractorNotFoundException(position); } + } set { diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs index f96230a..ba1ca31 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs @@ -80,21 +80,12 @@ namespace ProjectTractor.Generics public static bool operator -(TractorsGenericCollection collect, int pos) { - try - { T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); } return true; - } - catch (TractorNotFoundException ex) - { - return false; - Log.Warning($"Не удалось удалить объкт"); - MessageBox.Show(ex.Message); - } } /// /// Получение объектов коллекции diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericStorage.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericStorage.cs index 162d731..45f3882 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericStorage.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericStorage.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using ProjectTractor.DrawningObjects; using ProjectTractor.MovementStrategy; using ProjectTractor.Generics; +using ProjectTractor.Exceptions; namespace ProjectTractor { @@ -60,9 +61,19 @@ namespace ProjectTractor /// Название набора public void AddSet(string name) { - _tractorStorages.Add(name, + try + { + if (_tractorStorages.ContainsKey(name)) + throw new CollectionCreationErrorException(name); + + _tractorStorages.Add(name, new TractorsGenericCollection(_pictureWidth, _pictureHeight)); + } + catch + { + throw new CollectionCreationErrorException(name); + } } /// /// Удаление набора