From 5befb89c6baf415ea58ea71d3b49bfd8e76cf1d6 Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Sun, 5 Nov 2023 14:40:21 +0400 Subject: [PATCH] Lab4 --- .../FormMonorailCollection.Designer.cs | 349 +++++++++++------- Monorail/Monorail/FormMonorailCollection.cs | 236 ++++++++---- Monorail/Monorail/FormMonorailCollection.resx | 238 ++++++------ .../Generics/MonorailsGenericCollection.cs | 12 +- .../Generics/MonorailsGenericStorage.cs | 52 +++ Monorail/Monorail/Generics/SetGeneric.cs | 199 +++++----- 6 files changed, 654 insertions(+), 432 deletions(-) create mode 100644 Monorail/Monorail/Generics/MonorailsGenericStorage.cs diff --git a/Monorail/Monorail/FormMonorailCollection.Designer.cs b/Monorail/Monorail/FormMonorailCollection.Designer.cs index 67ab749..a1b15a0 100644 --- a/Monorail/Monorail/FormMonorailCollection.Designer.cs +++ b/Monorail/Monorail/FormMonorailCollection.Designer.cs @@ -1,138 +1,213 @@ -namespace Monorail -{ - partial class FormMonorailCollection - { - /// - /// 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() - { - pictureBoxCollection = new PictureBox(); - panel1 = new Panel(); - maskedTextBoxNumber = new TextBox(); - buttonRefreshCol = new Button(); - buttonDelMonorail = new Button(); - buttonAddMonorail = new Button(); - label1 = new Label(); - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); - panel1.SuspendLayout(); - SuspendLayout(); - // - // pictureBoxCollection - // - pictureBoxCollection.Location = new Point(5, -1); - pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(931, 454); - pictureBoxCollection.TabIndex = 5; - pictureBoxCollection.TabStop = false; - // - // panel1 - // - panel1.BackColor = SystemColors.Info; - panel1.Controls.Add(maskedTextBoxNumber); - panel1.Controls.Add(buttonRefreshCol); - panel1.Controls.Add(buttonDelMonorail); - panel1.Controls.Add(buttonAddMonorail); - panel1.Controls.Add(label1); - panel1.Location = new Point(942, 12); - panel1.Name = "panel1"; - panel1.Size = new Size(190, 429); - panel1.TabIndex = 4; - // - // maskedTextBoxNumber - // - maskedTextBoxNumber.Location = new Point(29, 109); - maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(125, 27); - maskedTextBoxNumber.TabIndex = 4; - // - // buttonRefreshCol - // - buttonRefreshCol.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - buttonRefreshCol.Location = new Point(12, 237); - buttonRefreshCol.Name = "buttonRefreshCol"; - buttonRefreshCol.Size = new Size(166, 41); - buttonRefreshCol.TabIndex = 3; - buttonRefreshCol.Text = "Обновить коллекцию"; - buttonRefreshCol.UseVisualStyleBackColor = true; - buttonRefreshCol.Click += ButtonRefreshCollection_Click; - // - // buttonDelMonorail - // - buttonDelMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - buttonDelMonorail.Location = new Point(12, 174); - buttonDelMonorail.Name = "buttonDelMonorail"; - buttonDelMonorail.Size = new Size(166, 41); - buttonDelMonorail.TabIndex = 2; - buttonDelMonorail.Text = "Удалить монорельс"; - buttonDelMonorail.UseVisualStyleBackColor = true; - buttonDelMonorail.Click += ButtonRemoveMonorail_Click; - // - // buttonAddMonorail - // - buttonAddMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - buttonAddMonorail.Location = new Point(12, 29); - buttonAddMonorail.Name = "buttonAddMonorail"; - buttonAddMonorail.Size = new Size(166, 41); - buttonAddMonorail.TabIndex = 1; - buttonAddMonorail.Text = "Добавить монорельс"; - buttonAddMonorail.UseVisualStyleBackColor = true; - buttonAddMonorail.Click += ButtonAddMonorail_Click; - // - // label1 - // - label1.AutoSize = true; - label1.Location = new Point(3, -3); - label1.Name = "label1"; - label1.Size = new Size(103, 20); - label1.TabIndex = 0; - label1.Text = "Инструменты"; - // - // FormMonorailCollection - // - AutoScaleDimensions = new SizeF(8F, 20F); - AutoScaleMode = AutoScaleMode.Font; - BackColor = SystemColors.ControlDarkDark; - ClientSize = new Size(1144, 453); - Controls.Add(pictureBoxCollection); - Controls.Add(panel1); - Name = "FormMonorailCollection"; - Text = "Набор Монорельсов"; - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); - panel1.ResumeLayout(false); - panel1.PerformLayout(); - ResumeLayout(false); - } - - #endregion - - private PictureBox pictureBoxCollection; - private Panel panel1; - private Button buttonRefreshCol; - private Button buttonDelMonorail; - private Button buttonAddMonorail; - private Label label1; - private TextBox maskedTextBoxNumber; - } +namespace Monorail +{ + partial class FormMonorailCollection + { + /// + /// 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() + { + pictureBoxCollection = new PictureBox(); + panel1 = new Panel(); + panel2 = new Panel(); + label2 = new Label(); + listBoxStorages = new ListBox(); + textBoxStorageName = new TextBox(); + button1 = new Button(); + ButtonAddObject = new Button(); + maskedTextBoxNumber = new TextBox(); + buttonRefreshCol = new Button(); + buttonDelMonorail = new Button(); + buttonAddMonorail = new Button(); + label1 = new Label(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + panel1.SuspendLayout(); + panel2.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(5, -1); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(1017, 517); + pictureBoxCollection.TabIndex = 5; + pictureBoxCollection.TabStop = false; + // + // panel1 + // + panel1.BackColor = SystemColors.Info; + panel1.Controls.Add(panel2); + panel1.Controls.Add(maskedTextBoxNumber); + panel1.Controls.Add(buttonRefreshCol); + panel1.Controls.Add(buttonDelMonorail); + panel1.Controls.Add(buttonAddMonorail); + panel1.Controls.Add(label1); + panel1.Location = new Point(1025, -1); + panel1.Name = "panel1"; + panel1.Size = new Size(204, 517); + panel1.TabIndex = 4; + // + // panel2 + // + panel2.Controls.Add(label2); + panel2.Controls.Add(listBoxStorages); + panel2.Controls.Add(textBoxStorageName); + panel2.Controls.Add(button1); + panel2.Controls.Add(ButtonAddObject); + panel2.Location = new Point(12, 20); + panel2.Name = "panel2"; + panel2.Size = new Size(179, 263); + panel2.TabIndex = 7; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(3, -3); + label2.Name = "label2"; + label2.Size = new Size(66, 20); + label2.TabIndex = 8; + label2.Text = "Наборы"; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(17, 109); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(144, 104); + listBoxStorages.TabIndex = 9; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(18, 29); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(143, 27); + textBoxStorageName.TabIndex = 8; + // + // button1 + // + button1.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); + button1.Location = new Point(18, 219); + button1.Name = "button1"; + button1.Size = new Size(143, 41); + button1.TabIndex = 5; + button1.Text = "Удалить набор"; + button1.UseVisualStyleBackColor = true; + button1.Click += ButtonDelObject_Click; + // + // ButtonAddObject + // + ButtonAddObject.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); + ButtonAddObject.Location = new Point(18, 62); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(143, 41); + ButtonAddObject.TabIndex = 6; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(30, 336); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(125, 27); + maskedTextBoxNumber.TabIndex = 4; + // + // buttonRefreshCol + // + buttonRefreshCol.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); + buttonRefreshCol.Location = new Point(12, 466); + buttonRefreshCol.Name = "buttonRefreshCol"; + buttonRefreshCol.Size = new Size(179, 41); + buttonRefreshCol.TabIndex = 3; + buttonRefreshCol.Text = "Обновить коллекцию"; + buttonRefreshCol.UseVisualStyleBackColor = true; + buttonRefreshCol.Click += ButtonRefreshCollection_Click; + // + // buttonDelMonorail + // + buttonDelMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); + buttonDelMonorail.Location = new Point(12, 369); + buttonDelMonorail.Name = "buttonDelMonorail"; + buttonDelMonorail.Size = new Size(179, 41); + buttonDelMonorail.TabIndex = 2; + buttonDelMonorail.Text = "Удалить монорельс"; + buttonDelMonorail.UseVisualStyleBackColor = true; + buttonDelMonorail.Click += ButtonRemoveMonorail_Click; + // + // buttonAddMonorail + // + buttonAddMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); + buttonAddMonorail.Location = new Point(12, 289); + buttonAddMonorail.Name = "buttonAddMonorail"; + buttonAddMonorail.Size = new Size(179, 41); + buttonAddMonorail.TabIndex = 1; + buttonAddMonorail.Text = "Добавить монорельс"; + buttonAddMonorail.UseVisualStyleBackColor = true; + buttonAddMonorail.Click += ButtonAddMonorail_Click; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(3, -3); + label1.Name = "label1"; + label1.Size = new Size(103, 20); + label1.TabIndex = 0; + label1.Text = "Инструменты"; + // + // FormMonorailCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + BackColor = SystemColors.ControlDarkDark; + ClientSize = new Size(1228, 518); + Controls.Add(pictureBoxCollection); + Controls.Add(panel1); + Name = "FormMonorailCollection"; + Text = "Набор Монорельсов"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + panel1.ResumeLayout(false); + panel1.PerformLayout(); + panel2.ResumeLayout(false); + panel2.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private PictureBox pictureBoxCollection; + private Panel panel1; + private Button buttonRefreshCol; + private Button buttonDelMonorail; + private Button buttonAddMonorail; + private Label label1; + private TextBox maskedTextBoxNumber; + private Button button1; + private Panel panel2; + private Label label2; + private ListBox listBoxStorages; + private TextBox textBoxStorageName; + private Button ButtonAddObject; + } } \ No newline at end of file diff --git a/Monorail/Monorail/FormMonorailCollection.cs b/Monorail/Monorail/FormMonorailCollection.cs index 3c840fb..39dd360 100644 --- a/Monorail/Monorail/FormMonorailCollection.cs +++ b/Monorail/Monorail/FormMonorailCollection.cs @@ -1,75 +1,161 @@ -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 Monorail.DrawningObjects; -using Monorail.Generics; -using Monorail.MovementStrategy; - -namespace Monorail -{ - public partial class FormMonorailCollection : Form - { - private readonly MonorailsGenericCollection _monorails; - - public FormMonorailCollection() - { - InitializeComponent(); - _monorails = new MonorailsGenericCollection - (pictureBoxCollection.Width, pictureBoxCollection.Height); - } - private void ButtonAddMonorail_Click(object sender, EventArgs e) - { - FormMonorail form = new(); - - if (form.ShowDialog() == DialogResult.OK) - { - if (_monorails + form.SelectedMonorail != -1) - { - MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _monorails.ShowMonorails(); - } - else - { - MessageBox.Show("Не удалось добавить объект"); - } - } - } - private void ButtonRemoveMonorail_Click(object sender, EventArgs e) - { - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) - { - return; - } - int pos; - if (maskedTextBoxNumber.Text == "") - { - MessageBox.Show("Введите позицию элемента выше"); - return; - } - else - { - pos = Convert.ToInt32(maskedTextBoxNumber.Text); - } - if (_monorails - pos != null) - { - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _monorails.ShowMonorails(); - } - else - { - MessageBox.Show("Не удалось удалить объект"); - } - } - private void ButtonRefreshCollection_Click(object sender, EventArgs e) - { - pictureBoxCollection.Image = _monorails.ShowMonorails(); - } - } -} +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 Monorail.DrawningObjects; +using Monorail.Generics; +using Monorail.MovementStrategy; + +namespace Monorail +{ + + public partial class FormMonorailCollection : Form + { + private readonly MonorailsGenericStorage _storage; + + public FormMonorailCollection() + { + InitializeComponent(); + _storage = new MonorailsGenericStorage + (pictureBoxCollection.Width, pictureBoxCollection.Height); + } + 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; + } + } + 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(); + } + private void ListBoxObjects_SelectedIndexChanged(object sender, +EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowMonorails(); + } + 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(); + } + } + + + private void ButtonAddMonorail_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + + FormMonorail form = new(); + + if (form.ShowDialog() == DialogResult.OK) + { + int addedIndex = obj + form.SelectedMonorail; + if (addedIndex != -1 && addedIndex <= 20) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowMonorails(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + private void ButtonRemoveMonorail_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; + if (maskedTextBoxNumber.Text == "") + { + MessageBox.Show("Введите позицию элемента выше"); + return; + } + else + { + pos = Convert.ToInt32(maskedTextBoxNumber.Text); + } + if (obj - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = obj.ShowMonorails(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowMonorails(); + } + } +} diff --git a/Monorail/Monorail/FormMonorailCollection.resx b/Monorail/Monorail/FormMonorailCollection.resx index a395bff..0f362c9 100644 --- a/Monorail/Monorail/FormMonorailCollection.resx +++ b/Monorail/Monorail/FormMonorailCollection.resx @@ -1,120 +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 - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/Monorail/Monorail/Generics/MonorailsGenericCollection.cs b/Monorail/Monorail/Generics/MonorailsGenericCollection.cs index 0850b17..6ad6ab4 100644 --- a/Monorail/Monorail/Generics/MonorailsGenericCollection.cs +++ b/Monorail/Monorail/Generics/MonorailsGenericCollection.cs @@ -33,18 +33,18 @@ namespace Monorail.Generics } return collect._collection.Insert(obj); } - public static bool operator -(MonorailsGenericCollection collect, int pos) + public static T? operator -(MonorailsGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { - return collect._collection.Remove(pos); + collect._collection.Remove(pos); } - return false; + return obj; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowMonorails() { @@ -78,7 +78,7 @@ namespace Monorail.Generics { // TODO получение объекта - DrawningMonorail? _monorail = _collection.Get(indexPlaces); + DrawningMonorail? _monorail = _collection[indexPlaces]; indexPlaces++; if (_monorail != null) diff --git a/Monorail/Monorail/Generics/MonorailsGenericStorage.cs b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs new file mode 100644 index 0000000..de7f0aa --- /dev/null +++ b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Monorail.DrawningObjects; +using Monorail.MovementStrategy; + +namespace Monorail.Generics +{ + internal class MonorailsGenericStorage + { + readonly Dictionary> _monorailsStorages; + public List Keys => _monorailsStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public MonorailsGenericStorage(int pictureWidth, int pictureHeight) + { + _monorailsStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; ; + } + public void AddSet(string name) + { + //TO DO + _monorailsStorages.Add(name, new MonorailsGenericCollection(_pictureWidth, _pictureHeight)); + } + public void DelSet(string name) + { + if (_monorailsStorages[name] == null) + { + return; + } + _monorailsStorages[name] = null; + } + public MonorailsGenericCollection? this[string ind] + { + get + { + //TO DO + if (_monorailsStorages[ind] == null) + { + return null; + } + return _monorailsStorages[ind]; + } + } + } +} diff --git a/Monorail/Monorail/Generics/SetGeneric.cs b/Monorail/Monorail/Generics/SetGeneric.cs index 2391130..755b4fb 100644 --- a/Monorail/Monorail/Generics/SetGeneric.cs +++ b/Monorail/Monorail/Generics/SetGeneric.cs @@ -1,95 +1,104 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Monorail.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 monorail) - { - if (monorail == null) - { - return -1; - } - - for (int i = Count-1; i != 0; i--) - { - _places[i] = _places[i - 1]; - } - _places[0] = monorail; - - return 0; - } - public bool Insert(T monorail, int position) - { - // TODO проверка позиции - if (position >= Count) - { - return false; - } - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - if (_places[position] != null) - { - for(int i = position + 1; i= Count) - { - return false; - } - // TODO удаление объекта из массива, присвоив элементу массива значение null - if (_places[position] != null) - { - _places[position] = null; - return true; - } - else - { - return false; - } - } - public T? Get(int position) - { - // TODO проверка позиции - if(position >= Count) - { - return null; - } - return _places[position]; - } - - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail.Generics +{ + internal class SetGeneric + where T : class + { + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; + public SetGeneric(int count) + { + _maxCount = count; + _places = new List(count); + } + public int Insert(T monorail) + { + return Insert(monorail, 0); + } + public int Insert(T monorail, int position) + { + if (Count == _maxCount) + return -1; + + if (position < 0 || monorail == null) + return -1; + + if (position >= _maxCount) + return -1; + + + if (Count == 0) + { + _places.Add(monorail); + } + else + { + _places.Insert(position, monorail); + } + return Count; + } + public bool Remove(int position) + { + // TODO проверка позиции + if (position >= Count) + { + return false; + } + // TODO удаление объекта из массива, присвоив элементу массива значение null + if (_places[position] != null) + { + _places[position] = null; + return true; + } + else + { + return false; + } + } + public T? this[int position] + { + get { + // TODO проверка позиции + if (position >= Count) + { + return null; + } + return _places[position]; + } + set + { + if(position >= Count) + { + return; + } + if (_places[position] != null) + { + return; + } + else + { + _places[position] = this[position]; + } + } + } + public IEnumerable GetMonorails(int? maxMonorails = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxMonorails.HasValue && i == maxMonorails.Value) + { + yield break; + } + } + } + + + } +}