Исправление ListGenericObjects

This commit is contained in:
Timur_Sharafutdinov 2024-05-15 20:51:56 +04:00
parent 612465b571
commit f936efef8f

View File

@ -51,17 +51,47 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position]; return _collection[position];
} }
public int Insert(T? obj) public int Insert(T obj, IEqualityComparer<DrawningLocomotive?>? 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); _collection.Add(obj);
return Count - 1; return _collection.Count;
} }
public int Insert(T? obj, int position) public int Insert(T obj, int position, IEqualityComparer<DrawningLocomotive?>? comparer = null)
{ {
if (Count == _maxCount) throw new CollectionOverflowException(Count); if (position < 0 || position > Count)
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position); {
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); _collection.Insert(position, obj);
return position; return position;
} }
@ -82,16 +112,6 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
} }
} }
public int Insert(T obj, IEqualityComparer<DrawningLocomotive?>? comparer = null)
{
throw new NotImplementedException();
}
public int Insert(T obj, int position, IEqualityComparer<DrawningLocomotive?>? comparer = null)
{
throw new NotImplementedException();
}
public void CollectionSort(IComparer<T?> comparer) public void CollectionSort(IComparer<T?> comparer)
{ {
_collection.Sort(comparer); _collection.Sort(comparer);