From ccd63daf043436bd917761a15cc674f42b2ce946 Mon Sep 17 00:00:00 2001 From: Geo7312 Date: Sun, 12 May 2024 17:15:20 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 135 ++++++++---------- .../Drawnings/DrawningTrolleyB.cs | 5 +- .../Drawnings/DrawningTrolleybus.cs | 6 +- 3 files changed, 69 insertions(+), 77 deletions(-) diff --git a/ProjectTrolleybus/ProjectTrolleybus/CollectionGenericObjects/StorageCollection.cs b/ProjectTrolleybus/ProjectTrolleybus/CollectionGenericObjects/StorageCollection.cs index 46403dd..c96f572 100644 --- a/ProjectTrolleybus/ProjectTrolleybus/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectTrolleybus/ProjectTrolleybus/CollectionGenericObjects/StorageCollection.cs @@ -105,41 +105,39 @@ public class StorageCollection File.Delete(filename); } - StringBuilder sb = new(); - sb.Append(_collectionKey); - foreach (KeyValuePair> value in _storages) + using (StreamWriter writer = new StreamWriter(filename)) { - sb.Append(Environment.NewLine); - // не сохраняем пустые коллекции - if (value.Value.Count == 0) + writer.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) { - continue; - } - - 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)) + writer.Write(Environment.NewLine); + // не сохраняем пустые коллекции + if (value.Value.Count == 0) { continue; } - sb.Append(data); - sb.Append(_separatorItems); + writer.Write(value.Key); + 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; } @@ -155,62 +153,55 @@ public class StorageCollection return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader reader = 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 str = reader.ReadLine(); - 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? collection = StorageCollection.CreateCollection(collectionType); - if (collection == null) + if (str == null || str.Length == 0) { return false; } - collection.MaxCount = Convert.ToInt32(record[2]); - - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + if (!str.StartsWith(_collectionKey)) { - if (elem?.CreateDrawningTrolleyB() is T trolleyB) - { - if (collection.Insert(trolleyB) == -1) - { - return false; - } - } + //если нет такой записи, то это не те данные + return false; } - _storages.Add(record[0], collection); - } + _storages.Clear(); + string strs = ""; + while ((strs = reader.ReadLine()) != null) + { + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) + { + continue; + } + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.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?.CreateDrawningTrolleyB() is T trolleyB) + { + if (collection.Insert(trolleyB) == -1) + { + return false; + } + } + } + + _storages.Add(record[0], collection); + } + } return true; } diff --git a/ProjectTrolleybus/ProjectTrolleybus/Drawnings/DrawningTrolleyB.cs b/ProjectTrolleybus/ProjectTrolleybus/Drawnings/DrawningTrolleyB.cs index 00f08a0..be7d1d0 100644 --- a/ProjectTrolleybus/ProjectTrolleybus/Drawnings/DrawningTrolleyB.cs +++ b/ProjectTrolleybus/ProjectTrolleybus/Drawnings/DrawningTrolleyB.cs @@ -1,4 +1,5 @@ using ProjectTrolleybus.Entities; +using System.Security.Policy; namespace ProjectTrolleybus.Drawnings; @@ -97,9 +98,9 @@ public class DrawningTrolleyB /// Конструктор, принимающий объект EntityTrolleyB в качестве параметра /// /// - public DrawningTrolleyB(EntityTrolleyB entityTrolleyB) + public DrawningTrolleyB(EntityTrolleyB bull) : this() { - EntityTrolleyB = entityTrolleyB; + EntityTrolleyB = new EntityTrolleyB(bull.Speed, bull.Weight, bull.BodyColor); } /// diff --git a/ProjectTrolleybus/ProjectTrolleybus/Drawnings/DrawningTrolleybus.cs b/ProjectTrolleybus/ProjectTrolleybus/Drawnings/DrawningTrolleybus.cs index d845a61..595f30d 100644 --- a/ProjectTrolleybus/ProjectTrolleybus/Drawnings/DrawningTrolleybus.cs +++ b/ProjectTrolleybus/ProjectTrolleybus/Drawnings/DrawningTrolleybus.cs @@ -25,10 +25,10 @@ public class DrawningTrolleybus : DrawningTrolleyB /// /// Конструктор, принимающий объект EntityTrolleyB в качестве параметра /// - /// - public DrawningTrolleybus(EntityTrolleyB entityTrolleyB) + /// + public DrawningTrolleybus(EntityTrolleybus exc) : base(150, 80) { - EntityTrolleyB = entityTrolleyB; + EntityTrolleyB = new EntityTrolleybus(exc.Speed, exc.Weight, exc.BodyColor, exc.AdditionalColor, exc.BatteryCompartment, exc.Horns); } public override void DrawTransport(Graphics g)