2024-04-17 17:05:21 +04:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2024-04-14 12:53:36 +04:00
|
|
|
|
using ProjectAirplaneWithRadar.Drawnings;
|
2024-04-18 18:14:42 +04:00
|
|
|
|
using ProjectAirplaneWithRadar.Exceptions;
|
2024-04-14 12:53:36 +04:00
|
|
|
|
|
|
|
|
|
namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
2024-03-28 18:07:22 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Класс-хранилище коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public class StorageCollection<T>
|
2024-04-14 12:53:36 +04:00
|
|
|
|
where T : DrawningAirplane
|
2024-03-28 18:07:22 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Словарь (хранилище) с коллекциями
|
|
|
|
|
/// </summary>
|
|
|
|
|
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<string> Keys => _storages.Keys.ToList();
|
|
|
|
|
|
2024-04-14 12:53:36 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ключевое слово, с которого должен начинаться файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _collectionKey = "CollectionsStorage";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorForKeyValue = "|";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorItems = ";";
|
|
|
|
|
|
2024-03-28 18:07:22 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public StorageCollection()
|
|
|
|
|
{
|
|
|
|
|
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление коллекции в хранилище
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
/// <param name="collectionType">тип коллекции</param>
|
|
|
|
|
public void AddCollection(string name, CollectionType collectionType)
|
|
|
|
|
{
|
|
|
|
|
if (name == null || _storages.ContainsKey(name))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
switch (collectionType)
|
|
|
|
|
{
|
|
|
|
|
case CollectionType.None:
|
|
|
|
|
return;
|
|
|
|
|
case CollectionType.Massive:
|
|
|
|
|
_storages[name] = new MassiveGenericObjects<T>();
|
|
|
|
|
return;
|
|
|
|
|
case CollectionType.List:
|
|
|
|
|
_storages[name] = new ListGenericObjects<T>();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
public void DelCollection(string name)
|
|
|
|
|
{
|
|
|
|
|
if(_storages.ContainsKey(name))
|
|
|
|
|
_storages.Remove(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Доступ к коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ICollectionGenericObjects<T>? this[string name]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (name == null || !_storages.ContainsKey(name))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return _storages[name];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-14 12:53:36 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение информации по самолетам в хранилище в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
2024-04-18 18:14:42 +04:00
|
|
|
|
public void SaveData(string filename)
|
2024-04-14 12:53:36 +04:00
|
|
|
|
{
|
2024-04-18 18:14:42 +04:00
|
|
|
|
if (_storages.Count == 0)
|
2024-04-18 21:16:14 +04:00
|
|
|
|
throw new InvalidDataException("В хранилище отсутсвуют коллекции для сохранения");
|
2024-04-14 12:53:36 +04:00
|
|
|
|
|
|
|
|
|
if(File.Exists(filename))
|
2024-04-17 17:05:21 +04:00
|
|
|
|
File.Delete(filename);
|
2024-04-14 12:53:36 +04:00
|
|
|
|
|
2024-04-17 17:05:21 +04:00
|
|
|
|
using FileStream fs = new(filename, FileMode.Create);
|
|
|
|
|
using StreamWriter sw = new StreamWriter(fs);
|
|
|
|
|
sw.Write(_collectionKey);
|
|
|
|
|
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
2024-04-14 12:53:36 +04:00
|
|
|
|
{
|
2024-04-17 17:05:21 +04:00
|
|
|
|
sw.Write(Environment.NewLine);
|
2024-04-14 12:53:36 +04:00
|
|
|
|
if (value.Value.Count == 0)
|
2024-04-17 17:05:21 +04:00
|
|
|
|
{
|
2024-04-14 12:53:36 +04:00
|
|
|
|
continue;
|
2024-04-17 17:05:21 +04:00
|
|
|
|
}
|
2024-04-14 12:53:36 +04:00
|
|
|
|
|
2024-04-17 17:05:21 +04:00
|
|
|
|
sw.Write(value.Key);
|
|
|
|
|
sw.Write(_separatorForKeyValue);
|
|
|
|
|
sw.Write(value.Value.GetCollectionType);
|
|
|
|
|
sw.Write(_separatorForKeyValue);
|
|
|
|
|
sw.Write(value.Value.MaxCount);
|
|
|
|
|
sw.Write(_separatorForKeyValue);
|
2024-04-14 12:53:36 +04:00
|
|
|
|
|
|
|
|
|
foreach (T? item in value.Value.GetItems())
|
|
|
|
|
{
|
|
|
|
|
string data = item?.GetDataForSave() ?? string.Empty;
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 17:05:21 +04:00
|
|
|
|
sw.Write(data);
|
|
|
|
|
sw.Write(_separatorItems);
|
2024-04-14 12:53:36 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации по самолетам в хранилище из файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
2024-04-18 18:14:42 +04:00
|
|
|
|
public void LoadData(string filename)
|
2024-04-14 12:53:36 +04:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2024-04-18 21:16:14 +04:00
|
|
|
|
throw new FileNotFoundException("Файл не существует");
|
2024-04-14 12:53:36 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (FileStream fs = new(filename, FileMode.Open))
|
|
|
|
|
{
|
2024-04-17 17:05:21 +04:00
|
|
|
|
using StreamReader sr = new StreamReader(fs);
|
2024-04-14 12:53:36 +04:00
|
|
|
|
|
2024-04-17 17:05:21 +04:00
|
|
|
|
string str = sr.ReadLine();
|
|
|
|
|
if (str == null || str.Length == 0)
|
2024-04-14 12:53:36 +04:00
|
|
|
|
{
|
2024-04-18 21:16:14 +04:00
|
|
|
|
throw new InvalidDataException("В файле нет данных");
|
2024-04-14 12:53:36 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 17:05:21 +04:00
|
|
|
|
if (!str.Equals(_collectionKey))
|
2024-04-14 12:53:36 +04:00
|
|
|
|
{
|
2024-04-18 21:16:14 +04:00
|
|
|
|
throw new InvalidOperationException("В файле неверные данные");
|
2024-04-14 12:53:36 +04:00
|
|
|
|
}
|
2024-04-17 17:05:21 +04:00
|
|
|
|
_storages.Clear();
|
2024-04-14 12:53:36 +04:00
|
|
|
|
|
2024-04-17 17:05:21 +04:00
|
|
|
|
while (!sr.EndOfStream)
|
2024-04-14 12:53:36 +04:00
|
|
|
|
{
|
2024-04-17 17:05:21 +04:00
|
|
|
|
string[] record = sr.ReadLine().Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (record.Length != 4)
|
2024-04-14 12:53:36 +04:00
|
|
|
|
{
|
2024-04-17 17:05:21 +04:00
|
|
|
|
continue;
|
2024-04-14 12:53:36 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 17:05:21 +04:00
|
|
|
|
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
|
|
|
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
2024-04-18 21:16:14 +04:00
|
|
|
|
throw new InvalidOperationException("Не удалось создать коллекцию");
|
2024-04-17 17:05:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
2024-04-14 12:53:36 +04:00
|
|
|
|
|
2024-04-17 17:05:21 +04:00
|
|
|
|
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (string elem in set)
|
|
|
|
|
{
|
|
|
|
|
if (elem?.CreateDrawningAirplane() is T airplane)
|
|
|
|
|
{
|
2024-04-18 18:14:42 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-04-18 20:43:19 +04:00
|
|
|
|
if (collection.Insert(airplane) == -1)
|
2024-04-18 18:14:42 +04:00
|
|
|
|
{
|
2024-04-18 21:16:14 +04:00
|
|
|
|
throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]);
|
2024-04-18 18:14:42 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (CollectionOverflowException ex)
|
|
|
|
|
{
|
2024-04-18 21:16:14 +04:00
|
|
|
|
throw new ArgumentOutOfRangeException("Коллекция переполнена", ex);
|
2024-04-18 18:14:42 +04:00
|
|
|
|
}
|
2024-04-17 17:05:21 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_storages.Add(record[0], collection);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-14 12:53:36 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание коллекции по типу
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collectionType"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static ICollectionGenericObjects<T>? CreateCollection(CollectionType collectionType)
|
|
|
|
|
{
|
|
|
|
|
return collectionType switch
|
|
|
|
|
{
|
|
|
|
|
CollectionType.Massive => new MassiveGenericObjects<T>(),
|
|
|
|
|
CollectionType.List => new ListGenericObjects<T>(),
|
|
|
|
|
_ => null,
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-03-28 18:07:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|