86 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Trolleybus
{
// Параметризованный набор объектов
internal class SetTrolleybusGeneric<T>
where T : class
{
// Массив объектов, которые храним
private readonly List<T> _places;
// Количество объектов в массиве
public int Count => _places.Count;
private readonly int _maxCount;
// Конструктор
public SetTrolleybusGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
private bool CorrectPos(int pos)
{
return 0 <= pos && pos < _maxCount;
}
// Добавление объекта в набор
public int Insert(T trolleybus)
{
// вставка в начало набора
return Insert(trolleybus, 0);
}
// Добавление объекта в набор на конкретную позицию
public int Insert(T trolleybus, int position)
{
// проверка позиции
if (!CorrectPos(position))
{
return -1;
}
// вставка по позиции
_places.Insert(position, trolleybus);
return position;
}
// Удаление объекта из набора с конкретной позиции
public T Remove(int position)
{
// проверка позиции
if (!CorrectPos(position))
return null;
// удаление объекта из массива, присовив элементу массива значение null
T temp = _places[position];
_places.RemoveAt(position);
return temp;
}
// Получение объекта из набора по позиции
public T this[int position]
{
get
{
return CorrectPos(position) && position < Count ? _places[position] : null;
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetTractors()
{
foreach (var tractor in _places)
{
if (tractor != null)
{
yield return tractor;
}
else
{
yield break;
}
}
}
}
}