From f936efef8f883dd9842a9aea5ff9fcd18c72f19d Mon Sep 17 00:00:00 2001 From: Timur_Sharafutdinov Date: Wed, 15 May 2024 20:51:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20ListGenericObjects?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ListGenericObjects.cs | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) 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);