PIbd23_Ivanova_S.V._Lab4 #6
@ -63,14 +63,13 @@ namespace MotorBoat.Generics
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static int operator +(BoatsGenericCollection<T, U> collect, T?
|
||||
obj)
|
||||
public static bool operator +(BoatsGenericCollection<T, U> collect, T? obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
return collect?._collection.Insert(obj) ?? -1;
|
||||
return collect?._collection.Insert(obj) ?? false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -79,15 +78,14 @@ namespace MotorBoat.Generics
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public static bool operator -(BoatsGenericCollection<T, U> collect, int
|
||||
pos)
|
||||
public static T? operator -(BoatsGenericCollection<T, U>? collect, int pos)
|
||||
{
|
||||
T? obj = collect._collection.Get(pos);
|
||||
T? obj = collect._collection[pos];
|
||||
if (obj != null)
|
||||
{
|
||||
collect._collection.Remove(pos);
|
||||
}
|
||||
return false;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -97,7 +95,7 @@ namespace MotorBoat.Generics
|
||||
/// <returns></returns>
|
||||
public U? GetU(int pos)
|
||||
{
|
||||
return (U?)_collection.Get(pos)?.GetMoveableObject;
|
||||
return (U?)_collection[pos]?.GetMoveableObject;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -122,15 +120,13 @@ namespace MotorBoat.Generics
|
||||
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);
|
||||
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);
|
||||
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth,
|
||||
_pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,16 +136,16 @@ namespace MotorBoat.Generics
|
||||
/// <param name="g"></param>
|
||||
private void DrawObjects(Graphics g)
|
||||
{
|
||||
T? t;
|
||||
int i = 0;
|
||||
int Rows = _pictureWidth / _placeSizeWidth;
|
||||
for (int i = 0; i < _collection.Count; i++)
|
||||
foreach(var boat in _collection.GetBoats())
|
||||
{
|
||||
t = _collection.Get(i);
|
||||
if (t != null)
|
||||
if (boat != null)
|
||||
{
|
||||
t.SetPosition(i % Rows * _placeSizeWidth, i / Rows * _placeSizeHeight + 5);
|
||||
t.DrawTransport(g);
|
||||
boat.SetPosition(i % Rows * _placeSizeWidth, i / Rows * _placeSizeHeight + 5);
|
||||
boat.DrawTransport(g);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
86
MotorBoat/MotorBoat/BoatsGenericStorage.cs
Normal file
86
MotorBoat/MotorBoat/BoatsGenericStorage.cs
Normal file
@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MotorBoat.DrawningObjects;
|
||||
using MotorBoat.MovementStrategy;
|
||||
|
||||
namespace MotorBoat.Generics
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс для хранения коллекции
|
||||
/// </summary>
|
||||
internal class BoatsGenericStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
readonly Dictionary<string, BoatsGenericCollection<DrawningBoat,DrawningObjectBoat>> _boatStorages;
|
||||
|
||||
/// <summary>
|
||||
/// Возвращение списка названий наборов
|
||||
/// </summary>
|
||||
public List<string> Keys => _boatStorages.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 BoatsGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_boatStorages = new Dictionary<string, BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void AddSet(string name)
|
||||
{
|
||||
if (_boatStorages.ContainsKey(name))
|
||||
return;
|
||||
_boatStorages[name] = new BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>(_pictureWidth, _pictureHeight);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удаление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void DelSet(string name)
|
||||
{
|
||||
if (!_boatStorages.ContainsKey(name))
|
||||
return;
|
||||
_boatStorages.Remove(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Доступ к набору
|
||||
/// </summary>
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>?
|
||||
this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_boatStorages.ContainsKey(ind))
|
||||
return _boatStorages[ind];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
82
MotorBoat/MotorBoat/FormBoatCollection.Designer.cs
generated
82
MotorBoat/MotorBoat/FormBoatCollection.Designer.cs
generated
@ -34,8 +34,14 @@
|
||||
ButtonRemoveBoat = new Button();
|
||||
ButtonRefreshCollection = new Button();
|
||||
groupBoxTools = new GroupBox();
|
||||
groupBoxCollections = new GroupBox();
|
||||
ButtonRemoveObject = new Button();
|
||||
listBoxStorages = new ListBox();
|
||||
ButtonAddObject = new Button();
|
||||
textBoxStorageName = new MaskedTextBox();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||
groupBoxTools.SuspendLayout();
|
||||
groupBoxCollections.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// pictureBoxCollection
|
||||
@ -43,20 +49,20 @@
|
||||
pictureBoxCollection.Dock = DockStyle.Left;
|
||||
pictureBoxCollection.Location = new Point(0, 0);
|
||||
pictureBoxCollection.Name = "pictureBoxCollection";
|
||||
pictureBoxCollection.Size = new Size(578, 461);
|
||||
pictureBoxCollection.Size = new Size(578, 500);
|
||||
pictureBoxCollection.TabIndex = 1;
|
||||
pictureBoxCollection.TabStop = false;
|
||||
//
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
maskedTextBoxNumber.Location = new Point(54, 135);
|
||||
maskedTextBoxNumber.Location = new Point(51, 371);
|
||||
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
maskedTextBoxNumber.Size = new Size(100, 23);
|
||||
maskedTextBoxNumber.TabIndex = 0;
|
||||
//
|
||||
// ButtonAddBoat
|
||||
//
|
||||
ButtonAddBoat.Location = new Point(6, 22);
|
||||
ButtonAddBoat.Location = new Point(6, 325);
|
||||
ButtonAddBoat.Name = "ButtonAddBoat";
|
||||
ButtonAddBoat.Size = new Size(188, 40);
|
||||
ButtonAddBoat.TabIndex = 1;
|
||||
@ -66,9 +72,9 @@
|
||||
//
|
||||
// ButtonRemoveBoat
|
||||
//
|
||||
ButtonRemoveBoat.Location = new Point(11, 171);
|
||||
ButtonRemoveBoat.Location = new Point(6, 407);
|
||||
ButtonRemoveBoat.Name = "ButtonRemoveBoat";
|
||||
ButtonRemoveBoat.Size = new Size(183, 41);
|
||||
ButtonRemoveBoat.Size = new Size(188, 41);
|
||||
ButtonRemoveBoat.TabIndex = 2;
|
||||
ButtonRemoveBoat.Text = "Удалить лодку";
|
||||
ButtonRemoveBoat.UseVisualStyleBackColor = true;
|
||||
@ -76,9 +82,9 @@
|
||||
//
|
||||
// ButtonRefreshCollection
|
||||
//
|
||||
ButtonRefreshCollection.Location = new Point(11, 218);
|
||||
ButtonRefreshCollection.Location = new Point(6, 454);
|
||||
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
|
||||
ButtonRefreshCollection.Size = new Size(183, 39);
|
||||
ButtonRefreshCollection.Size = new Size(188, 39);
|
||||
ButtonRefreshCollection.TabIndex = 3;
|
||||
ButtonRefreshCollection.Text = "Обновить коллекцию";
|
||||
ButtonRefreshCollection.UseVisualStyleBackColor = true;
|
||||
@ -86,6 +92,7 @@
|
||||
//
|
||||
// groupBoxTools
|
||||
//
|
||||
groupBoxTools.Controls.Add(groupBoxCollections);
|
||||
groupBoxTools.Controls.Add(ButtonRefreshCollection);
|
||||
groupBoxTools.Controls.Add(ButtonRemoveBoat);
|
||||
groupBoxTools.Controls.Add(ButtonAddBoat);
|
||||
@ -93,16 +100,66 @@
|
||||
groupBoxTools.Dock = DockStyle.Right;
|
||||
groupBoxTools.Location = new Point(584, 0);
|
||||
groupBoxTools.Name = "groupBoxTools";
|
||||
groupBoxTools.Size = new Size(200, 461);
|
||||
groupBoxTools.Size = new Size(200, 500);
|
||||
groupBoxTools.TabIndex = 0;
|
||||
groupBoxTools.TabStop = false;
|
||||
groupBoxTools.Text = "Инструменты";
|
||||
//
|
||||
// groupBoxCollections
|
||||
//
|
||||
groupBoxCollections.Controls.Add(ButtonRemoveObject);
|
||||
groupBoxCollections.Controls.Add(listBoxStorages);
|
||||
groupBoxCollections.Controls.Add(ButtonAddObject);
|
||||
groupBoxCollections.Controls.Add(textBoxStorageName);
|
||||
groupBoxCollections.Location = new Point(6, 22);
|
||||
groupBoxCollections.Name = "groupBoxCollections";
|
||||
groupBoxCollections.Size = new Size(188, 261);
|
||||
groupBoxCollections.TabIndex = 4;
|
||||
groupBoxCollections.TabStop = false;
|
||||
groupBoxCollections.Text = "Наборы";
|
||||
//
|
||||
// ButtonRemoveObject
|
||||
//
|
||||
ButtonRemoveObject.Location = new Point(6, 214);
|
||||
ButtonRemoveObject.Name = "ButtonRemoveObject";
|
||||
ButtonRemoveObject.Size = new Size(176, 41);
|
||||
ButtonRemoveObject.TabIndex = 3;
|
||||
ButtonRemoveObject.Text = "Удалить набор";
|
||||
ButtonRemoveObject.UseVisualStyleBackColor = true;
|
||||
ButtonRemoveObject.Click += ButtonRemoveObject_Click;
|
||||
//
|
||||
// listBoxStorages
|
||||
//
|
||||
listBoxStorages.FormattingEnabled = true;
|
||||
listBoxStorages.ItemHeight = 15;
|
||||
listBoxStorages.Location = new Point(6, 99);
|
||||
listBoxStorages.Name = "listBoxStorages";
|
||||
listBoxStorages.Size = new Size(176, 109);
|
||||
listBoxStorages.TabIndex = 2;
|
||||
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
|
||||
//
|
||||
// ButtonAddObject
|
||||
//
|
||||
ButtonAddObject.Location = new Point(6, 54);
|
||||
ButtonAddObject.Name = "ButtonAddObject";
|
||||
ButtonAddObject.Size = new Size(176, 39);
|
||||
ButtonAddObject.TabIndex = 1;
|
||||
ButtonAddObject.Text = "Добавить набор";
|
||||
ButtonAddObject.UseVisualStyleBackColor = true;
|
||||
ButtonAddObject.Click += buttonAddObject_Click;
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
textBoxStorageName.Location = new Point(6, 25);
|
||||
textBoxStorageName.Name = "textBoxStorageName";
|
||||
textBoxStorageName.Size = new Size(176, 23);
|
||||
textBoxStorageName.TabIndex = 0;
|
||||
//
|
||||
// FormBoatCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(784, 461);
|
||||
ClientSize = new Size(784, 500);
|
||||
Controls.Add(groupBoxTools);
|
||||
Controls.Add(pictureBoxCollection);
|
||||
Name = "FormBoatCollection";
|
||||
@ -110,6 +167,8 @@
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||
groupBoxTools.ResumeLayout(false);
|
||||
groupBoxTools.PerformLayout();
|
||||
groupBoxCollections.ResumeLayout(false);
|
||||
groupBoxCollections.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
}
|
||||
|
||||
@ -121,5 +180,10 @@
|
||||
private Button ButtonRemoveBoat;
|
||||
private Button ButtonRefreshCollection;
|
||||
private GroupBox groupBoxTools;
|
||||
private GroupBox groupBoxCollections;
|
||||
private Button ButtonRemoveObject;
|
||||
private ListBox listBoxStorages;
|
||||
private Button ButtonAddObject;
|
||||
private MaskedTextBox textBoxStorageName;
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@ namespace MotorBoat
|
||||
/// <summary>
|
||||
/// Набор объектов
|
||||
/// </summary>
|
||||
private readonly BoatsGenericCollection<DrawningBoat, DrawningObjectBoat> _boats;
|
||||
private readonly BoatsGenericStorage _storage;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
@ -29,7 +29,74 @@ namespace MotorBoat
|
||||
public FormBoatCollection()
|
||||
{
|
||||
InitializeComponent();
|
||||
_boats = new BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
_storage = new BoatsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Заполнение listBoxStorages
|
||||
/// </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]?.ShowBoats();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удаление набора
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonRemoveObject_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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -39,13 +106,21 @@ namespace MotorBoat
|
||||
/// <param name="e"></param>
|
||||
private void ButtonAddBoat_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
return;
|
||||
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
|
||||
if (obj == null)
|
||||
return;
|
||||
|
||||
FormMotorBoat form = new();
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_boats + form.SelectedBoat != -1)
|
||||
if (obj + form.SelectedBoat)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = _boats.ShowBoats();
|
||||
pictureBoxCollection.Image = obj.ShowBoats();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -61,22 +136,29 @@ namespace MotorBoat
|
||||
/// <param name="e"></param>
|
||||
private void ButtonRemoveBoat_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 (_boats - pos != null)
|
||||
if (obj - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = _boats.ShowBoats();
|
||||
pictureBoxCollection.Image = obj.ShowBoats();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -86,7 +168,15 @@ namespace MotorBoat
|
||||
/// <param name="e"></param>
|
||||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image = _boats.ShowBoats();
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
return;
|
||||
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
|
||||
if (obj == null)
|
||||
return;
|
||||
|
||||
pictureBoxCollection.Image = obj.ShowBoats();
|
||||
}
|
||||
}
|
||||
}
|
@ -14,14 +14,19 @@ namespace MotorBoat.Generics
|
||||
where T : class
|
||||
{
|
||||
/// <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>
|
||||
/// Конструктор
|
||||
@ -29,47 +34,40 @@ namespace MotorBoat.Generics
|
||||
/// <param name="count"></param>
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new T?[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T?>(count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
/// </summary>
|
||||
/// <param name="boat">Добавляемый автомобиль</param>
|
||||
/// <param name="boat">Добавляемая лодка</param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T boat)
|
||||
public bool Insert(T boat)
|
||||
{
|
||||
return Insert(boat, 0);
|
||||
if (_places.Count == _maxCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Insert(boat, 0);
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
/// </summary>
|
||||
/// <param name="car">Добавляемый автомобиль</param>
|
||||
/// <param name="boat">Добавляемая лодка</param>
|
||||
/// <param name="position">Позиция</param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T boat, int position)
|
||||
public bool Insert(T boat, int position)
|
||||
{
|
||||
int nullIndex = -1, i;
|
||||
if (position < 0 || position >= Count)
|
||||
if(!(position >= 0 && position <= Count && _places.Count < _maxCount))
|
||||
{
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
for (i = position; i < Count; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
nullIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nullIndex < 0) return -1;
|
||||
for (i = nullIndex; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[position] = boat;
|
||||
return position;
|
||||
_places.Insert(position, boat);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
/// </summary>
|
||||
@ -78,7 +76,7 @@ namespace MotorBoat.Generics
|
||||
public bool Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= Count) return false;
|
||||
_places[position] = null;
|
||||
_places.RemoveAt(position);
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
@ -86,10 +84,40 @@ namespace MotorBoat.Generics
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T? Get(int position)
|
||||
public T? this[int position]
|
||||
{
|
||||
if (position < 0 || position >= Count) return null;
|
||||
return _places[position];
|
||||
get
|
||||
{
|
||||
if (position < 0 || position > _maxCount)
|
||||
return null;
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
|
||||
{
|
||||
return;
|
||||
}
|
||||
_places.Insert(position, value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Проход по списку
|
||||
/// </summary>
|
||||
/// <param name="maxShips"></param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<T> GetBoats(int? maxBoats = null)
|
||||
{
|
||||
for (int i = 0; i < _places.Count; ++i)
|
||||
{
|
||||
yield return _places[i];
|
||||
if (maxBoats.HasValue && i == maxBoats.Value)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user