Лабораторная работа №6
This commit is contained in:
parent
2d99147d76
commit
7af4894154
@ -106,37 +106,36 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
File.Delete(filename);
|
File.Delete(filename);
|
||||||
}
|
}
|
||||||
StringBuilder sb = new();
|
using (StreamWriter writer = new(filename))
|
||||||
sb.Append(_collectionKey);
|
|
||||||
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
|
||||||
{
|
{
|
||||||
sb.Append(Environment.NewLine);
|
writer.Write(_collectionKey);
|
||||||
// не сохраняем пустые коллекции
|
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
||||||
if (value.Value.Count == 0)
|
|
||||||
{
|
{
|
||||||
continue;
|
writer.Write(Environment.NewLine);
|
||||||
}
|
// не сохраняем пустые коллекции
|
||||||
sb.Append(value.Key);
|
if (value.Value.Count == 0)
|
||||||
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;
|
||||||
}
|
}
|
||||||
sb.Append(data);
|
writer.Write(value.Key);
|
||||||
sb.Append(_separatorItems);
|
writer.Write(_separatorForKeyValue);
|
||||||
}
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,59 +150,53 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
string bufferTextFromFile = "";
|
//using (FileStream fs = new(filename, FileMode.Open))
|
||||||
using (FileStream fs = new(filename, FileMode.Open))
|
//{
|
||||||
|
|
||||||
|
//}
|
||||||
|
using (StreamReader reader = new(filename))
|
||||||
{
|
{
|
||||||
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;
|
|
||||||
}
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
if (!line.Equals(_collectionKey))
|
||||||
string[] set = record[3].Split(_separatorItems,
|
|
||||||
StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
foreach (string elem in set)
|
|
||||||
{
|
{
|
||||||
if (elem?.CreateDrawningCar() is T car)
|
//если нет такой записи, то это не те данные
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_storages.Clear();
|
||||||
|
while ((line = reader.ReadLine()) != null)
|
||||||
|
{
|
||||||
|
string[] record = line.Split(_separatorForKeyValue,
|
||||||
|
StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (record.Length != 4)
|
||||||
{
|
{
|
||||||
if (collection.Insert(car) == -1)
|
continue;
|
||||||
|
}
|
||||||
|
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 truck)
|
||||||
{
|
{
|
||||||
return false;
|
if (collection.Insert(truck) == -1)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_storages.Add(record[0], collection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_storages.Add(record[0], collection);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user