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

81 lines
1.8 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
{
2023-04-14 22:41:19 +04:00
class SetBoatsGeneric<T> where T : class
2023-04-14 14:36:46 +04:00
{
2023-04-14 22:41:19 +04:00
private readonly List<T> _places;
2023-04-14 14:36:46 +04:00
2023-04-14 22:41:19 +04:00
public int Count => _places.Count;
private readonly int _maxCount;
2023-04-14 14:36:46 +04:00
private int BusyPlaces = 0;
public SetBoatsGeneric(int count)
{
2023-04-14 22:41:19 +04:00
_maxCount = count;
_places = new List<T>();
2023-04-14 14:36:46 +04:00
}
public int Insert(T boat)
{
2023-04-14 21:28:29 +04:00
return Insert(boat, 0);
2023-04-14 14:36:46 +04:00
}
public int Insert(T boat, int position)
{
2023-04-14 22:41:19 +04:00
if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount)
2023-04-14 14:36:46 +04:00
{
return -1;
}
2023-04-14 21:28:29 +04:00
BusyPlaces++;
2023-04-14 22:41:19 +04:00
_places.Insert(position, boat);
2023-04-14 21:28:29 +04:00
return position;
2023-04-14 14:36:46 +04:00
}
2023-04-14 21:28:29 +04:00
public T Remove(int position)
2023-04-14 14:36:46 +04:00
{
2023-04-14 22:41:19 +04:00
if (position < 0 || position >= _maxCount)
2023-04-14 14:36:46 +04:00
{
return null;
}
2023-04-14 21:28:29 +04:00
BusyPlaces--;
T boat = _places[position];
2023-04-14 22:41:19 +04:00
_places.RemoveAt(position);
2023-04-14 21:28:29 +04:00
return boat;
2023-04-14 14:36:46 +04:00
}
2023-04-14 22:41:19 +04:00
public T this[int position]
2023-04-14 14:36:46 +04:00
{
2023-04-14 22:41:19 +04:00
get
2023-04-14 14:36:46 +04:00
{
2023-04-14 22:41:19 +04:00
if (position < 0 || position >= _maxCount) return null;
return _places[position];
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetBoats()
{
foreach (var boat in _places)
{
if (boat != null)
{
yield return boat;
}
else
{
yield break;
}
2023-04-14 14:36:46 +04:00
}
}
}
2023-04-14 22:41:19 +04:00
}