Лабораторная работа №6 сделана
This commit is contained in:
parent
e1cc1e40ad
commit
bfce6b7fbd
@ -1,5 +1,4 @@
|
||||
using ProjectMonorail.Scripts.Monorail.Drawnings;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects
|
||||
@ -115,23 +114,26 @@ namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
StringBuilder sb = new();
|
||||
sb.Append(_collectionKey);
|
||||
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in
|
||||
_storages)
|
||||
using (StreamWriter writer = new StreamWriter(filename))
|
||||
{
|
||||
sb.Append(Environment.NewLine);
|
||||
// не сохраняем пустые коллекции
|
||||
writer.Write(_collectionKey);
|
||||
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append(Environment.NewLine);
|
||||
|
||||
if (value.Value.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
sb.Append(value.Key);
|
||||
sb.Append(_separatorForKeyValue);
|
||||
sb.Append(value.Value.GetCollectionType);
|
||||
sb.Append(_separatorForKeyValue);
|
||||
sb.Append(value.Value.MaxCount);
|
||||
sb.Append(_separatorForKeyValue);
|
||||
|
||||
stringBuilder.Append(value.Key);
|
||||
stringBuilder.Append(_separatorForKeyValue);
|
||||
stringBuilder.Append(value.Value.GetCollectionType);
|
||||
stringBuilder.Append(_separatorForKeyValue);
|
||||
stringBuilder.Append(value.Value.MaxCount);
|
||||
stringBuilder.Append(_separatorForKeyValue);
|
||||
|
||||
foreach (T? item in value.Value.GetItems())
|
||||
{
|
||||
string data = item?.GetDataForSave() ?? string.Empty;
|
||||
@ -139,15 +141,14 @@ namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects
|
||||
{
|
||||
continue;
|
||||
}
|
||||
sb.Append(data);
|
||||
sb.Append(_separatorItems);
|
||||
stringBuilder.Append(data);
|
||||
stringBuilder.Append(_separatorItems);
|
||||
}
|
||||
writer.Write(stringBuilder);
|
||||
}
|
||||
using FileStream fs = new(filename, FileMode.Create);
|
||||
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
|
||||
fs.Write(info, 0, info.Length);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Загрузка информации по автомобилям в хранилище из файла
|
||||
@ -160,53 +161,43 @@ namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string bufferTextFromFile = "";
|
||||
using (FileStream fs = new(filename, FileMode.Open))
|
||||
|
||||
using (StreamReader streamRader = 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);
|
||||
}
|
||||
}
|
||||
string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (strs == null || strs.Length == 0)
|
||||
string inputString = streamRader.ReadLine();
|
||||
|
||||
if (inputString == null || inputString.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!strs[0].Equals(_collectionKey))
|
||||
if (!inputString.StartsWith(_collectionKey))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
return false;
|
||||
}
|
||||
|
||||
_storages.Clear();
|
||||
foreach (string data in strs)
|
||||
string strs = "";
|
||||
while ((strs = streamRader.ReadLine()) != null)
|
||||
{
|
||||
string[] record = data.Split(_separatorForKeyValue,
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] record = strs.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);
|
||||
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);
|
||||
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
if (elem?.CreateDrawingMonorail() is T monorail)
|
||||
if (elem?.CreateDrawingMonorail() is T ship)
|
||||
{
|
||||
if (collection.Insert(monorail) == -1)
|
||||
if (collection.Insert(ship) == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -216,6 +207,7 @@ namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static ICollectionGenericObjects<T>? CreateCollection(CollectionType collectionType)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user