From 3a6b043a1b479be2171492f523d3d34833335e50 Mon Sep 17 00:00:00 2001 From: "[USERNAME]" Date: Sun, 26 Nov 2023 11:47:30 +0400 Subject: [PATCH] Lab 4 --- .../Bulldozer/BulldozerGenericCollection.cs | 41 +++-- .../Bulldozer/BulldozersGenericStorage.cs | 87 ++++++++++ .../FormBulldozerCollection.Designer.cs | 100 +++++++++-- .../Bulldozer/FormBulldozerCollection.cs | 156 +++++++++++++++--- Bulldozer/Bulldozer/SetGeneric.cs | 72 ++++---- 5 files changed, 366 insertions(+), 90 deletions(-) create mode 100644 Bulldozer/Bulldozer/BulldozersGenericStorage.cs diff --git a/Bulldozer/Bulldozer/BulldozerGenericCollection.cs b/Bulldozer/Bulldozer/BulldozerGenericCollection.cs index ad36459..a5fece3 100644 --- a/Bulldozer/Bulldozer/BulldozerGenericCollection.cs +++ b/Bulldozer/Bulldozer/BulldozerGenericCollection.cs @@ -70,7 +70,7 @@ namespace Bulldozer.Generics public static bool operator -(BulldozerGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { return collect._collection.Remove(pos); @@ -84,7 +84,7 @@ namespace Bulldozer.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -126,32 +126,31 @@ namespace Bulldozer.Generics { int c = 11; int k = 3; - for (int i = 0; i < _collection.Count; i++) + foreach (var tractor in _collection.GetBulldozer()) { - if (i % 3 == 0 && i != 0) + if (tractor != null) { - c = c - 3; - } + int i = _collection.GetBulldozer().ToList().IndexOf(tractor); + if (i % 3 == 0 && i != 0) + { + c = c - 3; + } + + if (k != 0) + { + k--; + } + else + { + k = 2; + } - if (k != 0) - { - k--; - } - else - { - k = 2; - } - T? obj = _collection.Get(i); - if (obj != null) - { - // Получение объекта - IMoveableObject moveableObject = obj.GetMoveableObject; // Установка позиции int x = k % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth; int y = c / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight + 10; - moveableObject.SetPosition(x, y); + tractor.SetPosition(x, y); // Прорисовка объекта - moveableObject.Draw(g); + tractor.DrawTrasport(g); } } } diff --git a/Bulldozer/Bulldozer/BulldozersGenericStorage.cs b/Bulldozer/Bulldozer/BulldozersGenericStorage.cs new file mode 100644 index 0000000..50800b2 --- /dev/null +++ b/Bulldozer/Bulldozer/BulldozersGenericStorage.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Bulldozer.DrawningObjects; +using Bulldozer.Generics; +using Bulldozer.MovementStrategy; +using Bulldozer.Drawnings; + +namespace Bulldozer.Generics +{ + internal class BulldozersGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _tractorStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _tractorStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public BulldozersGenericStorage(int pictureWidth, int pictureHeight) + { + _tractorStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + // TODO Прописать логику для добавления + if (!_tractorStorages.ContainsKey(name)) + { + var tractorCollection = new BulldozerGenericCollection(_pictureWidth, _pictureHeight); + _tractorStorages.Add(name, tractorCollection); + } + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + // TODO Прописать логику для удаления + if (_tractorStorages.ContainsKey(name)) + { + _tractorStorages.Remove(name); + } + } + /// + /// Доступ к набору + /// + /// + /// + public BulldozerGenericCollection? this[string ind] + { + get + { + // TODO Продумать логику получения набора + if (_tractorStorages.ContainsKey(ind)) + { + return _tractorStorages[ind]; + } + return null; + } + } + } +} diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs index 859fc68..ede372e 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs @@ -34,14 +34,21 @@ pictureBoxCollection = new PictureBox(); maskedTextBoxNumber = new MaskedTextBox(); groupBox1 = new GroupBox(); + groupBox2 = new GroupBox(); + listBoxStorage = new ListBox(); + ButtonDelObject = new Button(); + ButtonAddObject = new Button(); + textBoxStorageName = new TextBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + groupBox1.SuspendLayout(); + groupBox2.SuspendLayout(); SuspendLayout(); // // ButtonAddBulldozer // - ButtonAddBulldozer.Location = new Point(668, 45); + ButtonAddBulldozer.Location = new Point(26, 314); ButtonAddBulldozer.Name = "ButtonAddBulldozer"; - ButtonAddBulldozer.Size = new Size(166, 31); + ButtonAddBulldozer.Size = new Size(166, 40); ButtonAddBulldozer.TabIndex = 0; ButtonAddBulldozer.Text = "Добавить трактор"; ButtonAddBulldozer.UseVisualStyleBackColor = true; @@ -49,9 +56,9 @@ // // ButtonRemoveBulldozer // - ButtonRemoveBulldozer.Location = new Point(668, 129); + ButtonRemoveBulldozer.Location = new Point(26, 400); ButtonRemoveBulldozer.Name = "ButtonRemoveBulldozer"; - ButtonRemoveBulldozer.Size = new Size(166, 31); + ButtonRemoveBulldozer.Size = new Size(166, 39); ButtonRemoveBulldozer.TabIndex = 1; ButtonRemoveBulldozer.Text = "Удалить трактор"; ButtonRemoveBulldozer.UseVisualStyleBackColor = true; @@ -59,9 +66,9 @@ // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(668, 215); + ButtonRefreshCollection.Location = new Point(26, 478); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - ButtonRefreshCollection.Size = new Size(166, 31); + ButtonRefreshCollection.Size = new Size(166, 40); ButtonRefreshCollection.TabIndex = 2; ButtonRefreshCollection.Text = "Обновить коллекцию"; ButtonRefreshCollection.UseVisualStyleBackColor = true; @@ -71,7 +78,7 @@ // pictureBoxCollection.Location = new Point(0, 0); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(624, 457); + pictureBoxCollection.Size = new Size(637, 530); pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom; pictureBoxCollection.TabIndex = 3; pictureBoxCollection.TabStop = false; @@ -79,7 +86,7 @@ // maskedTextBoxNumber // maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point); - maskedTextBoxNumber.Location = new Point(698, 100); + maskedTextBoxNumber.Location = new Point(56, 371); maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(100, 22); @@ -88,29 +95,83 @@ // // groupBox1 // - groupBox1.Location = new Point(646, 12); + groupBox1.Controls.Add(groupBox2); + groupBox1.Controls.Add(ButtonAddBulldozer); + groupBox1.Controls.Add(ButtonRefreshCollection); + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(ButtonRemoveBulldozer); + groupBox1.Location = new Point(643, 12); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(209, 412); + groupBox1.Size = new Size(209, 533); groupBox1.TabIndex = 5; groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // groupBox2 + // + groupBox2.Controls.Add(listBoxStorage); + groupBox2.Controls.Add(ButtonDelObject); + groupBox2.Controls.Add(ButtonAddObject); + groupBox2.Controls.Add(textBoxStorageName); + groupBox2.Location = new Point(6, 22); + groupBox2.Name = "groupBox2"; + groupBox2.Size = new Size(197, 274); + groupBox2.TabIndex = 6; + groupBox2.TabStop = false; + groupBox2.Text = "Наборы"; + // + // listBoxStorage + // + listBoxStorage.FormattingEnabled = true; + listBoxStorage.ItemHeight = 15; + listBoxStorage.Location = new Point(20, 106); + listBoxStorage.Name = "listBoxStorage"; + listBoxStorage.Size = new Size(158, 109); + listBoxStorage.TabIndex = 9; + listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; + // + // ButtonDelObject + // + ButtonDelObject.Location = new Point(33, 235); + ButtonDelObject.Name = "ButtonDelObject"; + ButtonDelObject.Size = new Size(136, 33); + ButtonDelObject.TabIndex = 8; + ButtonDelObject.Text = "Удалить набор"; + ButtonDelObject.UseVisualStyleBackColor = true; + ButtonDelObject.Click += ButtonDelObject_Click; + // + // ButtonAddObject + // + ButtonAddObject.Location = new Point(33, 54); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(136, 33); + ButtonAddObject.TabIndex = 7; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(20, 22); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(158, 23); + textBoxStorageName.TabIndex = 0; + // // FormBulldozerCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(877, 469); - Controls.Add(ButtonRefreshCollection); - Controls.Add(ButtonRemoveBulldozer); - Controls.Add(maskedTextBoxNumber); - Controls.Add(ButtonAddBulldozer); + ClientSize = new Size(867, 584); Controls.Add(groupBox1); Controls.Add(pictureBoxCollection); Name = "FormBulldozerCollection"; - Text = "Набор трактористов"; + Text = "Набор тракторов"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + groupBox2.ResumeLayout(false); + groupBox2.PerformLayout(); ResumeLayout(false); - PerformLayout(); } #endregion @@ -121,5 +182,10 @@ private PictureBox pictureBoxCollection; private MaskedTextBox maskedTextBoxNumber; private GroupBox groupBox1; + private GroupBox groupBox2; + private TextBox textBoxStorageName; + private ListBox listBoxStorage; + private Button ButtonDelObject; + private Button ButtonAddObject; } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.cs index 4f077bf..8e9988b 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.cs @@ -1,37 +1,121 @@ -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 Bulldozer.DrawningObjects; using Bulldozer.Drawnings; using Bulldozer.Generics; using Bulldozer.MovementStrategy; - namespace Bulldozer { + /// + /// Форма для работы с набором объектов класса DrawningCar + /// public partial class FormBulldozerCollection : Form { - private readonly BulldozerGenericCollection _tractor; + /// + /// Набор объектов + /// + private readonly BulldozersGenericStorage _storage; + /// + /// Конструктор + /// public FormBulldozerCollection() { InitializeComponent(); - _tractor = new BulldozerGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new BulldozersGenericStorage(pictureBoxCollection.Width, + pictureBoxCollection.Height); } + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + int index = listBoxStorage.SelectedIndex; + listBoxStorage.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorage.Items.Add(_storage.Keys[i]); + } + if (listBoxStorage.Items.Count > 0 && (index == -1 || index + >= listBoxStorage.Items.Count)) + { + listBoxStorage.SelectedIndex = 0; + } + else if (listBoxStorage.Items.Count > 0 && index > -1 && + index < listBoxStorage.Items.Count) + { + listBoxStorage.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 listBoxStorage_SelectedIndexChanged(object sender, + EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowBulldozer(); + } + /// + /// Удаление набора + /// + /// + /// + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + + { + _storage.DelSet(listBoxStorage.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } + } + /// + /// Добавление объекта в набор + /// + /// + /// private void ButtonAddBulldozer_Click(object sender, EventArgs e) { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FastBulldozer form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_tractor + form.SelectedTractor > -1) + if (obj + form.SelectedTractor > -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _tractor.ShowBulldozer(); + pictureBoxCollection.Image = obj.ShowBulldozer(); } else { @@ -39,27 +123,59 @@ namespace Bulldozer } } } + /// + /// Удаление объекта из набора + /// + /// + /// private void ButtonRemoveBulldozer_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_tractor - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _tractor.ShowBulldozer(); + pictureBoxCollection.Image = obj.ShowBulldozer(); } else { MessageBox.Show("Не удалось удалить объект"); } - } - private void ButtonRefreshCollection_Click(object sender, EventArgs e) + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs + e) { - pictureBoxCollection.Image = _tractor.ShowBulldozer(); + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowBulldozer(); } } } + diff --git a/Bulldozer/Bulldozer/SetGeneric.cs b/Bulldozer/Bulldozer/SetGeneric.cs index 0236517..930ef2c 100644 --- a/Bulldozer/Bulldozer/SetGeneric.cs +++ b/Bulldozer/Bulldozer/SetGeneric.cs @@ -10,18 +10,23 @@ /// /// Массив объектов, которые храним /// - 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(count); } /// /// Добавление объекта в набор @@ -41,32 +46,14 @@ public int Insert(T tractor, int position) { // TODO проверка позиции - if (position < 0 || position >= Count) + if (position < 0 || position >= _maxCount) { // Позиция недопустима return -1; } - if (_places[position] != null) - { - int d = 0; - for(int j = 1; j < Count; j++) - { - if (_places[position + j] == null) - { - d = position + j; - break; - } - } - if(d == 0) - { - return -1; - } - for(int j = d; j > position; j--) - { - _places[j] = _places[j - 1]; - } - } - _places[position] = tractor; + if (Count >= _maxCount) + return -1; + _places.Insert(position, tractor); return position; } @@ -79,13 +66,13 @@ { // TODO проверка позиции // Проверка позиции - if (position < 0 || position >= _places.Length) + if ((position < 0) || (position > _maxCount)) { // Позиция недопустима return false; } // TODO удаление объекта из массива, присвоив элементу массива значение null - _places[position] = null; + _places.RemoveAt(position); return true; } /// @@ -93,15 +80,36 @@ /// /// /// - public T? Get(int position) + public T? this[int position] { // TODO проверка позиции - if (position < 0 || position >= _places.Length) + get { - // Позиция недопустима - return null; + if (position < 0 || position > _maxCount) + return null; + return _places[position]; + } + set + { + if (position < 0 || position > _maxCount) + return; + _places[position] = value; + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetBulldozer(int? maxTractor = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxTractor.HasValue && i == maxTractor.Value) + { + yield break; + } } - return _places[position]; } } }