diff --git a/base/Catamaran/Catamaran/CatamaransGenericCollection.cs b/base/Catamaran/Catamaran/CatamaransGenericCollection.cs index 450908e..0146196 100644 --- a/base/Catamaran/Catamaran/CatamaransGenericCollection.cs +++ b/base/Catamaran/Catamaran/CatamaransGenericCollection.cs @@ -38,6 +38,10 @@ namespace Catamaran.Generics /// private readonly SetGeneric _collection; /// + /// Получение объектов коллекции + /// + public IEnumerable GetCatamarans => _collection.GetCatamarans(); + /// /// Конструктор /// /// diff --git a/base/Catamaran/Catamaran/CatamaransGenericStorage.cs b/base/Catamaran/Catamaran/CatamaransGenericStorage.cs index 39c92da..25c1a88 100644 --- a/base/Catamaran/Catamaran/CatamaransGenericStorage.cs +++ b/base/Catamaran/Catamaran/CatamaransGenericStorage.cs @@ -1,5 +1,7 @@ using Catamaran.DrawningObjects; using Catamaran.MovementStrategy; +using System.Text; + namespace Catamaran.Generics { /// @@ -25,6 +27,18 @@ namespace Catamaran.Generics /// private readonly int _pictureHeight; /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// /// Конструктор /// /// @@ -73,5 +87,89 @@ namespace Catamaran.Generics return null; } } + /// + /// Сохранение информации по катамаранам в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _catamaranStorages) + { + StringBuilder records = new(); + foreach (DrawningCatamaran? elem in record.Value.GetCatamarans) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.Write($"CatamaranStorage{Environment.NewLine}{data}"); + } + return true; + } + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + + using (StreamReader fs = File.OpenText(filename)) + { + string str = fs.ReadLine(); + if (str == null || str.Length == 0) + { + return false; + } + if (!str.StartsWith("CatamaranStorage")) + { + return false; + } + + _catamaranStorages.Clear(); + string strs = ""; + + while ((strs = fs.ReadLine()) != null) + { + if (strs == null) + { + return false; + } + + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + CatamaransGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningCatamaran? catamaran = elem?.CreateDrawningCatamaran(_separatorForObject, _pictureWidth, _pictureHeight); + if (catamaran != null) + { + if (!(collection + catamaran)) + { + return false; + } + } + } + _catamaranStorages.Add(record[0], collection); + } + return true; + } + } } -} +} \ No newline at end of file diff --git a/base/Catamaran/Catamaran/EntityCatamaran.cs b/base/Catamaran/Catamaran/EntityCatamaran.cs index 2d00394..f486b99 100644 --- a/base/Catamaran/Catamaran/EntityCatamaran.cs +++ b/base/Catamaran/Catamaran/EntityCatamaran.cs @@ -33,7 +33,8 @@ namespace Catamaran.Entities /// Скорость /// Вес автомобиля /// Основной цвет - public EntityCatamaran(int speed, double weight, Color bodyColor) + + public EntityCatamaran(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; diff --git a/base/Catamaran/Catamaran/ExtentionDrawningCatamaran.cs b/base/Catamaran/Catamaran/ExtentionDrawningCatamaran.cs new file mode 100644 index 0000000..836892c --- /dev/null +++ b/base/Catamaran/Catamaran/ExtentionDrawningCatamaran.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Catamaran.Entities; +namespace Catamaran.DrawningObjects +{ + /// + /// Расширение для класса EntityCatamaran + /// + public static class ExtentionDrawningCatamaran + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawningCatamaran? CreateDrawningCatamaran(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningCatamaran(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningSailCatamaran(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 DrawningCatamaran drawningCatamaran, + char separatorForObject) + { + var catamaran = drawningCatamaran.EntityCatamaran; + if (catamaran == null) + { + return string.Empty; + } + var str = $"{catamaran.Speed}{separatorForObject}{catamaran.Weight}{separatorForObject}{catamaran.BodyColor.Name}"; + if (catamaran is not EntitySailCatamaran sailCatamaran) + { + return str; + } + return + $"{str}{separatorForObject}{sailCatamaran.AdditionalColor.Name}{separatorForObject}{sailCatamaran.Sail}{separatorForObject}{sailCatamaran.FloatDetail}"; + } + } +} diff --git a/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs b/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs index 238e6f2..a057197 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs @@ -39,9 +39,16 @@ this.ButtonRefreshCollection = new System.Windows.Forms.Button(); this.ButtonRemoveCatamaran = new System.Windows.Forms.Button(); this.ButtonAddCatamaran = new System.Windows.Forms.Button(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.groupBoxTools.SuspendLayout(); this.groupBoxSets.SuspendLayout(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection @@ -59,6 +66,7 @@ this.groupBoxTools.Controls.Add(this.ButtonRefreshCollection); this.groupBoxTools.Controls.Add(this.ButtonRemoveCatamaran); this.groupBoxTools.Controls.Add(this.ButtonAddCatamaran); + this.groupBoxTools.Controls.Add(this.menuStrip); this.groupBoxTools.Location = new System.Drawing.Point(739, 1); this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Size = new System.Drawing.Size(147, 460); @@ -72,23 +80,23 @@ this.groupBoxSets.Controls.Add(this.buttonDelObject); this.groupBoxSets.Controls.Add(this.listBoxStorages); this.groupBoxSets.Controls.Add(this.buttonAddObject); - this.groupBoxSets.Location = new System.Drawing.Point(3, 21); + this.groupBoxSets.Location = new System.Drawing.Point(3, 63); this.groupBoxSets.Name = "groupBoxSets"; - this.groupBoxSets.Size = new System.Drawing.Size(138, 245); + this.groupBoxSets.Size = new System.Drawing.Size(138, 203); this.groupBoxSets.TabIndex = 4; this.groupBoxSets.TabStop = false; this.groupBoxSets.Text = "Наборы"; // // textBoxStorageName // - this.textBoxStorageName.Location = new System.Drawing.Point(3, 36); + this.textBoxStorageName.Location = new System.Drawing.Point(6, 22); this.textBoxStorageName.Name = "textBoxStorageName"; - this.textBoxStorageName.Size = new System.Drawing.Size(134, 23); + this.textBoxStorageName.Size = new System.Drawing.Size(130, 23); this.textBoxStorageName.TabIndex = 4; // // buttonDelObject // - this.buttonDelObject.Location = new System.Drawing.Point(3, 209); + this.buttonDelObject.Location = new System.Drawing.Point(3, 167); this.buttonDelObject.Name = "buttonDelObject"; this.buttonDelObject.Size = new System.Drawing.Size(134, 27); this.buttonDelObject.TabIndex = 3; @@ -100,17 +108,17 @@ // this.listBoxStorages.FormattingEnabled = true; this.listBoxStorages.ItemHeight = 15; - this.listBoxStorages.Location = new System.Drawing.Point(3, 102); + this.listBoxStorages.Location = new System.Drawing.Point(6, 82); this.listBoxStorages.Name = "listBoxStorages"; - this.listBoxStorages.Size = new System.Drawing.Size(134, 94); + this.listBoxStorages.Size = new System.Drawing.Size(132, 79); this.listBoxStorages.TabIndex = 2; this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); // // buttonAddObject // - this.buttonAddObject.Location = new System.Drawing.Point(3, 65); + this.buttonAddObject.Location = new System.Drawing.Point(6, 51); this.buttonAddObject.Name = "buttonAddObject"; - this.buttonAddObject.Size = new System.Drawing.Size(134, 25); + this.buttonAddObject.Size = new System.Drawing.Size(131, 25); this.buttonAddObject.TabIndex = 1; this.buttonAddObject.Text = "Добавить набор"; this.buttonAddObject.UseVisualStyleBackColor = true; @@ -153,13 +161,51 @@ this.ButtonAddCatamaran.UseVisualStyleBackColor = true; this.ButtonAddCatamaran.Click += new System.EventHandler(this.ButtonAddCatamaran_Click); // + // menuStrip + // + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.ToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(3, 19); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(141, 24); + this.menuStrip.TabIndex = 5; + this.menuStrip.Text = "menuStrip"; + // + // ToolStripMenuItem + // + this.ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.ToolStripMenuItem.Name = "ToolStripMenuItem"; + this.ToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.ToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog1"; + // // FormCatamaranCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(884, 461); + this.ClientSize = new System.Drawing.Size(885, 461); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.pictureBoxCollection); + this.MainMenuStrip = this.menuStrip; this.Name = "FormCatamaranCollection"; this.Text = "Набор катамаранов"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); @@ -167,6 +213,8 @@ this.groupBoxTools.PerformLayout(); this.groupBoxSets.ResumeLayout(false); this.groupBoxSets.PerformLayout(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); } @@ -184,5 +232,11 @@ private Button buttonDelObject; private ListBox listBoxStorages; private TextBox textBoxStorageName; + 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/base/Catamaran/Catamaran/FormCatamaranCollection.cs b/base/Catamaran/Catamaran/FormCatamaranCollection.cs index 503a692..98d3ad4 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.cs +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.cs @@ -192,7 +192,49 @@ namespace Catamaran return; } pictureBoxCollection.Image = obj.ShowCatamarans(); - + } + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + 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); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + ReloadObjects(); } } } diff --git a/base/Catamaran/Catamaran/FormCatamaranCollection.resx b/base/Catamaran/Catamaran/FormCatamaranCollection.resx index f298a7b..e9d6757 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.resx +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.resx @@ -57,4 +57,16 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 254, 17 + + + 96 + \ No newline at end of file diff --git a/base/Catamaran/Catamaran/FormCatamaranConfig.Designer.cs b/base/Catamaran/Catamaran/FormCatamaranConfig.Designer.cs index ce830d0..cbd3ca4 100644 --- a/base/Catamaran/Catamaran/FormCatamaranConfig.Designer.cs +++ b/base/Catamaran/Catamaran/FormCatamaranConfig.Designer.cs @@ -145,7 +145,7 @@ // // panelYellow // - this.panelYellow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128))))); + this.panelYellow.BackColor = System.Drawing.Color.Gold; this.panelYellow.Location = new System.Drawing.Point(187, 29); this.panelYellow.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.panelYellow.Name = "panelYellow";