diff --git a/AirBomber/AirBomber/AbstractStrategy.cs b/AirBomber/AirBomber/AbstractStrategy.cs index 7b44070..c82b1b6 100644 --- a/AirBomber/AirBomber/AbstractStrategy.cs +++ b/AirBomber/AirBomber/AbstractStrategy.cs @@ -37,6 +37,7 @@ namespace AirBomber public void SetData(IMoveableObject moveableObject, int width, int height) { + if (moveableObject == null) { _state = Status.NotInit; @@ -86,8 +87,7 @@ namespace AirBomber /// /// Параметры объекта /// - protected ObjectParameters? GetObjectParameters => - _moveableObject?.GetObjectPosition; + protected ObjectParameters? GetObjectParameters =>_moveableObject?.GetObjectPosition; /// /// Шаг объекта /// diff --git a/AirBomber/AirBomber/DrawningAirBomber.cs b/AirBomber/AirBomber/DrawningAirBomber.cs index 5ed19eb..05612cf 100644 --- a/AirBomber/AirBomber/DrawningAirBomber.cs +++ b/AirBomber/AirBomber/DrawningAirBomber.cs @@ -22,12 +22,12 @@ namespace AirBomber /// Ширина картинки /// Высота картинки public DrawningAirBomber(int speed, double weight, Color bodyColor, Color - additionalColor, bool bombs, Color bombsColor, bool fuelTanks, int width, int height) : + additionalColor, bool bombs, bool fuelTanks, int width, int height) : base(speed, weight, bodyColor, width, height, 160, 118) { if (EntityAirPlane != null) { - EntityAirPlane = new EntityAirBomber(speed, weight, bodyColor, additionalColor, bombs, bombsColor, fuelTanks); + EntityAirPlane = new EntityAirBomber(speed, weight, bodyColor, additionalColor, bombs, fuelTanks); } } public override void DrawPlane(Graphics g) @@ -38,14 +38,13 @@ namespace AirBomber } Pen pen = new(Color.Black); Brush additionalBrush = new SolidBrush(airBomber.AdditionalColor); - Brush bombsColor = new SolidBrush(airBomber.BombsColor); base.DrawPlane(g); // обвесы - g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 20, 15, 29); + g.FillEllipse(additionalBrush, _startPosX + 90, _startPosY + 20, 15, 29); g.DrawEllipse(pen, _startPosX + 90, _startPosY + 20, 15, 29); - g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 70, 15, 29); + g.FillEllipse(additionalBrush, _startPosX + 90, _startPosY + 70, 15, 29); g.DrawEllipse(pen, _startPosX + 90, _startPosY + 70, 15, 29); - g.FillEllipse(bombsColor, _startPosX + 140, _startPosY + 50, 15, 15); + g.FillEllipse(additionalBrush, _startPosX + 140, _startPosY + 50, 15, 15); g.DrawEllipse(pen, _startPosX + 140, _startPosY + 50, 15, 15); // fueltanks g.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 34, 20, 15); diff --git a/AirBomber/AirBomber/DrawningAirPlane.cs b/AirBomber/AirBomber/DrawningAirPlane.cs index 758f92c..ba2b1e2 100644 --- a/AirBomber/AirBomber/DrawningAirPlane.cs +++ b/AirBomber/AirBomber/DrawningAirPlane.cs @@ -269,5 +269,11 @@ namespace AirBomber break; } } + /// + /// Получение объекта IMoveableObject из объекта DrawningAirPlane + /// + public IMoveableObject GetMoveableObject => new + DrawningObjectAirPlane(this); + } } diff --git a/AirBomber/AirBomber/EntityAirBomber.cs b/AirBomber/AirBomber/EntityAirBomber.cs index e64ad03..2197b86 100644 --- a/AirBomber/AirBomber/EntityAirBomber.cs +++ b/AirBomber/AirBomber/EntityAirBomber.cs @@ -14,11 +14,10 @@ namespace AirBomber public Color BombsColor { get; private set; } public bool FuelTanks { get; private set; } public EntityAirBomber(int speed, double weight, Color bodyColor, Color - additionalColor, bool bombs, Color bombsColor, bool fuelTanks) : base(speed, weight, bodyColor) + additionalColor, bool bombs, bool fuelTanks) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; Bombs = bombs; - BombsColor = bombsColor; FuelTanks = fuelTanks; } } diff --git a/AirBomber/AirBomber/FormAirBomber.Designer.cs b/AirBomber/AirBomber/FormAirBomber.Designer.cs index 7dd0993..ae4cac6 100644 --- a/AirBomber/AirBomber/FormAirBomber.Designer.cs +++ b/AirBomber/AirBomber/FormAirBomber.Designer.cs @@ -36,7 +36,8 @@ pictureBoxAirBomber = new PictureBox(); comboBoxStrategy = new ComboBox(); buttonCreateAirPlane = new Button(); - buttonStep = new Button(); + buttonStrategyStep = new Button(); + buttonSelectPlane = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit(); SuspendLayout(); // @@ -131,23 +132,34 @@ buttonCreateAirPlane.UseVisualStyleBackColor = true; buttonCreateAirPlane.Click += buttonCreateAirPlane_Click; // - // buttonStep + // buttonStrategyStep // - buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonStep.Location = new Point(862, 77); - buttonStep.Name = "buttonStep"; - buttonStep.Size = new Size(112, 49); - buttonStep.TabIndex = 8; - buttonStep.Text = "Шаг"; - buttonStep.UseVisualStyleBackColor = true; - buttonStep.Click += buttonStep_Click; + buttonStrategyStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStrategyStep.Location = new Point(862, 77); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(112, 49); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // + // buttonSelectPlane + // + buttonSelectPlane.Location = new Point(862, 178); + buttonSelectPlane.Name = "buttonSelectPlane"; + buttonSelectPlane.Size = new Size(110, 91); + buttonSelectPlane.TabIndex = 9; + buttonSelectPlane.Text = "Выбрать самолет"; + buttonSelectPlane.UseVisualStyleBackColor = true; + buttonSelectPlane.Click += buttonSelectPlane_Click; // // FormAirBomber // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(986, 540); - Controls.Add(buttonStep); + Controls.Add(buttonSelectPlane); + Controls.Add(buttonStrategyStep); Controls.Add(buttonCreateAirPlane); Controls.Add(comboBoxStrategy); Controls.Add(buttonUp); @@ -172,6 +184,7 @@ private PictureBox pictureBoxAirBomber; private ComboBox comboBoxStrategy; private Button buttonCreateAirPlane; - private Button buttonStep; + private Button buttonStrategyStep; + private Button buttonSelectPlane; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormAirBomber.cs b/AirBomber/AirBomber/FormAirBomber.cs index aa7e5ba..c9dff90 100644 --- a/AirBomber/AirBomber/FormAirBomber.cs +++ b/AirBomber/AirBomber/FormAirBomber.cs @@ -1,4 +1,6 @@ -namespace AirBomber +using System.Windows.Forms; + +namespace AirBomber { public partial class FormAirBomber : Form { @@ -6,10 +8,17 @@ /// /// Стратегия перемещения /// - private AbstractStrategy? _abstractStrategy; + private AbstractStrategy? _strategy; + /// + /// Выбранный самолет + /// + public DrawningAirPlane? SelectedPlane { get; private set; } + public FormAirBomber() { InitializeComponent(); + _strategy = null; + SelectedPlane = null; } private void Draw() { @@ -25,25 +34,41 @@ private void buttonCreateAirBomber_Click(object sender, EventArgs e) { Random random = new(); - _drawningAirPlane = new DrawningAirBomber(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), - pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog colorDialog = new ColorDialog(); + //TODO выбор основного цвета DONE + if (colorDialog.ShowDialog() == DialogResult.OK) + { + color = colorDialog.Color; + } + + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + //TODO выбор дополнительного цвета DONE + if (colorDialog.ShowDialog() == DialogResult.OK) + { + dopColor = colorDialog.Color; + } + + _drawningAirPlane = new DrawningAirBomber(random.Next(100, 300), random.Next(1000, 3000), color, dopColor, + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); _drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); - } private void buttonCreateAirPlane_Click(object sender, EventArgs e) { Random random = new(); - _drawningAirPlane = new DrawningAirPlane(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _drawningAirPlane = new DrawningAirPlane(random.Next(100, 300), random.Next(1000, 3000), color, pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); _drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); + } private void buttonMove_Click(object sender, EventArgs e) { @@ -69,7 +94,7 @@ } Draw(); } - private void buttonStep_Click(object sender, EventArgs e) + private void buttonStrategyStep_Click(object sender, EventArgs e) { if (_drawningAirPlane == null) { @@ -77,34 +102,41 @@ } if (comboBoxStrategy.Enabled) { - _abstractStrategy = comboBoxStrategy.SelectedIndex - switch + _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.SetData(new - DrawningObjectAirPlane(_drawningAirPlane), pictureBoxAirBomber.Width, - pictureBoxAirBomber.Height); - comboBoxStrategy.Enabled = false; + _strategy.SetData(_drawningAirPlane.GetMoveableObject, + pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); } - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.MakeStep(); + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); Draw(); - if (_abstractStrategy.GetStatus() == Status.Finish) + if (_strategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; - _abstractStrategy = null; + _strategy = null; } - + } + /// + /// Выбор самолета + /// + /// + /// + private void buttonSelectPlane_Click(object sender, EventArgs e) + { + SelectedPlane = _drawningAirPlane; + DialogResult = DialogResult.OK; } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormPlaneCollection.Designer.cs b/AirBomber/AirBomber/FormPlaneCollection.Designer.cs new file mode 100644 index 0000000..f762bda --- /dev/null +++ b/AirBomber/AirBomber/FormPlaneCollection.Designer.cs @@ -0,0 +1,126 @@ +namespace AirBomber +{ + partial class FormPlaneCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + pictureBoxCollection = new PictureBox(); + panelTools = new Panel(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonRefreshCollection = new Button(); + buttonRemovePlane = new Button(); + buttonAddPlane = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + panelTools.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxCollection + // + pictureBoxCollection.BackColor = SystemColors.Control; + pictureBoxCollection.Dock = DockStyle.Left; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(708, 548); + pictureBoxCollection.TabIndex = 0; + pictureBoxCollection.TabStop = false; + // + // panelTools + // + panelTools.Controls.Add(maskedTextBoxNumber); + panelTools.Controls.Add(buttonRefreshCollection); + panelTools.Controls.Add(buttonRemovePlane); + panelTools.Controls.Add(buttonAddPlane); + panelTools.Dock = DockStyle.Right; + panelTools.Location = new Point(719, 0); + panelTools.Name = "panelTools"; + panelTools.Size = new Size(237, 548); + panelTools.TabIndex = 1; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(37, 218); + maskedTextBoxNumber.Mask = "00"; + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(160, 31); + maskedTextBoxNumber.TabIndex = 3; + maskedTextBoxNumber.ValidatingType = typeof(int); + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(37, 394); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(160, 86); + buttonRefreshCollection.TabIndex = 2; + buttonRefreshCollection.Text = "Обновить коллекцию"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += buttonRefreshCollection_Click; + // + // buttonRemovePlane + // + buttonRemovePlane.Location = new Point(37, 266); + buttonRemovePlane.Name = "buttonRemovePlane"; + buttonRemovePlane.Size = new Size(160, 60); + buttonRemovePlane.TabIndex = 1; + buttonRemovePlane.Text = "Удалить самолет"; + buttonRemovePlane.UseVisualStyleBackColor = true; + buttonRemovePlane.Click += buttonRemovePlane_Click; + // + // buttonAddPlane + // + buttonAddPlane.Location = new Point(37, 94); + buttonAddPlane.Name = "buttonAddPlane"; + buttonAddPlane.Size = new Size(160, 64); + buttonAddPlane.TabIndex = 0; + buttonAddPlane.Text = "Добавить самолет"; + buttonAddPlane.UseVisualStyleBackColor = true; + buttonAddPlane.Click += buttonAddPlane_Click; + // + // FormPlaneCollection + // + AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(956, 548); + Controls.Add(panelTools); + Controls.Add(pictureBoxCollection); + Name = "FormPlaneCollection"; + Text = "Набор самолетов"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + panelTools.ResumeLayout(false); + panelTools.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private PictureBox pictureBoxCollection; + private Panel panelTools; + private MaskedTextBox maskedTextBoxNumber; + private Button buttonRefreshCollection; + private Button buttonRemovePlane; + private Button buttonAddPlane; + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs new file mode 100644 index 0000000..eba8446 --- /dev/null +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AirBomber +{ + public partial class FormPlaneCollection : Form + { + /// + /// Набор объектов + /// + private readonly PlanesGenericCollection _planes; + + public FormPlaneCollection() + { + InitializeComponent(); + _planes = new PlanesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void buttonAddPlane_Click(object sender, EventArgs e) + { + FormAirBomber form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_planes + form.SelectedPlane != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void buttonRemovePlane_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_planes - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + } +} diff --git a/AirBomber/AirBomber/FormPlaneCollection.resx b/AirBomber/AirBomber/FormPlaneCollection.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/AirBomber/AirBomber/FormPlaneCollection.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/AirBomber/AirBomber/PlanesGenericCollection.cs b/AirBomber/AirBomber/PlanesGenericCollection.cs new file mode 100644 index 0000000..5d4cbf8 --- /dev/null +++ b/AirBomber/AirBomber/PlanesGenericCollection.cs @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class PlanesGenericCollection + where T : DrawningAirPlane + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 170; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 120; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public PlanesGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(PlanesGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj) ?? -1; + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(PlanesGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + return true; + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowPlanes() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) + {//линия рамзетки места + g.DrawLine(pen, i * _placeSizeWidth, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + int widthObjCount = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _collection.Count; i++) + { + T? type = _collection.Get(i); + if (type != null) + { + int row = i / widthObjCount; + int col = widthObjCount - 1 - (i % widthObjCount); + + type.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); + type?.DrawPlane(g); + } + } + } + } +} diff --git a/AirBomber/AirBomber/Program.cs b/AirBomber/AirBomber/Program.cs index 76b85fe..e7cb4a7 100644 --- a/AirBomber/AirBomber/Program.cs +++ b/AirBomber/AirBomber/Program.cs @@ -11,7 +11,7 @@ namespace AirBomber // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormAirBomber()); + Application.Run(new FormPlaneCollection()); } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs new file mode 100644 index 0000000..b1b0f54 --- /dev/null +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый самолет + /// + public int Insert(T plane) + { + //was TODO + return Insert(plane, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый самолет + /// Позиция + /// + public int Insert(T plane, int position) + { + // TODO проверка позиции DONE + // TODO проверка, что элемент массива по этой позиции пустой,если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + int NoEmpty = 0, temp = 0; + for (int i = position; i < Count; i++) + { + if (_places[i] != null) NoEmpty++; + } + if (NoEmpty == Count - position) return -1; + + if (position < Count && position >= 0) + { + for (int j = position; j < Count; j++) + { + if (_places[j] == null) + { + temp = j; + break; + } + } + + for (int i = temp; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = plane; + return position; + } + return -1; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции DONE + // TODO удаление объекта из массива, присвоив элементу массива значение null + if (!(position >= 0 && position < Count) || _places[position] == null) + { + return false; + } + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + // TODO проверка позиции DONE + if (position < 0 || position >= Count) + { + return null; + } + return _places[position]; + } + + } +}