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/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; + } + } } } }