From 557028cc392ede974b525d3a102a79bb2dba4362 Mon Sep 17 00:00:00 2001 From: malimova Date: Sun, 10 Dec 2023 20:53:32 +0400 Subject: [PATCH 1/9] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20Lab7?= =?UTF-8?q?.=20=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20PlaneNot?= =?UTF-8?q?FoundException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/PlaneNotFoundException.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 AirBomber/AirBomber/PlaneNotFoundException.cs diff --git a/AirBomber/AirBomber/PlaneNotFoundException.cs b/AirBomber/AirBomber/PlaneNotFoundException.cs new file mode 100644 index 0000000..0f5c7e6 --- /dev/null +++ b/AirBomber/AirBomber/PlaneNotFoundException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class PlaneNotFoundException: ApplicationException + { + public PlaneNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public PlaneNotFoundException() : base() { } + public PlaneNotFoundException(string message) : base(message) { } + public PlaneNotFoundException(string message, Exception exception) : base(message, exception) + { } + protected PlaneNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} -- 2.25.1 From 54a57600a46b7bc5929b03b0fb78079edc5f5637 Mon Sep 17 00:00:00 2001 From: malimova Date: Sun, 10 Dec 2023 20:56:51 +0400 Subject: [PATCH 2/9] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20StorageOverflowException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/PlaneNotFoundException.cs | 1 + .../AirBomber/StorageOverflowException.cs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 AirBomber/AirBomber/StorageOverflowException.cs diff --git a/AirBomber/AirBomber/PlaneNotFoundException.cs b/AirBomber/AirBomber/PlaneNotFoundException.cs index 0f5c7e6..211f8a9 100644 --- a/AirBomber/AirBomber/PlaneNotFoundException.cs +++ b/AirBomber/AirBomber/PlaneNotFoundException.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; namespace AirBomber { + [Serializable] internal class PlaneNotFoundException: ApplicationException { public PlaneNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } diff --git a/AirBomber/AirBomber/StorageOverflowException.cs b/AirBomber/AirBomber/StorageOverflowException.cs new file mode 100644 index 0000000..32e408b --- /dev/null +++ b/AirBomber/AirBomber/StorageOverflowException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + [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) { } + } +} -- 2.25.1 From b3844b27e523c20015595b0af6af82ca4535fa4a Mon Sep 17 00:00:00 2001 From: malimova Date: Sun, 10 Dec 2023 21:05:30 +0400 Subject: [PATCH 3/9] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B?= =?UTF-8?q?=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20PlanesGenericStorage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/PlanesGenericStorage.cs | 10 +++++----- AirBomber/AirBomber/SetGeneric.cs | 7 +++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/AirBomber/AirBomber/PlanesGenericStorage.cs b/AirBomber/AirBomber/PlanesGenericStorage.cs index 3de897f..de74006 100644 --- a/AirBomber/AirBomber/PlanesGenericStorage.cs +++ b/AirBomber/AirBomber/PlanesGenericStorage.cs @@ -112,7 +112,7 @@ namespace AirBomber } if (data.Length == 0) { - return false; + throw new Exception("Невалидная операция, нет данных для сохранения"); } using StreamWriter sw = new(filename); @@ -128,7 +128,7 @@ namespace AirBomber { if (!File.Exists(filename)) { - return false; + throw new Exception("Файл не найден"); } using (StreamReader sr = new(filename)) { @@ -136,12 +136,12 @@ namespace AirBomber if (str == null || str.Length == 0) { - return false; + throw new Exception("Нет данных для загрузки"); } if (!str.StartsWith("PlaneStorage")) { //если нет такой записи, то это не те данные - return false; + throw new Exception("Неверный формат данных"); } _planeStorages.Clear(); @@ -170,7 +170,7 @@ namespace AirBomber { if ((collection + plane) == -1) { - return false; + throw new Exception("Ошибка добавления в коллекцию"); } } } diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index ca8d15b..731639c 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -53,7 +53,10 @@ namespace AirBomber // проверка, что после вставляемого элемента в массиве есть пустой элемент // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента // TODO вставка по позиции - if (position < 0 || position >= _maxCount) return -1; + if (position < 0 || position >= _maxCount) + { + throw new StorageOverflowException(_maxCount); + } _places.Insert(position, plane); return position; } @@ -68,7 +71,7 @@ namespace AirBomber // TODO удаление объекта из массива, присвоив элементу массива значение null if (!(position >= 0 && position < Count) || _places[position] == null) { - return false; + throw new PlaneNotFoundException(position); } _places[position] = null; return true; -- 2.25.1 From 8021e8ec78b6d3cbbd45e9e088b8cb503fa26363 Mon Sep 17 00:00:00 2001 From: malimova Date: Sun, 10 Dec 2023 21:38:25 +0400 Subject: [PATCH 4/9] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/FormPlaneCollection.cs | 4 ++++ AirBomber/AirBomber/PlaneNotFoundException.cs | 2 +- AirBomber/AirBomber/StorageOverflowException.cs | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs index 2c07129..947e102 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -7,6 +7,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Microsoft.VisualBasic.Logging; +using AirBomber.Exceptions; +using Serilog; +using Log = Serilog.Log; namespace AirBomber { diff --git a/AirBomber/AirBomber/PlaneNotFoundException.cs b/AirBomber/AirBomber/PlaneNotFoundException.cs index 211f8a9..5dc61a2 100644 --- a/AirBomber/AirBomber/PlaneNotFoundException.cs +++ b/AirBomber/AirBomber/PlaneNotFoundException.cs @@ -5,7 +5,7 @@ using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; -namespace AirBomber +namespace AirBomber.Exceptions { [Serializable] internal class PlaneNotFoundException: ApplicationException diff --git a/AirBomber/AirBomber/StorageOverflowException.cs b/AirBomber/AirBomber/StorageOverflowException.cs index 32e408b..8ae7b29 100644 --- a/AirBomber/AirBomber/StorageOverflowException.cs +++ b/AirBomber/AirBomber/StorageOverflowException.cs @@ -5,7 +5,7 @@ using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; -namespace AirBomber +namespace AirBomber.Exceptions { [Serializable] internal class StorageOverflowException: ApplicationException -- 2.25.1 From dfaaa3c746b8396fc2348956065a123b3ed98718 Mon Sep 17 00:00:00 2001 From: malimova Date: Sun, 10 Dec 2023 22:48:28 +0400 Subject: [PATCH 5/9] =?UTF-8?q?=D0=94=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=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 --- AirBomber/AirBomber/AirBomber.csproj | 6 +++ AirBomber/AirBomber/FormPlaneCollection.cs | 52 +++++++++++++++------- AirBomber/AirBomber/Program.cs | 14 +++++- AirBomber/AirBomber/SetGeneric.cs | 1 + 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/AirBomber/AirBomber/AirBomber.csproj b/AirBomber/AirBomber/AirBomber.csproj index 13ee123..e65c673 100644 --- a/AirBomber/AirBomber/AirBomber.csproj +++ b/AirBomber/AirBomber/AirBomber.csproj @@ -8,6 +8,12 @@ enable + + + + + + True diff --git a/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs index 947e102..fdbe73a 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -10,7 +10,9 @@ using System.Windows.Forms; using Microsoft.VisualBasic.Logging; using AirBomber.Exceptions; using Serilog; -using Log = Serilog.Log; +using _logger = Serilog.Log; +using System.Xml.Linq; +using System.Linq.Expressions; namespace AirBomber { @@ -82,11 +84,13 @@ namespace AirBomber if (obj + plane > -1) { MessageBox.Show("Объект добавлен"); + _logger.Information("Объект добавлен"); pictureBoxCollection.Image = obj.ShowPlanes(); } else { MessageBox.Show("Не удалось добавить объект"); + _logger.Warning("Не удалось добавить объект"); } } /// @@ -110,14 +114,23 @@ namespace AirBomber return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (obj - pos != null) + try { - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = obj.ShowPlanes(); + if (obj - pos != null) + { + MessageBox.Show("Объект удален"); + _logger.Information("Объект удален"); + pictureBoxCollection.Image = obj.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + _logger.Warning("Не удалось удалить объект"); + } } - else + catch (PlaneNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show(ex.Message); } } /// @@ -147,23 +160,26 @@ namespace AirBomber { if (string.IsNullOrEmpty(textBoxStorageName.Text)) { - MessageBox.Show("Не все данные заполнены", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.Warning("Не все данные заполнены"); return; } _storage.AddSet(textBoxStorageName.Text); ReloadObjects(); + _logger.Information($"Добавлен набор: {textBoxStorageName.Text}"); } private void buttonDelObject_Click(object sender, EventArgs e) { + string Name = listBoxStorages.SelectedItem.ToString() ?? string.Empty; if (listBoxStorages.SelectedIndex == -1) { return; } - if (MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + if (MessageBox.Show($"Удалить набор {Name}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); + _storage.DelSet(Name); ReloadObjects(); + _logger.Information($"Удален набор: {Name}"); } } /// @@ -175,13 +191,15 @@ namespace AirBomber { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.SaveData(saveFileDialog.FileName)) + try { MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.Information("Сохранение прошло успешно"); } - else + catch (Exception ex) { MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.Warning($"Не сохранилось: {ex.Message}"); } } } @@ -195,15 +213,17 @@ namespace AirBomber // TODO продумать логику DONE if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.LoadData(openFileDialog.FileName)) + try { + _storage.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.Information("Загрузка прошла успешно"); ReloadObjects(); } - else + catch(Exception ex) { - MessageBox.Show("Не загрузилось!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); - + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.Warning($"Не загрузилось: {ex.Message}"); } } } diff --git a/AirBomber/AirBomber/Program.cs b/AirBomber/AirBomber/Program.cs index e7cb4a7..d3007a7 100644 --- a/AirBomber/AirBomber/Program.cs +++ b/AirBomber/AirBomber/Program.cs @@ -1,3 +1,9 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using Serilog; + namespace AirBomber { internal static class Program @@ -11,7 +17,13 @@ namespace AirBomber // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormPlaneCollection()); + var services = new ServiceCollection(); + ConfigureServices(services); + using (ServiceProvider serviceProvider = services.BuildServiceProvider()) + { + Application.Run(serviceProvider.GetRequiredService()); + } + //Application.Run(new FormPlaneCollection()); } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index 731639c..636bc27 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using AirBomber.Exceptions; namespace AirBomber { -- 2.25.1 From e4e8b564046428853b9a72c2fa25f71c04fd4040 Mon Sep 17 00:00:00 2001 From: malimova Date: Mon, 11 Dec 2023 00:45:10 +0400 Subject: [PATCH 6/9] =?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; + } } } /// -- 2.25.1 From 74d3f54bafe83f446ffc410e9e10cdc8a90391c3 Mon Sep 17 00:00:00 2001 From: malimova Date: Mon, 11 Dec 2023 01:01:15 +0400 Subject: [PATCH 7/9] =?UTF-8?q?=D0=92=D1=81=D1=91=20=D1=81=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=D0=BE..?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/FormPlaneCollection.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs index 177fe0d..fca6a06 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -200,6 +200,7 @@ namespace AirBomber { try { + _storage.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); _logger.Information("Сохранение прошло успешно"); } -- 2.25.1 From c53f18d1321402a5710f1aab96d2df5e61db22c7 Mon Sep 17 00:00:00 2001 From: malimova Date: Mon, 11 Dec 2023 12:12:21 +0400 Subject: [PATCH 8/9] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE,=20=D1=81=D0=B4=D0=B0=D0=BD=D0=B0=20Lab7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/AirBomber.csproj | 5 ++-- AirBomber/AirBomber/AppSettings.json | 2 +- AirBomber/AirBomber/FormPlaneCollection.cs | 33 +++++++++++----------- AirBomber/AirBomber/Program.cs | 4 +-- AirBomber/AirBomber/SetGeneric.cs | 4 +-- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/AirBomber/AirBomber/AirBomber.csproj b/AirBomber/AirBomber/AirBomber.csproj index 22896a3..7900f0b 100644 --- a/AirBomber/AirBomber/AirBomber.csproj +++ b/AirBomber/AirBomber/AirBomber.csproj @@ -10,12 +10,13 @@ + - + - + diff --git a/AirBomber/AirBomber/AppSettings.json b/AirBomber/AirBomber/AppSettings.json index a604a73..acbfec1 100644 --- a/AirBomber/AirBomber/AppSettings.json +++ b/AirBomber/AirBomber/AppSettings.json @@ -14,7 +14,7 @@ ], "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], "Properties": { - "Application": "AirBomber" + "Application": "AirBombers" } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs index fca6a06..ddb5d58 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -9,10 +9,10 @@ using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.VisualBasic.Logging; using AirBomber.Exceptions; -using Serilog; -using _logger = Serilog.Log; +//using Serilog; using System.Xml.Linq; using System.Linq.Expressions; +using Microsoft.Extensions.Logging; namespace AirBomber { @@ -22,10 +22,12 @@ namespace AirBomber /// Набор объектов /// private readonly PlanesGenericStorage _storage; - public FormPlaneCollection() + private readonly ILogger _logger; + public FormPlaneCollection(ILogger logger) { InitializeComponent(); _storage = new PlanesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + _logger = logger; } /// /// Заполнение listBoxObjects @@ -85,13 +87,13 @@ namespace AirBomber { _ = obj + plane; MessageBox.Show("Объект добавлен"); - _logger.Information("Объект добавлен"); + _logger.LogInformation("Объект добавлен"); pictureBoxCollection.Image = obj.ShowPlanes(); } catch (Exception ex) { MessageBox.Show(ex.Message); - _logger.Warning($"Объект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}"); + _logger.LogWarning($"Объект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}"); } } /// @@ -116,18 +118,17 @@ namespace AirBomber } try { - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); if (obj - pos != null) { MessageBox.Show("Объект удален"); - _logger.Information("Объект удален"); + _logger.LogInformation("Объект удален"); pictureBoxCollection.Image = obj.ShowPlanes(); } else { MessageBox.Show("Не удалось удалить объект"); - _logger.Warning("Не удалось удалить объект"); + _logger.LogWarning("Не удалось удалить объект"); } } catch (PlaneNotFoundException ex) @@ -137,7 +138,7 @@ namespace AirBomber catch(Exception ex) { MessageBox.Show("Неверный ввод"); - _logger.Warning("Неверный ввод"); + _logger.LogWarning("Неверный ввод"); } } /// @@ -168,12 +169,12 @@ namespace AirBomber if (string.IsNullOrEmpty(textBoxStorageName.Text)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - _logger.Warning("Не все данные заполнены"); + _logger.LogWarning("Не все данные заполнены"); return; } _storage.AddSet(textBoxStorageName.Text); ReloadObjects(); - _logger.Information($"Добавлен набор: {textBoxStorageName.Text}"); + _logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}"); } private void buttonDelObject_Click(object sender, EventArgs e) { @@ -186,7 +187,7 @@ namespace AirBomber { _storage.DelSet(Name); ReloadObjects(); - _logger.Information($"Удален набор: {Name}"); + _logger.LogInformation($"Удален набор: {Name}"); } } /// @@ -202,12 +203,12 @@ namespace AirBomber { _storage.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - _logger.Information("Сохранение прошло успешно"); + _logger.LogInformation("Сохранение прошло успешно"); } catch (Exception ex) { MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); - _logger.Warning($"Не сохранилось: {ex.Message}"); + _logger.LogWarning($"Не сохранилось: {ex.Message}"); } } } @@ -225,13 +226,13 @@ namespace AirBomber { _storage.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - _logger.Information("Загрузка прошла успешно"); + _logger.LogInformation("Загрузка прошла успешно"); ReloadObjects(); } catch(Exception ex) { MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); - _logger.Warning($"Не загрузилось: {ex.Message}"); + _logger.LogWarning($"Не загрузилось: {ex.Message}"); } } } diff --git a/AirBomber/AirBomber/Program.cs b/AirBomber/AirBomber/Program.cs index dcdd25a..a46f828 100644 --- a/AirBomber/AirBomber/Program.cs +++ b/AirBomber/AirBomber/Program.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; +using AirBomber; namespace AirBomber { @@ -22,7 +23,6 @@ namespace AirBomber { Application.Run(serviceProvider.GetRequiredService()); } - //Application.Run(new FormPlaneCollection()); } private static void ConfigureServices(ServiceCollection services) { @@ -34,7 +34,7 @@ namespace AirBomber { pathNeed += path[i] + "\\"; } - var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(path: $"{pathNeed}AppSettings.json", optional: false, reloadOnChange: true).Build(); + 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); diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index f736d9e..0b1c8bd 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -49,7 +49,7 @@ namespace AirBomber public bool Insert(T plane, int position) { if (position < 0 || position >= _maxCount) - throw new StorageOverflowException("Impossible to insert"); + throw new StorageOverflowException("Невозможно добавить"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); @@ -64,7 +64,7 @@ namespace AirBomber public bool Remove(int position) { if (position >= Count || position < 0) - throw new PlaneNotFoundException("Invalid operation"); + throw new PlaneNotFoundException("Невалидная операция"); if (_places[position] == null) throw new PlaneNotFoundException(position); _places.RemoveAt(position); -- 2.25.1 From 76e036db5b23033b4bf357b8cd4afc055196bae9 Mon Sep 17 00:00:00 2001 From: malimova Date: Wed, 13 Dec 2023 16:10:46 +0400 Subject: [PATCH 9/9] for pull request --- AirBomber/AirBomber/FormPlaneCollection.cs | 1 - AirBomber/AirBomber/PlanesGenericStorage.cs | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs index ddb5d58..15fd2e7 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -9,7 +9,6 @@ using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.VisualBasic.Logging; using AirBomber.Exceptions; -//using Serilog; using System.Xml.Linq; using System.Linq.Expressions; using Microsoft.Extensions.Logging; diff --git a/AirBomber/AirBomber/PlanesGenericStorage.cs b/AirBomber/AirBomber/PlanesGenericStorage.cs index f519685..40f64d7 100644 --- a/AirBomber/AirBomber/PlanesGenericStorage.cs +++ b/AirBomber/AirBomber/PlanesGenericStorage.cs @@ -113,7 +113,7 @@ namespace AirBomber } if (data.Length == 0) { - throw new Exception("Невалидная операция, нет данных для сохранения"); + throw new ArgumentException("Невалидная операция, нет данных для сохранения"); } using StreamWriter sw = new(filename); @@ -129,7 +129,7 @@ namespace AirBomber { if (!File.Exists(filename)) { - throw new Exception("Файл не найден"); + throw new FileNotFoundException("Файл не найден"); } using (StreamReader sr = new(filename)) { @@ -137,12 +137,12 @@ namespace AirBomber if (str == null || str.Length == 0) { - throw new Exception("Нет данных для загрузки"); + throw new ArgumentException("Нет данных для загрузки"); } if (!str.StartsWith("PlaneStorage")) { //если нет такой записи, то это не те данные - throw new Exception("Неверный формат данных"); + throw new InvalidDataException("Неверный формат данных"); } _planeStorages.Clear(); -- 2.25.1