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;
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
public bool SaveData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if(_storages.Count == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации по самолетам в хранилище из файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
|
|
|
|
public bool LoadData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-17 17:05:21 +04:00
|
|
|
|
return false;
|
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
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (collection.Insert(airplane) == -1)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_storages.Add(record[0], collection);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-14 12:53:36 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
}
|
|
|
|
|
}
|