diff --git a/AirBomber/AirBomber.csproj b/AirBomber/AirBomber.csproj index a048c22..ed5fdde 100644 --- a/AirBomber/AirBomber.csproj +++ b/AirBomber/AirBomber.csproj @@ -9,8 +9,12 @@ + - + + + + @@ -28,10 +32,4 @@ - - - Always - - - \ No newline at end of file diff --git a/AirBomber/CollectionGenericObjects/AirPlaneSharingService.cs b/AirBomber/CollectionGenericObjects/AirPlaneSharingService.cs index 9a50907..485f8d1 100644 --- a/AirBomber/CollectionGenericObjects/AirPlaneSharingService.cs +++ b/AirBomber/CollectionGenericObjects/AirPlaneSharingService.cs @@ -60,8 +60,12 @@ public class AirPlaneSharingService : AbstractCompany int row = numRows - 1, col = numCols; for (int i = 0; i < _collection?.Count; i++, col--) { - _collection?.Get(i)?.SetPictureSize(_pictureWidth, _pictureHeight); - _collection?.Get(i)?.SetPosition(locCoord[row * numCols - col].Item1 + 5, locCoord[row * numCols - col].Item2 + 9); + try + { + _collection?.Get(i)?.SetPictureSize(_pictureWidth, _pictureHeight); + _collection?.Get(i)?.SetPosition(locCoord[row * numCols - col].Item1 + 5, locCoord[row * numCols - col].Item2 + 9); + } + catch (Exception) { } if (col == 1) { col = numCols + 1; diff --git a/AirBomber/CollectionGenericObjects/ListGenericObjects.cs b/AirBomber/CollectionGenericObjects/ListGenericObjects.cs index 0095cd6..4c9bc9f 100644 --- a/AirBomber/CollectionGenericObjects/ListGenericObjects.cs +++ b/AirBomber/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,5 @@ -using System; +using AirBomber.Exceptions; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -40,38 +41,31 @@ public class ListGenericObjects : ICollectionGenericObjects public T? Get(int position) { - if (position >= 0 && position < Count) - { - return _collection[position]; - } - else - { - return null; - } - + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); + return _collection[position]; } public int Insert(T obj) { - if (Count == _maxCount) { return -1; } + if (Count == _maxCount) throw new CollectionOverflowException(Count); _collection.Add(obj); return Count; } public int Insert(T obj, int position) { - if (position < 0 || position >= Count || Count == _maxCount) - { - return -1; - } + if (position < 0 || position >= Count) + throw new PositionOutOfCollectionException(position); + + if (Count == _maxCount) + throw new CollectionOverflowException(Count); _collection.Insert(position, obj); return position; - } public T Remove(int position) { - if (position >= Count || position < 0) return null; + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); T obj = _collection[position]; _collection.RemoveAt(position); return obj; diff --git a/AirBomber/CollectionGenericObjects/MassiveGenericObjects.cs b/AirBomber/CollectionGenericObjects/MassiveGenericObjects.cs index 83c40dc..57f097b 100644 --- a/AirBomber/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirBomber/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ -using System; +using AirBomber.Exceptions; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -49,12 +50,9 @@ where T : class public T? Get(int position) { - if (position >= 0 && position < Count) - { - return _collection[position]; - } - - return null; + if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); + if (_collection[position] == null) throw new ObjectNotFoundException(position); + return _collection[position]; } public int Insert(T obj) @@ -67,14 +65,14 @@ where T : class return i; } } - return -1; + throw new CollectionOverflowException(Count); } public int Insert(T obj, int position) { if (position < 0 || position >= Count) { - return -1; + throw new PositionOutOfCollectionException(position); } if (_collection[position] == null) { @@ -99,15 +97,16 @@ where T : class } } - return -1; + throw new CollectionOverflowException(Count); } public T Remove(int position) { if (position < 0 || position >= Count) { - return null; + throw new PositionOutOfCollectionException(position); } + if (_collection[position] == null) throw new ObjectNotFoundException(position); T obj = _collection[position]; _collection[position] = null; return obj; diff --git a/AirBomber/CollectionGenericObjects/StorageCollection.cs b/AirBomber/CollectionGenericObjects/StorageCollection.cs index 68e1456..4d7db31 100644 --- a/AirBomber/CollectionGenericObjects/StorageCollection.cs +++ b/AirBomber/CollectionGenericObjects/StorageCollection.cs @@ -1,5 +1,5 @@ using AirBomber.Drawnings; -using ProjectCatamaran.Exceptions; +using AirBomber.Exceptions; using System; using System.Collections.Generic; using System.Linq; @@ -70,7 +70,7 @@ public class StorageCollection return; } } - + /// /// Удаление коллекции /// @@ -188,11 +188,11 @@ public class StorageCollection string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { - if (elem?.CreateDrawningAirPlane() is T boat) + if (elem?.CreateDrawningAirPlane() is T airplane) { try { - if (collection.Insert(boat) == -1) + if (collection.Insert(airplane) == -1) throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]); } catch (CollectionOverflowException ex) diff --git a/AirBomber/Exceptions/CollectionOverflowException.cs b/AirBomber/Exceptions/CollectionOverflowException.cs index 7b3e6d5..78adba7 100644 --- a/AirBomber/Exceptions/CollectionOverflowException.cs +++ b/AirBomber/Exceptions/CollectionOverflowException.cs @@ -1,6 +1,6 @@ using System.Runtime.Serialization; -namespace ProjectCatamaran.Exceptions; +namespace AirBomber.Exceptions; /// /// Класс, описывающий ошибку переполнения коллекции diff --git a/AirBomber/Exceptions/ObjectNotFoundException.cs b/AirBomber/Exceptions/ObjectNotFoundException.cs index 96919fd..d3c9d98 100644 --- a/AirBomber/Exceptions/ObjectNotFoundException.cs +++ b/AirBomber/Exceptions/ObjectNotFoundException.cs @@ -1,6 +1,6 @@ using System.Runtime.Serialization; -namespace ProjectCatamaran.Exceptions; +namespace AirBomber.Exceptions; /// /// Класс, описывающий ошибку, что по указанной позиции нет элемента diff --git a/AirBomber/Exceptions/PositionOutOfCollectionException.cs b/AirBomber/Exceptions/PositionOutOfCollectionException.cs index 8c06211..39fa85a 100644 --- a/AirBomber/Exceptions/PositionOutOfCollectionException.cs +++ b/AirBomber/Exceptions/PositionOutOfCollectionException.cs @@ -1,6 +1,6 @@ using System.Runtime.Serialization; -namespace ProjectCatamaran.Exceptions; +namespace AirBomber.Exceptions; /// /// Класс, описывающий ошибку выхода за границы коллекции diff --git a/AirBomber/FormAirPlaneCollection.Designer.cs b/AirBomber/FormAirPlaneCollection.Designer.cs index 97259d8..39d38a1 100644 --- a/AirBomber/FormAirPlaneCollection.Designer.cs +++ b/AirBomber/FormAirPlaneCollection.Designer.cs @@ -275,7 +275,6 @@ файлToolStripMenuItem.Name = "файлToolStripMenuItem"; файлToolStripMenuItem.Size = new Size(59, 24); файлToolStripMenuItem.Text = "Файл"; - файлToolStripMenuItem.Click += saveToolStripMenuItem_Click; // // saveToolStripMenuItem // diff --git a/AirBomber/FormAirPlaneCollection.cs b/AirBomber/FormAirPlaneCollection.cs index 4a3e8c1..13603c2 100644 --- a/AirBomber/FormAirPlaneCollection.cs +++ b/AirBomber/FormAirPlaneCollection.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Extensions.Logging; +using AirBomber.Exceptions; namespace AirBomber { @@ -71,19 +72,25 @@ namespace AirBomber /// private void SetAirPlane(DrawningAirPlane airplane) { - if (_company == null || airplane == null) + try { - return; - } + if (_company == null || airplane == null) + { + return; + } - if (_company + airplane != -1) - { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _company.Show(); + if (_company + airplane != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + _logger.LogInformation("Добавлен объект: " + airplane.GetDataForSave()); + } } - else + catch (ObjectNotFoundException) { } + catch (CollectionOverflowException ex) { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show("В коллекции превышено допустимое количество элементов"); + _logger.LogError("Ошибка: {Message}", ex.Message); } } @@ -112,25 +119,31 @@ namespace AirBomber /// private void ButtonRemoveAirPlane_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) - { - return; - } - - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) - { - return; - } - int pos = Convert.ToInt32(maskedTextBox.Text); - if (_company - pos != null) + try { - MessageBox.Show("Объект удален"); - pictureBox.Image = _company.Show(); + if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) + { + throw new Exception("Входные данные отсутствуют"); + } + + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } + + + if (_company - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + _logger.LogInformation("Объект удален"); + } } - else + catch (Exception ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show("Не найден объект по позиции " + pos); + _logger.LogError("Ошибка: {Message}", ex.Message); } } @@ -146,26 +159,33 @@ namespace AirBomber return; } - DrawningAirPlane? airplane = null; + DrawningAirPlane? airPlane = null; int counter = 100; - while (airplane == null) + try { - airplane = _company.GetRandomObject(); - counter--; - if (counter <= 0) + while (airPlane == null) { - break; + airPlane = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } } - } - if (airplane == null) + if (airPlane == null) + { + return; + } + + FormAirBomber form = new FormAirBomber(); + form.SetAirPlane = airPlane; + form.ShowDialog(); + } + catch (Exception ex) { - return; + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } - - FormAirBomber form = new FormAirBomber(); - form.SetAirPlane = airplane; - form.ShowDialog(); } /// @@ -195,18 +215,27 @@ namespace AirBomber MessageBox.Show("Не все данный заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - CollectionType collectionType = CollectionType.None; - if (radioButtonMassive.Checked) - { - collectionType = CollectionType.Massive; - } - else if (radioButtonList.Checked) - { - collectionType = CollectionType.List; - } - _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); - RefreshListBoxItems(); + try + { + CollectionType collectionType = CollectionType.None; + if (radioButtonMassive.Checked) + { + collectionType = CollectionType.Massive; + } + else if (radioButtonList.Checked) + { + collectionType = CollectionType.List; + } + + _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); + RefreshListBoxItems(); + _logger.LogInformation("Добавлена коллекция:", textBoxCollectionName.Text); + } + catch (Exception ex) + { + _logger.LogError("Ошибка: {Message}", ex.Message); + } } private void RefreshListBoxItems() diff --git a/AirBomber/Program.cs b/AirBomber/Program.cs index c7f2938..5925694 100644 --- a/AirBomber/Program.cs +++ b/AirBomber/Program.cs @@ -1,6 +1,8 @@ +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using NLog.Extensions.Logging +using Serilog; + namespace AirBomber; @@ -14,20 +16,29 @@ internal static class Program { // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); ServiceCollection services = new(); ConfigureServices(services); - ApplicationConfiguration.Initialize(); - Application.Run(new FormAirPlaneCollection()); + using ServiceProvider serviceProvider = services.BuildServiceProvider(); + Application.Run(serviceProvider.GetRequiredService()); } private static void ConfigureServices(ServiceCollection services) { + string[] path = Directory.GetCurrentDirectory().Split('\\'); + string pathNeed = ""; + for (int i = 0; i < path.Length - 3; i++) + { + pathNeed += path[i] + "\\"; + } + services.AddSingleton() - .AddLogging(option => - { - option.SetMinimumLevel(LogLevel.Information); - option.AddNLog("nlog.config"); - }); + .AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddSerilog(new LoggerConfiguration().ReadFrom.Configuration(new ConfigurationBuilder(). + AddJsonFile($"{pathNeed}serilog.json").Build()).CreateLogger()); + }); } } diff --git a/AirBomber/nlog.config b/AirBomber/nlog.config deleted file mode 100644 index 63b7d65..0000000 --- a/AirBomber/nlog.config +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/AirBomber/serilog.json b/AirBomber/serilog.json new file mode 100644 index 0000000..b947cc8 --- /dev/null +++ b/AirBomber/serilog.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { "path": "log.log" } + } + ], + "Properties": { + "Applicatoin": "Sample" + } + } +} \ No newline at end of file