From ccf9eb19d8e4f720c2ec6addc12149154f1d186e Mon Sep 17 00:00:00 2001 From: "safiulova.k" Date: Mon, 6 Nov 2023 18:06:51 +0400 Subject: [PATCH] =?UTF-8?q?4=20=D0=BB=D0=B0=D0=B1=D0=B0=20=D0=B3=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/Catamaran/Catamaran/Catamaran.cs | 8 +- .../Catamaran/CatamaransGenericCollection.cs | 6 +- .../Catamaran/CatamaransGenericStorage.cs | 7 + .../FormCatamaranCollection.Designer.cs | 84 ++++++++++-- .../Catamaran/FormCatamaranCollection.cs | 124 ++++++++++++++++-- base/Catamaran/Catamaran/SetGeneric.cs | 55 +++----- 6 files changed, 222 insertions(+), 62 deletions(-) diff --git a/base/Catamaran/Catamaran/Catamaran.cs b/base/Catamaran/Catamaran/Catamaran.cs index 5549617..6eaeb63 100644 --- a/base/Catamaran/Catamaran/Catamaran.cs +++ b/base/Catamaran/Catamaran/Catamaran.cs @@ -19,7 +19,7 @@ namespace Catamaran private AbstractStrategy? _strategy; /// - /// + /// /// public DrawningCatamaran? SelectedCatamaran { get; private set; } @@ -83,7 +83,7 @@ namespace Catamaran } /// - /// + /// /// /// /// @@ -106,7 +106,7 @@ namespace Catamaran } /// - /// + /// /// /// /// @@ -175,7 +175,7 @@ namespace Catamaran } } /// - /// + /// /// /// /// diff --git a/base/Catamaran/Catamaran/CatamaransGenericCollection.cs b/base/Catamaran/Catamaran/CatamaransGenericCollection.cs index eee7e4d..f421270 100644 --- a/base/Catamaran/Catamaran/CatamaransGenericCollection.cs +++ b/base/Catamaran/Catamaran/CatamaransGenericCollection.cs @@ -56,13 +56,13 @@ namespace Catamaran.Generics /// /// /// - public static int operator +(CatamaransGenericCollection collect, T? obj) + public static bool operator +(CatamaransGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect._collection.Insert(obj); + return collect?._collection.Insert(obj) ?? false; } /// /// Перегрузка оператора вычитания diff --git a/base/Catamaran/Catamaran/CatamaransGenericStorage.cs b/base/Catamaran/Catamaran/CatamaransGenericStorage.cs index 0d7b8d2..39c92da 100644 --- a/base/Catamaran/Catamaran/CatamaransGenericStorage.cs +++ b/base/Catamaran/Catamaran/CatamaransGenericStorage.cs @@ -43,6 +43,8 @@ namespace Catamaran.Generics public void AddSet(string name) { // TODO Прописать логику для добавления + if (_catamaranStorages.ContainsKey(name)) return; + _catamaranStorages[name] = new CatamaransGenericCollection(_pictureWidth, _pictureHeight); } /// /// Удаление набора @@ -51,6 +53,9 @@ namespace Catamaran.Generics public void DelSet(string name) { // TODO Прописать логику для удаления + if (!_catamaranStorages.ContainsKey(name)) + return; + _catamaranStorages.Remove(name); } /// /// Доступ к набору @@ -63,6 +68,8 @@ namespace Catamaran.Generics get { // TODO Продумать логику получения набора + if (_catamaranStorages.ContainsKey(ind)) + return _catamaranStorages[ind]; return null; } } diff --git a/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs b/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs index c133790..238e6f2 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs @@ -30,24 +30,31 @@ { this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.groupBoxSets = new System.Windows.Forms.GroupBox(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.buttonDelObject = new System.Windows.Forms.Button(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.buttonAddObject = new System.Windows.Forms.Button(); this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); this.ButtonRefreshCollection = new System.Windows.Forms.Button(); this.ButtonRemoveCatamaran = new System.Windows.Forms.Button(); this.ButtonAddCatamaran = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.groupBoxTools.SuspendLayout(); + this.groupBoxSets.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection // this.pictureBoxCollection.Location = new System.Drawing.Point(0, 1); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(741, 459); + this.pictureBoxCollection.Size = new System.Drawing.Size(736, 459); this.pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabStop = false; // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.groupBoxSets); this.groupBoxTools.Controls.Add(this.maskedTextBoxNumber); this.groupBoxTools.Controls.Add(this.ButtonRefreshCollection); this.groupBoxTools.Controls.Add(this.ButtonRemoveCatamaran); @@ -59,18 +66,68 @@ this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // groupBoxSets + // + this.groupBoxSets.Controls.Add(this.textBoxStorageName); + this.groupBoxSets.Controls.Add(this.buttonDelObject); + this.groupBoxSets.Controls.Add(this.listBoxStorages); + this.groupBoxSets.Controls.Add(this.buttonAddObject); + this.groupBoxSets.Location = new System.Drawing.Point(3, 21); + this.groupBoxSets.Name = "groupBoxSets"; + this.groupBoxSets.Size = new System.Drawing.Size(138, 245); + this.groupBoxSets.TabIndex = 4; + this.groupBoxSets.TabStop = false; + this.groupBoxSets.Text = "Наборы"; + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(3, 36); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(134, 23); + this.textBoxStorageName.TabIndex = 4; + // + // buttonDelObject + // + this.buttonDelObject.Location = new System.Drawing.Point(3, 209); + this.buttonDelObject.Name = "buttonDelObject"; + this.buttonDelObject.Size = new System.Drawing.Size(134, 27); + this.buttonDelObject.TabIndex = 3; + this.buttonDelObject.Text = "Удалить набор"; + this.buttonDelObject.UseVisualStyleBackColor = true; + this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); + // + // listBoxStorages + // + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 15; + this.listBoxStorages.Location = new System.Drawing.Point(3, 102); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(134, 94); + this.listBoxStorages.TabIndex = 2; + this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); + // + // buttonAddObject + // + this.buttonAddObject.Location = new System.Drawing.Point(3, 65); + this.buttonAddObject.Name = "buttonAddObject"; + this.buttonAddObject.Size = new System.Drawing.Size(134, 25); + this.buttonAddObject.TabIndex = 1; + this.buttonAddObject.Text = "Добавить набор"; + this.buttonAddObject.UseVisualStyleBackColor = true; + this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(0, 108); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(3, 321); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(141, 23); + this.maskedTextBoxNumber.Size = new System.Drawing.Size(137, 23); this.maskedTextBoxNumber.TabIndex = 3; // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(0, 211); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(0, 420); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - this.ButtonRefreshCollection.Size = new System.Drawing.Size(141, 34); + this.ButtonRefreshCollection.Size = new System.Drawing.Size(140, 34); this.ButtonRefreshCollection.TabIndex = 2; this.ButtonRefreshCollection.Text = "Обновить коллекцию"; this.ButtonRefreshCollection.UseVisualStyleBackColor = true; @@ -78,9 +135,9 @@ // // ButtonRemoveCatamaran // - this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(0, 157); + this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(0, 363); this.ButtonRemoveCatamaran.Name = "ButtonRemoveCatamaran"; - this.ButtonRemoveCatamaran.Size = new System.Drawing.Size(141, 34); + this.ButtonRemoveCatamaran.Size = new System.Drawing.Size(140, 34); this.ButtonRemoveCatamaran.TabIndex = 1; this.ButtonRemoveCatamaran.Text = "Удалить катамаран"; this.ButtonRemoveCatamaran.UseVisualStyleBackColor = true; @@ -88,9 +145,9 @@ // // ButtonAddCatamaran // - this.ButtonAddCatamaran.Location = new System.Drawing.Point(0, 22); + this.ButtonAddCatamaran.Location = new System.Drawing.Point(3, 270); this.ButtonAddCatamaran.Name = "ButtonAddCatamaran"; - this.ButtonAddCatamaran.Size = new System.Drawing.Size(141, 34); + this.ButtonAddCatamaran.Size = new System.Drawing.Size(137, 34); this.ButtonAddCatamaran.TabIndex = 0; this.ButtonAddCatamaran.Text = "Добавить катамаран"; this.ButtonAddCatamaran.UseVisualStyleBackColor = true; @@ -104,10 +161,12 @@ this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.pictureBoxCollection); this.Name = "FormCatamaranCollection"; - this.Text = "FormCatamaranCollection"; + this.Text = "Набор катамаранов"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); + this.groupBoxSets.ResumeLayout(false); + this.groupBoxSets.PerformLayout(); this.ResumeLayout(false); } @@ -120,5 +179,10 @@ private Button ButtonRefreshCollection; private Button ButtonRemoveCatamaran; private MaskedTextBox maskedTextBoxNumber; + private GroupBox groupBoxSets; + private Button buttonAddObject; + private Button buttonDelObject; + private ListBox listBoxStorages; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/base/Catamaran/Catamaran/FormCatamaranCollection.cs b/base/Catamaran/Catamaran/FormCatamaranCollection.cs index 3db931c..5710d91 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.cs +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.cs @@ -12,17 +12,87 @@ namespace Catamaran /// /// Набор объектов /// - private readonly CatamaransGenericCollection _catamarans; + private readonly CatamaransGenericStorage _storage; + /// /// Конструктор /// public FormCatamaranCollection() { InitializeComponent(); - _catamarans = new CatamaransGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new CatamaransGenericStorage(pictureBoxCollection.Width,pictureBoxCollection.Height); } + + /// + /// Заполнение listBoxStorages + /// + 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]?.ShowCatamarans(); + } + /// + /// Удаление набора + /// + /// + /// + 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(); + } + } + /// /// Добавление объекта в набор /// @@ -30,13 +100,23 @@ namespace Catamaran /// private void ButtonAddCatamaran_Click(object sender, EventArgs e) { - FormCatamaran form = new(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + + FormCatamaran form = new FormCatamaran(); if (form.ShowDialog() == DialogResult.OK) { - if (_catamarans + form.SelectedCatamaran != -1) + if (obj + form.SelectedCatamaran) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _catamarans.ShowCatamarans(); + pictureBoxCollection.Image = obj.ShowCatamarans(); } else { @@ -46,27 +126,38 @@ namespace Catamaran } /// /// Удаление объекта из набора - /// + /// listBoxStorages /// /// private void ButtonRemoveCatamaran_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 (_catamarans - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _catamarans.ShowCatamarans(); + pictureBoxCollection.Image = obj.ShowCatamarans(); } else { MessageBox.Show("Не удалось удалить объект"); } } + /// /// Обновление рисунка по набору /// @@ -75,7 +166,18 @@ namespace Catamaran private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _catamarans.ShowCatamarans(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowCatamarans(); + } } } diff --git a/base/Catamaran/Catamaran/SetGeneric.cs b/base/Catamaran/Catamaran/SetGeneric.cs index be8a789..64ee575 100644 --- a/base/Catamaran/Catamaran/SetGeneric.cs +++ b/base/Catamaran/Catamaran/SetGeneric.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; using System.Threading.Tasks; @@ -18,7 +19,7 @@ namespace Catamaran.Generics /// private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// public int Count => _places.Count; /// @@ -32,17 +33,22 @@ namespace Catamaran.Generics public SetGeneric(int count) { _maxCount = count; - _places = new List(count); + _places = new List(_maxCount); } /// /// Добавление объекта в набор /// /// Добавляемый катамаран /// - public int Insert(T catamaran) + public bool Insert(T catamaran) { // TODO вставка в начало набора - return Insert(catamaran, 0); + if (_places.Count == _maxCount) + { + return false; + } + Insert(catamaran, 0); + return true; } /// /// Добавление объекта в набор на конкретную позицию @@ -50,36 +56,20 @@ namespace Catamaran.Generics /// Добавляемый катамаран /// Позиция /// - public int Insert(T catamaran, int position) + public bool Insert(T catamaran, int position) { // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, //если нет, то проверка, что после вставляемого элемента в массиве есть пустой элемент // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента // TODO вставка по позиции - int nullIndex = -1, i; - - if (position < 0 || position >= Count) - return -1; - - for (i = position; i < Count; i++) + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) { - if (_places[i] == null) - { - nullIndex = i; - break; - } - } - if (nullIndex < 0) - return -1; - - for (i = nullIndex; i > position; i--) - { - _places[i] = _places[i - 1]; + return false; } - _places[position] = catamaran; - return position; + _places.Insert(position, catamaran); + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -90,9 +80,11 @@ namespace Catamaran.Generics { // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива значение null - if (position < 0 || position >= Count) { - return false; } - _places[position] = null; + if (position < 0 || position >= Count) + { + return false; + } + _places.RemoveAt(position); return true; } /// @@ -105,10 +97,8 @@ namespace Catamaran.Generics get { // TODO проверка позиции - if (position < 0 || position > _maxCount) - { + if (!(position >= 0 && position < Count)) return null; - } return _places[position]; } set @@ -117,10 +107,7 @@ namespace Catamaran.Generics // TODO проверка свободных мест в списке // TODO вставка в список по позиции if (!(position >= 0 && position < Count && _places.Count < _maxCount)) - { return; - } - _places.Insert(position, value); return; }