diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs index 35566e4..02fbcbe 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs @@ -67,14 +67,8 @@ namespace AirplaneWithRadar { bodyColor = dialog.Color; } - Color additionalColor = Color.FromArgb(random.Next(0, 256), - random.Next(0, 256), random.Next(0, 256)); - if (dialog.ShowDialog() == DialogResult.OK) - { - additionalColor = dialog.Color; - } _drawningAirplane = new DrawningAirplane(random.Next(100, 300), random.Next(1000, 3000), - bodyColor, additionalColor, + bodyColor, pictureBoxAirplane.Width, pictureBoxAirplane.Height); _drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs index da11b7b..a65fb60 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs @@ -56,13 +56,13 @@ namespace AirplaneWithRadar.Generics /// /// /// - public static int operator +(AirplanesGenericCollection collect, T? obj) + public static bool operator +(AirplanesGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj) ?? -1; + return (bool)collect?._collection.Insert(obj); } /// /// Перегрузка оператора вычитания @@ -73,7 +73,7 @@ namespace AirplaneWithRadar.Generics public static bool operator -(AirplanesGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); @@ -87,7 +87,7 @@ namespace AirplaneWithRadar.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -126,25 +126,18 @@ namespace AirplaneWithRadar.Generics /// private void DrawObjects(Graphics g) { - T? t; - int Rows = _pictureWidth / _placeSizeWidth; - int diff = 1; - int currWidth = 0; - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var airplane in _collection.GetAirplanes()) { - currWidth++; - if (currWidth > Rows) + if (airplane!= null) { - diff++; - currWidth = 1; - } - t = _collection.Get(i); - if (t != null) - { - - t.SetPosition((Rows - 1 - (i % Rows)) * _placeSizeWidth, i / Rows * _placeSizeHeight); - t.DrawTransport(g); + airplane.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + if (airplane is DrawningAirplaneWithRadar) + (airplane as DrawningAirplaneWithRadar).DrawTransport(g); + else airplane.DrawTransport(g); + airplane.DrawTransport(g); } + i++; } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs new file mode 100644 index 0000000..c15272c --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirplaneWithRadar.DrawningObjects; +using AirplaneWithRadar.MovementStrategy; + +namespace AirplaneWithRadar.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class AirplanesGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _airplaneStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _airplaneStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public AirplanesGenericStorage(int pictureWidth, int pictureHeight) + { + _airplaneStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (_airplaneStorages.ContainsKey(name)) + return; + _airplaneStorages[name] = new AirplanesGenericCollection(_pictureWidth, _pictureHeight); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_airplaneStorages.ContainsKey(name)) + return; + _airplaneStorages.Remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public AirplanesGenericCollection? + this[string ind] + { + get + { + if (_airplaneStorages.ContainsKey(ind)) + return _airplaneStorages[ind]; + return null; + } + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs index 1729e2c..08e9c39 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs @@ -52,12 +52,10 @@ namespace AirplaneWithRadar.DrawningObjects /// Скорость /// Вес /// Цвет фюзеляжа - /// Дополнительный цвет /// Ширина картинки /// Высота картинки /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public DrawningAirplane(int speed, double weight, Color bodyColor, Color - additionalColor, int width, int height) + public DrawningAirplane(int speed, double weight, Color bodyColor, int width, int height) { _pictureWidth = width; _pictureHeight = height; @@ -65,8 +63,7 @@ namespace AirplaneWithRadar.DrawningObjects { return; } - EntityAirplane = new EntityAirplane(speed, weight, bodyColor, - additionalColor); + EntityAirplane = new EntityAirplane(speed, weight, bodyColor); } /// /// Конструктор @@ -78,7 +75,7 @@ namespace AirplaneWithRadar.DrawningObjects /// Высота картинки /// Ширина прорисовки автомобиля /// Высота прорисовки автомобиля - protected DrawningAirplane(int speed, double weight, Color bodyColor, Color additionalColor, int + protected DrawningAirplane(int speed, double weight, Color bodyColor, int width, int height, int airplaneWidth, int airplaneHeight) { _pictureWidth = width; @@ -89,7 +86,7 @@ namespace AirplaneWithRadar.DrawningObjects { return; } - EntityAirplane = new EntityAirplane(speed, weight, bodyColor, additionalColor); + EntityAirplane = new EntityAirplane(speed, weight, bodyColor); } /// /// Установка позиции @@ -194,7 +191,6 @@ namespace AirplaneWithRadar.DrawningObjects return; } Pen penBlack = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityAirplane.AdditionalColor); Brush bodyBrush = new SolidBrush(EntityAirplane.BodyColor); //фюзеляж @@ -202,7 +198,7 @@ namespace AirplaneWithRadar.DrawningObjects g.DrawRectangle(penBlack, _startPosX + 4, _startPosY + 40, 150, 30); //киль - g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) }); + g.FillPolygon(bodyBrush, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) }); g.DrawPolygon(penBlack, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) }); //стабилизатор и крыло @@ -225,7 +221,7 @@ namespace AirplaneWithRadar.DrawningObjects //кабина Brush brLightBlue = new SolidBrush(Color.LightBlue); Pen penBlue = new Pen(Color.CadetBlue); - g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 150, _startPosY + 55), new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 72) }); + g.FillPolygon(bodyBrush, new Point[] { new Point(_startPosX + 150, _startPosY + 55), new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 72) }); g.FillPolygon(brLightBlue, new Point[] { new Point(_startPosX + 150, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 38), new Point(_startPosX + 190, _startPosY + 55) }); g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 38)); g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 38), new Point(_startPosX + 190, _startPosY + 55)); diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs index 96498a9..59b75b9 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs @@ -24,7 +24,7 @@ namespace AirplaneWithRadar.DrawningObjects /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах public DrawningAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor, bool radar, bool tank, bool pin, int width, int height) : - base(speed, weight, bodyColor, additionalColor, width, height) + base(speed, weight, bodyColor, width, height, 200, 85) { if (EntityAirplane != null) { @@ -39,8 +39,7 @@ namespace AirplaneWithRadar.DrawningObjects return; } Pen penBlack = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityAirplane.AdditionalColor); - Brush bodyBrush = new SolidBrush(EntityAirplane.BodyColor); + Brush additionalBrush = new SolidBrush(airplaneWithRadar.AdditionalColor); Pen penGray = new Pen(Color.Gray); // радар diff --git a/AirplaneWithRadar/AirplaneWithRadar/EntityAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplane.cs index 6eb305a..4791138 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/EntityAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplane.cs @@ -24,10 +24,6 @@ namespace AirplaneWithRadar.Entities /// public Color BodyColor { get; private set; } /// - /// Дополнительный цвет (для опциональных элементов) - /// - public Color AdditionalColor { get; private set; } - /// /// Шаг перемещения самолета /// public double Step => (double)Speed * 100 / Weight; @@ -37,14 +33,11 @@ namespace AirplaneWithRadar.Entities /// Скорость /// Вес самолета /// Основной цвет - /// Дополнительный цвет - public EntityAirplane(int speed, double weight, Color bodyColor, Color - additionalColor) + public EntityAirplane(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; - AdditionalColor = additionalColor; } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs index 1c29893..6ded1b0 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs @@ -11,6 +11,10 @@ namespace AirplaneWithRadar.Entities /// public class EntityAirplaneWithRadar : EntityAirplane { + /// + /// Дополнительный цвет (для опциональных элементов) + /// + public Color AdditionalColor { get; private set; } /// /// Признак (опция) наличия радара /// @@ -33,9 +37,10 @@ namespace AirplaneWithRadar.Entities /// Признак наличия радара /// Признак наличия доп.бака /// Признак наличия штыря - public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, Color - additionalColor, bool radar, bool tank, bool pin) : base (speed, weight, bodyColor, additionalColor) + public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, + Color additionalColor, bool radar, bool tank, bool pin) : base (speed, weight, bodyColor) { + AdditionalColor = additionalColor; Radar = radar; Tank = tank; Pin = pin; diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs index 6d380a3..48be20a 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs @@ -29,43 +29,102 @@ private void InitializeComponent() { groupBoxInstruments = new GroupBox(); + groupBoxSets = new GroupBox(); + buttonDeleteSet = new Button(); + listBoxStorages = new ListBox(); + buttonAddInSet = new Button(); + textBoxStorageName = new TextBox(); maskedTextBoxNumber = new MaskedTextBox(); buttonUpdate = new Button(); buttonAdd = new Button(); buttonDelete = new Button(); pictureBoxCollection = new PictureBox(); groupBoxInstruments.SuspendLayout(); + groupBoxSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // groupBoxInstruments // + groupBoxInstruments.Controls.Add(groupBoxSets); groupBoxInstruments.Controls.Add(maskedTextBoxNumber); groupBoxInstruments.Controls.Add(buttonUpdate); groupBoxInstruments.Controls.Add(buttonAdd); groupBoxInstruments.Controls.Add(buttonDelete); groupBoxInstruments.Dock = DockStyle.Right; - groupBoxInstruments.Location = new Point(699, 0); - groupBoxInstruments.Margin = new Padding(2, 2, 2, 2); + groupBoxInstruments.Location = new Point(707, 0); + groupBoxInstruments.Margin = new Padding(2); groupBoxInstruments.Name = "groupBoxInstruments"; - groupBoxInstruments.Padding = new Padding(2, 2, 2, 2); - groupBoxInstruments.Size = new Size(170, 497); + groupBoxInstruments.Padding = new Padding(2); + groupBoxInstruments.Size = new Size(214, 570); groupBoxInstruments.TabIndex = 5; groupBoxInstruments.TabStop = false; groupBoxInstruments.Text = "Инструменты"; // + // groupBoxSets + // + groupBoxSets.Controls.Add(buttonDeleteSet); + groupBoxSets.Controls.Add(listBoxStorages); + groupBoxSets.Controls.Add(buttonAddInSet); + groupBoxSets.Controls.Add(textBoxStorageName); + groupBoxSets.Location = new Point(14, 33); + groupBoxSets.Name = "groupBoxSets"; + groupBoxSets.Size = new Size(182, 313); + groupBoxSets.TabIndex = 6; + groupBoxSets.TabStop = false; + groupBoxSets.Text = "Наборы"; + // + // buttonDeleteSet + // + buttonDeleteSet.Location = new Point(9, 223); + buttonDeleteSet.Margin = new Padding(2); + buttonDeleteSet.Name = "buttonDeleteSet"; + buttonDeleteSet.Size = new Size(166, 30); + buttonDeleteSet.TabIndex = 9; + buttonDeleteSet.Text = "Удалить набор"; + buttonDeleteSet.UseVisualStyleBackColor = true; + buttonDeleteSet.Click += buttonDeleteSet_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(14, 111); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(152, 84); + listBoxStorages.TabIndex = 8; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + // + // buttonAddInSet + // + buttonAddInSet.Location = new Point(9, 65); + buttonAddInSet.Margin = new Padding(2); + buttonAddInSet.Name = "buttonAddInSet"; + buttonAddInSet.Size = new Size(166, 30); + buttonAddInSet.TabIndex = 7; + buttonAddInSet.Text = "Добавить в набор"; + buttonAddInSet.UseVisualStyleBackColor = true; + buttonAddInSet.Click += buttonAddInSet_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(9, 33); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(162, 27); + textBoxStorageName.TabIndex = 0; + // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(31, 142); - maskedTextBoxNumber.Margin = new Padding(2, 2, 2, 2); + maskedTextBoxNumber.Location = new Point(51, 425); + maskedTextBoxNumber.Margin = new Padding(2); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(119, 27); maskedTextBoxNumber.TabIndex = 5; // // buttonUpdate // - buttonUpdate.Location = new Point(18, 247); - buttonUpdate.Margin = new Padding(2, 2, 2, 2); + buttonUpdate.Location = new Point(38, 509); + buttonUpdate.Margin = new Padding(2); buttonUpdate.Name = "buttonUpdate"; buttonUpdate.Size = new Size(142, 50); buttonUpdate.TabIndex = 4; @@ -75,10 +134,10 @@ // // buttonAdd // - buttonAdd.Location = new Point(18, 53); - buttonAdd.Margin = new Padding(2, 2, 2, 2); + buttonAdd.Location = new Point(30, 369); + buttonAdd.Margin = new Padding(2); buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(142, 30); + buttonAdd.Size = new Size(166, 30); buttonAdd.TabIndex = 1; buttonAdd.Text = "Добавить самолет"; buttonAdd.UseVisualStyleBackColor = true; @@ -86,8 +145,8 @@ // // buttonDelete // - buttonDelete.Location = new Point(18, 171); - buttonDelete.Margin = new Padding(2, 2, 2, 2); + buttonDelete.Location = new Point(38, 456); + buttonDelete.Margin = new Padding(2); buttonDelete.Name = "buttonDelete"; buttonDelete.Size = new Size(142, 30); buttonDelete.TabIndex = 3; @@ -99,9 +158,9 @@ // pictureBoxCollection.Dock = DockStyle.Fill; pictureBoxCollection.Location = new Point(0, 0); - pictureBoxCollection.Margin = new Padding(2, 2, 2, 2); + pictureBoxCollection.Margin = new Padding(2); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(699, 497); + pictureBoxCollection.Size = new Size(707, 570); pictureBoxCollection.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxCollection.TabIndex = 6; pictureBoxCollection.TabStop = false; @@ -110,14 +169,16 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(869, 497); + ClientSize = new Size(921, 570); Controls.Add(pictureBoxCollection); Controls.Add(groupBoxInstruments); - Margin = new Padding(2, 2, 2, 2); + Margin = new Padding(2); Name = "FormAirplaneCollection"; Text = "FormAirplaneCollection"; groupBoxInstruments.ResumeLayout(false); groupBoxInstruments.PerformLayout(); + groupBoxSets.ResumeLayout(false); + groupBoxSets.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); PerformLayout(); @@ -130,5 +191,10 @@ private GroupBox groupBoxInstruments; private PictureBox pictureBoxCollection; private MaskedTextBox maskedTextBoxNumber; + private GroupBox groupBoxSets; + private Button buttonDeleteSet; + private ListBox listBoxStorages; + private Button buttonAddInSet; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs index 1fae88c..393d53b 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs @@ -21,16 +21,82 @@ namespace AirplaneWithRadar /// /// Набор объектов /// - private readonly AirplanesGenericCollection _airplanes; + private readonly AirplanesGenericStorage _storage; /// /// Конструктор /// public FormAirplaneCollection() { InitializeComponent(); - _airplanes = new AirplanesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new AirplanesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + int index = listBoxStorages.SelectedIndex; + listBoxStorages.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorages.Items.Add(_storage.Keys[i]); + } + if (listBoxStorages.Items.Count > 0 && (index == -1 || index + >= listBoxStorages.Items.Count)) + { + listBoxStorages.SelectedIndex = 0; + } + else if (listBoxStorages.Items.Count > 0 && index > -1 && + index < listBoxStorages.Items.Count) + { + listBoxStorages.SelectedIndex = index; + } + } + /// + /// Добавление набора в коллекцию + /// + /// + /// + private void buttonAddInSet_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + /// + /// Выбор набора + /// + /// + /// + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes(); + + } + /// + /// Удаление набора + /// + /// + /// + private void buttonDeleteSet_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект { listBoxStorages.SelectedItem}?", + "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxStorages.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } } /// /// Добавление объекта в набор @@ -39,13 +105,23 @@ namespace AirplaneWithRadar /// private void buttonAdd_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } AirplaneWithRadarForm form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_airplanes + form.SelectedAirplane != -1) + if (obj + form.SelectedAirplane) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _airplanes.ShowAirplanes(); + pictureBoxCollection.Image = obj.ShowAirplanes(); } else { @@ -60,21 +136,32 @@ namespace AirplaneWithRadar /// private void buttonDelete_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_airplanes - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _airplanes.ShowAirplanes(); + pictureBoxCollection.Image = obj.ShowAirplanes(); } else { MessageBox.Show("Не удалось удалить объект"); } + } /// /// Обновление рисунка по набору @@ -83,7 +170,18 @@ namespace AirplaneWithRadar /// private void buttonUpdate_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _airplanes.ShowAirplanes(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowAirplanes(); + } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs index 114ad8b..ba61aa2 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs @@ -14,58 +14,51 @@ namespace AirplaneWithRadar.Generics where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// 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; } /// /// Добавление объекта в набор на конкретную позицию /// - /// Добавляемый автомобиль + /// Добавляемый самолет /// Позиция /// - public int Insert(T airplane, int position) + public bool Insert(T airplane, int position) { - int nullIndex = -1, i; - if (position < 0 || position >= Count) - { - return -1; - } - 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; + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) return false; + _places.Insert(position, airplane); + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -75,7 +68,7 @@ namespace AirplaneWithRadar.Generics public bool Remove(int position) { if (position < 0 || position >= Count) return false; - _places[position] = null; + _places.RemoveAt(position); return true; } /// @@ -83,10 +76,34 @@ namespace AirplaneWithRadar.Generics /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= Count) return null; - return _places[position]; + get + { + if (position < 0 || position >= Count) return null; + return _places[position]; + } + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) return; + _places.Insert(position, value); + return; + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetAirplanes(int? maxAirplanes = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxAirplanes.HasValue && i == maxAirplanes.Value) + { + yield break; + } + } } } }