Готовая 6ая лаба

This commit is contained in:
bulatova_karina 2023-11-22 10:56:27 +03:00
parent a1f41941b2
commit 1275e0791a

View File

@ -105,8 +105,7 @@ namespace WarmlyShip.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
ShipsGenericCollection<DrawingWarmlyShip, DrawingObjectShip>> record in _shipStorages)
foreach (KeyValuePair<string, ShipsGenericCollection<DrawingWarmlyShip, DrawingObjectShip>> record in _shipStorages)
{
StringBuilder records = new();
foreach (DrawingWarmlyShip? elem in record.Value.GetShips)
@ -119,10 +118,10 @@ namespace WarmlyShip.Generics
{
return false;
}
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new
UTF8Encoding(true).GetBytes($"ShipStorage{Environment.NewLine}{data}");
fs.Write(info, 0, info.Length);
using (StreamWriter writer = new StreamWriter(filename))
{
writer.Write($"ShipStorage{Environment.NewLine}{data}");
}
return true;
}
/// <summary>
@ -137,40 +136,35 @@ namespace WarmlyShip.Generics
{
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)
{
bufferTextFromFile += temp.GetString(b);
}
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
string str = fs.ReadLine();
if (str == null || str.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("ShipStorage"))
if (!str.StartsWith("ShipStorage"))
{
//если нет такой записи, то это не те данные
return false;
}
_shipStorages.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)
{
continue;
}
ShipsGenericCollection<DrawingWarmlyShip, DrawingObjectShip>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
ShipsGenericCollection<DrawingWarmlyShip, DrawingObjectShip> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingWarmlyShip? ship = elem?.CreateDrawingShip(_separatorForObject, _pictureWidth, _pictureHeight);
@ -188,3 +182,4 @@ namespace WarmlyShip.Generics
}
}
}
}