From b15b824e76d70f0a7a3054821e6084a5d37f7604 Mon Sep 17 00:00:00 2001 From: Kirill <117719052+KirillFirsof@users.noreply.github.com> Date: Fri, 29 Dec 2023 21:21:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormTractorCollection.cs | 21 ++++++++----------- .../RPP_FirstLaba_Tractor/SetGeneric.cs | 14 +++++++++++-- .../TractorsGenericCollection.cs | 19 +++++++++++++---- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs index 245df64..4e55f2d 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs @@ -130,17 +130,19 @@ namespace ProjectTractor form.Show(); Action? tractorDelegate = new((m) => { - bool isAdditionSuccessful = (obj + m); - if (isAdditionSuccessful) + try { + bool isAdditionSuccessful = (obj + m); MessageBox.Show("Объект добавлен"); Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); m.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height); pictureBoxCollection.Image = obj.ShowTractors(); + } - else + catch { MessageBox.Show("Не удалось добавить объект"); + Log.Information($"Не удалось добавить объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); } }); form.AddEvent(tractorDelegate); @@ -171,18 +173,13 @@ namespace ProjectTractor int pos = Convert.ToInt32(maskedTextBoxNumber.Text); try { - if (obj - pos != null) - { - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = obj.ShowTractors(); - } - else - { - MessageBox.Show("Не удалось удалить объект"); - } + bool m = obj - pos; + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = obj.ShowTractors(); } catch (TractorNotFoundException ex) { + Log.Warning($"Не удалось удалить объкт"); MessageBox.Show(ex.Message); } diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs index d3f8a13..639f129 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ProjectTractor.Exceptions; +using Serilog; namespace ProjectTractor.Generics { @@ -77,9 +78,18 @@ namespace ProjectTractor.Generics { get { - if (!(position >= 0 && position <= Count)) + try + { + if (!(position >= 0 && position <= Count)) + return null; + return _places[position]; + } + catch (TractorNotFoundException ex) + { return null; - return _places[position]; + Log.Warning($"Не удалось удалить объкт"); + MessageBox.Show(ex.Message); + } } set { diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs index 8cc895e..f96230a 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs @@ -5,6 +5,8 @@ using System.Text; using System.Threading.Tasks; using ProjectTractor.DrawningObjects; using ProjectTractor.MovementStrategy; +using ProjectTractor.Exceptions; +using Serilog; namespace ProjectTractor.Generics { @@ -78,12 +80,21 @@ namespace ProjectTractor.Generics public static bool operator -(TractorsGenericCollection collect, int pos) { - T? obj = collect._collection[pos]; - if (obj != null) + try { - collect._collection.Remove(pos); + 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); } - return true; } /// /// Получение объектов коллекции