Сделанная 4 лаба

This commit is contained in:
Табеев Александр 2023-11-20 17:23:42 +04:00
parent c6fffe2777
commit 2374e686b4
5 changed files with 336 additions and 128 deletions

View File

@ -28,96 +28,161 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.panel1 = new System.Windows.Forms.Panel(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.ButtonRefreshCollection = new System.Windows.Forms.Button(); this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.ButtonAddObject = new System.Windows.Forms.Button();
this.ButtonDelObject = new System.Windows.Forms.Button();
this.ButtonAddAircraftGun = new System.Windows.Forms.Button(); this.ButtonAddAircraftGun = new System.Windows.Forms.Button();
this.ButtonRemoveAircraftGun = new System.Windows.Forms.Button(); this.ButtonRemoveAircraftGun = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.panel1.SuspendLayout(); this.groupBox2 = new System.Windows.Forms.GroupBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// panel1 // pictureBoxCollection
// //
this.panel1.Controls.Add(this.maskedTextBoxNumber); this.pictureBoxCollection.Location = new System.Drawing.Point(7, 6);
this.panel1.Controls.Add(this.ButtonRefreshCollection); this.pictureBoxCollection.Name = "pictureBoxCollection";
this.panel1.Controls.Add(this.ButtonAddAircraftGun); this.pictureBoxCollection.Size = new System.Drawing.Size(634, 439);
this.panel1.Controls.Add(this.ButtonRemoveAircraftGun); this.pictureBoxCollection.TabIndex = 0;
this.panel1.Location = new System.Drawing.Point(667, 12); this.pictureBoxCollection.TabStop = false;
this.panel1.Name = "panel1"; //
this.panel1.Size = new System.Drawing.Size(180, 436); // groupBox1
this.panel1.TabIndex = 0; //
this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Controls.Add(this.maskedTextBoxNumber);
this.groupBox1.Controls.Add(this.ButtonAddAircraftGun);
this.groupBox1.Controls.Add(this.ButtonRemoveAircraftGun);
this.groupBox1.Controls.Add(this.ButtonRefreshCollection);
this.groupBox1.Location = new System.Drawing.Point(658, 6);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(236, 437);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
// //
// maskedTextBoxNumber // maskedTextBoxNumber
// //
this.maskedTextBoxNumber.Location = new System.Drawing.Point(24, 80); this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 318);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(134, 23); this.maskedTextBoxNumber.Size = new System.Drawing.Size(156, 23);
this.maskedTextBoxNumber.TabIndex = 3; this.maskedTextBoxNumber.TabIndex = 5;
// //
// ButtonRefreshCollection // listBoxStorages
// //
this.ButtonRefreshCollection.Location = new System.Drawing.Point(3, 210); this.listBoxStorages.FormattingEnabled = true;
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.listBoxStorages.ItemHeight = 15;
this.ButtonRefreshCollection.Size = new System.Drawing.Size(174, 44); this.listBoxStorages.Location = new System.Drawing.Point(30, 86);
this.ButtonRefreshCollection.TabIndex = 2; this.listBoxStorages.Name = "listBoxStorages";
this.ButtonRefreshCollection.Text = "Обновить коллекцию"; this.listBoxStorages.Size = new System.Drawing.Size(156, 79);
this.ButtonRefreshCollection.UseVisualStyleBackColor = true; this.listBoxStorages.TabIndex = 4;
this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxStorages_SelectedIndexChanged);
//
// textBoxStorageName
//
this.textBoxStorageName.Location = new System.Drawing.Point(30, 26);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(156, 23);
this.textBoxStorageName.TabIndex = 3;
//
// ButtonAddObject
//
this.ButtonAddObject.Location = new System.Drawing.Point(30, 55);
this.ButtonAddObject.Name = "ButtonAddObject";
this.ButtonAddObject.Size = new System.Drawing.Size(156, 25);
this.ButtonAddObject.TabIndex = 2;
this.ButtonAddObject.Text = "Добавить набор";
this.ButtonAddObject.UseVisualStyleBackColor = true;
this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
//
// ButtonDelObject
//
this.ButtonDelObject.Location = new System.Drawing.Point(30, 171);
this.ButtonDelObject.Name = "ButtonDelObject";
this.ButtonDelObject.Size = new System.Drawing.Size(156, 25);
this.ButtonDelObject.TabIndex = 2;
this.ButtonDelObject.Text = "Удалить набор";
this.ButtonDelObject.UseVisualStyleBackColor = true;
this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
// //
// ButtonAddAircraftGun // ButtonAddAircraftGun
// //
this.ButtonAddAircraftGun.Location = new System.Drawing.Point(3, 12); this.ButtonAddAircraftGun.Location = new System.Drawing.Point(49, 285);
this.ButtonAddAircraftGun.Name = "ButtonAddAircraftGun"; this.ButtonAddAircraftGun.Name = "ButtonAddAircraftGun";
this.ButtonAddAircraftGun.Size = new System.Drawing.Size(174, 44); this.ButtonAddAircraftGun.Size = new System.Drawing.Size(156, 27);
this.ButtonAddAircraftGun.TabIndex = 0; this.ButtonAddAircraftGun.TabIndex = 2;
this.ButtonAddAircraftGun.Text = "Добавить зенитку"; this.ButtonAddAircraftGun.Text = "Добавить зенитку";
this.ButtonAddAircraftGun.UseVisualStyleBackColor = true; this.ButtonAddAircraftGun.UseVisualStyleBackColor = true;
this.ButtonAddAircraftGun.Click += new System.EventHandler(this.ButtonAddAircraftGun_Click); this.ButtonAddAircraftGun.Click += new System.EventHandler(this.ButtonAddAircraftGun_Click);
// //
// ButtonRemoveAircraftGun // ButtonRemoveAircraftGun
// //
this.ButtonRemoveAircraftGun.Location = new System.Drawing.Point(3, 135); this.ButtonRemoveAircraftGun.Location = new System.Drawing.Point(49, 347);
this.ButtonRemoveAircraftGun.Name = "ButtonRemoveAircraftGun"; this.ButtonRemoveAircraftGun.Name = "ButtonRemoveAircraftGun";
this.ButtonRemoveAircraftGun.Size = new System.Drawing.Size(174, 44); this.ButtonRemoveAircraftGun.Size = new System.Drawing.Size(156, 26);
this.ButtonRemoveAircraftGun.TabIndex = 1; this.ButtonRemoveAircraftGun.TabIndex = 2;
this.ButtonRemoveAircraftGun.Text = "Удалить зенитку"; this.ButtonRemoveAircraftGun.Text = "Удалить зенитку";
this.ButtonRemoveAircraftGun.UseVisualStyleBackColor = true; this.ButtonRemoveAircraftGun.UseVisualStyleBackColor = true;
this.ButtonRemoveAircraftGun.Click += new System.EventHandler(this.ButtonRemoveAircraftGun_Click); this.ButtonRemoveAircraftGun.Click += new System.EventHandler(this.ButtonRemoveAircraftGun_Click);
// //
// pictureBoxCollection // ButtonRefreshCollection
// //
this.pictureBoxCollection.Location = new System.Drawing.Point(2, 2); this.ButtonRefreshCollection.Location = new System.Drawing.Point(49, 379);
this.pictureBoxCollection.Name = "pictureBoxCollection"; this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(659, 490); this.ButtonRefreshCollection.Size = new System.Drawing.Size(156, 27);
this.pictureBoxCollection.TabIndex = 1; this.ButtonRefreshCollection.TabIndex = 2;
this.pictureBoxCollection.TabStop = false; this.ButtonRefreshCollection.Text = "Обновить коллекцию";
this.ButtonRefreshCollection.UseVisualStyleBackColor = true;
this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.listBoxStorages);
this.groupBox2.Controls.Add(this.textBoxStorageName);
this.groupBox2.Controls.Add(this.ButtonAddObject);
this.groupBox2.Controls.Add(this.ButtonDelObject);
this.groupBox2.Location = new System.Drawing.Point(19, 28);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(209, 222);
this.groupBox2.TabIndex = 6;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Наборы";
// //
// FormAircraftGunCollection // FormAircraftGunCollection
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(850, 504); this.ClientSize = new System.Drawing.Size(906, 450);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.pictureBoxCollection);
this.Controls.Add(this.panel1);
this.Name = "FormAircraftGunCollection"; this.Name = "FormAircraftGunCollection";
this.Text = "Набор зениток"; this.Text = "Набор зениток";
this.Load += new System.EventHandler(this.FormAircraftGunCollection_Load);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
} }
#endregion #endregion
private Panel panel1;
private Button ButtonRefreshCollection;
private Button ButtonAddAircraftGun;
private Button ButtonRemoveAircraftGun;
private PictureBox pictureBoxCollection; private PictureBox pictureBoxCollection;
private TextBox maskedTextBoxNumber; private GroupBox groupBox1;
private Button ButtonRefreshCollection;
private Button ButtonRemoveAircraftGun;
private Button ButtonAddAircraftGun;
private Button ButtonDelObject;
private Button ButtonAddObject;
private TextBox textBoxStorageName;
private ListBox listBoxStorages;
private MaskedTextBox maskedTextBoxNumber;
private GroupBox groupBox2;
} }
} }

View File

@ -1,4 +1,7 @@
using System; using AntiAircraftGun.DrawningObjects;
using AntiAircraftGun.Generics;
using AntiAircraftGun.MovementStrategy;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -7,37 +10,100 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using AntiAircraftGun.DrawningObjects;
using AntiAircraftGun.MovementStrategy;
using AntiAircraftGun.Generics;
namespace AntiAircraftGun namespace AntiAircraftGun
{ {
public partial class FormAircraftGunCollection : Form public partial class FormAircraftGunCollection : Form
{ {
private readonly AntiAircraftGunsGenericStorage _storage;
private readonly AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun> _aircraftguns; //private readonly AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun> _aircraftguns;
public FormAircraftGunCollection() public FormAircraftGunCollection()
{ {
InitializeComponent(); InitializeComponent();
_aircraftguns = new AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>(pictureBoxCollection.Width, pictureBoxCollection.Height); _storage = new AntiAircraftGunsGenericStorage(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 FormAircraftGunCollection_Load(object sender, EventArgs e) // Обновление рисунка по набору
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowAntiAircraftGuns();
}
// Удаление объекта из набора
private void ButtonRemoveAircraftGun_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)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удалён");
pictureBoxCollection.Image = obj.ShowAntiAircraftGuns();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
} }
// Добавление объекта в набор // Добавление объекта в набор
private void ButtonAddAircraftGun_Click(object sender, EventArgs e) private void ButtonAddAircraftGun_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormAntiAircraftGun form = new(); FormAntiAircraftGun form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if ((_aircraftguns + form.SelectedAircraftGun) == 0) if (obj + form.SelectedAircraftGun)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _aircraftguns.ShowAntiAircraftGuns(); pictureBoxCollection.Image = obj.ShowAntiAircraftGuns();
} }
else else
{ {
@ -46,29 +112,36 @@ namespace AntiAircraftGun
} }
} }
// Удаление объекта из набора // Удаление набора
private void ButtonRemoveAircraftGun_Click(object sender, EventArgs e) private void ButtonDelObject_Click(object sender, EventArgs e)
{ {
if (MessageBox.Show("удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) if (listBoxStorages.SelectedIndex == -1)
{ {
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxNumber.Text); if (MessageBox.Show($"удалить объект {listBoxStorages.SelectedItem}?", "удаление", MessageBoxButtons.YesNo,
if (_aircraftguns - pos != null) MessageBoxIcon.Question) == DialogResult.Yes)
{ {
MessageBox.Show("Объект удалён"); _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
pictureBoxCollection.Image = _aircraftguns.ShowAntiAircraftGuns(); ReloadObjects();
}
else
{
MessageBox.Show("Не удалось удалить объект");
} }
} }
// Обновление рисунка по набору // Добавление набора в коллекцию
private void ButtonRefreshCollection_Click(object sender, EventArgs e) private void ButtonAddObject_Click(object sender, EventArgs e)
{ {
pictureBoxCollection.Image = _aircraftguns.ShowAntiAircraftGuns(); 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]?.ShowAntiAircraftGuns();
} }
} }
} }

View File

@ -34,19 +34,19 @@ namespace AntiAircraftGun.Generics
} }
// Перегрузка оператора сложения // Перегрузка оператора сложения
public static int? operator +(AntiAircraftGunsGenericCollection<T, U> collect, T? obj) public static bool operator +(AntiAircraftGunsGenericCollection<T, U> collect, T? obj)
{ {
if (obj == null) if (obj == null)
{ {
return -1; return false;
} }
return collect?._collection.Insert(obj); return collect._collection.Insert(obj);
} }
// Перегрузка оператора вычитания // Перегрузка оператора вычитания
public static bool operator -(AntiAircraftGunsGenericCollection<T, U> collect, int pos) public static bool operator -(AntiAircraftGunsGenericCollection<T, U> collect, int pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj == null) if (obj == null)
{ {
return false; return false;
@ -58,7 +58,7 @@ namespace AntiAircraftGun.Generics
// Получение объекта IMoveableObject // Получение объекта IMoveableObject
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
// Вывод всего набора объектов // Вывод всего набора объектов
@ -90,15 +90,15 @@ namespace AntiAircraftGun.Generics
{ {
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++) int i = 0;
foreach (var antiaircraftgun in _collection.GetAntiAircraftGuns())
{ {
DrawningAircraftGun aircraftGun = _collection.Get(i); if (antiaircraftgun != null)
if(aircraftGun == null)
{ {
continue; antiaircraftgun.SetPosition((i % width) * _placeSizeWidth, (i / height) * _placeSizeHeight);
antiaircraftgun.DrawZenitka(g);
} }
aircraftGun.SetPosition((i % width) * _placeSizeWidth, (i / height) * _placeSizeHeight); i++;
aircraftGun.DrawZenitka(g);
} }
} }
} }

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntiAircraftGun.MovementStrategy;
using AntiAircraftGun.DrawningObjects;
namespace AntiAircraftGun.Generics
{
internal class AntiAircraftGunsGenericStorage
{
// Словарь
readonly Dictionary<string, AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>> _aircraftgunStorages;
// Возвращение списка названий наборов
public List<string> Keys => _aircraftgunStorages.Keys.ToList();
// Ширина окна отрисовки
private readonly int _pictureWidth;
// Высота окна отрисовки
private readonly int _pictureHeight;
// Конструктор
public AntiAircraftGunsGenericStorage(int pictureWidth, int pictureHeight)
{
_aircraftgunStorages = new Dictionary<string, AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
// Добавление набора
public void AddSet(string name)
{
if(_aircraftgunStorages.ContainsKey(name))
{
return;
}
_aircraftgunStorages[name] = new AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>(_pictureWidth, _pictureHeight);
}
// Удаление набора
public void DelSet(string name)
{
if (!_aircraftgunStorages.ContainsKey(name))
{
return;
}
_aircraftgunStorages.Remove(name);
}
// Доступ к набору
public AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>?this[string ind]
{
get
{
if(_aircraftgunStorages.ContainsKey(ind))
{
return _aircraftgunStorages[ind];
}
return null;
}
}
}
}

View File

@ -1,4 +1,7 @@
using System; using AntiAircraftGun.DrawningObjects;
using AntiAircraftGun.Generics;
using AntiAircraftGun.MovementStrategy;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -9,87 +12,90 @@ namespace AntiAircraftGun.Generics
internal class SetGeneric<T> internal class SetGeneric<T>
where T : class where T : class
{ {
// Массив // Список
private readonly T?[] _places; private readonly List<T?> _places;
// Кол-во объектов в массиве // Кол-во объектов в списке
public int Count => _places.Length; public int Count => _places.Count;
// Максимальное кол-во объектов в списке
private readonly int _maxCount;
// Конструктор // Конструктор
public SetGeneric(int Count) public SetGeneric(int Count)
{ {
_places = new T?[Count]; _maxCount = Count;
_places = new List<T?>(Count);
} }
// Добавление объекта в набор // Добавление объекта в набор
public int Insert(T antiaircraftgun) public bool Insert(T antiaircraftgun)
{ {
int i = 0; return Insert(antiaircraftgun, 0);
while(_places[i] != null)
{
i++;
if (i == Count)
{
return -1;
}
}
while (i != 0)
{
_places[i] = _places[i - 1];
i--;
}
_places[0] = antiaircraftgun;
return 0;
} }
// Добавление объекта на конкретную позицию // Добавление объекта на конкретную позицию
public bool Insert(T antiaircraftgun, int position) public bool Insert(T antiaircraftgun, int position)
{ {
if (position < 0 || position >= Count) if (position < 0 || position >= _maxCount)
{ {
return false; return false;
} }
if (_places[position] == null) if (Count >= _maxCount)
{
_places[position] = antiaircraftgun;
return true;
}
int index = position;
while (_places[index] != null)
{
index++;
}
if (index == Count)
{ {
return false; return false;
} }
for (int i = index; i > position; i--) _places.Insert(0, antiaircraftgun);
{
_places[i] = _places[i - 1];
}
_places[position] = antiaircraftgun;
return true; return true;
} }
// Удаление объекта из набора с конкретной позиции // Удаление объекта из набора с конкретной позиции
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position >= Count) if (position < 0 || position >= _maxCount)
{ {
return false; return false;
} }
_places[position] = null; if (position >= Count)
{
return false;
}
_places.RemoveAt(position);
return true; return true;
} }
// Получение объекта из набора по позиции // Получение объекта из набора по позиции
public T? Get(int position) public T? this[int position]
{ {
if (position < 0 || position >= Count) get
{ {
return null; if (position < 0 || position >= _maxCount)
{
return null;
}
return _places[position];
}
set
{
if (position < 0 || position >= _maxCount)
{
return;
}
_places[position] = value;
}
}
// Проход по списку
public IEnumerable<T?> GetAntiAircraftGuns(int? maxAntiAircraftGuns = null)
{
for (int i = 0; i < _places.Count; i++)
{
yield return _places[i];
if (maxAntiAircraftGuns.HasValue && i == maxAntiAircraftGuns.Value)
{
yield break;
}
} }
return _places[position];
} }
} }
} }