From 6d86c591d1fa3ca05b36fbb05b5a3152387a6601 Mon Sep 17 00:00:00 2001 From: russell Date: Fri, 1 Dec 2023 19:22:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=206?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawingObjects/ExtentionDrawingTruck.cs | 63 ++++++++++++ DumpTruck/FormTruckCollection.Designer.cs | 68 ++++++++++++- DumpTruck/FormTruckCollection.cs | 50 +++++++++- DumpTruck/FormTruckCollection.resx | 9 ++ DumpTruck/Generics/TrucksGenericCollection.cs | 10 +- DumpTruck/Generics/TrucksGenericStorage.cs | 99 ++++++++++++++++++- 6 files changed, 283 insertions(+), 16 deletions(-) create mode 100644 DumpTruck/DrawingObjects/ExtentionDrawingTruck.cs diff --git a/DumpTruck/DrawingObjects/ExtentionDrawingTruck.cs b/DumpTruck/DrawingObjects/ExtentionDrawingTruck.cs new file mode 100644 index 0000000..37f738c --- /dev/null +++ b/DumpTruck/DrawingObjects/ExtentionDrawingTruck.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.Entities; + +namespace DumpTruck.DrawingObjects +{ + public static class ExtentionDrawingTruck + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawingTruck? CreateDrawingTruck(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingTruck(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 7) + { + return new DrawingDumpTruck(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + Convert.ToBoolean(strs[3]), + Convert.ToBoolean(strs[4]), + Color.FromName(strs[5]), + Color.FromName(strs[6]), width, height); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Разделитель даннных + /// Строка с данными по объекту + public static string GetDataForSave(this DrawingTruck drawingTruck, char separatorForObject) + { + var truck = drawingTruck.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.Tent}{separatorForObject}{dumpTruck.DumpBox}{separatorForObject}{dumpTruck.TentColor.Name}{separatorForObject}{dumpTruck.DumpBoxColor.Name}"; + } + } +} diff --git a/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/FormTruckCollection.Designer.cs index 4d20db9..557082d 100644 --- a/DumpTruck/FormTruckCollection.Designer.cs +++ b/DumpTruck/FormTruckCollection.Designer.cs @@ -32,6 +32,10 @@ this.panelStorages = new System.Windows.Forms.Panel(); this.labelStorages = new System.Windows.Forms.Label(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.menuStripFile = new System.Windows.Forms.MenuStrip(); + this.toolStripMenuItemFile = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItemSave = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItemLoad = new System.Windows.Forms.ToolStripMenuItem(); this.buttonDelObject = new System.Windows.Forms.Button(); this.buttonAddObject = new System.Windows.Forms.Button(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -41,8 +45,11 @@ this.buttonRemoveTruck = new System.Windows.Forms.Button(); this.buttonAddTruck = new System.Windows.Forms.Button(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.panelTools.SuspendLayout(); this.panelStorages.SuspendLayout(); + this.menuStripFile.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.SuspendLayout(); // @@ -65,12 +72,13 @@ // this.panelStorages.Controls.Add(this.labelStorages); this.panelStorages.Controls.Add(this.textBoxStorageName); + this.panelStorages.Controls.Add(this.menuStripFile); this.panelStorages.Controls.Add(this.buttonDelObject); this.panelStorages.Controls.Add(this.buttonAddObject); this.panelStorages.Controls.Add(this.listBoxStorages); this.panelStorages.Location = new System.Drawing.Point(3, 18); this.panelStorages.Name = "panelStorages"; - this.panelStorages.Size = new System.Drawing.Size(200, 221); + this.panelStorages.Size = new System.Drawing.Size(200, 246); this.panelStorages.TabIndex = 5; // // labelStorages @@ -89,6 +97,40 @@ this.textBoxStorageName.Size = new System.Drawing.Size(150, 23); this.textBoxStorageName.TabIndex = 3; // + // menuStripFile + // + this.menuStripFile.Dock = System.Windows.Forms.DockStyle.None; + this.menuStripFile.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItemFile}); + this.menuStripFile.Location = new System.Drawing.Point(25, 222); + this.menuStripFile.Name = "menuStripFile"; + this.menuStripFile.Size = new System.Drawing.Size(176, 24); + this.menuStripFile.TabIndex = 2; + this.menuStripFile.Text = "menuStripFile"; + // + // toolStripMenuItemFile + // + this.toolStripMenuItemFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItemSave, + this.toolStripMenuItemLoad}); + this.toolStripMenuItemFile.Name = "toolStripMenuItemFile"; + this.toolStripMenuItemFile.Size = new System.Drawing.Size(48, 20); + this.toolStripMenuItemFile.Text = "Файл"; + // + // toolStripMenuItemSave + // + this.toolStripMenuItemSave.Name = "toolStripMenuItemSave"; + this.toolStripMenuItemSave.Size = new System.Drawing.Size(180, 22); + this.toolStripMenuItemSave.Text = "Сохранить"; + this.toolStripMenuItemSave.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // toolStripMenuItemLoad + // + this.toolStripMenuItemLoad.Name = "toolStripMenuItemLoad"; + this.toolStripMenuItemLoad.Size = new System.Drawing.Size(180, 22); + this.toolStripMenuItemLoad.Text = "Загрузить"; + this.toolStripMenuItemLoad.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // // buttonDelObject // this.buttonDelObject.Location = new System.Drawing.Point(25, 185); @@ -121,7 +163,7 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 302); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 306); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 23); this.maskedTextBoxNumber.TabIndex = 4; @@ -148,7 +190,7 @@ // // buttonRemoveTruck // - this.buttonRemoveTruck.Location = new System.Drawing.Point(28, 336); + this.buttonRemoveTruck.Location = new System.Drawing.Point(28, 335); this.buttonRemoveTruck.Name = "buttonRemoveTruck"; this.buttonRemoveTruck.Size = new System.Drawing.Size(150, 30); this.buttonRemoveTruck.TabIndex = 2; @@ -158,7 +200,7 @@ // // buttonAddTruck // - this.buttonAddTruck.Location = new System.Drawing.Point(28, 257); + this.buttonAddTruck.Location = new System.Drawing.Point(28, 270); this.buttonAddTruck.Name = "buttonAddTruck"; this.buttonAddTruck.Size = new System.Drawing.Size(150, 30); this.buttonAddTruck.TabIndex = 1; @@ -174,6 +216,15 @@ this.pictureBoxCollection.TabIndex = 1; this.pictureBoxCollection.TabStop = false; // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog1"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormTruckCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -181,12 +232,15 @@ this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.panelTools); + this.MainMenuStrip = this.menuStripFile; this.Name = "FormTruckCollection"; this.Text = "FormTruckCollection"; this.panelTools.ResumeLayout(false); this.panelTools.PerformLayout(); this.panelStorages.ResumeLayout(false); this.panelStorages.PerformLayout(); + this.menuStripFile.ResumeLayout(false); + this.menuStripFile.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); @@ -207,5 +261,11 @@ private ListBox listBoxStorages; private Label labelStorages; private TextBox textBoxStorageName; + private MenuStrip menuStripFile; + private ToolStripMenuItem toolStripMenuItemFile; + private ToolStripMenuItem toolStripMenuItemSave; + private ToolStripMenuItem toolStripMenuItemLoad; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/DumpTruck/FormTruckCollection.cs b/DumpTruck/FormTruckCollection.cs index 502c319..b5a1d22 100644 --- a/DumpTruck/FormTruckCollection.cs +++ b/DumpTruck/FormTruckCollection.cs @@ -96,8 +96,8 @@ namespace DumpTruck private void AddTruck(DrawingTruck truck) { - truck._pictureWidth = pictureBoxCollection.Width; - truck._pictureHeight = pictureBoxCollection.Height; + truck._pictureWidth = pictureBoxCollection.Image.Width; + truck._pictureHeight = pictureBoxCollection.Image.Height; if (listBoxStorages.SelectedIndex == -1) { return; @@ -170,6 +170,48 @@ namespace DumpTruck } pictureBoxCollection.Image = obj.ShowTrucks(); } - } - + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.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 (_storage.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", + "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadObjects(); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } } diff --git a/DumpTruck/FormTruckCollection.resx b/DumpTruck/FormTruckCollection.resx index f298a7b..6163eb4 100644 --- a/DumpTruck/FormTruckCollection.resx +++ b/DumpTruck/FormTruckCollection.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 143, 17 + + + 276, 17 + \ No newline at end of file diff --git a/DumpTruck/Generics/TrucksGenericCollection.cs b/DumpTruck/Generics/TrucksGenericCollection.cs index ee7e665..c2b7d77 100644 --- a/DumpTruck/Generics/TrucksGenericCollection.cs +++ b/DumpTruck/Generics/TrucksGenericCollection.cs @@ -38,6 +38,10 @@ namespace DumpTruck.Generics /// private readonly SetGeneric _collection; /// + /// Получение объектов коллекции + /// + public IEnumerable GetTrucks => _collection.GetTrucks(); + /// /// Конструктор /// /// @@ -128,11 +132,11 @@ namespace DumpTruck.Generics { int index = 0; foreach (var truck in _collection.GetTrucks()) - { + { if (truck != null) { - truck.SetPosition(index % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, index / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); - truck.DrawTransport(g); + truck.SetPosition(index % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, index / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); + truck.DrawTransport(g); } index++; } diff --git a/DumpTruck/Generics/TrucksGenericStorage.cs b/DumpTruck/Generics/TrucksGenericStorage.cs index de67f59..e299c2c 100644 --- a/DumpTruck/Generics/TrucksGenericStorage.cs +++ b/DumpTruck/Generics/TrucksGenericStorage.cs @@ -13,10 +13,22 @@ namespace DumpTruck.Generics /// internal class TrucksGenericStorage { - /// - /// Словарь (хранилище) - /// - readonly Dictionary> _truckStorages; + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _truckStorages; /// /// Возвращение списка названий наборов /// @@ -70,5 +82,82 @@ 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 (DrawingTruck? 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 < DrawingTruck, DrawningObjectTruck > collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingTruck? truck = elem?.CreateDrawingTruck(_separatorForObject, _pictureWidth, _pictureHeight); + if (truck != null) + { + if (collection + truck == -1) + { + return false; + } + } + } + _truckStorages.Add(record[0], collection); + } + } + return true; + } + } } -- 2.25.1