From f33c17eade2d73737c6057d259f7d2dc34e27d3b Mon Sep 17 00:00:00 2001 From: Whoisthatjulia Date: Wed, 13 Dec 2023 03:15:53 +0400 Subject: [PATCH] lab_6 --- AirFighter/AirFighter/AirFighterDelegate.cs | 11 ++ .../AirFighter/AirFighterGenericCollection.cs | 2 + .../AirFighter/AirFighterGenericStorage.cs | 129 +++++++++++++++--- AirFighter/AirFighter/ExtentionAirFighter.cs | 49 +++++++ .../FormAirFighterCollection.Designer.cs | 65 ++++++++- .../AirFighter/FormAirFighterCollection.cs | 39 +++++- .../AirFighter/FormAirFighterCollection.resx | 9 ++ .../FormAirFighterConfig.Designer.cs | 9 +- AirFighter/AirFighter/SetGeneric.cs | 7 +- 9 files changed, 281 insertions(+), 39 deletions(-) create mode 100644 AirFighter/AirFighter/AirFighterDelegate.cs create mode 100644 AirFighter/AirFighter/ExtentionAirFighter.cs diff --git a/AirFighter/AirFighter/AirFighterDelegate.cs b/AirFighter/AirFighter/AirFighterDelegate.cs new file mode 100644 index 0000000..83a021e --- /dev/null +++ b/AirFighter/AirFighter/AirFighterDelegate.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirFighter.DrawningObjects; + +namespace AirFighter +{ + public delegate void AirFighterDelegate(DrawningAirFighter fighter); +} diff --git a/AirFighter/AirFighter/AirFighterGenericCollection.cs b/AirFighter/AirFighter/AirFighterGenericCollection.cs index 11152ff..e91db23 100644 --- a/AirFighter/AirFighter/AirFighterGenericCollection.cs +++ b/AirFighter/AirFighter/AirFighterGenericCollection.cs @@ -10,6 +10,8 @@ namespace AirFighter.Generics where T : DrawningAirFighter where U : IMoveableObject { + public IEnumerable GetAirFighter => _collection.GetAirFighter(); + private readonly int _pictureWidth; private readonly int _pictureHeight; diff --git a/AirFighter/AirFighter/AirFighterGenericStorage.cs b/AirFighter/AirFighter/AirFighterGenericStorage.cs index 848bf4b..f005ab9 100644 --- a/AirFighter/AirFighter/AirFighterGenericStorage.cs +++ b/AirFighter/AirFighter/AirFighterGenericStorage.cs @@ -3,53 +3,138 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; - -using AirFighter.Drawnings; -using AirFighter.MovementStrategy; using AirFighter.DrawningObjects; +using AirFighter.MovementStrategy; +using AirFighter.Generics; +using System.IO; namespace AirFighter.Generics { internal class AirFighterGenericStorage { - readonly Dictionary> _AirFighterStorages; - public List Keys => _AirFighterStorages.Keys.ToList(); + readonly Dictionary> _fighterStorages; + + public List Keys => _fighterStorages.Keys.ToList(); private readonly int _pictureWidth; - private readonly int _pictureHeight; public AirFighterGenericStorage(int pictureWidth, int pictureHeight) { - _AirFighterStorages = new Dictionary>(); + _fighterStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string name) { - if (!_AirFighterStorages.ContainsKey(name)) - { - var fighterCollection = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); - _AirFighterStorages.Add(name, fighterCollection); - } + if (_fighterStorages.ContainsKey(name)) return; + _fighterStorages[name] = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); } + public void DelSet(string name) { - if (_AirFighterStorages.ContainsKey(name)) - { - _AirFighterStorages.Remove(name); - } + if (!_fighterStorages.ContainsKey(name)) return; + _fighterStorages.Remove(name); } - public AirFighterGenericCollection? this[string ind] + + public AirFighterGenericCollection? + this[string ind] { get { - if (_AirFighterStorages.ContainsKey(ind)) - { - return _AirFighterStorages[ind]; - } + if (_fighterStorages.ContainsKey(ind)) return _fighterStorages[ind]; return null; } } + + 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 _fighterStorages) + { + StringBuilder records = new(); + foreach (DrawningAirFighter? elem in record.Value.GetAirFighter) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using StreamWriter fs = new StreamWriter(filename); + { + fs.WriteLine($"AirFighterStorages{Environment.NewLine}"); + fs.WriteLine(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("AirFighterStorages")) + { + //если нет такой записи, то это не те данные + return false; + } + + _fighterStorages.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; + } + AirFighterGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningAirFighter? fighter = elem?.CreateDrawningAirFighter(_separatorForObject, _pictureWidth, _pictureHeight); + if (fighter != null) + { + if ((collection + fighter) == -1) + { + return false; + } + } + } + _fighterStorages.Add(record[0], collection); + } + return true; + } + } } } - diff --git a/AirFighter/AirFighter/ExtentionAirFighter.cs b/AirFighter/AirFighter/ExtentionAirFighter.cs new file mode 100644 index 0000000..f831a1e --- /dev/null +++ b/AirFighter/AirFighter/ExtentionAirFighter.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirFighter.Entities; + +namespace AirFighter.DrawningObjects +{ + public static class ExtentionDrawningAirFighter + { + public static DrawningAirFighter? CreateDrawningAirFighter(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningAirFighter(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningAirFighterMilitary(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 DrawningAirFighter drawningAirFighter, + char separatorForObject) + { + var fighter = drawningAirFighter.EntityAirFighter; + if (fighter == null) + { + return string.Empty; + } + var str = + $"{fighter.Speed}{separatorForObject}{fighter.Weight}{separatorForObject}{fighter.BodyColor.Name}"; + if (fighter is not EntityAirFighterMilitary fighterMilitary) + { + return str; + } + return + $"{str}{separatorForObject}{fighterMilitary.AdditionalColor.Name}{separatorForObject}{fighterMilitary.Wing}{separatorForObject}{fighterMilitary.Rocket}"; + } + } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs b/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs index b440b6d..918c733 100644 --- a/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs +++ b/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs @@ -39,9 +39,16 @@ buttonRefreshCollection = new Button(); buttonAddFighter = new Button(); pictureBoxCollection = new PictureBox(); + menuStrip1 = new MenuStrip(); + fileToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog1 = new OpenFileDialog(); + saveFileDialog1 = new SaveFileDialog(); groupBox1.SuspendLayout(); groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + menuStrip1.SuspendLayout(); SuspendLayout(); // // groupBox1 @@ -149,29 +156,73 @@ // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Location = new Point(0, 27); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(734, 448); + pictureBoxCollection.Size = new Size(734, 421); pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom; pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; pictureBoxCollection.Click += ButtonAddAirFighter_Click; // - // FormFighterCollection + // menuStrip1 + // + menuStrip1.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(953, 24); + menuStrip1.TabIndex = 2; + menuStrip1.Text = "menuStrip1"; + // + // fileToolStripMenuItem + // + fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + fileToolStripMenuItem.Size = new Size(48, 20); + fileToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(180, 22); + SaveToolStripMenuItem.Text = "Сохранить"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(180, 22); + LoadToolStripMenuItem.Text = "Загрузить"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // openFileDialog1 + // + openFileDialog1.FileName = "openFileDialog1"; + openFileDialog1.Filter = "txt file | *.txt"; + // + // saveFileDialog1 + // + saveFileDialog1.Filter = "txt file | *.txt"; + // + // FormAirFighterCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(953, 448); Controls.Add(pictureBoxCollection); Controls.Add(groupBox1); - Name = "FormFighterCollection"; + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; + Name = "FormAirFighterCollection"; Text = "Набор истребителя"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); groupBox2.ResumeLayout(false); groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -187,5 +238,11 @@ private Button buttonDelObject; private ListBox listBoxStorage; private Button buttonAddObject; + private MenuStrip menuStrip1; + private ToolStripMenuItem fileToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog1; + private SaveFileDialog saveFileDialog1; } } \ No newline at end of file diff --git a/AirFighter/AirFighter/FormAirFighterCollection.cs b/AirFighter/AirFighter/FormAirFighterCollection.cs index def6fe4..27006b7 100644 --- a/AirFighter/AirFighter/FormAirFighterCollection.cs +++ b/AirFighter/AirFighter/FormAirFighterCollection.cs @@ -7,7 +7,7 @@ using AirFighter.MovementStrategy; namespace AirFighter -{ +{ /// /// Форма для работы с набором объектов класса DrawningAirFighter /// @@ -126,10 +126,6 @@ namespace AirFighter } private void ButtonAddAirFighter_Click(object sender, EventArgs e) { - if (listBoxStorage.SelectedIndex == -1) - { - return; - } var formAirFighterConfig = new FormAirFighterConfig(); formAirFighterConfig.AddEvent(addAirFighter); formAirFighterConfig.Show(); @@ -190,7 +186,40 @@ namespace AirFighter pictureBoxCollection.Image = obj.ShowAirFighter(); } + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog1.ShowDialog() == DialogResult.OK) + { + if (_storage.SaveData(saveFileDialog1.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) + { + if (openFileDialog1.ShowDialog() == DialogResult.OK) + { + if (_storage.LoadData(openFileDialog1.FileName)) + { + ReloadObjects(); + MessageBox.Show("Загрузка прошла успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/AirFighter/AirFighter/FormAirFighterCollection.resx b/AirFighter/AirFighter/FormAirFighterCollection.resx index a395bff..eb2673c 100644 --- a/AirFighter/AirFighter/FormAirFighterCollection.resx +++ b/AirFighter/AirFighter/FormAirFighterCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 132, 17 + + + 272, 17 + \ No newline at end of file diff --git a/AirFighter/AirFighter/FormAirFighterConfig.Designer.cs b/AirFighter/AirFighter/FormAirFighterConfig.Designer.cs index 8e92c71..04887ae 100644 --- a/AirFighter/AirFighter/FormAirFighterConfig.Designer.cs +++ b/AirFighter/AirFighter/FormAirFighterConfig.Designer.cs @@ -119,7 +119,7 @@ // // panelPurple // - panelPurple.BackColor = Color.FromArgb(192, 0, 192); + panelPurple.BackColor = Color.Purple; panelPurple.Location = new Point(139, 63); panelPurple.Name = "panelPurple"; panelPurple.Size = new Size(35, 30); @@ -164,7 +164,7 @@ // // panelGreen // - panelGreen.BackColor = Color.FromArgb(0, 192, 0); + panelGreen.BackColor = Color.Green; panelGreen.Location = new Point(57, 22); panelGreen.Name = "panelGreen"; panelGreen.Size = new Size(35, 30); @@ -311,7 +311,7 @@ buttonCancel.Text = "Отмена"; buttonCancel.UseVisualStyleBackColor = true; // - // FormFighterConfig + // FormAirFighterConfig // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; @@ -320,9 +320,8 @@ Controls.Add(ButtonOk); Controls.Add(panelColor); Controls.Add(groupBox1); - Name = "FormFighterConfig"; + Name = "FormAirFighterConfig"; Text = "FormFighterConfig"; - groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); groupBox2.ResumeLayout(false); diff --git a/AirFighter/AirFighter/SetGeneric.cs b/AirFighter/AirFighter/SetGeneric.cs index d3b6d5e..9910b40 100644 --- a/AirFighter/AirFighter/SetGeneric.cs +++ b/AirFighter/AirFighter/SetGeneric.cs @@ -1,4 +1,6 @@ -namespace AirFighter.Generics +using System.Numerics; + +namespace AirFighter.Generics { internal class SetGeneric where T : class @@ -18,8 +20,7 @@ } public int Insert(T fighter, int position) { - if (position < 0 || position > Count || Count >= _maxCount) - return -1; + if (position < 0 || position >= _maxCount) return -1; _places.Insert(position, fighter); return position; }