PIbd-21_Valitov_D.F_Sailboa.../Sailboat/MapsCollection.cs

110 lines
3.8 KiB
C#
Raw Normal View History

2023-04-14 22:41:19 +04:00
using System;
using System.Collections.Generic;
2023-04-21 23:04:27 +04:00
using System.IO;
2023-04-14 22:41:19 +04:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat
{
internal class MapsCollection
{
2023-04-21 23:04:27 +04:00
readonly Dictionary<string, MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>> _mapStorages;
2023-04-14 22:41:19 +04:00
public List<string> Keys => _mapStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
2023-04-21 23:04:27 +04:00
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
2023-04-14 22:41:19 +04:00
public MapsCollection(int pictureWidth, int pictureHeight)
{
2023-04-21 23:04:27 +04:00
_mapStorages = new Dictionary<string, MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>>();
2023-04-14 22:41:19 +04:00
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(string name, AbstractMap map)
{
if (Keys.Contains(name)) return;
2023-04-21 23:04:27 +04:00
_mapStorages.Add(name, new MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
2023-04-14 22:41:19 +04:00
}
public void DelMap(string name)
{
_mapStorages.Remove(name);
}
2023-04-21 23:04:27 +04:00
public MapWithSetBoatsGeneric<IDrawingObject, AbstractMap> this[string ind]
2023-04-14 22:41:19 +04:00
{
get
{
_mapStorages.TryGetValue(ind, out var result);
return result;
}
}
2023-04-21 23:04:27 +04:00
/// <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))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
throw new FileFormatException("Формат данных в файле не правильный");
}
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var tempElem = str.Split(separatorDict);
AbstractMap map = null;
switch (tempElem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "WaterMap":
map = new WaterMap();
break;
}
_mapStorages.Add(tempElem[0], new MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[tempElem[0]].LoadData(tempElem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
}
2023-04-14 22:41:19 +04:00
}
}