ISEbd-22_Baygulov_A.A._Elec.../ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs

143 lines
5.3 KiB
C#

using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics
{
internal class LocomotivesGenericStorage
{
readonly Dictionary<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotivesStorage;
public List<string> Keys => _locomotivesStorage.Keys.ToList();
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public LocomotivesGenericStorage(int pictureWidth, int pictureHeight)
{
_locomotivesStorage = new Dictionary<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (!_locomotivesStorage.ContainsKey(name))
{
_locomotivesStorage.Add(name, new LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight));
}
}
public void DelSet(string name)
{
if (_locomotivesStorage.ContainsKey(name))
{
_locomotivesStorage.Remove(name);
}
}
public LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>?
this[string ind]
{
get
{
if (_locomotivesStorage.ContainsKey(ind))
{
return _locomotivesStorage[ind];
}
return null;
}
}
public void SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record in _locomotivesStorage)
{
StringBuilder records = new();
foreach (DrawingLocomotive? elem in record.Value.GetLocomotives)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
throw new Exception("Нет данных для записи, ошибка");
}
using StreamWriter fs = new StreamWriter(filename);
{
fs.WriteLine($"LocomotiveStorage{Environment.NewLine}");
fs.WriteLine(data);
}
return;
}
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
throw new FileNotFoundException("Файл не найден");
}
using (StreamReader fs = File.OpenText(filename))
{
string str = fs.ReadLine();
if (str == null || str.Length == 0)
{
throw new IOException("Нет данных для загрузки");
}
if (!str.StartsWith("LocomotiveStorage"))
{
//если нет такой записи, то это не те данные
throw new FileFormatException("Неверный формат данных");
}
_locomotivesStorage.Clear();
string strs = "";
while ((strs = fs.ReadLine()) != null)
{
if (strs == null)
{
throw new FileNotFoundException("Нет данных для загрузки");
}
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingLocomotive? loco = elem?.CreateDrawningLocomotive(_separatorForObject, _pictureWidth, _pictureHeight);
if (loco != null)
{
if ((collection + loco) == -1) // for my realization it's -1, for eegov's realization it's boolean
{
throw new Exception("Ошибка добавления ");
}
}
}
_locomotivesStorage.Add(record[0], collection);
}
return;
}
}
}
}