From 2a71e707f23381623a7fbe6216d3f06178e9ba91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=95=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2?= Date: Sun, 6 Oct 2024 01:03:12 +0400 Subject: [PATCH] =?UTF-8?q?Revert=20"=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=5FLab=5F7"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sailboat/FormBoatCollection.Designer.cs | 1 + Sailboat/Sailboat/FormBoatCollection.cs | 55 ++++++++++--------- Sailboat/Sailboat/FormSailboat.Designer.cs | 1 + Sailboat/Sailboat/FormSailboat.cs | 8 ++- Sailboat/Sailboat/Program.cs | 15 +++-- Sailboat/Sailboat/SetGeneric.cs | 32 ++++++----- 6 files changed, 69 insertions(+), 43 deletions(-) diff --git a/Sailboat/Sailboat/FormBoatCollection.Designer.cs b/Sailboat/Sailboat/FormBoatCollection.Designer.cs index ba990d9..a30a1ac 100644 --- a/Sailboat/Sailboat/FormBoatCollection.Designer.cs +++ b/Sailboat/Sailboat/FormBoatCollection.Designer.cs @@ -260,5 +260,6 @@ namespace Sailboat private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private EventHandler buttonRemoveBoat_Click; } } \ No newline at end of file diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index 43b8bc4..454cfda 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -13,13 +13,14 @@ using Sailboat.Exceptions; using Sailboat.Generics; using Sailboat.MovementStrategy; + namespace Sailboat { public partial class FormBoatCollection : Form { private readonly BoatsGenericStorage _storage; private readonly ILogger _logger; - public FormBoatCollection(ILogger logger) + private FormBoatCollection(ILogger logger) { InitializeComponent(); _storage = new BoatsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); @@ -70,6 +71,7 @@ namespace Sailboat { if (listBoxStorages.SelectedIndex == -1) { + _logger.LogWarning("Добавление пустого объекта"); return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; @@ -95,7 +97,7 @@ namespace Sailboat } } - private void buttonRemoveBoat_Click(object sender, EventArgs e) + private void ButtonRemoveBoat_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { @@ -133,7 +135,6 @@ namespace Sailboat _logger.LogWarning($"{ex.Message} из набора {listBoxStorages.SelectedItem.ToString()}"); } } - private void buttonRefreshCollection_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) @@ -165,7 +166,8 @@ namespace Sailboat private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) { - pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBoats(); + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBoats(); } private void buttonDelObject_Click(object sender, EventArgs e) @@ -176,9 +178,10 @@ namespace Sailboat return; } string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty; - if (MessageBox.Show($"Удалить объект {name}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _storage.DelSet(name); + _storage.DelSet(listBoxStorages.SelectedItem.ToString() + ?? string.Empty); ReloadObjects(); _logger.LogInformation($"Удален набор: {name}"); } @@ -187,20 +190,17 @@ namespace Sailboat private void SaveToolStripMenuItem_Click(object sender, EventArgs e) { - if (saveFileDialog.ShowDialog() == DialogResult.OK) + try { - try - { - _storage.SaveData(saveFileDialog.FileName); - MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - _logger.LogInformation($"Данные загружены в файл {saveFileDialog.FileName}"); + _storage.SaveData(saveFileDialog.FileName); + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation($"Данные загружены в файл {saveFileDialog.FileName}"); - } - catch (Exception ex) - { - MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - _logger.LogWarning($"Не удалось сохранить информацию в файл: {ex.Message}"); - } + } + catch (Exception ex) + { + MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning($"Не удалось сохранить информацию в файл: {ex.Message}"); } } @@ -211,21 +211,26 @@ namespace Sailboat try { _storage.LoadData(openFileDialog.FileName); - ReloadObjects(); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); _logger.LogInformation($"Данные загружены из файла {openFileDialog.FileName}"); + foreach (var collection in _storage.Keys) + { + listBoxStorages.Items.Add(collection); + } + ReloadObjects(); + { + MessageBox.Show("Загрузка прошла успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } } catch (Exception ex) { - MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning($"Не удалось загрузить информацию из файла: {ex.Message}"); } } + ReloadObjects(); } - private void pictureBoxCollection_Click(object sender, EventArgs e) - { - - } } -} +} \ No newline at end of file diff --git a/Sailboat/Sailboat/FormSailboat.Designer.cs b/Sailboat/Sailboat/FormSailboat.Designer.cs index a7469f0..9f39a67 100644 --- a/Sailboat/Sailboat/FormSailboat.Designer.cs +++ b/Sailboat/Sailboat/FormSailboat.Designer.cs @@ -210,6 +210,7 @@ Name = "FormSailboat"; StartPosition = FormStartPosition.CenterScreen; Text = "Парусник"; + Load += FormSailboat_Load; ((System.ComponentModel.ISupportInitialize)pictureBoxSailboat).EndInit(); ResumeLayout(false); PerformLayout(); diff --git a/Sailboat/Sailboat/FormSailboat.cs b/Sailboat/Sailboat/FormSailboat.cs index bd6e458..bf21d65 100644 --- a/Sailboat/Sailboat/FormSailboat.cs +++ b/Sailboat/Sailboat/FormSailboat.cs @@ -129,6 +129,12 @@ namespace Sailboat { SelectedBoat = _drawingBoat; DialogResult = DialogResult.OK; + + } + + private static void FormSailboat_Load(object sender, EventArgs e) + { + } } -} \ No newline at end of file +} \ No newline at end of file diff --git a/Sailboat/Sailboat/Program.cs b/Sailboat/Sailboat/Program.cs index 4e9c193..b83fd3c 100644 --- a/Sailboat/Sailboat/Program.cs +++ b/Sailboat/Sailboat/Program.cs @@ -1,6 +1,8 @@ -using Microsoft.Extensions.Configuration; +using System; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; using Serilog; namespace Sailboat @@ -13,13 +15,16 @@ namespace Sailboat [STAThread] static void Main() { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); var services = new ServiceCollection(); ConfigureServices(services); - using (ServiceProvider serviceProvider = services.BuildServiceProvider()) - { + ServiceProvider serviceProvider = services.BuildServiceProvider(); + { Application.Run(serviceProvider.GetRequiredService()); - } + } + } private static void ConfigureServices(ServiceCollection services) @@ -37,7 +42,9 @@ namespace Sailboat option.SetMinimumLevel(LogLevel.Information); option.AddSerilog(logger); + option.AddNLog("nlog.config"); }); + } } } \ No newline at end of file diff --git a/Sailboat/Sailboat/SetGeneric.cs b/Sailboat/Sailboat/SetGeneric.cs index ab71800..b3d3f93 100644 --- a/Sailboat/Sailboat/SetGeneric.cs +++ b/Sailboat/Sailboat/SetGeneric.cs @@ -1,10 +1,10 @@ using System; +using Sailboat.Exceptions; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; - -using Sailboat.Exceptions; +using System.Windows.Forms; namespace Sailboat.Generics { @@ -13,7 +13,7 @@ namespace Sailboat.Generics /// /// Список объектов, которые храним /// - private readonly List _places; + private readonly List _places; /// /// Количество объектов в массиве /// @@ -38,7 +38,12 @@ namespace Sailboat.Generics /// public bool Insert(T boat) { - return Insert(boat, 0); + if (_places.Count == _maxCount) + { + throw new StorageOverflowException(_maxCount); + } + Insert(boat, 0); + return true; } /// /// Добавление объекта в набор на конкретную позицию @@ -48,12 +53,13 @@ namespace Sailboat.Generics /// public bool Insert(T boat, int position) { - if (position < 0 || position >= _maxCount) - throw new BoatNotFoundException(position); - - if (Count >= _maxCount) + if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); - _places.Insert(0, boat); + if (!(position >= 0 && position <= Count)) + { + return false; + } + _places.Insert(position, boat); return true; } /// @@ -63,7 +69,7 @@ namespace Sailboat.Generics /// public bool Remove(int position) { - if (position < 0 || position > _maxCount || position >= Count) + if (position < 0 || position >= Count) { throw new BoatNotFoundException(position); } @@ -81,7 +87,7 @@ namespace Sailboat.Generics { if (position < 0 || position >= Count) { - return null; + return null; } return _places[position]; } @@ -89,7 +95,7 @@ namespace Sailboat.Generics { if (!(position >= 0 && position < Count && _places.Count < _maxCount)) { - return; + return; } _places.Insert(position, value); return; @@ -111,4 +117,4 @@ namespace Sailboat.Generics } } } -} +} \ No newline at end of file