PIbd-21_Valitov_D.F_Sailboa.../Sailboat/SetBoatsGeneric.cs

104 lines
2.4 KiB
C#
Raw Normal View History

2023-04-14 14:36:46 +04:00
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;
private readonly int _maxCount;
public int Count => _places.Count;
private int BusyPlaces = 0;
public SetBoatsGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
public int Insert(T boat)
{
if (Count + 1 <= _maxCount) return Insert(boat, 0);
else return -1;
}
public int Insert(T boat, int position)
{
if (position >= _maxCount && position < 0)
{
return -1;
}
_places.Insert(position, boat);
return position;
}
public T Remove(int position)
{
if (position < _maxCount && position >= 0)
{
if (_places.ElementAt(position) != null)
{
T result = _places.ElementAt(position);
_places.RemoveAt(position);
return result;
}
return null;
}
return null;
}
public T Get(int position)
{
if (position < 0 || position >= _places.Count) return null;
return _places[position];
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T this[int position]
{
get
{
if (position < _maxCount && position >= 0)
{
return _places.ElementAt(position);
}
return null;
}
set
{
if (position < _maxCount && position >= 0)
{
Insert(this[position], position);
}
}
}
public IEnumerable<T> GetBoats()
{
foreach (var boat in _places)
{
if (boat != null)
{
yield return boat;
}
else
{
yield break;
}
}
}
}
}