From 5ad06ae239db161f8257e74cfb849d3a4c4a0aa5 Mon Sep 17 00:00:00 2001 From: sardq Date: Sat, 2 Dec 2023 23:16:34 +0400 Subject: [PATCH] =?UTF-8?q?6=20=D0=BB=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HoistingCrane/CranesGenericCollection.cs | 1 + .../HoistingCrane/CranesGenericStorage.cs | 89 +++++++++ .../HoistingCrane/ExtentionDrawningCrane.cs | 50 ++++++ .../FormCraneCollection.Designer.cs | 169 ++++++++++++------ .../HoistingCrane/FormCraneCollection.cs | 34 ++++ .../HoistingCrane/FormCraneCollection.resx | 9 + 6 files changed, 299 insertions(+), 53 deletions(-) create mode 100644 HoistingCrane/HoistingCrane/ExtentionDrawningCrane.cs diff --git a/HoistingCrane/HoistingCrane/CranesGenericCollection.cs b/HoistingCrane/HoistingCrane/CranesGenericCollection.cs index 71988fa..3c325a0 100644 --- a/HoistingCrane/HoistingCrane/CranesGenericCollection.cs +++ b/HoistingCrane/HoistingCrane/CranesGenericCollection.cs @@ -18,6 +18,7 @@ namespace HoistingCrane.Generics private readonly int _placeSizeHeight = 150; /// Набор объектов private readonly SetGeneric _collection; + public IEnumerable GetCranes => _collection.GetCranes(); /// Конструктор public CranesGenericCollection(int picWidth, int picHeight) { diff --git a/HoistingCrane/HoistingCrane/CranesGenericStorage.cs b/HoistingCrane/HoistingCrane/CranesGenericStorage.cs index 6465166..17c5742 100644 --- a/HoistingCrane/HoistingCrane/CranesGenericStorage.cs +++ b/HoistingCrane/HoistingCrane/CranesGenericStorage.cs @@ -1,6 +1,7 @@ using HoistingCrane.DrawningObjects; using HoistingCrane.Generics; using HoistingCrane.MovementStrategy; +using System.Text; namespace HoistingCrane { @@ -14,6 +15,94 @@ namespace HoistingCrane private readonly int _pictureWidth; /// Высота окна отрисовки private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + /// Разделитель для записей коллекции данных в файл + private readonly char _separatorRecords = ';'; + /// Разделитель для записи информации по объекту в файл + private static readonly char _separatorForObject = ':'; + /// Сохранение информации по автомобилям в хранилище в файл + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _craneStorages) + { + StringBuilder records = new(); + foreach (DrawingCrane? elem in record.Value.GetCranes) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + + using (StreamWriter sw = new StreamWriter(filename)) + { + sw.WriteLine("CarStorage"); + sw.Write(data.ToString()); + } + return true; + } + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + + using (StreamReader reader = new StreamReader(filename)) + { + string cheker = reader.ReadLine(); + if (cheker == null) + { + return false; + } + if (!cheker.StartsWith("CarStorage")) + { + return false; + } + _craneStorages.Clear(); + string strs; + bool firstinit = true; + while ((strs = reader.ReadLine()) != null) + { + if (strs == null && firstinit) + { + return false; + } + if (strs == null) + { + return false; + } + firstinit = false; + string name = strs.Split(_separatorForKeyValue)[0]; + CranesGenericCollection collection = new(_pictureWidth, _pictureHeight); + foreach (string data in strs.Split(_separatorForKeyValue)[1].Split(_separatorRecords)) + { + DrawingCrane? crane = + data?.CreateDrawningCrane(_separatorForObject, _pictureWidth, _pictureHeight); + if (crane != null) + { + int? result = collection + crane; + if (result == null || result.Value == -1) + { + return false; + } + } + } + _craneStorages.Add(name, collection); + } + return true; + } + } /// Конструктор public CranesGenericStorage(int pictureWidth, int pictureHeight) { diff --git a/HoistingCrane/HoistingCrane/ExtentionDrawningCrane.cs b/HoistingCrane/HoistingCrane/ExtentionDrawningCrane.cs new file mode 100644 index 0000000..9510334 --- /dev/null +++ b/HoistingCrane/HoistingCrane/ExtentionDrawningCrane.cs @@ -0,0 +1,50 @@ +using HoistingCrane.Entities; + +namespace HoistingCrane.DrawningObjects +{ + public static class ExtentionDrawningCrane + { + + public static DrawingCrane? CreateDrawningCrane(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingCrane(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new AdditionalDrawingCrane(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + Color.FromName(strs[3]), + Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), + width, height); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Разделитель даннных + /// Строка с данными по объекту + public static string GetDataForSave(this DrawingCrane drawningCrane, + char separatorForObject) + { + var car = drawningCrane.EntityCrane; + if (car == null) + { + return string.Empty; + } + var str = $"{car.Speed}{separatorForObject}{car.Weight}{separatorForObject}{car.BodyColor.Name}"; + if (car is not AdditionEntityCrane hoistingCrane) + { + return str; + } + return $"{str}{separatorForObject}{hoistingCrane.AdditionalColor.Name}{separatorForObject}{hoistingCrane.Crane}{separatorForObject}{hoistingCrane.CounterWeight}"; + } + } +} diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs b/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs index 0899d7c..2c0924d 100644 --- a/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs @@ -29,19 +29,26 @@ private void InitializeComponent() { this.groupBoxInstruments = new System.Windows.Forms.GroupBox(); - this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); - this.buttonReload = new System.Windows.Forms.Button(); - this.buttonDelete = new System.Windows.Forms.Button(); - this.buttonAdd = new System.Windows.Forms.Button(); - this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.groupBoxSet = new System.Windows.Forms.GroupBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); this.buttonAddSet = new System.Windows.Forms.Button(); this.listBoxStorages = new System.Windows.Forms.ListBox(); this.buttonRemoveSet = new System.Windows.Forms.Button(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.buttonReload = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ToolStripMenuItemLoad = new System.Windows.Forms.ToolStripMenuItem(); + this.ToolStripMenuItemSave = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.groupBoxInstruments.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.groupBoxSet.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // groupBoxInstruments @@ -58,52 +65,6 @@ this.groupBoxInstruments.TabStop = false; this.groupBoxInstruments.Text = "Инструменты"; // - // maskedTextBoxNumber - // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 319); - this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 31); - this.maskedTextBoxNumber.TabIndex = 9; - // - // buttonReload - // - this.buttonReload.Location = new System.Drawing.Point(12, 397); - this.buttonReload.Name = "buttonReload"; - this.buttonReload.Size = new System.Drawing.Size(167, 35); - this.buttonReload.TabIndex = 8; - this.buttonReload.Text = "Обновить экран"; - this.buttonReload.UseVisualStyleBackColor = true; - this.buttonReload.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); - // - // buttonDelete - // - this.buttonDelete.Location = new System.Drawing.Point(12, 356); - this.buttonDelete.Name = "buttonDelete"; - this.buttonDelete.Size = new System.Drawing.Size(167, 35); - this.buttonDelete.TabIndex = 7; - this.buttonDelete.Text = "Удалить кран"; - this.buttonDelete.UseVisualStyleBackColor = true; - this.buttonDelete.Click += new System.EventHandler(this.ButtonRemoveCrane_Click); - // - // buttonAdd - // - this.buttonAdd.Location = new System.Drawing.Point(12, 278); - this.buttonAdd.Name = "buttonAdd"; - this.buttonAdd.Size = new System.Drawing.Size(167, 35); - this.buttonAdd.TabIndex = 6; - this.buttonAdd.Text = "Добавить кран"; - this.buttonAdd.UseVisualStyleBackColor = true; - this.buttonAdd.Click += new System.EventHandler(this.ButtonAddCrane_Click); - // - // pictureBoxCollection - // - this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); - this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(996, 434); - this.pictureBoxCollection.TabIndex = 5; - this.pictureBoxCollection.TabStop = false; - // // groupBoxSet // this.groupBoxSet.Controls.Add(this.textBoxStorageName); @@ -154,6 +115,98 @@ this.buttonRemoveSet.UseVisualStyleBackColor = true; this.buttonRemoveSet.Click += new System.EventHandler(this.ButtonDelObject_Click); // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 319); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 31); + this.maskedTextBoxNumber.TabIndex = 9; + // + // buttonReload + // + this.buttonReload.Location = new System.Drawing.Point(12, 397); + this.buttonReload.Name = "buttonReload"; + this.buttonReload.Size = new System.Drawing.Size(167, 35); + this.buttonReload.TabIndex = 8; + this.buttonReload.Text = "Обновить экран"; + this.buttonReload.UseVisualStyleBackColor = true; + this.buttonReload.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(12, 356); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(167, 35); + this.buttonDelete.TabIndex = 7; + this.buttonDelete.Text = "Удалить кран"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.ButtonRemoveCrane_Click); + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(12, 278); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(167, 35); + this.buttonAdd.TabIndex = 6; + this.buttonAdd.Text = "Добавить кран"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAddCrane_Click); + // + // pictureBoxCollection + // + this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(996, 401); + this.pictureBoxCollection.TabIndex = 5; + this.pictureBoxCollection.TabStop = false; + // + // menuStrip1 + // + this.menuStrip1.Dock = System.Windows.Forms.DockStyle.Bottom; + this.menuStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Visible; + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24); + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 401); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional; + this.menuStrip1.Size = new System.Drawing.Size(996, 33); + this.menuStrip1.TabIndex = 12; + this.menuStrip1.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.ToolStripMenuItemLoad, + this.ToolStripMenuItemSave}); + this.файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + this.файлToolStripMenuItem.Size = new System.Drawing.Size(68, 29); + this.файлToolStripMenuItem.Text = "файл"; + // + // ToolStripMenuItemLoad + // + this.ToolStripMenuItemLoad.Name = "ToolStripMenuItemLoad"; + this.ToolStripMenuItemLoad.Size = new System.Drawing.Size(197, 34); + this.ToolStripMenuItemLoad.Text = "загрузить"; + this.ToolStripMenuItemLoad.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // ToolStripMenuItemSave + // + this.ToolStripMenuItemSave.Name = "ToolStripMenuItemSave"; + this.ToolStripMenuItemSave.Size = new System.Drawing.Size(197, 34); + this.ToolStripMenuItemSave.Text = "сохранить"; + this.ToolStripMenuItemSave.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog1"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormCraneCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); @@ -161,14 +214,18 @@ this.ClientSize = new System.Drawing.Size(996, 434); this.Controls.Add(this.groupBoxInstruments); this.Controls.Add(this.pictureBoxCollection); + this.Controls.Add(this.menuStrip1); this.Name = "FormCraneCollection"; this.Text = "Набор кранов"; this.groupBoxInstruments.ResumeLayout(false); this.groupBoxInstruments.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.groupBoxSet.ResumeLayout(false); this.groupBoxSet.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -185,5 +242,11 @@ private Button buttonAddSet; private ListBox listBoxStorages; private Button buttonRemoveSet; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem ToolStripMenuItemLoad; + private ToolStripMenuItem ToolStripMenuItemSave; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.cs b/HoistingCrane/HoistingCrane/FormCraneCollection.cs index b58efc4..e9cb413 100644 --- a/HoistingCrane/HoistingCrane/FormCraneCollection.cs +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.cs @@ -1,6 +1,7 @@ using HoistingCrane.DrawningObjects; using HoistingCrane.Generics; using HoistingCrane.MovementStrategy; +using System.Windows.Forms; namespace HoistingCrane { @@ -29,6 +30,39 @@ namespace HoistingCrane listBoxStorages.SelectedIndex = index; } } + 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("Загрузка прошло успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadObjects(); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } private void ButtonAddObject_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxStorageName.Text)) diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.resx b/HoistingCrane/HoistingCrane/FormCraneCollection.resx index f298a7b..a15ebdd 100644 --- a/HoistingCrane/HoistingCrane/FormCraneCollection.resx +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 173, 17 + + + 367, 17 + \ No newline at end of file