80 lines
1.8 KiB
C#
80 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DoubleDeckerBus
|
|
{
|
|
internal class SetBusesGeneric<T>
|
|
where T : IDrawingObject
|
|
{
|
|
private readonly List<T> _places;
|
|
|
|
public int Count => _places.Count;
|
|
private int BusyPlaces = 0;
|
|
|
|
private readonly int _maxCount;
|
|
|
|
public SetBusesGeneric(int count)
|
|
{
|
|
_maxCount = count;
|
|
_places = new List<T>();
|
|
}
|
|
|
|
public int Insert(T bus)
|
|
{
|
|
return Insert(bus, 0);
|
|
}
|
|
|
|
public int Insert(T bus, int position)
|
|
{
|
|
if (position < 0 || position >= _maxCount)
|
|
{
|
|
throw new BusNotFoundException("Место указано неверно");
|
|
}
|
|
|
|
|
|
BusyPlaces++;
|
|
_places.Insert(position, bus);
|
|
return position;
|
|
}
|
|
|
|
public T Remove(int position)
|
|
{
|
|
|
|
if (position < 0 || position >= _maxCount) {
|
|
throw new BusNotFoundException(position);
|
|
}
|
|
T savedBus = _places[position];
|
|
_places.RemoveAt(position);
|
|
return savedBus;
|
|
}
|
|
|
|
public T this[int position] {
|
|
get {
|
|
if (position < 0 || position >= _maxCount) return default(T);
|
|
return _places[position];
|
|
}
|
|
set {
|
|
Insert(value, position);
|
|
}
|
|
|
|
}
|
|
|
|
public IEnumerable<T> GetBuses() {
|
|
foreach (var bus in _places)
|
|
{
|
|
if (bus != null)
|
|
{
|
|
yield return bus;
|
|
}
|
|
else
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|