using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sailboat { class SetBoatsGeneric where T : class { private readonly List _places; public int Count => _places.Count; private readonly int _maxCount; private int BusyPlaces = 0; public SetBoatsGeneric(int count) { _maxCount = count; _places = new List(); } public int Insert(T boat) { return Insert(boat, 0); } public int Insert(T boat, int position) { if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) { return -1; } BusyPlaces++; _places.Insert(position, boat); return position; } public T Remove(int position) { if (position < 0 || position >= _maxCount) { return null; } BusyPlaces--; T boat = _places[position]; _places.RemoveAt(position); return boat; } public T this[int position] { get { if (position < 0 || position >= _maxCount) return null; return _places[position]; } set { Insert(value, position); } } public IEnumerable GetBoats() { foreach (var boat in _places) { if (boat != null) { yield return boat; } else { yield break; } } } } }