diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs index d0605f1..74d1a9b 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -8,9 +8,9 @@ public interface ICollectionGenericObjects int MaxCount { get; set; } - int Insert(T obj, IEqualityComparer? comparer = null); + int Insert(T obj, IEqualityComparer? comparer = null); - int Insert(T obj, int position, IEqualityComparer? comparer = null); + int Insert(T obj, int position, IEqualityComparer? comparer = null); T? Remove(int position); diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs index 620dd4f..9f3ccf5 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs @@ -32,50 +32,45 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj, IEqualityComparer? comparer = null) + public int Insert(T obj, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если переполнение - // TODO выброс ошибки, если такой объект есть в коллекции + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectAlreadyInCollectionException(); + } + } if (Count == _maxCount) { throw new CollectionOverflowException(Count); } - for (int i = 0; i < Count; i++) - { - if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) - { - throw new ObjectAlreadyInCollectionException(i); - } - } - _collection.Add(obj); return _collection.Count; } - public int Insert(T obj, int position, IEqualityComparer? comparer = null) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если выход за границы списка // TODO выброс ошибки, если переполнение - // TODO выброс ошибки, если такой объект есть в коллекции + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectAlreadyInCollectionException(); + } + } 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 DrawningShip), (obj as DrawningShip))) - { - throw new ObjectAlreadyInCollectionException(i); - } - } - _collection.Insert(position, obj); return position; } diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs index bebadca..1254fd9 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs @@ -53,18 +53,21 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj, IEqualityComparer? comparer = null) + public int Insert(T obj, IEqualityComparer? comparer = null) { - // TODO выброс ошибки, если такой объект есть в коллекции // TODO выброс ошибки, если переполнение - for (int i = 0; i < Count; i++) + if (comparer != null) { - if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) + foreach (T? item in _collection) { - throw new ObjectAlreadyInCollectionException(i); + if ((comparer as IEqualityComparer).Equals(obj as DrawningShip, item as DrawningShip)) + { + throw new ObjectAlreadyInCollectionException(); + } } } + for (int i = 0; i < Count; i++) { if (_collection[i] == null) @@ -77,24 +80,26 @@ public class MassiveGenericObjects : ICollectionGenericObjects throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position, IEqualityComparer? comparer = null) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если выход за границы массива - // TODO выброс ошибки, если такой объект есть в коллекции // TODO выброс ошибки, если переполнение + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningShip, item as DrawningShip)) + { + throw new ObjectAlreadyInCollectionException(); + } + } + } + if (position < 0 || position >= Count) { throw new PositionOutOfCollectionException(position); } - for (int i = 0; i < Count; i++) - { - if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) - { - throw new ObjectAlreadyInCollectionException(i); - } - } - if (_collection[position] == null) { _collection[position] = obj; diff --git a/ProjectPlane/ProjectPlane/log20240520.txt b/ProjectPlane/ProjectPlane/log20240520.txt new file mode 100644 index 0000000..ca355b0 --- /dev/null +++ b/ProjectPlane/ProjectPlane/log20240520.txt @@ -0,0 +1,12 @@ +2024-05-20 13:25:42.8162 | INFORMATION | ProjectPlane.FormShipCollection | Добавлена коллекция: edhh типа: Massive +2024-05-20 13:25:50.1068 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Blue +2024-05-20 13:25:54.2419 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Red +2024-05-20 13:26:02.3952 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Blue:Red:True:True +2024-05-20 13:26:19.6205 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Purple +2024-05-20 13:26:34.6992 | INFORMATION | ProjectPlane.FormShipCollection | Добавлена коллекция: eha blya типа: List +2024-05-20 13:26:47.2484 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Black:Black:True:True +2024-05-20 13:27:00.5461 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Black:White:True:True +2024-05-20 13:28:19.2272 | INFORMATION | ProjectPlane.FormShipCollection | Удален объект по позиции 1 +2024-05-20 13:28:39.0064 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Red:Purple:True:True +2024-05-20 13:28:46.8157 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Red +2024-05-20 13:29:12.9298 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Black