Создание классов исключений. Обновление SetGeneric

This commit is contained in:
AparyanLuiza 2023-12-29 02:08:37 +04:00
parent 5630e074f1
commit fce83f96d5
3 changed files with 57 additions and 11 deletions

View File

@ -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) { }
}
}

View File

@ -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) { }
}
}

View File

@ -1,4 +1,5 @@
namespace ProjectBulldozer.Generics
using ProjectBulldozer.Exceptions;
namespace ProjectBulldozer.Generics
{
internal class SetGeneric<T> where T : class
{
@ -12,24 +13,35 @@
_places = new List<T?>(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]
{