ещё раз

This commit is contained in:
ikswi 2024-05-16 08:51:00 +04:00
parent 26b6a7c8f0
commit 13860fad44
10 changed files with 101 additions and 31 deletions

View File

@ -1,8 +1,7 @@
namespace ProjectAirFighter.CollectionGenericObjects;
using ProjectAirFighter.CollectionGenericObjects;
namespace AirFighter;
/// <summary>
/// Класс, хранящиий информацию по коллекции
/// </summary>
public class CollectionInfo : IEquatable<CollectionInfo>
{
/// <summary>
@ -31,7 +30,8 @@ public class CollectionInfo : IEquatable<CollectionInfo>
/// <param name="name">Название</param>
/// <param name="collectionType">Тип</param>
/// <param name="description">Описание</param>
public CollectionInfo(string name, CollectionType collectionType, string description)
public CollectionInfo(string name, CollectionType collectionType, string
description)
{
Name = name;
CollectionType = collectionType;
@ -45,13 +45,15 @@ public class CollectionInfo : IEquatable<CollectionInfo>
/// <returns>Объект или null</returns>
public static CollectionInfo? GetCollectionInfo(string data)
{
string[] strs = data.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
string[] strs = data.Split(_separator,
StringSplitOptions.RemoveEmptyEntries);
if (strs.Length < 1 || strs.Length > 3)
{
return null;
}
return new CollectionInfo(strs[0], (CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ? strs[2] : string.Empty);
return new CollectionInfo(strs[0],
(CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ? strs[2] : string.Empty);
}
public override string ToString()
@ -69,6 +71,12 @@ public class CollectionInfo : IEquatable<CollectionInfo>
return Equals(obj as CollectionInfo);
}
public bool IsEmpty()
{
if (string.IsNullOrEmpty(Name) && CollectionType != CollectionType.None) return true;
return false;
}
public override int GetHashCode()
{
return Name.GetHashCode();

View File

@ -1,4 +1,5 @@
using ProjectAirFighter.Exceptions;
using AirFighter;
using ProjectAirFighter.Exceptions;
namespace ProjectAirFighter.CollectionGenericObjects;

View File

@ -1,5 +1,8 @@
using ProjectAirFighter.Drawnings;
using AirFighter;
using Microsoft.AspNetCore.Http;
using ProjectAirFighter.Drawnings;
using ProjectAirFighter.Exceptions;
using System.Collections.Generic;
using System.Text;
namespace ProjectAirFighter.CollectionGenericObjects;
@ -46,16 +49,15 @@ public class StorageCollection<T>
/// <summary>
/// Добавление коллекции в хранилище
/// </summary>
/// <param name="name">Название коллекции</param>
/// <param name="collectionType">тип коллекции</param>
public void AddCollection(string name, CollectionType collectionType)
/// <param name="collectionInfo">тип коллекции</param>
public void AddCollection(CollectionInfo collectionInfo)
{
if (name == null || _storages.ContainsKey(name)) { return; }
if (_storages.ContainsKey(collectionInfo)) throw new CollectionAlreadyExistsException(collectionInfo);
switch (collectionType)
{
case CollectionType.None:
return;
throw new CollectionTypeException("Пустой тип коллекции");
case CollectionType.Massive:
_storages[name] = new MassiveGenericObjects<T>();
return;
@ -69,10 +71,10 @@ public class StorageCollection<T>
/// Удаление коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
public void DelCollection(CollectionInfo collectionInfo)
{
if (_storages.ContainsKey(name))
_storages.Remove(name);
if (_storages.ContainsKey(collectionInfo))
_storages.Remove(collectionInfo);
}
/// <summary>
@ -80,12 +82,13 @@ public class StorageCollection<T>
/// </summary>
/// <param name="name">Название коллекции</param>
/// <returns></returns>
public ICollectionGenericObjects<T>? this[string name]
public ICollectionGenericObjects<T>? this[CollectionInfo collectionInfo]
{
get
{
if (name == null || !_storages.ContainsKey(name)) { return null; }
return _storages[name];
if(_storages.ContainsKey(collectionInfo))
return _storages[collectionInfo];
return null;
}
}
@ -152,7 +155,7 @@ public class StorageCollection<T>
{
if (!File.Exists(filename))
{
throw new FileNotFoundException("Файл не существует");
throw new FileNotFoundException(filename);
}
using (StreamReader fs = File.OpenText(filename))
{
@ -165,7 +168,7 @@ public class StorageCollection<T>
if (!str.StartsWith(_collectionKey))
{
throw new FormatException("В файле неверные данные");
throw new FormatException(filename);
}
_storages.Clear();
@ -196,14 +199,11 @@ public class StorageCollection<T>
{
try
{
if (collection.Insert(militaryAircraft) == -1)
{
throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]);
}
collection.Insert(militaryAircraft);
}
catch (CollectionOverflowException ex)
catch (Exception ex)
{
throw new CollectionOverflowException("Коллекция переполнена", ex);
throw new FileFormatException(filename, ex);
}
}
}

View File

@ -1,4 +1,6 @@
namespace AirFighter;
using System.Runtime.Serialization;
namespace AirFighter;
public class CollectionInfoException : Exception
{

View File

@ -1,4 +1,5 @@
namespace AirFighter;
using System.Runtime.Serialization;
namespace AirFighter;
public class CollectionInsertException : Exception
{

View File

@ -0,0 +1,14 @@
using System.Runtime.Serialization;
namespace AirFighter;
public class DrawningEquitablesException : Exception
{
public DrawningEquitablesException() : base("Объекты прорисовки одинаковые") { }
public DrawningEquitablesException(string message) : base(message) { }
public DrawningEquitablesException(string message, Exception exception) :
base(message, exception)
{ }
protected DrawningEquitablesException(SerializationInfo info, StreamingContext
contex) : base(info, contex) { }
}

View File

@ -0,0 +1,15 @@
using System.Runtime.Serialization;
namespace AirFighter;
public class EmptyFileExeption : Exception
{
public EmptyFileExeption(string name) : base($"Файл {name} пустой ") { }
public EmptyFileExeption() : base("В хранилище отсутствуют коллекции для сохранения") { }
public EmptyFileExeption(string name, string message) : base(message) { }
public EmptyFileExeption(string name, string message, Exception exception) :
base(message, exception)
{ }
protected EmptyFileExeption(SerializationInfo info, StreamingContext
contex) : base(info, contex) { }
}

View File

@ -0,0 +1,14 @@
using System.Runtime.Serialization;
namespace AirFighter;
public class FileFormatException : Exception
{
public FileFormatException() : base() { }
public FileFormatException(string message) : base(message) { }
public FileFormatException(string name, Exception exception) :
base($"Файл {name} имеет неверный формат. Ошибка: {exception.Message}", exception)
{ }
protected FileFormatException(SerializationInfo info, StreamingContext
contex) : base(info, contex) { }
}

View File

@ -0,0 +1,15 @@
using System.Runtime.Serialization;
namespace AirFighter;
public class FileNotFoundException : Exception
{
public FileNotFoundException(string name) : base($"Файл {name} не существует ") { }
public FileNotFoundException() : base() { }
public FileNotFoundException(string name, string message) : base(message) { }
public FileNotFoundException(string name, string message, Exception exception) :
base(message, exception)
{ }
protected FileNotFoundException(SerializationInfo info, StreamingContext
contex) : base(info, contex) { }
}

View File

@ -14,7 +14,7 @@
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "Stormtrooper"
"Application": "AirFighter"
}
}
}