Изменение методов в классе 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,46 +133,48 @@ 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);
}
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{ {
return false; return false;
} }
if (!strs[0].StartsWith("CarStorage"))
if (!str.StartsWith("LocomotiveStorage"))
{ {
//если нет такой записи, то это не те данные //если нет такой записи, то это не те данные
return false; return false;
} }
_locomotivesStorage.Clear(); _locomotivesStorage.Clear();
foreach (string data in strs) string strs = "";
while ((strs = fs.ReadLine()) != null)
{ {
string[] record = data.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries); if (strs == null)
{
return false;
}
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2) if (record.Length != 2)
{ {
continue; continue;
} }
LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> collection = new(_pictureWidth, _pictureHeight);
collection = new(_pictureWidth, _pictureHeight); string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
string[] set = record[1].Split(_separatorRecords, foreach (string thing in set)
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{ {
DrawingLocomotive? loco = elem?.CreateDrawningCar(_separatorForObject, _pictureWidth, _pictureHeight); DrawingLocomotive? loco = thing?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight);
if (loco != null) if (loco != null)
{ {
if (collection + loco == -1) if ((collection + loco) == -1)
{ {
return false; return false;
} }
@ -184,6 +185,6 @@ namespace ProjectElectricLocomotive
return true; return true;
} }
} }
}
} }