diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs index d230653..e0d2fe8 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs @@ -47,38 +47,29 @@ public class ListGenericObjects : ICollectionGenericObjects public int Insert(T obj, IEqualityComparer? comparer = null) { + if (Count == _maxCount) throw new CollectionOverflowException(Count); if (comparer != null) { if (_collection.Contains(obj, comparer)) { - throw new ObjectNotUniqueException(); + throw new ObjectNotUniqueException(obj); } } - if (Count == _maxCount) - { - throw new CollectionOverflowException(Count); - } _collection.Add(obj); return Count; } public int Insert(T obj, int position, IEqualityComparer? comparer = null) { + if (Count == _maxCount) throw new CollectionOverflowException(); + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); if (comparer != null) { if (_collection.Contains(obj, comparer)) { - throw new ObjectNotUniqueException(); + throw new ObjectNotUniqueException(obj); } } - if (Count == _maxCount) - { - throw new CollectionOverflowException(Count); - } - if (position >= Count || position < 0) - { - throw new CollectionOverflowException(Count); - } _collection.Insert(position, obj); return position; } diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs index 9163ce7..2ed8270 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs @@ -56,11 +56,11 @@ public class MassiveGenericObjects : ICollectionGenericObjects { if (comparer != null) { - foreach (T? item in _collection) + foreach (T? i in _collection) { - if ((comparer as IEqualityComparer).Equals(obj as DrawingBase, item as DrawingBase)) + if (comparer.Equals(i, obj)) { - throw new ObjectNotUniqueException(); + throw new ObjectNotUniqueException(i); } } } @@ -72,15 +72,22 @@ public class MassiveGenericObjects : ICollectionGenericObjects return i; } } - - throw new CollectionOverflowException(Count); + throw new CollectionOverflowException(); } public int Insert(T obj, int position, IEqualityComparer? comparer = null) { - if (position < 0 || position >= Count) + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); + + if (comparer != null) { - throw new PositionOutOfCollectionException(position); + foreach (T? i in _collection) + { + if (comparer.Equals(i, obj)) + { + throw new ObjectNotUniqueException(i); + } + } } if (_collection[position] == null) @@ -88,28 +95,27 @@ public class MassiveGenericObjects : ICollectionGenericObjects _collection[position] = obj; return position; } - else + int temp = position + 1; + while (temp < Count) { - for (int i = position + 1; i < Count; i++) + if (_collection[temp] == null) { - if (_collection[i] == null) - { - _collection[i] = obj; - return i; - } - } - - for (int i = 0; i < position; i++) - { - if (_collection[i] == null) - { - _collection[i] = obj; - return i; - } + _collection[temp] = obj; + return temp; } + ++temp; } - - throw new CollectionOverflowException(Count); + temp = position - 1; + while (temp >= 0) + { + if (_collection[temp] == null) + { + _collection[temp] = obj; + return temp; + } + --temp; + } + throw new CollectionOverflowException(); } public T? Remove(int position) diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingSAUEqutables.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingSAUEqutables.cs index aad25f4..f803729 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingSAUEqutables.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingSAUEqutables.cs @@ -16,6 +16,7 @@ public class DrawingSAUEqutables : IEqualityComparer if (y == null || y.BaseSAU == null) return false; + if (x.GetType().Name != y.GetType().Name) return false; if (x.BaseSAU.Speed != y.BaseSAU.Speed) return false; diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/ObjectNotUniqueException.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/ObjectNotUniqueException.cs index 9aee3ee..16709d4 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/ObjectNotUniqueException.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/ObjectNotUniqueException.cs @@ -7,14 +7,17 @@ using System.Runtime.Serialization; namespace Project_SelfPropelledArtilleryUnit.Exceptions; -[Serializable] -internal class ObjectNotUniqueException: ApplicationException +[Serializable] +public class ObjectNotUniqueException : ApplicationException { public ObjectNotUniqueException(object i) : base("В коллекции уже есть такой элемент " + i) { } + public ObjectNotUniqueException() : base() { } + public ObjectNotUniqueException(string message) : base(message) { } - public ObjectNotUniqueException(string message, Exception exception) : base(message, exception) - { } - protected ObjectNotUniqueException(SerializationInfo info, StreamingContext context) : base(info, context) { } + + public ObjectNotUniqueException(string message, Exception exception) : base(message, exception) { } + + protected ObjectNotUniqueException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } } diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUCollection.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUCollection.cs index 77f90ad..df6ed2c 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUCollection.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUCollection.cs @@ -70,6 +70,12 @@ public partial class FormSAUCollection : Form MessageBox.Show(ex.Message); _logger.LogError("Ошибка: {Message}", ex.Message); } + catch (ObjectNotUniqueException ex) + { + MessageBox.Show("В коллекции уже есть такой элемент"); + _logger.LogError("Ошибка: {Message}", ex.Message); + } + } private void buttonRemoveSAU_Click(object sender, EventArgs e)