From cebb91a24c79e177db460686bac1fa7e30118982 Mon Sep 17 00:00:00 2001 From: Saanechkaa Date: Tue, 13 Dec 2022 15:32:05 +0400 Subject: [PATCH 1/2] Lab7 ready --- .../RoadTrain/FormMapWithSetRoadTrains.cs | 101 +++++++++++++----- RoadTrain/RoadTrain/MapsCollection.cs | 14 ++- RoadTrain/RoadTrain/Program.cs | 34 +++++- RoadTrain/RoadTrain/RoadTrain.csproj | 13 +++ .../RoadTrain/RoadTrainNotFoundException.cs | 18 ++++ RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs | 9 +- .../RoadTrain/StorageOverflowException.cs | 18 ++++ RoadTrain/RoadTrain/appsettings.json | 16 +++ RoadTrain/RoadTrain/nlog.config | 15 +++ 9 files changed, 198 insertions(+), 40 deletions(-) create mode 100644 RoadTrain/RoadTrain/RoadTrainNotFoundException.cs create mode 100644 RoadTrain/RoadTrain/StorageOverflowException.cs create mode 100644 RoadTrain/RoadTrain/appsettings.json create mode 100644 RoadTrain/RoadTrain/nlog.config diff --git a/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.cs b/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.cs index 396de92..c2b9ca7 100644 --- a/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.cs +++ b/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.cs @@ -1,9 +1,15 @@ using static System.Net.Mime.MediaTypeNames; +using Microsoft.Extensions.Logging; namespace RoadTrain { public partial class FormMapWithSetRoadTrains : Form { + /// + /// Логер + /// + private readonly ILogger _logger; + /// /// Словарь для выпадающего списка /// @@ -21,9 +27,10 @@ namespace RoadTrain /// /// Конструктор /// - public FormMapWithSetRoadTrains() + public FormMapWithSetRoadTrains(ILogger logger) { InitializeComponent(); + _logger = logger; _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); comboBoxSelectorMap.Items.Clear(); foreach (var elem in _mapsDict) @@ -63,6 +70,7 @@ namespace RoadTrain private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + _logger.LogInformation("Был осуществлен переход на карту под названием: {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty); } /// @@ -75,15 +83,18 @@ namespace RoadTrain if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogInformation("При добавлении карты {0}", comboBoxSelectorMap.SelectedIndex == -1 ? " Не все данные заполнены " : "Не была названа карта"); return; } if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) { MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning($"Нет такой карты {textBoxNewMapName.Text}"); return; } _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); ReloadMaps(); + _logger.LogInformation($"Добавлена карта {textBoxNewMapName.Text}"); } /// @@ -102,6 +113,7 @@ namespace RoadTrain "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + _logger.LogInformation("Удалена карта {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty); ReloadMaps(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); @@ -127,20 +139,31 @@ namespace RoadTrain /// private void AddRoadTrainOnForm(DrawningRoadTrain drawningRoadTrain) { - if (listBoxMaps.SelectedIndex == -1) + try { - return; + if (listBoxMaps.SelectedIndex == -1) + { + _logger.LogInformation("Попытка добавить объект, без создания карты"); + return; + } + DrawningObjectRoadTrain roadTrain = new(drawningRoadTrain); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + roadTrain >= 0) + { + MessageBox.Show("Объект добавлен"); + _logger.LogInformation($"Добавлен объект {drawningRoadTrain}"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + _logger.LogInformation("Не удалось добавить объект"); + } } - DrawningObjectRoadTrain roadTrain = new(drawningRoadTrain); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + roadTrain >= 0) + catch (StorageOverflowException ex) { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - } - else - { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning($"Ошибка переполнения хранилища: {ex.Message}"); } } @@ -164,13 +187,30 @@ namespace RoadTrain return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + try { - MessageBox.Show("Объект удален"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + { + MessageBox.Show("Объект удален"); + _logger.LogInformation($"Удален объект {_mapsCollection[listBoxMaps.SelectedItem?.ToString()]}"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + _logger.LogInformation("Не удалось удалить объект"); + } + } + catch (RoadTrainNotFoundException ex) + { + MessageBox.Show($"Ошибка удаления: {ex.Message}"); + _logger.LogWarning("Ошибка удаления: {0}", ex.Message); + } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); } - else { MessageBox.Show("Не удалось удалить объект"); } } /// @@ -243,13 +283,16 @@ namespace RoadTrain { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) - { - MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - } - else - { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + try + { + _mapsCollection.SaveData(saveFileDialog.FileName); + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation($"Сохранение прошло успешно. Файл находится: {saveFileDialog.FileName}"); + } + catch (Exception ex) + { + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning("Не удалось сохранить файл '{0}'. Текст ошибки: {1}", saveFileDialog.FileName, ex.Message); } } } @@ -263,13 +306,17 @@ namespace RoadTrain { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) + try { + _mapsCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation($"Файл '{openFileDialog.FileName}' успешно загружен"); + ReloadMaps(); } - else + catch (Exception ex) { - MessageBox.Show("Ошибка загрузки", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning($"Не получилось загрузить файл. Текст ошибки: {ex.Message}"); } } ReloadMaps(); diff --git a/RoadTrain/RoadTrain/MapsCollection.cs b/RoadTrain/RoadTrain/MapsCollection.cs index 5327baf..2b4631a 100644 --- a/RoadTrain/RoadTrain/MapsCollection.cs +++ b/RoadTrain/RoadTrain/MapsCollection.cs @@ -107,7 +107,7 @@ namespace RoadTrain /// /// Путь и имя файла /// - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -120,8 +120,7 @@ namespace RoadTrain { sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); } - } - return true; + } } /// @@ -129,11 +128,11 @@ namespace RoadTrain /// /// /// - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) - { - return false; + { + throw new Exception("Файл не найден"); } string bufferTextFromFile = ""; using (StreamReader sr = new(filename)) @@ -141,7 +140,7 @@ namespace RoadTrain string str = ""; if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection")) { - return false; + throw new FileFormatException("Формат данных в файле не правильный"); } _mapStorages.Clear(); while ((str = sr.ReadLine()) != null) @@ -161,7 +160,6 @@ namespace RoadTrain _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } } - return true; } } } diff --git a/RoadTrain/RoadTrain/Program.cs b/RoadTrain/RoadTrain/Program.cs index 6e01c71..1560326 100644 --- a/RoadTrain/RoadTrain/Program.cs +++ b/RoadTrain/RoadTrain/Program.cs @@ -1,4 +1,10 @@ -namespace RoadTrain +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; + + +namespace RoadTrain { internal static class Program { @@ -12,7 +18,31 @@ // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMapWithSetRoadTrains()); + var services = new ServiceCollection(); + ConfigureServices(services); + using (ServiceProvider serviceProvider = services.BuildServiceProvider()) + { + Application.Run(serviceProvider.GetRequiredService()); + } + } + + private static void ConfigureServices(ServiceCollection services) + { + services.AddSingleton() + .AddLogging(option => + { + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile(path: "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/RoadTrain/RoadTrain/RoadTrain.csproj b/RoadTrain/RoadTrain/RoadTrain.csproj index 59c8c45..92b34c7 100644 --- a/RoadTrain/RoadTrain/RoadTrain.csproj +++ b/RoadTrain/RoadTrain/RoadTrain.csproj @@ -16,6 +16,19 @@ + + + + + + + + + + + + + True diff --git a/RoadTrain/RoadTrain/RoadTrainNotFoundException.cs b/RoadTrain/RoadTrain/RoadTrainNotFoundException.cs new file mode 100644 index 0000000..71014c1 --- /dev/null +++ b/RoadTrain/RoadTrain/RoadTrainNotFoundException.cs @@ -0,0 +1,18 @@ +using System.Runtime.Serialization; + +namespace RoadTrain +{ + [Serializable] + internal class RoadTrainNotFoundException : ApplicationException + { + public RoadTrainNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + + public RoadTrainNotFoundException() : base() { } + + public RoadTrainNotFoundException(string message) : base(message) { } + + public RoadTrainNotFoundException(string message, Exception exception) : base(message, exception) { } + + protected RoadTrainNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs b/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs index 5abcb3f..051cec2 100644 --- a/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs +++ b/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs @@ -38,7 +38,7 @@ { // проверка на _maxCount if (_places.Count + 1 >= _maxCount) - return -1; + throw new StorageOverflowException(_maxCount); _places.Insert(0, roadTrain); return 0; } @@ -57,7 +57,7 @@ // проверка на _maxCount if (_places.Count + 1 >= _maxCount) - return -1; + throw new StorageOverflowException(_maxCount); // вставка по позиции _places[position] = roadTrain; @@ -75,7 +75,10 @@ if (position < 0 || position >= _maxCount) return null; T delObj = _places[position]; - // удаление объекта из массива + if (delObj == null) + { + throw new RoadTrainNotFoundException(position); + } _places.RemoveAt(position); return delObj; } diff --git a/RoadTrain/RoadTrain/StorageOverflowException.cs b/RoadTrain/RoadTrain/StorageOverflowException.cs new file mode 100644 index 0000000..82808f2 --- /dev/null +++ b/RoadTrain/RoadTrain/StorageOverflowException.cs @@ -0,0 +1,18 @@ +using System.Runtime.Serialization; + +namespace RoadTrain +{ + [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) { } + } +} diff --git a/RoadTrain/RoadTrain/appsettings.json b/RoadTrain/RoadTrain/appsettings.json new file mode 100644 index 0000000..6e66299 --- /dev/null +++ b/RoadTrain/RoadTrain/appsettings.json @@ -0,0 +1,16 @@ +{ + "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}" + } + } + ] + } +} \ No newline at end of file diff --git a/RoadTrain/RoadTrain/nlog.config b/RoadTrain/RoadTrain/nlog.config new file mode 100644 index 0000000..ff605b2 --- /dev/null +++ b/RoadTrain/RoadTrain/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file -- 2.25.1 From 5f39e71615a889ffe0d8affb4d35c22b8dba1d4f Mon Sep 17 00:00:00 2001 From: Saanechkaa Date: Tue, 13 Dec 2022 17:24:29 +0400 Subject: [PATCH 2/2] Lab7 fix --- RoadTrain/RoadTrain/MapWithSetRoadTrainsGeneric.cs | 2 +- RoadTrain/RoadTrain/RoadTrain.csproj | 1 + RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs | 8 ++++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/RoadTrain/RoadTrain/MapWithSetRoadTrainsGeneric.cs b/RoadTrain/RoadTrain/MapWithSetRoadTrainsGeneric.cs index d92ef94..78197e0 100644 --- a/RoadTrain/RoadTrain/MapWithSetRoadTrainsGeneric.cs +++ b/RoadTrain/RoadTrain/MapWithSetRoadTrainsGeneric.cs @@ -49,7 +49,7 @@ { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; - _setRoadTrains = new SetRoadTrainsGeneric(width * height - 2); + _setRoadTrains = new SetRoadTrainsGeneric(width * height - 1); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; diff --git a/RoadTrain/RoadTrain/RoadTrain.csproj b/RoadTrain/RoadTrain/RoadTrain.csproj index 92b34c7..e0d9d40 100644 --- a/RoadTrain/RoadTrain/RoadTrain.csproj +++ b/RoadTrain/RoadTrain/RoadTrain.csproj @@ -27,6 +27,7 @@ + diff --git a/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs b/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs index 051cec2..13d3946 100644 --- a/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs +++ b/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs @@ -72,9 +72,13 @@ public T Remove(int position) { // проверка позиции - if (position < 0 || position >= _maxCount) + if (position < 0 || position >= _maxCount - 1) return null; - T delObj = _places[position]; + if (position >= _places.Count) + { + throw new RoadTrainNotFoundException(position); + } + var delObj = _places[position]; if (delObj == null) { throw new RoadTrainNotFoundException(position); -- 2.25.1