using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ProjectElectricLocomotive.Exceptions; namespace ProjectElectricLocomotive.CollectionGenericObjects { public class ListGenericObjects : ICollectionGenericObjects where T: class { /// /// Список объектов, которые храним /// private readonly List _collection; /// /// Максимально допустимое число объектов в списке /// private int _maxCount; public int MaxCount { get { return Count; } set { if (value > 0) { _maxCount = value; } } } public int Count => _collection.Count; public CollectionType GetCollectionType => CollectionType.List; /// /// Конструктор /// public ListGenericObjects() { _collection = new(); } public T? Get(int position) { // проверка позиции // выброс ошибки, если выход за границы массива if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); return _collection[position]; } public int Insert(T obj, IEqualityComparer? comparer = null) { // выброс ошибки если переполнение //выброс ошибки если такой объект уже есть в коллекции if(comparer != null) { for(int i = 0; i < Count; i++) { if (comparer.Equals(_collection[i], obj)) { throw new CollectionDuplicateException(obj); } } } if (Count == _maxCount) throw new CollectionOverflowException(Count); _collection.Add(obj); return Count; } public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // проверка, что не превышено максимальное количество элементов // проверка позиции // вставка по позиции // выброс ошибки, если переполнение // выброс ошибки если выход за границу //выброс ошибки если такой объект уже есть в коллекции if (comparer != null) { for (int i = 0; i < Count; i++) { if (comparer.Equals(_collection[i], obj)) { throw new CollectionDuplicateException(obj); } } } if (Count == _maxCount) throw new CollectionOverflowException(Count); if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); _collection.Insert(position, obj); return position; } public T Remove(int position) { // проверка позиции // удаление объекта из списка //выброс ошибки, если выход за границы массива if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); T obj = _collection[position]; _collection.RemoveAt(position); return obj; } public IEnumerable GetItems() { for (int i = 0; i < Count; ++i) { yield return _collection[i]; } } public void CollectionSort(IComparer comparer) { _collection.Sort(comparer); // throw new NotImplementedException(); } } }