2024-08-29 19:32:46 +04:00
|
|
|
|
using lab1.Drawnings;
|
2024-08-29 23:07:11 +04:00
|
|
|
|
using lab1.Exceptions;
|
2024-08-29 19:32:46 +04:00
|
|
|
|
using System;
|
2024-04-25 02:02:47 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace lab1.CollectionGenericObjects;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Класс-хранилище коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public class StorageCollection<T>
|
2024-08-29 19:32:46 +04:00
|
|
|
|
where T : DrawningTrackedVehicle
|
2024-04-25 02:02:47 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Словарь(хранилище) с коллекциями
|
|
|
|
|
/// </summary>
|
2024-10-22 13:37:29 +04:00
|
|
|
|
readonly Dictionary<CollectionInfo, ICollectionGenericObjects<T>> _storages;
|
2024-04-25 02:02:47 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий коллекций
|
|
|
|
|
/// </summary>
|
2024-10-22 13:37:29 +04:00
|
|
|
|
public List<CollectionInfo> Keys => _storages.Keys.ToList();
|
2024-04-25 02:02:47 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public StorageCollection()
|
|
|
|
|
{
|
2024-10-22 13:37:29 +04:00
|
|
|
|
_storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
|
2024-04-25 02:02:47 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление коллекции в хранилище
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
/// <param name="collectionType">Тип коллекции</param>
|
|
|
|
|
public void AddCollection(string name, CollectionType collectionType)
|
|
|
|
|
{
|
|
|
|
|
// TODO проверка, что name не пустой и нет в словаре записи с таким ключом
|
|
|
|
|
// TODO Прописать логику для добавления
|
2024-10-22 13:37:29 +04:00
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty);
|
|
|
|
|
if (_storages.ContainsKey(collectionInfo)) return;
|
|
|
|
|
if (collectionType == CollectionType.None) return;
|
|
|
|
|
else if (collectionType == CollectionType.Massive)
|
|
|
|
|
_storages[collectionInfo] = new MassiveGenericObjects<T>();
|
|
|
|
|
else if (collectionType == CollectionType.List)
|
|
|
|
|
_storages[collectionInfo] = new ListGenericObjects<T>();
|
2024-04-25 02:02:47 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
public void DelCollection(string name)
|
|
|
|
|
{
|
|
|
|
|
// TODO Прописать логику для удаления коллекции
|
2024-10-22 13:37:29 +04:00
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
|
|
|
|
|
if (_storages.ContainsKey(collectionInfo))
|
|
|
|
|
_storages.Remove(collectionInfo);
|
2024-04-25 02:02:47 +04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Доступ к коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ICollectionGenericObjects<T>? this[string name]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// TODO Продумать логику получения объекта
|
2024-10-22 13:37:29 +04:00
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
|
|
|
|
|
if (_storages.ContainsKey(collectionInfo))
|
|
|
|
|
return _storages[collectionInfo];
|
2024-04-25 02:02:47 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-08-29 19:32:46 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ключевое слово, с которого должен начинаться файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _collectionKey = "CollectionsStorage";
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorForKeyValue = "|";
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorItems = ";";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение информации по штурмовику в хранилище в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
2024-10-22 13:37:29 +04:00
|
|
|
|
|
|
|
|
|
public void SaveData(string filename)
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
|
|
|
|
if (_storages.Count == 0)
|
|
|
|
|
{
|
2024-10-22 13:37:29 +04:00
|
|
|
|
throw new Exception("В хранилище отсутствуют коллекции для сохранения");
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
using (StreamWriter writer = new StreamWriter(filename))
|
|
|
|
|
{
|
|
|
|
|
writer.Write(_collectionKey);
|
2024-10-22 13:37:29 +04:00
|
|
|
|
foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
2024-10-22 13:37:29 +04:00
|
|
|
|
StringBuilder sb = new();
|
|
|
|
|
sb.Append(Environment.NewLine);
|
|
|
|
|
// не сохраняем пустые коллекции
|
2024-08-29 19:32:46 +04:00
|
|
|
|
if (value.Value.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-10-22 13:37:29 +04:00
|
|
|
|
sb.Append(value.Key);
|
|
|
|
|
sb.Append(_separatorForKeyValue);
|
|
|
|
|
sb.Append(value.Value.MaxCount);
|
|
|
|
|
sb.Append(_separatorForKeyValue);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
foreach (T? item in value.Value.GetItems())
|
|
|
|
|
{
|
|
|
|
|
string data = item?.GetDataForSave() ?? string.Empty;
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-10-22 13:37:29 +04:00
|
|
|
|
sb.Append(data);
|
|
|
|
|
sb.Append(_separatorItems);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-10-22 13:37:29 +04:00
|
|
|
|
writer.Write(sb);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-10-22 13:37:29 +04:00
|
|
|
|
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации по штурмовику в хранилище из файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
2024-10-22 13:37:29 +04:00
|
|
|
|
|
2024-08-29 23:07:11 +04:00
|
|
|
|
public void LoadData(string filename)
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
throw new Exception("Файл не существует");
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
|
|
|
|
using (StreamReader fs = File.OpenText(filename))
|
|
|
|
|
{
|
|
|
|
|
string str = fs.ReadLine();
|
|
|
|
|
if (str == null || str.Length == 0)
|
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
throw new Exception("В файле нет данных");
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
|
|
|
|
if (!str.StartsWith(_collectionKey))
|
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
throw new Exception("В файле неверные данные");
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
|
|
|
|
_storages.Clear();
|
|
|
|
|
string strs = "";
|
|
|
|
|
while ((strs = fs.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
2024-10-22 13:37:29 +04:00
|
|
|
|
if (record.Length != 3)
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-10-22 13:37:29 +04:00
|
|
|
|
CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ??
|
|
|
|
|
throw new Exception("Не удалось определить информацию коллекции: " + record[0]);
|
|
|
|
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionInfo.CollectionType) ??
|
|
|
|
|
throw new Exception("Не удалось создать коллекцию");
|
2024-08-29 19:32:46 +04:00
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
2024-10-22 13:37:29 +04:00
|
|
|
|
throw new Exception("Не удалось определить тип коллекции:" + record[1]);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-10-22 13:37:29 +04:00
|
|
|
|
collection.MaxCount = Convert.ToInt32(record[1]);
|
|
|
|
|
string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
foreach (string elem in set)
|
|
|
|
|
{
|
2024-10-22 13:37:29 +04:00
|
|
|
|
if (elem?.CreateDrawningEntityFighter() is T airplan)
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
try
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
2024-10-22 13:37:29 +04:00
|
|
|
|
if (collection.Insert(airplan) == -1)
|
2024-08-29 23:07:11 +04:00
|
|
|
|
{
|
2024-10-22 13:37:29 +04:00
|
|
|
|
throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]);
|
2024-08-29 23:07:11 +04:00
|
|
|
|
}
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
catch (CollectionOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Коллекция переполнена", ex);
|
|
|
|
|
}
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-22 13:37:29 +04:00
|
|
|
|
_storages.Add(collectionInfo, collection);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-08-29 19:32:46 +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-04-25 02:02:47 +04:00
|
|
|
|
}
|