PIbd22_Kamcharova_K.A._Doub.../DoubleDeckerBus/Generic/BusesGenericStorage.cs

126 lines
4.3 KiB
C#
Raw Permalink Normal View History

2023-11-25 14:55:27 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DoubleDeckerbus.Drawing;
using DoubleDeckerbus.Move_Strategy;
namespace DoubleDeckerbus.Generic
{
internal class BusesGenericStorage
{
readonly Dictionary<string, BusGenericCollection<DrawingBus, DrawingObjectBus>> _busStorages;
public List<string> Keys => _busStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
2023-12-15 20:07:22 +04:00
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
2023-11-25 14:55:27 +04:00
public BusesGenericStorage(int pictureWidth, int pictureHeight)
{
_busStorages = new Dictionary<string, BusGenericCollection<DrawingBus, DrawingObjectBus>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
2023-12-15 20:07:22 +04:00
2023-11-25 14:55:27 +04:00
public void AddSet(string name)
{
foreach (string nameStorage in Keys)
{
if (nameStorage == name)
{
MessageBox.Show("Набор с заданным именем уже есть", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
_busStorages.Add(name, new BusGenericCollection<DrawingBus, DrawingObjectBus>(_pictureWidth, _pictureHeight));
}
2023-12-15 20:07:22 +04:00
2023-11-25 14:55:27 +04:00
public void DelSet(string name)
{
if (_busStorages.ContainsKey(name))
{
_busStorages.Remove(name);
}
2023-12-15 20:07:22 +04:00
2023-11-25 14:55:27 +04:00
}
public BusGenericCollection<DrawingBus, DrawingObjectBus>? this[string ind]
{
get
{
if (_busStorages.ContainsKey(ind))
{
return _busStorages[ind];
}
return null;
}
}
2023-12-15 20:07:22 +04:00
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = File.CreateText(filename))
{
sw.WriteLine($"DoubleDeckerBusStorage");
foreach (var record in _busStorages)
{
StringBuilder records = new();
foreach (DrawingBus? elem in record.Value.GetBus)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
sw.WriteLine($"{record.Key}{_separatorForKeyValue}{records}");
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = File.OpenText(filename))
{
string? curLine = sr.ReadLine();
if (curLine == null || !curLine.Contains("AirbusStorage"))
{
return false;
}
_busStorages.Clear();
curLine = sr.ReadLine();
while (curLine != null)
{
string[] record = curLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
BusGenericCollection<DrawingBus, DrawingObjectBus> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingBus? Bus = elem?.CreateDrawingBus(_separatorForObject, _pictureWidth, _pictureHeight);
if (Bus != null)
{
if (collection + Bus == -1)
{
return false;
}
}
}
_busStorages.Add(record[0], collection);
curLine = sr.ReadLine();
}
}
return true;
}
2023-11-25 14:55:27 +04:00
}
}