diff --git a/Cruiser/Cruiser/CruiserGenericCollection.cs b/Cruiser/Cruiser/CruiserGenericCollection.cs index a914aba..41fe331 100644 --- a/Cruiser/Cruiser/CruiserGenericCollection.cs +++ b/Cruiser/Cruiser/CruiserGenericCollection.cs @@ -14,6 +14,7 @@ namespace Cruiser.Generics where T : DrawningCruiser where U : IMoveableObject { + public IEnumerable GetTheBuses => _collection.GetCruiser(); private readonly int _pictureWidth; private readonly int _pictureHeight; private readonly int _placeSizeWidth = 210; @@ -89,5 +90,7 @@ namespace Cruiser.Generics i++; } } + + } } \ No newline at end of file diff --git a/Cruiser/Cruiser/CruiserGenericStorage.cs b/Cruiser/Cruiser/CruiserGenericStorage.cs index bd67e7b..58e7994 100644 --- a/Cruiser/Cruiser/CruiserGenericStorage.cs +++ b/Cruiser/Cruiser/CruiserGenericStorage.cs @@ -15,6 +15,15 @@ namespace Cruiser.Generics public List Keys => _cruiserStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; public CruiserGenericStorage(int pictureWidth, int pictureHeight) { _cruiserStorages = new Dictionary>(); @@ -45,6 +54,94 @@ namespace Cruiser.Generics return null; } } + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _cruiserStorages) + { + StringBuilder records = new(); + foreach (DrawningCruiser? elem in record.Value.GetTheBuses) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + + if (data.Length == 0) + { + return false; + } + string toWrite = $"BusStorage{Environment.NewLine}{data}"; + var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + + using (StreamWriter sw = new(filename)) + { + foreach (var str in strs) + { + sw.WriteLine(str); + } + } + return true; + } + + /// + /// Загрузка информации по автобусам в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = new(filename)) + { + string str = sr.ReadLine(); + var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("BusStorage")) + { + return false; + } + _cruiserStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + str = sr.ReadLine(); + continue; + } + CruiserGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningCruiser? bus = + elem?.CreateDrawningCruiser(_separatorForObject, _pictureWidth, _pictureHeight); + if (bus != null) + { + if (!(collection + bus)) + { + return false; + } + } + } + _cruiserStorages.Add(record[0], collection); + + str = sr.ReadLine(); + } while (str != null); + } + return true; + } + } } diff --git a/Cruiser/Cruiser/ExtentionDrawingCruiser.cs b/Cruiser/Cruiser/ExtentionDrawingCruiser.cs new file mode 100644 index 0000000..3593048 --- /dev/null +++ b/Cruiser/Cruiser/ExtentionDrawingCruiser.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Cruiser.Entities; + +namespace Cruiser.DrawningObjects +{ + public static class ExtentionDrawingCruiser + { + public static DrawningCruiser? CreateDrawningCruiser(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningCruiser(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 7) + { + return new DrawningAdvancedCruiser(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 DrawningCruiser drawningcruiser, char separatorForObject) + { + var cruiser = drawningcruiser.EntityCruiser; + if (cruiser == null) + { + return string.Empty; + } + var str = $"{cruiser.Speed}{separatorForObject}{cruiser.Weight}{separatorForObject}{cruiser.BodyColor.Name}"; + if (cruiser is not EntityAdvancedCruiser advancedCruiser) + { + return str; + } + return + $"{str}{separatorForObject}{advancedCruiser.AdditionalColor.Name}{separatorForObject}" + + $"{separatorForObject}{advancedCruiser.HelicopterPad}{separatorForObject}{advancedCruiser.Coating}"; + } + } +} + diff --git a/Cruiser/Cruiser/FormCruiserCollection.Designer.cs b/Cruiser/Cruiser/FormCruiserCollection.Designer.cs index f5199cc..881a8d2 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.Designer.cs +++ b/Cruiser/Cruiser/FormCruiserCollection.Designer.cs @@ -17,26 +17,33 @@ { this.pictureBoxCruiser = new System.Windows.Forms.PictureBox(); this.panelCruiser = new System.Windows.Forms.Panel(); - this.panelSet = new System.Windows.Forms.Panel(); - this.textBoxSet = new System.Windows.Forms.TextBox(); - this.ButtonAddObject = new System.Windows.Forms.Button(); - this.listBoxStorages = new System.Windows.Forms.ListBox(); - this.ButtonDelObject = new System.Windows.Forms.Button(); - this.ButtonAddCruiser = new System.Windows.Forms.Button(); - this.textBoxCruiser = new System.Windows.Forms.TextBox(); - this.ButtonRemoveCruiser = new System.Windows.Forms.Button(); this.ButtonRefreshCollection = new System.Windows.Forms.Button(); + this.ButtonRemoveCruiser = new System.Windows.Forms.Button(); + this.textBoxCruiser = new System.Windows.Forms.TextBox(); + this.ButtonAddCruiser = new System.Windows.Forms.Button(); + this.panelSet = new System.Windows.Forms.Panel(); + this.ButtonDelObject = new System.Windows.Forms.Button(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.ButtonAddObject = new System.Windows.Forms.Button(); + this.textBoxSet = new System.Windows.Forms.TextBox(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.FileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit(); this.panelCruiser.SuspendLayout(); this.panelSet.SuspendLayout(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCruiser // this.pictureBoxCruiser.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxCruiser.Location = new System.Drawing.Point(0, 0); + this.pictureBoxCruiser.Location = new System.Drawing.Point(0, 33); this.pictureBoxCruiser.Name = "pictureBoxCruiser"; - this.pictureBoxCruiser.Size = new System.Drawing.Size(904, 510); + this.pictureBoxCruiser.Size = new System.Drawing.Size(904, 477); this.pictureBoxCruiser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxCruiser.TabIndex = 0; this.pictureBoxCruiser.TabStop = false; @@ -53,70 +60,15 @@ this.panelCruiser.Size = new System.Drawing.Size(221, 486); this.panelCruiser.TabIndex = 1; // - // panelSet + // ButtonRefreshCollection // - this.panelSet.Controls.Add(this.ButtonDelObject); - this.panelSet.Controls.Add(this.listBoxStorages); - this.panelSet.Controls.Add(this.ButtonAddObject); - this.panelSet.Controls.Add(this.textBoxSet); - this.panelSet.Location = new System.Drawing.Point(22, 19); - this.panelSet.Name = "panelSet"; - this.panelSet.Size = new System.Drawing.Size(185, 241); - this.panelSet.TabIndex = 0; - // - // textBoxSet - // - this.textBoxSet.Location = new System.Drawing.Point(38, 21); - this.textBoxSet.Name = "textBoxSet"; - this.textBoxSet.Size = new System.Drawing.Size(118, 31); - this.textBoxSet.TabIndex = 0; - // - // ButtonAddObject - // - this.ButtonAddObject.Location = new System.Drawing.Point(18, 58); - this.ButtonAddObject.Name = "ButtonAddObject"; - this.ButtonAddObject.Size = new System.Drawing.Size(153, 51); - this.ButtonAddObject.TabIndex = 2; - this.ButtonAddObject.Text = "Создать набор"; - this.ButtonAddObject.UseVisualStyleBackColor = true; - this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); - // - // listBoxStorages - // - this.listBoxStorages.FormattingEnabled = true; - this.listBoxStorages.ItemHeight = 25; - this.listBoxStorages.Location = new System.Drawing.Point(49, 115); - this.listBoxStorages.Name = "listBoxStorages"; - this.listBoxStorages.Size = new System.Drawing.Size(97, 54); - this.listBoxStorages.TabIndex = 3; - this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged); - // - // ButtonDelObject - // - this.ButtonDelObject.Location = new System.Drawing.Point(18, 175); - this.ButtonDelObject.Name = "ButtonDelObject"; - this.ButtonDelObject.Size = new System.Drawing.Size(153, 47); - this.ButtonDelObject.TabIndex = 2; - this.ButtonDelObject.Text = "Удалить набор"; - this.ButtonDelObject.UseVisualStyleBackColor = true; - this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); - // - // ButtonAddCruiser - // - this.ButtonAddCruiser.Location = new System.Drawing.Point(45, 266); - this.ButtonAddCruiser.Name = "ButtonAddCruiser"; - this.ButtonAddCruiser.Size = new System.Drawing.Size(133, 38); - this.ButtonAddCruiser.TabIndex = 2; - this.ButtonAddCruiser.Text = "Добавить"; - this.ButtonAddCruiser.UseVisualStyleBackColor = true; - this.ButtonAddCruiser.Click += new System.EventHandler(this.ButtonAddCruiser_Click); - // - // textBoxCruiser - // - this.textBoxCruiser.Location = new System.Drawing.Point(45, 330); - this.textBoxCruiser.Name = "textBoxCruiser"; - this.textBoxCruiser.Size = new System.Drawing.Size(133, 31); - this.textBoxCruiser.TabIndex = 3; + this.ButtonRefreshCollection.Location = new System.Drawing.Point(45, 435); + this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + this.ButtonRefreshCollection.Size = new System.Drawing.Size(138, 41); + this.ButtonRefreshCollection.TabIndex = 2; + this.ButtonRefreshCollection.Text = "Обновить"; + this.ButtonRefreshCollection.UseVisualStyleBackColor = true; + this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); // // ButtonRemoveCruiser // @@ -128,15 +80,106 @@ this.ButtonRemoveCruiser.UseVisualStyleBackColor = true; this.ButtonRemoveCruiser.Click += new System.EventHandler(this.ButtonRemoveCruiser_Click); // - // ButtonRefreshCollection + // textBoxCruiser // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(45, 435); - this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - this.ButtonRefreshCollection.Size = new System.Drawing.Size(138, 41); - this.ButtonRefreshCollection.TabIndex = 2; - this.ButtonRefreshCollection.Text = "Обновить"; - this.ButtonRefreshCollection.UseVisualStyleBackColor = true; - this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); + this.textBoxCruiser.Location = new System.Drawing.Point(45, 330); + this.textBoxCruiser.Name = "textBoxCruiser"; + this.textBoxCruiser.Size = new System.Drawing.Size(133, 31); + this.textBoxCruiser.TabIndex = 3; + // + // ButtonAddCruiser + // + this.ButtonAddCruiser.Location = new System.Drawing.Point(45, 266); + this.ButtonAddCruiser.Name = "ButtonAddCruiser"; + this.ButtonAddCruiser.Size = new System.Drawing.Size(133, 38); + this.ButtonAddCruiser.TabIndex = 2; + this.ButtonAddCruiser.Text = "Добавить"; + this.ButtonAddCruiser.UseVisualStyleBackColor = true; + this.ButtonAddCruiser.Click += new System.EventHandler(this.ButtonAddCruiser_Click); + // + // panelSet + // + this.panelSet.Controls.Add(this.ButtonDelObject); + this.panelSet.Controls.Add(this.listBoxStorages); + this.panelSet.Controls.Add(this.ButtonAddObject); + this.panelSet.Controls.Add(this.textBoxSet); + this.panelSet.Location = new System.Drawing.Point(22, 19); + this.panelSet.Name = "panelSet"; + this.panelSet.Size = new System.Drawing.Size(185, 241); + this.panelSet.TabIndex = 0; + // + // ButtonDelObject + // + this.ButtonDelObject.Location = new System.Drawing.Point(18, 175); + this.ButtonDelObject.Name = "ButtonDelObject"; + this.ButtonDelObject.Size = new System.Drawing.Size(153, 47); + this.ButtonDelObject.TabIndex = 2; + this.ButtonDelObject.Text = "Удалить набор"; + this.ButtonDelObject.UseVisualStyleBackColor = true; + this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); + // + // listBoxStorages + // + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 25; + this.listBoxStorages.Location = new System.Drawing.Point(49, 115); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(97, 54); + this.listBoxStorages.TabIndex = 3; + this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged); + // + // ButtonAddObject + // + this.ButtonAddObject.Location = new System.Drawing.Point(18, 58); + this.ButtonAddObject.Name = "ButtonAddObject"; + this.ButtonAddObject.Size = new System.Drawing.Size(153, 51); + this.ButtonAddObject.TabIndex = 2; + this.ButtonAddObject.Text = "Создать набор"; + this.ButtonAddObject.UseVisualStyleBackColor = true; + this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // + // textBoxSet + // + this.textBoxSet.Location = new System.Drawing.Point(38, 21); + this.textBoxSet.Name = "textBoxSet"; + this.textBoxSet.Size = new System.Drawing.Size(118, 31); + this.textBoxSet.TabIndex = 0; + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog"; + // + // menuStrip + // + this.menuStrip.ImageScalingSize = new System.Drawing.Size(24, 24); + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.FileToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(904, 33); + this.menuStrip.TabIndex = 2; + this.menuStrip.Text = "menuStrip1"; + // + // FileToolStripMenuItem + // + this.FileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.FileToolStripMenuItem.Name = "FileToolStripMenuItem"; + this.FileToolStripMenuItem.Size = new System.Drawing.Size(69, 29); + this.FileToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(273, 34); + this.SaveToolStripMenuItem.Text = "Сохранить"; + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.LoadToolStripMenuItem.Text = "Загрузить"; // // FormCruiserCollection // @@ -145,6 +188,8 @@ this.ClientSize = new System.Drawing.Size(904, 510); this.Controls.Add(this.panelCruiser); this.Controls.Add(this.pictureBoxCruiser); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormCruiserCollection"; this.Text = "FormCruiserCollection"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).EndInit(); @@ -152,6 +197,8 @@ this.panelCruiser.PerformLayout(); this.panelSet.ResumeLayout(false); this.panelSet.PerformLayout(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -169,5 +216,11 @@ private Button ButtonRemoveCruiser; private TextBox textBoxCruiser; private Button ButtonRefreshCollection; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; + private MenuStrip menuStrip; + private ToolStripMenuItem FileToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; } } \ No newline at end of file diff --git a/Cruiser/Cruiser/FormCruiserCollection.cs b/Cruiser/Cruiser/FormCruiserCollection.cs index 2eedbe3..ab0bd06 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.cs +++ b/Cruiser/Cruiser/FormCruiserCollection.cs @@ -140,5 +140,43 @@ namespace Cruiser } pictureBoxCruiser.Image = obj.ShowCruisers(); } + 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); + foreach (var collection in _storage.Keys) + { + listBoxStorages.Items.Add(collection); + } + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } \ No newline at end of file diff --git a/Cruiser/Cruiser/FormCruiserCollection.resx b/Cruiser/Cruiser/FormCruiserCollection.resx index f298a7b..017b32e 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.resx +++ b/Cruiser/Cruiser/FormCruiserCollection.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 201, 17 + + + 379, 17 + \ No newline at end of file