From 5b6e6256ff9aef01c00950a913b0ef50913595ef Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sat, 21 Oct 2023 17:51:21 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Trolleybus/BusesGenericCollection.cs | 29 ++--- Trolleybus/Trolleybus/BusesGenericStorage.cs | 77 +++++++++++++ .../Trolleybus/FormBusCollection.Designer.cs | 74 +++++++++++- Trolleybus/Trolleybus/FormBusCollection.cs | 107 ++++++++++++++---- Trolleybus/Trolleybus/SetGeneric.cs | 70 +++++++----- 5 files changed, 288 insertions(+), 69 deletions(-) create mode 100644 Trolleybus/Trolleybus/BusesGenericStorage.cs diff --git a/Trolleybus/Trolleybus/BusesGenericCollection.cs b/Trolleybus/Trolleybus/BusesGenericCollection.cs index 08dcc19..f438fd7 100644 --- a/Trolleybus/Trolleybus/BusesGenericCollection.cs +++ b/Trolleybus/Trolleybus/BusesGenericCollection.cs @@ -52,11 +52,11 @@ namespace ProjectTrolleybus.Generics /// /// /// - public static int operator +(BusesGenericCollection? collect, T? obj) + public static bool operator +(BusesGenericCollection? collect, T? obj) { if (obj == null || collect == null) - return -1; - return collect._collection.Insert(obj); + return false; + return collect?._collection.Insert(obj) ?? false; } /// /// Перегрузка оператора вычитания @@ -64,12 +64,12 @@ namespace ProjectTrolleybus.Generics /// /// /// - public static bool operator -(BusesGenericCollection collect, int pos) + public static T? operator -(BusesGenericCollection collect, int pos) { - T? obj = collect?._collection.Get(pos); - if (obj == null || collect == null) - return false; - return collect._collection.Remove(pos); + T? obj = collect._collection[pos]; + if (obj != null) + collect._collection.Remove(pos); + return obj; } /// /// Получение объекта IMoveableObject @@ -78,7 +78,7 @@ namespace ProjectTrolleybus.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -118,15 +118,16 @@ namespace ProjectTrolleybus.Generics /// private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var bus in _collection.GetBuses()) { - DrawingBus? trolleybus = _collection.Get(i); - if (trolleybus != null) + if (bus != null) { int inRow = _pictureWidth / _placeSizeWidth; - trolleybus.SetPosition(_placeSizeWidth * (inRow - 1) - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight); - trolleybus.DrawTransport(g); + bus.SetPosition(_placeSizeWidth * (inRow - 1) - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight); + bus.DrawTransport(g); } + i++; } } } diff --git a/Trolleybus/Trolleybus/BusesGenericStorage.cs b/Trolleybus/Trolleybus/BusesGenericStorage.cs new file mode 100644 index 0000000..4ebcbb5 --- /dev/null +++ b/Trolleybus/Trolleybus/BusesGenericStorage.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTrolleybus.DrawingObjects; +using ProjectTrolleybus.MovementStrategy; + +namespace ProjectTrolleybus.Generics +{ + internal class BusesGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _busStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _busStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public BusesGenericStorage(int pictureWidth, int pictureHeight) + { + _busStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + _busStorages.Add(name, new BusesGenericCollection(_pictureWidth, _pictureHeight)); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_busStorages.ContainsKey(name)) + return; + _busStorages.Remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public BusesGenericCollection? + this[string ind] + { + get + { + if (_busStorages.ContainsKey(ind)) + return _busStorages[ind]; + return null; + } + } + } +} diff --git a/Trolleybus/Trolleybus/FormBusCollection.Designer.cs b/Trolleybus/Trolleybus/FormBusCollection.Designer.cs index 27d737c..9366e27 100644 --- a/Trolleybus/Trolleybus/FormBusCollection.Designer.cs +++ b/Trolleybus/Trolleybus/FormBusCollection.Designer.cs @@ -29,17 +29,24 @@ private void InitializeComponent() { groupBoxTrolleybus = new GroupBox(); + groupBoxSets = new GroupBox(); + textBoxStorageName = new TextBox(); + buttonDelObject = new Button(); + listBoxStorages = new ListBox(); + buttonAddObject = new Button(); buttonUpdateCollection = new Button(); buttonDeleteBus = new Button(); maskedTextBoxNumber = new MaskedTextBox(); buttonAddBus = new Button(); pictureBoxCollection = new PictureBox(); groupBoxTrolleybus.SuspendLayout(); + groupBoxSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // groupBoxTrolleybus // + groupBoxTrolleybus.Controls.Add(groupBoxSets); groupBoxTrolleybus.Controls.Add(buttonUpdateCollection); groupBoxTrolleybus.Controls.Add(buttonDeleteBus); groupBoxTrolleybus.Controls.Add(maskedTextBoxNumber); @@ -51,9 +58,61 @@ groupBoxTrolleybus.TabStop = false; groupBoxTrolleybus.Text = "Инструменты"; // + // groupBoxSets + // + groupBoxSets.Anchor = AnchorStyles.Top | AnchorStyles.Right; + groupBoxSets.Controls.Add(textBoxStorageName); + groupBoxSets.Controls.Add(buttonDelObject); + groupBoxSets.Controls.Add(listBoxStorages); + groupBoxSets.Controls.Add(buttonAddObject); + groupBoxSets.Location = new Point(6, 22); + groupBoxSets.Name = "groupBoxSets"; + groupBoxSets.Size = new Size(245, 234); + groupBoxSets.TabIndex = 4; + groupBoxSets.TabStop = false; + groupBoxSets.Text = "Наборы"; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(6, 22); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(233, 23); + textBoxStorageName.TabIndex = 4; + // + // buttonDelObject + // + buttonDelObject.Location = new Point(6, 194); + buttonDelObject.Name = "buttonDelObject"; + buttonDelObject.Size = new Size(233, 30); + buttonDelObject.TabIndex = 3; + buttonDelObject.Text = "Удалить набор"; + buttonDelObject.UseVisualStyleBackColor = true; + buttonDelObject.Click += ButtonDelObject_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(6, 94); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(233, 94); + listBoxStorages.TabIndex = 2; + listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; + // + // buttonAddObject + // + buttonAddObject.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonAddObject.Location = new Point(6, 62); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(233, 26); + buttonAddObject.TabIndex = 1; + buttonAddObject.Text = "Добавить набор"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += ButtonAddObject_Click; + // // buttonUpdateCollection // - buttonUpdateCollection.Location = new Point(10, 250); + buttonUpdateCollection.Location = new Point(10, 408); buttonUpdateCollection.Name = "buttonUpdateCollection"; buttonUpdateCollection.Size = new Size(241, 28); buttonUpdateCollection.TabIndex = 3; @@ -63,7 +122,7 @@ // // buttonDeleteBus // - buttonDeleteBus.Location = new Point(10, 198); + buttonDeleteBus.Location = new Point(10, 343); buttonDeleteBus.Name = "buttonDeleteBus"; buttonDeleteBus.Size = new Size(241, 29); buttonDeleteBus.TabIndex = 2; @@ -73,7 +132,7 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(74, 86); + maskedTextBoxNumber.Location = new Point(70, 305); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(115, 23); maskedTextBoxNumber.TabIndex = 1; @@ -81,7 +140,7 @@ // buttonAddBus // buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonAddBus.Location = new Point(10, 22); + buttonAddBus.Location = new Point(10, 262); buttonAddBus.Name = "buttonAddBus"; buttonAddBus.Size = new Size(241, 28); buttonAddBus.TabIndex = 0; @@ -110,6 +169,8 @@ Text = "Набор автобусов"; groupBoxTrolleybus.ResumeLayout(false); groupBoxTrolleybus.PerformLayout(); + groupBoxSets.ResumeLayout(false); + groupBoxSets.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } @@ -122,5 +183,10 @@ private Button buttonUpdateCollection; private Button buttonDeleteBus; private PictureBox pictureBoxCollection; + private GroupBox groupBoxSets; + private Button buttonDelObject; + private ListBox listBoxStorages; + private Button buttonAddObject; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/Trolleybus/Trolleybus/FormBusCollection.cs b/Trolleybus/Trolleybus/FormBusCollection.cs index e48b547..f79cdde 100644 --- a/Trolleybus/Trolleybus/FormBusCollection.cs +++ b/Trolleybus/Trolleybus/FormBusCollection.cs @@ -15,27 +15,78 @@ namespace ProjectTrolleybus { public partial class FormBusCollection : Form { - private readonly BusesGenericCollection _buses; - + private readonly BusesGenericStorage _storage; public FormBusCollection() { InitializeComponent(); - _buses = new BusesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new BusesGenericStorage(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]?.ShowBuses(); + } + 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 ButtonAddBus_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FormTrolleybus form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_buses + form.SelectedBus != -1) + if (obj + form.SelectedBus) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _buses.ShowBuses(); + pictureBoxCollection.Image = obj.ShowBuses(); } else { @@ -43,37 +94,47 @@ namespace ProjectTrolleybus } } } - /// - /// Удаление объекта из набора - /// - /// - /// 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(maskedTextBoxNumber.Text); - if (_buses - pos) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _buses.ShowBuses(); + pictureBoxCollection.Image = obj.ShowBuses(); } else { MessageBox.Show("Не удалось удалить объект"); } } - /// - /// Обновление рисунка по набору - /// - /// - /// 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/Trolleybus/Trolleybus/SetGeneric.cs b/Trolleybus/Trolleybus/SetGeneric.cs index eff0865..b09e533 100644 --- a/Trolleybus/Trolleybus/SetGeneric.cs +++ b/Trolleybus/Trolleybus/SetGeneric.cs @@ -9,54 +9,68 @@ namespace ProjectTrolleybus.Generics internal class SetGeneric where T : class { - private readonly T?[] _places; + private readonly List _places; - public int Count => _places.Length; + private readonly int _maxCount; + + public int Count => _places.Count; public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } - public int Insert(T trolleybus) + public bool Insert(T trolleybus) { - if (_places[Count - 1] != null) - return -1; - return Insert(trolleybus, 0); + if (_places.Count == _maxCount) + return false; + Insert(trolleybus, 0); + return true; } - public int Insert(T trolleybus, int position) + public bool Insert(T trolleybus, 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]; - } + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + return false; + _places.Insert(position, trolleybus); _places[position] = trolleybus; - return position; + return true; } 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; } - public T? Get(int position) + public T? this[int position] { - if (!(position >= 0 && position < Count)) - return null; - return _places[position]; + get + { + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + return; + _places.Insert(position, value); + } + } + public IEnumerable GetBuses(int? maxBuses = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxBuses.HasValue && i == maxBuses.Value) + { + yield break; + } + } } - } } -- 2.25.1 From 38a949a2ca814a7397b3ebf98aafe20ae0f3b6f5 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sat, 28 Oct 2023 00:28:20 +0400 Subject: [PATCH 2/2] It Done --- Trolleybus/Trolleybus/SetGeneric.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Trolleybus/Trolleybus/SetGeneric.cs b/Trolleybus/Trolleybus/SetGeneric.cs index b09e533..44bc8f6 100644 --- a/Trolleybus/Trolleybus/SetGeneric.cs +++ b/Trolleybus/Trolleybus/SetGeneric.cs @@ -34,7 +34,6 @@ namespace ProjectTrolleybus.Generics if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) return false; _places.Insert(position, trolleybus); - _places[position] = trolleybus; return true; } -- 2.25.1