From e40550d7a2d31bbbbfdbdb773e0945c884ccd3a4 Mon Sep 17 00:00:00 2001 From: Factorino73 Date: Tue, 24 Oct 2023 15:41:30 +0400 Subject: [PATCH 1/2] PIbd-21_MasenkinMS_LabWork04 --- .../BusCollectionForm.Designer.cs | 71 +++++++++-- .../AccordionBus/BusCollectionForm.cs | 118 ++++++++++++++++-- .../AccordionBus/BusGenericCollection.cs | 8 +- .../AccordionBus/BusGenericStorage.cs | 92 ++++++++++++++ AccordionBus/AccordionBus/SetGeneric.cs | 94 +++++++------- 5 files changed, 316 insertions(+), 67 deletions(-) create mode 100644 AccordionBus/AccordionBus/BusGenericStorage.cs diff --git a/AccordionBus/AccordionBus/BusCollectionForm.Designer.cs b/AccordionBus/AccordionBus/BusCollectionForm.Designer.cs index 37deb1a..849a532 100644 --- a/AccordionBus/AccordionBus/BusCollectionForm.Designer.cs +++ b/AccordionBus/AccordionBus/BusCollectionForm.Designer.cs @@ -30,7 +30,11 @@ { pictureBoxCollection = new PictureBox(); panelTools = new Panel(); - maskedTextBox = new MaskedTextBox(); + buttonDeleteObject = new Button(); + listBoxStorages = new ListBox(); + maskedTextBoxStorageName = new MaskedTextBox(); + buttonAddObject = new Button(); + maskedTextBoxNumber = new MaskedTextBox(); buttonRefreshCollection = new Button(); buttonRemoveBus = new Button(); buttonAddBus = new Button(); @@ -49,7 +53,11 @@ // // panelTools // - panelTools.Controls.Add(maskedTextBox); + panelTools.Controls.Add(buttonDeleteObject); + panelTools.Controls.Add(listBoxStorages); + panelTools.Controls.Add(maskedTextBoxStorageName); + panelTools.Controls.Add(buttonAddObject); + panelTools.Controls.Add(maskedTextBoxNumber); panelTools.Controls.Add(buttonRefreshCollection); panelTools.Controls.Add(buttonRemoveBus); panelTools.Controls.Add(buttonAddBus); @@ -59,16 +67,53 @@ panelTools.Size = new Size(174, 461); panelTools.TabIndex = 1; // - // maskedTextBox + // buttonDeleteObject // - maskedTextBox.Location = new Point(30, 160); - maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(120, 23); - maskedTextBox.TabIndex = 4; + buttonDeleteObject.Location = new Point(30, 180); + buttonDeleteObject.Name = "buttonDeleteObject"; + buttonDeleteObject.Size = new Size(120, 40); + buttonDeleteObject.TabIndex = 8; + buttonDeleteObject.Text = "Удалить набор"; + buttonDeleteObject.UseVisualStyleBackColor = true; + buttonDeleteObject.Click += buttonDeleteObject_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(30, 110); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(120, 64); + listBoxStorages.TabIndex = 7; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + // + // maskedTextBoxStorageName + // + maskedTextBoxStorageName.Location = new Point(30, 30); + maskedTextBoxStorageName.Name = "maskedTextBoxStorageName"; + maskedTextBoxStorageName.Size = new Size(120, 23); + maskedTextBoxStorageName.TabIndex = 6; + // + // buttonAddObject + // + buttonAddObject.Location = new Point(30, 60); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(120, 40); + buttonAddObject.TabIndex = 5; + buttonAddObject.Text = "Добавить набор"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += buttonAddObject_Click; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(30, 300); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(120, 23); + maskedTextBoxNumber.TabIndex = 4; // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(30, 290); + buttonRefreshCollection.Location = new Point(30, 400); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(120, 40); buttonRefreshCollection.TabIndex = 3; @@ -78,7 +123,7 @@ // // buttonRemoveBus // - buttonRemoveBus.Location = new Point(30, 190); + buttonRemoveBus.Location = new Point(30, 330); buttonRemoveBus.Name = "buttonRemoveBus"; buttonRemoveBus.Size = new Size(120, 40); buttonRemoveBus.TabIndex = 2; @@ -88,7 +133,7 @@ // // buttonAddBus // - buttonAddBus.Location = new Point(30, 60); + buttonAddBus.Location = new Point(30, 250); buttonAddBus.Name = "buttonAddBus"; buttonAddBus.Size = new Size(120, 40); buttonAddBus.TabIndex = 1; @@ -125,9 +170,13 @@ private PictureBox pictureBoxCollection; private Panel panelTools; private Label labelTools; - private MaskedTextBox maskedTextBox; + private MaskedTextBox maskedTextBoxNumber; private Button buttonRefreshCollection; private Button buttonRemoveBus; private Button buttonAddBus; + private MaskedTextBox maskedTextBoxStorageName; + private Button buttonAddObject; + private Button buttonDeleteObject; + private ListBox listBoxStorages; } } \ No newline at end of file diff --git a/AccordionBus/AccordionBus/BusCollectionForm.cs b/AccordionBus/AccordionBus/BusCollectionForm.cs index 19d3cc5..fb6c589 100644 --- a/AccordionBus/AccordionBus/BusCollectionForm.cs +++ b/AccordionBus/AccordionBus/BusCollectionForm.cs @@ -22,7 +22,7 @@ namespace AccordionBus /// /// Набор объектов /// - private readonly BusGenericCollection _buses; + private readonly BusGenericStorage _storage; /// /// Конструктор @@ -30,7 +30,76 @@ namespace AccordionBus public BusCollectionForm() { InitializeComponent(); - _buses = new BusGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new BusGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + /// + /// Заполнение ListBoxObject + /// + 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(buttonAddObject.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _storage.AddSet(maskedTextBoxStorageName.Text); + ReloadObjects(); + } + + /// + /// Выбор набора + /// + /// + /// + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBuses(); + } + + /// + /// Удаление набора + /// + /// + /// + private void buttonDeleteObject_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(); + } } /// @@ -40,13 +109,24 @@ namespace AccordionBus /// private void buttonAddBus_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + AccordionBusForm form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_buses + form.SelectedBus > -1) + if (obj + form.SelectedBus > -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _buses.ShowBuses(); + pictureBoxCollection.Image = obj.ShowBuses(); } else { @@ -62,16 +142,27 @@ namespace AccordionBus /// private void buttonRemoveBus_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 = Convert.ToInt32(maskedTextBox.Text); - if (_buses - pos) + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _buses.ShowBuses(); + pictureBoxCollection.Image = obj.ShowBuses(); } else { @@ -86,7 +177,18 @@ namespace AccordionBus /// private void buttonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _buses.ShowBuses(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + + pictureBoxCollection.Image = obj.ShowBuses(); } } } diff --git a/AccordionBus/AccordionBus/BusGenericCollection.cs b/AccordionBus/AccordionBus/BusGenericCollection.cs index 7ce53ad..6cf768d 100644 --- a/AccordionBus/AccordionBus/BusGenericCollection.cs +++ b/AccordionBus/AccordionBus/BusGenericCollection.cs @@ -81,11 +81,11 @@ namespace AccordionBus.Generics /// public static bool operator -(BusGenericCollection 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; @@ -98,7 +98,7 @@ namespace AccordionBus.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// @@ -139,7 +139,7 @@ namespace AccordionBus.Generics for (int i = 0; i < _collection.Count; i++) { // Получение объекта - var obj = _collection.Get(i); + var obj = _collection[i]; // Установка позиции obj?.SetPosition( (int)((width - 1) * _placeSizeWidth - (i % width * _placeSizeWidth)), diff --git a/AccordionBus/AccordionBus/BusGenericStorage.cs b/AccordionBus/AccordionBus/BusGenericStorage.cs new file mode 100644 index 0000000..63634a1 --- /dev/null +++ b/AccordionBus/AccordionBus/BusGenericStorage.cs @@ -0,0 +1,92 @@ +using AccordionBus.Drawings; +using AccordionBus.Generics; +using AccordionBus.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccordionBus +{ + /// + /// Класс для хранения коллекции + /// + internal class BusGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _busStorages; + + /// + /// Возвращение списка названий наборов + /// + public List Keys => _busStorages.Keys.ToList(); + + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + + /// + /// Конструктор + /// + /// Ширина картинки + /// Высота картинки + public BusGenericStorage(int pictureWidth, int pictureHeight) + { + _busStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + _busStorages.Add(name, new BusGenericCollection(_pictureWidth, _pictureHeight)); + } + + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + // Проверка наличия ключа + if (!_busStorages.ContainsKey(name)) + { + return; + } + + _busStorages.Remove(name); + } + + /// + /// Доступ к набору + /// + /// + /// + public BusGenericCollection? this[string ind] + { + get + { + // Проверка наличия ключа + if (_busStorages.ContainsKey(ind)) + { + return _busStorages[ind]; + } + + return null; + } + } + } +} diff --git a/AccordionBus/AccordionBus/SetGeneric.cs b/AccordionBus/AccordionBus/SetGeneric.cs index 504d31c..f46d2cc 100644 --- a/AccordionBus/AccordionBus/SetGeneric.cs +++ b/AccordionBus/AccordionBus/SetGeneric.cs @@ -13,14 +13,19 @@ namespace AccordionBus.Generics internal class SetGeneric 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; /// /// Конструктор @@ -28,7 +33,8 @@ namespace AccordionBus.Generics /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// @@ -50,42 +56,13 @@ namespace AccordionBus.Generics public int Insert(T bus, int position) { // Проверка позиции - if (position < 0 || position >= Count) + if (position < 0 || position >= _maxCount) { return -1; } - // Проверка, что элемент массива по этой позиции пустой - if (_places[position] != null) - { - // Проверка, что после вставляемого элемента в массиве есть пустой элемент - int index = -1; - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - index = i; - break; - } - } - - // Проверка, если пустого элемента нет - if (index == -1) - { - return -1; - } - - // Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - int j = index - 1; - while (j >= position) - { - _places[j + 1] = _places[j]; - j--; - } - } - // Вставка по позиции - _places[position] = bus; + _places.Insert(position, bus); return position; } @@ -97,13 +74,13 @@ namespace AccordionBus.Generics public bool Remove(int position) { // Проверка позиции - if (position < 0 || position >= Count) + if (position < 0 || position >= _maxCount) { return false; } - // Удаление объекта из массива, присвоив элементу массива значение null - _places[position] = null; + // Удаление объекта из списка + _places.RemoveAt(position); return true; } @@ -112,15 +89,44 @@ namespace AccordionBus.Generics /// /// Позиция /// - public T? Get(int position) + public T? this[int position] { - // Проверка позиции - if (position < 0 || position >= Count) + get { - return null; + // Проверка позиции + if (position < 0 || position >= _maxCount) + { + return null; + } + return _places[position]; } + set + { + // Проверка позиции + if (position < 0 || position >= _maxCount) + { + return; + } - return _places[position]; + _places.Insert(position, value); + } + } + + /// + /// Проход по списку + /// + /// + /// + public IEnumerable GetBuses(int? maxBuses = null) + { + for (int i = 0; i < _maxCount; i++) + { + yield return _places[i]; + if (maxBuses.HasValue && i == maxBuses.Value) + { + yield break; + } + } } } } -- 2.25.1 From faa713ee33ce30b2b29cafc05d858d037838ba6e Mon Sep 17 00:00:00 2001 From: Factorino73 Date: Tue, 24 Oct 2023 15:54:12 +0400 Subject: [PATCH 2/2] PIbd-21_MasenkinMS_LabWork04 --- AccordionBus/AccordionBus/BusCollectionForm.cs | 2 +- AccordionBus/AccordionBus/BusGenericCollection.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/AccordionBus/AccordionBus/BusCollectionForm.cs b/AccordionBus/AccordionBus/BusCollectionForm.cs index fb6c589..64dc52d 100644 --- a/AccordionBus/AccordionBus/BusCollectionForm.cs +++ b/AccordionBus/AccordionBus/BusCollectionForm.cs @@ -159,7 +159,7 @@ namespace AccordionBus } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (obj - pos != null) + if (obj - pos) { MessageBox.Show("Объект удален"); pictureBoxCollection.Image = obj.ShowBuses(); diff --git a/AccordionBus/AccordionBus/BusGenericCollection.cs b/AccordionBus/AccordionBus/BusGenericCollection.cs index 6cf768d..f01a9c9 100644 --- a/AccordionBus/AccordionBus/BusGenericCollection.cs +++ b/AccordionBus/AccordionBus/BusGenericCollection.cs @@ -85,7 +85,7 @@ namespace AccordionBus.Generics if (obj != null) { - collect._collection.Remove(pos); + return collect._collection.Remove(pos); } return false; -- 2.25.1