diff --git a/ProjectBulldozer/Exceptions/BulldozerNotFoundException.cs b/ProjectBulldozer/Exceptions/BulldozerNotFoundException.cs new file mode 100644 index 0000000..ad76077 --- /dev/null +++ b/ProjectBulldozer/Exceptions/BulldozerNotFoundException.cs @@ -0,0 +1,17 @@ +using System.Runtime.Serialization; +namespace ProjectBulldozer.Exceptions +{ + [Serializable] + internal class BulldozerNotFoundException : ApplicationException + { + public BulldozerNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + + public BulldozerNotFoundException() : base() { } + + public BulldozerNotFoundException(string message) : base(message) { } + + public BulldozerNotFoundException(string message, Exception exception) : base(message, exception) { } + + protected BulldozerNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} diff --git a/ProjectBulldozer/Exceptions/StorageOverflowException.cs b/ProjectBulldozer/Exceptions/StorageOverflowException.cs new file mode 100644 index 0000000..3a180e8 --- /dev/null +++ b/ProjectBulldozer/Exceptions/StorageOverflowException.cs @@ -0,0 +1,17 @@ +using System.Runtime.Serialization; +namespace ProjectBulldozer.Exceptions +{ + [Serializable] + internal class StorageOverflowException : ApplicationException + { + public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: {count}") { } + + public StorageOverflowException() : base() { } + + public StorageOverflowException(string message) : base(message) { } + + public StorageOverflowException(string message, Exception Exception) : base(message, Exception) { } + + protected StorageOverflowException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} diff --git a/ProjectBulldozer/Generics/SetGeneric.cs b/ProjectBulldozer/Generics/SetGeneric.cs index 85feb5c..e72614a 100644 --- a/ProjectBulldozer/Generics/SetGeneric.cs +++ b/ProjectBulldozer/Generics/SetGeneric.cs @@ -1,4 +1,5 @@ -namespace ProjectBulldozer.Generics +using ProjectBulldozer.Exceptions; +namespace ProjectBulldozer.Generics { internal class SetGeneric where T : class { @@ -12,24 +13,35 @@ _places = new List(count); } /// Добавление объекта в набор - public int Insert(T tract) + public bool Insert(T tract) { return Insert(tract, 0); } - public int Insert(T tract, int position) + public bool Insert(T tract, int position) { - if (position < 0 || position >= _maxCount) return -1; + if (position < 0 || position >= _maxCount) + { + throw new BulldozerNotFoundException(position); + } + if (Count >= _maxCount) + { + throw new StorageOverflowException(_maxCount); + } _places.Insert(position, tract); - return position; + return true; } - public T? Remove(int position) + public bool Remove(int position) { - if (position >= Count || position < 0) - return null; - - T? tmp = _places[position]; + if (position < 0 || position >= _maxCount) + { + return false; + } + if (_places[position] == null) + { + throw new BulldozerNotFoundException(position); + } _places[position] = null; - return tmp; + return true; } public T? this[int position] {