PIbd-21_KozyrevSS_GasolineT.../Lab/CarsGenericStorage.cs

127 lines
4.3 KiB
C#
Raw Normal View History

2023-10-18 12:24:01 +04:00
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;
2023-10-18 12:28:36 +04:00
namespace Lab.Generics
2023-10-18 12:24:01 +04:00
{
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;
2023-11-15 10:25:49 +04:00
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
2023-10-18 12:24:01 +04:00
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;
}
}
2023-11-15 10:25:49 +04:00
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}");
2023-11-15 13:46:17 +04:00
2023-11-15 10:25:49 +04:00
}
if (data.Length == 0)
{
return false;
}
2023-11-15 11:34:08 +04:00
using (StreamWriter writer = new StreamWriter(filename))
{
writer.WriteLine("CarStorage");
writer.Write(data.ToString());
return true;
}
2023-11-15 10:25:49 +04:00
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
2023-11-15 11:34:08 +04:00
using (StreamReader reader = new StreamReader(filename))
2023-11-15 10:25:49 +04:00
{
2023-11-15 11:34:08 +04:00
string checker = reader.ReadLine();
if (checker == null)
return false;
if (!checker.StartsWith("CarStorage"))
return false;
_carStorages.Clear();
2023-11-15 13:46:17 +04:00
string strs;
bool firstinit = true;
while ((strs = reader.ReadLine()) != null)
2023-11-15 10:25:49 +04:00
{
2023-11-15 13:46:17 +04:00
if (strs == null && firstinit)
return false;
if (strs == null )
break;
firstinit = false;
string name = strs.Split('|')[0];
2023-11-15 11:34:08 +04:00
CarsGenericCollection<DrawTanker, DrawingObjectTanker> collection = new(_pictureWidth, _pictureHeight);
2023-11-15 13:46:17 +04:00
foreach (string data in strs.Split('|')[1].Split(';'))
{
2023-11-15 11:34:08 +04:00
DrawTanker? car =
2023-11-15 13:46:17 +04:00
data?.CreateDrawTanker(_separatorForObject, _pictureWidth, _pictureHeight);
2023-11-15 11:34:08 +04:00
if (car != null)
2023-11-15 10:25:49 +04:00
{
2023-11-15 11:34:08 +04:00
if (!(collection + car))
{
return false;
}
2023-11-15 10:25:49 +04:00
}
2023-11-15 13:46:17 +04:00
2023-11-15 10:25:49 +04:00
}
2023-11-15 13:46:17 +04:00
_carStorages.Add(name, collection);
2023-11-15 10:25:49 +04:00
}
2023-11-15 11:34:08 +04:00
return true;
2023-11-15 10:25:49 +04:00
}
}
2023-10-18 12:24:01 +04:00
}
}