2022-11-23 20:03:52 +04:00

145 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
/// </summary>
public List<string> Keys => _mapStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
if (!_mapStorages.ContainsKey(name))
{
MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap> Map = new(_pictureWidth, _pictureHeight, map);
_mapStorages.Add(name, Map);
}
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name);
}
/// <summary>
/// Доступ к парковке
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap> this[string ind]
{
get
{
if (_mapStorages.ContainsKey(ind))
return _mapStorages[ind];
return null;
}
}
/// <summary>
/// Сохранение информации по автомобилям в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns></returns>
public void SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = new(filename))
{
sw.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
}
/// <summary>
/// Загрузка нформации по автомобилям на парковках из файла
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
throw new FileNotFoundException("Файл не найден");
}
using (StreamReader sr = new(filename))
{
bool isFirst = true;
string str;
while ((str = sr.ReadLine()) != null)
{
if (isFirst)
{
if (!str.Contains("MapsCollection"))
{
throw new FileFormatException("Формат данных в файле не правильный");
}
else
{
_mapStorages.Clear();
}
isFirst = false;
}
else
{
var tempElem = str.Split(separatorDict);
AbstractMap map = null;
switch (tempElem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "SecondMap":
map = new SecondMap();
break;
case "ThirdMap":
map = new ThirdMap();
break;
}
_mapStorages.Add(tempElem[0], new MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[tempElem[0]].LoadData(tempElem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
}
}
}
}