From 84fb3e45a206b9f03b9e456939f9dfe071abec73 Mon Sep 17 00:00:00 2001 From: Aidar Date: Fri, 17 May 2024 23:14:57 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=966=20(=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D0=BE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ShipSharingService.cs | 6 +- .../StorageCollection.cs | 148 +++++++----------- 2 files changed, 58 insertions(+), 96 deletions(-) diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ShipSharingService.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ShipSharingService.cs index 2efdeea..016f957 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ShipSharingService.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ShipSharingService.cs @@ -35,11 +35,11 @@ public class ShipSharingService : AbstractCompany g.DrawLine(pen, x, y + _placeSizeHeight, x + _placeSizeWidth, y + _placeSizeHeight); g.DrawLine(pen, x + _placeSizeWidth, y + _placeSizeHeight, x + _placeSizeWidth, y); locCoord.Add(new Tuple(x, y)); - x += _placeSizeWidth + 2; + x += _placeSizeWidth; } countInRow = count; - x = 1; - y += _placeSizeHeight + 5; + x = 0; + y += _placeSizeHeight; countOfRows++; } } diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/StorageCollection.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/StorageCollection.cs index 1acf277..3aa3486 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/StorageCollection.cs @@ -1,9 +1,7 @@ using ProjectLiner.Drawnings; -using System.Text; namespace ProjectLiner.CollectionGenericObjects; - /// /// Класс-хранилище коллекций /// @@ -11,6 +9,16 @@ namespace ProjectLiner.CollectionGenericObjects; public class StorageCollection where T : DrawningShip { + /// + /// Словарь (хранилище) с коллекциями + /// + private Dictionary> _storages; + + /// + /// Возвращение списка названий коллекций + /// + public List Keys => _storages.Keys.ToList(); + /// /// Ключевое слово, с которого должен начинаться файл /// @@ -26,15 +34,7 @@ public class StorageCollection /// private readonly string _separatorItems = ";"; - /// - /// Словарь (хранилище) с коллекциями - /// - readonly Dictionary> _storages; - /// - /// Возвращение списка названий коллекций - /// - public List Keys => _storages.Keys.ToList(); /// /// Конструктор @@ -113,52 +113,32 @@ public class StorageCollection { 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.WriteLine(_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)) - { - continue; + writer.Write($"{value.Key}{_separatorForKeyValue}{value.Value.GetCollectionType}{_separatorForKeyValue}{value.Value.MaxCount}{_separatorForKeyValue}"); + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (!string.IsNullOrEmpty(data)) + { + writer.Write(data + _separatorItems); + } } - - sb.Append(data); - sb.Append(_separatorItems); + writer.WriteLine(); } } - - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); - fs.Write(info, 0, info.Length); return true; } - + /// /// Загрузка информации по кораблям в хранилище из файла /// @@ -171,68 +151,50 @@ public class StorageCollection return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader reader = new StreamReader(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 line = reader.ReadLine(); + if (line == null || !line.Equals(_collectionKey)) { return false; } - collection.MaxCount = Convert.ToInt32(record[2]); + _storages.Clear(); - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + while (line != null) { - if (elem?.CreateDrawningShip() is T ship) + string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) { - if (collection.Insert(ship) == -1) + line = reader.ReadLine(); + 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?.CreateDrawningShip() is T ship) { - return false; + if (collection.Insert(ship) == -1) + { + return false; + } } } + _storages.Add(record[0], collection); + line = reader.ReadLine(); } - - _storages.Add(record[0], collection); } - return true; }