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