diff --git a/Trolleybus/Trolleybus/Form1.cs b/Trolleybus/Trolleybus/Form1.cs index 3b2ab87..37c3ae0 100644 --- a/Trolleybus/Trolleybus/Form1.cs +++ b/Trolleybus/Trolleybus/Form1.cs @@ -47,8 +47,13 @@ namespace Trolleybus private void buttonCreate_Click(object sender, EventArgs e) { - Random rnd = new Random(); - _trolleybus = new DrawingTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + Random random = new(); + Color myColor = new Color(); + ColorDialog MyDialog = new ColorDialog(); + if (MyDialog.ShowDialog() == DialogResult.OK) + myColor = MyDialog.Color; + _trolleybus = new DrawingTrolleybus(random.Next(30, 50), random.Next(1000, 2000), + myColor); SetData(); Draw(); } @@ -82,11 +87,20 @@ namespace Trolleybus private void buttonCreateModif_Click(object sender, EventArgs e) { - Random rnd = new Random(); - _trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); + Random random = new(); + Color firstColor = new Color(); + Color secondColor = new Color(); + ColorDialog MyDialog = new ColorDialog(); + if (MyDialog.ShowDialog() == DialogResult.OK) + firstColor = MyDialog.Color; + MyDialog = new ColorDialog(); + if (MyDialog.ShowDialog() == DialogResult.OK) + secondColor = MyDialog.Color; + _trolleybus = new DrawningSmallTrolleybus(random.Next(100, 300), random.Next(1000, 2000), + 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)), Convert.ToBoolean(random.Next(0, 2))); + SetData(); Draw(); } diff --git a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.Designer.cs b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.Designer.cs index 0ae2210..992de1d 100644 --- a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.Designer.cs +++ b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.Designer.cs @@ -29,17 +29,17 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); - this.buttonAddTrolleybus = new System.Windows.Forms.Button(); - this.pictureBox = new System.Windows.Forms.PictureBox(); - this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); - this.buttonRemoveTrolleybus = new System.Windows.Forms.Button(); - this.buttonShowStorage = new System.Windows.Forms.Button(); - this.buttonShowOnMap = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonRemoveTrolleybus = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonAddTrolleybus = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); @@ -64,85 +64,16 @@ this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // - // comboBoxSelectorMap + // buttonLeft // - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 42); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(182, 21); - this.comboBoxSelectorMap.TabIndex = 0; - // - // buttonAddTrolleybus - // - this.buttonAddTrolleybus.Location = new System.Drawing.Point(7, 85); - this.buttonAddTrolleybus.Name = "buttonAddTrolleybus"; - this.buttonAddTrolleybus.Size = new System.Drawing.Size(181, 23); - this.buttonAddTrolleybus.TabIndex = 1; - this.buttonAddTrolleybus.Text = "Добавить троллейбус"; - this.buttonAddTrolleybus.UseVisualStyleBackColor = true; - // - // pictureBox - // - this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBox.Location = new System.Drawing.Point(0, 0); - this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(600, 450); - this.pictureBox.TabIndex = 1; - this.pictureBox.TabStop = false; - // - // maskedTextBoxPosition - // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 128); - this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(181, 20); - this.maskedTextBoxPosition.TabIndex = 2; - // - // buttonRemoveTrolleybus - // - this.buttonRemoveTrolleybus.Location = new System.Drawing.Point(6, 170); - this.buttonRemoveTrolleybus.Name = "buttonRemoveTrolleybus"; - this.buttonRemoveTrolleybus.Size = new System.Drawing.Size(182, 23); - this.buttonRemoveTrolleybus.TabIndex = 3; - this.buttonRemoveTrolleybus.Text = "Удалить троллейбус"; - this.buttonRemoveTrolleybus.UseVisualStyleBackColor = true; - // - // buttonShowStorage - // - this.buttonShowStorage.Location = new System.Drawing.Point(7, 216); - this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(181, 23); - this.buttonShowStorage.TabIndex = 4; - this.buttonShowStorage.Text = "Посмотреть хранилище"; - this.buttonShowStorage.UseVisualStyleBackColor = true; - // - // buttonShowOnMap - // - this.buttonShowOnMap.Location = new System.Drawing.Point(7, 256); - this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(181, 23); - this.buttonShowOnMap.TabIndex = 5; - this.buttonShowOnMap.Text = "Посмотреть карту"; - this.buttonShowOnMap.UseVisualStyleBackColor = true; - // - // buttonUp - // - this.buttonUp.BackgroundImage = global::Trolleybus.Properties.Resources.up30; - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(97, 365); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(32, 31); - this.buttonUp.TabIndex = 6; - this.buttonUp.UseVisualStyleBackColor = true; - // - // buttonDown - // - this.buttonDown.BackgroundImage = global::Trolleybus.Properties.Resources.down30; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(97, 402); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(32, 31); - this.buttonDown.TabIndex = 7; - this.buttonDown.UseVisualStyleBackColor = true; + this.buttonLeft.BackgroundImage = global::Trolleybus.Properties.Resources.left30; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(59, 402); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(32, 31); + this.buttonLeft.TabIndex = 9; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonRight // @@ -153,16 +84,97 @@ this.buttonRight.Size = new System.Drawing.Size(32, 31); this.buttonRight.TabIndex = 8; this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); // - // buttonLeft + // buttonDown // - this.buttonLeft.BackgroundImage = global::Trolleybus.Properties.Resources.left30; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(59, 402); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(32, 31); - this.buttonLeft.TabIndex = 9; - this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonDown.BackgroundImage = global::Trolleybus.Properties.Resources.down30; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(97, 402); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(32, 31); + this.buttonDown.TabIndex = 7; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonUp + // + this.buttonUp.BackgroundImage = global::Trolleybus.Properties.Resources.up30; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(97, 365); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(32, 31); + this.buttonUp.TabIndex = 6; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(7, 256); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(181, 23); + this.buttonShowOnMap.TabIndex = 5; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(7, 216); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(181, 23); + this.buttonShowStorage.TabIndex = 4; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonRemoveTrolleybus + // + this.buttonRemoveTrolleybus.Location = new System.Drawing.Point(6, 170); + this.buttonRemoveTrolleybus.Name = "buttonRemoveTrolleybus"; + this.buttonRemoveTrolleybus.Size = new System.Drawing.Size(182, 23); + this.buttonRemoveTrolleybus.TabIndex = 3; + this.buttonRemoveTrolleybus.Text = "Удалить троллейбус"; + this.buttonRemoveTrolleybus.UseVisualStyleBackColor = true; + this.buttonRemoveTrolleybus.Click += new System.EventHandler(this.ButtonRemoveTrolleybus_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 128); + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(181, 20); + this.maskedTextBoxPosition.TabIndex = 2; + // + // buttonAddTrolleybus + // + this.buttonAddTrolleybus.Location = new System.Drawing.Point(7, 85); + this.buttonAddTrolleybus.Name = "buttonAddTrolleybus"; + this.buttonAddTrolleybus.Size = new System.Drawing.Size(181, 23); + this.buttonAddTrolleybus.TabIndex = 1; + this.buttonAddTrolleybus.Text = "Добавить троллейбус"; + this.buttonAddTrolleybus.UseVisualStyleBackColor = true; + this.buttonAddTrolleybus.Click += new System.EventHandler(this.ButtonAddTrolleybus_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Сложная карта"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 42); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(182, 21); + this.comboBoxSelectorMap.TabIndex = 0; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // + // pictureBox + // + this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Name = "pictureBox"; + this.pictureBox.Size = new System.Drawing.Size(600, 450); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; // // FormMapWithSetTrolleybus // diff --git a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs index 9635a2e..384d5e5 100644 --- a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs +++ b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs @@ -12,11 +12,11 @@ namespace Trolleybus { public partial class FormMapWithSetTrolleybus : Form { - private MapWithSetTrolleybusGeneric _mapCarsCollectionGeneric; public FormMapWithSetTrolleybus() { InitializeComponent(); } + private MapWithSetTrolleybusGeneric _mapTrolleybusCollectionGeneric; private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { AbstractMap map = null; @@ -25,36 +25,34 @@ namespace Trolleybus case "Простая карта": map = new SimpleMap(); break; + case "Сложная карта": + map = new HardMap(); + break; } if (map != null) { - _mapCarsCollectionGeneric = new MapWithSetTrolleybusGeneric( + _mapTrolleybusCollectionGeneric = new MapWithSetTrolleybusGeneric( pictureBox.Width, pictureBox.Height, map); } else { - _mapCarsCollectionGeneric = null; + _mapTrolleybusCollectionGeneric = null; } } - /// - /// Добавление объекта - /// - /// - /// - private void ButtonAddCar_Click(object sender, EventArgs e) + private void ButtonAddTrolleybus_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapTrolleybusCollectionGeneric == null) { return; } Form1 form = new(); if (form.ShowDialog() == DialogResult.OK) { - DrawningObjectTrolleybus car = new(form.SelectedTrolleybus); - if (_mapCarsCollectionGeneric + car) + DrawningObjectTrolleybus trolleybus = new(form.SelectedTrolleybus); + if (_mapTrolleybusCollectionGeneric + trolleybus !=-1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet(); } else { @@ -67,7 +65,7 @@ namespace Trolleybus /// /// /// - private void ButtonRemoveCar_Click(object sender, EventArgs e) + private void ButtonRemoveTrolleybus_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { @@ -78,10 +76,10 @@ namespace Trolleybus return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapCarsCollectionGeneric - pos) + if (_mapTrolleybusCollectionGeneric - pos !=null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet(); } else { @@ -95,11 +93,11 @@ namespace Trolleybus /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapTrolleybusCollectionGeneric == null) { return; } - pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet(); } /// /// Вывод карты @@ -108,11 +106,11 @@ namespace Trolleybus /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapTrolleybusCollectionGeneric == null) { return; } - pictureBox.Image = _mapCarsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowOnMap(); } /// /// Перемещение @@ -121,7 +119,7 @@ namespace Trolleybus /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapTrolleybusCollectionGeneric == null) { return; } @@ -143,7 +141,7 @@ namespace Trolleybus dir = Direction.Right; break; } - pictureBox.Image = _mapCarsCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapTrolleybusCollectionGeneric.MoveObject(dir); } } } diff --git a/Trolleybus/Trolleybus/HardMap.cs b/Trolleybus/Trolleybus/HardMap.cs new file mode 100644 index 0000000..5ddb463 --- /dev/null +++ b/Trolleybus/Trolleybus/HardMap.cs @@ -0,0 +1,30 @@ +using System.Threading; +using Trolleybus; + +namespace Trolleybus +{ + internal class HardMap : SimpleMap + { + protected override void GenerateMap() + { + _map = new int[100, 100]; + _size_x = (float)_width / _map.GetLength(0); + _size_y = (float)_height / _map.GetLength(1); + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[i, j] = _freeRoad; + } + } + for (int i = 0; i < 50; i++) + { + for (int j = 0; j < 50; j++) + { + _map[i, j] = _barrier; + } + } + } + } + +} \ No newline at end of file diff --git a/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs b/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs index 8f49c2c..2b312bf 100644 --- a/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs +++ b/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs @@ -54,11 +54,11 @@ namespace Trolleybus /// Перегрузка оператора сложения /// /// - /// + /// /// - public static bool operator +(MapWithSetTrolleybusGeneric map, T car) + public static int operator +(MapWithSetTrolleybusGeneric map, T trolleybus) { - return map._setTrolleybus.Insert(car); + return map._setTrolleybus.Insert(trolleybus); } /// /// Перегрузка оператора вычитания @@ -66,7 +66,7 @@ namespace Trolleybus /// /// /// - public static bool operator -(MapWithSetTrolleybusGeneric map, int position) + public static T operator -(MapWithSetTrolleybusGeneric map, int position) { return map._setTrolleybus.Remove(position); } @@ -79,7 +79,7 @@ namespace Trolleybus Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); - DrawCars(gr); + DrawTrolleybus(gr); return bmp; } /// @@ -159,12 +159,29 @@ namespace Trolleybus /// Метод прорисовки объектов /// /// - private void DrawCars(Graphics g) + private void DrawTrolleybus(Graphics g) { - for (int i = 0; i < _setTrolleybus.Count; i++) { - // TODO установка позиции - _setTrolleybus.Get(i)?.DrawningObject(g); + int xForLocomotive = 2; + int yForLocomotive = 10; + int countInRow = 0; + for (int i = 0; i < _setTrolleybus.Count; i++) + { + if (countInRow >= _pictureWidth / (_placeSizeWidth + 30)) + { + xForLocomotive = 2; + yForLocomotive += _placeSizeHeight; + countInRow = 0; + } + if (_setTrolleybus.Get(i) != null) + { + T locomotive = _setTrolleybus.Get(i); + locomotive.SetObject(xForLocomotive, yForLocomotive, _pictureWidth, _pictureHeight); + locomotive.DrawningObject(g); + } + xForLocomotive += _placeSizeWidth + 30; + countInRow++; + } } } } diff --git a/Trolleybus/Trolleybus/Program.cs b/Trolleybus/Trolleybus/Program.cs index be48583..7ada713 100644 --- a/Trolleybus/Trolleybus/Program.cs +++ b/Trolleybus/Trolleybus/Program.cs @@ -16,7 +16,7 @@ namespace Trolleybus { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new FormMap()); + Application.Run(new FormMapWithSetTrolleybus()); } } } diff --git a/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs b/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs index 48d60f4..49eb79c 100644 --- a/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs +++ b/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs @@ -6,71 +6,76 @@ using System.Threading.Tasks; namespace Trolleybus { + // Параметризованный набор объектов internal class SetTrolleybusGeneric where T : class { - /// - /// Массив объектов, которые храним - /// + // Массив объектов, которые храним private readonly T[] _places; - /// - /// Количество объектов в массиве - /// + // Количество объектов в массиве public int Count => _places.Length; - /// - /// Конструктор - /// - /// + // Конструктор public SetTrolleybusGeneric(int count) { _places = new T[count]; } - /// - /// Добавление объекта в набор - /// - /// Добавляемый автомобиль - /// - public bool Insert(T trolleybus) + // Добавление объекта в набор + public int Insert(T trolleybus) { - // TODO вставка в начало набора - return true; + // вставка в начало набора + return Insert(trolleybus, 0); } - /// - /// Добавление объекта в набор на конкретную позицию - /// - /// Добавляемый автомобиль - /// Позиция - /// - public bool Insert(T trolleybus, int position) + // Добавление объекта в набор на конкретную позицию + public int Insert(T trolleybus, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции + // проверка позиции + if (position >= _places.Length || position < 0) + return -1; + //проверка, что элемент массива по этой позиции пустой, если нет, то + if (_places[position] == null) + { + _places[position] = trolleybus; + return position; + } + //проверка, что после вставляемого элемента в массиве есть пустой элемент + int findEmptyPos = -1; + + for (int i = position + 1; i < Count; i++) + { + if (_places[i] == null) + { + findEmptyPos = i; + break; + } + } + if (findEmptyPos < 0) return -1; + + //сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + for (int i = findEmptyPos; i > position; i--) + { + _places[i] = _places[i - 1]; + } + // вставка по позиции _places[position] = trolleybus; - return true; + return position; } - /// - /// Удаление объекта из набора с конкретной позиции - /// - /// - /// - public bool Remove(int position) + // Удаление объекта из набора с конкретной позиции + public T Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присовив элементу массива значение null - return true; + // проверка позиции + if (position >= _places.Length || position < 0) return null; + // удаление объекта из массива, присовив элементу массива значение null + T temp = _places[position]; + _places[position] = null; + return temp; } - /// - /// Получение объекта из набора по позиции - /// - /// - /// + // Получение объекта из набора по позиции public T Get(int position) { - // TODO проверка позиции + // проверка позиции + if (position >= _places.Length || position < 0) + return null; return _places[position]; } } -} +} \ No newline at end of file diff --git a/Trolleybus/Trolleybus/Trolleybus.csproj b/Trolleybus/Trolleybus/Trolleybus.csproj index dfc8ec3..ddd44f9 100644 --- a/Trolleybus/Trolleybus/Trolleybus.csproj +++ b/Trolleybus/Trolleybus/Trolleybus.csproj @@ -73,6 +73,7 @@ FormMapWithSetTrolleybus.cs +