From fce83f96d581c3f5f6f1c1172dd6a88b0523693e Mon Sep 17 00:00:00 2001 From: AparyanLuiza Date: Fri, 29 Dec 2023 02:08:37 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2=20=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B9.=20?= =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?SetGeneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Exceptions/BulldozerNotFoundException.cs | 17 ++++++++++ .../Exceptions/StorageOverflowException.cs | 17 ++++++++++ ProjectBulldozer/Generics/SetGeneric.cs | 34 +++++++++++++------ 3 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 ProjectBulldozer/Exceptions/BulldozerNotFoundException.cs create mode 100644 ProjectBulldozer/Exceptions/StorageOverflowException.cs 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] {