3 лабораторная
This commit is contained in:
parent
467726e7d3
commit
0a16b54539
14
HoistingCrane/HoistingCrane/CraneVisual.Designer.cs
generated
14
HoistingCrane/HoistingCrane/CraneVisual.Designer.cs
generated
@ -38,6 +38,7 @@
|
||||
this.buttonStep = new System.Windows.Forms.Button();
|
||||
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
|
||||
this.buttonCreateAdditional = new System.Windows.Forms.Button();
|
||||
this.buttonChoose = new System.Windows.Forms.Button();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCrane)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
@ -144,11 +145,23 @@
|
||||
this.buttonCreateAdditional.UseVisualStyleBackColor = true;
|
||||
this.buttonCreateAdditional.Click += new System.EventHandler(this.ButtonCreateAdditional_Click);
|
||||
//
|
||||
// buttonChoose
|
||||
//
|
||||
this.buttonChoose.ImeMode = System.Windows.Forms.ImeMode.NoControl;
|
||||
this.buttonChoose.Location = new System.Drawing.Point(430, 378);
|
||||
this.buttonChoose.Name = "buttonChoose";
|
||||
this.buttonChoose.Size = new System.Drawing.Size(170, 59);
|
||||
this.buttonChoose.TabIndex = 28;
|
||||
this.buttonChoose.Text = "Выбрать";
|
||||
this.buttonChoose.UseVisualStyleBackColor = true;
|
||||
this.buttonChoose.Click += new System.EventHandler(this.buttonChoose_Click);
|
||||
//
|
||||
// CraneVisual
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(878, 444);
|
||||
this.Controls.Add(this.buttonChoose);
|
||||
this.Controls.Add(this.buttonStep);
|
||||
this.Controls.Add(this.comboBoxStrategy);
|
||||
this.Controls.Add(this.buttonCreateAdditional);
|
||||
@ -176,5 +189,6 @@
|
||||
private Button buttonStep;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonCreateAdditional;
|
||||
private Button buttonChoose;
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ namespace HoistingCrane
|
||||
{
|
||||
private DrawingCrane? _drawingCrane;
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
public DrawingCrane? SelectedCrane { get; private set; }
|
||||
public CraneVisual()
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -22,13 +23,35 @@ namespace HoistingCrane
|
||||
private void ButtonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingCrane = new DrawingCrane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxCrane.Width, pictureBoxCrane.Height); _drawingCrane.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
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;
|
||||
}
|
||||
_drawingCrane = new DrawingCrane(random.Next(100, 300),
|
||||
random.Next(1000, 3000),
|
||||
color, pictureBoxCrane.Width, pictureBoxCrane.Height);
|
||||
_drawingCrane.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
private void ButtonCreateAdditional_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new Random();
|
||||
_drawingCrane = new AdditionalDrawingCrane(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)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxCrane.Width, pictureBoxCrane.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;
|
||||
}
|
||||
Color Additionalcolor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
||||
dialog = new();
|
||||
if (dialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
Additionalcolor = dialog.Color;
|
||||
}
|
||||
_drawingCrane = new AdditionalDrawingCrane(random.Next(100, 300), random.Next(1000, 3000), color, Additionalcolor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxCrane.Width, pictureBoxCrane.Height);
|
||||
_drawingCrane.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
@ -52,6 +75,11 @@ namespace HoistingCrane
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
private void buttonChoose_Click(object sender, EventArgs e)
|
||||
{
|
||||
SelectedCrane = _drawingCrane;
|
||||
DialogResult = DialogResult.OK;
|
||||
}
|
||||
private void buttonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingCrane == null)
|
||||
|
103
HoistingCrane/HoistingCrane/CranesGenericCollection.cs
Normal file
103
HoistingCrane/HoistingCrane/CranesGenericCollection.cs
Normal file
@ -0,0 +1,103 @@
|
||||
using HoistingCrane.DrawningObjects;
|
||||
using HoistingCrane.MovementStrategy;
|
||||
|
||||
namespace HoistingCrane.Generics
|
||||
{
|
||||
/// Параметризованный класс для набора объектов DrawningCar
|
||||
internal class CranesGenericCollection<T, U>
|
||||
where T : DrawingCrane
|
||||
where U : IMoveableObject
|
||||
{
|
||||
/// Ширина окна прорисовки
|
||||
private readonly int _pictureWidth;
|
||||
/// Высота окна прорисовки
|
||||
private readonly int _pictureHeight;
|
||||
/// Размер занимаемого объектом места (ширина)
|
||||
private readonly int _placeSizeWidth = 200;
|
||||
/// Размер занимаемого объектом места (высота)
|
||||
private readonly int _placeSizeHeight = 150;
|
||||
/// Набор объектов
|
||||
private readonly SetGeneric<T> _collection;
|
||||
/// Конструктор
|
||||
public CranesGenericCollection(int picWidth, int picHeight)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
/// Перегрузка оператора сложения
|
||||
public static int operator +(CranesGenericCollection<T, U> collect, T? obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return collect._collection.Insert(obj);
|
||||
}
|
||||
/// Перегрузка оператора вычитания
|
||||
public static bool operator -(CranesGenericCollection<T, U> collect, int pos)
|
||||
{
|
||||
T? obj = collect._collection.Get(pos);
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return collect?._collection.Remove(pos) ?? false;
|
||||
}
|
||||
/// Получение объекта IMoveableObject
|
||||
public U? GetU(int pos)
|
||||
{
|
||||
return (U?)_collection.Get(pos)?.GetMoveableObject;
|
||||
}
|
||||
/// Вывод всего набора объектов
|
||||
public Bitmap ShowCars()
|
||||
{
|
||||
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)
|
||||
{
|
||||
T? obj;
|
||||
int j = 3;
|
||||
int k = 1;
|
||||
for (int i = 0; i < _collection.Count; i++)
|
||||
{
|
||||
if(_collection.Get(i) == null)
|
||||
continue;
|
||||
if (j < 0)
|
||||
{
|
||||
j += 4;
|
||||
k--;
|
||||
}
|
||||
if (_collection.Get(i) != null)
|
||||
{
|
||||
obj = _collection.Get(i);
|
||||
obj.SetPosition(_placeSizeWidth * j, _placeSizeHeight * k);
|
||||
obj.DrawTransport(g);
|
||||
}
|
||||
j--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
using HoistingCrane.Entities;
|
||||
using HoistingCrane.MovementStrategy;
|
||||
|
||||
namespace HoistingCrane.DrawningObjects
|
||||
{
|
||||
@ -15,6 +16,7 @@ namespace HoistingCrane.DrawningObjects
|
||||
protected readonly int _craneWidth = 200;
|
||||
protected readonly int _craneHeight = 150;
|
||||
|
||||
public IMoveableObject GetMoveableObject => new DrawningObjectsCrane(this);
|
||||
public DrawingCrane(int speed, double weight, Color bodyColor, int width, int height)
|
||||
{
|
||||
if ((_craneWidth > width) || (_craneHeight > height)) return;
|
||||
|
125
HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs
generated
Normal file
125
HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs
generated
Normal file
@ -0,0 +1,125 @@
|
||||
namespace HoistingCrane
|
||||
{
|
||||
partial class FormCraneCollection
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.groupBoxInstruments = new System.Windows.Forms.GroupBox();
|
||||
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
|
||||
this.buttonReload = new System.Windows.Forms.Button();
|
||||
this.buttonDelete = new System.Windows.Forms.Button();
|
||||
this.buttonAdd = new System.Windows.Forms.Button();
|
||||
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
|
||||
this.groupBoxInstruments.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// groupBoxInstruments
|
||||
//
|
||||
this.groupBoxInstruments.Controls.Add(this.maskedTextBoxNumber);
|
||||
this.groupBoxInstruments.Controls.Add(this.buttonReload);
|
||||
this.groupBoxInstruments.Controls.Add(this.buttonDelete);
|
||||
this.groupBoxInstruments.Controls.Add(this.buttonAdd);
|
||||
this.groupBoxInstruments.Location = new System.Drawing.Point(811, 0);
|
||||
this.groupBoxInstruments.Name = "groupBoxInstruments";
|
||||
this.groupBoxInstruments.Size = new System.Drawing.Size(185, 451);
|
||||
this.groupBoxInstruments.TabIndex = 0;
|
||||
this.groupBoxInstruments.TabStop = false;
|
||||
this.groupBoxInstruments.Text = "Инструменты";
|
||||
//
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
this.maskedTextBoxNumber.Location = new System.Drawing.Point(11, 71);
|
||||
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 31);
|
||||
this.maskedTextBoxNumber.TabIndex = 9;
|
||||
//
|
||||
// buttonReload
|
||||
//
|
||||
this.buttonReload.Location = new System.Drawing.Point(6, 172);
|
||||
this.buttonReload.Name = "buttonReload";
|
||||
this.buttonReload.Size = new System.Drawing.Size(167, 35);
|
||||
this.buttonReload.TabIndex = 8;
|
||||
this.buttonReload.Text = "Обновить экран";
|
||||
this.buttonReload.UseVisualStyleBackColor = true;
|
||||
this.buttonReload.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
|
||||
//
|
||||
// buttonDelete
|
||||
//
|
||||
this.buttonDelete.Location = new System.Drawing.Point(6, 108);
|
||||
this.buttonDelete.Name = "buttonDelete";
|
||||
this.buttonDelete.Size = new System.Drawing.Size(167, 35);
|
||||
this.buttonDelete.TabIndex = 7;
|
||||
this.buttonDelete.Text = "Удалить кран";
|
||||
this.buttonDelete.UseVisualStyleBackColor = true;
|
||||
this.buttonDelete.Click += new System.EventHandler(this.ButtonRemoveCrane_Click);
|
||||
//
|
||||
// buttonAdd
|
||||
//
|
||||
this.buttonAdd.Location = new System.Drawing.Point(6, 30);
|
||||
this.buttonAdd.Name = "buttonAdd";
|
||||
this.buttonAdd.Size = new System.Drawing.Size(167, 35);
|
||||
this.buttonAdd.TabIndex = 6;
|
||||
this.buttonAdd.Text = "Добавить кран";
|
||||
this.buttonAdd.UseVisualStyleBackColor = true;
|
||||
this.buttonAdd.Click += new System.EventHandler(this.ButtonAddCrane_Click);
|
||||
//
|
||||
// pictureBoxCollection
|
||||
//
|
||||
this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0);
|
||||
this.pictureBoxCollection.Name = "pictureBoxCollection";
|
||||
this.pictureBoxCollection.Size = new System.Drawing.Size(996, 444);
|
||||
this.pictureBoxCollection.TabIndex = 5;
|
||||
this.pictureBoxCollection.TabStop = false;
|
||||
//
|
||||
// FormCraneCollection
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(996, 444);
|
||||
this.Controls.Add(this.groupBoxInstruments);
|
||||
this.Controls.Add(this.pictureBoxCollection);
|
||||
this.Name = "FormCraneCollection";
|
||||
this.Text = "FormCraneCollection";
|
||||
this.groupBoxInstruments.ResumeLayout(false);
|
||||
this.groupBoxInstruments.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private GroupBox groupBoxInstruments;
|
||||
private MaskedTextBox maskedTextBoxNumber;
|
||||
private Button buttonReload;
|
||||
private Button buttonDelete;
|
||||
private Button buttonAdd;
|
||||
private PictureBox pictureBoxCollection;
|
||||
}
|
||||
}
|
60
HoistingCrane/HoistingCrane/FormCraneCollection.cs
Normal file
60
HoistingCrane/HoistingCrane/FormCraneCollection.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using HoistingCrane.DrawningObjects;
|
||||
using HoistingCrane.Generics;
|
||||
using HoistingCrane.MovementStrategy;
|
||||
|
||||
namespace HoistingCrane
|
||||
{
|
||||
public partial class FormCraneCollection : Form
|
||||
{
|
||||
private readonly CranesGenericCollection<DrawingCrane, DrawningObjectsCrane> _cranes;
|
||||
public FormCraneCollection()
|
||||
{
|
||||
InitializeComponent();
|
||||
_cranes = new CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
}
|
||||
private void ButtonAddCrane_Click(object sender, EventArgs e)
|
||||
{
|
||||
CraneVisual form = new();
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_cranes + form.SelectedCrane != -1)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = _cranes.ShowCars();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void ButtonRemoveCrane_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (MessageBox.Show("Удалить объект?", "Удаление",
|
||||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||
if (_cranes - pos)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = _cranes.ShowCars();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonRefreshCollection_Click(object sender, EventArgs
|
||||
e)
|
||||
{
|
||||
pictureBoxCollection.Image = _cranes.ShowCars();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
60
HoistingCrane/HoistingCrane/FormCraneCollection.resx
Normal file
60
HoistingCrane/HoistingCrane/FormCraneCollection.resx
Normal file
@ -0,0 +1,60 @@
|
||||
<root>
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
@ -11,7 +11,7 @@ namespace HoistingCrane
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new CraneVisual());
|
||||
Application.Run(new FormCraneCollection());
|
||||
}
|
||||
}
|
||||
}
|
68
HoistingCrane/HoistingCrane/SetGeneric.cs
Normal file
68
HoistingCrane/HoistingCrane/SetGeneric.cs
Normal file
@ -0,0 +1,68 @@
|
||||
namespace HoistingCrane.Generics
|
||||
{
|
||||
/// Параметризованный набор объектов
|
||||
internal class SetGeneric<T>
|
||||
where T : class
|
||||
|
||||
{
|
||||
/// Массив объектов, которые храним
|
||||
private readonly T?[] _places;
|
||||
/// Количество объектов в массиве
|
||||
public int Count => _places.Length;
|
||||
/// Конструктор
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new T?[count];
|
||||
}
|
||||
/// Добавление объекта в набор
|
||||
public int Insert(T crane)
|
||||
{
|
||||
int pos = -1;
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pos != -1)
|
||||
{
|
||||
for (int i = pos; i > 0; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
}
|
||||
_places[0] = crane;
|
||||
return 0;
|
||||
}
|
||||
public int Insert(T crane, int position)
|
||||
{
|
||||
if ((position < 0) && (position > Count)) return -1;
|
||||
if (_places[position] != null)
|
||||
{
|
||||
for (int i = Count - 1; i >= position; i--)
|
||||
{
|
||||
if (_places[i - 1] != null)
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
}
|
||||
_places[position] = crane;
|
||||
return position;
|
||||
}
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
public bool Remove(int position)
|
||||
{
|
||||
if ((position < 0) || (position > Count)) return false;
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
/// Получение объекта из набора по позиции
|
||||
public T? Get(int position)
|
||||
{
|
||||
if ((position < 0) || (position > Count)) return null;
|
||||
return _places[position];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user