From 83035ffc778e66515135cd91dfc7fde1bf78d875 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Tue, 19 Dec 2023 22:35:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D1=83=20=D0=B4=D0=BE=D0=BB=D0=B6=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B1=D1=8B=D1=82=D1=8C=20=D0=B3=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=207?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/FormShipCollection.cs | 19 +++++++---- Battleship/Battleship/SetGeneric.cs | 36 +++++++++----------- Battleship/Battleship/ShipsGenericStorage.cs | 14 ++++++-- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/Battleship/Battleship/FormShipCollection.cs b/Battleship/Battleship/FormShipCollection.cs index 1f912fa..e9298e0 100644 --- a/Battleship/Battleship/FormShipCollection.cs +++ b/Battleship/Battleship/FormShipCollection.cs @@ -145,6 +145,7 @@ namespace Battleship ReloadObjects(); _logger.LogInformation($"Удален набор: {nameSet}"); } + _logger.LogWarning("Отмена удаления набора"); } /// /// Добавление объекта в набор @@ -177,27 +178,32 @@ namespace Battleship var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { + _logger.LogWarning("Добавление пустого объекта"); return; } + try + { if (obj + drawningShip) { MessageBox.Show("Объект добавлен"); pictureBoxCollection.Image = obj.ShowShips(); _logger.LogInformation($"Объект {obj.GetType()} добавлен"); - } - else - { - MessageBox.Show("Не удалось добавить объект"); - _logger.LogInformation($"Не удалось добавить объект"); } - + } + catch(StorageOverflowException ex) + { + MessageBox.Show(ex.Message); + MessageBox.Show("Не удалось добавить объект"); + _logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}"); + } } private void buttonRemoveShip_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { + _logger.LogWarning("Удаление объекта из несуществующего набора"); return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; @@ -207,6 +213,7 @@ namespace Battleship } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { + _logger.LogWarning("Отмена удаления объекта"); return; } diff --git a/Battleship/Battleship/SetGeneric.cs b/Battleship/Battleship/SetGeneric.cs index 6280321..270d0cc 100644 --- a/Battleship/Battleship/SetGeneric.cs +++ b/Battleship/Battleship/SetGeneric.cs @@ -17,33 +17,31 @@ namespace Battleship.Generics public SetGeneric(int count) { _maxCount = count; - _places = new List(count); + _places = new List(_maxCount); } - - public void Insert(T ship) - { - if(_places.Count == _maxCount) - { - throw new StorageOverflowException(_maxCount); - } - Insert(ship, 0); - } - - public void Insert(T ship, int position) - { - if(_places.Count == _maxCount) - throw new StorageOverflowException(_maxCount); - if (position < 0 || position >= _maxCount) - throw new ShipNotFoundException("Impossible to insert"); - _places.Insert(position, ship); + public bool Insert(T ship) + { + return Insert(ship, 0); } - public void Remove(int position) + + public bool Insert(T ship, int position) + { + if (position < 0 || position >= _maxCount) + throw new ShipNotFoundException(position); + + if (Count >= _maxCount) + throw new StorageOverflowException(_maxCount); + _places.Insert(0, ship); + return true; + } + public bool Remove(int position) { if (position < 0 || position > _maxCount || position >= Count) throw new ShipNotFoundException(position); _places.RemoveAt(position); + return true; } public T? this[int position] { diff --git a/Battleship/Battleship/ShipsGenericStorage.cs b/Battleship/Battleship/ShipsGenericStorage.cs index bfee486..a948da3 100644 --- a/Battleship/Battleship/ShipsGenericStorage.cs +++ b/Battleship/Battleship/ShipsGenericStorage.cs @@ -1,4 +1,5 @@ using Battleship.DrawningObjects; +using Battleship.Exceptions; using Battleship.MovementStrategy; using System; using System.Collections.Generic; @@ -139,9 +140,18 @@ namespace Battleship.Generics DrawningShip? plane = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); if (plane != null) { - if (!(collection + plane)) + try { - throw new InvalidOperationException("Ошибка добавления в коллекцию"); + _ = collection + plane; + } + catch (ShipNotFoundException e) + { + throw e; + } + + catch (StorageOverflowException e) + { + throw e; } } }