From 442f917927479befa68b488490c9021ced46d254 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Fri, 24 Nov 2023 21:30:21 +0300 Subject: [PATCH 1/2] Done --- AirFighter/AirplaneGenericCollection.cs | 1 + AirFighter/AirplanesGenericStorage.cs | 93 +++++++++++++++++++ AirFighter/DrawningAirplane.cs | 3 - AirFighter/FormAirplaneCollection.Designer.cs | 73 +++++++++++++-- AirFighter/FormAirplaneCollection.cs | 43 ++++++++- AirFighter/FormAirplaneCollection.resx | 9 ++ 6 files changed, 210 insertions(+), 12 deletions(-) diff --git a/AirFighter/AirplaneGenericCollection.cs b/AirFighter/AirplaneGenericCollection.cs index e709ee1..d602e8f 100644 --- a/AirFighter/AirplaneGenericCollection.cs +++ b/AirFighter/AirplaneGenericCollection.cs @@ -8,6 +8,7 @@ namespace ProjectAirFighter.Generics where T :DrawningAirplane where U: IMoveableObject { + public IEnumerable GetAirplanes => _collection.GetAirplanes(); private readonly int _pictureWidth; private readonly int _pictureHeight; diff --git a/AirFighter/AirplanesGenericStorage.cs b/AirFighter/AirplanesGenericStorage.cs index c723fbf..b89e988 100644 --- a/AirFighter/AirplanesGenericStorage.cs +++ b/AirFighter/AirplanesGenericStorage.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.IO; namespace ProjectAirFighter.Generics { @@ -18,13 +19,105 @@ namespace ProjectAirFighter.Generics private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; public AirplanesGenericStorage(int pictureWidth, int pictureHeight) { _airplaneStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _airplaneStorages) + { + StringBuilder records = new(); + foreach (DrawningAirplane? elem in record.Value.GetAirplanes) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + string toWrite = $"AirplaneStorage{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; + } + + 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("AirplaneStorage")) + { + return false; + } + _airplaneStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + str = sr.ReadLine(); + continue; + } + AirplaneslGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningAirplane? airplane = + elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight); + if (airplane != null) + { + if (!(collection + airplane)) + { + return false; + } + } + } + _airplaneStorages.Add(record[0], collection); + + str = sr.ReadLine(); + } while (str != null); + } + return true; + } public void AddSet(string name) { _airplaneStorages.Add(name, new AirplaneslGenericCollection(_pictureWidth, _pictureHeight)); diff --git a/AirFighter/DrawningAirplane.cs b/AirFighter/DrawningAirplane.cs index 3b0f398..4c89625 100644 --- a/AirFighter/DrawningAirplane.cs +++ b/AirFighter/DrawningAirplane.cs @@ -32,9 +32,7 @@ namespace ProjectAirFighter.DrawningObjects if (width <= _airplaneWidth || height <= _airplanewingHeight) return; _pictureWidth = width; - _pictureHeight = height; - EntityAirplane = new EntityAirplane(speed, weight, bodyColor); } @@ -47,7 +45,6 @@ width, int height, int airplaneWidth, int airplaneHeight) _pictureHeight = height; _airplaneWidth = airplaneWidth; _airplanewingHeight = airplaneHeight; - EntityAirplane = new EntityAirplane(speed, weight, bodyColor); } public void ChangeColor(Color col) diff --git a/AirFighter/FormAirplaneCollection.Designer.cs b/AirFighter/FormAirplaneCollection.Designer.cs index 66aa308..da9ef7a 100644 --- a/AirFighter/FormAirplaneCollection.Designer.cs +++ b/AirFighter/FormAirplaneCollection.Designer.cs @@ -38,9 +38,16 @@ this.updateCollectionButton = new System.Windows.Forms.Button(); this.deleteAirplaneButton = new System.Windows.Forms.Button(); this.addAirplaneButton = new System.Windows.Forms.Button(); + this.menuStrip1 = 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.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.toolGroupBox.SuspendLayout(); this.kitGroupbox.SuspendLayout(); + this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.SuspendLayout(); // @@ -51,6 +58,7 @@ this.toolGroupBox.Controls.Add(this.updateCollectionButton); this.toolGroupBox.Controls.Add(this.deleteAirplaneButton); this.toolGroupBox.Controls.Add(this.addAirplaneButton); + this.toolGroupBox.Controls.Add(this.menuStrip1); this.toolGroupBox.Location = new System.Drawing.Point(769, 12); this.toolGroupBox.Name = "toolGroupBox"; this.toolGroupBox.Size = new System.Drawing.Size(232, 632); @@ -60,7 +68,7 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(7, 387); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(7, 406); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(215, 27); this.maskedTextBoxNumber.TabIndex = 7; @@ -71,7 +79,7 @@ this.kitGroupbox.Controls.Add(this.ButtonDelObject); this.kitGroupbox.Controls.Add(this.ButtonAddObject); this.kitGroupbox.Controls.Add(this.listBoxStorages); - this.kitGroupbox.Location = new System.Drawing.Point(8, 39); + this.kitGroupbox.Location = new System.Drawing.Point(7, 74); this.kitGroupbox.Name = "kitGroupbox"; this.kitGroupbox.Size = new System.Drawing.Size(217, 280); this.kitGroupbox.TabIndex = 5; @@ -80,7 +88,7 @@ // // textBoxStorageName // - this.textBoxStorageName.Location = new System.Drawing.Point(23, 27); + this.textBoxStorageName.Location = new System.Drawing.Point(17, 26); this.textBoxStorageName.Name = "textBoxStorageName"; this.textBoxStorageName.Size = new System.Drawing.Size(185, 27); this.textBoxStorageName.TabIndex = 9; @@ -113,10 +121,11 @@ this.listBoxStorages.Name = "listBoxStorages"; this.listBoxStorages.Size = new System.Drawing.Size(185, 104); this.listBoxStorages.TabIndex = 6; + this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxStorages_SelectedIndexChanged); // // updateCollectionButton // - this.updateCollectionButton.Location = new System.Drawing.Point(6, 484); + this.updateCollectionButton.Location = new System.Drawing.Point(6, 505); this.updateCollectionButton.Name = "updateCollectionButton"; this.updateCollectionButton.Size = new System.Drawing.Size(215, 40); this.updateCollectionButton.TabIndex = 3; @@ -126,7 +135,7 @@ // // deleteAirplaneButton // - this.deleteAirplaneButton.Location = new System.Drawing.Point(7, 439); + this.deleteAirplaneButton.Location = new System.Drawing.Point(6, 449); this.deleteAirplaneButton.Name = "deleteAirplaneButton"; this.deleteAirplaneButton.Size = new System.Drawing.Size(215, 40); this.deleteAirplaneButton.TabIndex = 2; @@ -136,7 +145,7 @@ // // addAirplaneButton // - this.addAirplaneButton.Location = new System.Drawing.Point(8, 324); + this.addAirplaneButton.Location = new System.Drawing.Point(8, 360); this.addAirplaneButton.Name = "addAirplaneButton"; this.addAirplaneButton.Size = new System.Drawing.Size(215, 40); this.addAirplaneButton.TabIndex = 0; @@ -144,6 +153,40 @@ this.addAirplaneButton.UseVisualStyleBackColor = true; this.addAirplaneButton.Click += new System.EventHandler(this.addAirplaneButton_Click); // + // menuStrip1 + // + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(3, 23); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(226, 28); + this.menuStrip1.TabIndex = 8; + this.menuStrip1.Text = "menuStrip1"; + // + // файл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(177, 26); + this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(177, 26); + this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // // pictureBoxCollection // this.pictureBoxCollection.Location = new System.Drawing.Point(12, 12); @@ -152,6 +195,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"; + // // FormAirplaneCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -159,12 +211,15 @@ this.ClientSize = new System.Drawing.Size(1017, 654); this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.toolGroupBox); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormAirplaneCollection"; this.Text = "FormMonorailCollection"; this.toolGroupBox.ResumeLayout(false); this.toolGroupBox.PerformLayout(); this.kitGroupbox.ResumeLayout(false); this.kitGroupbox.PerformLayout(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); @@ -183,5 +238,11 @@ private Button ButtonDelObject; private TextBox textBoxStorageName; private MaskedTextBox maskedTextBoxNumber; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/AirFighter/FormAirplaneCollection.cs b/AirFighter/FormAirplaneCollection.cs index 0881b4d..cb0d96a 100644 --- a/AirFighter/FormAirplaneCollection.cs +++ b/AirFighter/FormAirplaneCollection.cs @@ -23,6 +23,44 @@ namespace ProjectAirFighter _storage = new AirplanesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } + + 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); + } + } + } private void ReloadObjects() { int index = listBoxStorages.SelectedIndex; @@ -42,6 +80,7 @@ namespace ProjectAirFighter { listBoxStorages.SelectedIndex = index; } + if (listBoxStorages.SelectedIndex == -1) { return; @@ -53,7 +92,6 @@ namespace ProjectAirFighter return; } pictureBoxCollection.Image = obj.ShowAirplanes(); - } private void ButtonAddObject_Click(object sender, EventArgs e) { @@ -111,7 +149,6 @@ namespace ProjectAirFighter pictureBoxCollection.Image = obj.ShowAirplanes(); } - private void addAirplaneButton_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) @@ -145,6 +182,7 @@ namespace ProjectAirFighter { pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes(); + } private void ButtonDelObject_Click(object sender, EventArgs e) @@ -162,6 +200,5 @@ MessageBoxIcon.Question) == DialogResult.Yes) } } - } } diff --git a/AirFighter/FormAirplaneCollection.resx b/AirFighter/FormAirplaneCollection.resx index f298a7b..988cd24 100644 --- a/AirFighter/FormAirplaneCollection.resx +++ b/AirFighter/FormAirplaneCollection.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 152, 17 + + + 311, 17 + \ No newline at end of file -- 2.25.1 From bb75afe94449a5cf6afe794e8899f9833f028ca6 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Fri, 24 Nov 2023 21:30:27 +0300 Subject: [PATCH 2/2] Done --- AirFighter/ExtentionDrawningAirplane.cs | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 AirFighter/ExtentionDrawningAirplane.cs diff --git a/AirFighter/ExtentionDrawningAirplane.cs b/AirFighter/ExtentionDrawningAirplane.cs new file mode 100644 index 0000000..3b0356a --- /dev/null +++ b/AirFighter/ExtentionDrawningAirplane.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectAirFighter.Entities; +using System.Drawing; + +namespace ProjectAirFighter.DrawningObjects +{ + public static class ExtentionDrawningAirplane { + public static DrawningAirplane? CreateDrawningAirplane(this string info, char separatorForObject, + int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningAirplane(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningAirFighter(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 DrawningAirplane drawningAirplane, char separatorForObject) + { + var airplane = drawningAirplane.EntityAirplane; + if (airplane == null) + { + return string.Empty; + } + var str = $"{airplane.Speed}{separatorForObject}{airplane.Weight}{separatorForObject}{airplane.BodyColor.Name}"; + if (airplane is not EntityAirFighter airfighter) + { + return str; + } + return +$"{str}{separatorForObject}{airfighter.AdditionalColor.Name}{separatorForObject}{airfighter.Wing}{separatorForObject}{airfighter.Racket}"; + + + + } + } +} + -- 2.25.1