Lab8
This commit is contained in:
parent
0ae3e3887e
commit
19eb001cf1
@ -47,38 +47,29 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
|
||||
public int Insert(T obj, IEqualityComparer<T?>? 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<T?>? 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;
|
||||
}
|
||||
|
@ -56,11 +56,11 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
throw new CollectionOverflowException(Count);
|
||||
throw new CollectionOverflowException();
|
||||
}
|
||||
|
||||
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)
|
||||
@ -88,28 +95,27 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
_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)
|
||||
|
@ -16,6 +16,7 @@ public class DrawingSAUEqutables : IEqualityComparer<DrawingBase?>
|
||||
|
||||
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;
|
||||
|
@ -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) { }
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user