This commit is contained in:
kirkorovka 2024-06-09 19:21:15 +04:00
parent 0ae3e3887e
commit 19eb001cf1
5 changed files with 51 additions and 44 deletions

View File

@ -47,38 +47,29 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
public int Insert(T obj, IEqualityComparer<T?>? comparer = null) public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{ {
if (Count == _maxCount) throw new CollectionOverflowException(Count);
if (comparer != null) if (comparer != null)
{ {
if (_collection.Contains(obj, comparer)) if (_collection.Contains(obj, comparer))
{ {
throw new ObjectNotUniqueException(); throw new ObjectNotUniqueException(obj);
} }
} }
if (Count == _maxCount)
{
throw new CollectionOverflowException(Count);
}
_collection.Add(obj); _collection.Add(obj);
return Count; return Count;
} }
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null) public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{ {
if (Count == _maxCount) throw new CollectionOverflowException();
if (position >= Count || position < 0) throw new PositionOutOfCollectionException();
if (comparer != null) if (comparer != null)
{ {
if (_collection.Contains(obj, comparer)) 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); _collection.Insert(position, obj);
return position; return position;
} }

View File

@ -56,11 +56,11 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
{ {
if (comparer != null) if (comparer != null)
{ {
foreach (T? item in _collection) foreach (T? i in _collection)
{ {
if ((comparer as IEqualityComparer<DrawingBase>).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<T> : ICollectionGenericObjects<T>
return i; return i;
} }
} }
throw new CollectionOverflowException();
throw new CollectionOverflowException(Count);
} }
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null) public int Insert(T obj, int position, IEqualityComparer<T?>? 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) if (_collection[position] == null)
@ -88,28 +95,27 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
_collection[position] = obj; _collection[position] = obj;
return position; 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[temp] = obj;
{ return temp;
_collection[i] = obj;
return i;
}
}
for (int i = 0; i < position; i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
} }
++temp;
} }
temp = position - 1;
throw new CollectionOverflowException(Count); while (temp >= 0)
{
if (_collection[temp] == null)
{
_collection[temp] = obj;
return temp;
}
--temp;
}
throw new CollectionOverflowException();
} }
public T? Remove(int position) public T? Remove(int position)

View File

@ -16,6 +16,7 @@ public class DrawingSAUEqutables : IEqualityComparer<DrawingBase?>
if (y == null || y.BaseSAU == null) return false; if (y == null || y.BaseSAU == null) return false;
if (x.GetType().Name != y.GetType().Name) return false; if (x.GetType().Name != y.GetType().Name) return false;
if (x.BaseSAU.Speed != y.BaseSAU.Speed) return false; if (x.BaseSAU.Speed != y.BaseSAU.Speed) return false;

View File

@ -7,14 +7,17 @@ using System.Runtime.Serialization;
namespace Project_SelfPropelledArtilleryUnit.Exceptions; namespace Project_SelfPropelledArtilleryUnit.Exceptions;
[Serializable]
internal class ObjectNotUniqueException: ApplicationException [Serializable]
public class ObjectNotUniqueException : ApplicationException
{ {
public ObjectNotUniqueException(object i) : base("В коллекции уже есть такой элемент " + i) { } public ObjectNotUniqueException(object i) : base("В коллекции уже есть такой элемент " + i) { }
public ObjectNotUniqueException() : base() { } public ObjectNotUniqueException() : base() { }
public ObjectNotUniqueException(string message) : base(message) { } public ObjectNotUniqueException(string message) : base(message) { }
public ObjectNotUniqueException(string message, Exception exception) : base(message, exception)
{ } public ObjectNotUniqueException(string message, Exception exception) : base(message, exception) { }
protected ObjectNotUniqueException(SerializationInfo info, StreamingContext context) : base(info, context) { }
protected ObjectNotUniqueException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
} }

View File

@ -70,6 +70,12 @@ public partial class FormSAUCollection : Form
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message);
_logger.LogError("Ошибка: {Message}", 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) private void buttonRemoveSAU_Click(object sender, EventArgs e)