Compare commits

...

5 Commits

Author SHA1 Message Date
469dc33087 Add serialization exceptions 2023-11-25 23:46:44 +04:00
dd34fcbfa9 Add exceptions 2023-11-25 23:44:01 +04:00
1a799909f9 fix 2023-11-25 23:43:42 +04:00
e1b3695a42 Add storages count check 2023-11-25 23:39:59 +04:00
2306f8b82c Check file existance 2023-11-25 23:27:05 +04:00
4 changed files with 72 additions and 8 deletions

View File

@ -0,0 +1,28 @@
using System.Runtime.Serialization;
namespace AirBomber.Exceptions
{
[Serializable]
internal class EntityNotFoundException : ApplicationException
{
public EntityNotFoundException(int i)
: base($"Не найден объект по позиции {i}")
{ }
public EntityNotFoundException()
: base()
{ }
public EntityNotFoundException(string Message)
: base(Message)
{ }
public EntityNotFoundException(string Message, Exception Exception)
: base(Message, Exception)
{ }
public EntityNotFoundException(SerializationInfo Info, StreamingContext Context)
: base(Info, Context)
{ }
}
}

View File

@ -0,0 +1,28 @@
using System.Runtime.Serialization;
namespace AirBomber.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)
{ }
public StorageOverflowException(SerializationInfo Info, StreamingContext Context)
: base(Info, Context)
{ }
}
}

View File

@ -58,7 +58,7 @@ namespace AirBomber.Generics
public bool SaveData(string FileName) public bool SaveData(string FileName)
{ {
if (_entityStorages.Count == 0) if (_entityStorages.Count == 0)
return false; throw new Exception("Невалиданя операция, нет данных для сохранения");
using (StreamWriter writer = new StreamWriter(FileName, false)) using (StreamWriter writer = new StreamWriter(FileName, false))
{ {
@ -80,10 +80,13 @@ namespace AirBomber.Generics
public bool LoadData(string FileName) public bool LoadData(string FileName)
{ {
if (!File.Exists(FileName))
throw new Exception("Файл не найден");
using (StreamReader reader = new StreamReader(FileName)) using (StreamReader reader = new StreamReader(FileName))
{ {
if (reader.ReadLine() != "BomberStorage") if (reader.ReadLine() != "BomberStorage")
return false; throw new Exception("Неверный формат данных");
_entityStorages.Clear(); _entityStorages.Clear();
@ -105,7 +108,7 @@ namespace AirBomber.Generics
if (Renderer != null) if (Renderer != null)
{ {
if ((Collection + Renderer) == -1) if ((Collection + Renderer) == -1)
return false; throw new Exception("Ошибка добавления в коллекцию");
} }
} }

View File

@ -1,4 +1,6 @@
namespace AirBomber.Generics using AirBomber.Exceptions;
namespace AirBomber.Generics
{ {
internal class SetGeneric<T> internal class SetGeneric<T>
where T : class where T : class
@ -27,13 +29,13 @@
return i; return i;
} }
return -1; throw new StorageOverflowException();
} }
public int Insert(T Entity, int Position) public int Insert(T Entity, int Position)
{ {
if (Position >= _maxCount) if (Position >= _maxCount)
return -1; throw new StorageOverflowException();
if (_objects[Position] is null) if (_objects[Position] is null)
{ {
@ -41,13 +43,16 @@
return Position; return Position;
} }
return -1; throw new StorageOverflowException();
} }
public bool Remove(int Position) public bool Remove(int Position)
{ {
if (Position >= _maxCount) if (Position >= _maxCount)
return false; throw new EntityNotFoundException();
if (_objects[Position] is null)
throw new EntityNotFoundException();
_objects[Position] = default(T); _objects[Position] = default(T);
return true; return true;