99 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace Battleship
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetBattleshipGeneric<T>
where T : class
{
private readonly List<T> _places;
public int Count => _places.Count;
private int BattleshipPlaces = 0;
private readonly int _maxCount;
public SetBattleshipGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
public int Insert(T battleship)
{
if (Count == _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
if (Count + 1 <= _maxCount) return Insert(battleship, 0);
else return -1;
}
public int Insert(T battleship, int position)
{
if (position > _maxCount && position < 0)
{
return -1;
}
if (_places.Contains(battleship))
{
throw new ArgumentException($"Объект {battleship} уже есть в наборе");
}
_places.Insert(position, battleship);
return position;
}
public T Remove(int position)
{
if (position >= Count || position < 0)
{
throw new BattleshipNotFoundException(position);
}
T result = _places[position];
_places.RemoveAt(position);
return result;
}
public T this[int position]
{
get
{
if (position < 0 || position >= _maxCount) return null;
return _places[position];
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetBattleship()
{
foreach (var battleship in _places)
{
if (battleship != null)
{
yield return battleship;
}
else
{
yield break;
}
}
}
}
}