142 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus
{
//класс для хранения коллекции карт
internal class MapsCollection
{
//словарь (хранилище) с картами
readonly Dictionary<string, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>> _mapStorage;
//возвращение списка названий карт
public List<string> Keys => _mapStorage.Keys.ToList();
//разделитель для записи информации по элементу словаря в файл
private readonly char separatorDict = '|';
//разделитель для записи коллекции данных в файл
private readonly char separatorData = ';';
//ширина окна отрисовки
private readonly int _pictureWidth;
//высота окна отрисовки
private readonly int _pictureHeight;
//конструктор
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorage = new Dictionary<string, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
//добавление карты
public void AddMap(string name, AbstractMap map)
{
if (!_mapStorage.ContainsKey(name))
{
_mapStorage.Add(name, new MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
//удаление карты
public void DelMap(string name)
{
if (_mapStorage.ContainsKey(name))
{
_mapStorage.Remove(name);
}
}
//сохранение информации по самолётам в ангарах в файл
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 _mapStorage)
{
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}" +
$"{Environment.NewLine}");
}
}
}
//загрузка нформации по по самолётам в ангарах из файла
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
throw new FileNotFoundException("Файл не найден");
}
using (StreamReader sr = new(filename))
{
string str = "";
//если не содержит такую запись или пустой файл
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
throw new FileFormatException("Формат данных в файле неправильный");
}
_mapStorage.Clear();
while ((str = sr.ReadLine()) != null)
{
var element = str.Split(separatorDict);
AbstractMap map = null;
switch (element[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "DesertStormMap":
map = new DesertStormMap();
break;
case "StarWarsMap":
map = new StarWarsMap();
break;
}
_mapStorage.Add(element[0], new MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight,
map));
_mapStorage[element[0]].LoadData(element[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
}
//доступ к аэродрому
public MapWithSetPlanesGeneric<IDrawningObject, AbstractMap> this[string ind]
{
get
{
if(ind != string.Empty)
{
MapWithSetPlanesGeneric<IDrawningObject, AbstractMap> value;
if(_mapStorage.TryGetValue(ind, out value))
{
return value;
}
}
return null;
}
}
}
}