This commit is contained in:
Sergey Kozyrev 2023-11-15 11:34:08 +04:00
parent 8079e18f75
commit 3fb85c16dd
3 changed files with 39 additions and 44 deletions

View File

@ -68,11 +68,13 @@ namespace Lab.Generics
{ {
return false; return false;
} }
using FileStream fs = new(filename, FileMode.Create); using (StreamWriter writer = new StreamWriter(filename))
byte[] info = new {
UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); writer.WriteLine("CarStorage");
fs.Write(info, 0, info.Length); writer.Write(data.ToString());
return true; return true;
}
} }
public bool LoadData(string filename) public bool LoadData(string filename)
@ -81,52 +83,41 @@ namespace Lab.Generics
{ {
return false; return false;
} }
string bufferTextFromFile = ""; using (StreamReader reader = new StreamReader(filename))
using (FileStream fs = new(filename, FileMode.Open))
{ {
byte[] b = new byte[fs.Length]; string checker = reader.ReadLine();
UTF8Encoding temp = new(true); if (checker == null)
while (fs.Read(b, 0, b.Length) > 0) return false;
string strs = reader.ReadLine();
if (!checker.StartsWith("CarStorage"))
return false;
_carStorages.Clear();
foreach (string data in strs.Split(';'))
{ {
bufferTextFromFile += temp.GetString(b); string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
} if (record.Length != 2)
}
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;
}
_carStorages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
CarsGenericCollection<DrawTanker, DrawingObjectTanker> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawTanker? car =
elem?.CreateDrawTanker(_separatorForObject, _pictureWidth, _pictureHeight);
if (car != null)
{ {
if (!(collection + car)) continue;
}
CarsGenericCollection<DrawTanker, DrawingObjectTanker> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawTanker? car =
elem?.CreateDrawTanker(_separatorForObject, _pictureWidth, _pictureHeight);
if (car != null)
{ {
return false; if (!(collection + car))
{
return false;
}
} }
} }
_carStorages.Add(record[0], collection);
} }
_carStorages.Add(record[0], collection); return true;
} }
return true;
} }
} }
} }

View File

@ -38,12 +38,12 @@ namespace Lab
{ {
return string.Empty; return string.Empty;
} }
var str = $"{Tanker.Speed}{separatorForObject}{Tanker.Weight}{separatorForObject}{Tanker.BodyColor.Name}"; var str = $"{Tanker.Speed}{separatorForObject}{Tanker.Weight}{separatorForObject}{Tanker.BodyColor}";
if (Tanker is not GasolineTanker gasTanker) if (Tanker is not GasolineTanker gasTanker)
{ {
return str; return str;
} }
return $"{str}{separatorForObject}{gasTanker.AdditionalColor.Name}{separatorForObject}{gasTanker.BodyKit}{separatorForObject}{gasTanker.Wing}{separatorForObject}{gasTanker.SportLine}"; return $"{str}{separatorForObject}{gasTanker.AdditionalColor}{separatorForObject}{gasTanker.BodyKit}{separatorForObject}{gasTanker.Wing}{separatorForObject}{gasTanker.SportLine}";
} }
} }

View File

@ -49,12 +49,16 @@ namespace Lab.Generics
{ {
if (position < 0 || position > _maxCount) if (position < 0 || position > _maxCount)
return null; return null;
if (_places.Count <= position)
return null;
return _places[position]; return _places[position];
} }
set set
{ {
if (position < 0 || position > _maxCount) if (position < 0 || position > _maxCount)
return; return;
if (_places.Count <= position)
return;
_places[position] = value; _places[position] = value;
} }
} }