diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.Designer.cs index 1136dd0..2f8b278 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.Designer.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.Designer.cs @@ -38,9 +38,15 @@ this.buttonRemoveAirplane = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddAirplane = new System.Windows.Forms.Button(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.ButtonDeleteMap = new System.Windows.Forms.Button(); + this.ListBoxMaps = new System.Windows.Forms.ListBox(); + this.ButtonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox.SuspendLayout(); + this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // @@ -55,13 +61,13 @@ this.groupBox.Controls.Add(this.buttonRemoveAirplane); this.groupBox.Controls.Add(this.maskedTextBoxPosition); this.groupBox.Controls.Add(this.buttonAddAirplane); - this.groupBox.Controls.Add(this.comboBoxSelectorMap); + this.groupBox.Controls.Add(this.groupBox1); this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox.Location = new System.Drawing.Point(685, 0); this.groupBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.groupBox.Name = "groupBox"; this.groupBox.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.groupBox.Size = new System.Drawing.Size(229, 600); + this.groupBox.Size = new System.Drawing.Size(229, 653); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; @@ -71,7 +77,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(99, 537); + this.buttonDown.Location = new System.Drawing.Point(98, 605); this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(34, 40); @@ -84,7 +90,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(141, 537); + this.buttonRight.Location = new System.Drawing.Point(140, 605); this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(34, 40); @@ -97,7 +103,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(58, 537); + this.buttonLeft.Location = new System.Drawing.Point(57, 605); this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(34, 40); @@ -110,7 +116,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(99, 489); + this.buttonUp.Location = new System.Drawing.Point(98, 557); this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(34, 40); @@ -121,7 +127,7 @@ // buttonShowOnMap // this.buttonShowOnMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowOnMap.Location = new System.Drawing.Point(22, 416); + this.buttonShowOnMap.Location = new System.Drawing.Point(22, 509); this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(193, 40); @@ -133,7 +139,7 @@ // buttonShowStorage // this.buttonShowStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowStorage.Location = new System.Drawing.Point(22, 311); + this.buttonShowStorage.Location = new System.Drawing.Point(22, 461); this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(193, 40); @@ -145,7 +151,7 @@ // buttonRemoveAirplane // this.buttonRemoveAirplane.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRemoveAirplane.Location = new System.Drawing.Point(22, 228); + this.buttonRemoveAirplane.Location = new System.Drawing.Point(22, 413); this.buttonRemoveAirplane.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonRemoveAirplane.Name = "buttonRemoveAirplane"; this.buttonRemoveAirplane.Size = new System.Drawing.Size(193, 40); @@ -157,7 +163,7 @@ // maskedTextBoxPosition // this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 189); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 378); this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; @@ -167,7 +173,7 @@ // buttonAddAirplane // this.buttonAddAirplane.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddAirplane.Location = new System.Drawing.Point(22, 120); + this.buttonAddAirplane.Location = new System.Drawing.Point(22, 330); this.buttonAddAirplane.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonAddAirplane.Name = "buttonAddAirplane"; this.buttonAddAirplane.Size = new System.Drawing.Size(193, 40); @@ -176,20 +182,69 @@ this.buttonAddAirplane.UseVisualStyleBackColor = true; this.buttonAddAirplane.Click += new System.EventHandler(this.ButtonAddAirplane_Click); // + // groupBox1 + // + this.groupBox1.Controls.Add(this.ButtonDeleteMap); + this.groupBox1.Controls.Add(this.ListBoxMaps); + this.groupBox1.Controls.Add(this.ButtonAddMap); + this.groupBox1.Controls.Add(this.textBoxNewMapName); + this.groupBox1.Controls.Add(this.comboBoxSelectorMap); + this.groupBox1.Location = new System.Drawing.Point(6, 26); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(250, 300); + this.groupBox1.TabIndex = 18; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Карты"; + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(10, 255); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(207, 29); + this.ButtonDeleteMap.TabIndex = 13; + this.ButtonDeleteMap.Text = "Удалить карту"; + this.ButtonDeleteMap.UseVisualStyleBackColor = true; + this.ButtonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // ListBoxMaps + // + this.ListBoxMaps.FormattingEnabled = true; + this.ListBoxMaps.ItemHeight = 20; + this.ListBoxMaps.Location = new System.Drawing.Point(10, 136); + this.ListBoxMaps.Name = "ListBoxMaps"; + this.ListBoxMaps.Size = new System.Drawing.Size(207, 104); + this.ListBoxMaps.TabIndex = 12; + this.ListBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(10, 101); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(207, 29); + this.ButtonAddMap.TabIndex = 11; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(10, 34); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(207, 27); + this.textBoxNewMapName.TabIndex = 10; + // // comboBoxSelectorMap // - this.comboBoxSelectorMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.Items.AddRange(new object[] { "Простая карта", "Карта неба с облаками", "Карта туманного неба с грозой"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(22, 44); - this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(10, 67); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(193, 28); - this.comboBoxSelectorMap.TabIndex = 0; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(207, 28); + this.comboBoxSelectorMap.TabIndex = 9; this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // pictureBox @@ -198,7 +253,7 @@ this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(685, 600); + this.pictureBox.Size = new System.Drawing.Size(685, 653); this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; @@ -207,7 +262,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(914, 600); + this.ClientSize = new System.Drawing.Size(914, 653); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); @@ -215,6 +270,8 @@ this.Text = "FormMapWithSetAirplane"; this.groupBox.ResumeLayout(false); this.groupBox.PerformLayout(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -235,5 +292,10 @@ private Button buttonAddAirplane; private ComboBox comboBoxSelectorMap; private PictureBox pictureBox; + private GroupBox groupBox1; + private Button ButtonDeleteMap; + private ListBox ListBoxMaps; + private Button ButtonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs index b5eba7a..5256dda 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs @@ -17,9 +17,41 @@ namespace AirplaneWithRadar /// Объект от класса карты с набором объектов /// private MapWithSetAirplaneGeneric _mapAirplaneCollectionGeneric; + private readonly Dictionary _mapDict = new() + { + {"Простая карта", new SimpleMap() }, + {"Карта неба с облаками", new SkyMap() }, + {"Карта туманного неба с грозой", new FoggySkyMap() } + }; + private readonly MapsCollection _mapsCollection; public FormMapWithSetAirplane() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } + } + private void ReloadMaps() + { + int index = ListBoxMaps.SelectedIndex; + + ListBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) + { + ListBoxMaps.Items.Add(_mapsCollection.Keys[i]); + } + + if (ListBoxMaps.Items.Count > 0 && (index == -1 || index >= ListBoxMaps.Items.Count)) + { + ListBoxMaps.SelectedIndex = 0; + } + else if (ListBoxMaps.Items.Count > 0 && index > -1 && index < ListBoxMaps.Items.Count) + { + ListBoxMaps.SelectedIndex = index; + } } /// /// Выбор карты @@ -58,7 +90,7 @@ namespace AirplaneWithRadar /// private void ButtonAddAirplane_Click(object sender, EventArgs e) { - if (_mapAirplaneCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -66,10 +98,10 @@ namespace AirplaneWithRadar if (form.ShowDialog() == DialogResult.OK) { DrawningObjectAirplane airplane = new(form.SelectedAirplane); - if (_mapAirplaneCollectionGeneric + airplane != -1) + if ((_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airplane != -1)) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapAirplaneCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -84,6 +116,10 @@ namespace AirplaneWithRadar /// private void ButtonRemoveAirplane_Click(object sender, EventArgs e) { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -96,7 +132,7 @@ namespace AirplaneWithRadar if (_mapAirplaneCollectionGeneric - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapAirplaneCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -110,11 +146,11 @@ namespace AirplaneWithRadar /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapAirplaneCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAirplaneCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// /// Вывод карты @@ -123,11 +159,11 @@ namespace AirplaneWithRadar /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapAirplaneCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAirplaneCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Перемещение @@ -136,7 +172,7 @@ namespace AirplaneWithRadar /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapAirplaneCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -158,7 +194,40 @@ namespace AirplaneWithRadar dir = Direction.Right; break; } - pictureBox.Image = _mapAirplaneCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapDict[comboBoxSelectorMap.Text]); + ReloadMaps(); + } + + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void ButtonDeleteMap_Click(object sender, EventArgs e) + { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить карту {ListBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _mapsCollection.DelMap(ListBoxMaps.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); + } } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplaneGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplaneGeneric.cs index 69f4d04..6916b7d 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplaneGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplaneGeneric.cs @@ -50,13 +50,9 @@ public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setAirplane.Count; i++) + foreach (var ship in _setAirplane.GetAirplane()) { - var airplane = _setAirplane.Get(i); - if (airplane != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, airplane); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); } return new(_pictureWidth, _pictureHeight); } @@ -75,11 +71,11 @@ int j = _setAirplane.Count - 1; for (int i = 0; i < _setAirplane.Count; i++) { - if (_setAirplane.Get(i) == null) + if (_setAirplane[i] == null) { for (; j > i; j--) { - var airplane = _setAirplane.Get(j); + var airplane = _setAirplane[j]; if (airplane != null) { _setAirplane.Insert(airplane, i); @@ -119,17 +115,17 @@ int curWidth = 0; int curHeight = 0; - for (int i = _setAirplane.Count; i >= 0; i--) + foreach (var airplane in _setAirplane.GetAirplane()) { - _setAirplane.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 80, + airplane?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 80, curHeight * _placeSizeHeight + 30, _pictureWidth, _pictureHeight); - _setAirplane.Get(i)?.DrawningObject(g); + airplane?.DrawningObject(g); if (curWidth < widthEl) curWidth++; else { - curWidth = 1; + curWidth = 0; curHeight++; } if (curHeight > heightEl) diff --git a/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs new file mode 100644 index 0000000..1994358 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs @@ -0,0 +1,45 @@ +using AirplaneWithRadar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar +{ + internal class MapsCollection + { + readonly Dictionary> _mapStorages; + public List Keys => _mapStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddMap(string name, AbstractMap map) + { + if (!_mapStorages.ContainsKey(name)) + { + _mapStorages.Add(name, new MapWithSetAirplaneGeneric(_pictureWidth, _pictureHeight, map)); + } + } + public void DelMap(string name) + { + if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); + } + public MapWithSetAirplaneGeneric this[string ind] + { + get + { + if (_mapStorages.ContainsKey(ind)) + { + return _mapStorages[ind]; + } + return null; + } + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs index 53a1c55..6ebcebc 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs @@ -4,72 +4,76 @@ internal class SetAirplaneGeneric where T : class { - // Массив объектов, которые храним - private readonly T[] _places; - // Количество объектов в массиве - public int Count => _places.Length; - // Конструктор - public SetAirplaneGeneric(int count) + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; + // Конструктор + public SetAirplaneGeneric(int count) { - _places = new T[count]; - } + _maxCount = count; + _places = new List(); + } // Добавление объекта в набор public int Insert(T airplane) { - // вставка в начало набора - return Insert(airplane, 0); + if (_places.Count > _maxCount) + { + return -1; + } + // вставка в начало набора + return Insert(airplane, 0); } // Добавление объекта в набор на конкретную позицию public int Insert(T airplane, int position) { - // проверка позиции - if (position >= _places.Length || position < 0) - return -1; - //проверка, что элемент массива по этой позиции пустой, если нет, то - if (_places[position] == null) - { - _places[position] = airplane; - return position; - } - //проверка, что после вставляемого элемента в массиве есть пустой элемент - int findEmptyPos = -1; - - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - findEmptyPos = i; - break; - } - } - if (findEmptyPos < 0) return -1; - - //сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - for (int i = findEmptyPos; i > position; i--) - { - _places[i] = _places[i - 1]; - } - // вставка по позиции - _places[position] = airplane; - return position; + if (position >= _maxCount || position < 0) return -1; + _places.Insert(position, airplane); + return position; } // Удаление объекта из набора с конкретной позиции public T Remove(int position) { - // проверка позиции - if (position >= _places.Length || position < 0) return null; + // проверка позиции + if (position >= _maxCount || position < 0) return null; // удаление объекта из массива, присовив элементу массива значение null T temp = _places[position]; - _places[position] = null; - return temp; + _places.RemoveAt(position); + return temp; } - // Получение объекта из набора по позиции - public T Get(int position) - { - // проверка позиции - if (position >= _places.Length || position < 0) - return null; - return _places[position]; - } - } + // Получение объекта из набора по позиции + public T this[int position] + { + get + { + if (position >= _places.Count || position < 0) + { + return null; + } + return _places[position]; + } + set + { + if (position >= _places.Count || position < 0) + { + return; + } + Insert(value, position); + + } + } + public IEnumerable GetAirplane() + { + foreach (var airplane in _places) + { + if (airplane != null) + { + yield return airplane; + } + else + { + yield break; + } + } + } + } } \ No newline at end of file