шаг первый

This commit is contained in:
ikswi 2024-05-12 02:46:08 +04:00
parent 40bc918983
commit bacf66aad8
5 changed files with 100 additions and 40 deletions

View File

@ -1,4 +1,6 @@
namespace ProjectAirFighter.CollectionGenericObjects;
using ProjectSportCar.Exceptions;
namespace ProjectAirFighter.CollectionGenericObjects;
/// <summary>
/// Параметризованный набор объектов
@ -46,31 +48,31 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
public T? Get(int position)
{
if (position >= Count || position < 0) return null;
if (position >= Count || position < 0) throw new PositionOutOfCollectionException();
return _collection[position];
}
public int Insert(T obj)
{
if (Count + 1 > _maxCount) return -1;
if (Count + 1 > _maxCount) throw new CollectionOverflowException();
_collection.Add(obj);
return Count;
}
public int Insert(T obj, int position)
{
if (Count + 1 > _maxCount) return -1;
if (position < 0 || position > Count) return -1;
if (Count + 1 > _maxCount) throw new CollectionOverflowException();
if (position < 0 || position > Count) throw new PositionOutOfCollectionException();
_collection.Insert(position, obj);
return 1;
return position;
}
public T? Remove(int position)
{
if (position < 0 || position > Count) return null;
T? pos = _collection[position];
if (position >= Count || position < 0) throw new PositionOutOfCollectionException();
T temp = _collection[position];
_collection.RemoveAt(position);
return pos;
return temp;
}
public IEnumerable<T?> GetItems()

View File

@ -1,4 +1,6 @@
namespace ProjectAirFighter.CollectionGenericObjects;
using ProjectSportCar.Exceptions;
namespace ProjectAirFighter.CollectionGenericObjects;
/// <summary>
/// Параметризованный набор объектов
@ -48,66 +50,62 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
public T? Get(int position)
{
if (position >= _collection.Length || position < 0)
{ return null; }
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException();
if (_collection[position] == null) throw new ObjectNotFoundException();
return _collection[position];
}
public int Insert(T obj)
{
int index = 0;
while (index < _collection.Length)
for (int i = 0; i < Count; i++)
{
if (_collection[index] == null)
if (_collection[i] == null)
{
_collection[index] = obj;
return index;
_collection[i] = obj;
return i;
}
index++;
}
return -1;
throw new CollectionOverflowException();
}
public int Insert(T obj, int position)
{
if (position >= _collection.Length || position < 0)
{ return -1; }
if (position >= Count || position < 0) throw new PositionOutOfCollectionException();
if (_collection[position] == null)
{
_collection[position] = obj;
return position;
}
int index;
for (index = position + 1; index < _collection.Length; ++index)
int temp = position + 1;
while (temp < Count)
{
if (_collection[index] == null)
if (_collection[temp] == null)
{
_collection[position] = obj;
return position;
_collection[temp] = obj;
return temp;
}
++temp;
}
for (index = position - 1; index >= 0; --index)
temp = position - 1;
while (temp >= 0)
{
if (_collection[index] == null)
if (_collection[temp] == null)
{
_collection[position] = obj;
return position;
_collection[temp] = obj;
return temp;
}
--temp;
}
return -1;
throw new CollectionOverflowException();
}
public T? Remove(int position)
{
if (position >= _collection.Length || position < 0)
{
return null;
}
T obj = _collection[position];
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position);
if (_collection[position] == null) throw new ObjectNotFoundException(position);
T? obj = _collection[position];
_collection[position] = null;
return obj;
}

View File

@ -0,0 +1,20 @@
using System.Runtime.Serialization;
namespace ProjectSportCar.Exceptions;
/// <summary>
/// Класс, описывающий ошибку переполнения коллекции
/// </summary>
[Serializable]
internal class CollectionOverflowException : ApplicationException
{
public CollectionOverflowException(int count) : base("В коллекции превышено допустимое количество: " + count) { }
public CollectionOverflowException() : base() { }
public CollectionOverflowException(string message) : base(message) { }
public CollectionOverflowException(string message, Exception exception) : base(message, exception) { }
protected CollectionOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}

View File

@ -0,0 +1,20 @@
using System.Runtime.Serialization;
namespace ProjectSportCar.Exceptions;
/// <summary>
/// Класс, описывающий ошибку, что по указанной позиции нет элемента
/// </summary>
[Serializable]
internal class ObjectNotFoundException : ApplicationException
{
public ObjectNotFoundException(int i) : base("Не найден объект по позиции " + i) { }
public ObjectNotFoundException() : base() { }
public ObjectNotFoundException(string message) : base(message) { }
public ObjectNotFoundException(string message, Exception exception) : base(message, exception) { }
protected ObjectNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}

View File

@ -0,0 +1,20 @@
using System.Runtime.Serialization;
namespace ProjectSportCar.Exceptions;
/// <summary>
/// Класс, описывающий ошибку выхода за границы коллекции
/// </summary>
[Serializable]
internal class PositionOutOfCollectionException : ApplicationException
{
public PositionOutOfCollectionException(int i) : base("Выход за границы коллекции. Позиция " + i) { }
public PositionOutOfCollectionException() : base() { }
public PositionOutOfCollectionException(string message) : base(message) { }
public PositionOutOfCollectionException(string message, Exception exception) : base(message, exception) { }
protected PositionOutOfCollectionException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}