diff --git a/Sailboat/Sailboat/BoatsGenericCollection.cs b/Sailboat/Sailboat/BoatsGenericCollection.cs index 7535a70..4736a47 100644 --- a/Sailboat/Sailboat/BoatsGenericCollection.cs +++ b/Sailboat/Sailboat/BoatsGenericCollection.cs @@ -37,6 +37,7 @@ namespace Sailboat.Generics /// Набор объектов /// private readonly SetGeneric _collection; + public IEnumerable GetBoats => _collection.GetBoats(); /// /// Конструктор /// diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index 8dd5aaa..8b68b99 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -28,6 +28,18 @@ namespace Sailboat.Generics /// private readonly int _pictureHeight; /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// /// Конструктор /// /// @@ -68,8 +80,7 @@ namespace Sailboat.Generics /// /// /// - public BoatsGenericCollection? - this[string ind] + public BoatsGenericCollection? this[string ind] { get { @@ -80,5 +91,98 @@ namespace Sailboat.Generics return null; } } + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _boatStorages) + { + StringBuilder records = new(); + foreach (DrawingBoat? elem in record.Value.GetBoats) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new + UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); + 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); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("CarStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + _boatStorages.Clear(); + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + BoatsGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingBoat? boat = + elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); + if (boat != null) + { + if (!(collection + boat)) + { + return false; + } + } + } + _boatStorages.Add(record[0], collection); + } + return true; + } } } diff --git a/Sailboat/Sailboat/ExtentionDrawingBoat.cs b/Sailboat/Sailboat/ExtentionDrawingBoat.cs new file mode 100644 index 0000000..190503f --- /dev/null +++ b/Sailboat/Sailboat/ExtentionDrawingBoat.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.Entities; + +namespace Sailboat.DrawingObjects +{ + public static class ExtentionDrawingBoat + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawingBoat? CreateDrawingBoat(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingBoat(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 7) + { + return new DrawingSailboat(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 DrawingBoat drawingBoat, char separatorForObject) + { + var boat = drawingBoat.EntityBoat; + if (boat == null) + { + return string.Empty; + } + + var str = $"{boat.Speed}{separatorForObject}{boat.Weight}{separatorForObject}{boat.BodyColor.Name}"; + + if (boat is not EntitySailboat sailboat) + { + return str; + } + return $"{str}{separatorForObject}{sailboat.AdditionalColor.Name}{separatorForObject}{sailboat.Hull}{separatorForObject}{sailboat.Sail}"; + } + } +} diff --git a/Sailboat/Sailboat/FormBoatCollection.Designer.cs b/Sailboat/Sailboat/FormBoatCollection.Designer.cs index a3be7c3..871bc2d 100644 --- a/Sailboat/Sailboat/FormBoatCollection.Designer.cs +++ b/Sailboat/Sailboat/FormBoatCollection.Designer.cs @@ -39,16 +39,24 @@ this.buttonRefreshCollection = new System.Windows.Forms.Button(); this.buttonRemoveBoat = new System.Windows.Forms.Button(); this.buttonAddBoat = new System.Windows.Forms.Button(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); + this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.panelTools.SuspendLayout(); this.panelCollection.SuspendLayout(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection // this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(750, 600); + this.pictureBoxCollection.Size = new System.Drawing.Size(750, 720); this.pictureBoxCollection.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabStop = false; @@ -61,9 +69,10 @@ this.panelTools.Controls.Add(this.buttonRefreshCollection); this.panelTools.Controls.Add(this.buttonRemoveBoat); this.panelTools.Controls.Add(this.buttonAddBoat); - this.panelTools.Location = new System.Drawing.Point(756, 12); + this.panelTools.Controls.Add(this.menuStrip); + this.panelTools.Location = new System.Drawing.Point(756, 0); this.panelTools.Name = "panelTools"; - this.panelTools.Size = new System.Drawing.Size(209, 588); + this.panelTools.Size = new System.Drawing.Size(209, 722); this.panelTools.TabIndex = 1; // // panelCollection @@ -73,7 +82,7 @@ this.panelCollection.Controls.Add(this.listBoxStorages); this.panelCollection.Controls.Add(this.buttonAddObject); this.panelCollection.Controls.Add(this.textBoxStorageName); - this.panelCollection.Location = new System.Drawing.Point(16, 15); + this.panelCollection.Location = new System.Drawing.Point(20, 166); this.panelCollection.Name = "panelCollection"; this.panelCollection.Size = new System.Drawing.Size(181, 287); this.panelCollection.TabIndex = 4; @@ -117,14 +126,14 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(45, 416); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 567); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(125, 27); this.maskedTextBoxNumber.TabIndex = 3; // // buttonRefreshCollection // - this.buttonRefreshCollection.Location = new System.Drawing.Point(16, 531); + this.buttonRefreshCollection.Location = new System.Drawing.Point(20, 682); this.buttonRefreshCollection.Name = "buttonRefreshCollection"; this.buttonRefreshCollection.Size = new System.Drawing.Size(180, 34); this.buttonRefreshCollection.TabIndex = 2; @@ -134,7 +143,7 @@ // // buttonRemoveBoat // - this.buttonRemoveBoat.Location = new System.Drawing.Point(17, 469); + this.buttonRemoveBoat.Location = new System.Drawing.Point(21, 620); this.buttonRemoveBoat.Name = "buttonRemoveBoat"; this.buttonRemoveBoat.Size = new System.Drawing.Size(180, 34); this.buttonRemoveBoat.TabIndex = 1; @@ -144,7 +153,7 @@ // // buttonAddBoat // - this.buttonAddBoat.Location = new System.Drawing.Point(16, 339); + this.buttonAddBoat.Location = new System.Drawing.Point(20, 490); this.buttonAddBoat.Name = "buttonAddBoat"; this.buttonAddBoat.Size = new System.Drawing.Size(180, 34); this.buttonAddBoat.TabIndex = 0; @@ -152,13 +161,60 @@ this.buttonAddBoat.UseVisualStyleBackColor = true; this.buttonAddBoat.Click += new System.EventHandler(this.buttonAddBoat_Click); // + // menuStrip + // + this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItem1, + this.ToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(209, 28); + this.menuStrip.TabIndex = 5; + // + // toolStripMenuItem1 + // + this.toolStripMenuItem1.Name = "toolStripMenuItem1"; + this.toolStripMenuItem1.Size = new System.Drawing.Size(14, 24); + // + // 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(59, 24); + this.ToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Text = "Сохранение"; + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.LoadToolStripMenuItem.Text = "Загрузка"; + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormBoatCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(969, 604); + this.ClientSize = new System.Drawing.Size(969, 728); this.Controls.Add(this.panelTools); this.Controls.Add(this.pictureBoxCollection); + this.MainMenuStrip = this.menuStrip; this.Name = "FormBoatCollection"; this.Text = "FormBoatCollection"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); @@ -166,6 +222,8 @@ this.panelTools.PerformLayout(); this.panelCollection.ResumeLayout(false); this.panelCollection.PerformLayout(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -184,5 +242,12 @@ private ListBox listBoxStorages; private Button buttonAddObject; private TextBox textBoxStorageName; + private MenuStrip menuStrip; + private ToolStripMenuItem toolStripMenuItem1; + private ToolStripMenuItem ToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index a86d336..8ea390a 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -165,5 +165,38 @@ namespace Sailboat ReloadObjects(); } } + + ///// + ///// Обработка нажатия "Сохранение" + ///// + ///// + ///// + //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) + //{ + // // TODO продумать логику + //} + } } diff --git a/Sailboat/Sailboat/FormBoatCollection.resx b/Sailboat/Sailboat/FormBoatCollection.resx index f298a7b..66d62b4 100644 --- a/Sailboat/Sailboat/FormBoatCollection.resx +++ b/Sailboat/Sailboat/FormBoatCollection.resx @@ -57,4 +57,19 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 17, 17 + + + 144, 0 + + + 144, 0 + + + 25 + \ No newline at end of file