ну должна быть готова 7

This commit is contained in:
Казначеева Елизавета 2023-12-19 22:35:22 +04:00
parent 9f41ebdfdf
commit 83035ffc77
3 changed files with 42 additions and 27 deletions

View File

@ -145,6 +145,7 @@ namespace Battleship
ReloadObjects();
_logger.LogInformation($"Удален набор: {nameSet}");
}
_logger.LogWarning("Отмена удаления набора");
}
/// <summary>
/// Добавление объекта в набор
@ -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;
}

View File

@ -17,33 +17,31 @@ namespace Battleship.Generics
public SetGeneric(int count)
{
_maxCount = count;
_places = new List<T?>(count);
_places = new List<T?>(_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]
{

View File

@ -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;
}
}
}