From 2b931f9b23c4af64a180304df1b4a9ff818a0fb8 Mon Sep 17 00:00:00 2001 From: allllen4a Date: Wed, 18 Oct 2023 13:00:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusesGenericCollection.cs | 132 +++++++++++++++++ .../projectDoubleDeckerBus/DrawningBus.cs | 13 +- .../EntityDoubleDeckerBus.cs | 3 +- .../FormBusCollection.Designer.cs | 136 ++++++++++++++++++ .../FormBusCollection.cs | 68 +++++++++ .../FormBusCollection.resx | 120 ++++++++++++++++ .../FormDoubleDeckerBus.Designer.cs | 13 ++ .../FormDoubleDeckerBus.cs | 58 ++++++-- .../projectDoubleDeckerBus/Program.cs | 2 +- .../projectDoubleDeckerBus/SetGeneric.cs | 110 ++++++++++++++ 10 files changed, 639 insertions(+), 16 deletions(-) create mode 100644 projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericCollection.cs create mode 100644 projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.Designer.cs create mode 100644 projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.cs create mode 100644 projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.resx create mode 100644 projectDoubleDeckerBus/projectDoubleDeckerBus/SetGeneric.cs diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericCollection.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericCollection.cs new file mode 100644 index 0000000..6712108 --- /dev/null +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericCollection.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using projectDoubleDeckerBus.Drawings; +using projectDouble_Decker_Bus.MovementStrategy; +using projectDoubleDeckerBus.Generics; + +namespace projectDoubleDeckerBus +{ + internal class BusesGenericCollection + where T : DrawningBus + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 172; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 112; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public BusesGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + /// + public static int? operator +(BusesGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + public static bool operator -(BusesGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj == null) + { + return false; + } + return collect._collection.Remove(pos); + } + + 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) + { + for (int i = 0; i < _collection.Count; i++) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int j = 0; j < _collection.Count; j++) + { + DrawningBus? bus = _collection.Get(j); + if (bus == null) + continue; + bus.SetPosition((width - j % width - 1) * _placeSizeWidth, j / width * _placeSizeHeight); + bus.DrawTransport(g); + } + } + } + } +} diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/DrawningBus.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/DrawningBus.cs index 11d1431..27eb86e 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/DrawningBus.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/DrawningBus.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using projectDoubleDeckerBus; using System.Drawing; using projectDoubleDeckerBus.Entity; +using projectDouble_Decker_Bus.MovementStrategy; namespace projectDoubleDeckerBus.Drawings { @@ -56,6 +57,9 @@ namespace projectDoubleDeckerBus.Drawings /// Высота объекта /// public int GetHeight => _busHeight; + + public IMoveableObject GetMoveableObject => new + DrawningObjectBus(this); /// /// Инициализация свойств /// @@ -162,6 +166,7 @@ namespace projectDoubleDeckerBus.Drawings { if (busWidth >= width || busHeight >= height) { + Console.WriteLine("Проверка не пройдена, нельзя создать объект в этих размерах"); if (busWidth >= width) { busWidth = width - busWidth; @@ -172,6 +177,7 @@ namespace projectDoubleDeckerBus.Drawings } } else + Console.WriteLine("Объект создан"); _pictureWidth = width; _pictureHeight = height; @@ -182,7 +188,7 @@ namespace projectDoubleDeckerBus.Drawings public void SetPosition(int x, int y) { - if (((x + _busWidth > _pictureWidth)) || ((y >= 0) && (y + _busHeight > _pictureHeight))) + if (((x >= 0) && (x + _busWidth > _pictureWidth)) || ((y >= 0) && (y + _busHeight > _pictureHeight))) { _startPosX = _pictureWidth - _busWidth; _startPosY = _pictureHeight - _busHeight; @@ -231,7 +237,7 @@ namespace projectDoubleDeckerBus.Drawings { _startPosY += (int)EntityBus.Step; } - break; + break; } } /// @@ -255,7 +261,7 @@ namespace projectDoubleDeckerBus.Drawings Brush brRed = new SolidBrush(Color.Red); Pen pen = new(Color.Black); - + Brush br = new SolidBrush(EntityBus?.BodyColor ?? Color.Black); g.FillRectangle(br, _startPosX + 40, _startPosY + 34, 130, 52); @@ -267,6 +273,7 @@ namespace projectDoubleDeckerBus.Drawings g.DrawEllipse(pen, _startPosX + 130, _startPosY + 50, windowSize, windowSize); g.DrawEllipse(pen, _startPosX + 145, _startPosY + 50, windowSize, windowSize); + g.FillRectangle(brBlack, _startPosX + 80, _startPosY + 46, 15, 40); g.FillEllipse(brBlack, _startPosX + 50, _startPosY + 75, 20, 20); g.FillEllipse(brBlack, _startPosX + 140, _startPosY + 75, 20, 20); diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/EntityDoubleDeckerBus.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/EntityDoubleDeckerBus.cs index cb92a35..cfd031d 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/EntityDoubleDeckerBus.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/EntityDoubleDeckerBus.cs @@ -7,8 +7,7 @@ using System.Drawing; namespace projectDoubleDeckerBus.Entity { - - public class EntityDoubleDeckerBus : EntityBus + public class EntityDoubleDeckerBus : EntityBus { /// /// Дополнительный цвет (для опциональных элементов) diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.Designer.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.Designer.cs new file mode 100644 index 0000000..9b100d0 --- /dev/null +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.Designer.cs @@ -0,0 +1,136 @@ +namespace projectDoubleDeckerBus +{ + partial class FormBusCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + panel1 = new Panel(); + label1 = new Label(); + InputtextBox = new TextBox(); + buttonUpdateCollection = new Button(); + buttonDeleteBus = new Button(); + buttonAddBus = new Button(); + DrawBus = new PictureBox(); + panel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DrawBus).BeginInit(); + SuspendLayout(); + // + // panel1 + // + panel1.Controls.Add(label1); + panel1.Controls.Add(InputtextBox); + panel1.Controls.Add(buttonUpdateCollection); + panel1.Controls.Add(buttonDeleteBus); + panel1.Controls.Add(buttonAddBus); + panel1.Controls.Add(DrawBus); + panel1.Dock = DockStyle.Fill; + panel1.Location = new Point(0, 0); + panel1.Name = "panel1"; + panel1.Size = new Size(800, 450); + panel1.TabIndex = 0; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(700, 9); + label1.Name = "label1"; + label1.Size = new Size(34, 15); + label1.TabIndex = 1; + label1.Text = "Tools"; + label1.TextAlign = ContentAlignment.TopCenter; + // + // InputtextBox + // + InputtextBox.Location = new Point(700, 150); + InputtextBox.Name = "InputtextBox"; + InputtextBox.Size = new Size(88, 23); + InputtextBox.TabIndex = 1; + // + // buttonUpdateCollection + // + buttonUpdateCollection.Location = new Point(700, 206); + buttonUpdateCollection.Name = "buttonUpdateCollection"; + buttonUpdateCollection.Size = new Size(88, 39); + buttonUpdateCollection.TabIndex = 1; + buttonUpdateCollection.Text = "Update the collection"; + buttonUpdateCollection.UseVisualStyleBackColor = true; + buttonUpdateCollection.Click += buttonUpdateCollection_Click; + // + // buttonDeleteBus + // + buttonDeleteBus.Location = new Point(700, 92); + buttonDeleteBus.Name = "buttonDeleteBus"; + buttonDeleteBus.Size = new Size(88, 36); + buttonDeleteBus.TabIndex = 1; + buttonDeleteBus.Text = "Delete bus"; + buttonDeleteBus.UseVisualStyleBackColor = true; + buttonDeleteBus.Click += buttonDeleteBus_Click; + // + // buttonAddBus + // + buttonAddBus.Location = new Point(700, 30); + buttonAddBus.Name = "buttonAddBus"; + buttonAddBus.Size = new Size(88, 36); + buttonAddBus.TabIndex = 1; + buttonAddBus.Text = "Add bus"; + buttonAddBus.UseVisualStyleBackColor = true; + buttonAddBus.Click += buttonAddBus_Click; + // + // DrawBus + // + DrawBus.Dock = DockStyle.Fill; + DrawBus.Location = new Point(0, 0); + DrawBus.Name = "DrawBus"; + DrawBus.Size = new Size(800, 450); + DrawBus.TabIndex = 0; + DrawBus.TabStop = false; + // + // FormBusCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(panel1); + Name = "FormBusCollection"; + Text = "FormBusCollection"; + panel1.ResumeLayout(false); + panel1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)DrawBus).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Panel panel1; + private Label label1; + private TextBox InputtextBox; + private Button buttonUpdateCollection; + private Button buttonDeleteBus; + private Button buttonAddBus; + private PictureBox DrawBus; + } +} \ No newline at end of file diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.cs new file mode 100644 index 0000000..2dbf41d --- /dev/null +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using projectDoubleDeckerBus.Drawings; +using projectDouble_Decker_Bus.MovementStrategy; +using projectDoubleDeckerBus.Generics; + +namespace projectDoubleDeckerBus +{ + public partial class FormBusCollection : Form + { + private readonly BusesGenericCollection _buses; + public FormBusCollection() + { + InitializeComponent(); + _buses = new BusesGenericCollection(DrawBus.Width, DrawBus.Height); + } + + private void buttonAddBus_Click(object sender, EventArgs e) + { + FormDoubleDeckerBus form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_buses + form.SelectedBus != -1) + { + MessageBox.Show("Объект добавлен"); + DrawBus.Image = _buses.ShowCars(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void buttonDeleteBus_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(InputtextBox.Text); + if (_buses - pos != null) + { + MessageBox.Show("Объект удален"); + DrawBus.Image = _buses.ShowCars(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void buttonUpdateCollection_Click(object sender, EventArgs e) + { + DrawBus.Image = _buses.ShowCars(); + } + } +} diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.resx b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormDoubleDeckerBus.Designer.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormDoubleDeckerBus.Designer.cs index 660786e..e259386 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormDoubleDeckerBus.Designer.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormDoubleDeckerBus.Designer.cs @@ -37,6 +37,7 @@ ButtonCreateBus = new Button(); buttonStep = new Button(); comboBoxStrategy = new ComboBox(); + buttonAddBus = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit(); SuspendLayout(); // @@ -134,11 +135,22 @@ comboBoxStrategy.Size = new Size(117, 23); comboBoxStrategy.TabIndex = 9; // + // buttonAddBus + // + buttonAddBus.Location = new Point(671, 135); + buttonAddBus.Name = "buttonAddBus"; + buttonAddBus.Size = new Size(117, 30); + buttonAddBus.TabIndex = 10; + buttonAddBus.Text = "Add bus"; + buttonAddBus.UseVisualStyleBackColor = true; + buttonAddBus.Click += buttonAddBus_Click; + // // FormDoubleDeckerBus // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonAddBus); Controls.Add(comboBoxStrategy); Controls.Add(buttonStep); Controls.Add(ButtonCreateBus); @@ -165,5 +177,6 @@ private Button ButtonCreateBus; private Button buttonStep; private ComboBox comboBoxStrategy; + private Button buttonAddBus; } } \ No newline at end of file diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormDoubleDeckerBus.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormDoubleDeckerBus.cs index 3611786..227b05a 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormDoubleDeckerBus.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormDoubleDeckerBus.cs @@ -1,3 +1,12 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; using projectDoubleDeckerBus.Drawings; using projectDoubleDeckerBus.Entity; using projectDouble_Decker_Bus.MovementStrategy; @@ -9,9 +18,18 @@ namespace projectDoubleDeckerBus private DrawningBus? _drawningBus; private AbstractStrategy? _abstractStrategy; + + private AbstractStrategy? _strategy; + /// + /// + /// + public DrawningBus? SelectedBus { get; private set; } + public FormDoubleDeckerBus() { InitializeComponent(); + _strategy = null; + SelectedBus = null; } private void Draw() @@ -29,16 +47,25 @@ namespace projectDoubleDeckerBus private void buttonCreateDoubleDeckerBus_Click(object sender, EventArgs e) { Random random = new(); + Color mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + mainColor = dialog.Color; + } + Color addColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + addColor = dialog.Color; + } _drawningBus = new DrawningDoubleDeckerBus(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)), - pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); + random.Next(1000, 3000), mainColor, + addColor, Convert.ToBoolean(random.Next(0, 2)), + Convert.ToBoolean(random.Next(0, 2)), + pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); } @@ -70,10 +97,15 @@ namespace projectDoubleDeckerBus private void buttonCreateBus_Click(object sender, EventArgs e) { Random random = new(); + 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; + } _drawningBus = new DrawningBus(random.Next(100, 300), - random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), + random.Next(1000, 3000), color, pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); @@ -117,5 +149,11 @@ namespace projectDoubleDeckerBus } } + + private void buttonAddBus_Click(object sender, EventArgs e) + { + SelectedBus = _drawningBus; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/Program.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/Program.cs index 3d6ee53..d561a66 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/Program.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/Program.cs @@ -11,7 +11,7 @@ namespace projectDoubleDeckerBus // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormDoubleDeckerBus()); + Application.Run(new FormBusCollection()); } } } \ No newline at end of file diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/SetGeneric.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/SetGeneric.cs new file mode 100644 index 0000000..54778d7 --- /dev/null +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/SetGeneric.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace projectDoubleDeckerBus.Generics +{ + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public int Insert(T bus) + { + int index = -1; + for (int i = 0; i < _places.Length; i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + { + return -1; + } + for (int i = index; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = bus; + return 0; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T bus, int position) + { + if (position < 0 || position >= Count) + return -1; + if (_places[position] == null) + { + _places[position] = bus; + return position; + } + int index = -1; + for (int i = position; i < Count; i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + return -1; + for (int i = index; index > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = bus; + 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]; + } + } +} -- 2.25.1