PIbd-21_Krasnikov_Lab1.base/WinFormsApp1/SetTraktorGeneric.cs

88 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormsApp1
{
internal class SetTraktorGeneric<T>
where T : class
{
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
private int TractorPlaces = 0;
public SetTraktorGeneric(int count)
{
_maxCount = count;
_places = new List<T>(); ;
}
public int Insert(T tractor)
{
return Insert(tractor, 0);
}
public int Insert(T tractor, int position)
{
if (position < 0 && position > _maxCount)
{
return -1;
}
else
{
_places.Insert(position, tractor);
return position;
}
}
public T Remove(int position)
{
if (position < 0 || position >= _maxCount)
{
return null;
}
var result = _places[position];
_places.RemoveAt(position);
return result;
}
public T this[int position]
{
get
{
if (position >= 0 && position < _maxCount && position < Count)
{
return _places[position];
}
else
{
return null;
}
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetBuses()
{
foreach (var traktor in _places)
{
if (traktor != null)
{
yield return traktor;
}
else
{
yield break;
}
}
}
}
}