89 lines
2.0 KiB
C#
89 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sailboat
|
|
{
|
|
class SetBoatsGeneric<T> where T : class
|
|
{
|
|
private readonly List<T> _places;
|
|
|
|
public int Count => _places.Count;
|
|
|
|
private readonly int _maxCount;
|
|
private int BusyPlaces = 0;
|
|
|
|
public SetBoatsGeneric(int count)
|
|
{
|
|
_maxCount = count;
|
|
_places = new List<T>();
|
|
}
|
|
|
|
public int Insert(T boat)
|
|
{
|
|
return Insert(boat, 0);
|
|
}
|
|
|
|
public int Insert(T boat, int position)
|
|
{
|
|
if (Count == _maxCount)
|
|
{
|
|
throw new StorageOverflowException(_maxCount);
|
|
}
|
|
if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount)
|
|
{
|
|
return -1;
|
|
}
|
|
BusyPlaces++;
|
|
_places.Insert(position, boat);
|
|
return position;
|
|
}
|
|
|
|
public T Remove(int position)
|
|
{
|
|
if (position >= Count || position < 0)
|
|
{
|
|
throw new BoatNotFoundException(position);
|
|
}
|
|
if (position < 0 || position >= _maxCount)
|
|
{
|
|
return null;
|
|
}
|
|
BusyPlaces--;
|
|
T boat = _places[position];
|
|
_places.RemoveAt(position);
|
|
return boat;
|
|
}
|
|
|
|
public T this[int position]
|
|
{
|
|
get
|
|
{
|
|
if (position < 0 || position >= _maxCount) return null;
|
|
return _places[position];
|
|
}
|
|
set
|
|
{
|
|
Insert(value, position);
|
|
}
|
|
|
|
}
|
|
|
|
public IEnumerable<T> GetBoats()
|
|
{
|
|
foreach (var boat in _places)
|
|
{
|
|
if (boat != null)
|
|
{
|
|
yield return boat;
|
|
}
|
|
else
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |