PIbd-21_KozyrevSS_GasolineT.../Lab/CarsGenericStorage.cs
2023-11-15 11:34:08 +04:00

124 lines
4.3 KiB
C#

using Lab.DrawningObjects;
using Lab.MovementStrategy;
using Lab.Generics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab.Generics
{
internal class CarsGenericStorage
{
readonly Dictionary<string, CarsGenericCollection<DrawTanker, DrawingObjectTanker>> _carStorages;
public List<string> Keys => _carStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public CarsGenericStorage(int pictureWidth, int pictureHeight)
{
_carStorages = new Dictionary<string, CarsGenericCollection<DrawTanker, DrawingObjectTanker>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (_carStorages.ContainsKey(name)) return;
_carStorages[name] = new CarsGenericCollection<DrawTanker, DrawingObjectTanker>(_pictureWidth, _pictureHeight);
}
public void DelSet(string name)
{
if (!_carStorages.ContainsKey(name)) return;
_carStorages.Remove(name);
}
public CarsGenericCollection<DrawTanker, DrawingObjectTanker>?
this[string ind]
{
get
{
if (_carStorages.ContainsKey(ind)) return _carStorages[ind];
return null;
}
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, CarsGenericCollection<DrawTanker, DrawingObjectTanker>> record in _carStorages)
{
StringBuilder records = new();
foreach (DrawTanker? elem in record.Value.GetCars)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using (StreamWriter writer = new StreamWriter(filename))
{
writer.WriteLine("CarStorage");
writer.Write(data.ToString());
return true;
}
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader reader = new StreamReader(filename))
{
string checker = reader.ReadLine();
if (checker == null)
return false;
string strs = reader.ReadLine();
if (!checker.StartsWith("CarStorage"))
return false;
_carStorages.Clear();
foreach (string data in strs.Split(';'))
{
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
CarsGenericCollection<DrawTanker, DrawingObjectTanker> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawTanker? car =
elem?.CreateDrawTanker(_separatorForObject, _pictureWidth, _pictureHeight);
if (car != null)
{
if (!(collection + car))
{
return false;
}
}
}
_carStorages.Add(record[0], collection);
}
return true;
}
}
}
}