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

This commit is contained in:
DjonniStorm 2024-04-18 17:28:18 +04:00
parent df70c5840f
commit 0ae62e1d72
2 changed files with 62 additions and 68 deletions

View File

@ -106,22 +106,23 @@ public class StorageCollection<T>
{ {
File.Delete(filename); File.Delete(filename);
} }
StringBuilder sb = new(); using (StreamWriter writer = new StreamWriter(filename))
sb.Append(_collectionKey); {
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())
{ {
@ -130,13 +131,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;
} }
@ -151,52 +150,38 @@ public class StorageCollection<T>
{ {
return false; return false;
} }
string bufferTextFromFile = ""; using (StreamReader reader = File.OpenText(filename))
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 (!strs[0].Equals(_collectionKey)) if (!line.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, string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4) if (record.Length != 4)
{ {
continue; continue;
} }
CollectionType collectionType = CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
(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?.CreateDrawningCar() is T car) if (elem?. CreateDrawningCar() is T ship)
{ {
if (collection.Insert(car) == -1) if (collection.Insert(ship) == -1)
{ {
return false; return false;
} }
@ -206,6 +191,7 @@ public class StorageCollection<T>
} }
return true; return true;
} }
}
/// <summary> /// <summary>
/// Создание коллекции по типу /// Создание коллекции по типу

View File

@ -267,7 +267,15 @@ 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);
}
} }
} }
} }