lab04 fix rewriting

This commit is contained in:
NikGapon 2022-11-19 17:18:41 +04:00
parent c1848a955e
commit d553e2123d
5 changed files with 295 additions and 122 deletions

View File

@ -29,6 +29,12 @@
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.listBoxMaps = new System.Windows.Forms.ListBox();
this.buttonDelMap = new System.Windows.Forms.Button();
this.buttonAddMap = new System.Windows.Forms.Button();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
@ -38,14 +44,15 @@
this.buttonRemoveAirplane = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddAirplane = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.buttonDown);
this.groupBoxTools.Controls.Add(this.buttonRight);
this.groupBoxTools.Controls.Add(this.buttonLeft);
@ -55,21 +62,84 @@
this.groupBoxTools.Controls.Add(this.buttonRemoveAirplane);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonAddAirplane);
this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(600, 0);
this.groupBoxTools.Location = new System.Drawing.Point(631, 0);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(200, 450);
this.groupBoxTools.Size = new System.Drawing.Size(200, 593);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
//
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.listBoxMaps);
this.groupBoxMaps.Controls.Add(this.buttonDelMap);
this.groupBoxMaps.Controls.Add(this.buttonAddMap);
this.groupBoxMaps.Controls.Add(this.textBoxNewMapName);
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(6, 22);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(188, 211);
this.groupBoxMaps.TabIndex = 10;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Карты";
//
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 15;
this.listBoxMaps.Location = new System.Drawing.Point(6, 105);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(170, 64);
this.listBoxMaps.TabIndex = 4;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged);
//
// buttonDelMap
//
this.buttonDelMap.Location = new System.Drawing.Point(43, 175);
this.buttonDelMap.Name = "buttonDelMap";
this.buttonDelMap.Size = new System.Drawing.Size(106, 23);
this.buttonDelMap.TabIndex = 3;
this.buttonDelMap.Text = "Удалить карту";
this.buttonDelMap.UseVisualStyleBackColor = true;
this.buttonDelMap.Click += new System.EventHandler(this.buttonDelMap_Click);
//
// buttonAddMap
//
this.buttonAddMap.Location = new System.Drawing.Point(6, 76);
this.buttonAddMap.Name = "buttonAddMap";
this.buttonAddMap.Size = new System.Drawing.Size(170, 23);
this.buttonAddMap.TabIndex = 2;
this.buttonAddMap.Text = "добавить карту";
this.buttonAddMap.UseVisualStyleBackColor = true;
this.buttonAddMap.Click += new System.EventHandler(this.buttonAddMap_Click);
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(6, 18);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(170, 23);
this.textBoxNewMapName.TabIndex = 1;
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Первая карта",
"Вторая карта",
"Третья карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 47);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(170, 23);
this.comboBoxSelectorMap.TabIndex = 0;
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Airbus.Properties.Resources.v4;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(80, 380);
this.buttonDown.Location = new System.Drawing.Point(80, 523);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(35, 35);
this.buttonDown.TabIndex = 9;
@ -81,7 +151,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Airbus.Properties.Resources.v3;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(112, 348);
this.buttonRight.Location = new System.Drawing.Point(112, 491);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(35, 35);
this.buttonRight.TabIndex = 8;
@ -93,7 +163,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Airbus.Properties.Resources.v1;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(49, 348);
this.buttonLeft.Location = new System.Drawing.Point(49, 491);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(35, 35);
this.buttonLeft.TabIndex = 7;
@ -105,7 +175,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Airbus.Properties.Resources.v2;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(80, 316);
this.buttonUp.Location = new System.Drawing.Point(80, 459);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(35, 35);
this.buttonUp.TabIndex = 6;
@ -114,7 +184,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(62, 268);
this.buttonShowOnMap.Location = new System.Drawing.Point(62, 370);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(75, 23);
this.buttonShowOnMap.TabIndex = 5;
@ -124,7 +194,7 @@
//
// buttonStorage
//
this.buttonStorage.Location = new System.Drawing.Point(6, 239);
this.buttonStorage.Location = new System.Drawing.Point(6, 399);
this.buttonStorage.Name = "buttonStorage";
this.buttonStorage.Size = new System.Drawing.Size(188, 23);
this.buttonStorage.TabIndex = 4;
@ -134,7 +204,7 @@
//
// buttonRemoveAirplane
//
this.buttonRemoveAirplane.Location = new System.Drawing.Point(6, 146);
this.buttonRemoveAirplane.Location = new System.Drawing.Point(12, 297);
this.buttonRemoveAirplane.Name = "buttonRemoveAirplane";
this.buttonRemoveAirplane.Size = new System.Drawing.Size(182, 23);
this.buttonRemoveAirplane.TabIndex = 3;
@ -144,7 +214,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 117);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(12, 268);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(182, 23);
@ -152,7 +222,7 @@
//
// buttonAddAirplane
//
this.buttonAddAirplane.Location = new System.Drawing.Point(6, 74);
this.buttonAddAirplane.Location = new System.Drawing.Point(12, 239);
this.buttonAddAirplane.Name = "buttonAddAirplane";
this.buttonAddAirplane.Size = new System.Drawing.Size(182, 23);
this.buttonAddAirplane.TabIndex = 1;
@ -160,25 +230,12 @@
this.buttonAddAirplane.UseVisualStyleBackColor = true;
this.buttonAddAirplane.Click += new System.EventHandler(this.buttonAddAirplane_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Первая карта",
"Вторая карта",
"Третья карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 22);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(182, 23);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(600, 450);
this.pictureBox.Size = new System.Drawing.Size(631, 593);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -186,18 +243,22 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.ClientSize = new System.Drawing.Size(831, 593);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Name = "FormMapWithSetAirplane";
this.Text = "FormMapWithSetAirplane";
this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout();
this.groupBoxMaps.ResumeLayout(false);
this.groupBoxMaps.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private GroupBox groupBoxTools;
private Button buttonShowOnMap;
private Button buttonStorage;
@ -210,5 +271,10 @@
private Button buttonLeft;
private Button buttonRight;
private Button buttonDown;
private GroupBox groupBoxMaps;
private ListBox listBoxMaps;
private Button buttonDelMap;
private Button buttonAddMap;
private TextBox textBoxNewMapName;
}
}

View File

@ -13,55 +13,89 @@ namespace Airbus
{
public partial class FormMapWithSetAirplane : Form
{
private AbstractMap _abstractMap;
private MapWithSetAirplaneGeneric<DrawningObjectAirplane, AbstractMap> _mapAirplanesCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{ "Простая карта", new SimpleMap() }, { "Вторая карта", new SecondMap() },{ "Третья карта", new ThirdMap() }
};
/// <summary>
/// Объект от коллекции карт
/// </summary>
private readonly MapsCollection _mapsCollection;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetAirplane()
{
InitializeComponent();
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapsDict)
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
}
/// <summary>
/// Выбор карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender,
EventArgs e)
private void ReloadMaps()
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
int index = listBoxMaps.SelectedIndex;
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{
case "Первая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
case "Третья карта":
map = new ThirdMap();
break;
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
}
if (map != null)
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >=
listBoxMaps.Items.Count))
{
_mapAirplanesCollectionGeneric = new
MapWithSetAirplaneGeneric<DrawningObjectAirplane, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
listBoxMaps.SelectedIndex = 0;
}
else
else if (listBoxMaps.Items.Count > 0 && index > -1 && index <
listBoxMaps.Items.Count)
{
_mapAirplanesCollectionGeneric = null;
listBoxMaps.SelectedIndex = index;
}
}
/// <summary>
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddMap_Click(object sender, EventArgs e)
{
if (comboBoxSelectorMap.SelectedIndex == -1 ||
string.IsNullOrEmpty(textBoxNewMapName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text,
_mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
}
private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void buttonDelMap_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();
}
}
private void buttonAddAirplane_Click(object sender, EventArgs e)
{
if (_mapAirplanesCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -69,10 +103,11 @@ namespace Airbus
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectAirplane airplane = new(form.SelectedAirplane);
if (_mapAirplanesCollectionGeneric + airplane > -1)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airplane > -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapAirplanesCollectionGeneric.ShowSet();
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -80,13 +115,12 @@ namespace Airbus
}
}
}
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonRemoveAirplane_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
@ -97,42 +131,42 @@ namespace Airbus
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapAirplanesCollectionGeneric - pos != null )
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ??
string.Empty] - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapAirplanesCollectionGeneric.ShowSet();
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Вывод набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStorage_Click(object sender, EventArgs e)
{
if (_mapAirplanesCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapAirplanesCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Вывод карты
/// Выбор карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapAirplanesCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapAirplanesCollectionGeneric.ShowOnMap();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
}
/// <summary>
/// Перемещение
@ -141,7 +175,7 @@ namespace Airbus
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapAirplanesCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -163,7 +197,8 @@ namespace Airbus
dir = Direction.Right;
break;
}
pictureBox.Image = _mapAirplanesCollectionGeneric.MoveObject(dir);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
}
}

View File

@ -91,7 +91,7 @@ namespace Airbus
Shaking();
for (int i = 0; i < _setAirplane.Count; i++)
{
var airplane = _setAirplane.Get(i);
var airplane = _setAirplane[i];
if (airplane != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, airplane);
@ -120,11 +120,11 @@ namespace Airbus
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);
@ -177,9 +177,9 @@ namespace Airbus
int maxLeft = (countInLine - 1) * _placeSizeWidth;
for (int i = 0; i < _setAirplane.Count; i++)
{
_setAirplane.Get(i)?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, (_pictureHeight - (i / 2 * _placeSizeHeight + _placeSizeHeight + 30)), _pictureWidth, _placeSizeHeight);
_setAirplane.Get(i)?.DrawningObject(g);
_setAirplane[i]?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, (_pictureHeight - (i / 2 * _placeSizeHeight + _placeSizeHeight + 30)), _pictureWidth, _placeSizeHeight);
_setAirplane[i]?.DrawningObject(g);
}
}
}
}
}

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetAirplaneGeneric<DrawningObjectAirplane, AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
/// </summary>
public List<string> Keys => _mapStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetAirplaneGeneric<DrawningObjectAirplane, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
if (!_mapStorages.ContainsKey(name))
{
MapWithSetAirplaneGeneric<DrawningObjectAirplane, AbstractMap> Map = new(_pictureWidth, _pictureHeight, map);
_mapStorages.Add(name, Map);
}
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name);
}
/// <summary>
/// Доступ к парковке
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetAirplaneGeneric<DrawningObjectAirplane, AbstractMap> this[string ind]
{
get
{
if (_mapStorages.ContainsKey(ind))
return _mapStorages[ind];
return null;
}
}
}
}

View File

@ -9,27 +9,14 @@ namespace Airbus
internal class SetAirplaneGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetAirplaneGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
private bool CanInsert(int position)
{
for (int i = position; i < _places.Length; i++)
if (_places[i] == null)
return true;
return false;
}
public int Insert(T airplane)
{
return Insert(airplane, 0);
@ -37,19 +24,8 @@ namespace Airbus
public int Insert(T airplane, int position)
{
if (position < 0 || position > _places.Length) return 0;
if (_places[position] != null && CanInsert(position))
{
for (int i = _places.Length - 1; i > position; --i)
{
if (_places[i] == null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = airplane;
if (position < 0 || position > _places.Count) return -1;
_places.Insert(position, airplane);
return position;
}
/// <summary>
@ -70,10 +46,35 @@ namespace Airbus
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
if (position < 0 || position > _places.Length) return null;
return _places[position];
get
{
if (position <= Count || position >= 0)
{
return _places[position];
}
else return null;
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetCars()
{
foreach (var car in _places)
{
if (car != null)
{
yield return car;
}
else
{
yield break;
}
}
}
}
}