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

93 lines
2.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormsApp1
{
internal class SetTraktorGeneric<T>
2022-12-23 01:49:51 +04:00
where T : class, IEquatable<T>
{
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)
{
2022-12-23 00:48:39 +04:00
if (position > _maxCount && position < 0)
{
return -1;
}
2022-12-23 00:48:39 +04:00
if (_places.Contains(tractor))
{
2022-12-23 00:48:39 +04:00
throw new ArgumentException($"Объект {tractor} уже есть в наборе");
}
2022-12-23 00:48:39 +04:00
if (Count == _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
_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);
}
}
2022-11-29 01:53:02 +04:00
public IEnumerable<T> GetTraktor()
{
foreach (var traktor in _places)
{
if (traktor != null)
{
yield return traktor;
}
else
{
yield break;
}
}
}
}
}