From 0b68380b1e24fc4354ea079ea80012cf7ef073a7 Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sun, 14 Apr 2024 11:51:59 +0400 Subject: [PATCH 1/8] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B2=20?= =?UTF-8?q?Entities?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entities/EntityAirplane.cs | 22 +++++++++++++++++ .../Entities/EntityAirplaneWithRadar.cs | 24 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs index 3e6fe12..e2732d9 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs @@ -46,5 +46,27 @@ { BodyColor = bodyColor; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityAirplane), Speed.ToString(), Weight.ToString(), BodyColor.Name }; + } + + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityAirplane? CreateEntityAirplane(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityAirplane)) + return null; + + return new EntityAirplane(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplaneWithRadar.cs index f73d974..03085e9 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplaneWithRadar.cs @@ -44,5 +44,29 @@ { AdditionalColor = additionalColor; } + + /// + /// Переопределение метода создания объекта из массива строк + /// + /// + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntityAirplaneWithRadar), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, Wheels.ToString(), Radar.ToString() }; + } + + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityAirplaneWithRadar? CreateEntityAirplaneWithRadar(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntityAirplaneWithRadar)) + return null; + + return new EntityAirplaneWithRadar(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), + Color.FromName(strs[3]), Color.FromName(strs[4]), + Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } } } -- 2.25.1 From 336946ecfebd784e2c4ef4edbd6d7829ce7356ca Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sun, 14 Apr 2024 12:12:24 +0400 Subject: [PATCH 2/8] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Extentuon=20?= =?UTF-8?q?=D0=B8=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=BA=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BC=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawnings/DrawingAirplaneWithRadar.cs | 12 +++- .../Drawnings/DrawningAirplane.cs | 9 +++ .../Drawnings/ExtentionDrawningAirplane.cs | 55 +++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/ExtentionDrawningAirplane.cs diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs index 8ba2e38..a4188e6 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs @@ -6,7 +6,7 @@ namespace ProjectAirplaneWithRadar.Drawnings /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// public class DrawingAirplaneWithRadar : DrawningAirplane - { + { /// /// Инициализация свойств /// @@ -21,6 +21,16 @@ namespace ProjectAirplaneWithRadar.Drawnings EntityAirplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, additionalColor, wheels, radar); } + /// + /// Конструктор для метода создания объекта из строки (ExtentionDrawningAirplane) + /// + /// + public DrawingAirplaneWithRadar(EntityAirplane? airplane) : base(airplane) + { + if (airplane != null) + EntityAirplane = airplane; + } + /// /// Прорисовка объекта /// diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs index 63981d4..c2e33b6 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs @@ -83,6 +83,15 @@ namespace ProjectAirplaneWithRadar.Drawnings EntityAirplane = new EntityAirplane(speed, weight, bodyColor); } + /// + /// Конструктор для метода создания объекта из строки (ExtentionDrawningAirplane) + /// + /// + public DrawningAirplane(EntityAirplane? airplane) : this() + { + EntityAirplane = airplane; + } + /// /// Конструктор для наследников /// diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/ExtentionDrawningAirplane.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/ExtentionDrawningAirplane.cs new file mode 100644 index 0000000..b23ed4b --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/ExtentionDrawningAirplane.cs @@ -0,0 +1,55 @@ +using ProjectAirplaneWithRadar.Entities; + +namespace ProjectAirplaneWithRadar.Drawnings +{ + public static class ExtentionDrawningAirplane + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separatorForObject = ":"; + + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Объект + public static DrawningAirplane? CreateDrawningAirplane(this string info) + { + string[] strs = info.Split(_separatorForObject); + EntityAirplane? airplane = EntityAirplaneWithRadar.CreateEntityAirplaneWithRadar(strs); + + if (airplane != null) + { + return new DrawingAirplaneWithRadar(airplane); + } + + airplane = EntityAirplane.CreateEntityAirplane(strs); + + if (airplane != null) + { + return new DrawningAirplane(airplane); + } + + return null; + } + + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Строка с данными по объекту + public static string GetDataForSave(this DrawningAirplane drawningAirplane) + { + string[]? array = drawningAirplane?.EntityAirplane?.GetStringRepresentation(); + + if (array == null) + { + return string.Empty; + } + + return string.Join(_separatorForObject, array); + } + + } +} -- 2.25.1 From 75ec5da6bd3c741cbff522bcb38b408997839dae Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sun, 14 Apr 2024 12:18:57 +0400 Subject: [PATCH 3/8] =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=88=D0=B8=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81-=D0=BA=D0=BE=D0=BB=D0=BB=D0=B5=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ICollectionGenericObjects.cs | 12 ++++++++++++ .../CollectionGenericObjects/ListGenericObjects.cs | 13 ++++++++++++- .../MassiveGenericObjects.cs | 13 ++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs index d2f6a7f..d0b1a69 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -47,5 +47,17 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// Позиция /// Объект T? Get(int position); + + /// + /// Получение типа коллекции + /// + CollectionType GetCollectionType { get; } + + /// + /// Получение объектов коллекции по одному + /// + /// Поэлементый вывод элементов коллекции + IEnumerable GetItems(); + } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs index 9e05461..5f75759 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,5 @@ -namespace ProjectAirplaneWithRadar.CollectionGenericObjects + +namespace ProjectAirplaneWithRadar.CollectionGenericObjects { /// /// Параметризованный набор объектов @@ -21,6 +22,8 @@ public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public CollectionType GetCollectionType => CollectionType.List; + /// /// Конструктор /// @@ -64,5 +67,13 @@ _collection.RemoveAt(position); return temp; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Count; ++i) + { + yield return _collection[i]; + } + } } } diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs index f15d0de..dd5804a 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ -namespace ProjectAirplaneWithRadar.CollectionGenericObjects + +namespace ProjectAirplaneWithRadar.CollectionGenericObjects { /// /// Параметризованный набор объектов @@ -32,6 +33,8 @@ } } + public CollectionType GetCollectionType => CollectionType.Massive; + /// /// Конструктор /// @@ -110,5 +113,13 @@ _collection[position] = null; return temp; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; ++i) + { + yield return _collection[i]; + } + } } } \ No newline at end of file -- 2.25.1 From ac10e0d6e7cf49957f59e2491a2c5c90cf5467bd Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sun, 14 Apr 2024 12:25:50 +0400 Subject: [PATCH 4/8] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20SetMaxCount=20=D0=BD=D0=B0=20MaxCount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ICollectionGenericObjects.cs | 2 +- .../CollectionGenericObjects/ListGenericObjects.cs | 14 +++++++++++++- .../MassiveGenericObjects.cs | 7 ++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs index d0b1a69..be6574b 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -17,7 +17,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// /// Установка максимального количества элементов /// - int SetMaxCount { set; } + int MaxCount { get; set; } /// /// Добавление объекта в коллекцию diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs index 5f75759..3313fb3 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs @@ -20,7 +20,19 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int MaxCount { + set + { + if (value > 0) + { + _maxCount = value; + } + } + get + { + return _maxCount; + } + } public CollectionType GetCollectionType => CollectionType.List; diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs index dd5804a..6b87964 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs @@ -15,7 +15,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects public int Count => _collection.Length; - public int SetMaxCount + public int MaxCount { set { @@ -31,6 +31,11 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects } } } + + get + { + return _collection.Length; + } } public CollectionType GetCollectionType => CollectionType.Massive; -- 2.25.1 From 179f5b9b943818025526b48fbfcf2af60f539836 Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sun, 14 Apr 2024 12:27:49 +0400 Subject: [PATCH 5/8] =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BD=D0=B0=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B0=D0=BC=D0=B8=20get=20=D0=B8=20set?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionGenericObjects/ListGenericObjects.cs | 13 +++++++------ .../MassiveGenericObjects.cs | 12 ++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs index 3313fb3..8463540 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs @@ -20,18 +20,19 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects public int Count => _collection.Count; - public int MaxCount { + public int MaxCount { + get + { + return _maxCount; + } + set { if (value > 0) { _maxCount = value; } - } - get - { - return _maxCount; - } + } } public CollectionType GetCollectionType => CollectionType.List; diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs index 6b87964..0f4dfab 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs @@ -17,6 +17,11 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects public int MaxCount { + get + { + return _collection.Length; + } + set { if (value > 0) @@ -30,12 +35,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects _collection = new T?[value]; } } - } - - get - { - return _collection.Length; - } + } } public CollectionType GetCollectionType => CollectionType.Massive; -- 2.25.1 From c670d03f08c355f393b1eda0a6c619c0f39a61bb Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sun, 14 Apr 2024 12:53:36 +0400 Subject: [PATCH 6/8] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20?= =?UTF-8?q?=D1=81=D0=BE=20StorageCollection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 154 +++++++++++++++++- 1 file changed, 152 insertions(+), 2 deletions(-) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs index 75b6233..2935ca3 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs @@ -1,11 +1,14 @@ -namespace ProjectAirplaneWithRadar.CollectionGenericObjects +using System.Text; +using ProjectAirplaneWithRadar.Drawnings; + +namespace ProjectAirplaneWithRadar.CollectionGenericObjects { /// /// Класс-хранилище коллекций /// /// public class StorageCollection - where T : class + where T : DrawningAirplane { /// /// Словарь (хранилище) с коллекциями @@ -17,6 +20,21 @@ /// public List Keys => _storages.Keys.ToList(); + /// + /// Ключевое слово, с которого должен начинаться файл + /// + private readonly string _collectionKey = "CollectionsStorage"; + + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private readonly string _separatorForKeyValue = "|"; + + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly string _separatorItems = ";"; + /// /// Конструктор /// @@ -73,5 +91,137 @@ return _storages[name]; } } + + /// + /// Сохранение информации по самолетам в хранилище в файл + /// + /// Путь и имя файла + /// 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) + { + sb.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); + + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) + { + continue; + } + + sb.Append(data); + sb.Append(_separatorItems); + } + } + + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); + fs.Write(info, 0, info.Length); + return true; + } + + /// + /// Загрузка информации по самолетам в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + 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)) + { + 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) + { + return false; + } + + collection.MaxCount = Convert.ToInt32(record[2]); + + string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + if (elem?.CreateDrawningAirplane() is T airplane) + { + if (collection.Insert(airplane) == -1) + return false; + } + } + + _storages.Add(record[0], collection); + } + + return true; + } + + /// + /// Создание коллекции по типу + /// + /// + /// + private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType) + { + return collectionType switch + { + CollectionType.Massive => new MassiveGenericObjects(), + CollectionType.List => new ListGenericObjects(), + _ => null, + }; + } } } -- 2.25.1 From e9e19bd2cd15f674ef6adbb605ed9302c35a0907 Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sun, 14 Apr 2024 13:10:36 +0400 Subject: [PATCH 7/8] =?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=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 2 +- .../FormAirplaneCollection.Designer.cs | 68 +++++++++++++++++-- .../FormAirplaneCollection.cs | 53 +++++++++++++-- .../FormAirplaneCollection.resx | 9 +++ 4 files changed, 120 insertions(+), 12 deletions(-) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs index 6019f3d..167cb08 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs @@ -48,7 +48,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } /// diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs index 44be092..79d7649 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs @@ -46,10 +46,17 @@ labelCollectionName = new Label(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + menuStrip = new MenuStrip(); + файлToolStripMenuItem = new ToolStripMenuItem(); + saveToolStripMenuItem = new ToolStripMenuItem(); + loadToolStripMenuItem = new ToolStripMenuItem(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); groupBoxTools.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + menuStrip.SuspendLayout(); SuspendLayout(); // // groupBoxTools @@ -59,9 +66,9 @@ groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(883, 0); + groupBoxTools.Location = new Point(883, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(206, 607); + groupBoxTools.Size = new Size(206, 583); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -75,7 +82,7 @@ panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 417); + panelCompanyTools.Location = new Point(3, 393); panelCompanyTools.Name = "panelCompanyTools"; panelCompanyTools.Size = new Size(200, 187); panelCompanyTools.TabIndex = 8; @@ -240,12 +247,52 @@ // pictureBox // pictureBox.Dock = DockStyle.Fill; - pictureBox.Location = new Point(0, 0); + pictureBox.Location = new Point(0, 24); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(883, 607); + pictureBox.Size = new Size(883, 583); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(1089, 24); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(48, 20); + файлToolStripMenuItem.Text = "Файл"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + saveToolStripMenuItem.Size = new Size(181, 22); + saveToolStripMenuItem.Text = "Сохранение"; + saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // loadToolStripMenuItem + // + loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; + loadToolStripMenuItem.Size = new Size(181, 22); + loadToolStripMenuItem.Text = "Загрузка"; + loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + openFileDialog.Filter = "txt file | *.txt"; + // // FormAirplaneCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -253,6 +300,8 @@ ClientSize = new Size(1089, 607); Controls.Add(pictureBox); Controls.Add(groupBoxTools); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormAirplaneCollection"; Text = "Коллекция самолетов"; groupBoxTools.ResumeLayout(false); @@ -261,7 +310,10 @@ panelStorage.ResumeLayout(false); panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -284,5 +336,11 @@ private ListBox listBoxCollection; private Button buttonCollectionAdd; private Panel panelCompanyTools; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs index bf30f73..6074f1f 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs @@ -35,7 +35,7 @@ namespace ProjectAirplaneWithRadar private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { panelCompanyTools.Enabled = false; - } + } /// /// Добавление самолета @@ -158,14 +158,14 @@ namespace ProjectAirplaneWithRadar /// private void ButtonCollectionAdd_Click(object sender, EventArgs e) { - if(string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked)) + if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } CollectionType collectionType = CollectionType.None; - if(radioButtonMassive.Checked) + if (radioButtonMassive.Checked) collectionType = CollectionType.Massive; else if (radioButtonList.Checked) collectionType = CollectionType.List; @@ -181,7 +181,7 @@ namespace ProjectAirplaneWithRadar /// private void ButtonCollectionDel_Click(object sender, EventArgs e) { - if(listBoxCollection.SelectedItems == null || listBoxCollection.SelectedIndex < 0) + if (listBoxCollection.SelectedItems == null || listBoxCollection.SelectedIndex < 0) { MessageBox.Show("Коллекция не выбрана"); return; @@ -204,7 +204,7 @@ namespace ProjectAirplaneWithRadar for (int i = 0; i < _storageCollection.Keys?.Count; ++i) { string? colName = _storageCollection.Keys?[i]; - if(!string.IsNullOrEmpty(colName)) + if (!string.IsNullOrEmpty(colName)) listBoxCollection.Items.Add(colName); } } @@ -221,7 +221,7 @@ namespace ProjectAirplaneWithRadar MessageBox.Show("Коллекция не выбрана"); return; } - + ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; if (collection == null) { @@ -239,5 +239,46 @@ namespace ProjectAirplaneWithRadar panelCompanyTools.Enabled = true; RefreshListBoxItems(); } + + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storageCollection.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + /// + /// Обработка нажатия "Загрузка" + /// + /// + /// + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) + { + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storageCollection.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + RefreshListBoxItems(); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.resx b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.resx index af32865..8b1dfa1 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.resx +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 126, 17 + + + 261, 17 + \ No newline at end of file -- 2.25.1 From 15565fd27be540908aed293a2a19d46dd23302dd Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Wed, 17 Apr 2024 17:05:21 +0400 Subject: [PATCH 8/8] =?UTF-8?q?=D0=A1=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B8=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D1=8C=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20StreamWriter=20=D0=B8?= =?UTF-8?q?=20StreamReader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 114 ++++++++---------- 1 file changed, 52 insertions(+), 62 deletions(-) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs index 2935ca3..81db8f9 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.IO; +using System.Text; using ProjectAirplaneWithRadar.Drawnings; namespace ProjectAirplaneWithRadar.CollectionGenericObjects @@ -103,24 +104,25 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects return false; if(File.Exists(filename)) - File.Delete(filename); + File.Delete(filename); - StringBuilder sb = new(); - - sb.Append(_collectionKey); - foreach(KeyValuePair> value in _storages) + using FileStream fs = new(filename, FileMode.Create); + using StreamWriter sw = new StreamWriter(fs); + sw.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) { - sb.Append(Environment.NewLine); - + sw.Write(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); + sw.Write(value.Key); + sw.Write(_separatorForKeyValue); + sw.Write(value.Value.GetCollectionType); + sw.Write(_separatorForKeyValue); + sw.Write(value.Value.MaxCount); + sw.Write(_separatorForKeyValue); foreach (T? item in value.Value.GetItems()) { @@ -130,14 +132,10 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects continue; } - sb.Append(data); - sb.Append(_separatorItems); + sw.Write(data); + sw.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; } @@ -153,59 +151,51 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects 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); - } - } + using StreamReader sr = new StreamReader(fs); - 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 = sr.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.Equals(_collectionKey)) { - if (elem?.CreateDrawningAirplane() is T airplane) - { - if (collection.Insert(airplane) == -1) - return false; - } + return false; } + _storages.Clear(); - _storages.Add(record[0], collection); + while (!sr.EndOfStream) + { + string[] record = sr.ReadLine().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?.CreateDrawningAirplane() is T airplane) + { + if (collection.Insert(airplane) == -1) + return false; + } + } + _storages.Add(record[0], collection); + } } - return true; } -- 2.25.1