From 4dd8e12530079242d75a0f8dadae71ea77fca1db Mon Sep 17 00:00:00 2001 From: chtzsch ~ Date: Wed, 1 Nov 2023 12:18:13 +0300 Subject: [PATCH] lab_4 --- .../speed_Boat/BoatsGenericCollection.cs | 40 +++--- speed_Boat/speed_Boat/BoatsGenericStorage.cs | 92 ++++++++++++++ .../speed_Boat/FormBoatCollection.Designer.cs | 84 +++++++++++-- speed_Boat/speed_Boat/FormBoatCollection.cs | 114 ++++++++++++++++-- speed_Boat/speed_Boat/GenericClass.cs | 110 ++++++++++++----- 5 files changed, 373 insertions(+), 67 deletions(-) create mode 100644 speed_Boat/speed_Boat/BoatsGenericStorage.cs diff --git a/speed_Boat/speed_Boat/BoatsGenericCollection.cs b/speed_Boat/speed_Boat/BoatsGenericCollection.cs index 3a63d73..3b86da7 100644 --- a/speed_Boat/speed_Boat/BoatsGenericCollection.cs +++ b/speed_Boat/speed_Boat/BoatsGenericCollection.cs @@ -60,21 +60,21 @@ namespace speed_Boat.Generics /// /// Перегрузка оператора вычитания /// - public static bool? operator - (BoatsGenericCollection collect, int pos)//bool?? + public static T? operator - (BoatsGenericCollection collect, int pos)//bool?? { - T? obj = collect._collection.Get(pos); - if (obj == null) + T? obj = collect._collection[pos]; + if (obj != null) { - return false; + collect?._collection.Remove(pos); } - return collect?._collection.Remove(pos) ?? false; + return obj; } /// /// Получение объекта IMoveableObject /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -114,25 +114,27 @@ namespace speed_Boat.Generics /// private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) + int col = 0; + int i = 0; + int width_Col = _pictureWidth / _placeSizeWidth;//количество колонок в окне прорисовки + foreach (var boat in _collection.GetBoats()) { - int col = 0; - int width_Col = _pictureWidth / _placeSizeWidth;//количество колонок в окне прорисовки - for (int j = 0; j < _collection.Count; j++) + if(boat != null) { - DrawingBoat? boat = _collection.Get(j); - if (boat == null) - { - col++; - if (col > 2) - col = 0; - continue; - } - boat.SetPosition(col * _placeSizeWidth, j / width_Col * _placeSizeHeight); + boat.SetPosition(col * _placeSizeWidth, i / width_Col * _placeSizeHeight); col++; if (col > 2) col = 0; boat.DrawTransport(g); + i++; + } + else if(boat == null) + { + col++; + if (col > 2) + col = 0; + i++; + continue; } } } diff --git a/speed_Boat/speed_Boat/BoatsGenericStorage.cs b/speed_Boat/speed_Boat/BoatsGenericStorage.cs new file mode 100644 index 0000000..6c2b59f --- /dev/null +++ b/speed_Boat/speed_Boat/BoatsGenericStorage.cs @@ -0,0 +1,92 @@ +using speed_Boat.MovementStrategy; +using SpeedBoatLab.Drawings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace speed_Boat.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class BoatsGenericStorage + { + /// + /// Словарь(хранилище) + /// + readonly Dictionary> _boatStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _boatStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + public BoatsGenericStorage(int pictureWidth, int pictureHeight) + { + _boatStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + /// + /// Добавление набора + /// + public void AddSet(string name) + { + if (_boatStorages.ContainsKey(name)) + { + MessageBox.Show("Словарь уже содержит набор с таким названием", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + else + { + _boatStorages.Add(name, new BoatsGenericCollection(_pictureWidth, _pictureHeight)); + } + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + BoatsGenericCollection boat; + if (_boatStorages.TryGetValue(name, out boat)) + { + _boatStorages.Remove(name); + } + } + /// + /// Доступ к набору + /// + /// + /// + public BoatsGenericCollection?this[string ind] + { + get + { + BoatsGenericCollection boat; + //проверка есть ли в словаре обьект с ключом ind + if (_boatStorages.TryGetValue(ind, out boat)) + { + return boat; + } + return null; + } + } + + + + } +} diff --git a/speed_Boat/speed_Boat/FormBoatCollection.Designer.cs b/speed_Boat/speed_Boat/FormBoatCollection.Designer.cs index 5a22003..7368c60 100644 --- a/speed_Boat/speed_Boat/FormBoatCollection.Designer.cs +++ b/speed_Boat/speed_Boat/FormBoatCollection.Designer.cs @@ -30,6 +30,11 @@ namespace SpeedBoatLab private void InitializeComponent() { groupBox1 = new System.Windows.Forms.GroupBox(); + groupBox2 = new System.Windows.Forms.GroupBox(); + deleteStorageButton = new System.Windows.Forms.Button(); + storagesListBox = new System.Windows.Forms.ListBox(); + addStorageButton = new System.Windows.Forms.Button(); + nameStorageTextBox = new System.Windows.Forms.TextBox(); label1 = new System.Windows.Forms.Label(); maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); UpdateCollectionButton = new System.Windows.Forms.Button(); @@ -37,27 +42,79 @@ namespace SpeedBoatLab AddBoatButton = new System.Windows.Forms.Button(); pictureBoxCollection = new System.Windows.Forms.PictureBox(); groupBox1.SuspendLayout(); + groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // groupBox1 // + groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(label1); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(UpdateCollectionButton); groupBox1.Controls.Add(DeleteBoatButton); groupBox1.Controls.Add(AddBoatButton); - groupBox1.Location = new System.Drawing.Point(581, 12); + groupBox1.Location = new System.Drawing.Point(581, 2); groupBox1.Name = "groupBox1"; - groupBox1.Size = new System.Drawing.Size(205, 426); + groupBox1.Size = new System.Drawing.Size(205, 436); groupBox1.TabIndex = 0; groupBox1.TabStop = false; groupBox1.Text = "Настройки"; // + // groupBox2 + // + groupBox2.Controls.Add(deleteStorageButton); + groupBox2.Controls.Add(storagesListBox); + groupBox2.Controls.Add(addStorageButton); + groupBox2.Controls.Add(nameStorageTextBox); + groupBox2.Location = new System.Drawing.Point(6, 22); + groupBox2.Name = "groupBox2"; + groupBox2.Size = new System.Drawing.Size(193, 240); + groupBox2.TabIndex = 5; + groupBox2.TabStop = false; + groupBox2.Text = "Наборы"; + // + // deleteStorageButton + // + deleteStorageButton.Location = new System.Drawing.Point(6, 205); + deleteStorageButton.Name = "deleteStorageButton"; + deleteStorageButton.Size = new System.Drawing.Size(181, 29); + deleteStorageButton.TabIndex = 5; + deleteStorageButton.Text = "Удалить набор"; + deleteStorageButton.UseVisualStyleBackColor = true; + deleteStorageButton.Click += deleteStorageButton_Click; + // + // storagesListBox + // + storagesListBox.FormattingEnabled = true; + storagesListBox.ItemHeight = 20; + storagesListBox.Location = new System.Drawing.Point(6, 94); + storagesListBox.Name = "storagesListBox"; + storagesListBox.Size = new System.Drawing.Size(181, 104); + storagesListBox.TabIndex = 4; + storagesListBox.SelectedIndexChanged += storageListBox_SelectedIndexChanged; + // + // addStorageButton + // + addStorageButton.Location = new System.Drawing.Point(6, 59); + addStorageButton.Name = "addStorageButton"; + addStorageButton.Size = new System.Drawing.Size(181, 29); + addStorageButton.TabIndex = 3; + addStorageButton.Text = "Добавить набор"; + addStorageButton.UseVisualStyleBackColor = true; + addStorageButton.Click += storageAddButton_Click; + // + // nameStorageTextBox + // + nameStorageTextBox.Location = new System.Drawing.Point(6, 26); + nameStorageTextBox.Name = "nameStorageTextBox"; + nameStorageTextBox.Size = new System.Drawing.Size(181, 27); + nameStorageTextBox.TabIndex = 2; + // // label1 // label1.AutoSize = true; - label1.Location = new System.Drawing.Point(6, 85); + label1.Location = new System.Drawing.Point(6, 299); label1.Name = "label1"; label1.Size = new System.Drawing.Size(135, 20); label1.TabIndex = 4; @@ -65,16 +122,16 @@ namespace SpeedBoatLab // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new System.Drawing.Point(6, 108); + maskedTextBoxNumber.Location = new System.Drawing.Point(6, 322); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new System.Drawing.Size(193, 27); maskedTextBoxNumber.TabIndex = 3; // // UpdateCollectionButton // - UpdateCollectionButton.Location = new System.Drawing.Point(6, 194); + UpdateCollectionButton.Location = new System.Drawing.Point(6, 391); UpdateCollectionButton.Name = "UpdateCollectionButton"; - UpdateCollectionButton.Size = new System.Drawing.Size(193, 47); + UpdateCollectionButton.Size = new System.Drawing.Size(193, 29); UpdateCollectionButton.TabIndex = 2; UpdateCollectionButton.Text = "Обновить коллекцию"; UpdateCollectionButton.UseVisualStyleBackColor = true; @@ -82,9 +139,9 @@ namespace SpeedBoatLab // // DeleteBoatButton // - DeleteBoatButton.Location = new System.Drawing.Point(6, 141); + DeleteBoatButton.Location = new System.Drawing.Point(6, 355); DeleteBoatButton.Name = "DeleteBoatButton"; - DeleteBoatButton.Size = new System.Drawing.Size(193, 47); + DeleteBoatButton.Size = new System.Drawing.Size(193, 30); DeleteBoatButton.TabIndex = 1; DeleteBoatButton.Text = "Удалить Катер"; DeleteBoatButton.UseVisualStyleBackColor = true; @@ -92,9 +149,9 @@ namespace SpeedBoatLab // // AddBoatButton // - AddBoatButton.Location = new System.Drawing.Point(6, 26); + AddBoatButton.Location = new System.Drawing.Point(6, 268); AddBoatButton.Name = "AddBoatButton"; - AddBoatButton.Size = new System.Drawing.Size(193, 47); + AddBoatButton.Size = new System.Drawing.Size(193, 28); AddBoatButton.TabIndex = 0; AddBoatButton.Text = "Добавить Катер"; AddBoatButton.UseVisualStyleBackColor = true; @@ -119,6 +176,8 @@ namespace SpeedBoatLab Text = "Коллекция катеров"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); + groupBox2.ResumeLayout(false); + groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } @@ -132,5 +191,10 @@ namespace SpeedBoatLab private System.Windows.Forms.PictureBox pictureBoxCollection; private System.Windows.Forms.MaskedTextBox maskedTextBoxNumber; private System.Windows.Forms.Label label1; + private System.Windows.Forms.GroupBox groupBox2; + private System.Windows.Forms.ListBox storagesListBox; + private System.Windows.Forms.Button addStorageButton; + private System.Windows.Forms.TextBox nameStorageTextBox; + private System.Windows.Forms.Button deleteStorageButton; } } \ No newline at end of file diff --git a/speed_Boat/speed_Boat/FormBoatCollection.cs b/speed_Boat/speed_Boat/FormBoatCollection.cs index b08c0e0..caf92ac 100644 --- a/speed_Boat/speed_Boat/FormBoatCollection.cs +++ b/speed_Boat/speed_Boat/FormBoatCollection.cs @@ -19,15 +19,80 @@ namespace SpeedBoatLab /// /// Набор объектов /// - private readonly BoatsGenericCollection _boats; + private readonly BoatsGenericStorage _storage; /// /// Конструктор /// public FormBoatCollection() { InitializeComponent(); - _boats = new BoatsGenericCollection - (pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new BoatsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Заполнение collectionsListBox + /// + private void ReloadObjects() + { + int index = storagesListBox.SelectedIndex; + + storagesListBox.Items.Clear(); + + for (int i = 0; i < _storage.Keys.Count; i++) + { + storagesListBox.Items.Add(_storage.Keys[i]); + } + + if (storagesListBox.Items.Count > 0 && (index == -1 || + index >= storagesListBox.Items.Count)) + { + storagesListBox.SelectedIndex = 0; + } + else if (storagesListBox.Items.Count > 0 && index > -1 && + index < storagesListBox.Items.Count) + { + storagesListBox.SelectedIndex = index; + } + + } + + /// + /// Добавление набора в коллекцию + /// + private void storageAddButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(nameStorageTextBox.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(nameStorageTextBox.Text); + ReloadObjects(); + } + /// + /// Выбор набора + /// + private void storageListBox_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = + _storage[storagesListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowBoats(); + } + /// + /// Удаление набора + /// + private void deleteStorageButton_Click(Object sender, EventArgs e) + { + if (storagesListBox.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {storagesListBox.SelectedItem}?", + "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(storagesListBox.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); + } + } /// /// Добавление объекта в набор @@ -36,13 +101,23 @@ namespace SpeedBoatLab /// private void ButtonAddBoat_Click(object sender, EventArgs e) { + if (storagesListBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[storagesListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + FormSpeedBoat form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_boats + form.SelectedBoat != false) + if (obj + form.SelectedBoat) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _boats.ShowBoats(); + pictureBoxCollection.Image = obj.ShowBoats(); } else { @@ -57,8 +132,17 @@ namespace SpeedBoatLab /// private void ButtonRemoveBoat_Click(object sender, EventArgs e) { + if (storagesListBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[storagesListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } @@ -67,13 +151,13 @@ namespace SpeedBoatLab if (insertPosition != string.Empty) { int.TryParse(insertPosition, out pos); - if (pos < 0 || pos > _boats._collection.Count - 1) + if (pos < 0 || pos > obj._collection.Count - 1) MessageBox.Show("Неверный формат позиции"); - if (_boats - pos != false) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _boats.ShowBoats(); + pictureBoxCollection.Image = obj.ShowBoats(); } } else if (insertPosition == string.Empty) @@ -92,7 +176,17 @@ namespace SpeedBoatLab /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _boats.ShowBoats(); + if (storagesListBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[storagesListBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowBoats(); } } } diff --git a/speed_Boat/speed_Boat/GenericClass.cs b/speed_Boat/speed_Boat/GenericClass.cs index 88764e1..8ccb957 100644 --- a/speed_Boat/speed_Boat/GenericClass.cs +++ b/speed_Boat/speed_Boat/GenericClass.cs @@ -13,28 +13,56 @@ namespace speed_Boat.Generics /// /// Массив объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество обьектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// public GenericClass(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор /// public bool Insert(T boat) { - int currentI = -1; - for(int i = 0; i < _places.Length; i++) + //TODO vstavka v nachalo nabora!!! + + if(_places.Count == 0) { - if (_places[i] == null) + _places.Add(boat); + return true; + } + else + { + if (_places.Count < _maxCount) + { + _places.Add(boat); + for(int i = 0; i < _places.Count; i++) + { + T temp = _places[i]; + _places[i] = _places[_places.Count - 1]; + _places[_places.Count - 1] = temp; + } + return true; + } + } + return false; + + /*int currentI = -1; + for(int i = 0; i < _maxCount; i++)//_maxCount ili _places.Count? + { + if (_places[i] != null) { currentI = i; break; @@ -52,7 +80,7 @@ namespace speed_Boat.Generics } _places[0] = boat; - return true; + return true;*/ } /// /// Добавление объекта в набор на конкретную позицию. @@ -64,6 +92,8 @@ namespace speed_Boat.Generics { if (position < 0 || position >= Count) return false; + if (_places == null) + return false; if (_places[position] == null) { @@ -71,25 +101,18 @@ namespace speed_Boat.Generics return true; } - int currentI = -1; - for (int i = position; i < Count; i++) + if (_places.Count < _maxCount) { - if (_places[i] == null) + _places.Add(boat); + for (int i = position; i < _places.Count; i++) { - currentI = i; - break; + T temp = _places[i]; + _places[i] = _places[_places.Count - 1]; + _places[_places.Count - 1] = temp; } + return true; } - - if (currentI < 0) - return false; - - for (int i = currentI + 1; i > 0; i--) - _places[i] = _places[i - 1]; - - - _places[currentI] = boat; - return true; + return false; } /// @@ -107,17 +130,48 @@ namespace speed_Boat.Generics /// /// Получение объекта из набора по позиции /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= Count) + get { - return null; + if (position < 0 || position >= Count) + { + return null; + } + else + { + return _places[position]; + } } - else + set { - return _places[position]; + if (position < 0 || position >= Count) + { + MessageBox.Show("Позиция элемента находится вне списка"); + } + else if(_places.Count >= Count) + { + MessageBox.Show("Вставка невозможна, т.к. список заполнен"); + } + else + { + Insert(value, position); + } + } + } + /// + /// Проход по списку + /// + public IEnumerable GetBoats(int? maxBoats = null) + { + for(int i = 0; i < _places.Count; i++) + { + yield return _places[i]; + if (maxBoats.HasValue && i == maxBoats.Value) + { + yield break; + } } - } } }