Лабораторная работа №6

This commit is contained in:
xom9kxom9k 2024-04-19 19:01:19 +04:00
parent a868c596e2
commit 53d3354462

View File

@ -87,7 +87,7 @@ public class StorageCollection<T>
/// Сохранение информации по автомобилям в хранилище в файл /// Сохранение информации по автомобилям в хранилище в файл
/// </summary> /// </summary>
/// <param name="filename">Путь и имя файла</param> /// <param name="filename">Путь и имя файла</param>
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns> /// <returns></returns>
public bool SaveData(string filename) public bool SaveData(string filename)
{ {
if (_storages.Count == 0) if (_storages.Count == 0)
@ -102,22 +102,24 @@ public class StorageCollection<T>
StringBuilder sb = new(); StringBuilder sb = new();
sb.Append(_collectionKey); using (StreamWriter writer = new(filename))
{
writer.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages) foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{ {
sb.Append(Environment.NewLine); writer.Write(Environment.NewLine);
// не сохраняем пустые коллекции // не сохраняем пустые коллекции
if (value.Value.Count == 0) if (value.Value.Count == 0)
{ {
continue; continue;
} }
sb.Append(value.Key); writer.Write(value.Key);
sb.Append(_separatorForKeyValue); writer.Write(_separatorForKeyValue);
sb.Append(value.Value.GetCollectionType); writer.Write(value.Value.GetCollectionType);
sb.Append(_separatorForKeyValue); writer.Write(_separatorForKeyValue);
sb.Append(value.Value.MaxCount); writer.Write(value.Value.MaxCount);
sb.Append(_separatorForKeyValue); writer.Write(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems()) foreach (T? item in value.Value.GetItems())
{ {
@ -127,14 +129,11 @@ public class StorageCollection<T>
continue; continue;
} }
sb.Append(data); writer.Write(data);
sb.Append(_separatorItems); writer.Write(_separatorItems);
}
} }
} }
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
fs.Write(info, 0, info.Length);
return true; return true;
} }
@ -149,63 +148,49 @@ public class StorageCollection<T>
{ {
return false; return false;
} }
using (StreamReader reader = new(filename))
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
{ {
byte[] b = new byte[fs.Length]; string line = reader.ReadLine();
UTF8Encoding temp = new(true); if (line == null || line.Length == 0)
while (fs.Read(b, 0, b.Length) > 0)
{
bufferTextFromFile += temp.GetString(b);
}
}
string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{ {
return false; return false;
} }
if (!line.Equals(_collectionKey))
if (!strs[0].Equals(_collectionKey))
{ {
//если нет такой записи, то это не те данные //если нет такой записи, то это не те данные
return false; return false;
} }
_storages.Clear(); _storages.Clear();
foreach (string data in strs) while ((line = reader.ReadLine()) != null)
{ {
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); string[] record = line.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4) if (record.Length != 4)
{ {
continue; continue;
} }
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType); ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null) if (collection == null)
{ {
return false; return false;
} }
collection.MaxCount = Convert.ToInt32(record[2]); collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems,
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set) foreach (string elem in set)
{ {
if (elem?.CreateDrawningArmoredCar() is T car) if (elem?.CreateDrawningArmoredCar() is T truck)
{ {
if (collection.Insert(car)== -1) if (collection.Insert(truck) == -1)
{ {
return false; return false;
} }
} }
} }
_storages.Add(record[0], collection); _storages.Add(record[0], collection);
} }
}
return true; return true;
} }