From 5066020b8a82f21979610ee6b5c7f4376c6aa5df Mon Sep 17 00:00:00 2001 From: Evgehil Date: Mon, 15 Apr 2024 09:50:58 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=BE=D0=B9=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 142 ++++++++---------- .../Project_airbus/FormAirplanConfig.cs | 14 +- 2 files changed, 68 insertions(+), 88 deletions(-) diff --git a/Project_airbus/Project_airbus/CollectionGenericObjects/StorageCollection.cs b/Project_airbus/Project_airbus/CollectionGenericObjects/StorageCollection.cs index 807da31..fa1915b 100644 --- a/Project_airbus/Project_airbus/CollectionGenericObjects/StorageCollection.cs +++ b/Project_airbus/Project_airbus/CollectionGenericObjects/StorageCollection.cs @@ -85,57 +85,52 @@ public class StorageCollection } /// - /// Сохранение информации по автомобилям в хранилище в файл - /// - /// Путь и имя файла - /// true - сохранение прошло успешно, false - ошибка при сохранении данных - public bool SaveData(string filename) + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) { if (_storages.Count == 0) { return false; } - if (File.Exists(filename)) { 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)) + StringBuilder sb = new(); + sb.Append(Environment.NewLine); + // не сохраняем пустые коллекции + if (value.Value.Count == 0) { continue; } - - sb.Append(data); - sb.Append(_separatorItems); + 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)) + { + continue; + } + sb.Append(data); + sb.Append(_separatorItems); + } + writer.Write(sb); } - } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); - fs.Write(info, 0, info.Length); + } return true; } @@ -150,63 +145,48 @@ public class StorageCollection { return false; } - - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader fs = 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) - { - 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) + string str = fs.ReadLine(); + 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?.CreateDrawingAirplan() is T airplan) + return false; + } + _storages.Clear(); + string strs = ""; + while ((strs = fs.ReadLine()) != null) + { + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) { - if (collection.Insert(airplan) == -1) + 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?. CreateDrawingAirplan() 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; } /// diff --git a/Project_airbus/Project_airbus/FormAirplanConfig.cs b/Project_airbus/Project_airbus/FormAirplanConfig.cs index 9e80d4e..bc3ea35 100644 --- a/Project_airbus/Project_airbus/FormAirplanConfig.cs +++ b/Project_airbus/Project_airbus/FormAirplanConfig.cs @@ -69,10 +69,10 @@ public partial class FormAirplanConfig : Form } /// - /// Проверка получаемой информации (ее типа на соответствие требуемому) + /// Проверка получаемой информации(ее типа на соответствие требуемому) /// - /// - /// + /// + /// < param name="e"> private void PanelObject_DragEnter(object sender, DragEventArgs e) { e.Effect = e.Data?.GetDataPresent(DataFormats.Text) ?? false ? DragDropEffects.Copy : DragDropEffects.None; @@ -81,8 +81,8 @@ public partial class FormAirplanConfig : Form /// /// Действия при приеме перетаскиваемой информации /// - /// - /// + /// + /// < param name="e"> private void PanelObject_DragDrop(object sender, DragEventArgs e) { switch (e.Data?.GetData(DataFormats.Text)?.ToString()) @@ -102,8 +102,8 @@ public partial class FormAirplanConfig : Form /// /// Передаем информацию при нажатии на Panel /// - /// - /// + /// + /// < param name="e"> private void Panel_MouseDown(object? sender, MouseEventArgs e) { (sender as Control)?.DoDragDrop((sender as Control)?.BackColor, DragDropEffects.Move | DragDropEffects.Copy);