diff --git a/ProjectStudying/ProjectStudying/CollectionGenericObjects/ListGenericObjects.cs b/ProjectStudying/ProjectStudying/CollectionGenericObjects/ListGenericObjects.cs index b775639..de8ab37 100644 --- a/ProjectStudying/ProjectStudying/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectStudying/ProjectStudying/CollectionGenericObjects/ListGenericObjects.cs @@ -51,17 +51,47 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T? obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { - if (Count == _maxCount) throw new CollectionOverflowException(Count); + // TODO выброс ошибки, если переполнение + // TODO выброс ошибки, если такой объект есть в коллекции + if (Count == _maxCount) + { + throw new CollectionOverflowException(Count); + } + + for (int i = 0; i < Count; i++) + { + if (comparer.Equals((_collection[i] as DrawningLocomotive), (obj as DrawningLocomotive))) + { + throw new ObjectAlreadyInCollectionException(i); + } + } + _collection.Add(obj); - return Count - 1; + return _collection.Count; } - public int Insert(T? obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { - if (Count == _maxCount) throw new CollectionOverflowException(Count); - if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position); + if (position < 0 || position > Count) + { + throw new PositionOutOfCollectionException(position); + } + + if (Count == _maxCount) + { + throw new CollectionOverflowException(Count); + } + + for (int i = 0; i < Count; i++) + { + if (comparer.Equals((_collection[i] as DrawningLocomotive), (obj as DrawningLocomotive))) + { + throw new ObjectAlreadyInCollectionException(i); + } + } + _collection.Insert(position, obj); return position; } @@ -82,16 +112,6 @@ public class ListGenericObjects : ICollectionGenericObjects } } - public int Insert(T obj, IEqualityComparer? comparer = null) - { - throw new NotImplementedException(); - } - - public int Insert(T obj, int position, IEqualityComparer? comparer = null) - { - throw new NotImplementedException(); - } - public void CollectionSort(IComparer comparer) { _collection.Sort(comparer);