From c010d1ece9303ba51becf467cf4cf9402ad0c4e2 Mon Sep 17 00:00:00 2001 From: sofiaivv Date: Tue, 26 Dec 2023 19:51:23 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=20done=20lab?= =?UTF-8?q?6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MotorBoat/MotorBoat/BoatsGenericStorage.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/MotorBoat/MotorBoat/BoatsGenericStorage.cs b/MotorBoat/MotorBoat/BoatsGenericStorage.cs index 20128b7..e6a9a7a 100644 --- a/MotorBoat/MotorBoat/BoatsGenericStorage.cs +++ b/MotorBoat/MotorBoat/BoatsGenericStorage.cs @@ -129,7 +129,6 @@ namespace MotorBoat.Generics writer.Write($"BoatStorage{Environment.NewLine}{data}"); } return true; - */ } /// -- 2.25.1 From 28ab4fd98fd6febd22ac7ce99ac7be788a07eb53 Mon Sep 17 00:00:00 2001 From: sofiaivv Date: Wed, 27 Dec 2023 06:35:09 +0400 Subject: [PATCH 2/5] =?UTF-8?q?lab7=20done=20=D0=BD=D0=BE=20=D0=BD=D0=B5?= =?UTF-8?q?=20=D0=BE=D1=87=D0=B5=D0=BD=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MotorBoat/MotorBoat/AppSetting.json | 20 ++++ MotorBoat/MotorBoat/BoatGenericCollection.cs | 11 +- MotorBoat/MotorBoat/BoatNotFoundException.cs | 20 ++++ MotorBoat/MotorBoat/BoatsGenericStorage.cs | 45 +++++--- MotorBoat/MotorBoat/ExtentionDrawningBoat.cs | 1 + MotorBoat/MotorBoat/FormBoatCollection.cs | 107 +++++++++++++----- MotorBoat/MotorBoat/FormMotorBoat.cs | 2 +- MotorBoat/MotorBoat/MotorBoat.csproj | 12 ++ MotorBoat/MotorBoat/Program.cs | 30 ++++- MotorBoat/MotorBoat/SetGeneric.cs | 33 +++--- .../MotorBoat/StorageOverflowException.cs | 18 +++ 11 files changed, 227 insertions(+), 72 deletions(-) create mode 100644 MotorBoat/MotorBoat/AppSetting.json create mode 100644 MotorBoat/MotorBoat/BoatNotFoundException.cs create mode 100644 MotorBoat/MotorBoat/StorageOverflowException.cs diff --git a/MotorBoat/MotorBoat/AppSetting.json b/MotorBoat/MotorBoat/AppSetting.json new file mode 100644 index 0000000..1e7e6ed --- /dev/null +++ b/MotorBoat/MotorBoat/AppSetting.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": "MotorBoat" + } + } +} \ No newline at end of file diff --git a/MotorBoat/MotorBoat/BoatGenericCollection.cs b/MotorBoat/MotorBoat/BoatGenericCollection.cs index c849648..3ea5a6c 100644 --- a/MotorBoat/MotorBoat/BoatGenericCollection.cs +++ b/MotorBoat/MotorBoat/BoatGenericCollection.cs @@ -6,6 +6,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using MotorBoat.Exceptions; +using Microsoft.Extensions.Logging; namespace MotorBoat.Generics { @@ -65,11 +67,12 @@ namespace MotorBoat.Generics /// public static bool operator +(BoatsGenericCollection collect, T? obj) { - if (obj == null) + if (obj == null || collect == null) { - return false; + collect._collection.Insert(obj); + return true; } - return collect?._collection.Insert(obj) ?? false; + return false; } /// @@ -81,7 +84,7 @@ namespace MotorBoat.Generics public static T? operator -(BoatsGenericCollection? collect, int pos) { T? obj = collect._collection[pos]; - if (obj != null) + if (obj != null && collect != null) { collect._collection.Remove(pos); } diff --git a/MotorBoat/MotorBoat/BoatNotFoundException.cs b/MotorBoat/MotorBoat/BoatNotFoundException.cs new file mode 100644 index 0000000..5355455 --- /dev/null +++ b/MotorBoat/MotorBoat/BoatNotFoundException.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace MotorBoat.Exceptions +{ + internal class BoatNotFoundException : ApplicationException + { + public BoatNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public BoatNotFoundException() : base() { } + public BoatNotFoundException(string message) : base(message) { } + public BoatNotFoundException(string message, Exception exception) : base(message, exception) + { } + protected BoatNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + + } +} diff --git a/MotorBoat/MotorBoat/BoatsGenericStorage.cs b/MotorBoat/MotorBoat/BoatsGenericStorage.cs index e6a9a7a..beff92a 100644 --- a/MotorBoat/MotorBoat/BoatsGenericStorage.cs +++ b/MotorBoat/MotorBoat/BoatsGenericStorage.cs @@ -5,6 +5,9 @@ using System.Text; using System.Threading.Tasks; using MotorBoat.DrawningObjects; using MotorBoat.MovementStrategy; +using MotorBoat.Exceptions; +using Microsoft.Extensions.Logging; +using System.Numerics; namespace MotorBoat.Generics { @@ -66,9 +69,9 @@ namespace MotorBoat.Generics /// Название набора public void AddSet(string name) { - if (_boatStorages.ContainsKey(name)) + if (!_boatStorages.ContainsKey(name)) return; - _boatStorages[name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); + _boatStorages.Add(name, new BoatsGenericCollection(_pictureWidth, _pictureHeight)); } /// @@ -77,9 +80,8 @@ namespace MotorBoat.Generics /// Название набора public void DelSet(string name) { - if (!_boatStorages.ContainsKey(name)) - return; - _boatStorages.Remove(name); + if (_boatStorages.ContainsKey(name)) + _boatStorages.Remove(name); } /// @@ -103,7 +105,7 @@ namespace MotorBoat.Generics /// /// Путь и имя файла /// true - сохранение прошло успешно, false - ошибка при сохранении данных - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -121,14 +123,13 @@ namespace MotorBoat.Generics } if (data.Length == 0) { - return false; + throw new InvalidOperationException("Невалидная операция, нет данных для сохранения"); } using (StreamWriter writer = new StreamWriter(filename)) { writer.Write($"BoatStorage{Environment.NewLine}{data}"); } - return true; } /// @@ -136,11 +137,11 @@ namespace MotorBoat.Generics /// /// Путь и имя файла /// true - загрузка прошла успешно, false - ошибка при загрузке данных - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new FileNotFoundException($"Файл {filename} не найден"); } using (StreamReader fs = File.OpenText(filename)) @@ -148,11 +149,12 @@ namespace MotorBoat.Generics string str = fs.ReadLine(); if (str == null || str.Length == 0) { - return false; + throw new NullReferenceException("Нет данных для загрузки"); } if (!str.StartsWith("BoatStorage")) { - return false; + // если нет такой записи,то это не те данные + throw new FormatException("Неверный формат данных"); } _boatStorages.Clear(); @@ -162,7 +164,7 @@ namespace MotorBoat.Generics { if (strs == null) { - return false; + throw new NullReferenceException("Нет данных для загрузки"); } string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); @@ -178,14 +180,23 @@ namespace MotorBoat.Generics if (boat != null) { if (!(collection + boat)) - { - return false; - } + try + { + _ = collection + boat; + } + catch (BoatNotFoundException e) + { + throw e; + } + + catch (StorageOverflowException e) + { + throw e; + } } } _boatStorages.Add(record[0], collection); } - return true; } } } diff --git a/MotorBoat/MotorBoat/ExtentionDrawningBoat.cs b/MotorBoat/MotorBoat/ExtentionDrawningBoat.cs index ecd5f11..c5c9302 100644 --- a/MotorBoat/MotorBoat/ExtentionDrawningBoat.cs +++ b/MotorBoat/MotorBoat/ExtentionDrawningBoat.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using MotorBoat.Entities; +using System.Runtime.Serialization; namespace MotorBoat.DrawningObjects { diff --git a/MotorBoat/MotorBoat/FormBoatCollection.cs b/MotorBoat/MotorBoat/FormBoatCollection.cs index df19cfa..fc8169a 100644 --- a/MotorBoat/MotorBoat/FormBoatCollection.cs +++ b/MotorBoat/MotorBoat/FormBoatCollection.cs @@ -10,6 +10,9 @@ using System.Windows.Forms; using MotorBoat.DrawningObjects; using MotorBoat.Generics; using MotorBoat.MovementStrategy; +using Microsoft.Extensions.Logging; +using MotorBoat.Exceptions; +using System.Xml.Linq; namespace MotorBoat { @@ -23,13 +26,18 @@ namespace MotorBoat /// private readonly BoatsGenericStorage _storage; + /// Логер + /// + private readonly ILogger _logger; + /// /// Конструктор /// - public FormBoatCollection() + public FormBoatCollection(ILogger logger) { InitializeComponent(); _storage = new BoatsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + _logger = logger; } /// @@ -62,12 +70,12 @@ namespace MotorBoat { if (string.IsNullOrEmpty(textBoxStorageName.Text)) { - MessageBox.Show("Не все данные заполнены", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _storage.AddSet(textBoxStorageName.Text); ReloadObjects(); + _logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}"); } /// @@ -90,13 +98,18 @@ namespace MotorBoat { if (listBoxStorages.SelectedIndex == -1) { + _logger.LogWarning("Коллекция не выбрана"); return; } - if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + string nameSet = listBoxStorages.SelectedItem.ToString() ?? string.Empty; + if (MessageBox.Show($"Удалить объект {nameSet}?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); + _storage.DelSet(nameSet); ReloadObjects(); + _logger.LogInformation($"Удален набор: {nameSet}"); } + _logger.LogWarning("Отмена удаления набора"); } /// @@ -107,7 +120,10 @@ namespace MotorBoat private void ButtonAddBoat_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) + { + _logger.LogWarning("Коллекция не выбрана"); return; + } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; @@ -127,17 +143,25 @@ namespace MotorBoat var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) + { + _logger.LogWarning("Добавление пустого объекта"); return; - - - if (obj + drawningBoat) - { - MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = obj.ShowBoats(); } - else + + try { + if (obj + drawningBoat) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowBoats(); + _logger.LogInformation($"Объект {obj.GetType()} добавлен"); + } + } + catch (StorageOverflowException ex) + { + MessageBox.Show(ex.Message); MessageBox.Show("Не удалось добавить объект"); + _logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}"); } } @@ -149,27 +173,47 @@ namespace MotorBoat private void ButtonRemoveBoat_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) + { + _logger.LogWarning("Удаление объекта из несуществующего набора"); return; + } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) return; - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { + _logger.LogWarning("Отмена удаления объекта"); return; } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (obj - pos != null) + try + { + if (obj - pos != null) + { + MessageBox.Show("Объект удален"); + _logger.LogInformation($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); + pictureBoxCollection.Image = obj.ShowBoats(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + _logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorages.SelectedItem.ToString()}"); + } + } + catch (BoatNotFoundException ex) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = obj.ShowBoats(); + MessageBox.Show(ex.Message); + _logger.LogWarning($"Нет объекта{ex.Message} из набора {listBoxStorages.SelectedItem.ToString()}"); } - else + catch (FormatException) { - MessageBox.Show("Не удалось удалить объект"); + _logger.LogWarning($"Было введено не число"); + MessageBox.Show("Введите число"); } } @@ -200,15 +244,17 @@ namespace MotorBoat { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.SaveData(saveFileDialog.FileName)) + try { - MessageBox.Show("Сохранение прошло успешно", - "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _storage.SaveData(saveFileDialog.FileName); + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation($"Данные загружены в файл {saveFileDialog.FileName}"); } - else + + catch (Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogWarning($"Не удалось сохранить информацию в файл: {ex.Message}"); } } } @@ -222,17 +268,16 @@ namespace MotorBoat { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.LoadData(openFileDialog.FileName)) + try { + _storage.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - foreach (var collection in _storage.Keys) - { - listBoxStorages.Items.Add(collection); - } + _logger.LogInformation($"Данные загружены из файла {openFileDialog.FileName}"); } - else + catch (Exception ex) { - MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogWarning($"Не удалось загрузить информацию из файла: {ex.Message}"); } } } diff --git a/MotorBoat/MotorBoat/FormMotorBoat.cs b/MotorBoat/MotorBoat/FormMotorBoat.cs index 771428c..d2b7bc2 100644 --- a/MotorBoat/MotorBoat/FormMotorBoat.cs +++ b/MotorBoat/MotorBoat/FormMotorBoat.cs @@ -28,7 +28,7 @@ namespace MotorBoat /// /// /// - public DrawningBoat? SelectedBoat { get; private set; } + public DrawningBoat? SelectedBoat { get; set; } /// /// diff --git a/MotorBoat/MotorBoat/MotorBoat.csproj b/MotorBoat/MotorBoat/MotorBoat.csproj index 13ee123..46504f3 100644 --- a/MotorBoat/MotorBoat/MotorBoat.csproj +++ b/MotorBoat/MotorBoat/MotorBoat.csproj @@ -8,6 +8,18 @@ enable + + + + + + + + + + + + True diff --git a/MotorBoat/MotorBoat/Program.cs b/MotorBoat/MotorBoat/Program.cs index 8145421..9b04e68 100644 --- a/MotorBoat/MotorBoat/Program.cs +++ b/MotorBoat/MotorBoat/Program.cs @@ -1,3 +1,8 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; + namespace MotorBoat { internal static class Program @@ -11,7 +16,30 @@ namespace MotorBoat // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormBoatCollection()); + 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 => + { + 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}appSetting.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/MotorBoat/MotorBoat/SetGeneric.cs b/MotorBoat/MotorBoat/SetGeneric.cs index 43c788b..e3fda1c 100644 --- a/MotorBoat/MotorBoat/SetGeneric.cs +++ b/MotorBoat/MotorBoat/SetGeneric.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using MotorBoat.Exceptions; namespace MotorBoat.Generics { @@ -35,7 +36,7 @@ namespace MotorBoat.Generics public SetGeneric(int count) { _maxCount = count; - _places = new List(count); + _places = new List(_maxCount); } /// @@ -44,13 +45,8 @@ namespace MotorBoat.Generics /// Добавляемая лодка /// public bool Insert(T boat) - { - if (_places.Count == _maxCount) - { - return false; - } - Insert(boat, 0); - return true; + { + return Insert(boat, 0); } /// /// Добавление объекта в набор на конкретную позицию @@ -60,11 +56,12 @@ namespace MotorBoat.Generics /// public bool Insert(T boat, int position) { - if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) - { - return false; - } - _places.Insert(position, boat); + if (position < 0 || position >= _maxCount) + throw new BoatNotFoundException(position); + + if (Count >= _maxCount) + throw new StorageOverflowException(_maxCount); + _places.Insert(0, boat); return true; } @@ -75,7 +72,8 @@ namespace MotorBoat.Generics /// public bool Remove(int position) { - if (position < 0 || position >= Count) return false; + if (position < 0 || position > _maxCount || position >= Count) + throw new BoatNotFoundException(position); _places.RemoveAt(position); return true; } @@ -88,18 +86,17 @@ namespace MotorBoat.Generics { get { - if (position < 0 || position > _maxCount) + if (position < 0 || position >= Count) return null; return _places[position]; } set { - if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + if (position < 0 || position > _maxCount || Count == _maxCount) { return; } - _places.Insert(position, value); - return; + _places[position] = value; } } diff --git a/MotorBoat/MotorBoat/StorageOverflowException.cs b/MotorBoat/MotorBoat/StorageOverflowException.cs new file mode 100644 index 0000000..166d373 --- /dev/null +++ b/MotorBoat/MotorBoat/StorageOverflowException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + +namespace MotorBoat.Exceptions +{ + 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 12a157fa6d75ada01380b339658450c1c2188d15 Mon Sep 17 00:00:00 2001 From: sofiaivv Date: Thu, 28 Dec 2023 22:33:02 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D1=8D=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MotorBoat/MotorBoat/DrawningBoat.cs | 6 ++++++ MotorBoat/MotorBoat/MotorBoat.csproj | 1 + 2 files changed, 7 insertions(+) diff --git a/MotorBoat/MotorBoat/DrawningBoat.cs b/MotorBoat/MotorBoat/DrawningBoat.cs index 69c4dc7..a2d78e8 100644 --- a/MotorBoat/MotorBoat/DrawningBoat.cs +++ b/MotorBoat/MotorBoat/DrawningBoat.cs @@ -210,5 +210,11 @@ namespace MotorBoat.DrawningObjects break; } } + + public void ChangePictureBoxSize(int pictureBoxWidth, int pictureBoxHeight) + { + _pictureWidth = pictureBoxWidth; + _pictureHeight = pictureBoxHeight; + } } } \ No newline at end of file diff --git a/MotorBoat/MotorBoat/MotorBoat.csproj b/MotorBoat/MotorBoat/MotorBoat.csproj index 46504f3..e6e1f13 100644 --- a/MotorBoat/MotorBoat/MotorBoat.csproj +++ b/MotorBoat/MotorBoat/MotorBoat.csproj @@ -18,6 +18,7 @@ + -- 2.25.1 From 5077f1ff45c46ab5e4734f9eb9ec4a4f842f21c5 Mon Sep 17 00:00:00 2001 From: sofiaivv Date: Fri, 29 Dec 2023 00:09:58 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D1=8D=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MotorBoat/MotorBoat/BoatsGenericStorage.cs | 36 +++++++++++----------- MotorBoat/MotorBoat/FormBoatCollection.cs | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/MotorBoat/MotorBoat/BoatsGenericStorage.cs b/MotorBoat/MotorBoat/BoatsGenericStorage.cs index beff92a..d098453 100644 --- a/MotorBoat/MotorBoat/BoatsGenericStorage.cs +++ b/MotorBoat/MotorBoat/BoatsGenericStorage.cs @@ -69,9 +69,9 @@ namespace MotorBoat.Generics /// Название набора public void AddSet(string name) { - if (!_boatStorages.ContainsKey(name)) + if (_boatStorages.ContainsKey(name)) return; - _boatStorages.Add(name, new BoatsGenericCollection(_pictureWidth, _pictureHeight)); + _boatStorages[name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); } /// @@ -80,8 +80,9 @@ namespace MotorBoat.Generics /// Название набора public void DelSet(string name) { - if (_boatStorages.ContainsKey(name)) - _boatStorages.Remove(name); + if (!_boatStorages.ContainsKey(name)) + return; + _boatStorages.Remove(name); } /// @@ -133,7 +134,7 @@ namespace MotorBoat.Generics } /// - /// Загрузка информации по автомобилям в хранилище из файла + /// Загрузка информации по лодкам в хранилище из файла /// /// Путь и имя файла /// true - загрузка прошла успешно, false - ошибка при загрузке данных @@ -179,20 +180,19 @@ namespace MotorBoat.Generics DrawningBoat? boat = elem?.CreateDrawningBoat(_separatorForObject, _pictureWidth, _pictureHeight); if (boat != null) { - if (!(collection + boat)) - try - { - _ = collection + boat; - } - catch (BoatNotFoundException e) - { - throw e; - } + try + { + _ = collection + boat; + } + catch (BoatNotFoundException e) + { + throw e; + } - catch (StorageOverflowException e) - { - throw e; - } + catch (StorageOverflowException e) + { + throw e; + } } } _boatStorages.Add(record[0], collection); diff --git a/MotorBoat/MotorBoat/FormBoatCollection.cs b/MotorBoat/MotorBoat/FormBoatCollection.cs index fc8169a..bd717aa 100644 --- a/MotorBoat/MotorBoat/FormBoatCollection.cs +++ b/MotorBoat/MotorBoat/FormBoatCollection.cs @@ -206,7 +206,6 @@ namespace MotorBoat } catch (BoatNotFoundException ex) { - MessageBox.Show("Объект удален"); MessageBox.Show(ex.Message); _logger.LogWarning($"Нет объекта{ex.Message} из набора {listBoxStorages.SelectedItem.ToString()}"); } @@ -280,6 +279,7 @@ namespace MotorBoat _logger.LogWarning($"Не удалось загрузить информацию из файла: {ex.Message}"); } } + ReloadObjects(); } } } \ No newline at end of file -- 2.25.1 From c7bd11dd18d49c2b543a071c283959234f3d6486 Mon Sep 17 00:00:00 2001 From: sofiaivv Date: Fri, 29 Dec 2023 18:32:48 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=B2=D1=81=D1=91=20=D0=BF=D0=BE=20=D0=BA?= =?UTF-8?q?=D1=80=D0=B0=D1=81=D0=BE=D1=82=D0=B5,=20=D1=81=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=D0=BE=20=D0=B8=20=D1=81=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE,=20=D0=BB=D0=B0=D0=B17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MotorBoat/MotorBoat/BoatGenericCollection.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MotorBoat/MotorBoat/BoatGenericCollection.cs b/MotorBoat/MotorBoat/BoatGenericCollection.cs index 3ea5a6c..55440ec 100644 --- a/MotorBoat/MotorBoat/BoatGenericCollection.cs +++ b/MotorBoat/MotorBoat/BoatGenericCollection.cs @@ -67,7 +67,7 @@ namespace MotorBoat.Generics /// public static bool operator +(BoatsGenericCollection collect, T? obj) { - if (obj == null || collect == null) + if (obj != null && collect != null) { collect._collection.Insert(obj); return true; @@ -83,7 +83,7 @@ namespace MotorBoat.Generics /// public static T? operator -(BoatsGenericCollection? collect, int pos) { - T? obj = collect._collection[pos]; + T? obj = collect?._collection[pos]; if (obj != null && collect != null) { collect._collection.Remove(pos); -- 2.25.1