From 31f294023d4f774301b9924dbaa7599685075c3d Mon Sep 17 00:00:00 2001 From: Anna Date: Sat, 2 Dec 2023 00:24:08 +0400 Subject: [PATCH] laba 4 --- .../FormAirplaneCollection.Designer.cs | 154 ++++++++++++++---- Airbus_Base/FormAirplaneCollection.cs | 118 +++++++++++++- Airbus_Base/FormAirplaneCollection.resx | 62 +------ Airbus_Base/Generics/SetGeneric.cs | 96 ++++++----- .../Generics/TheAirplaneGenericStorage.cs | 89 ++++++++++ .../Generics/TheAirplanesGenericCollection.cs | 29 ++-- 6 files changed, 395 insertions(+), 153 deletions(-) create mode 100644 Airbus_Base/Generics/TheAirplaneGenericStorage.cs diff --git a/Airbus_Base/FormAirplaneCollection.Designer.cs b/Airbus_Base/FormAirplaneCollection.Designer.cs index 5566cf7..72ce97c 100644 --- a/Airbus_Base/FormAirplaneCollection.Designer.cs +++ b/Airbus_Base/FormAirplaneCollection.Designer.cs @@ -28,26 +28,98 @@ /// private void InitializeComponent() { - this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.toolsPanel = new System.Windows.Forms.Panel(); + this.panelSets = new System.Windows.Forms.Panel(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.listBoxObjects = new System.Windows.Forms.ListBox(); + this.ButtonAddObject = new System.Windows.Forms.Button(); + this.ButtonDelObject = new System.Windows.Forms.Button(); + this.SetsLabel = new System.Windows.Forms.Label(); this.ButtonAddAirplane = new System.Windows.Forms.Button(); this.ButtonDeleteAirplane = new System.Windows.Forms.Button(); - this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); this.ButtonRefreshCollection = new System.Windows.Forms.Button(); - this.label1 = new System.Windows.Forms.Label(); + this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); + this.LabelTools = new System.Windows.Forms.Label(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.toolsPanel.SuspendLayout(); + this.panelSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.SuspendLayout(); // - // pictureBoxCollection + // toolsPanel // - this.pictureBoxCollection.Location = new System.Drawing.Point(1, -2); - this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(676, 451); - this.pictureBoxCollection.TabIndex = 0; - this.pictureBoxCollection.TabStop = false; + this.toolsPanel.Controls.Add(this.panelSets); + this.toolsPanel.Controls.Add(this.ButtonAddAirplane); + this.toolsPanel.Controls.Add(this.ButtonDeleteAirplane); + this.toolsPanel.Controls.Add(this.ButtonRefreshCollection); + this.toolsPanel.Controls.Add(this.maskedTextBoxNumber); + this.toolsPanel.Controls.Add(this.LabelTools); + this.toolsPanel.Location = new System.Drawing.Point(675, -2); + this.toolsPanel.Name = "toolsPanel"; + this.toolsPanel.Size = new System.Drawing.Size(220, 451); + this.toolsPanel.TabIndex = 0; + // + // panelSets + // + this.panelSets.Controls.Add(this.textBoxStorageName); + this.panelSets.Controls.Add(this.listBoxObjects); + this.panelSets.Controls.Add(this.ButtonAddObject); + this.panelSets.Controls.Add(this.ButtonDelObject); + this.panelSets.Controls.Add(this.SetsLabel); + this.panelSets.Location = new System.Drawing.Point(8, 20); + this.panelSets.Name = "panelSets"; + this.panelSets.Size = new System.Drawing.Size(210, 208); + this.panelSets.TabIndex = 6; + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(10, 27); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(190, 27); + this.textBoxStorageName.TabIndex = 12; + // + // listBoxObjects + // + this.listBoxObjects.FormattingEnabled = true; + this.listBoxObjects.ItemHeight = 20; + this.listBoxObjects.Location = new System.Drawing.Point(10, 99); + this.listBoxObjects.Name = "listBoxObjects"; + this.listBoxObjects.Size = new System.Drawing.Size(190, 64); + this.listBoxObjects.TabIndex = 11; + this.listBoxObjects.Click += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged); + // + // ButtonAddObject + // + this.ButtonAddObject.Location = new System.Drawing.Point(10, 60); + this.ButtonAddObject.Name = "ButtonAddObject"; + this.ButtonAddObject.Size = new System.Drawing.Size(191, 33); + this.ButtonAddObject.TabIndex = 8; + this.ButtonAddObject.Text = "Добавить набор"; + this.ButtonAddObject.UseVisualStyleBackColor = true; + this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // + // ButtonDelObject + // + this.ButtonDelObject.Location = new System.Drawing.Point(10, 169); + this.ButtonDelObject.Name = "ButtonDelObject"; + this.ButtonDelObject.Size = new System.Drawing.Size(192, 33); + this.ButtonDelObject.TabIndex = 7; + this.ButtonDelObject.Text = "Удалить набор\r\n"; + this.ButtonDelObject.UseVisualStyleBackColor = true; + this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); + // + // SetsLabel + // + this.SetsLabel.AutoSize = true; + this.SetsLabel.Location = new System.Drawing.Point(13, 4); + this.SetsLabel.Name = "SetsLabel"; + this.SetsLabel.Size = new System.Drawing.Size(66, 20); + this.SetsLabel.TabIndex = 0; + this.SetsLabel.Text = "Наборы"; // // ButtonAddAirplane // - this.ButtonAddAirplane.Location = new System.Drawing.Point(750, 77); + this.ButtonAddAirplane.Location = new System.Drawing.Point(61, 234); this.ButtonAddAirplane.Name = "ButtonAddAirplane"; this.ButtonAddAirplane.Size = new System.Drawing.Size(103, 49); this.ButtonAddAirplane.TabIndex = 1; @@ -57,7 +129,7 @@ // // ButtonDeleteAirplane // - this.ButtonDeleteAirplane.Location = new System.Drawing.Point(750, 212); + this.ButtonDeleteAirplane.Location = new System.Drawing.Point(61, 301); this.ButtonDeleteAirplane.Name = "ButtonDeleteAirplane"; this.ButtonDeleteAirplane.Size = new System.Drawing.Size(103, 55); this.ButtonDeleteAirplane.TabIndex = 2; @@ -65,16 +137,9 @@ this.ButtonDeleteAirplane.UseVisualStyleBackColor = true; this.ButtonDeleteAirplane.Click += new System.EventHandler(this.ButtonDeleteAirplane_Click); // - // maskedTextBoxNumber - // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(750, 154); - this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(103, 27); - this.maskedTextBoxNumber.TabIndex = 3; - // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(750, 299); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(61, 395); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Size = new System.Drawing.Size(103, 56); this.ButtonRefreshCollection.TabIndex = 4; @@ -82,41 +147,62 @@ this.ButtonRefreshCollection.UseVisualStyleBackColor = true; this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); // - // label1 + // maskedTextBoxNumber // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(750, 31); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(103, 20); - this.label1.TabIndex = 5; - this.label1.Text = "Инструменты"; + this.maskedTextBoxNumber.Location = new System.Drawing.Point(48, 362); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(125, 27); + this.maskedTextBoxNumber.TabIndex = 2; + // + // LabelTools + // + this.LabelTools.AutoSize = true; + this.LabelTools.Location = new System.Drawing.Point(18, 0); + this.LabelTools.Name = "LabelTools"; + this.LabelTools.Size = new System.Drawing.Size(103, 20); + this.LabelTools.TabIndex = 0; + this.LabelTools.Text = "Инструменты"; + // + // pictureBoxCollection + // + this.pictureBoxCollection.Location = new System.Drawing.Point(1, -2); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(676, 451); + this.pictureBoxCollection.TabIndex = 0; + this.pictureBoxCollection.TabStop = false; // // FormAirplaneCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(900, 450); - this.Controls.Add(this.label1); - this.Controls.Add(this.ButtonRefreshCollection); - this.Controls.Add(this.maskedTextBoxNumber); - this.Controls.Add(this.ButtonDeleteAirplane); - this.Controls.Add(this.ButtonAddAirplane); this.Controls.Add(this.pictureBoxCollection); + this.Controls.Add(this.toolsPanel); this.Name = "FormAirplaneCollection"; this.Text = "FormAirplaneCollection"; + this.toolsPanel.ResumeLayout(false); + this.toolsPanel.PerformLayout(); + this.panelSets.ResumeLayout(false); + this.panelSets.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); - this.PerformLayout(); } #endregion + private Panel toolsPanel; + private Label LabelTools; private PictureBox pictureBoxCollection; private Button ButtonAddAirplane; private Button ButtonDeleteAirplane; - private MaskedTextBox maskedTextBoxNumber; + public TextBox maskedTextBoxNumber; + private Panel panelSets; + private Label SetsLabel; + private Button ButtonDelObject; + private Button ButtonAddObject; private Button ButtonRefreshCollection; - private Label label1; + private ListBox listBoxObjects; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/Airbus_Base/FormAirplaneCollection.cs b/Airbus_Base/FormAirplaneCollection.cs index dfe1fba..53b0aaa 100644 --- a/Airbus_Base/FormAirplaneCollection.cs +++ b/Airbus_Base/FormAirplaneCollection.cs @@ -21,7 +21,7 @@ namespace Airbus_Base /// /// Набор объектов /// - private readonly TheAirplanesGenericCollection _theAirplanes; + private readonly TheAirplaneGenericStorage _storage; /// /// Конструктор @@ -29,8 +29,78 @@ namespace Airbus_Base public FormAirplaneCollection() { InitializeComponent(); - _theAirplanes = new TheAirplanesGenericCollection - (pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new TheAirplaneGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + int index = listBoxObjects.SelectedIndex; + + listBoxObjects.Items.Clear(); + + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxObjects.Items.Add(_storage.Keys[i]); + } + + if (listBoxObjects.Items.Count > 0 && (index == -1 || index >= listBoxObjects.Items.Count)) + { + listBoxObjects.SelectedIndex = 0; + } + + else if (listBoxObjects.Items.Count > 0 && index > -1 && index < listBoxObjects.Items.Count) + { + listBoxObjects.SelectedIndex = index; + } + } + + /// + /// Добавление набора в коллекцию + /// + /// + /// + private void ButtonAddObject_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + + /// + /// Выбор набора + /// + /// + /// + private void listBoxObjects_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxObjects.SelectedItem?.ToString() ?? string.Empty]?.ShowTheAirplanes(); + } + + /// + /// Удаление набора + /// + /// + /// + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (listBoxObjects.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект{listBoxObjects.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxObjects.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } } /// @@ -40,13 +110,22 @@ namespace Airbus_Base /// private void ButtonAddAirplane_Click(object sender, EventArgs e) { + if (listBoxObjects.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } FormAirbus form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_theAirplanes + form.SelectedAirplane != -1) + if (obj + form.SelectedAirplane) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _theAirplanes.ShowTheAirplanes(); + pictureBoxCollection.Image = obj.ShowTheAirplanes(); } else { @@ -62,17 +141,28 @@ namespace Airbus_Base /// private void ButtonDeleteAirplane_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxObjects.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_theAirplanes - pos != true) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _theAirplanes.ShowTheAirplanes(); + pictureBoxCollection.Image = obj.ShowTheAirplanes(); } else { @@ -87,7 +177,17 @@ namespace Airbus_Base /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _theAirplanes.ShowTheAirplanes(); + if (listBoxObjects.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowTheAirplanes(); } } } diff --git a/Airbus_Base/FormAirplaneCollection.resx b/Airbus_Base/FormAirplaneCollection.resx index 1af7de1..f298a7b 100644 --- a/Airbus_Base/FormAirplaneCollection.resx +++ b/Airbus_Base/FormAirplaneCollection.resx @@ -1,64 +1,4 @@ - - - + diff --git a/Airbus_Base/Generics/SetGeneric.cs b/Airbus_Base/Generics/SetGeneric.cs index 8dea268..707d9b2 100644 --- a/Airbus_Base/Generics/SetGeneric.cs +++ b/Airbus_Base/Generics/SetGeneric.cs @@ -14,14 +14,19 @@ namespace Airbus_Base.Generics where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в cписке /// - public int Count => _places.Length; + public int Count => _places.Count; + + /// + /// Максимальное количество объектов в cписке + /// + private readonly int _maxCount; /// /// Конструктор @@ -29,16 +34,23 @@ namespace Airbus_Base.Generics /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор /// /// Добавляемый самолёт /// - public int Insert(T airplane) + public bool Insert(T airplane) { - return Insert(airplane, 0); + if (_places.Count == _maxCount) + { + return false; + } + + Insert(airplane, 0); + return true; } /// @@ -47,36 +59,15 @@ namespace Airbus_Base.Generics /// Добавляемый самолёт /// Позиция /// - public int Insert(T airplane, int position) + public bool Insert(T airplane, int position) { - int nullIndex = -1, i; - - if (position < 0 || position >= Count) + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) { - return -1; + return false; } - for (i = position; i < Count; i++) - { - if (_places[i] == null) - { - nullIndex = i; - break; - } - } - - if (nullIndex < 0) - { - return -1; - } - - for (i = nullIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - - _places[position] = airplane; - return position; + _places.Insert(position, airplane); + return true; } /// @@ -91,7 +82,7 @@ namespace Airbus_Base.Generics return false; } - _places[position] = null; + _places.RemoveAt(position); return true; } @@ -100,14 +91,43 @@ namespace Airbus_Base.Generics /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= Count) + get { - return null; + if (position < 0 || position >= _maxCount) + { + return null; + } + + return _places[position]; } - return _places[position]; + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + { + return; + } + + _places.Insert(position, value); + return; + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetTheAirplanes(int? maxTheAirplanes = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxTheAirplanes.HasValue && i == maxTheAirplanes.Value) + { + yield break; + } + } } } } diff --git a/Airbus_Base/Generics/TheAirplaneGenericStorage.cs b/Airbus_Base/Generics/TheAirplaneGenericStorage.cs new file mode 100644 index 0000000..edabd4a --- /dev/null +++ b/Airbus_Base/Generics/TheAirplaneGenericStorage.cs @@ -0,0 +1,89 @@ +using Airbus_Base.DrawningObjects; +using Airbus_Base.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Airbus_Base.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class TheAirplaneGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _airplaneStorages; + + /// + /// Возвращение списка названий наборов + /// + public List Keys => _airplaneStorages.Keys.ToList(); + + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + + /// + /// Конструктор + /// + /// + /// + public TheAirplaneGenericStorage(int pictureWidth, int pictureHeight) + { + _airplaneStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + _airplaneStorages.Add(name, new TheAirplanesGenericCollection(_pictureWidth, _pictureHeight)); + } + + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_airplaneStorages.ContainsKey(name)) + { + return; + } + + _airplaneStorages.Remove(name); + } + + /// + /// Доступ к набору + /// + /// + /// + public TheAirplanesGenericCollection? this[string ind] + { + get + { + if (_airplaneStorages.ContainsKey(ind)) + { + return _airplaneStorages[ind]; + } + + return null; + } + } + } +} diff --git a/Airbus_Base/Generics/TheAirplanesGenericCollection.cs b/Airbus_Base/Generics/TheAirplanesGenericCollection.cs index 3358166..b5a01b0 100644 --- a/Airbus_Base/Generics/TheAirplanesGenericCollection.cs +++ b/Airbus_Base/Generics/TheAirplanesGenericCollection.cs @@ -62,13 +62,13 @@ namespace Airbus_Base.Generics /// /// /// - public static int operator +(TheAirplanesGenericCollection collect, T? obj) + public static bool operator +(TheAirplanesGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect._collection.Insert(obj); + return collect?._collection.Insert(obj) ?? false; } /// @@ -77,14 +77,14 @@ namespace Airbus_Base.Generics /// /// /// - public static bool operator -(TheAirplanesGenericCollection collect, int pos) + public static T? operator -(TheAirplanesGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); } - return false; + return obj; } /// @@ -94,7 +94,7 @@ namespace Airbus_Base.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// @@ -135,14 +135,21 @@ namespace Airbus_Base.Generics ///