шаг первый

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

View File

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