Изменение методов в классе LocomotivesGenericStorage

This commit is contained in:
Андрей Байгулов 2023-11-27 00:33:49 +04:00
parent a1d6167007
commit fb1f7abf5c

View File

@ -1,5 +1,4 @@
using ProjectElectricLocomotive.DrawingObjects; using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.Generics;
using ProjectElectricLocomotive.MovementStrategy; using ProjectElectricLocomotive.MovementStrategy;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -7,7 +6,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ProjectElectricLocomotive namespace ProjectElectricLocomotive.Generics
{ {
internal class LocomotivesGenericStorage internal class LocomotivesGenericStorage
{ {
@ -103,8 +102,7 @@ namespace ProjectElectricLocomotive
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string, foreach (KeyValuePair<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record in _locomotivesStorage)
LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record in _locomotivesStorage)
{ {
StringBuilder records = new(); StringBuilder records = new();
foreach (DrawingLocomotive? elem in record.Value.GetLocomotives) foreach (DrawingLocomotive? elem in record.Value.GetLocomotives)
@ -117,10 +115,11 @@ namespace ProjectElectricLocomotive
{ {
return false; return false;
} }
using FileStream fs = new(filename, FileMode.Create); using StreamWriter fs = new StreamWriter(filename);
byte[] info = new {
UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); fs.WriteLine($"LocomotiveStorage{Environment.NewLine}");
fs.Write(info, 0, info.Length); fs.WriteLine(data);
}
return true; return true;
} }
/// <summary> /// <summary>
@ -134,56 +133,58 @@ namespace ProjectElectricLocomotive
{ {
return false; return false;
} }
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open)) using (StreamReader fs = File.OpenText(filename))
{ {
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true); string str = fs.ReadLine();
while (fs.Read(b, 0, b.Length) > 0)
if (str == null || str.Length == 0)
{ {
bufferTextFromFile += temp.GetString(b); return false;
} }
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, if (!str.StartsWith("LocomotiveStorage"))
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("CarStorage"))
{
//если нет такой записи, то это не те данные
return false;
}
_locomotivesStorage.Clear();
foreach (string data in strs)
{
string[] record = data.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?.CreateDrawningCar(_separatorForObject, _pictureWidth, _pictureHeight); //если нет такой записи, то это не те данные
if (loco != null) return false;
}
_locomotivesStorage.Clear();
string strs = "";
while ((strs = fs.ReadLine()) != null)
{
if (strs == null)
{ {
if (collection + loco == -1) return false;
}
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 thing in set)
{
DrawingLocomotive? loco = thing?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight);
if (loco != null)
{ {
return false; if ((collection + loco) == -1)
{
return false;
}
} }
} }
_locomotivesStorage.Add(record[0], collection);
} }
_locomotivesStorage.Add(record[0], collection); return true;
} }
return true;
} }
} }
} }