revert 0ae62e1d72702f5460dfd04addb75e42854c0602

revert Лабораторная работа №6
This commit is contained in:
chillya 2024-04-18 18:41:15 +04:00
parent 0ae62e1d72
commit 2d99147d76
2 changed files with 68 additions and 62 deletions

View File

@ -106,36 +106,37 @@ public class StorageCollection<T>
{ {
File.Delete(filename); File.Delete(filename);
} }
using (StreamWriter writer = new StreamWriter(filename)) StringBuilder sb = new();
sb.Append(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{ {
writer.Write(_collectionKey); sb.Append(Environment.NewLine);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages) // не сохраняем пустые коллекции
if (value.Value.Count == 0)
{ {
writer.Write(Environment.NewLine); continue;
// не сохраняем пустые коллекции }
if (value.Value.Count == 0) sb.Append(value.Key);
sb.Append(_separatorForKeyValue);
sb.Append(value.Value.GetCollectionType);
sb.Append(_separatorForKeyValue);
sb.Append(value.Value.MaxCount);
sb.Append(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{ {
continue; continue;
} }
writer.Write(value.Key); sb.Append(data);
writer.Write(_separatorForKeyValue); sb.Append(_separatorItems);
writer.Write(value.Value.GetCollectionType);
writer.Write(_separatorForKeyValue);
writer.Write(value.Value.MaxCount);
writer.Write(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{
continue;
}
writer.Write(data);
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;
} }
@ -150,47 +151,60 @@ public class StorageCollection<T>
{ {
return false; return false;
} }
using (StreamReader reader = File.OpenText(filename)) string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
{ {
string line = reader.ReadLine(); byte[] b = new byte[fs.Length];
if (line == null || line.Length == 0) UTF8Encoding temp = new(true);
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;
}
if (!strs[0].Equals(_collectionKey))
{
//если нет такой записи, то это не те данные
return false;
}
_storages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{
continue;
}
CollectionType collectionType =
(CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection =
StorageCollection<T>.CreateCollection(collectionType);
if (collection == null)
{ {
return false; return false;
} }
if (!line.Equals(_collectionKey)) collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{ {
return false; if (elem?.CreateDrawningCar() is T car)
}
_storages.Clear();
while ((line = reader.ReadLine()) != null)
{
string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{ {
continue; if (collection.Insert(car) == -1)
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null)
{
return false;
}
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
if (elem?. CreateDrawningCar() is T ship)
{ {
if (collection.Insert(ship) == -1) return false;
{
return false;
}
} }
} }
_storages.Add(record[0], collection);
} }
return true; _storages.Add(record[0], collection);
} }
return true;
} }
/// <summary> /// <summary>

View File

@ -267,15 +267,7 @@ public partial class FormCleaningCarCollection : Form
{ {
if (openFileDialog.ShowDialog() == DialogResult.OK) if (openFileDialog.ShowDialog() == DialogResult.OK)
{ {
if (_storageCollection.LoadData(openFileDialog.FileName))
{
MessageBox.Show("Загрузка прошла успешно", "Реузльтат", MessageBoxButtons.OK, MessageBoxIcon.Information);
RerfreshListBoxItems();
}
else
{
MessageBox.Show("Не удалось загрузить", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
} }
} }