diff --git a/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs index 64884c3..14c9a6d 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,6 @@ -namespace ProjectAirFighter.CollectionGenericObjects; +using ProjectSportCar.Exceptions; + +namespace ProjectAirFighter.CollectionGenericObjects; /// /// Параметризованный набор объектов @@ -46,31 +48,31 @@ public class ListGenericObjects : ICollectionGenericObjects 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 GetItems() diff --git a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs index ccda1cf..33df30e 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,6 @@ -namespace ProjectAirFighter.CollectionGenericObjects; +using ProjectSportCar.Exceptions; + +namespace ProjectAirFighter.CollectionGenericObjects; /// /// Параметризованный набор объектов @@ -48,66 +50,62 @@ public class MassiveGenericObjects : ICollectionGenericObjects 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; } diff --git a/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs b/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs new file mode 100644 index 0000000..e331bc7 --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; + +namespace ProjectSportCar.Exceptions; + +/// +/// Класс, описывающий ошибку переполнения коллекции +/// +[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) { } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs b/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs new file mode 100644 index 0000000..4704965 --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; + +namespace ProjectSportCar.Exceptions; + +/// +/// Класс, описывающий ошибку, что по указанной позиции нет элемента +/// +[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) { } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs b/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs new file mode 100644 index 0000000..ae01475 --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; + +namespace ProjectSportCar.Exceptions; + +/// +/// Класс, описывающий ошибку выхода за границы коллекции +/// +[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) { } +} \ No newline at end of file