85 lines
2.0 KiB
C#
85 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ContainerShip
|
|
{
|
|
internal class SetShipGeneric<T>
|
|
where T : class
|
|
{
|
|
private readonly List<T> _places;
|
|
|
|
public int Count => _places.Count;
|
|
|
|
private readonly int _maxCount;
|
|
|
|
public SetShipGeneric(int count)
|
|
{
|
|
_maxCount = count;
|
|
_places = new List<T>();
|
|
}
|
|
|
|
public int Insert(T ship)
|
|
{
|
|
return Insert(ship, 0);
|
|
}
|
|
|
|
public int Insert(T ship, int position)
|
|
{
|
|
if (position < 0 || position > Count || Count == _maxCount)
|
|
{
|
|
throw new StorageOverflowException(_maxCount);
|
|
}
|
|
_places.Insert(position, ship);
|
|
return position;
|
|
}
|
|
|
|
public T Remove(int position)
|
|
{
|
|
if (position >= Count || position < 0)
|
|
{
|
|
throw new ShipNotFoundException(position);
|
|
}
|
|
T removedObject = _places[position];
|
|
_places.RemoveAt(position);
|
|
return removedObject;
|
|
}
|
|
|
|
public T this[int position]
|
|
{
|
|
get
|
|
{
|
|
if (position < 0 || position >= Count)
|
|
{
|
|
return null;
|
|
}
|
|
return _places[position];
|
|
}
|
|
set
|
|
{
|
|
if (position < 0 || position >= Count)
|
|
{
|
|
return;
|
|
}
|
|
Insert(value, position);
|
|
}
|
|
}
|
|
public IEnumerable<T> GetShip()
|
|
{
|
|
foreach (var ship in _places)
|
|
{
|
|
if (ship != null)
|
|
{
|
|
yield return ship;
|
|
}
|
|
else
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|