think Done
This commit is contained in:
parent
203daf9a16
commit
91a8bca853
@ -30,12 +30,18 @@
|
||||
{
|
||||
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
|
||||
this.panelTools = new System.Windows.Forms.Panel();
|
||||
this.textBox = new System.Windows.Forms.TextBox();
|
||||
this.panel1 = new System.Windows.Forms.Panel();
|
||||
this.textBoxStorageName = new System.Windows.Forms.TextBox();
|
||||
this.listBoxStorages = new System.Windows.Forms.ListBox();
|
||||
this.buttonAddCollection = new System.Windows.Forms.Button();
|
||||
this.buttonDelCollection = new System.Windows.Forms.Button();
|
||||
this.maskedTextBoxNumber = new System.Windows.Forms.TextBox();
|
||||
this.buttonUpdateColletion = new System.Windows.Forms.Button();
|
||||
this.buttonDeleteZenit = new System.Windows.Forms.Button();
|
||||
this.buttonAddZenit = new System.Windows.Forms.Button();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
|
||||
this.panelTools.SuspendLayout();
|
||||
this.panel1.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// pictureBoxCollection
|
||||
@ -48,7 +54,8 @@
|
||||
//
|
||||
// panelTools
|
||||
//
|
||||
this.panelTools.Controls.Add(this.textBox);
|
||||
this.panelTools.Controls.Add(this.panel1);
|
||||
this.panelTools.Controls.Add(this.maskedTextBoxNumber);
|
||||
this.panelTools.Controls.Add(this.buttonUpdateColletion);
|
||||
this.panelTools.Controls.Add(this.buttonDeleteZenit);
|
||||
this.panelTools.Controls.Add(this.buttonAddZenit);
|
||||
@ -58,16 +65,64 @@
|
||||
this.panelTools.TabIndex = 1;
|
||||
this.panelTools.Tag = "";
|
||||
//
|
||||
// textBox
|
||||
// panel1
|
||||
//
|
||||
this.textBox.Location = new System.Drawing.Point(23, 164);
|
||||
this.textBox.Name = "textBox";
|
||||
this.textBox.Size = new System.Drawing.Size(193, 23);
|
||||
this.textBox.TabIndex = 3;
|
||||
this.panel1.Controls.Add(this.textBoxStorageName);
|
||||
this.panel1.Controls.Add(this.listBoxStorages);
|
||||
this.panel1.Controls.Add(this.buttonAddCollection);
|
||||
this.panel1.Controls.Add(this.buttonDelCollection);
|
||||
this.panel1.Location = new System.Drawing.Point(15, 52);
|
||||
this.panel1.Name = "panel1";
|
||||
this.panel1.Size = new System.Drawing.Size(215, 294);
|
||||
this.panel1.TabIndex = 8;
|
||||
this.panel1.Tag = "Наборы";
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
this.textBoxStorageName.Location = new System.Drawing.Point(12, 53);
|
||||
this.textBoxStorageName.Name = "textBoxStorageName";
|
||||
this.textBoxStorageName.Size = new System.Drawing.Size(193, 23);
|
||||
this.textBoxStorageName.TabIndex = 7;
|
||||
//
|
||||
// listBoxStorages
|
||||
//
|
||||
this.listBoxStorages.FormattingEnabled = true;
|
||||
this.listBoxStorages.ItemHeight = 15;
|
||||
this.listBoxStorages.Location = new System.Drawing.Point(12, 137);
|
||||
this.listBoxStorages.Name = "listBoxStorages";
|
||||
this.listBoxStorages.Size = new System.Drawing.Size(191, 94);
|
||||
this.listBoxStorages.TabIndex = 6;
|
||||
//
|
||||
// buttonAddCollection
|
||||
//
|
||||
this.buttonAddCollection.Location = new System.Drawing.Point(10, 92);
|
||||
this.buttonAddCollection.Name = "buttonAddCollection";
|
||||
this.buttonAddCollection.Size = new System.Drawing.Size(193, 40);
|
||||
this.buttonAddCollection.TabIndex = 5;
|
||||
this.buttonAddCollection.Text = "Добавить набор";
|
||||
this.buttonAddCollection.UseVisualStyleBackColor = true;
|
||||
this.buttonAddCollection.Click += new System.EventHandler(this.ButtonAddObject_Click);
|
||||
//
|
||||
// buttonDelCollection
|
||||
//
|
||||
this.buttonDelCollection.Location = new System.Drawing.Point(12, 249);
|
||||
this.buttonDelCollection.Name = "buttonDelCollection";
|
||||
this.buttonDelCollection.Size = new System.Drawing.Size(193, 40);
|
||||
this.buttonDelCollection.TabIndex = 4;
|
||||
this.buttonDelCollection.Text = "Удалить набор";
|
||||
this.buttonDelCollection.UseVisualStyleBackColor = true;
|
||||
this.buttonDelCollection.Click += new System.EventHandler(this.ButtonDelObject_Click);
|
||||
//
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 435);
|
||||
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
this.maskedTextBoxNumber.Size = new System.Drawing.Size(193, 23);
|
||||
this.maskedTextBoxNumber.TabIndex = 3;
|
||||
//
|
||||
// buttonUpdateColletion
|
||||
//
|
||||
this.buttonUpdateColletion.Location = new System.Drawing.Point(23, 263);
|
||||
this.buttonUpdateColletion.Location = new System.Drawing.Point(23, 525);
|
||||
this.buttonUpdateColletion.Name = "buttonUpdateColletion";
|
||||
this.buttonUpdateColletion.Size = new System.Drawing.Size(193, 40);
|
||||
this.buttonUpdateColletion.TabIndex = 2;
|
||||
@ -77,7 +132,7 @@
|
||||
//
|
||||
// buttonDeleteZenit
|
||||
//
|
||||
this.buttonDeleteZenit.Location = new System.Drawing.Point(23, 193);
|
||||
this.buttonDeleteZenit.Location = new System.Drawing.Point(23, 464);
|
||||
this.buttonDeleteZenit.Name = "buttonDeleteZenit";
|
||||
this.buttonDeleteZenit.Size = new System.Drawing.Size(193, 40);
|
||||
this.buttonDeleteZenit.TabIndex = 1;
|
||||
@ -87,7 +142,7 @@
|
||||
//
|
||||
// buttonAddZenit
|
||||
//
|
||||
this.buttonAddZenit.Location = new System.Drawing.Point(23, 32);
|
||||
this.buttonAddZenit.Location = new System.Drawing.Point(23, 389);
|
||||
this.buttonAddZenit.Name = "buttonAddZenit";
|
||||
this.buttonAddZenit.Size = new System.Drawing.Size(193, 40);
|
||||
this.buttonAddZenit.TabIndex = 0;
|
||||
@ -107,6 +162,8 @@
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
|
||||
this.panelTools.ResumeLayout(false);
|
||||
this.panelTools.PerformLayout();
|
||||
this.panel1.ResumeLayout(false);
|
||||
this.panel1.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
@ -115,9 +172,14 @@
|
||||
|
||||
private PictureBox pictureBoxCollection;
|
||||
private Panel panelTools;
|
||||
private TextBox textBox;
|
||||
private TextBox maskedTextBoxNumber;
|
||||
private Button buttonUpdateColletion;
|
||||
private Button buttonDeleteZenit;
|
||||
private Button buttonAddZenit;
|
||||
private Panel panel1;
|
||||
private TextBox textBoxStorageName;
|
||||
private ListBox listBoxStorages;
|
||||
private Button buttonAddCollection;
|
||||
private Button buttonDelCollection;
|
||||
}
|
||||
}
|
@ -15,44 +15,130 @@ namespace AntiAircraftGun
|
||||
{
|
||||
public partial class FormAntiAirCraftGunCollection : Form
|
||||
{
|
||||
private readonly ZenitsGenericCollection<BaseDrawingAntiAirCraftGun,DrawingObjectAntiAirCraftGun> _zenits;
|
||||
private readonly AntiAirCraftGunGenericStorage _storage;
|
||||
public FormAntiAirCraftGunCollection()
|
||||
{
|
||||
InitializeComponent();
|
||||
_zenits = new ZenitsGenericCollection<BaseDrawingAntiAirCraftGun,
|
||||
DrawingObjectAntiAirCraftGun>(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
_storage = new AntiAirCraftGunGenericStorage(pictureBoxCollection.Width,pictureBoxCollection.Height);
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Заполнение listBoxObjects
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление набора в коллекцию
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonAddObject_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrEmpty(textBoxStorageName.Text))
|
||||
{
|
||||
MessageBox.Show("Не все данные заполнены", "Ошибка",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
_storage.AddSet(textBoxStorageName.Text);
|
||||
ReloadObjects();
|
||||
}
|
||||
/// <summary>
|
||||
/// Выбор набора
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ListBoxObjects_SelectedIndexChanged(object sender,
|
||||
EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image =
|
||||
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowZenits();
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление набора
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonDelObject_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();
|
||||
}
|
||||
}
|
||||
private void ButtonAddZenit_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
MessageBox.Show("Выберите набор в списке.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
MessageBox.Show("Выбранный набор не найден.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
FormAntiAirCraftGun form = new();
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_zenits + form.SelectedZenit != null)
|
||||
if (obj + form.SelectedZenit)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = _zenits.ShowZenits();
|
||||
pictureBoxCollection.Image = obj.ShowZenits();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonDeleteZenit_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int pos = Convert.ToInt32(textBox.Text);
|
||||
if (_zenits - pos != null)
|
||||
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 = _zenits.ShowZenits();
|
||||
pictureBoxCollection.Image = obj.ShowZenits();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -62,7 +148,17 @@ namespace AntiAircraftGun
|
||||
|
||||
private void ButtonUpdateCollection_Click(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image = _zenits.ShowZenits();
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowZenits();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace AntiAircraftGun.Generics
|
||||
{
|
||||
internal class ZenitsGenericCollection<T,U>
|
||||
internal class AntiAirCraftGunGenericCollection<T,U>
|
||||
where T : BaseDrawingAntiAirCraftGun
|
||||
where U : IMoveableObject
|
||||
{
|
||||
@ -37,7 +37,7 @@ namespace AntiAircraftGun.Generics
|
||||
/// </summary>
|
||||
/// <param name="picWidth"></param>
|
||||
/// <param name="picHeight"></param>
|
||||
public ZenitsGenericCollection(int picWidth, int picHeight)
|
||||
public AntiAirCraftGunGenericCollection(int picWidth, int picHeight)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
@ -51,11 +51,11 @@ namespace AntiAircraftGun.Generics
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static bool operator +(ZenitsGenericCollection<T, U> collect, T?
|
||||
obj)
|
||||
public static bool operator +(AntiAirCraftGunGenericCollection<T, U> collect, T? obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
MessageBox.Show("Объект пустой. Невозможно добавить.");
|
||||
return false;
|
||||
}
|
||||
return collect?._collection.Insert(obj) ?? false;
|
||||
@ -67,10 +67,9 @@ namespace AntiAircraftGun.Generics
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public static T? operator -(ZenitsGenericCollection<T, U> collect, int
|
||||
pos)
|
||||
public static T? operator -(AntiAirCraftGunGenericCollection<T, U> collect, int pos)
|
||||
{
|
||||
T? obj = collect._collection.Get(pos);
|
||||
T? obj = collect._collection[pos];
|
||||
if (obj != null)
|
||||
{
|
||||
collect._collection.Remove(pos);
|
||||
@ -84,7 +83,7 @@ namespace AntiAircraftGun.Generics
|
||||
/// <returns></returns>
|
||||
public U? GetU(int pos)
|
||||
{
|
||||
return (U?)_collection.Get(pos)?.GetMoveableObject;
|
||||
return (U?)_collection[pos]?.GetMoveableObject;
|
||||
}
|
||||
/// <summary>
|
||||
/// Вывод всего набора объектов
|
||||
@ -129,10 +128,11 @@ namespace AntiAircraftGun.Generics
|
||||
int maxX = _pictureWidth; // Максимальное значение X, при достижении которого будет ошибка
|
||||
int maxY = _pictureHeight; // Максимальное значение Y, когда нужно изменить X
|
||||
int distance_between_objects = _placeSizeHeight;
|
||||
for (int i = 0; i < _collection.Count; i++)
|
||||
|
||||
foreach (var zenit in _collection.GetZenits())
|
||||
{
|
||||
// TODO: Получение объекта
|
||||
T? obj = _collection.Get(i);
|
||||
T? obj = zenit;
|
||||
|
||||
if (obj != null)
|
||||
{
|
||||
@ -156,7 +156,7 @@ namespace AntiAircraftGun.Generics
|
||||
if (y + distance_between_objects >= maxY)
|
||||
{
|
||||
y = 0;
|
||||
x += obj.GetWidth+10 + distance_between_objects;
|
||||
x += obj.GetWidth + 10 + _placeSizeWidth;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
using AntiAircraftGun.DrawingObjects;
|
||||
using AntiAircraftGun.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AntiAircraftGun.Generics
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс для хранения коллекции
|
||||
/// </summary>
|
||||
internal class AntiAirCraftGunGenericStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
readonly Dictionary<string, AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>> _zenitStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий наборов
|
||||
/// </summary>
|
||||
public List<string> Keys => _zenitStorages.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 AntiAirCraftGunGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_zenitStorages = new Dictionary<string, AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void AddSet(string name)
|
||||
{
|
||||
// TODO: Прописать логику для добавления набора
|
||||
if (_zenitStorages.ContainsKey(name))
|
||||
{
|
||||
MessageBox.Show("Набор с таким именем уже существует");
|
||||
return;
|
||||
}
|
||||
|
||||
_zenitStorages.Add(name, new AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удаление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void DelSet(string name)
|
||||
{
|
||||
// TODO: Прописать логику для удаления набора
|
||||
if (_zenitStorages.ContainsKey(name) == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_zenitStorages.Remove(name);
|
||||
}
|
||||
/// <summary>
|
||||
/// Доступ к набору
|
||||
/// </summary>
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>?
|
||||
this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
// TODO: Продумать логику получения набора
|
||||
if (_zenitStorages.ContainsKey(ind))
|
||||
{
|
||||
return _zenitStorages[ind];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -12,18 +12,23 @@ namespace AntiAircraftGun.Generics
|
||||
/// <summary>
|
||||
/// Массив объектов, которые храним
|
||||
/// </summary>
|
||||
private readonly T?[] _places;
|
||||
private readonly List<T?> _places;
|
||||
/// <summary>
|
||||
/// Количество объектов в массиве
|
||||
/// Количество объектов в списке
|
||||
/// </summary>
|
||||
public int Count => _places.Length;
|
||||
public int Count => _places.Count;
|
||||
/// <summary>
|
||||
/// Максимальное количество объектов в списке
|
||||
/// </summary>
|
||||
private readonly int _maxCount;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new T?[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T?>(count);
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
@ -32,50 +37,21 @@ namespace AntiAircraftGun.Generics
|
||||
/// <returns></returns>
|
||||
public bool Insert(T car)
|
||||
{
|
||||
// Вставка в начало набора
|
||||
for (int i = 0; i < _places.Length; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
_places[i] = car;
|
||||
if (_places.Count == _maxCount)
|
||||
return false;
|
||||
Insert(car, 0);
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
return false; // Если нет пустых мест
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
/// /// </summary>
|
||||
/// <param name="car">Добавляемый автомобиль</param>
|
||||
/// <param name="position">Позиция</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public bool Insert(T car, int position)
|
||||
{
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= _places.Length)
|
||||
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
|
||||
return false;
|
||||
|
||||
// Проверка, что элемент массива по этой позиции пустой
|
||||
if (_places[position] != null)
|
||||
return false;
|
||||
|
||||
// Проверка, что после вставляемого элемента в массиве есть пустой элемент
|
||||
for (int i = position + 1; i < _places.Length; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
// Сдвиг всех объектов справа от позиции до первого пустого элемента
|
||||
for (int j = i; j > position; j--)
|
||||
{
|
||||
_places[j] = _places[j - 1];
|
||||
}
|
||||
_places[position] = car;
|
||||
_places.Insert(position, car);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false; // Если нет пустых мест справа
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
/// </summary>
|
||||
@ -84,7 +60,7 @@ namespace AntiAircraftGun.Generics
|
||||
public bool Remove(int position)
|
||||
{
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= _places.Length)
|
||||
if (position < 0 || position >= Count)
|
||||
return false;
|
||||
|
||||
// Удаление объекта из массива, присвоив элементу массива значение null
|
||||
@ -96,9 +72,54 @@ namespace AntiAircraftGun.Generics
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T? this[int position]
|
||||
{
|
||||
get
|
||||
{
|
||||
// TODO: Проверить позицию
|
||||
if (position < 0 || position >= Count)
|
||||
{
|
||||
MessageBox.Show("Индекс выходит за пределы диапазона");
|
||||
}
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
// TODO: Проверить позицию
|
||||
if (position < 0 || position >= Count)
|
||||
{
|
||||
throw new IndexOutOfRangeException("Индекс выходит за пределы диапазона");
|
||||
}
|
||||
// TODO: Проверить наличие доступных слотов в списке
|
||||
if (_places.Count < _maxCount)
|
||||
{
|
||||
// Вставить на указанную позицию
|
||||
_places.Insert(position, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Список уже заполнен");
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Проход по списку
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<T?> GetZenits(int? maxCars = null)
|
||||
{
|
||||
for (int i = 0; i < _places.Count; ++i)
|
||||
{
|
||||
yield return _places[i];
|
||||
if (maxCars.HasValue && i == maxCars.Value)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
public T? Get(int position)
|
||||
{
|
||||
if (position < 0 || position >= _places.Length)
|
||||
if (position < 0 || position >= Count)
|
||||
{
|
||||
// Позиция находится за пределами допустимого диапазона, вернем null
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user