ISEbd-21. Pyatkin I.A. Lab work 04_hard. #11

Closed
IvanPyatkin wants to merge 1 commits from lab04hard into lab03hard
7 changed files with 404 additions and 81 deletions

View File

@ -12,7 +12,7 @@ namespace ProjectExcavator.MovementStrategy
/// </summary> /// </summary>
public class DrawingObjectExcavator : IMoveableObject public class DrawingObjectExcavator : IMoveableObject
{ {
private readonly DrawingExcavator? _drawingExcavator = null; public DrawingExcavator _drawingExcavator { get; private set; }
public DrawingObjectExcavator(DrawingExcavator drawingExcavator) public DrawingObjectExcavator(DrawingExcavator drawingExcavator)
{ {
_drawingExcavator = drawingExcavator; _drawingExcavator = drawingExcavator;

View File

@ -56,32 +56,33 @@ namespace ProjectExcavator.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <returns></returns>
public static int operator +(ExcavatorGenericCollection<T, U> collect, T? public static bool operator +(ExcavatorGenericCollection<T, U> collect, T?
obj) obj)
{ {
if (obj == null) if (obj == null)
{ {
return -1; return false;
} }
return collect?._collection.Insert(obj) ?? -1; return collect?._collection.Insert(obj) ?? false;
} }
/// <summary> /// <summary>
/// Перегрузка оператора вычитания /// Перегрузка оператора вычитания
/// </summary> /// </summary>
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="pos"></param> /// <param name="pos"></param>
/// <returns></returns> /// <returns></returns>
public static bool operator -(ExcavatorGenericCollection<T, U> collect, int public static T? operator -(ExcavatorGenericCollection<T, U> collect, int
pos) pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
{ {
collect._collection.Remove(pos); collect._collection.Remove(pos);
return true;
} }
return false; return obj;
} }
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
/// </summary> /// </summary>
@ -89,7 +90,8 @@ namespace ProjectExcavator.Generics
/// <returns></returns> /// <returns></returns>
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
/// <summary> /// <summary>
/// Вывод всего набора объектов /// Вывод всего набора объектов
@ -133,12 +135,15 @@ namespace ProjectExcavator.Generics
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++) for (int i = 0; i < _collection.Count; i++)
{ {
DrawingExcavator? excavator = _collection.Get(i); // TODO получение объекта
T? excavator = _collection[i];
if (excavator == null) if (excavator == null)
continue; continue;
int r = i / width; int r = i / width;
int s = width - 1 - (i % width); int s = width - 1 - (i % width);
// TODO установка позиции
excavator.SetPosition(s * _placeSizeWidth, r * _placeSizeHeight); excavator.SetPosition(s * _placeSizeWidth, r * _placeSizeHeight);
// TODO прорисовка объекта
excavator.DrawTransport(g); excavator.DrawTransport(g);
} }
} }

View File

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectExcavator.DrawingObjects;
using ProjectExcavator.MovementStrategy;
namespace ProjectExcavator.Generics
{
internal class ExcavatorGenericStorage
{
readonly Dictionary<string, ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>> _excavatorStorages;
public List<string> Keys => _excavatorStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public ExcavatorGenericStorage(int pictureWidth, int pictureHeight)
{
_excavatorStorages = new Dictionary<string, ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_removedObjects = new LinkedList<DrawingObjectExcavator>();
}
public void AddSet(string name)
{
if (!_excavatorStorages.ContainsKey(name))
{
ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator> newSet = new ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>(_pictureWidth, _pictureHeight);
_excavatorStorages.Add(name, newSet);
}
}
public void DelSet(string name)
{
if (_excavatorStorages.ContainsKey(name))
{
_excavatorStorages.Remove(name);
}
}
public ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>? this[string ind]
{
get
{
if (_excavatorStorages.ContainsKey(ind))
{
return _excavatorStorages[ind];
}
return null;
}
}
public DrawingObjectExcavator? this[string dictIndex, int objIndex]
{
get
{
if (_excavatorStorages.ContainsKey(dictIndex))
{
var selectedDictElement = _excavatorStorages[dictIndex];
var selectedObject = selectedDictElement.GetU(objIndex);
return selectedObject;
}
return null;
}
}
private LinkedList<DrawingObjectExcavator> _removedObjects;
public DrawingObjectExcavator RemovedObject
{
set
{
_removedObjects.AddLast(value);
}
get
{
if (_removedObjects == null || _removedObjects.Count == 0)
{
return null;
}
var removedObject = _removedObjects.Last();
_removedObjects.RemoveLast();
return removedObject;
}
}
}
}

View File

@ -15,12 +15,13 @@ namespace ProjectExcavator
public FormExcavator() public FormExcavator()
{ {
InitializeComponent(); InitializeComponent();
comboBoxStrategy.Items.Add("Öåíòð");
comboBoxStrategy.Items.Add("Ãðàíèöà");
} }
public FormExcavator(DrawingExcavator drawingObject)
private void Draw() {
_drawingExcavator = drawingObject;
InitializeComponent();
}
public void Draw()
{ {
if (_drawingExcavator == null) if (_drawingExcavator == null)
{ {

View File

@ -34,9 +34,16 @@
this.buttonRemoveEx = new System.Windows.Forms.Button(); this.buttonRemoveEx = new System.Windows.Forms.Button();
this.buttonRefreshCollection = new System.Windows.Forms.Button(); this.buttonRefreshCollection = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.buttonDelObject = new System.Windows.Forms.Button();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.buttonAddObject = new System.Windows.Forms.Button();
this.buttonGeneration = new System.Windows.Forms.Button(); this.buttonGeneration = new System.Windows.Forms.Button();
this.buttonRemoveobj = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// pictureBoxCollection // pictureBoxCollection
@ -50,14 +57,14 @@
// //
// maskedTextBoxNumber // maskedTextBoxNumber
// //
this.maskedTextBoxNumber.Location = new System.Drawing.Point(26, 125); this.maskedTextBoxNumber.Location = new System.Drawing.Point(35, 339);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 23); this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 23);
this.maskedTextBoxNumber.TabIndex = 1; this.maskedTextBoxNumber.TabIndex = 1;
// //
// buttonAddEx // buttonAddEx
// //
this.buttonAddEx.Location = new System.Drawing.Point(6, 57); this.buttonAddEx.Location = new System.Drawing.Point(13, 310);
this.buttonAddEx.Name = "buttonAddEx"; this.buttonAddEx.Name = "buttonAddEx";
this.buttonAddEx.Size = new System.Drawing.Size(150, 23); this.buttonAddEx.Size = new System.Drawing.Size(150, 23);
this.buttonAddEx.TabIndex = 2; this.buttonAddEx.TabIndex = 2;
@ -67,36 +74,89 @@
// //
// buttonRemoveEx // buttonRemoveEx
// //
this.buttonRemoveEx.Location = new System.Drawing.Point(6, 166); this.buttonRemoveEx.Location = new System.Drawing.Point(13, 368);
this.buttonRemoveEx.Name = "buttonRemoveEx"; this.buttonRemoveEx.Name = "buttonRemoveEx";
this.buttonRemoveEx.Size = new System.Drawing.Size(150, 23); this.buttonRemoveEx.Size = new System.Drawing.Size(150, 23);
this.buttonRemoveEx.TabIndex = 3; this.buttonRemoveEx.TabIndex = 3;
this.buttonRemoveEx.Text = "Удалить экскаватор"; this.buttonRemoveEx.Text = "Удалить экскаватор";
this.buttonRemoveEx.UseVisualStyleBackColor = true; this.buttonRemoveEx.UseVisualStyleBackColor = true;
this.buttonRemoveEx.Click += new System.EventHandler(this.ButtonRemoveEx_Click);
// //
// buttonRefreshCollection // buttonRefreshCollection
// //
this.buttonRefreshCollection.Location = new System.Drawing.Point(6, 230); this.buttonRefreshCollection.Location = new System.Drawing.Point(13, 432);
this.buttonRefreshCollection.Name = "buttonRefreshCollection"; this.buttonRefreshCollection.Name = "buttonRefreshCollection";
this.buttonRefreshCollection.Size = new System.Drawing.Size(148, 23); this.buttonRefreshCollection.Size = new System.Drawing.Size(148, 23);
this.buttonRefreshCollection.TabIndex = 4; this.buttonRefreshCollection.TabIndex = 4;
this.buttonRefreshCollection.Text = "Обновить коллекцию"; this.buttonRefreshCollection.Text = "Обновить коллекцию";
this.buttonRefreshCollection.UseVisualStyleBackColor = true; this.buttonRefreshCollection.UseVisualStyleBackColor = true;
this.buttonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
// //
// groupBox1 // groupBox1
// //
this.groupBox1.Controls.Add(this.buttonGeneration); this.groupBox1.Controls.Add(this.buttonRemoveobj);
this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Controls.Add(this.buttonAddEx); this.groupBox1.Controls.Add(this.buttonAddEx);
this.groupBox1.Controls.Add(this.buttonRefreshCollection); this.groupBox1.Controls.Add(this.buttonRefreshCollection);
this.groupBox1.Controls.Add(this.maskedTextBoxNumber); this.groupBox1.Controls.Add(this.maskedTextBoxNumber);
this.groupBox1.Controls.Add(this.buttonRemoveEx); this.groupBox1.Controls.Add(this.buttonRemoveEx);
this.groupBox1.Location = new System.Drawing.Point(749, 0); this.groupBox1.Location = new System.Drawing.Point(741, 0);
this.groupBox1.Name = "groupBox1"; this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(160, 461); this.groupBox1.Size = new System.Drawing.Size(168, 461);
this.groupBox1.TabIndex = 5; this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false; this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты"; this.groupBox1.Text = "Инструменты";
// //
// groupBox2
//
this.groupBox2.Controls.Add(this.textBoxStorageName);
this.groupBox2.Controls.Add(this.buttonDelObject);
this.groupBox2.Controls.Add(this.listBoxStorages);
this.groupBox2.Controls.Add(this.buttonAddObject);
this.groupBox2.Location = new System.Drawing.Point(13, 28);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(143, 214);
this.groupBox2.TabIndex = 5;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Наборы";
//
// textBoxStorageName
//
this.textBoxStorageName.Location = new System.Drawing.Point(15, 30);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(122, 23);
this.textBoxStorageName.TabIndex = 6;
//
// buttonDelObject
//
this.buttonDelObject.Location = new System.Drawing.Point(15, 185);
this.buttonDelObject.Name = "buttonDelObject";
this.buttonDelObject.Size = new System.Drawing.Size(122, 23);
this.buttonDelObject.TabIndex = 8;
this.buttonDelObject.Text = "Удалить набор";
this.buttonDelObject.UseVisualStyleBackColor = true;
this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
//
// listBoxStorages
//
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 15;
this.listBoxStorages.Location = new System.Drawing.Point(15, 88);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(122, 79);
this.listBoxStorages.TabIndex = 6;
this.listBoxStorages.Click += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged);
//
// buttonAddObject
//
this.buttonAddObject.Location = new System.Drawing.Point(15, 59);
this.buttonAddObject.Name = "buttonAddObject";
this.buttonAddObject.Size = new System.Drawing.Size(122, 23);
this.buttonAddObject.TabIndex = 7;
this.buttonAddObject.Text = "Добавить набор";
this.buttonAddObject.UseVisualStyleBackColor = true;
this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
//
// buttonGeneration // buttonGeneration
// //
this.buttonGeneration.Location = new System.Drawing.Point(6, 332); this.buttonGeneration.Location = new System.Drawing.Point(6, 332);
@ -107,6 +167,16 @@
this.buttonGeneration.UseVisualStyleBackColor = true; this.buttonGeneration.UseVisualStyleBackColor = true;
this.buttonGeneration.Click += new System.EventHandler(this.buttonGeneration_Click); this.buttonGeneration.Click += new System.EventHandler(this.buttonGeneration_Click);
// //
// buttonRemoveobj
//
this.buttonRemoveobj.Location = new System.Drawing.Point(16, 265);
this.buttonRemoveobj.Name = "buttonRemoveobj";
this.buttonRemoveobj.Size = new System.Drawing.Size(145, 23);
this.buttonRemoveobj.TabIndex = 6;
this.buttonRemoveobj.Text = "Удалённые обьекты";
this.buttonRemoveobj.UseVisualStyleBackColor = true;
this.buttonRemoveobj.Click += new System.EventHandler(this.buttonRemoveobj_Click);
//
// FormExcavatorCollection // FormExcavatorCollection
// //
this.ClientSize = new System.Drawing.Size(909, 461); this.ClientSize = new System.Drawing.Size(909, 461);
@ -116,6 +186,8 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.groupBox1.ResumeLayout(false); this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout(); this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
} }
@ -128,6 +200,12 @@
private Button buttonRemoveEx; private Button buttonRemoveEx;
private Button buttonRefreshCollection; private Button buttonRefreshCollection;
private GroupBox groupBox1; private GroupBox groupBox1;
private GroupBox groupBox2;
private TextBox textBoxStorageName;
private Button buttonDelObject;
private ListBox listBoxStorages;
private Button buttonAddObject;
private Button buttonGeneration; private Button buttonGeneration;
private Button buttonRemoveobj;
} }
} }

View File

@ -12,16 +12,105 @@ namespace ProjectExcavator
/// <summary> /// <summary>
/// Набор объектов /// Набор объектов
/// </summary> /// </summary>
private readonly ExcavatorGenericCollection<DrawingExcavator, private readonly ExcavatorGenericStorage _storage;
DrawingObjectExcavator> _excavator;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormExcavatorCollection() public FormExcavatorCollection()
{ {
InitializeComponent(); InitializeComponent();
_excavator = new ExcavatorGenericCollection<DrawingExcavator, _storage = new ExcavatorGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
DrawingObjectExcavator>(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();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowExcavator();
}
/// <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]?.ShowExcavator();
}
/// <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();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
MessageBox.Show("Набор удален");
pictureBoxCollection.Image = obj.ShowExcavator();
}
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
@ -30,14 +119,23 @@ namespace ProjectExcavator
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonAddEx_Click(object sender, EventArgs e) private void ButtonAddEx_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FormExcavator form = new(); FormExcavator form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
int result = _excavator + form.SelectedExcavator; if (obj + form.SelectedExcavator)
if (result != -2)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _excavator.ShowExcavator(); pictureBoxCollection.Image = obj.ShowExcavator();
} }
else else
{ {
@ -52,16 +150,28 @@ namespace ProjectExcavator
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRemoveEx_Click(object sender, EventArgs e) private void ButtonRemoveEx_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("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxNumber.Text); int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_excavator - pos != null) var removableObject = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty, pos];
if (_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _excavator.ShowExcavator(); _storage.RemovedObject = (DrawingObjectExcavator)removableObject;
pictureBoxCollection.Image = obj.ShowExcavator();
} }
else else
{ {
@ -75,7 +185,17 @@ namespace ProjectExcavator
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e) private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{ {
pictureBoxCollection.Image = _excavator.ShowExcavator(); if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowExcavator();
} }
private void buttonGeneration_Click(object sender, EventArgs e) private void buttonGeneration_Click(object sender, EventArgs e)
@ -83,5 +203,18 @@ namespace ProjectExcavator
RandGeneration form = new(); RandGeneration form = new();
form.ShowDialog(); form.ShowDialog();
} }
private void buttonRemoveobj_Click(object sender, EventArgs e)
{
DrawingObjectExcavator removedObject = _storage.RemovedObject;
if (removedObject == null)
{
MessageBox.Show("Не удалось показать удаленные объекты");
return;
}
FormExcavator formRemovedObject = new FormExcavator(removedObject._drawingExcavator);
formRemovedObject.Show();
formRemovedObject.Draw();
}
} }
} }

View File

@ -14,34 +14,40 @@ namespace ProjectExcavator.Generics
where T : class where T : class
{ {
/// <summary> /// <summary>
/// Массив объектов, которые храним /// Список объектов, которые храним
/// </summary> /// </summary>
private readonly T?[] _places; private readonly List<T?> _places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в массиве
/// </summary> /// </summary>
public int Count => _places.Length; public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(count);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="excavator">Добавляемый экскаватор</param> /// <param name="excavator">Добавляемый экскаватор</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T excavator) public bool Insert(T excavator)
{ {
for (int i = _places.Length - 1; i > 0; i--) // TODO вставка в начало набора
if (_places.Count >= _maxCount)
{ {
_places[i] = _places[i - 1]; return false;
} }
_places[0] = excavator; _places.Insert(0, excavator);
return Insert(excavator, 0); return true;
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -49,66 +55,83 @@ namespace ProjectExcavator.Generics
/// <param name="excavator">Добавляемый экскаватор</param> /// <param name="excavator">Добавляемый экскаватор</param>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T excavator, int position) public bool Insert(T excavator, int position)
{ {
if (position < 0 || position >= _places.Length) // TODO проверка позиции
if (position < 0 || position >= _places.Count)
{ {
return -1; return false;
} }
// TODO проверка, что есть место для вставки
if (_places[position] != null) if (_places.Count >= _maxCount)
{ {
return -1; return false;
} }
// TODO вставка по позиции
int NullIndex = 0; _places.Insert(position, excavator);
for (int i = position; i < _places.Length; i++) return true;
{
if (_places[i] == null)
{
NullIndex = i;
break;
}
}
if (NullIndex == 0)
{
return -1;
}
for (int i = NullIndex; i >= position; i--)
{
_places[i + 1] = _places[i];
}
_places[position] = excavator;
return position;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
/// </summary> /// </summary>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
public bool Remove(int position) public T Remove(int position)
{ {
if (position < 0 || position >= _places.Length) if (position > Count || _places[position] == null)
{ {
return false; return null;
} }
var result = _places[position];
_places[position] = null; _places.RemoveAt(position);
return true; return result;
} }
/// <summary> /// <summary>
/// Получение объекта из набора по позиции /// Получение объекта из набора по позиции
/// </summary> /// </summary>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
public T? Get(int position) public T? this[int position]
{ {
if (position < 0 || position >= _places.Length) get
return null; {
// TODO проверка позиции
return _places[position]; if (position < 0 || position >= _places.Count)
{
return null;
}
return _places[position];
}
set
{
// TODO проверка позиции
if (position < 0 || position >= _places.Count)
{
return;
}
// TODO проверка свободных мест в списке
if (_places.Count >= _maxCount)
{
return;
}
// TODO вставка в список по позиции
_places.Insert(position, value);
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetExcavators(int? maxExcavators = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxExcavators.HasValue && i == maxExcavators.Value)
{
yield break;
}
}
} }
} }
} }