From 2dbfb4694ffa17f1ff9f5b3fcbfadd63003236c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Tue, 17 Oct 2023 20:46:21 +0400 Subject: [PATCH] export --- .../FormBusCollection.Designer.cs | 154 +++++++++++++----- .../FormBusCollection.cs | 120 ++++++++++++-- .../Generics/SetGeneric.cs | 100 +++++++----- .../Generics/TheBusesGenericCollection.cs | 24 +-- .../Generics/TheBusesGenericStorage.cs | 90 ++++++++++ 5 files changed, 386 insertions(+), 102 deletions(-) create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs index d1a86e3..aa9992e 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs @@ -28,32 +28,98 @@ /// private void InitializeComponent() { - toolsLabel = new Panel(); + toolsPanel = new Panel(); + panelSets = new Panel(); + textBoxStorageName = new TextBox(); + listBoxObjects = new ListBox(); + ButtonAddObject = new Button(); + ButtonDelObject = new Button(); + SetsLabel = new Label(); ButtonRefreshCollection = new Button(); ButtonDeleteBus = new Button(); maskedTextBoxNumber = new TextBox(); - addBus = new Button(); - label1 = new Label(); + ButtonAddBus = new Button(); + LabelTools = new Label(); pictureBoxCollection = new PictureBox(); - toolsLabel.SuspendLayout(); + toolsPanel.SuspendLayout(); + panelSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // - // toolsLabel + // toolsPanel // - toolsLabel.Controls.Add(ButtonRefreshCollection); - toolsLabel.Controls.Add(ButtonDeleteBus); - toolsLabel.Controls.Add(maskedTextBoxNumber); - toolsLabel.Controls.Add(addBus); - toolsLabel.Controls.Add(label1); - toolsLabel.Location = new Point(678, -2); - toolsLabel.Name = "toolsLabel"; - toolsLabel.Size = new Size(221, 451); - toolsLabel.TabIndex = 0; + toolsPanel.Controls.Add(panelSets); + toolsPanel.Controls.Add(ButtonRefreshCollection); + toolsPanel.Controls.Add(ButtonDeleteBus); + toolsPanel.Controls.Add(maskedTextBoxNumber); + toolsPanel.Controls.Add(ButtonAddBus); + toolsPanel.Controls.Add(LabelTools); + toolsPanel.Location = new Point(662, -2); + toolsPanel.Name = "toolsPanel"; + toolsPanel.Size = new Size(221, 504); + toolsPanel.TabIndex = 0; + // + // panelSets + // + panelSets.Controls.Add(textBoxStorageName); + panelSets.Controls.Add(listBoxObjects); + panelSets.Controls.Add(ButtonAddObject); + panelSets.Controls.Add(ButtonDelObject); + panelSets.Controls.Add(SetsLabel); + panelSets.Location = new Point(8, 20); + panelSets.Name = "panelSets"; + panelSets.Size = new Size(210, 208); + panelSets.TabIndex = 6; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(10, 27); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(190, 27); + textBoxStorageName.TabIndex = 12; + // + // listBoxObjects + // + listBoxObjects.FormattingEnabled = true; + listBoxObjects.ItemHeight = 20; + listBoxObjects.Location = new Point(10, 99); + listBoxObjects.Name = "listBoxObjects"; + listBoxObjects.Size = new Size(190, 64); + listBoxObjects.TabIndex = 11; + listBoxObjects.SelectedIndexChanged += listBoxObjects_SelectedIndexChanged; + // + // ButtonAddObject + // + ButtonAddObject.Location = new Point(10, 60); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(191, 33); + ButtonAddObject.TabIndex = 8; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; + // + // ButtonDelObject + // + ButtonDelObject.Location = new Point(10, 169); + ButtonDelObject.Name = "ButtonDelObject"; + ButtonDelObject.Size = new Size(192, 33); + ButtonDelObject.TabIndex = 7; + ButtonDelObject.Text = "Удалить набор\r\n"; + ButtonDelObject.UseVisualStyleBackColor = true; + ButtonDelObject.Click += ButtonDelObject_Click; + // + // SetsLabel + // + SetsLabel.AutoSize = true; + SetsLabel.Location = new Point(3, 4); + SetsLabel.Name = "SetsLabel"; + SetsLabel.Size = new Size(66, 20); + SetsLabel.TabIndex = 0; + SetsLabel.Text = "Наборы"; // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(10, 194); + ButtonRefreshCollection.Location = new Point(8, 452); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(197, 41); ButtonRefreshCollection.TabIndex = 4; @@ -63,7 +129,7 @@ // // ButtonDeleteBus // - ButtonDeleteBus.Location = new Point(10, 121); + ButtonDeleteBus.Location = new Point(8, 379); ButtonDeleteBus.Name = "ButtonDeleteBus"; ButtonDeleteBus.Size = new Size(197, 41); ButtonDeleteBus.TabIndex = 3; @@ -73,35 +139,35 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(50, 88); + maskedTextBoxNumber.Location = new Point(48, 346); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(125, 27); maskedTextBoxNumber.TabIndex = 2; // - // addBus + // ButtonAddBus // - addBus.Location = new Point(10, 26); - addBus.Name = "addBus"; - addBus.Size = new Size(197, 41); - addBus.TabIndex = 1; - addBus.Text = "Добавить автобус"; - addBus.UseVisualStyleBackColor = true; - addBus.Click += ButtonAddBus_Click; + ButtonAddBus.Location = new Point(8, 284); + ButtonAddBus.Name = "ButtonAddBus"; + ButtonAddBus.Size = new Size(197, 41); + ButtonAddBus.TabIndex = 1; + ButtonAddBus.Text = "Добавить автобус"; + ButtonAddBus.UseVisualStyleBackColor = true; + ButtonAddBus.Click += ButtonAddBus_Click; // - // label1 + // LabelTools // - label1.AutoSize = true; - label1.Location = new Point(5, 0); - label1.Name = "label1"; - label1.Size = new Size(103, 20); - label1.TabIndex = 0; - label1.Text = "Инструменты"; + LabelTools.AutoSize = true; + LabelTools.Location = new Point(5, 0); + LabelTools.Name = "LabelTools"; + LabelTools.Size = new Size(103, 20); + LabelTools.TabIndex = 0; + LabelTools.Text = "Инструменты"; // // pictureBoxCollection // pictureBoxCollection.Location = new Point(1, -2); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(676, 451); + pictureBoxCollection.Size = new Size(663, 504); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // @@ -109,25 +175,33 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(900, 450); + ClientSize = new Size(882, 510); Controls.Add(pictureBoxCollection); - Controls.Add(toolsLabel); + Controls.Add(toolsPanel); Name = "FormBusCollection"; Text = "Набор автобусов"; - toolsLabel.ResumeLayout(false); - toolsLabel.PerformLayout(); + toolsPanel.ResumeLayout(false); + toolsPanel.PerformLayout(); + panelSets.ResumeLayout(false); + panelSets.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } #endregion - private Panel toolsLabel; - private Label label1; + private Panel toolsPanel; + private Label LabelTools; private Button ButtonDeleteBus; - private Button addBus; + private Button ButtonAddBus; private Button ButtonRefreshCollection; private PictureBox pictureBoxCollection; public TextBox maskedTextBoxNumber; + private Panel panelSets; + private Label SetsLabel; + private Button ButtonDelObject; + private Button ButtonAddObject; + private ListBox listBoxObjects; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs index 5cfbb8e..167259d 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs @@ -21,7 +21,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base /// /// Набор объектов /// - private readonly TheBusesGenericCollection _theBuses; + private readonly TheBusesGenericStorage _storage; /// /// Конструктор @@ -29,8 +29,77 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base public FormBusCollection() { InitializeComponent(); - _theBuses = new TheBusesGenericCollection - (pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new TheBusesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + int index = listBoxObjects.SelectedIndex; + + listBoxObjects.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxObjects.Items.Add(_storage.Keys[i]); + } + + if (listBoxObjects.Items.Count > 0 && (index == -1 || index >= listBoxObjects.Items.Count)) + { + listBoxObjects.SelectedIndex = 0; + } + + else if (listBoxObjects.Items.Count > 0 && index > -1 && index < listBoxObjects.Items.Count) + { + listBoxObjects.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[listBoxObjects.SelectedItem?.ToString() ?? string.Empty]?.ShowTheBuses(); + } + + /// + /// Удаление набора + /// + /// + /// + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (listBoxObjects.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект{listBoxObjects.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxObjects.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } } /// @@ -40,19 +109,29 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base /// private void ButtonAddBus_Click(object sender, EventArgs e) { + if (listBoxObjects.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } FormDoubleDeckerBus form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_theBuses + form.SelectedBus != -1) + if (obj + form.SelectedBus) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _theBuses.ShowTheBuses(); + pictureBoxCollection.Image = obj.ShowTheBuses(); } else { MessageBox.Show("Не удалось добавить объект"); } } + } /// @@ -62,17 +141,26 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base /// private void ButtonDeleteBus_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxObjects.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxObjects.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 (_theBuses - pos != true) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _theBuses.ShowTheBuses(); + pictureBoxCollection.Image = obj.ShowTheBuses(); } else { @@ -87,7 +175,17 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _theBuses.ShowTheBuses(); + if (listBoxObjects.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowTheBuses(); } } } diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs index d8ee52f..36b2cc6 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs @@ -14,14 +14,19 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в cписке /// - public int Count => _places.Length; + public int Count => _places.Count; + + /// + /// Максимальное количество объектов в cписке + /// + private readonly int _maxCount; /// /// Конструктор @@ -29,7 +34,8 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// @@ -37,9 +43,15 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// Добавляемый автобус /// - public int Insert(T bus) + public bool Insert(T bus) { - return Insert(bus, 0); + if (_places.Count == _maxCount) + { + return false; + } + + Insert(bus, 0); + return true; } /// @@ -48,36 +60,15 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// Добавляемый автобус /// Позиция /// - public int Insert(T bus, int position) + public bool Insert(T bus, int position) { - int nullIndex = -1, i; - - if (position < 0 || position >= Count) + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) { - return -1; + return false; } - for (i = position; i < Count; i++) - { - if (_places[i] == null) - { - nullIndex = i; - break; - } - } - - if (nullIndex < 0) - { - return -1; - } - - for (i = nullIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - - _places[position] = bus; - return position; + _places.Insert(position, bus); + return true; } /// @@ -92,7 +83,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics return false; } - _places[position] = null; + _places.RemoveAt(position); return true; } @@ -101,14 +92,43 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.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]; + return _places[position]; + } + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + { + return; + } + + _places.Insert(position, value); + return; + } + } + + /// + /// Проход по списку + /// + /// + public IEnumerable GetTheBuses(int? maxTheBuses = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxTheBuses.HasValue && i == maxTheBuses.Value) + { + yield break; + } + } } } -} +} \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs index 316dfdc..43a2365 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs @@ -63,13 +63,14 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// /// - public static int operator +(TheBusesGenericCollection collect, T? obj) + public static bool operator +(TheBusesGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect._collection.Insert(obj); + + return collect?._collection.Insert(obj) ?? false; } /// @@ -78,14 +79,15 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// /// - public static bool operator -(TheBusesGenericCollection collect, int pos) + public static T? operator -(TheBusesGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); } - return false; + + return obj; } /// @@ -95,7 +97,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// @@ -136,17 +138,17 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var bus in _collection.GetTheBuses()) { - DrawningBus bus = _collection.Get(i); - if (bus != null) { int inRow = _pictureWidth / _placeSizeWidth; bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight); bus.DrawTransport(g); } + i++; } } } -} +} \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs new file mode 100644 index 0000000..e30548c --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs @@ -0,0 +1,90 @@ +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class TheBusesGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _busStorages; + + /// + /// Возвращение списка названий наборов + /// + public List Keys => _busStorages.Keys.ToList(); + + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + + /// + /// Конструктор + /// + /// + /// + public TheBusesGenericStorage(int pictureWidth, int pictureHeight) + { + _busStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + _busStorages.Add(name, new TheBusesGenericCollection(_pictureWidth, _pictureHeight)); + } + + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_busStorages.ContainsKey(name)) + { + return; + } + + _busStorages.Remove(name); + } + + /// + /// Доступ к набору + /// + /// + /// + public TheBusesGenericCollection? this[string ind] + { + get + { + if (_busStorages.ContainsKey(ind)) + { + return _busStorages[ind]; + } + + return null; + } + } + } +} -- 2.25.1