diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs index e85df62..a784a61 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs @@ -28,97 +28,172 @@ /// private void InitializeComponent() { - pictureBoxCollection = new PictureBox(); - panelTools = new Panel(); - textBox = new TextBox(); - buttonUpdateColletion = new Button(); - buttonDeleteBulldozer = new Button(); - buttonAddBulldozer = new Button(); - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); - panelTools.SuspendLayout(); - SuspendLayout(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.panelTools = new System.Windows.Forms.Panel(); + this.groupListBulldozersBox = new System.Windows.Forms.GroupBox(); + this.buttonDeleteBulldozersList = new System.Windows.Forms.Button(); + this.buttonAddBulldozersList = new System.Windows.Forms.Button(); + this.listBoxBulldozerStorages = new System.Windows.Forms.ListBox(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.textBoxDeletingBulldozer = new System.Windows.Forms.TextBox(); + this.buttonUpdateColletion = new System.Windows.Forms.Button(); + this.buttonDeleteBulldozer = new System.Windows.Forms.Button(); + this.buttonAddBulldozer = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.panelTools.SuspendLayout(); + this.groupListBulldozersBox.SuspendLayout(); + this.SuspendLayout(); // // pictureBoxCollection // - pictureBoxCollection.Anchor = AnchorStyles.Left; - pictureBoxCollection.BackColor = SystemColors.Control; - pictureBoxCollection.Location = new Point(12, 12); - pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(711, 571); - pictureBoxCollection.TabIndex = 0; - pictureBoxCollection.TabStop = false; + this.pictureBoxCollection.Anchor = System.Windows.Forms.AnchorStyles.Left; + this.pictureBoxCollection.BackColor = System.Drawing.SystemColors.Control; + this.pictureBoxCollection.Location = new System.Drawing.Point(14, 16); + this.pictureBoxCollection.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(813, 761); + this.pictureBoxCollection.TabIndex = 0; + this.pictureBoxCollection.TabStop = false; // // panelTools // - panelTools.Controls.Add(textBox); - panelTools.Controls.Add(buttonUpdateColletion); - panelTools.Controls.Add(buttonDeleteBulldozer); - panelTools.Controls.Add(buttonAddBulldozer); - panelTools.Location = new Point(729, 12); - panelTools.Name = "panelTools"; - panelTools.Size = new Size(234, 571); - panelTools.TabIndex = 1; - panelTools.Tag = ""; + this.panelTools.Anchor = System.Windows.Forms.AnchorStyles.Right; + this.panelTools.Controls.Add(this.groupListBulldozersBox); + this.panelTools.Controls.Add(this.textBoxDeletingBulldozer); + this.panelTools.Controls.Add(this.buttonUpdateColletion); + this.panelTools.Controls.Add(this.buttonDeleteBulldozer); + this.panelTools.Controls.Add(this.buttonAddBulldozer); + this.panelTools.Location = new System.Drawing.Point(833, 16); + this.panelTools.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.panelTools.Name = "panelTools"; + this.panelTools.Size = new System.Drawing.Size(267, 761); + this.panelTools.TabIndex = 1; + this.panelTools.Tag = ""; // - // textBox + // groupListBulldozersBox // - textBox.Location = new Point(23, 164); - textBox.Name = "textBox"; - textBox.Size = new Size(193, 23); - textBox.TabIndex = 3; + this.groupListBulldozersBox.Controls.Add(this.buttonDeleteBulldozersList); + this.groupListBulldozersBox.Controls.Add(this.buttonAddBulldozersList); + this.groupListBulldozersBox.Controls.Add(this.listBoxBulldozerStorages); + this.groupListBulldozersBox.Controls.Add(this.textBoxStorageName); + this.groupListBulldozersBox.Location = new System.Drawing.Point(16, 33); + this.groupListBulldozersBox.Name = "groupListBulldozersBox"; + this.groupListBulldozersBox.Size = new System.Drawing.Size(230, 282); + this.groupListBulldozersBox.TabIndex = 8; + this.groupListBulldozersBox.TabStop = false; + this.groupListBulldozersBox.Text = "Наборы"; + // + // buttonDeleteBulldozersList + // + this.buttonDeleteBulldozersList.Location = new System.Drawing.Point(6, 226); + this.buttonDeleteBulldozersList.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonDeleteBulldozersList.Name = "buttonDeleteBulldozersList"; + this.buttonDeleteBulldozersList.Size = new System.Drawing.Size(221, 53); + this.buttonDeleteBulldozersList.TabIndex = 7; + this.buttonDeleteBulldozersList.Text = "Удалить набор"; + this.buttonDeleteBulldozersList.UseVisualStyleBackColor = true; + this.buttonDeleteBulldozersList.Click += new System.EventHandler(this.buttonDeleteBulldozersList_Click); + // + // buttonAddBulldozersList + // + this.buttonAddBulldozersList.Location = new System.Drawing.Point(6, 55); + this.buttonAddBulldozersList.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonAddBulldozersList.Name = "buttonAddBulldozersList"; + this.buttonAddBulldozersList.Size = new System.Drawing.Size(221, 53); + this.buttonAddBulldozersList.TabIndex = 6; + this.buttonAddBulldozersList.Text = "Добавить набор"; + this.buttonAddBulldozersList.UseVisualStyleBackColor = true; + this.buttonAddBulldozersList.Click += new System.EventHandler(this.buttonAddBulldozersList_Click); + // + // listBoxBulldozerStorages + // + this.listBoxBulldozerStorages.FormattingEnabled = true; + this.listBoxBulldozerStorages.ItemHeight = 20; + this.listBoxBulldozerStorages.Location = new System.Drawing.Point(6, 115); + this.listBoxBulldozerStorages.Name = "listBoxBulldozerStorages"; + this.listBoxBulldozerStorages.Size = new System.Drawing.Size(220, 104); + this.listBoxBulldozerStorages.TabIndex = 5; + this.listBoxBulldozerStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxBulldozerStorages_SelectedIndexChanged); + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(7, 20); + this.textBoxStorageName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(220, 27); + this.textBoxStorageName.TabIndex = 4; + // + // textBoxDeletingBulldozer + // + this.textBoxDeletingBulldozer.Location = new System.Drawing.Point(25, 525); + this.textBoxDeletingBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.textBoxDeletingBulldozer.Name = "textBoxDeletingBulldozer"; + this.textBoxDeletingBulldozer.Size = new System.Drawing.Size(220, 27); + this.textBoxDeletingBulldozer.TabIndex = 3; // // buttonUpdateColletion // - buttonUpdateColletion.Location = new Point(23, 263); - buttonUpdateColletion.Name = "buttonUpdateColletion"; - buttonUpdateColletion.Size = new Size(193, 40); - buttonUpdateColletion.TabIndex = 2; - buttonUpdateColletion.Text = "Обновить коллекцию"; - buttonUpdateColletion.UseVisualStyleBackColor = true; - buttonUpdateColletion.Click += ButtonUpdateCollection_Click; + this.buttonUpdateColletion.Location = new System.Drawing.Point(25, 692); + this.buttonUpdateColletion.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonUpdateColletion.Name = "buttonUpdateColletion"; + this.buttonUpdateColletion.Size = new System.Drawing.Size(221, 53); + this.buttonUpdateColletion.TabIndex = 2; + this.buttonUpdateColletion.Text = "Обновить коллекцию"; + this.buttonUpdateColletion.UseVisualStyleBackColor = true; // // buttonDeleteBulldozer // - buttonDeleteBulldozer.Location = new Point(23, 193); - buttonDeleteBulldozer.Name = "buttonDeleteBulldozer"; - buttonDeleteBulldozer.Size = new Size(193, 40); - buttonDeleteBulldozer.TabIndex = 1; - buttonDeleteBulldozer.Text = "Удалить объект"; - buttonDeleteBulldozer.UseVisualStyleBackColor = true; - buttonDeleteBulldozer.Click += ButtonDeleteBulldozer_Click; + this.buttonDeleteBulldozer.Location = new System.Drawing.Point(25, 563); + this.buttonDeleteBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonDeleteBulldozer.Name = "buttonDeleteBulldozer"; + this.buttonDeleteBulldozer.Size = new System.Drawing.Size(221, 53); + this.buttonDeleteBulldozer.TabIndex = 1; + this.buttonDeleteBulldozer.Text = "Удалить объект"; + this.buttonDeleteBulldozer.UseVisualStyleBackColor = true; + this.buttonDeleteBulldozer.Click += new System.EventHandler(this.buttonDeleteBulldozer_Click); // // buttonAddBulldozer // - buttonAddBulldozer.Location = new Point(23, 32); - buttonAddBulldozer.Name = "buttonAddBulldozer"; - buttonAddBulldozer.Size = new Size(193, 40); - buttonAddBulldozer.TabIndex = 0; - buttonAddBulldozer.Text = "Добавить объект"; - buttonAddBulldozer.UseVisualStyleBackColor = true; - buttonAddBulldozer.Click += ButtonAddBulldozer_Click; + this.buttonAddBulldozer.Location = new System.Drawing.Point(24, 464); + this.buttonAddBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonAddBulldozer.Name = "buttonAddBulldozer"; + this.buttonAddBulldozer.Size = new System.Drawing.Size(221, 53); + this.buttonAddBulldozer.TabIndex = 0; + this.buttonAddBulldozer.Text = "Добавить объект"; + this.buttonAddBulldozer.UseVisualStyleBackColor = true; + this.buttonAddBulldozer.Click += new System.EventHandler(this.buttonAddBulldozer_Click); // // FormBulldozerCollection // - AutoScaleDimensions = new SizeF(7F, 15F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(971, 595); - Controls.Add(panelTools); - Controls.Add(pictureBoxCollection); - Name = "FormBulldozerCollection"; - Text = "Bulldozer Collection"; - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); - panelTools.ResumeLayout(false); - panelTools.PerformLayout(); - ResumeLayout(false); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1110, 793); + this.Controls.Add(this.panelTools); + this.Controls.Add(this.pictureBoxCollection); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.Name = "FormBulldozerCollection"; + this.Text = "Bulldozer Collection"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.panelTools.ResumeLayout(false); + this.panelTools.PerformLayout(); + this.groupListBulldozersBox.ResumeLayout(false); + this.groupListBulldozersBox.PerformLayout(); + this.ResumeLayout(false); + } #endregion private PictureBox pictureBoxCollection; private Panel panelTools; - private TextBox textBox; + private TextBox textBoxDeletingBulldozer; private Button buttonUpdateColletion; private Button buttonDeleteBulldozer; private Button buttonAddBulldozer; + private ListBox listBoxBulldozerStorages; + private TextBox textBoxStorageName; + private GroupBox groupListBulldozersBox; + private Button buttonDeleteBulldozersList; + private Button buttonAddBulldozersList; } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.cs index 15c6f8f..df57677 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.cs @@ -7,44 +7,142 @@ namespace Bulldozer { public partial class FormBulldozerCollection : Form { - private readonly BulldozersGenericCollection _bulldozers; + /// + /// Набор объектов + /// + private readonly BulldozersGenericStorage _storage; + + public FormBulldozerCollection() { InitializeComponent(); - _bulldozers = new BulldozersGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new BulldozersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } - private void ButtonAddBulldozer_Click(object sender, EventArgs e) + + /// + /// Заполнение listBoxBulldozerStorages + /// + private void ReloadObjects() { + int index = listBoxBulldozerStorages.SelectedIndex; + listBoxBulldozerStorages.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxBulldozerStorages.Items.Add(_storage.Keys[i]); + } + if (listBoxBulldozerStorages.Items.Count > 0 && (index == -1 || index >= listBoxBulldozerStorages.Items.Count)) + { + listBoxBulldozerStorages.SelectedIndex = 0; + } + else if (listBoxBulldozerStorages.Items.Count > 0 && index > -1 && index < listBoxBulldozerStorages.Items.Count) + { + listBoxBulldozerStorages.SelectedIndex = index; + } + } + + /// + /// Добавление набора в коллекцию + /// + /// + /// + private void buttonAddBulldozersList_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + + /// + /// Выбор набора + /// + /// + /// + private void listBoxBulldozerStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxBulldozerStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBulldozers(); + } + + /// + /// Удаление набора + /// + /// + /// + private void buttonDeleteBulldozersList_Click(object sender, EventArgs e) + { + if (listBoxBulldozerStorages.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {listBoxBulldozerStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); + } + } + + /// + /// Добавление объекта в набор + /// + /// + /// + private void buttonAddBulldozer_Click(object sender, EventArgs e) + { + if (listBoxBulldozerStorages.SelectedIndex == -1) + { + MessageBox.Show("Выберите набор в списке.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + var obj = _storage[listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + MessageBox.Show("Выбранный набор не найден.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } FormBulldozer form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_bulldozers + form.SelectedBulldozer != null) + if (obj + form.SelectedBulldozer > -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _bulldozers.ShowBulldozers(); + pictureBoxCollection.Image = obj.ShowBulldozers(); } else { MessageBox.Show("Не удалось добавить объект"); } - } } - private void ButtonDeleteBulldozer_Click(object sender, EventArgs e) + /// + /// Удаление объекта из набора + /// + /// + /// + private void buttonDeleteBulldozer_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) - + if (listBoxBulldozerStorages.SelectedIndex == -1) { return; } - int pos = Convert.ToInt32(textBox.Text); - if (_bulldozers - pos != null) + var obj = _storage[listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(textBoxDeletingBulldozer.Text); + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _bulldozers.ShowBulldozers(); + pictureBoxCollection.Image = obj.ShowBulldozers(); } else { @@ -52,9 +150,23 @@ namespace Bulldozer } } + /// + /// Обновление рисунка по набору + /// + /// + /// private void ButtonUpdateCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _bulldozers.ShowBulldozers(); + if (listBoxBulldozerStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowBulldozers(); } } } diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.resx b/Bulldozer/Bulldozer/FormBulldozerCollection.resx index af32865..f298a7b 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.resx +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.resx @@ -1,64 +1,4 @@ - - - + diff --git a/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs b/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs index a7b11a6..5102687 100644 --- a/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs +++ b/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs @@ -18,7 +18,7 @@ namespace Bulldozer.Generics /// /// Размер занимаемого объектом места (ширина) /// - private readonly int _placeSizeWidth = 300; + private readonly int _placeSizeWidth = 270; /// /// Размер занимаемого объектом места (высота) /// @@ -53,13 +53,20 @@ namespace Bulldozer.Generics return collect?._collection.Insert(obj) ?? -1; } - public static bool operator -(BulldozersGenericCollection collect, int -pos) + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T? operator -(BulldozersGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) - return collect._collection.Remove(pos); - return false; + { + collect._collection.Remove(pos); + } + return obj; } /// /// Получение объекта IMoveableObject @@ -68,7 +75,7 @@ pos) /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -95,7 +102,7 @@ pos) 1; ++j) { g.DrawLine(pen, i * _placeSizeWidth, j * - _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j * _placeSizeHeight); } g.DrawLine(pen, i * _placeSizeWidth, 0, i * @@ -111,19 +118,24 @@ pos) int heightObjCount = _pictureHeight / _placeSizeHeight; int widthObjCount = _pictureWidth / _placeSizeWidth; int totalObjects = _collection.Count; - - for (int i = 0; i < totalObjects; i++) + int i = 0; + foreach (var bulldozer in _collection.GetBulldozers()) { - T? type = _collection.Get(i); - if (type != null) + + if (bulldozer != null) { int col = widthObjCount - 1 - (i % widthObjCount); int row = heightObjCount - 1 - (i / widthObjCount); - type.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); - type?.DrawBulldozer(g); + bulldozer.SetPosition(col * _placeSizeWidth + 3, row * _placeSizeHeight + 3); + bulldozer?.DrawBulldozer(g); + i++; } - } + if (i > totalObjects) + { + return; + } + } } } } diff --git a/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs b/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs new file mode 100644 index 0000000..41934ac --- /dev/null +++ b/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs @@ -0,0 +1,87 @@ +using Bulldozer.DrawingObjects; +using Bulldozer.MovementStrategy; + +namespace Bulldozer.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class BulldozersGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _bulldozerStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _bulldozerStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public BulldozersGenericStorage(int pictureWidth, int pictureHeight) + { + _bulldozerStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (!_bulldozerStorages.ContainsKey(name)) + { + _bulldozerStorages.Add(name, new BulldozersGenericCollection(_pictureWidth, _pictureHeight)); + } + + } + + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + // TODO: Прописать логику для удаления набора + if (_bulldozerStorages.ContainsKey(name)) + { + _bulldozerStorages.Remove(name); + } + + } + /// + /// Доступ к набору + /// + /// + /// + public BulldozersGenericCollection? + this[string ind] + { + get + { + // TODO: Продумать логику получения набора + if (_bulldozerStorages.ContainsKey(ind)) + { + return _bulldozerStorages[ind]; + } + else + { + return null; + } + } + } + } + +} diff --git a/Bulldozer/Bulldozer/Generics/SetGeneric.cs b/Bulldozer/Bulldozer/Generics/SetGeneric.cs index 74ad077..33aa1c1 100644 --- a/Bulldozer/Bulldozer/Generics/SetGeneric.cs +++ b/Bulldozer/Bulldozer/Generics/SetGeneric.cs @@ -1,4 +1,6 @@ -namespace Bulldozer.Generics +using System.Numerics; + +namespace Bulldozer.Generics { /// /// Параметризованный набор объектов @@ -8,57 +10,49 @@ where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор /// - /// Добавляемый автомобиль + /// Добавляемый бульдозер /// public int Insert(T bulldozer) { - if (_places[Count - 1] != null) + if (_places.Count == _maxCount) return -1; - return Insert(bulldozer, 0); - + Insert(bulldozer, 0); + return 1; } /// /// Добавление объекта в набор на конкретную позицию /// - /// Добавляемый автомобиль - /// Позиция + /// Добавляемый бульдозер /// - public int Insert(T bulldozer, int position) + public int Insert(T bulldozer , int position) { - if (!(position >= 0 && position < Count)) - return -1; - if (_places[position] != null) - { - int ind = position; - while (ind < Count && _places[ind] != null) - ind++; - if (ind == Count) - return -1; - for (int i = ind - 1; i >= position; i--) - _places[i + 1] = _places[i]; - } - _places[position] = bulldozer; + if (position < 0 || position >= _maxCount) return -1; + _places.Insert(position, bulldozer); return position; - } /// /// Удаление объекта из набора с конкретной позиции @@ -67,9 +61,9 @@ /// public bool Remove(int position) { - if (!(position >= 0 && position < Count) || _places[position] == null) + if (position < 0 || position >= Count) return false; - _places[position] = null; + _places.RemoveAt(position); return true; } /// @@ -79,9 +73,45 @@ /// public T? Get(int position) { - if (!(position >= 0 && position < Count)) + if (position < 0 || position >= Count) + { + // Позиция находится за пределами допустимого диапазона, вернем null return null; + } return _places[position]; } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? this[int position] + { + get + { + if (position < 0 || position >= Count) return null; + return _places[position]; + } + set + { + if (position < 0 || position >= Count || Count == _maxCount) return; + _places.Insert(position, value); + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetBulldozers(int? maxBulldozers = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxBulldozers.HasValue && i == maxBulldozers.Value) + { + yield break; + } + } + } } } \ No newline at end of file