From 1778910e976dfeb52f7816df8040b7fdab64a7a2 Mon Sep 17 00:00:00 2001 From: urlilpolly Date: Sat, 2 Dec 2023 19:33:16 +0400 Subject: [PATCH] laba6 --- RPP/RPP/AirbusGenericCollection.cs | 2 +- RPP/RPP/AirbusGenericStorage.cs | 77 ++++++++++++++++++++++ RPP/RPP/ExtentionDrawningAirbus.cs | 50 ++++++++++++++ RPP/RPP/FormAirbusCollection.Designer.cs | 83 ++++++++++++++++++++---- RPP/RPP/FormAirbusCollection.cs | 37 ++++++++++- RPP/RPP/FormAirbusCollection.resx | 9 +++ RPP/RPP/SetGeneric.cs | 6 +- 7 files changed, 247 insertions(+), 17 deletions(-) create mode 100644 RPP/RPP/ExtentionDrawningAirbus.cs diff --git a/RPP/RPP/AirbusGenericCollection.cs b/RPP/RPP/AirbusGenericCollection.cs index 317749f..8ae67a9 100644 --- a/RPP/RPP/AirbusGenericCollection.cs +++ b/RPP/RPP/AirbusGenericCollection.cs @@ -17,7 +17,7 @@ namespace RPP.Generics private readonly int _placeSizeHeight = 120; private readonly SetGeneric _collection; - + public IEnumerable GetAirbus => _collection.GetAirbus(); public AirbusGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; diff --git a/RPP/RPP/AirbusGenericStorage.cs b/RPP/RPP/AirbusGenericStorage.cs index 8b0b220..799d56b 100644 --- a/RPP/RPP/AirbusGenericStorage.cs +++ b/RPP/RPP/AirbusGenericStorage.cs @@ -19,6 +19,9 @@ namespace RPP.Generics private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; public AirbusGenericStorage(int pictureWidth, int pictureHeight) { _airbusStorages = new Dictionary> record in _airbusStorages) + { + StringBuilder records = new(); + foreach (DrawningAirbus? elem in record.Value.GetAirbus) + { + 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.WriteLine("AirbusStorage"); + writer.Write(data.ToString()); + return true; + } + + } + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader reader = new StreamReader(filename)) + { + string checker = reader.ReadLine(); + if (checker == null) + return false; + if (!checker.StartsWith("AirbusStorage")) + return false; + _airbusStorages.Clear(); + string strs; + bool firstinit = true; + while ((strs = reader.ReadLine()) != null) + { + if (strs == null && firstinit) + return false; + if (strs == null) + break; + firstinit = false; + string name = strs.Split('|')[0]; + AirbusGenericCollection collection = new(_pictureWidth, _pictureHeight); + foreach (string data in strs.Split('|')[1].Split(';')) + { + DrawningAirbus? car = + data?.CreateDrawningAirbus(_separatorForObject, _pictureWidth, _pictureHeight); + if (car != null) + { + if (!(collection + car)) + { + return false; + } + } + + + } + _airbusStorages.Add(name, collection); + } + return true; + } + } } } diff --git a/RPP/RPP/ExtentionDrawningAirbus.cs b/RPP/RPP/ExtentionDrawningAirbus.cs new file mode 100644 index 0000000..49da344 --- /dev/null +++ b/RPP/RPP/ExtentionDrawningAirbus.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RPP.Entities; + +namespace RPP.DrawningObjects +{ + public static class ExtentionDrawningAirbus + { + public static DrawningAirbus? CreateDrawningAirbus(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningAirbus(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningFlyAirbus(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 DrawningAirbus drawningAirbus, + char separatorForObject) + { + var plane = drawningAirbus._EntityAirbus; + if (plane == null) + { + return string.Empty; + } + var str = + $"{plane.Speed}{separatorForObject}{plane.Weight}{separatorForObject}{plane.BodyColor.Name}"; + if (plane is not EntityFlyAirbus FlyAirbus) + { + return str; + } + return + $"{str}{separatorForObject}{FlyAirbus.AdditionalColor.Name}{separatorForObject}{FlyAirbus.Compartment}{separatorForObject}{FlyAirbus.Engine}"; + } + } +} diff --git a/RPP/RPP/FormAirbusCollection.Designer.cs b/RPP/RPP/FormAirbusCollection.Designer.cs index 349195b..7860e1d 100644 --- a/RPP/RPP/FormAirbusCollection.Designer.cs +++ b/RPP/RPP/FormAirbusCollection.Designer.cs @@ -35,14 +35,21 @@ buttonAddObject = new Button(); textBoxStorageName = new TextBox(); label2 = new Label(); + label1 = new Label(); ButtonRefreshCollection = new Button(); ButtonRemoveAirbus = new Button(); maskedTextBoxNumber = new TextBox(); AddAirbusButton = new Button(); - label1 = new Label(); + menuStrip = new MenuStrip(); + menuToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); pictureBoxCollection = new PictureBox(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); panel1.SuspendLayout(); panel2.SuspendLayout(); + menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // @@ -53,13 +60,14 @@ panel1.Controls.Add(ButtonRemoveAirbus); panel1.Controls.Add(maskedTextBoxNumber); panel1.Controls.Add(AddAirbusButton); - panel1.Controls.Add(label1); + panel1.Controls.Add(menuStrip); panel1.Dock = DockStyle.Right; - panel1.Location = new Point(685, 0); + panel1.Location = new Point(682, 0); panel1.Margin = new Padding(3, 4, 3, 4); panel1.Name = "panel1"; - panel1.Size = new Size(229, 600); + panel1.Size = new Size(232, 600); panel1.TabIndex = 0; + panel1.Paint += panel1_Paint; // // panel2 // @@ -68,6 +76,7 @@ panel2.Controls.Add(buttonAddObject); panel2.Controls.Add(textBoxStorageName); panel2.Controls.Add(label2); + panel2.Controls.Add(label1); panel2.Location = new Point(3, 36); panel2.Margin = new Padding(3, 4, 3, 4); panel2.Name = "panel2"; @@ -126,9 +135,17 @@ label2.Text = "Наборы"; label2.Click += label2_Click; // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(117, 1); + label1.Name = "label1"; + label1.Size = new Size(0, 20); + label1.TabIndex = 0; + // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(6, 543); + ButtonRefreshCollection.Location = new Point(8, 531); ButtonRefreshCollection.Margin = new Padding(3, 4, 3, 4); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(217, 53); @@ -167,14 +184,37 @@ AddAirbusButton.UseVisualStyleBackColor = true; AddAirbusButton.Click += AddAirbusButton_Click; // - // label1 + // menuStrip // - label1.AutoSize = true; - label1.Location = new Point(6, 0); - label1.Name = "label1"; - label1.Size = new Size(103, 20); - label1.TabIndex = 0; - label1.Text = "Инструменты"; + menuStrip.ImageScalingSize = new Size(20, 20); + menuStrip.Items.AddRange(new ToolStripItem[] { menuToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Padding = new Padding(7, 3, 0, 3); + menuStrip.Size = new Size(232, 30); + menuStrip.TabIndex = 6; + menuStrip.Text = "menuStrip1"; + // + // menuToolStripMenuItem + // + menuToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + menuToolStripMenuItem.Name = "menuToolStripMenuItem"; + menuToolStripMenuItem.Size = new Size(65, 24); + menuToolStripMenuItem.Text = "Меню"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(224, 26); + SaveToolStripMenuItem.Text = "Сохранить"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(224, 26); + LoadToolStripMenuItem.Text = "Загрузить"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // // pictureBoxCollection // @@ -182,10 +222,18 @@ pictureBoxCollection.Location = new Point(0, 0); pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(685, 600); + pictureBoxCollection.Size = new Size(682, 600); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + // // FormAirbusCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -193,6 +241,7 @@ ClientSize = new Size(914, 600); Controls.Add(pictureBoxCollection); Controls.Add(panel1); + MainMenuStrip = menuStrip; Margin = new Padding(3, 4, 3, 4); Name = "FormAirbusCollection"; Text = "FormFlyAirbus"; @@ -201,6 +250,8 @@ panel1.PerformLayout(); panel2.ResumeLayout(false); panel2.PerformLayout(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } @@ -220,5 +271,11 @@ private ListBox listBoxStorages; private Button buttonAddObject; private Button buttonDelObject; + private MenuStrip menuStrip; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; + private ToolStripMenuItem menuToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; } } \ No newline at end of file diff --git a/RPP/RPP/FormAirbusCollection.cs b/RPP/RPP/FormAirbusCollection.cs index e4587cd..d4c8c70 100644 --- a/RPP/RPP/FormAirbusCollection.cs +++ b/RPP/RPP/FormAirbusCollection.cs @@ -1,6 +1,6 @@ using RPP.DrawningObjects; using RPP.Generics; -using RPP.MovementStrategy; +using System.Windows.Forms; namespace RPP @@ -146,6 +146,36 @@ namespace RPP } pictureBoxCollection.Image = obj.ShowCars(); } + 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)) + { + ReloadObjects(); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } private void panel2_Paint(object sender, PaintEventArgs e) { @@ -156,5 +186,10 @@ namespace RPP { } + + private void panel1_Paint(object sender, PaintEventArgs e) + { + + } } } diff --git a/RPP/RPP/FormAirbusCollection.resx b/RPP/RPP/FormAirbusCollection.resx index a395bff..6afff5b 100644 --- a/RPP/RPP/FormAirbusCollection.resx +++ b/RPP/RPP/FormAirbusCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 291, 17 + + + 17, 17 + + + 152, 17 + \ No newline at end of file diff --git a/RPP/RPP/SetGeneric.cs b/RPP/RPP/SetGeneric.cs index 0487579..8a81e38 100644 --- a/RPP/RPP/SetGeneric.cs +++ b/RPP/RPP/SetGeneric.cs @@ -9,8 +9,6 @@ namespace RPP.Generics { public class SetGeneric where T : class { - - private readonly List _places; public int Count => _places.Count; @@ -63,12 +61,16 @@ namespace RPP.Generics { if (position < 0 || position > _maxCount) return null; + if (_places.Count <= position) + return null; return _places[position]; } set { if (position < 0 || position > _maxCount) return; + if (_places.Count <= position) + return; _places[position] = value; } }