From 2fd36d47850ebec42c86875b23d86ba2b8a35b9c Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 29 Dec 2023 04:13:04 +0400 Subject: [PATCH] 50% --- DumpTruck/DumpTruck/DumpTruck.csproj | 3 +- DumpTruck/DumpTruck/ExtentionDrawningTruck.cs | 67 ++++++++++++++ .../DumpTruck/FormTruckCollection.Designer.cs | 18 ++-- .../DumpTruck/FormTruckConfig.Designer.cs | 2 +- DumpTruck/DumpTruck/TruckGenericStorage.cs | 90 +++++++++++++++++++ .../DumpTruck/TrucksGenericCollection.cs | 5 ++ 6 files changed, 174 insertions(+), 11 deletions(-) create mode 100644 DumpTruck/DumpTruck/ExtentionDrawningTruck.cs diff --git a/DumpTruck/DumpTruck/DumpTruck.csproj b/DumpTruck/DumpTruck/DumpTruck.csproj index 616bcc0..efe6e12 100644 --- a/DumpTruck/DumpTruck/DumpTruck.csproj +++ b/DumpTruck/DumpTruck/DumpTruck.csproj @@ -2,12 +2,13 @@ WinExe - net6.0-windows10.0.22621.0 + net8.0-windows10.0.22621.0 enable true enable 10.0.22621.0 True + \ No newline at end of file diff --git a/DumpTruck/DumpTruck/ExtentionDrawningTruck.cs b/DumpTruck/DumpTruck/ExtentionDrawningTruck.cs new file mode 100644 index 0000000..d4f1fa7 --- /dev/null +++ b/DumpTruck/DumpTruck/ExtentionDrawningTruck.cs @@ -0,0 +1,67 @@ +using DumpTruck.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.DrawningObjects +{ + /// + /// Расширение для класса EntityCar + /// + public static class ExtentionDrawningTruck + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawningTruck? CreateDrawningTruck(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningTruck(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningDumpTruck(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + Color.FromName(strs[3]), + Convert.ToBoolean(strs[4]), + width, height); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Разделитель даннных + /// Строка с данными по объекту + public static string GetDataForSave(this DrawningTruck drawningTruck, + char separatorForObject) + { + var truck = drawningTruck.EntityTruck; + if (truck == null) + { + return string.Empty; + } + var str = + $"{truck.Speed}{separatorForObject}{truck.Weight}{separatorForObject}{truck.BodyColor.Name}"; + if (truck is not EntityDumpTruck dumpTruck) + { + return str; + } + return + $"{str}{separatorForObject}{dumpTruck.AdditionalColor.Name}{separatorForObject}{dumpTruck.Trailer}"; + } + } +} diff --git a/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs index dd2855d..44c4b6d 100644 --- a/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs +++ b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs @@ -30,6 +30,7 @@ { groupBox1 = new GroupBox(); groupBox2 = new GroupBox(); + textBoxStorageName = new TextBox(); listBoxStorages = new ListBox(); buttonRemoveObject = new Button(); buttonAddObject = new Button(); @@ -38,7 +39,6 @@ buttonRemoveTruck = new Button(); buttonAddTruck = new Button(); pictureBoxCollection = new PictureBox(); - textBoxStorageName = new TextBox(); groupBox1.SuspendLayout(); groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); @@ -68,11 +68,18 @@ groupBox2.Controls.Add(buttonAddObject); groupBox2.Location = new Point(3, 24); groupBox2.Name = "groupBox2"; - groupBox2.Size = new Size(220, 331); + groupBox2.Size = new Size(220, 302); groupBox2.TabIndex = 5; groupBox2.TabStop = false; groupBox2.Text = "Наборы"; // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(30, 50); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(171, 27); + textBoxStorageName.TabIndex = 3; + // // listBoxStorages // listBoxStorages.FormattingEnabled = true; @@ -153,13 +160,6 @@ pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // - // textBoxStorageName - // - textBoxStorageName.Location = new Point(30, 50); - textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(171, 27); - textBoxStorageName.TabIndex = 3; - // // FormTruckCollection // AutoScaleDimensions = new SizeF(8F, 20F); diff --git a/DumpTruck/DumpTruck/FormTruckConfig.Designer.cs b/DumpTruck/DumpTruck/FormTruckConfig.Designer.cs index 8bfd707..36575a4 100644 --- a/DumpTruck/DumpTruck/FormTruckConfig.Designer.cs +++ b/DumpTruck/DumpTruck/FormTruckConfig.Designer.cs @@ -90,7 +90,7 @@ // labelSimpleObject // labelSimpleObject.BorderStyle = BorderStyle.FixedSingle; - labelSimpleObject.Location = new Point(317, 264); + labelSimpleObject.Location = new Point(300, 264); labelSimpleObject.Name = "labelSimpleObject"; labelSimpleObject.Size = new Size(140, 40); labelSimpleObject.TabIndex = 6; diff --git a/DumpTruck/DumpTruck/TruckGenericStorage.cs b/DumpTruck/DumpTruck/TruckGenericStorage.cs index b7b3277..372b911 100644 --- a/DumpTruck/DumpTruck/TruckGenericStorage.cs +++ b/DumpTruck/DumpTruck/TruckGenericStorage.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -29,6 +30,18 @@ namespace DumpTruck.Generics /// private readonly int _pictureHeight; /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// /// Конструктор /// /// @@ -73,6 +86,83 @@ namespace DumpTruck.Generics return null; } } + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _truckStorages) + { + StringBuilder records = new(); + foreach (DrawningTruck? elem in record.Value.GetTrucks) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using StreamWriter sw = new(filename); + sw.Write($"TruckStorage{Environment.NewLine}{data}"); + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = new(filename)) + { + string str = sr.ReadLine(); + if (str == null || str.Length == 0) + { + return false; + } + if (!str.StartsWith("TruckStorage")) + { + return false; + } + _truckStorages.Clear(); + while ((str = sr.ReadLine()) != null) + { + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + TrucksGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningTruck? truck = elem?.CreateDrawningTruck(_separatorForObject, _pictureWidth, _pictureHeight); + if (truck != null) + { + if (collection + truck == -1) + { + return false; + } + } + } + _truckStorages.Add(record[0], collection); + } + } + return true; + } } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/TrucksGenericCollection.cs b/DumpTruck/DumpTruck/TrucksGenericCollection.cs index 6a9a999..82806e5 100644 --- a/DumpTruck/DumpTruck/TrucksGenericCollection.cs +++ b/DumpTruck/DumpTruck/TrucksGenericCollection.cs @@ -132,5 +132,10 @@ namespace DumpTruck.Generics truck.DrawTransport(g); } } + + /// + /// Получение объектов коллекции + /// + public IEnumerable GetTrucks => _collection.GetTrucks(); } } \ No newline at end of file