From e4e8b564046428853b9a72c2fa25f71c04fd4040 Mon Sep 17 00:00:00 2001 From: malimova Date: Mon, 11 Dec 2023 00:45:10 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B0=D1=8E=20+=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D1=88=D0=BB=D0=BE=D1=81=D1=8C=20=D0=BC=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B8=D0=B7=20=D0=BF=D1=80=D0=BE=D1=88=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE,=20=D0=BF=D0=BE=D1=82=D0=BE=D0=BC=D1=83=20?= =?UTF-8?q?=D1=87=D1=82=D0=BE=20=D0=B2=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D0=B8=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B2=D1=81=D0=B5=D0=B3=D0=B4=D0=B0=20bool=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BD=D0=BE=20true?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/AirBomber.csproj | 4 ++ AirBomber/AirBomber/AppSettings.json | 20 +++++++++ AirBomber/AirBomber/FormPlaneCollection.cs | 17 +++++--- .../AirBomber/PlanesGenericCollection.cs | 12 +++--- AirBomber/AirBomber/PlanesGenericStorage.cs | 18 ++++++-- AirBomber/AirBomber/Program.cs | 18 +++++++- AirBomber/AirBomber/SetGeneric.cs | 42 +++++++++---------- 7 files changed, 93 insertions(+), 38 deletions(-) create mode 100644 AirBomber/AirBomber/AppSettings.json diff --git a/AirBomber/AirBomber/AirBomber.csproj b/AirBomber/AirBomber/AirBomber.csproj index e65c673..22896a3 100644 --- a/AirBomber/AirBomber/AirBomber.csproj +++ b/AirBomber/AirBomber/AirBomber.csproj @@ -9,9 +9,13 @@ + + + + diff --git a/AirBomber/AirBomber/AppSettings.json b/AirBomber/AirBomber/AppSettings.json new file mode 100644 index 0000000..a604a73 --- /dev/null +++ b/AirBomber/AirBomber/AppSettings.json @@ -0,0 +1,20 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Information", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/log_.log", + "rollingInterval": "Day", + "outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}" + } + } + ], + "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], + "Properties": { + "Application": "AirBomber" + } + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs index fdbe73a..177fe0d 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -81,16 +81,17 @@ namespace AirBomber { return; } - if (obj + plane > -1) + try { + _ = obj + plane; MessageBox.Show("Объект добавлен"); _logger.Information("Объект добавлен"); pictureBoxCollection.Image = obj.ShowPlanes(); } - else + catch (Exception ex) { - MessageBox.Show("Не удалось добавить объект"); - _logger.Warning("Не удалось добавить объект"); + MessageBox.Show(ex.Message); + _logger.Warning($"Объект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}"); } } /// @@ -113,9 +114,10 @@ namespace AirBomber { return; } - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); try { + + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); if (obj - pos != null) { MessageBox.Show("Объект удален"); @@ -132,6 +134,11 @@ namespace AirBomber { MessageBox.Show(ex.Message); } + catch(Exception ex) + { + MessageBox.Show("Неверный ввод"); + _logger.Warning("Неверный ввод"); + } } /// /// Обновление рисунка по набору diff --git a/AirBomber/AirBomber/PlanesGenericCollection.cs b/AirBomber/AirBomber/PlanesGenericCollection.cs index 80f7989..5c38d3c 100644 --- a/AirBomber/AirBomber/PlanesGenericCollection.cs +++ b/AirBomber/AirBomber/PlanesGenericCollection.cs @@ -49,13 +49,13 @@ namespace AirBomber /// /// /// - public static int operator +(PlanesGenericCollection collect, T? obj) + public static bool operator +(PlanesGenericCollection collect, T obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj) ?? -1; + return collect._collection.Insert(obj); } /// /// Перегрузка оператора вычитания @@ -63,15 +63,15 @@ namespace AirBomber /// /// /// - public static bool operator -(PlanesGenericCollection collect, int pos) + public static T? operator -(PlanesGenericCollection collect, int + pos) { T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); - return true; } - return false; + return obj; } /// /// Получение объекта IMoveableObject diff --git a/AirBomber/AirBomber/PlanesGenericStorage.cs b/AirBomber/AirBomber/PlanesGenericStorage.cs index de74006..f519685 100644 --- a/AirBomber/AirBomber/PlanesGenericStorage.cs +++ b/AirBomber/AirBomber/PlanesGenericStorage.cs @@ -1,4 +1,5 @@ -using System; +using AirBomber.Exceptions; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -168,9 +169,20 @@ namespace AirBomber DrawningAirPlane? plane = elem?.CreateDrawningAirPlane(_separatorForObject, _pictureWidth, _pictureHeight); if (plane != null) { - if ((collection + plane) == -1) + if (!(collection + plane)) { - throw new Exception("Ошибка добавления в коллекцию"); + try + { + _ = collection + plane; + } + catch (PlaneNotFoundException e) + { + throw e; + } + catch (StorageOverflowException e) + { + throw e; + } } } } diff --git a/AirBomber/AirBomber/Program.cs b/AirBomber/AirBomber/Program.cs index d3007a7..dcdd25a 100644 --- a/AirBomber/AirBomber/Program.cs +++ b/AirBomber/AirBomber/Program.cs @@ -1,7 +1,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using NLog.Extensions.Logging; using Serilog; namespace AirBomber @@ -25,5 +24,22 @@ namespace AirBomber } //Application.Run(new FormPlaneCollection()); } + private static void ConfigureServices(ServiceCollection services) + { + services.AddSingleton().AddLogging(option => + { + string[] path = Directory.GetCurrentDirectory().Split('\\'); + string pathNeed = ""; + for (int i = 0; i < path.Length - 3; i++) + { + pathNeed += path[i] + "\\"; + } + var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(path: $"{pathNeed}AppSettings.json", optional: false, reloadOnChange: true).Build(); + var logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger(); + + option.SetMinimumLevel(LogLevel.Information); + option.AddSerilog(logger); + }); + } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index 636bc27..f736d9e 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -36,9 +36,8 @@ namespace AirBomber /// /// Добавляемый самолет /// - public int Insert(T plane) + public bool Insert(T plane) { - //was TODO return Insert(plane, 0); } /// @@ -47,19 +46,15 @@ namespace AirBomber /// Добавляемый самолет /// Позиция /// - public int Insert(T plane, int position) + public bool Insert(T plane, int position) { - // TODO проверка позиции DONE - // TODO проверка, что элемент массива по этой позиции пустой,если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции if (position < 0 || position >= _maxCount) - { + throw new StorageOverflowException("Impossible to insert"); + + if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - } - _places.Insert(position, plane); - return position; + _places.Insert(0, plane); + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -68,13 +63,11 @@ namespace AirBomber /// public bool Remove(int position) { - // TODO проверка позиции DONE - // TODO удаление объекта из массива, присвоив элементу массива значение null - if (!(position >= 0 && position < Count) || _places[position] == null) - { + if (position >= Count || position < 0) + throw new PlaneNotFoundException("Invalid operation"); + if (_places[position] == null) throw new PlaneNotFoundException(position); - } - _places[position] = null; + _places.RemoveAt(position); return true; } /// @@ -95,11 +88,14 @@ namespace AirBomber } set { - // TODO проверка позиции DONE - // TODO проверка свободных мест в списке DONE - // TODO вставка в список по позиции DONE - if (position < 0 || position >= Count || Count == _maxCount) return; - _places.Insert(position, value); + try + { + Insert(value, position); + } + catch + { + return; + } } } ///