193 lines
7.8 KiB
C#
193 lines
7.8 KiB
C#
using Airbus_Base.DrawningObjects;
|
|
using Airbus_Base.MovementStrategy;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Airbus_Base.Generics
|
|
{
|
|
/// <summary>
|
|
/// Класс для хранения коллекции
|
|
/// </summary>
|
|
internal class TheAirplaneGenericStorage
|
|
{
|
|
/// <summary>
|
|
/// Словарь (хранилище)
|
|
/// </summary>
|
|
readonly Dictionary<AirplanesCollectionInfo, TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> _airplaneStorages;
|
|
|
|
/// <summary>
|
|
/// Возвращение списка названий наборов
|
|
/// </summary>
|
|
public List<AirplanesCollectionInfo> Keys => _airplaneStorages.Keys.ToList();
|
|
|
|
/// <summary>
|
|
/// Ширина окна отрисовки
|
|
/// </summary>
|
|
private readonly int _pictureWidth;
|
|
|
|
/// <summary>
|
|
/// Высота окна отрисовки
|
|
/// </summary>
|
|
private readonly int _pictureHeight;
|
|
|
|
/// <summary>
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
/// </summary>
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
/// <summary>
|
|
/// Разделитель для записей коллекции данных в файл
|
|
/// </summary>
|
|
private readonly char _separatorRecords = ';';
|
|
/// <summary>
|
|
/// Разделитель для записи информации по объекту в файл
|
|
/// </summary>
|
|
private static readonly char _separatorForObject = ':';
|
|
|
|
/// <summary>
|
|
/// Конструктор
|
|
/// </summary>
|
|
/// <param name="pictureWidth"></param>
|
|
/// <param name="pictureHeight"></param>
|
|
public TheAirplaneGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_airplaneStorages = new Dictionary<AirplanesCollectionInfo, TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
/// <summary>
|
|
/// Сохранение информации по самолётам в хранилище в файл
|
|
/// </summary>
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
|
public void SaveData(string filename)
|
|
{
|
|
if (File.Exists(filename))
|
|
{
|
|
File.Delete(filename);
|
|
}
|
|
StringBuilder data = new();
|
|
foreach (KeyValuePair<AirplanesCollectionInfo, TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplaneStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawningAirplane? elem in record.Value.GetTheAirplanes)
|
|
{
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
}
|
|
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
|
|
}
|
|
|
|
if (data.Length == 0)
|
|
{
|
|
throw new Exception("Невалиданя операция, нет данных для сохранения");
|
|
}
|
|
string toWrite = $"AirplaneStorage{Environment.NewLine}{data}";
|
|
var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
using (StreamWriter sw = new(filename))
|
|
{
|
|
foreach (var str in strs)
|
|
{
|
|
sw.WriteLine(str);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Загрузка информации по самолётам в хранилище из файла
|
|
/// </summary>
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
|
public void LoadData(string filename)
|
|
{
|
|
if (!File.Exists(filename))
|
|
{
|
|
throw new IOException("Файл не найден");
|
|
}
|
|
using (StreamReader sr = new(filename))
|
|
{
|
|
string str = sr.ReadLine();
|
|
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
if (strs == null || strs.Length == 0)
|
|
{
|
|
throw new IOException("Нет данных для загрузки");
|
|
}
|
|
if (!strs[0].StartsWith("AirplaneStorage"))
|
|
{
|
|
throw new IOException("Неверный формат данных");
|
|
}
|
|
_airplaneStorages.Clear();
|
|
do
|
|
{
|
|
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 2)
|
|
{
|
|
str = sr.ReadLine();
|
|
continue;
|
|
}
|
|
TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane> collection = new(_pictureWidth, _pictureHeight);
|
|
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
DrawningAirplane? airplane = elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (airplane != null)
|
|
{
|
|
if (!(collection + airplane))
|
|
{
|
|
throw new IOException("Ошибка добавления в коллекцию");
|
|
}
|
|
}
|
|
}
|
|
_airplaneStorages.Add(new AirplanesCollectionInfo(record[0], string.Empty), collection);
|
|
|
|
str = sr.ReadLine();
|
|
} while (str != null);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавление набора
|
|
/// </summary>
|
|
/// <param name="name">Название набора</param>
|
|
public void AddSet(string name)
|
|
{
|
|
_airplaneStorages.Add(new AirplanesCollectionInfo(name, string.Empty), new TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление набора
|
|
/// </summary>
|
|
/// <param name="name">Название набора</param>
|
|
public void DelSet(string name)
|
|
{
|
|
if (!_airplaneStorages.ContainsKey(new AirplanesCollectionInfo(name, string.Empty)))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_airplaneStorages.Remove(new AirplanesCollectionInfo(name, string.Empty));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Доступ к набору
|
|
/// </summary>
|
|
/// <param name="ind"></param>
|
|
/// <returns></returns>
|
|
public TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>? this[string ind]
|
|
{
|
|
get
|
|
{
|
|
AirplanesCollectionInfo indObj = new AirplanesCollectionInfo(ind, string.Empty);
|
|
if (_airplaneStorages.ContainsKey(indObj))
|
|
{
|
|
return _airplaneStorages[indObj];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|