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